CuteLogger
Fast and simple logging solution for Qt based applications
database.h
1 /*
2  * Copyright (c) 2013-2018 Meltytech, LLC
3  *
4  * This program is free software: you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation, either version 3 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License
15  * along with this program. If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #ifndef DATABASE_H
19 #define DATABASE_H
20 
21 #include <QThread>
22 #include <QImage>
23 #include <QMutex>
24 #include <QWaitCondition>
25 
26 struct DatabaseJob;
27 class QTimer;
28 class Database : public QThread
29 {
30  Q_OBJECT
31  explicit Database(QObject *parent = 0);
32 
33 public:
34  static Database& singleton(QWidget* parent = 0);
35 
36  bool upgradeVersion1();
37  bool putThumbnail(const QString& hash, const QImage& image);
38  QImage getThumbnail(const QString& hash);
39  bool isShutdown() const;
40  bool isFailing() const { return m_isFailing; }
41 
42 private slots:
43  void commitTransaction();
44 
45 private slots:
46  void shutdown();
47 
48 private:
49  void doJob(DatabaseJob * job);
50  void submitAndWaitForJob(DatabaseJob * job);
51  void deleteOldThumbnails();
52  void run();
53 
54  QList<DatabaseJob*> m_jobs;
55  QMutex m_mutex;
56  QWaitCondition m_waitForFinished;
57  QWaitCondition m_waitForNewJob;
58  QTimer * m_commitTimer;
59  bool m_isFailing;
60 };
61 
62 #define DB Database::singleton()
63 
64 #endif // DATABASE_H