| 1 | /* |
| 2 | This file is part of the KDE libraries |
| 3 | SPDX-FileCopyrightText: 2003 Waldo Bastian <bastian@kde.org> |
| 4 | |
| 5 | SPDX-License-Identifier: LGPL-2.0-only |
| 6 | */ |
| 7 | |
| 8 | #ifndef VFOLDER_MENU_H |
| 9 | #define |
| 10 | |
| 11 | #include <QDomDocument> |
| 12 | #include <QHash> |
| 13 | #include <QObject> |
| 14 | #include <QSet> |
| 15 | #include <QStack> |
| 16 | #include <QStringList> |
| 17 | |
| 18 | #include <kservice.h> |
| 19 | |
| 20 | class KBuildSycocaInterface; |
| 21 | class KServiceFactory; |
| 22 | |
| 23 | class : public QObject |
| 24 | { |
| 25 | Q_OBJECT |
| 26 | public: |
| 27 | class AppsInfo; |
| 28 | class |
| 29 | { |
| 30 | public: |
| 31 | () |
| 32 | : isDeleted(false) |
| 33 | , apps_info(nullptr) |
| 34 | { |
| 35 | items.reserve(size: 43); |
| 36 | } |
| 37 | () |
| 38 | { |
| 39 | qDeleteAll(c: subMenus); |
| 40 | } |
| 41 | (const SubMenu &) = delete; |
| 42 | SubMenu &(const SubMenu &) = delete; |
| 43 | |
| 44 | public: |
| 45 | QString ; |
| 46 | QString ; |
| 47 | QList<SubMenu *> ; |
| 48 | QHash<QString, KService::Ptr> ; |
| 49 | QHash<QString, KService::Ptr> ; // Needed when merging due to Move. |
| 50 | QDomElement ; |
| 51 | QDomElement ; |
| 52 | bool ; |
| 53 | QStringList ; |
| 54 | AppsInfo *; |
| 55 | }; |
| 56 | |
| 57 | (KServiceFactory *serviceFactory, KBuildSycocaInterface *kbuildsycocaInterface); |
| 58 | () override; |
| 59 | |
| 60 | /*! |
| 61 | * Parses VFolder menu definition and generates a menu layout. |
| 62 | * The newService signals is used as callback to load |
| 63 | * a specific service description. |
| 64 | * |
| 65 | * \a file Menu file to load |
| 66 | */ |
| 67 | SubMenu *(const QString &file); |
| 68 | |
| 69 | /*! |
| 70 | * Returns a list of all directories involved in the last call to |
| 71 | * parseMenu(). |
| 72 | * |
| 73 | * A change in any of these directories or in any of their child- |
| 74 | * directories can result in changes to the menu. |
| 75 | */ |
| 76 | QStringList (); |
| 77 | |
| 78 | /*! |
| 79 | * Debug function to enable tracking of what happens with a specific |
| 80 | * menu item id |
| 81 | */ |
| 82 | void (const QString &id); |
| 83 | |
| 84 | public: |
| 85 | struct { |
| 86 | enum { , , }; |
| 87 | Type ; |
| 88 | KService::Ptr ; |
| 89 | SubMenu *; |
| 90 | }; |
| 91 | |
| 92 | public: |
| 93 | QStringList ; // A list of all the directories that we touch |
| 94 | |
| 95 | QStringList ; |
| 96 | QStringList ; |
| 97 | QStringList ; |
| 98 | |
| 99 | QStringList ; // Current set of applicable <DirectoryDir> dirs |
| 100 | QHash<QString, SubMenu *> ; // Dictionary that stores Menu nodes |
| 101 | // associated with legacy tree. |
| 102 | |
| 103 | class |
| 104 | { |
| 105 | public: |
| 106 | QString ; // Relative base dir of current menu file |
| 107 | QString ; // Filename of current menu file without ".menu" |
| 108 | QString ; // Full path of current menu file including ".menu" |
| 109 | }; |
| 110 | |
| 111 | DocInfo ; // DocInfo for current doc |
| 112 | QStack<VFolderMenu::DocInfo> ; |
| 113 | |
| 114 | class |
| 115 | { |
| 116 | public: |
| 117 | () |
| 118 | { |
| 119 | dictCategories.reserve(size: 53); |
| 120 | applications.reserve(size: 997); |
| 121 | appRelPaths.reserve(size: 997); |
| 122 | } |
| 123 | |
| 124 | () |
| 125 | { |
| 126 | } |
| 127 | |
| 128 | QHash<QString, KService::List> ; // category -> apps |
| 129 | QHash<QString, KService::Ptr> ; // rel path -> service |
| 130 | QHash<KService::Ptr, QString> ; // service -> rel path |
| 131 | }; |
| 132 | |
| 133 | AppsInfo *; // AppsInfo for current menu |
| 134 | QList<AppsInfo *> ; // All applicable AppsInfo for current menu |
| 135 | QList<AppsInfo *> ; // List of all AppsInfo objects. |
| 136 | QSet<QString /*menuId*/> ; // all applications that have been allocated |
| 137 | |
| 138 | QDomDocument ; |
| 139 | SubMenu *; |
| 140 | SubMenu *; |
| 141 | bool ; |
| 142 | QString ; |
| 143 | |
| 144 | private: |
| 145 | /*! |
| 146 | * Lookup application by relative path |
| 147 | */ |
| 148 | KService::Ptr (const QString &relPath); |
| 149 | |
| 150 | /*! |
| 151 | * Lookup applications by category |
| 152 | */ |
| 153 | QList<KService::List *> (const QString &category); |
| 154 | |
| 155 | /*! |
| 156 | * Add new application |
| 157 | */ |
| 158 | void (const QString &id, KService::Ptr service); |
| 159 | |
| 160 | /*! |
| 161 | * Build application indices |
| 162 | */ |
| 163 | void (bool unusedOnly); |
| 164 | |
| 165 | /*! |
| 166 | * Create a AppsInfo frame for current menu |
| 167 | */ |
| 168 | void (); |
| 169 | |
| 170 | /*! |
| 171 | * Load additional AppsInfo frame for current menu |
| 172 | */ |
| 173 | void (); |
| 174 | |
| 175 | /*! |
| 176 | * Unload additional AppsInfo frame for current menu |
| 177 | */ |
| 178 | void (); |
| 179 | |
| 180 | QDomDocument (); |
| 181 | void (QDomElement &docElem, QString &name); |
| 182 | void (QDomElement &docElem, const QDomNode &mergeHere); |
| 183 | void (const QString &filename); |
| 184 | |
| 185 | /*! |
| 186 | * Merge the items2 set into the items1 set |
| 187 | */ |
| 188 | void (QHash<QString, KService::Ptr> &items1, const QHash<QString, KService::Ptr> &items2); |
| 189 | |
| 190 | /*! |
| 191 | * Remove all items from the items1 set that aren't also in the items2 set |
| 192 | */ |
| 193 | void (QHash<QString, KService::Ptr> &items1, const QHash<QString, KService::Ptr> &items2); |
| 194 | |
| 195 | /*! |
| 196 | * Remove all items in the items2 set from the items1 set |
| 197 | */ |
| 198 | void (QHash<QString, KService::Ptr> &items1, const QHash<QString, KService::Ptr> &items2); |
| 199 | |
| 200 | /*! |
| 201 | * Search the parentMenu tree for the menu menuName and takes it |
| 202 | * out. |
| 203 | * |
| 204 | * This function returns a pointer to the menu if it was found |
| 205 | * or \c nullptr if it was not found. |
| 206 | */ |
| 207 | SubMenu *(SubMenu *, const QString &); |
| 208 | |
| 209 | /*! |
| 210 | * Insert the menu newMenu with name menuName into the parentMenu. |
| 211 | * If such menu already exist the result is merged, if any additional |
| 212 | * submenus are required they are created. |
| 213 | * If reversePriority is false, newMenu has priority over the existing |
| 214 | * menu during merging. |
| 215 | * If reversePriority is true, the existing menu has priority over newMenu |
| 216 | * during merging. |
| 217 | */ |
| 218 | void (VFolderMenu::SubMenu *, const QString &, VFolderMenu::SubMenu *, bool reversePriority = false); |
| 219 | |
| 220 | /*! |
| 221 | * Merge menu2 and its submenus into menu1 and its submenus |
| 222 | * If reversePriority is false, menu2 has priority over menu1 |
| 223 | * If reversePriority is true, menu1 has priority over menu2 |
| 224 | */ |
| 225 | void (SubMenu *, SubMenu *, bool reversePriority = false); |
| 226 | |
| 227 | /*! |
| 228 | * Inserts service into the menu using name relative to parentMenu |
| 229 | * Any missing sub-menus are created. |
| 230 | */ |
| 231 | void (SubMenu *, const QString &name, KService::Ptr newService); |
| 232 | |
| 233 | /*! |
| 234 | * Register the directory that \a file is in. |
| 235 | * \sa allDirectories() |
| 236 | */ |
| 237 | void (const QString &file); |
| 238 | |
| 239 | /*! |
| 240 | * Fill m_usedAppsDict with all applications from \a items |
| 241 | */ |
| 242 | void (const QHash<QString, KService::Ptr> &items); |
| 243 | |
| 244 | /*! |
| 245 | * Register \a directory |
| 246 | * \sa allDirectories() |
| 247 | */ |
| 248 | void (const QString &directory); |
| 249 | |
| 250 | void (const QString &dir, const QString &relDir, const QString &prefix); |
| 251 | void (QDomElement &docElem, int pass); |
| 252 | void (VFolderMenu::SubMenu *, QStringList defaultLayout); |
| 253 | void (QDomElement &docElem, QHash<QString, KService::Ptr> &items); |
| 254 | |
| 255 | void (); |
| 256 | |
| 257 | void (const QString &fileName, const QString &baseDir = QString()); |
| 258 | void (const QString &basePath, const QString &baseDir); |
| 259 | void (); |
| 260 | |
| 261 | QString (const QString &_dir, const QString &baseDir, bool keepRelativeToCfg = false); |
| 262 | QString (const QString &fileName); |
| 263 | QString (const QString &fileName); |
| 264 | void (const QString &, const QString &); |
| 265 | QStringList (const QDomElement &docElem) const; |
| 266 | |
| 267 | private: |
| 268 | KServiceFactory *; |
| 269 | KBuildSycocaInterface *; |
| 270 | }; |
| 271 | |
| 272 | #endif |
| 273 | |