Scribus
Open source desktop publishing at your fingertips
scimagecachemanager.h
1 /*
2 For general Scribus (>=1.3.2) copyright and licensing information please refer
3 to the COPYING file provided with the program. Following this notice may exist
4 a copyright and/or license notice that predates the release of Scribus 1.3.2
5 for which a new license (GPL+exception) is in place.
6 */
7 /***************************************************************************
8  copyright : (C) 2010 by Marcus Holland-Moritz
9  email : scribus@mhxnet.de
10 ***************************************************************************/
11 
12 /***************************************************************************
13 * *
14 * This program is free software; you can redistribute it and/or modify *
15 * it under the terms of the GNU General Public License as published by *
16 * the Free Software Foundation; either version 2 of the License, or *
17 * (at your option) any later version. *
18 * *
19 ***************************************************************************/
20 
21 #ifndef SCIMAGECACHEMANAGER_H
22 #define SCIMAGECACHEMANAGER_H
23 
24 #include <QList>
25 #include <QString>
26 #include <QDebug>
27 
28 #include "scribusapi.h"
29 #include "scimagecachedir.h"
30 
31 class QTemporaryFile;
32 class QFileInfo;
33 class ScImageCacheFile;
34 
39 class SCRIBUS_API ScImageCacheManager : public QObject
40 {
41  Q_OBJECT
42 
43 public:
44  typedef ScImageCacheDir::AccessCounter AccessCounter;
45 
50  static ScImageCacheManager & instance();
56  static QString absolutePath(const QString & fn);
57 
62  void setEnabled(bool enableCache);
67  bool enabled(void) const { return m_isEnabled; }
73  bool setMaxCacheSizeMiB(int maxCacheSizeMiB);
79  bool setMaxCacheEntries(int maxCacheEntries);
87  bool setCompressionLevel(int level);
92  int compressionLevel() const;
93 
97  void initialize();
101  void tryCleanup();
106  bool acquireWriteLock();
111  bool releaseWriteLock();
119  void removeMasterLock();
120 
131  bool updateAccess(const QString & dir, AccessCounter from, AccessCounter to);
141  bool updateFile(const QString & file);
142 
143 private slots:
144  void fileCreated(ScImageCacheFile * file, const QFileInfo & info);
145  void fileChanged(ScImageCacheFile * file, const QFileInfo & info);
146  void fileRemoved(ScImageCacheFile * file);
147 
148 private:
149  class MetaAgeList
150  {
151  public:
152  MetaAgeList();
153  void insert(ScImageCacheFile *p);
154  void update(ScImageCacheFile *p, const QFileInfo & newInfo);
155  void remove(ScImageCacheFile *p);
156  ScImageCacheFile *getOldest();
157  int count() const { return m_fa.size(); }
158 
159  private:
160  typedef QList<ScImageCacheFile *> FAL;
161  FAL m_fa;
162 
163  static bool ageLessThan(const ScImageCacheFile *a, const ScImageCacheFile *b);
164  };
165 
168 
169  static void create();
170  static void cleanupLockDir();
171  static bool createLockDir();
172  static QString lockDir();
173  static QString masterLockFile();
174  static QString writeLockTemplate();
175 
176  void sanitizeCache();
177  void updateCache();
178  void cleanupCache();
179  ScImageCacheFile *getOldestCacheEntry();
180 
181  bool acquireMasterLock();
182  bool releaseMasterLock();
183 
184  bool m_isEnabled;
185  bool m_haveMasterLock;
186  bool m_inCleanup;
187  int m_writeLockCount;
188  int m_compressionLevel;
189  int m_maxEntries;
190  int m_maxSizeMiB;
191  qint64 m_maxTotalSize;
192  qint64 m_totalCacheSize;
193 
194  MetaAgeList m_metaAge;
195 
196  QTemporaryFile *m_writeLockFile;
197  ScImageCacheDir *m_root;
198 };
199 
200 #endif
bool enabled(void) const
Check if the image cache is enabled.
Definition: scimagecachemanager.h:67
Representation of a directory node in the image cache tree.
Definition: scimagecachedir.h:38
Scribus image cache manager.
Definition: scimagecachemanager.h:39
Representation of a file node in the image cache tree.
Definition: scimagecachefile.h:37