File indexing completed on 2024-04-21 03:56:53

0001 /*
0002     This file is part of the KDE libraries
0003     SPDX-FileCopyrightText: 1999 David Faure <faure@kde.org>
0004 
0005     SPDX-License-Identifier: LGPL-2.0-only
0006 */
0007 
0008 #ifndef KBUILDSYCOCA_H
0009 #define KBUILDSYCOCA_H
0010 
0011 #include "kbuildsycocainterface_p.h"
0012 
0013 #include <kservice.h>
0014 #include <ksycoca.h>
0015 
0016 #include "vfolder_menu_p.h"
0017 
0018 class KBuildServiceGroupFactory;
0019 class QDataStream;
0020 class KCTimeFactory;
0021 class KCTimeDict;
0022 
0023 /**
0024  * @internal
0025  * Exported for kbuildsycoca, but not installed.
0026  */
0027 class KSERVICE_EXPORT KBuildSycoca : public KSycoca, public KBuildSycocaInterface
0028 {
0029     Q_OBJECT
0030 public:
0031     explicit KBuildSycoca();
0032     ~KBuildSycoca() override;
0033 
0034     /**
0035      * Recreate the database file.
0036      * @return true if it was indeed recreated (by us or possibly by someone else), false on error
0037      */
0038     bool recreate(bool incremental = true);
0039 
0040     void setTrackId(const QString &id)
0041     {
0042         m_trackId = id;
0043     }
0044 
0045     void setMenuTest(bool b)
0046     {
0047         m_menuTest = b;
0048     }
0049 
0050     static QStringList factoryResourceDirs();
0051     static QStringList factoryExtraFiles();
0052     static QStringList existingResourceDirs();
0053 
0054     /**
0055      * Returns a number that identifies the current version of the file @p filename,
0056      * which is located under GenericDataLocation (including local overrides).
0057      *
0058      * When a change is made to the file this number will change.
0059      */
0060     static quint32 calcResourceHash(const QString &subdir, const QString &filename);
0061 
0062     /**
0063      * Compare our current settings (language, prefixes...) with the ones from the existing ksycoca global header.
0064      * @return true if they match (= we can reuse this ksycoca), false otherwise (full build)
0065      */
0066     bool checkGlobalHeader();
0067 
0068     /**
0069      * @brief path to the sycoca file, for the crash handler in kbuildsycoca
0070      */
0071     static const char *sycocaPath();
0072 
0073 private:
0074     /**
0075      * Add single entry to the sycoca database.
0076      * Either from a previous database or regenerated from file.
0077      */
0078     KSERVICE_NO_EXPORT KSycocaEntry::Ptr createEntry(KSycocaFactory *currentFactory, const QString &file);
0079 
0080     /**
0081      * Implementation of KBuildSycocaInterface
0082      * Create service and return it. The caller must add it to the servicefactory.
0083      */
0084     KService::Ptr createService(const QString &path) override;
0085 
0086     /**
0087      * Convert a VFolderMenu::SubMenu to KServiceGroups.
0088      */
0089     KSERVICE_NO_EXPORT void createMenu(const QString &caption, const QString &name, VFolderMenu::SubMenu *menu);
0090 
0091     /**
0092      * Build the whole system cache, from .desktop files
0093      */
0094     KSERVICE_NO_EXPORT bool build();
0095 
0096     /**
0097      * Save the ksycoca file
0098      */
0099     KSERVICE_NO_EXPORT void save(QDataStream *str);
0100 
0101     /**
0102      * Clear the factories
0103      */
0104     KSERVICE_NO_EXPORT void clear();
0105 
0106     /**
0107      * @internal
0108      * @return true if building (i.e. if a KBuildSycoca);
0109      */
0110     bool isBuilding() override
0111     {
0112         return true;
0113     }
0114 
0115     QMap<QString, qint64> m_allResourceDirs; // dir, mtime in ms since epoch
0116     QMap<QString, qint64> m_extraFiles; // file, mtime in ms since epoch
0117     QString m_trackId;
0118 
0119     QByteArray m_resource; // e.g. "services" (old resource name, now only used for the signal, see kctimefactory.cpp)
0120     QString m_resourceSubdir; // e.g. "mime" (xdgdata subdir)
0121 
0122     KSycocaEntry::List m_tempStorage;
0123     typedef QList<KSycocaEntry::List> KSycocaEntryListList;
0124     KSycocaEntryListList *m_allEntries; // entries from existing ksycoca
0125     KBuildServiceGroupFactory *m_buildServiceGroupFactory = nullptr;
0126     KCTimeFactory *m_ctimeFactory = nullptr;
0127     KCTimeDict *m_ctimeDict; // old timestamps
0128     typedef QHash<QString, KSycocaEntry::Ptr> KBSEntryDict;
0129     KBSEntryDict *m_currentEntryDict = nullptr;
0130     KBSEntryDict *m_serviceGroupEntryDict = nullptr;
0131     VFolderMenu *m_vfolder = nullptr;
0132     qint64 m_newTimestamp;
0133 
0134     bool m_menuTest;
0135     bool m_changed;
0136 };
0137 
0138 #endif