1 | |
2 | /* |
3 | * infokcmmodel.h |
4 | * |
5 | * Copyright (C) 2010 David Hubner <hubnerd@ntlworld.com> |
6 | * |
7 | * This program is free software; you can redistribute it and/or modify |
8 | * it under the terms of the GNU General Public License as published by |
9 | * the Free Software Foundation; either version 2 of the License, or |
10 | * (at your option) any later version. |
11 | * |
12 | * This program is distributed in the hope that it will be useful, |
13 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
14 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
15 | * GNU General Public License for more details. |
16 | * |
17 | * You should have received a copy of the GNU General Public License |
18 | * along with this program; if not, write to the Free Software |
19 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. |
20 | * |
21 | */ |
22 | |
23 | //Local |
24 | #include "infokcmmodel.h" |
25 | #include "kcmcategoryitem.h" |
26 | |
27 | //KDE |
28 | #include <KServiceTypeTrader> |
29 | #include <KDebug> |
30 | |
31 | InfoKcmModel::InfoKcmModel(QObject *parent) : QAbstractItemModel(parent), m_root(new KcmCategoryItem(i18n("Information Modules" ))) |
32 | { |
33 | createTreeItems(); |
34 | } |
35 | |
36 | InfoKcmModel::~InfoKcmModel() |
37 | { |
38 | delete m_root; |
39 | } |
40 | |
41 | void InfoKcmModel::createTreeItems() |
42 | { |
43 | KService::List categoryList = KServiceTypeTrader::self()->query("KInfoCenterCategory" ); |
44 | foreach(const KService::Ptr &categoryModule, categoryList) |
45 | { |
46 | m_root->addChild(new KcmCategoryItem(categoryModule,m_root)); |
47 | } |
48 | |
49 | KService::List moduleList = KServiceTypeTrader::self()->query("KCModule" , "[X-KDE-ParentApp] == 'kinfocenter'" ); |
50 | foreach(const KService::Ptr &kcmModule, moduleList) |
51 | { |
52 | if (kcmModule->isType(KST_KService) == true) |
53 | { |
54 | QString category = kcmModule->property("X-KDE-KInfoCenter-Category" ).toString().trimmed(); |
55 | if(!category.isEmpty() || !category.isNull()) |
56 | { |
57 | KcmTreeItem *item = m_root->containsCategory(category); |
58 | if(item != NULL) |
59 | { |
60 | item->addChild(new KcmTreeItem(kcmModule,item)); |
61 | } |
62 | else |
63 | { |
64 | KcmTreeItem *lost = m_root->containsCategory("lost_and_found" ); |
65 | if(lost != NULL) |
66 | { |
67 | lost->addChild(new KcmTreeItem(kcmModule,lost)); |
68 | } |
69 | else |
70 | { |
71 | kWarning() << "Lost and found category not found, unable to display lost Kcontrol modules" ; |
72 | } |
73 | } |
74 | } |
75 | else |
76 | { |
77 | m_root->addChild(new KcmTreeItem(kcmModule,m_root)); |
78 | } |
79 | } |
80 | } |
81 | } |
82 | |
83 | QModelIndex InfoKcmModel::index(int row, int column, const QModelIndex &parent) const |
84 | { |
85 | KcmTreeItem *parentItem; |
86 | |
87 | if (!parent.isValid()) |
88 | { |
89 | parentItem = m_root; |
90 | } |
91 | else |
92 | { |
93 | parentItem = static_cast<KcmTreeItem*>(parent.internalPointer()); |
94 | } |
95 | |
96 | KcmTreeItem *childItem = parentItem->child(row); |
97 | |
98 | if (childItem) |
99 | { |
100 | return createIndex(row, column, childItem); |
101 | } |
102 | else |
103 | { |
104 | return QModelIndex(); |
105 | } |
106 | } |
107 | |
108 | QModelIndex InfoKcmModel::index(int row, int column, KcmTreeItem *parent) const |
109 | { |
110 | KcmTreeItem *childItem = parent->child(row); |
111 | |
112 | if (childItem) |
113 | { |
114 | return createIndex(row, column, childItem); |
115 | } |
116 | else |
117 | { |
118 | return QModelIndex(); |
119 | } |
120 | } |
121 | |
122 | QModelIndex InfoKcmModel::parent(const QModelIndex &index) const |
123 | { |
124 | if (!index.isValid()) |
125 | { |
126 | return QModelIndex(); |
127 | } |
128 | |
129 | KcmTreeItem *child = static_cast<KcmTreeItem*>(index.internalPointer()); |
130 | KcmTreeItem *parent = child->parent(); |
131 | |
132 | if (parent == m_root) |
133 | { |
134 | return QModelIndex(); |
135 | } |
136 | |
137 | return createIndex(parent->row(), 0, parent); |
138 | } |
139 | |
140 | QModelIndex InfoKcmModel::indexOf(KcmTreeItem *item) |
141 | { |
142 | QModelIndex tmpIndex = createIndex(item->row(), 0, item); |
143 | |
144 | if(!tmpIndex.isValid()) |
145 | { |
146 | return QModelIndex(); |
147 | } |
148 | return tmpIndex; |
149 | } |
150 | |
151 | int InfoKcmModel::rowCount(const QModelIndex &parent) const |
152 | { |
153 | KcmTreeItem *parentItem; |
154 | |
155 | if (!parent.isValid()) |
156 | { |
157 | parentItem = m_root; |
158 | } |
159 | else |
160 | { |
161 | parentItem = static_cast<KcmTreeItem*>(parent.internalPointer()); |
162 | } |
163 | |
164 | return parentItem->childCount(); |
165 | } |
166 | |
167 | int InfoKcmModel::columnCount(const QModelIndex &parent) const |
168 | { |
169 | // Hard coded, menu should never have more than one column |
170 | |
171 | Q_UNUSED(parent); |
172 | return 1; |
173 | } |
174 | |
175 | QVariant InfoKcmModel::data(const QModelIndex &index, int role) const |
176 | { |
177 | if (!index.isValid()) |
178 | { |
179 | return QVariant(); |
180 | } |
181 | |
182 | KcmTreeItem *item = static_cast<KcmTreeItem*>(index.internalPointer()); |
183 | switch(role) |
184 | { |
185 | case Qt::DisplayRole: |
186 | return item->data(); |
187 | break; |
188 | case Qt::UserRole: |
189 | return item->weight(); |
190 | break; |
191 | case Qt::DecorationRole: |
192 | return item->icon(); |
193 | break; |
194 | default: |
195 | return QVariant(); |
196 | } |
197 | return QVariant(); |
198 | } |
199 | |
200 | QVariant InfoKcmModel::(int section, Qt::Orientation orientation, int role) const |
201 | { |
202 | Q_UNUSED(section); |
203 | |
204 | if (orientation == Qt::Horizontal && role == Qt::DisplayRole) |
205 | { |
206 | return m_root->data(); |
207 | } |
208 | |
209 | return QVariant(); |
210 | } |
211 | |
212 | Qt::ItemFlags InfoKcmModel::flags(const QModelIndex &index) const |
213 | { |
214 | if (!index.isValid()) |
215 | { |
216 | return Qt::ItemIsEnabled; |
217 | } |
218 | |
219 | return Qt::ItemIsEnabled | Qt::ItemIsSelectable; |
220 | } |
221 | |
222 | QModelIndex InfoKcmModel::firstValid() const |
223 | { |
224 | int rows = m_root->childCount(); |
225 | |
226 | //Massive large number to max compare |
227 | unsigned int winner = 0; winner--; |
228 | |
229 | QModelIndex winnerIndex = QModelIndex(); |
230 | |
231 | for(int i=0;i<rows;i++) |
232 | { |
233 | KcmTreeItem *item = m_root->child(i); |
234 | if(item->type() == KcmTreeItem::KCM) |
235 | { |
236 | if(winner >= (unsigned int)item->weight()) |
237 | { |
238 | winner = item->weight(); |
239 | winnerIndex = index(item->row(),0,item->parent()); |
240 | } |
241 | } |
242 | } |
243 | return winnerIndex; |
244 | } |
245 | |
246 | QStringList InfoKcmModel::allChildrenKeywords() |
247 | { |
248 | return childrenKeywords(m_root); |
249 | } |
250 | |
251 | QStringList InfoKcmModel::childrenKeywords(KcmTreeItem *kcmItem) |
252 | { |
253 | QStringList childKeywords; |
254 | |
255 | int rows = kcmItem->childCount(); |
256 | for(int i=0;i<rows;i++) |
257 | { |
258 | KcmTreeItem *item = kcmItem->child(i); |
259 | if(item->type() == KcmTreeItem::CATEGORY) |
260 | { |
261 | childKeywords = childKeywords + childrenKeywords(item); |
262 | } |
263 | else |
264 | { |
265 | childKeywords = childKeywords + item->keywords(); |
266 | } |
267 | } |
268 | return childKeywords; |
269 | } |
270 | |