From b98afd5a8d22a2c4292deff4414896c96e31ec01 Mon Sep 17 00:00:00 2001 From: Matthias Koefferlein Date: Mon, 13 Mar 2017 23:55:57 +0100 Subject: [PATCH] WIP: next steps on package manager. --- src/lay/laySaltGrains.cc | 104 +++++++++++++++++++++++++++ src/lay/laySaltGrains.h | 151 +++++++++++++++++++++++++++++++++++++++ 2 files changed, 255 insertions(+) diff --git a/src/lay/laySaltGrains.cc b/src/lay/laySaltGrains.cc index 8134e3178..6e2f30f48 100644 --- a/src/lay/laySaltGrains.cc +++ b/src/lay/laySaltGrains.cc @@ -21,11 +21,115 @@ */ #include "laySaltGrains.h" +#include "tlString.h" + +#include +#include namespace lay { +SaltGrains::SaltGrains () +{ + // .. nothing yet .. +} +bool +SaltGrains::operator== (const SaltGrains &other) const +{ + return m_name == other.m_name && + m_path == other.m_path && + m_title == other.m_title && + m_collections == other.m_collections && + m_grains == other.m_grains; +} +void +SaltGrains::set_name (const std::string &n) +{ + m_name = n; +} + +void +SaltGrains::set_title (const std::string &t) +{ + m_title = t; +} + +void +SaltGrains::set_path (const std::string &p) +{ + m_path = p; +} + +void +SaltGrains::add_collection (const SaltGrains &collection) +{ + m_collections.push_back (collection); +} + +void +SaltGrains::remove_collection (collection_iterator iter) +{ + // NOTE: this is kind of inefficient, but in order to maintain the const iterator semantics this approach is required + for (collections_type::iterator i = m_collections.begin (); i != m_collections.end (); ++i) { + if (i == iter) { + m_collections.erase (i); + break; + } + } +} + +void +SaltGrains::add_grain (const SaltGrain &grain) +{ + m_grains.push_back (grain); +} + +void +SaltGrains::remove_grain (grain_iterator iter) +{ + // NOTE: this is kind of inefficient, but in order to maintain the const iterator semantics this approach is required + for (grains_type::iterator i = m_grains.begin (); i != m_grains.end (); ++i) { + if (i == iter) { + m_grains.erase (i); + break; + } + } +} + +bool +SaltGrains::is_empty () const +{ + return m_collections.empty () && m_grains.empty (); +} + +SaltGrains +SaltGrains::from_path (const std::string &path) +{ + SaltGrains grains; + + QDir dir (tl::to_qstring (path)); + QStringList entries = dir.entryList (QDir::NoFilter, QDir::Name); + for (QStringList::const_iterator e = entries.begin (); e != entries.end (); ++e) { + + std::string epath = tl::to_string (dir.absoluteFilePath (*e)); + if (SaltGrain::is_grain (epath)) { + try { + grains.add_grain (SaltGrain::from_path (epath)); + } catch (...) { + // ignore errors (TODO: what to do here?) + } + } else if (QFileInfo (tl::to_qstring (epath)).isDir ()) { + SaltGrains c = SaltGrains::from_path (epath); + if (! c.is_empty ()) { + grains.add_collection (c); + } + } + + } + + return grains; +} } diff --git a/src/lay/laySaltGrains.h b/src/lay/laySaltGrains.h index 74b60b38b..93f256c58 100644 --- a/src/lay/laySaltGrains.h +++ b/src/lay/laySaltGrains.h @@ -23,11 +23,162 @@ #ifndef HDR_laySaltGrains #define HDR_laySaltGrains +#include "laySaltGrain.h" + +#include + namespace lay { +/** + * @brief A class representing a collection of grains (packages) + * A collection can have child collections and grains (leafs). + */ +class LAY_PUBLIC SaltGrains +{ +public: + typedef std::list collections_type; + typedef collections_type::const_iterator collection_iterator; + typedef std::list grains_type; + typedef grains_type::const_iterator grain_iterator; + /** + * @brief Constructor: creates an empty collection + */ + SaltGrains (); + /** + * @brief Equality + */ + bool operator== (const SaltGrains &other) const; + + /** + * @brief Inequality + */ + bool operator!= (const SaltGrains &other) const + { + return !operator== (other); + } + + /** + * @brief Gets the name of the grain collection + * + * The name is either a plain name (a word) or a path into a collection. + * Name paths are formed using the "/" separator. "mycollection" is a plain name, + * while "mycollection/subcollection" is a collection within a collection. + */ + const std::string &name () const + { + return m_name; + } + + /** + * @brief Sets the name of the grain collection + */ + void set_name (const std::string &p); + + /** + * @brief Gets the title of the grain collection + * + * The title is a brief description that is shown in the title of the + * package manager. + */ + const std::string &title () const + { + return m_title; + } + + /** + * @brief Sets the title of the grain collection + */ + void set_title (const std::string &t); + + /** + * @brief Gets the absolute file path of the installed grain + * This is the file path to the grain folder. + */ + const std::string &path () const + { + return m_path; + } + + /** + * @brief Sets the absolute file path of the installed grain + */ + void set_path (const std::string &p); + + /** + * @brief Gets the collections which are members of this collection (begin iterator) + */ + collection_iterator begin_collections () const + { + return m_collections.begin (); + } + + /** + * @brief Gets the collections which are members of this collection (end iterator) + */ + collection_iterator end_collections () const + { + return m_collections.begin (); + } + + /** + * @brief Adds a collection to this collection + */ + void add_collection (const SaltGrains &collection); + + /** + * @brief Removes the collection given by the collection iterator + */ + void remove_collection (collection_iterator iter); + + /** + * @brief Gets the grains (leaf nodes) which are members of this collection (begin iterator) + */ + grain_iterator begin_grains () const + { + return m_grains.begin (); + } + + /** + * @brief Gets the grains (leaf nodes) which are members of this collection (end iterator) + */ + grain_iterator end_grains () const + { + return m_grains.begin (); + } + + /** + * @brief Adds a grain to this collection + */ + void add_grain (const SaltGrain &grain); + + /** + * @brief Removes the grain given by the grain iterator + */ + void remove_grain (grain_iterator iter); + + /** + * @brief Gets a value indicating whether the collection is empty + */ + bool is_empty () const; + + /** + * @brief Scan grains from a given path + * This will scan the grains found within this path and return a collection containing + * the grains from this path. + * Sub-collections are created from folders which contain grains or sub-collections. + */ + static SaltGrains from_path (const std::string &path); + +private: + std::string m_name; + std::string m_title; + std::string m_path; + collections_type m_collections; + grains_type m_grains; +}; }