File indexing completed on 2024-05-19 04:58:02

0001 // SPDX-FileCopyrightText: 2020 Jonah BrĂ¼chert <jbb@kaidan.im>
0002 // SPDX-FileCopyrightText: 2020 Rinigus <rinigus.git@gmail.com>
0003 //
0004 // SPDX-License-Identifier: GPL-2.0-or-later
0005 
0006 #ifndef BOOKMARKSHISTORYMODEL_H
0007 #define BOOKMARKSHISTORYMODEL_H
0008 
0009 #include <QAbstractListModel>
0010 
0011 struct BookmarksHistoryRecord {
0012     using ColumnTypes = std::tuple<int, QString, QString, QString, int>;
0013 
0014     bool operator==(const BookmarksHistoryRecord &other) const = default;
0015 
0016     int id;
0017     QString url;
0018     QString title;
0019     QString icon;
0020     int lastVisitedDelta;
0021 };
0022 
0023 /**
0024  * @class BookmarksHistoryModel
0025  * @short Model for listing Bookmarks and History items.
0026  */
0027 class BookmarksHistoryModel : public QAbstractListModel
0028 {
0029     Q_OBJECT
0030 
0031     // while active, data is shown and changes in the used database table(s)
0032     // will trigger new query
0033     Q_PROPERTY(bool active READ active WRITE setActive NOTIFY activeChanged)
0034     // set to true for including bookmarks
0035     Q_PROPERTY(bool bookmarks READ bookmarks WRITE setBookmarks NOTIFY bookmarksChanged)
0036     // set to true for including history
0037     Q_PROPERTY(bool history READ history WRITE setHistory NOTIFY historyChanged)
0038     // set to string to filter url or title by it. without filter set, only
0039     // bookmarks are shown
0040     Q_PROPERTY(QString filter READ filter WRITE setFilter NOTIFY filterChanged)
0041 
0042     enum Role {
0043         Id = Qt::UserRole + 1,
0044         Url,
0045         Title,
0046         Icon,
0047         LastVisitedDelta
0048     };
0049 
0050 public:
0051     BookmarksHistoryModel(QObject *parent = nullptr);
0052 
0053     int rowCount(const QModelIndex &index) const override {
0054         return index.isValid() ? 0 : m_entries.size();
0055     }
0056 
0057     QHash<int, QByteArray> roleNames() const override;
0058 
0059     QVariant data(const QModelIndex &index, int role) const override;
0060 
0061     bool active() const
0062     {
0063         return m_active;
0064     }
0065     void setActive(bool a);
0066 
0067     bool bookmarks() const
0068     {
0069         return m_bookmarks;
0070     }
0071     void setBookmarks(bool b);
0072 
0073     bool history() const
0074     {
0075         return m_history;
0076     }
0077     void setHistory(bool h);
0078 
0079     QString filter() const
0080     {
0081         return m_filter;
0082     }
0083     void setFilter(const QString &f);
0084 
0085 Q_SIGNALS:
0086     void activeChanged();
0087     void bookmarksChanged();
0088     void historyChanged();
0089     void filterChanged();
0090 
0091 private:
0092     void onDatabaseChanged(const QString &table);
0093 
0094     void fetchData();
0095     void clear();
0096 
0097 private:
0098     bool m_active = true;
0099     bool m_bookmarks = false;
0100     bool m_history = false;
0101     QString m_filter;
0102 
0103     std::vector<BookmarksHistoryRecord> m_entries;
0104 };
0105 
0106 #endif // BOOKMARKSHISTORYMODEL_H