1 #ifndef SEAFILE_CLIENT_FILE_BROWSER_DATA_CACHE_H
2 #define SEAFILE_CLIENT_FILE_BROWSER_DATA_CACHE_H
3 
4 #include <QList>
5 #include <utility>
6 
7 #include "seaf-dirent.h"
8 #include "utils/singleton.h"
9 
10 template<typename Key, typename T> class QCache;
11 
12 struct sqlite3;
13 struct sqlite3_stmt;
14 
15 /**
16  * Cache dirents by (repo_id + path, dirents) in memory
17  */
18 class DirentsCache {
19     SINGLETON_DEFINE(DirentsCache)
20 public:
21     typedef std::pair<bool ,QList<SeafDirent>*> ReturnEntry;
22     ReturnEntry getCachedDirents(const QString& repo_id,
23                                  const QString& path);
24 
25     void expireCachedDirents(const QString& repo_id, const QString& path);
26 
27     void saveCachedDirents(const QString& repo_id,
28                            const QString& path,
29                            bool current_readonly,
30                            const QList<SeafDirent>& dirents);
31 
32 private:
33     DirentsCache();
34     ~DirentsCache();
35     struct CacheEntry {
36         qint64 timestamp;
37         bool current_readonly;
38         QList<SeafDirent> dirents;
39     };
40 
41     QCache<QString, CacheEntry> *cache_;
42 };
43 
44 #if 0
45 /**
46  * Record the file id of downloaded files.
47  * The schema is (repo_id, path, downloaded_file_id)
48  */
49 class FileCache {
50     SINGLETON_DEFINE(FileCache)
51 public:
52     struct CacheEntry {
53         QString repo_id;
54         QString path;
55         QString file_id;
56         QString account_sig;
57     };
58 
59 
60     QString getCachedFileId(const QString& repo_id,
61                             const QString& path);
62     CacheEntry getCacheEntry(const QString& repo_id,
63                              const QString& path);
64     void saveCachedFileId(const QString& repo_id,
65                           const QString& path,
66                           const QString& file_id,
67                           const QString& account_sig);
68 
69     QList<CacheEntry> getAllCachedFiles();
70     void cleanCurrentAccountCache();
71 
72 private:
73     FileCache();
74     ~FileCache();
75 
76     QHash<QString, CacheEntry> *cache_;
77 };
78 #endif
79 
80 /**
81  * Record the file id of downloaded files.
82  */
83 class FileCache {
84     SINGLETON_DEFINE(FileCache)
85 public:
86     struct CacheEntry {
87         QString repo_id;
88         QString path;
89         QString account_sig;
90         QString file_id;
91         qint64  seafile_mtime;
92         qint64  seafile_size;
93     };
94 
95     void start();
96 
97     bool getCacheEntry(const QString& repo_id,
98                        const QString& path,
99                        CacheEntry *entry);
100     void saveCachedFileId(const QString& repo_id,
101                           const QString& path,
102                           const QString& file_id,
103                           const QString& account_sig,
104                           const QString& local_file_path);
105 
106     QList<CacheEntry> getCachedFilesForDirectory(const QString& account_sig,
107                                                  const QString& repo_id,
108                                                  const QString& parent_dir);
109 
110     QList<CacheEntry> getAllCachedFiles();
111     void cleanUnModifiedCacheItemInDatabase(const QString file_id);
112 
113 private:
114     FileCache();
115     ~FileCache();
116     static bool getCacheEntryCB(sqlite3_stmt *stmt, void *data);
117     static bool collectCachedFile(sqlite3_stmt *stmt, void *data);
118 
119     sqlite3 *db_;
120 };
121 
122 
123 #endif // SEAFILE_CLIENT_FILE_BROWSER_DATA_CACHE_H
124