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 
29 class Worker : public QObject
30 {
31  Q_OBJECT
32 
33 public:
34  void submitAndWaitForJob(DatabaseJob * job);
35  void quit();
36 
37 signals:
38  void opened(bool);
39  void failing(bool);
40 
41 public slots:
42  void run();
43 
44 private slots:
45  void commitTransaction();
46 
47 private:
48  bool upgradeVersion1();
49  void doJob(DatabaseJob * job);
50  void deleteOldThumbnails();
51 
52  QList<DatabaseJob*> m_jobs;
53  QMutex m_mutex;
54  QWaitCondition m_waitForFinished;
55  QWaitCondition m_waitForNewJob;
56  QTimer* m_commitTimer {nullptr};
57  bool m_quit {false};
58 };
59 
60 class Database : public QObject
61 {
62  Q_OBJECT
63  explicit Database(QObject *parent = 0);
64 
65 public:
66  static Database& singleton(QObject *parent = 0);
67 
68  bool putThumbnail(const QString& hash, const QImage& image);
69  QImage getThumbnail(const QString& hash);
70  bool isShutdown() const;
71  bool isFailing() const { return m_isFailing; }
72 
73 signals:
74  void start();
75 
76 private slots:
77  void shutdown();
78  void onOpened(bool success) { m_isOpened = success; }
79  void onFailing(bool failing) { m_isFailing = failing; }
80 
81 private:
82  Worker m_worker;
83  QThread m_thread;
84  bool m_isFailing {false};
85  bool m_isOpened {false};
86 };
87 
88 #define DB Database::singleton()
89 
90 #endif // DATABASE_H