File indexing completed on 2024-04-14 03:47:44

0001 // SPDX-License-Identifier: LGPL-2.1-or-later
0002 //
0003 // SPDX-FileCopyrightText: 2009 Bastian Holst <bastianholst@gmx.de>
0004 //
0005 
0006 #ifndef MARBLE_FILESTORAGEWATCHER_H
0007 #define MARBLE_FILESTORAGEWATCHER_H
0008 
0009 #include <QThread>
0010 #include <QMutex>
0011 #include <QMultiMap>
0012 #include <QDateTime>
0013 
0014 namespace Marble
0015 {
0016     
0017 // Worker object that lives inside the new Thread
0018 class FileStorageWatcherThread : public QObject
0019 {
0020     Q_OBJECT
0021     
0022     public:
0023     explicit FileStorageWatcherThread( const QString &dataDirectory, QObject * parent = nullptr );
0024     
0025     ~FileStorageWatcherThread() override;
0026     
0027     quint64 cacheLimit();
0028     
0029     Q_SIGNALS:
0030     /**
0031      * Is emitted when a variable has changed.
0032      */
0033     void variableChanged();
0034     
0035     public Q_SLOTS:
0036     /**
0037      * Sets the limit of the cache in @p bytes.
0038     */
0039     void setCacheLimit( quint64 bytes );
0040     
0041     /**
0042      * Add @p bytes to the current cache size.
0043      * So FileStorageWatcher is aware of the current cache size.
0044      */
0045     void addToCurrentSize( qint64 bytes );
0046     
0047     /**
0048      * Setting current cache size to 0.
0049      */
0050     void resetCurrentSize();
0051     
0052     /**
0053      * Stop doing things that take a long time to quit.
0054      */
0055     void prepareQuit();
0056     
0057     /**
0058      * Getting the current size of the data stored on the disc
0059      */
0060     void getCurrentCacheSize();
0061 
0062     private Q_SLOTS:
0063     /**
0064      * Ensures that the cache doesn't exceed limits.
0065      */
0066     void ensureCacheSize();
0067     
0068     private:
0069     Q_DISABLE_COPY( FileStorageWatcherThread )
0070     
0071     /**
0072      * Returns true if it is necessary to delete files.
0073      */
0074     bool keepDeleting() const;
0075     
0076     QString m_dataDirectory;
0077     QMultiMap<QDateTime,QString> m_filesCache;
0078     quint64 m_cacheLimit;
0079     quint64 m_cacheSoftLimit;
0080     quint64 m_currentCacheSize;
0081     int     m_filesDeleted;
0082     bool    m_deleting;
0083     QMutex  m_limitMutex;
0084     bool    m_willQuit;
0085 };
0086 
0087 
0088 // Lives inside main thread
0089 class FileStorageWatcher : public QThread
0090 {
0091     Q_OBJECT
0092     
0093     public:
0094     /**
0095      * Creates a new FileStorageWatcher, which is a thread watching the
0096      * space Marble takes on the hard drive and deletes files if necessary.
0097      *
0098      * @param dataDirectory The directory where the data is stored
0099      * @param parent The parent of the object.
0100      */
0101     explicit FileStorageWatcher( const QString &dataDirectory = QString(), QObject * parent = nullptr );
0102     
0103     ~FileStorageWatcher() override;
0104     
0105     /**
0106      * Returns the limit of the cache in bytes.
0107      */
0108     quint64 cacheLimit();
0109     
0110     public Q_SLOTS:
0111     /**
0112          * Sets the limit of the cache in @p bytes.
0113          */
0114     void setCacheLimit( quint64 bytes );
0115     
0116     /**
0117      * Add @p bytes to the current cache size.
0118      * So FileStorageWatcher is aware of the current cache size.
0119      */
0120     void addToCurrentSize( qint64 bytes );
0121     
0122     /**
0123      * Setting current cache size to 0.
0124      */
0125     void resetCurrentSize();
0126     
0127 
0128     Q_SIGNALS:
0129     void sizeChanged( qint64 bytes );
0130     void cleared();
0131     
0132     protected:
0133     /**
0134      * The function being called at starting Thread.
0135      * The thread is started by QThread::start().
0136      */
0137     void run() override;
0138     
0139     private:
0140     Q_DISABLE_COPY( FileStorageWatcher )
0141     
0142     QString m_dataDirectory;
0143     FileStorageWatcherThread *m_thread;
0144     QMutex *m_limitMutex;
0145     quint64 m_limit;
0146     bool m_started;
0147     bool m_quitting;
0148 };
0149 
0150 }
0151 
0152 #endif