Marble

FileStorageWatcher.h
1 // SPDX-License-Identifier: LGPL-2.1-or-later
2 //
3 // SPDX-FileCopyrightText: 2009 Bastian Holst <[email protected]>
4 //
5 
6 #ifndef MARBLE_FILESTORAGEWATCHER_H
7 #define MARBLE_FILESTORAGEWATCHER_H
8 
9 #include <QThread>
10 #include <QMutex>
11 #include <QMultiMap>
12 #include <QDateTime>
13 
14 namespace Marble
15 {
16 
17 // Worker object that lives inside the new Thread
18 class FileStorageWatcherThread : public QObject
19 {
20  Q_OBJECT
21 
22  public:
23  explicit FileStorageWatcherThread( const QString &dataDirectory, QObject * parent = nullptr );
24 
25  ~FileStorageWatcherThread() override;
26 
27  quint64 cacheLimit();
28 
29  Q_SIGNALS:
30  /**
31  * Is emitted when a variable has changed.
32  */
33  void variableChanged();
34 
35  public Q_SLOTS:
36  /**
37  * Sets the limit of the cache in @p bytes.
38  */
39  void setCacheLimit( quint64 bytes );
40 
41  /**
42  * Add @p bytes to the current cache size.
43  * So FileStorageWatcher is aware of the current cache size.
44  */
45  void addToCurrentSize( qint64 bytes );
46 
47  /**
48  * Setting current cache size to 0.
49  */
50  void resetCurrentSize();
51 
52  /**
53  * Stop doing things that take a long time to quit.
54  */
55  void prepareQuit();
56 
57  /**
58  * Getting the current size of the data stored on the disc
59  */
60  void getCurrentCacheSize();
61 
62  private Q_SLOTS:
63  /**
64  * Ensures that the cache doesn't exceed limits.
65  */
66  void ensureCacheSize();
67 
68  private:
69  Q_DISABLE_COPY( FileStorageWatcherThread )
70 
71  /**
72  * Returns true if it is necessary to delete files.
73  */
74  bool keepDeleting() const;
75 
76  QString m_dataDirectory;
77  QMultiMap<QDateTime,QString> m_filesCache;
78  quint64 m_cacheLimit;
79  quint64 m_cacheSoftLimit;
80  quint64 m_currentCacheSize;
81  int m_filesDeleted;
82  bool m_deleting;
83  QMutex m_limitMutex;
84  bool m_willQuit;
85 };
86 
87 
88 // Lives inside main thread
89 class FileStorageWatcher : public QThread
90 {
91  Q_OBJECT
92 
93  public:
94  /**
95  * Creates a new FileStorageWatcher, which is a thread watching the
96  * space Marble takes on the hard drive and deletes files if necessary.
97  *
98  * @param dataDirectory The directory where the data is stored
99  * @param parent The parent of the object.
100  */
101  explicit FileStorageWatcher( const QString &dataDirectory = QString(), QObject * parent = nullptr );
102 
103  ~FileStorageWatcher() override;
104 
105  /**
106  * Returns the limit of the cache in bytes.
107  */
108  quint64 cacheLimit();
109 
110  public Q_SLOTS:
111  /**
112  * Sets the limit of the cache in @p bytes.
113  */
114  void setCacheLimit( quint64 bytes );
115 
116  /**
117  * Add @p bytes to the current cache size.
118  * So FileStorageWatcher is aware of the current cache size.
119  */
120  void addToCurrentSize( qint64 bytes );
121 
122  /**
123  * Setting current cache size to 0.
124  */
125  void resetCurrentSize();
126 
127 
128  Q_SIGNALS:
129  void sizeChanged( qint64 bytes );
130  void cleared();
131 
132  protected:
133  /**
134  * The function being called at starting Thread.
135  * The thread is started by QThread::start().
136  */
137  void run() override;
138 
139  private:
140  Q_DISABLE_COPY( FileStorageWatcher )
141 
142  QString m_dataDirectory;
143  FileStorageWatcherThread *m_thread;
144  QMutex *m_limitMutex;
145  quint64 m_limit;
146  bool m_started;
147  bool m_quitting;
148 };
149 
150 }
151 
152 #endif
Q_OBJECTQ_OBJECT
Q_SLOTSQ_SLOTS
Binds a QML item to a specific geodetic location in screen coordinates.
KIOWIDGETS_EXPORT bool run(const QUrl &_url, bool _is_local)
Q_SIGNALSQ_SIGNALS
Q_DISABLE_COPY(Class)
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Oct 2 2023 03:52:07 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.