Changeset View
Changeset View
Standalone View
Standalone View
src/sycoca/vfolder_menu.cpp
Show First 20 Lines • Show All 108 Lines • ▼ Show 20 Line(s) | 108 | { | |||
---|---|---|---|---|---|
109 | if (itemList.contains(menuId)) { | 109 | if (itemList.contains(menuId)) { | ||
110 | printf("%s: %s INCL %d EXCL %d\n", qPrintable(menuName), qPrintable(comment), includeList.contains(menuId) ? 1 : 0, excludeList.contains(menuId) ? 1 : 0); | 110 | printf("%s: %s INCL %d EXCL %d\n", qPrintable(menuName), qPrintable(comment), includeList.contains(menuId) ? 1 : 0, excludeList.contains(menuId) ? 1 : 0); | ||
111 | } | 111 | } | ||
112 | } | 112 | } | ||
113 | 113 | | |||
114 | void | 114 | void | ||
115 | VFolderMenu::includeItems(QHash<QString, KService::Ptr> &items1, const QHash<QString, KService::Ptr> &items2) | 115 | VFolderMenu::includeItems(QHash<QString, KService::Ptr> &items1, const QHash<QString, KService::Ptr> &items2) | ||
116 | { | 116 | { | ||
117 | foreach (const KService::Ptr &p, items2) { | 117 | for (const KService::Ptr &p : items2) { | ||
118 | items1.insert(p->menuId(), p); | 118 | items1.insert(p->menuId(), p); | ||
119 | } | 119 | } | ||
120 | } | 120 | } | ||
121 | 121 | | |||
122 | void | 122 | void | ||
123 | VFolderMenu::matchItems(QHash<QString, KService::Ptr> &items1, const QHash<QString, KService::Ptr> &items2) | 123 | VFolderMenu::matchItems(QHash<QString, KService::Ptr> &items1, const QHash<QString, KService::Ptr> &items2) | ||
124 | { | 124 | { | ||
125 | foreach (const KService::Ptr &p, items1) { | 125 | const QHash<QString, KService::Ptr> tmpItems1 = items1; | ||
126 | for (const KService::Ptr &p : tmpItems1) { | ||||
dfaure: This makes me realize that this code was indeed detaching a copy of items1.
Good catch.
But… | |||||
126 | QString id = p->menuId(); | 127 | QString id = p->menuId(); | ||
127 | if (!items2.contains(id)) { | 128 | if (!items2.contains(id)) { | ||
128 | items1.remove(id); | 129 | items1.remove(id); | ||
129 | } | 130 | } | ||
130 | } | 131 | } | ||
131 | } | 132 | } | ||
132 | 133 | | |||
133 | void | 134 | void | ||
134 | VFolderMenu::excludeItems(QHash<QString, KService::Ptr> &items1, const QHash<QString, KService::Ptr> &items2) | 135 | VFolderMenu::excludeItems(QHash<QString, KService::Ptr> &items1, const QHash<QString, KService::Ptr> &items2) | ||
135 | { | 136 | { | ||
136 | foreach (const KService::Ptr &p, items2) { | 137 | for (const KService::Ptr &p : items2) { | ||
137 | items1.remove(p->menuId()); | 138 | items1.remove(p->menuId()); | ||
138 | } | 139 | } | ||
139 | } | 140 | } | ||
140 | 141 | | |||
141 | VFolderMenu::SubMenu * | 142 | VFolderMenu::SubMenu * | ||
142 | VFolderMenu::takeSubMenu(SubMenu *parentMenu, const QString &menuName) | 143 | VFolderMenu::takeSubMenu(SubMenu *parentMenu, const QString &menuName) | ||
143 | { | 144 | { | ||
144 | const int i = menuName.indexOf(QLatin1Char('/')); | 145 | const int i = menuName.indexOf(QLatin1Char('/')); | ||
▲ Show 20 Lines • Show All 76 Lines • ▼ Show 20 Line(s) | |||||
221 | void | 222 | void | ||
222 | VFolderMenu::insertSubMenu(SubMenu *parentMenu, const QString &menuName, SubMenu *newMenu, bool reversePriority) | 223 | VFolderMenu::insertSubMenu(SubMenu *parentMenu, const QString &menuName, SubMenu *newMenu, bool reversePriority) | ||
223 | { | 224 | { | ||
224 | const int i = menuName.indexOf(QLatin1Char('/')); | 225 | const int i = menuName.indexOf(QLatin1Char('/')); | ||
225 | const QString s1 = menuName.left(i); | 226 | const QString s1 = menuName.left(i); | ||
226 | const QString s2 = menuName.mid(i + 1); | 227 | const QString s2 = menuName.mid(i + 1); | ||
227 | 228 | | |||
228 | // Look up menu | 229 | // Look up menu | ||
229 | foreach (SubMenu *menu, parentMenu->subMenus) { | 230 | for (SubMenu *menu : qAsConst(parentMenu->subMenus)) { | ||
230 | if (menu->name == s1) { | 231 | if (menu->name == s1) { | ||
231 | if (i == -1) { | 232 | if (i == -1) { | ||
232 | mergeMenu(menu, newMenu, reversePriority); | 233 | mergeMenu(menu, newMenu, reversePriority); | ||
233 | return; | 234 | return; | ||
234 | } else { | 235 | } else { | ||
235 | insertSubMenu(menu, s2, newMenu, reversePriority); | 236 | insertSubMenu(menu, s2, newMenu, reversePriority); | ||
236 | return; | 237 | return; | ||
237 | } | 238 | } | ||
Show All 21 Lines | 258 | if (i == -1) { | |||
259 | parentMenu->items.insert(newService->menuId(), newService); | 260 | parentMenu->items.insert(newService->menuId(), newService); | ||
260 | return; | 261 | return; | ||
261 | } | 262 | } | ||
262 | 263 | | |||
263 | QString s1 = name.left(i); | 264 | QString s1 = name.left(i); | ||
264 | QString s2 = name.mid(i + 1); | 265 | QString s2 = name.mid(i + 1); | ||
265 | 266 | | |||
266 | // Look up menu | 267 | // Look up menu | ||
267 | foreach (SubMenu *menu, parentMenu->subMenus) { | 268 | for (SubMenu *menu : qAsConst(parentMenu->subMenus)) { | ||
268 | if (menu->name == s1) { | 269 | if (menu->name == s1) { | ||
269 | insertService(menu, s2, newService); | 270 | insertService(menu, s2, newService); | ||
270 | return; | 271 | return; | ||
271 | } | 272 | } | ||
272 | } | 273 | } | ||
273 | 274 | | |||
274 | SubMenu *menu = new SubMenu; | 275 | SubMenu *menu = new SubMenu; | ||
275 | menu->name = s1; | 276 | menu->name = s1; | ||
Show All 15 Lines | |||||
291 | 292 | | |||
292 | VFolderMenu::~VFolderMenu() | 293 | VFolderMenu::~VFolderMenu() | ||
293 | { | 294 | { | ||
294 | qDeleteAll(m_appsInfoList); | 295 | qDeleteAll(m_appsInfoList); | ||
295 | delete m_rootMenu; | 296 | delete m_rootMenu; | ||
296 | } | 297 | } | ||
297 | 298 | | |||
298 | #define FOR_ALL_APPLICATIONS(it) \ | 299 | #define FOR_ALL_APPLICATIONS(it) \ | ||
299 | foreach (AppsInfo *info, m_appsInfoStack) \ | 300 | for (AppsInfo *info : qAsConst(m_appsInfoStack)) \ | ||
300 | { \ | 301 | { \ | ||
301 | QHashIterator<QString,KService::Ptr> it = info->applications; \ | 302 | QHashIterator<QString,KService::Ptr> it = info->applications; \ | ||
302 | while (it.hasNext()) \ | 303 | while (it.hasNext()) \ | ||
303 | { \ | 304 | { \ | ||
304 | it.next(); | 305 | it.next(); | ||
305 | #define FOR_ALL_APPLICATIONS_END } } | 306 | #define FOR_ALL_APPLICATIONS_END } } | ||
306 | 307 | | |||
307 | #define FOR_CATEGORY(category, it) \ | 308 | #define FOR_CATEGORY(category, it) \ | ||
308 | foreach (AppsInfo *info, m_appsInfoStack) \ | 309 | for (AppsInfo *info : qAsConst(m_appsInfoStack)) \ | ||
309 | { \ | 310 | { \ | ||
310 | const KService::List list = info->dictCategories.value(category); \ | 311 | const KService::List list = info->dictCategories.value(category); \ | ||
311 | for(KService::List::ConstIterator it = list.constBegin(); \ | 312 | for(KService::List::ConstIterator it = list.constBegin(); \ | ||
312 | it != list.constEnd(); ++it) \ | 313 | it != list.constEnd(); ++it) \ | ||
313 | { | 314 | { | ||
314 | #define FOR_CATEGORY_END } } | 315 | #define FOR_CATEGORY_END } } | ||
315 | 316 | | |||
316 | KService::Ptr | 317 | KService::Ptr | ||
317 | VFolderMenu::findApplication(const QString &relPath) | 318 | VFolderMenu::findApplication(const QString &relPath) | ||
318 | { | 319 | { | ||
319 | foreach (AppsInfo *info, m_appsInfoStack) { | 320 | for (AppsInfo *info : qAsConst(m_appsInfoStack)) { | ||
320 | if (info->applications.contains(relPath)) { | 321 | if (info->applications.contains(relPath)) { | ||
321 | KService::Ptr s = info->applications[relPath]; | 322 | KService::Ptr s = info->applications[relPath]; | ||
322 | if (s) { | 323 | if (s) { | ||
323 | return s; | 324 | return s; | ||
324 | } | 325 | } | ||
325 | } | 326 | } | ||
326 | } | 327 | } | ||
327 | return KService::Ptr(); | 328 | return KService::Ptr(); | ||
328 | } | 329 | } | ||
329 | 330 | | |||
330 | void | 331 | void | ||
331 | VFolderMenu::addApplication(const QString &id, KService::Ptr service) | 332 | VFolderMenu::addApplication(const QString &id, KService::Ptr service) | ||
332 | { | 333 | { | ||
333 | service->setMenuId(id); | 334 | service->setMenuId(id); | ||
334 | m_appsInfo->applications.insert(id, service); // replaces, if already there | 335 | m_appsInfo->applications.insert(id, service); // replaces, if already there | ||
335 | m_serviceFactory->addEntry(KSycocaEntry::Ptr(service)); | 336 | m_serviceFactory->addEntry(KSycocaEntry::Ptr(service)); | ||
336 | } | 337 | } | ||
337 | 338 | | |||
338 | void | 339 | void | ||
339 | VFolderMenu::buildApplicationIndex(bool unusedOnly) | 340 | VFolderMenu::buildApplicationIndex(bool unusedOnly) | ||
340 | { | 341 | { | ||
341 | foreach (AppsInfo *info, m_appsInfoList) { | 342 | for (AppsInfo *info : qAsConst(m_appsInfoList)) { | ||
342 | info->dictCategories.clear(); | 343 | info->dictCategories.clear(); | ||
343 | QMutableHashIterator<QString, KService::Ptr> it = info->applications; | 344 | QMutableHashIterator<QString, KService::Ptr> it = info->applications; | ||
344 | while (it.hasNext()) { | 345 | while (it.hasNext()) { | ||
345 | KService::Ptr s = it.next().value(); | 346 | KService::Ptr s = it.next().value(); | ||
346 | if (unusedOnly && m_usedAppsDict.contains(s->menuId())) { | 347 | if (unusedOnly && m_usedAppsDict.contains(s->menuId())) { | ||
347 | // Remove and skip this one | 348 | // Remove and skip this one | ||
348 | it.remove(); | 349 | it.remove(); | ||
349 | continue; | 350 | continue; | ||
350 | } | 351 | } | ||
351 | 352 | | |||
352 | Q_FOREACH (const QString &cat, s->categories()) { | 353 | const auto categories = s->categories(); | ||
354 | for (const QString &cat : categories) { | ||||
353 | info->dictCategories[cat].append(s); // find or insert entry in hash | 355 | info->dictCategories[cat].append(s); // find or insert entry in hash | ||
354 | } | 356 | } | ||
355 | } | 357 | } | ||
356 | } | 358 | } | ||
357 | } | 359 | } | ||
358 | 360 | | |||
359 | void | 361 | void | ||
360 | VFolderMenu::createAppsInfo() | 362 | VFolderMenu::createAppsInfo() | ||
▲ Show 20 Lines • Show All 239 Lines • ▼ Show 20 Line(s) | 587 | } else if (e.tagName() == QLatin1String("MergeFile")) { | |||
600 | docElem.removeChild(last); // Remove the MergeFile node | 602 | docElem.removeChild(last); // Remove the MergeFile node | ||
601 | continue; | 603 | continue; | ||
602 | } else if (e.tagName() == QLatin1String("MergeDir")) { | 604 | } else if (e.tagName() == QLatin1String("MergeDir")) { | ||
603 | QString dir = absoluteDir(e.text(), e.attribute(QStringLiteral("__BaseDir")), true); | 605 | QString dir = absoluteDir(e.text(), e.attribute(QStringLiteral("__BaseDir")), true); | ||
604 | Q_ASSERT(dir.endsWith(QLatin1Char('/'))); | 606 | Q_ASSERT(dir.endsWith(QLatin1Char('/'))); | ||
605 | 607 | | |||
606 | const bool relative = QDir::isRelativePath(dir); | 608 | const bool relative = QDir::isRelativePath(dir); | ||
607 | const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericConfigLocation, QStringLiteral("menus/") + dir, QStandardPaths::LocateDirectory); | 609 | const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericConfigLocation, QStringLiteral("menus/") + dir, QStandardPaths::LocateDirectory); | ||
608 | Q_FOREACH (const QString &menuDir, dirs) { | 610 | for (const QString &menuDir : dirs) { | ||
609 | registerDirectory(menuDir); | 611 | registerDirectory(menuDir); | ||
610 | } | 612 | } | ||
611 | 613 | | |||
612 | QStringList fileList; | 614 | QStringList fileList; | ||
613 | Q_FOREACH (const QString &menuDir, dirs) { | 615 | for (const QString &menuDir : dirs) { | ||
614 | const QStringList fileNames = QDir(menuDir).entryList(QStringList() << QStringLiteral("*.menu")); | 616 | const QStringList fileNames = QDir(menuDir).entryList(QStringList() << QStringLiteral("*.menu")); | ||
615 | Q_FOREACH (const QString &file, fileNames) { | 617 | for (const QString &file : fileNames) { | ||
616 | const QString fileToAdd = relative ? dir + file : menuDir + file; | 618 | const QString fileToAdd = relative ? dir + file : menuDir + file; | ||
617 | if (!fileList.contains(fileToAdd)) { | 619 | if (!fileList.contains(fileToAdd)) { | ||
618 | fileList.append(fileToAdd); | 620 | fileList.append(fileToAdd); | ||
619 | } | 621 | } | ||
620 | } | 622 | } | ||
621 | } | 623 | } | ||
622 | 624 | | |||
623 | Q_FOREACH (const QString &file, fileList) { | 625 | for (const QString &file : qAsConst(fileList)) { | ||
624 | pushDocInfo(file); | 626 | pushDocInfo(file); | ||
625 | mergeFile(docElem, n); | 627 | mergeFile(docElem, n); | ||
626 | popDocInfo(); | 628 | popDocInfo(); | ||
627 | } | 629 | } | ||
628 | 630 | | |||
629 | QDomNode last = n; | 631 | QDomNode last = n; | ||
630 | n = n.nextSibling(); | 632 | n = n.nextSibling(); | ||
631 | docElem.removeChild(last); // Remove the MergeDir node | 633 | docElem.removeChild(last); // Remove the MergeDir node | ||
Show All 14 Lines | |||||
646 | } | 648 | } | ||
647 | n = n.nextSibling(); | 649 | n = n.nextSibling(); | ||
648 | } | 650 | } | ||
649 | } | 651 | } | ||
650 | 652 | | |||
651 | static QString makeRelative(const QString &dir) | 653 | static QString makeRelative(const QString &dir) | ||
652 | { | 654 | { | ||
653 | const QString canonical = QDir(dir).canonicalPath(); | 655 | const QString canonical = QDir(dir).canonicalPath(); | ||
654 | Q_FOREACH (const QString &base, QStandardPaths::locateAll(QStandardPaths::GenericConfigLocation, QStringLiteral("menus"), QStandardPaths::LocateDirectory)) { | 656 | const auto list = QStandardPaths::locateAll(QStandardPaths::GenericConfigLocation, QStringLiteral("menus"), QStandardPaths::LocateDirectory); | ||
657 | for (const QString &base : list) { | ||||
655 | if (canonical.startsWith(base)) { | 658 | if (canonical.startsWith(base)) { | ||
656 | return canonical.mid(base.length() + 1); | 659 | return canonical.mid(base.length() + 1); | ||
657 | } | 660 | } | ||
658 | } | 661 | } | ||
659 | return dir; | 662 | return dir; | ||
660 | } | 663 | } | ||
661 | 664 | | |||
662 | void | 665 | void | ||
▲ Show 20 Lines • Show All 376 Lines • ▼ Show 20 Line(s) | 1019 | while (!n.isNull()) { | |||
1039 | n = n.nextSibling(); | 1042 | n = n.nextSibling(); | ||
1040 | } | 1043 | } | ||
1041 | 1044 | | |||
1042 | // Setup current menu entry | 1045 | // Setup current menu entry | ||
1043 | if (pass == 0) { | 1046 | if (pass == 0) { | ||
1044 | m_currentMenu = nullptr; | 1047 | m_currentMenu = nullptr; | ||
1045 | // Look up menu | 1048 | // Look up menu | ||
1046 | if (parentMenu) { | 1049 | if (parentMenu) { | ||
1047 | foreach (SubMenu *menu, parentMenu->subMenus) { | 1050 | for (SubMenu *menu : qAsConst(parentMenu->subMenus)) { | ||
1048 | if (menu->name == name) { | 1051 | if (menu->name == name) { | ||
1049 | m_currentMenu = menu; | 1052 | m_currentMenu = menu; | ||
1050 | break; | 1053 | break; | ||
1051 | } | 1054 | } | ||
1052 | } | 1055 | } | ||
1053 | } | 1056 | } | ||
1054 | 1057 | | |||
1055 | if (!m_currentMenu) { // Not found? | 1058 | if (!m_currentMenu) { // Not found? | ||
Show All 18 Lines | |||||
1074 | } | 1077 | } | ||
1075 | m_currentMenu->isDeleted = isDeleted; | 1078 | m_currentMenu->isDeleted = isDeleted; | ||
1076 | 1079 | | |||
1077 | m_currentMenu->defaultLayoutNode = defaultLayoutNode; | 1080 | m_currentMenu->defaultLayoutNode = defaultLayoutNode; | ||
1078 | m_currentMenu->layoutNode = layoutNode; | 1081 | m_currentMenu->layoutNode = layoutNode; | ||
1079 | } else { | 1082 | } else { | ||
1080 | // Look up menu | 1083 | // Look up menu | ||
1081 | if (parentMenu) { | 1084 | if (parentMenu) { | ||
1082 | foreach (SubMenu *menu, parentMenu->subMenus) { | 1085 | for (SubMenu *menu : qAsConst(parentMenu->subMenus)) { | ||
1083 | if (menu->name == name) { | 1086 | if (menu->name == name) { | ||
1084 | m_currentMenu = menu; | 1087 | m_currentMenu = menu; | ||
1085 | break; | 1088 | break; | ||
1086 | } | 1089 | } | ||
1087 | } | 1090 | } | ||
1088 | } else { | 1091 | } else { | ||
1089 | m_currentMenu = m_rootMenu; | 1092 | m_currentMenu = m_rootMenu; | ||
1090 | } | 1093 | } | ||
▲ Show 20 Lines • Show All 270 Lines • ▼ Show 20 Line(s) | 1363 | if (menu->layoutNode.isNull()) { | |||
1361 | menu->layoutList = defaultLayout; | 1364 | menu->layoutList = defaultLayout; | ||
1362 | } else { | 1365 | } else { | ||
1363 | menu->layoutList = parseLayoutNode(menu->layoutNode); | 1366 | menu->layoutList = parseLayoutNode(menu->layoutNode); | ||
1364 | if (menu->layoutList.isEmpty()) { | 1367 | if (menu->layoutList.isEmpty()) { | ||
1365 | menu->layoutList = defaultLayout; | 1368 | menu->layoutList = defaultLayout; | ||
1366 | } | 1369 | } | ||
1367 | } | 1370 | } | ||
1368 | 1371 | | |||
1369 | foreach (VFolderMenu::SubMenu *subMenu, menu->subMenus) { | 1372 | for (VFolderMenu::SubMenu *subMenu : qAsConst(menu->subMenus)) { | ||
1370 | layoutMenu(subMenu, defaultLayout); | 1373 | layoutMenu(subMenu, defaultLayout); | ||
1371 | } | 1374 | } | ||
1372 | } | 1375 | } | ||
1373 | 1376 | | |||
1374 | void | 1377 | void | ||
1375 | VFolderMenu::markUsedApplications(const QHash<QString, KService::Ptr> &items) | 1378 | VFolderMenu::markUsedApplications(const QHash<QString, KService::Ptr> &items) | ||
1376 | { | 1379 | { | ||
1377 | foreach (const KService::Ptr &p, items) { | 1380 | for (const KService::Ptr &p : items) { | ||
1378 | m_usedAppsDict.insert(p->menuId()); | 1381 | m_usedAppsDict.insert(p->menuId()); | ||
1379 | } | 1382 | } | ||
1380 | } | 1383 | } | ||
1381 | 1384 | | |||
1382 | VFolderMenu::SubMenu *VFolderMenu::parseMenu(const QString &file) | 1385 | VFolderMenu::SubMenu *VFolderMenu::parseMenu(const QString &file) | ||
1383 | { | 1386 | { | ||
1384 | m_appsInfo = nullptr; | 1387 | m_appsInfo = nullptr; | ||
1385 | 1388 | | |||
▲ Show 20 Lines • Show All 52 Lines • Show Last 20 Lines |
This makes me realize that this code was indeed detaching a copy of items1.
Good catch.
But better code would use a java-style iterator and call remove(), to avoid the copy and the extra lookup at remove time.
This can be a separate commit, this port is correct.