1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
|
/*
Copyright (C) 2012 Sebastian Herbord. All rights reserved.
This file is part of Mod Organizer.
Mod Organizer is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
Mod Organizer is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with Mod Organizer. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef CATEGORIES_H
#define CATEGORIES_H
#include <QString>
#include <functional>
#include <map>
#include <vector>
class CategoriesDialog;
/**
* @brief Manage the available mod categories
* @warning member functions of this class currently use a wild mix of ids and indexes
*to look up categories, optimized to where the request comes from. Therefore be very
*careful which of the two you have available
**/
class CategoryFactory : public QObject
{
Q_OBJECT;
friend class CategoriesDialog;
public:
enum SpecialCategories
{
Checked = 10000,
UpdateAvailable,
HasCategory,
Conflict,
HasHiddenFiles,
Endorsed,
Backup,
Managed,
HasGameData,
HasNexusID,
Tracked
};
public:
struct NexusCategory
{
NexusCategory(const QString name, const int nexusID) : m_Name(name), m_ID(nexusID)
{}
friend bool operator==(const NexusCategory& LHS, const NexusCategory& RHS)
{
return LHS.ID() == RHS.ID();
}
friend bool operator==(const NexusCategory& LHS, const int RHS)
{
return LHS.ID() == RHS;
}
friend bool operator<(const NexusCategory& LHS, const NexusCategory& RHS)
{
return LHS.ID() < RHS.ID();
}
QString name() const { return m_Name; }
int ID() const { return m_ID; }
int categoryID() const { return m_CategoryID; }
void setCategoryID(int categoryID) { m_CategoryID = categoryID; }
private:
QString m_Name;
int m_ID;
int m_CategoryID = -1;
};
struct Category
{
Category(int sortValue, int id, const QString name, int parentID,
std::vector<NexusCategory> nexusCats)
: m_SortValue(sortValue), m_ID(id), m_Name(name), m_HasChildren(false),
m_ParentID(parentID), m_NexusCats(std::move(nexusCats))
{}
friend bool operator<(const Category& LHS, const Category& RHS)
{
return LHS.sortValue() < RHS.sortValue();
}
int sortValue() const { return m_SortValue; }
int ID() const { return m_ID; }
int parentID() const { return m_ParentID; }
QString name() const { return m_Name; }
bool hasChildren() const { return m_HasChildren; }
void setHasChildren(bool b) { m_HasChildren = b; }
private:
int m_SortValue;
int m_ID;
int m_ParentID;
QString m_Name;
std::vector<NexusCategory> m_NexusCats;
bool m_HasChildren;
};
public:
/**
* @brief reset the list of categories
**/
void reset();
/**
* @brief read categories from file
*/
void loadCategories();
/**
* @brief save the categories to the categories.dat file
**/
void saveCategories();
void setNexusCategories(const std::vector<CategoryFactory::NexusCategory>& nexusCats);
void refreshNexusCategories(CategoriesDialog* dialog);
int addCategory(const QString& name, const std::vector<NexusCategory>& nexusCats,
int parentID);
/**
* @brief retrieve the number of available categories
*
* @return unsigned int number of categories
**/
size_t numCategories() const { return m_Categories.size(); }
/**
* @brief count all categories that match a specified filter
* @param filter the filter to test
* @return number of matching categories
*/
unsigned int countCategories(std::function<bool(const Category& category)> filter);
/**
* @brief get the id of the parent category
*
* @param index the index to look up
* @return int id of the parent category
**/
int getParentID(unsigned int index) const;
/**
* @brief determine if a category exists (by id)
*
* @param id the id to check for existance
* @return true if the category exists, false otherwise
**/
bool categoryExists(int id) const;
/**
* @brief test if a category is child of a second one
* @param id the presumed child id
* @param parentID the parent id to test for
* @return true if id is a child of parentID
**/
bool isDescendantOf(int id, int parentID) const;
/**
* @brief test if the specified category has child categories
*
* @param index index of the category to look up
* @return bool true if the category has child categories
**/
bool hasChildren(unsigned int index) const;
/**
* @brief retrieve the name of a category
*
* @param index index of the category to look up
* @return QString name of the category
**/
QString getCategoryName(unsigned int index) const;
QString getSpecialCategoryName(SpecialCategories type) const;
QString getCategoryNameByID(int id) const;
/**
* @brief look up the id of a category by its index
*
* @param index index of the category to look up
* @return int id of the category
**/
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
* @return unsigned int index of the category
**/
int getCategoryIndex(int ID) const;
/**
* @brief retrieve the index of a category by its nexus id
*
* @param nexusID nexus id of the category to look up
* @return unsigned int index of the category or 0 if no category matches
**/
unsigned int resolveNexusID(int nexusID) const;
public:
/**
* @brief retrieve a reference to the singleton instance
*
* @return the reference to the singleton
**/
static CategoryFactory& instance();
/**
* @return path to the file that contains the categories list
*/
static QString categoriesFilePath();
/**
* @return path to the file that contains the nexus category mappings
*/
static QString nexusMappingFilePath();
signals:
void nexusCategoryRefresh(CategoriesDialog*);
void categoriesSaved();
private:
explicit CategoryFactory();
void loadDefaultCategories();
void addCategory(int id, const QString& name,
const std::vector<NexusCategory>& nexusCats, int parentID);
void addCategory(int id, const QString& name, int parentID);
void setParents();
static void cleanup();
private:
static CategoryFactory* s_Instance;
std::vector<Category> m_Categories;
std::map<int, unsigned int> m_IDMap;
std::map<int, NexusCategory> m_NexusMap;
private:
// called by isDescendantOf()
bool isDescendantOfImpl(int id, int parentID, std::set<int>& seen) const;
};
#endif // CATEGORIES_H
|