1 | /* |
2 | Copyright (c) 2007 Volker Krause <vkrause@kde.org> |
3 | |
4 | This library is free software; you can redistribute it and/or modify it |
5 | under the terms of the GNU Library General Public License as published by |
6 | the Free Software Foundation; either version 2 of the License, or (at your |
7 | option) any later version. |
8 | |
9 | This library is distributed in the hope that it will be useful, but WITHOUT |
10 | ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
11 | FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public |
12 | License for more details. |
13 | |
14 | You should have received a copy of the GNU Library General Public License |
15 | along with this library; see the file COPYING.LIB. If not, write to the |
16 | Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
17 | 02110-1301, USA. |
18 | */ |
19 | |
20 | #ifndef AKONADI_CACHECLEANER_H |
21 | #define AKONADI_CACHECLEANER_H |
22 | |
23 | #include "collectionscheduler.h" |
24 | |
25 | #include <QMutex> |
26 | |
27 | namespace Akonadi { |
28 | namespace Server { |
29 | |
30 | class Collection; |
31 | |
32 | /** |
33 | * A RAII helper class to temporarily stop the CacheCleaner. This allows long-lasting |
34 | * operations to safely retrieve all data from resource and perform an operation on them |
35 | * (like move or copy) without risking that the cache will be cleaned in the meanwhile |
36 | * |
37 | * The inhibitor is recursive, so it's possible to create multiple instances of the |
38 | * CacheCleanerInhibitor and the CacheCleaner will be inhibited until all instances |
39 | * are destroyed again. However it's not possible to inhibit a single inhibitor |
40 | * multiple times. |
41 | */ |
42 | class CacheCleanerInhibitor |
43 | { |
44 | public: |
45 | CacheCleanerInhibitor(bool inhibit = true); |
46 | ~CacheCleanerInhibitor(); |
47 | |
48 | void inhibit(); |
49 | void uninhibit(); |
50 | |
51 | private: |
52 | static QMutex sLock; |
53 | static int sInhibitCount; |
54 | bool mInhibited; |
55 | }; |
56 | |
57 | /** |
58 | Cache cleaner thread. |
59 | */ |
60 | class CacheCleaner : public CollectionScheduler |
61 | { |
62 | Q_OBJECT |
63 | |
64 | public: |
65 | /** |
66 | Creates a new cache cleaner thread. |
67 | @param parent The parent object. |
68 | */ |
69 | CacheCleaner(QObject *parent = 0); |
70 | ~CacheCleaner(); |
71 | |
72 | protected: |
73 | void collectionExpired(const Collection &collection); |
74 | int collectionScheduleInterval(const Collection &collection); |
75 | bool hasChanged(const Collection &collection, const Collection &changed); |
76 | bool shouldScheduleCollection(const Collection &collection); |
77 | |
78 | private: |
79 | static CacheCleaner *sInstance; |
80 | |
81 | friend class CacheCleanerInhibitor; |
82 | |
83 | }; |
84 | |
85 | } // namespace Server |
86 | } // namespace Akonadi |
87 | |
88 | #endif |
89 | |