Dooble
dooble_history.h
1 /*
2 ** Copyright (c) 2008 - present, Alexis Megas.
3 ** All rights reserved.
4 **
5 ** Redistribution and use in source and binary forms, with or without
6 ** modification, are permitted provided that the following conditions
7 ** are met:
8 ** 1. Redistributions of source code must retain the above copyright
9 ** notice, this list of conditions and the following disclaimer.
10 ** 2. Redistributions in binary form must reproduce the above copyright
11 ** notice, this list of conditions and the following disclaimer in the
12 ** documentation and/or other materials provided with the distribution.
13 ** 3. The name of the author may not be used to endorse or promote products
14 ** derived from Dooble without specific prior written permission.
15 **
16 ** DOOBLE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
17 ** IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
18 ** OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
19 ** IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
20 ** INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
21 ** NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
22 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
23 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
25 ** DOOBLE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26 */
27 
28 #ifndef dooble_history_h
29 #define dooble_history_h
30 
31 #include <QAtomicInteger>
32 #include <QFuture>
33 #include <QReadWriteLock>
34 #include <QSqlDatabase>
35 #include <QTimer>
36 #include <QWebEngineHistoryItem>
37 
38 class QAction;
39 class QStandardItemModel;
40 typedef QList<QPair<QIcon, QString> > QListPairIconString;
41 typedef QList<QUrl> QListUrl;
42 typedef QList<QVector<QByteArray> > QListVectorByteArray;
43 
44 class dooble_history: public QObject
45 {
46  Q_OBJECT
47 
48  public:
49  enum HistoryItem
50  {
51  FAVICON = 0,
52  FAVORITE,
53  LAST_VISITED,
54  NUMBER_OF_VISITS,
55  TITLE,
56  URL,
57  URL_DIGEST
58  };
59 
60  dooble_history(void);
61  ~dooble_history();
62  QHash<QUrl, QHash<dooble_history::HistoryItem, QVariant> >
63  history(void) const;
64  QList<QAction *> last_n_actions(int n) const;
65  QStandardItemModel *favorites_model(void) const;
66  bool is_favorite(const QUrl &url) const;
67  void abort(void);
68  void purge_all(void);
69  void purge_favorites(void);
70  void purge_history(void);
71  void remove_favorite(const QUrl &url);
72  void remove_items_list(const QList<QUrl> &url);
73  void save_favicon(const QIcon &icon, const QUrl &url);
74  void save_favorite(const QUrl &url, bool state);
75  void save_item(const QIcon &icon,
76  const QWebEngineHistoryItem &item,
77  bool force);
78 
79  private:
80  QAtomicInteger<short> m_interrupt;
81  QFuture<void> m_populate_future;
82  QFuture<void> m_purge_future;
83  QHash<QUrl, QHash<HistoryItem, QVariant> > m_history;
84  QMultiMap<QDateTime, QUrl> m_history_date_time;
85  QStandardItemModel *m_favorites_model;
86  QTimer m_purge_timer;
87  mutable QReadWriteLock m_history_mutex;
88  void create_tables(QSqlDatabase &db);
89  void populate(const QByteArray &authentication_key,
90  const QByteArray &encryption_key);
91  void purge(const QByteArray &authentication_key,
92  const QByteArray &encryption_key);
93  void update_favorite(const QHash<HistoryItem, QVariant> &hash);
94 
95  private slots:
96  void slot_populate(void);
97  void slot_populated_favorites(const QListVectorByteArray &favorites);
98  void slot_remove_items(const QListUrl &urls);
99  void slot_purge_timer_timeout(void);
100 
101  signals:
102  void icon_updated(const QIcon &icon, const QUrl &url);
103  void item_updated(const QIcon &icon, const QWebEngineHistoryItem &item);
104  void new_item(const QIcon &icon, const QWebEngineHistoryItem &item);
105  void populated(const QListPairIconString &list);
106  void populated(void);
107  void populated_favorites(const QListVectorByteArray &favorites);
108  void remove_items(const QListUrl &urls);
109 };
110 
111 #endif
Definition: dooble_history.h:44