File indexing completed on 2024-04-21 05:27:19

0001 /*
0002  *   SPDX-FileCopyrightText: 2003 Waldo Bastian <bastian@kde.org>
0003  *
0004  *   SPDX-License-Identifier: GPL-2.0-or-later
0005  *
0006  */
0007 
0008 #ifndef menuinfo_h
0009 #define menuinfo_h
0010 
0011 #include <QList>
0012 
0013 #include <KService>
0014 #include <QKeySequence>
0015 
0016 class MenuFile;
0017 class MenuEntryInfo;
0018 
0019 class MenuInfo
0020 {
0021 public:
0022     MenuInfo()
0023     {
0024     }
0025 
0026     virtual ~MenuInfo()
0027     {
0028     }
0029 };
0030 
0031 class MenuSeparatorInfo : public MenuInfo
0032 {
0033 public:
0034     MenuSeparatorInfo()
0035     {
0036     }
0037 };
0038 
0039 class MenuFolderInfo : public MenuInfo
0040 {
0041 public:
0042     MenuFolderInfo()
0043         : dirty(false)
0044         , hidden(false)
0045     {
0046     }
0047 
0048     ~MenuFolderInfo() override;
0049     // Add separator
0050     void add(MenuSeparatorInfo *, bool initial = false);
0051 
0052     // Add sub menu
0053     void add(MenuFolderInfo *, bool initial = false);
0054 
0055     // Remove sub menu (without deleting it)
0056     void take(MenuFolderInfo *);
0057 
0058     // Remove sub menu (without deleting it)
0059     // @return true if found
0060     bool takeRecursive(MenuFolderInfo *info);
0061 
0062     // Add entry
0063     void add(MenuEntryInfo *, bool initial = false);
0064 
0065     // Remove entry (without deleting it)
0066     void take(MenuEntryInfo *);
0067 
0068     // Return a unique sub-menu caption inspired by @p caption
0069     QString uniqueMenuCaption(const QString &caption);
0070 
0071     // Return a unique item caption inspired by @p caption but different
0072     // from @p exclude
0073     QString uniqueItemCaption(const QString &caption, const QString &exclude = QString());
0074 
0075     // Update full id's for this item and all submenus
0076     void updateFullId(const QString &parentId);
0077 
0078     // Return a list of existing submenu ids
0079     QStringList existingMenuIds();
0080 
0081     void setCaption(const QString &_caption)
0082     {
0083         if (_caption == caption) {
0084             return;
0085         }
0086         caption = _caption;
0087         setDirty();
0088     }
0089 
0090     void setIcon(const QString &_icon)
0091     {
0092         if (_icon == icon) {
0093             return;
0094         }
0095         icon = _icon;
0096         setDirty();
0097     }
0098 
0099     void setGenericName(const QString &_description)
0100     {
0101         if (_description == genericname) {
0102             return;
0103         }
0104         genericname = _description;
0105         setDirty();
0106     }
0107 
0108     void setComment(const QString &_comment)
0109     {
0110         if (_comment == comment) {
0111             return;
0112         }
0113         comment = _comment;
0114         setDirty();
0115     }
0116 
0117     // Mark menu as dirty
0118     void setDirty();
0119 
0120     // Return whether this menu or any entry or submenu contained in it is dirty.
0121     bool hasDirt();
0122 
0123     // Return whether this menu should be explicitly added to its parent menu
0124     bool needInsertion();
0125 
0126     // Save menu and all its entries and submenus
0127     void save(MenuFile *);
0128 
0129     // Search service by shortcut
0130     KService::Ptr findServiceShortcut(const QKeySequence &);
0131 
0132     // Set whether the entry is in active use (as opposed to in the clipboard/deleted)
0133     void setInUse(bool inUse);
0134 
0135 public:
0136     QString id; // Relative to parent
0137     QString fullId; // Name in tree
0138     QString caption; // Visible name
0139     QString genericname; // Generic description
0140     QString comment; // Comment
0141     QString directoryFile; // File describing this folder.
0142     QString icon; // Icon
0143     QList<MenuFolderInfo *> subFolders; // Sub menus in this folder
0144     QList<MenuEntryInfo *> entries; // Menu entries in this folder
0145     QList<MenuInfo *> initialLayout; // Layout of menu entries according to sycoca
0146     bool dirty;
0147     bool hidden;
0148 };
0149 
0150 class MenuEntryInfo : public MenuInfo
0151 {
0152 public:
0153     explicit MenuEntryInfo(const KService::Ptr &_service, KDesktopFile *_df = nullptr)
0154         : service(_service)
0155         , m_desktopFile(_df)
0156         , shortcutLoaded(false)
0157         , shortcutDirty(false)
0158         , dirty(_df != 0)
0159         , hidden(false)
0160     {
0161         caption = service->name();
0162         description = service->genericName();
0163         icon = service->icon();
0164     }
0165 
0166     ~MenuEntryInfo() override;
0167 
0168     void setCaption(const QString &_caption);
0169     void setDescription(const QString &_description);
0170     void setIcon(const QString &_icon);
0171 
0172     QString menuId() const
0173     {
0174         return service->menuId();
0175     }
0176 
0177     QString file() const
0178     {
0179         return service->entryPath();
0180     }
0181 
0182     QKeySequence shortcut();
0183     void setShortcut(const QKeySequence &_shortcut);
0184     bool isShortcutAvailable(const QKeySequence &_shortcut);
0185 
0186     void setDirty();
0187 
0188     // Set whether the entry is in active use (as opposed to in the clipboard/deleted)
0189     void setInUse(bool inUse);
0190 
0191     // Return whether this menu should be explicitly added to its parent menu
0192     bool needInsertion();
0193 
0194     void save();
0195 
0196     KDesktopFile *desktopFile();
0197 
0198 public:
0199     QString caption;
0200     QString description;
0201     QString icon;
0202     KService::Ptr service;
0203     KDesktopFile *m_desktopFile;
0204     QKeySequence shortCut;
0205     bool shortcutLoaded;
0206     bool shortcutDirty;
0207     bool dirty;
0208     bool hidden;
0209 };
0210 
0211 #endif