diff options
| author | Tannin <devnull@localhost> | 2015-06-10 22:09:32 +0200 |
|---|---|---|
| committer | Tannin <devnull@localhost> | 2015-06-10 22:09:32 +0200 |
| commit | 793dfb1637322c60bcbf32fdc5a9c23bb7ed5ec4 (patch) | |
| tree | 29fb382b16bc6987132701efd46691302ef8800d /src | |
| parent | d25a81071c078585f14176283b89e1a6b0ad5795 (diff) | |
added plugin interface to add/remove/list categories to/from/of mods
Diffstat (limited to 'src')
| -rw-r--r-- | src/categories.cpp | 32 | ||||
| -rw-r--r-- | src/categories.h | 8 | ||||
| -rw-r--r-- | src/modinfo.cpp | 34 | ||||
| -rw-r--r-- | src/modinfo.h | 4 |
4 files changed, 73 insertions, 5 deletions
diff --git a/src/categories.cpp b/src/categories.cpp index 0b201dcf..400cc74b 100644 --- a/src/categories.cpp +++ b/src/categories.cpp @@ -166,22 +166,32 @@ void CategoryFactory::saveCategories() unsigned int CategoryFactory::countCategories(std::tr1::function<bool (const Category &category)> filter)
{
unsigned int result = 0;
- for (auto iter = m_Categories.begin(); iter != m_Categories.end(); ++iter) {
- if (filter(*iter)) {
+ for (const Category &cat : m_Categories) {
+ if (filter(cat)) {
++result;
}
}
return result;
}
+int CategoryFactory::addCategory(const QString &name, const std::vector<int> &nexusIDs, int parentID)
+{
+ int id = 1;
+ while (m_IDMap.find(id) != m_IDMap.end()) {
+ ++id;
+ }
+ addCategory(id, name, nexusIDs, parentID);
+
+ saveCategories();
+ return id;
+}
void CategoryFactory::addCategory(int id, const QString &name, const std::vector<int> &nexusIDs, int parentID)
{
int index = m_Categories.size();
m_Categories.push_back(Category(index, id, name, nexusIDs, parentID));
- for (std::vector<int>::const_iterator iter = nexusIDs.begin();
- iter != nexusIDs.end(); ++iter) {
- m_NexusMap[*iter] = index;
+ for (int nexusID : nexusIDs) {
+ m_NexusMap[nexusID] = index;
}
m_IDMap[id] = index;
}
@@ -305,6 +315,18 @@ int CategoryFactory::getCategoryIndex(int ID) const }
+int CategoryFactory::getCategoryID(const QString &name) const
+{
+ auto iter = std::find_if(m_Categories.begin(), m_Categories.end(), [name] (const Category &cat) -> bool {
+ return cat.m_Name == name;
+ });
+ if (iter != m_Categories.end()) {
+ return iter->m_ID;
+ } else {
+ return -1;
+ }
+}
+
unsigned int CategoryFactory::resolveNexusID(int nexusID) const
{
diff --git a/src/categories.h b/src/categories.h index 96e436e5..f2a5de39 100644 --- a/src/categories.h +++ b/src/categories.h @@ -80,6 +80,8 @@ public: **/
void saveCategories();
+ int addCategory(const QString &name, const std::vector<int> &nexusIDs, int parentID);
+
/**
* @brief retrieve the number of available categories
*
@@ -143,6 +145,12 @@ public: int getCategoryID(unsigned int index) const;
/**
+ * @brief look up the id of a category by its name
+ * @note O(n)
+ */
+ int getCategoryID(const QString &name) const;
+
+ /**
* @brief look up the index of a category by its id
*
* @param id index of the category to look up
diff --git a/src/modinfo.cpp b/src/modinfo.cpp index 3fa42da9..526ec62b 100644 --- a/src/modinfo.cpp +++ b/src/modinfo.cpp @@ -303,6 +303,40 @@ void ModInfo::setVersion(const VersionInfo &version) m_Version = version; } +void ModInfo::addCategory(const QString &categoryName) +{ + int id = CategoryFactory::instance().getCategoryID(categoryName); + if (id == -1) { + id = CategoryFactory::instance().addCategory(categoryName, std::vector<int>(), 0); + } + setCategory(id, true); +} + +bool ModInfo::removeCategory(const QString &categoryName) +{ + int id = CategoryFactory::instance().getCategoryID(categoryName); + if (id == -1) { + return false; + } + if (!categorySet(id)) { + return false; + } + setCategory(id, false); + return true; +} + +QStringList ModInfo::categories() +{ + QStringList result; + + CategoryFactory &catFac = CategoryFactory::instance(); + for (int id : m_Categories) { + result.append(catFac.getCategoryName(catFac.getCategoryIndex(id))); + } + + return result; +} + bool ModInfo::hasFlag(ModInfo::EFlag flag) const { std::vector<EFlag> flags = getFlags(); diff --git a/src/modinfo.h b/src/modinfo.h index 8c8d2baf..ba248bdb 100644 --- a/src/modinfo.h +++ b/src/modinfo.h @@ -300,6 +300,10 @@ public: */
virtual void addNexusCategory(int categoryID) = 0;
+ virtual void addCategory(const QString &categoryName) override;
+ virtual bool removeCategory(const QString &categoryName) override;
+ virtual QStringList categories() override;
+
/**
* update the endorsement state for the mod. This only changes the
* buffered state, it does not sync with Nexus
|
