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

0001 /*
0002     SPDX-FileCopyrightText: 2009 Frederik Gladhorn <gladhorn@kde.org>
0003     SPDX-FileCopyrightText: 2010 Matthias Fuchs <mat69@gmx.net>
0004 
0005     SPDX-License-Identifier: LGPL-2.1-or-later
0006 */
0007 
0008 #ifndef CACHE_H
0009 #define CACHE_H
0010 
0011 #include <QObject>
0012 #include <QSet>
0013 
0014 #include "entry.h"
0015 #include "provider.h"
0016 
0017 #include "knewstuffcore_export.h"
0018 
0019 #include <memory.h>
0020 
0021 namespace KNSCore
0022 {
0023 class CachePrivate;
0024 class KNEWSTUFFCORE_EXPORT Cache : public QObject
0025 {
0026     Q_OBJECT
0027 
0028 public:
0029     /**
0030      * Returns an instance of a shared cache for appName
0031      * That way it is made sure, that there do not exist different
0032      * instances of cache, with different contents
0033      * @param appName The file name of the registry - this is usually
0034      * the application name, it will be stored in "apps/knewstuff3/appname.knsregistry"
0035      */
0036     static QSharedPointer<Cache> getCache(const QString &appName);
0037 
0038     ~Cache() override;
0039 
0040     /// Read the installed entries (on startup)
0041     void readRegistry();
0042     /// All entries that have been installed by a certain provider
0043     Entry::List registryForProvider(const QString &providerId);
0044 
0045     /// All entries known by the cache (this corresponds with entries which are installed, regardless of status)
0046     Entry::List registry() const;
0047 
0048     /// Save the list of installed entries
0049     void writeRegistry();
0050 
0051     void insertRequest(const KNSCore::Provider::SearchRequest &, const KNSCore::Entry::List &entries);
0052     Entry::List requestFromCache(const KNSCore::Provider::SearchRequest &);
0053 
0054     /**
0055      * This will run through all entries in the cache, and remove all entries
0056      * where all the installed files they refer to no longer exist.
0057      *
0058      * This cannot be done wholesale for all caches, as some consumers will allow
0059      * this to happen (or indeed expect it to), and so we have to do this on a
0060      * per-type basis
0061      *
0062      * This will also cause the cache store to be updated
0063      *
0064      * @since 5.71
0065      */
0066     void removeDeletedEntries();
0067 
0068     /**
0069      * Get the entry which installed the passed file. If no entry lists the
0070      * passed file as having been installed by it, an invalid entry will be
0071      * returned.
0072      * @param installedFile The full path name for an installed file
0073      * @return An entry if one was found, or an invalid entry if no entry says it installed that file
0074      * since 5.74
0075      */
0076     KNSCore::Entry entryFromInstalledFile(const QString &installedFile) const;
0077 
0078     /**
0079      * Emitted when the cache has changed underneath us, and need users of the cache to know
0080      * that this has happened.
0081      * @param entry The entry which has changed
0082      * @since 5.75
0083      */
0084     Q_SIGNAL void entryChanged(const KNSCore::Entry &entry);
0085 
0086 public Q_SLOTS:
0087     void registerChangedEntry(const KNSCore::Entry &entry);
0088 
0089 private:
0090     Q_DISABLE_COPY(Cache)
0091     Cache(const QString &appName);
0092 
0093 private:
0094     std::unique_ptr<CachePrivate> d;
0095 };
0096 
0097 }
0098 
0099 #endif