1 /************************************************************************** 2 * Otter Browser: Web browser controlled by the user, not vice-versa. 3 <<<<<<< HEAD 4 * Copyright (C) 2013 - 2018 Michal Dutkiewicz aka Emdek <michal@emdek.pl> 5 ======= 6 * Copyright (C) 2013 - 2020 Michal Dutkiewicz aka Emdek <michal@emdek.pl> 7 >>>>>>> 6f9973e3b... Validate data first 8 * Copyright (C) 2017 Jan Bajer aka bajasoft <jbajer@gmail.com> 9 * 10 * This program is free software: you can redistribute it and/or modify 11 * it under the terms of the GNU General Public License as published by 12 * the Free Software Foundation, either version 3 of the License, or 13 * (at your option) any later version. 14 * 15 * This program is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with this program. If not, see <http://www.gnu.org/licenses/>. 22 * 23 **************************************************************************/ 24 25 #ifndef OTTER_BOOKMARKSMODEL_H 26 #define OTTER_BOOKMARKSMODEL_H 27 28 #include <QtCore/QUrl> 29 #include <QtCore/QXmlStreamReader> 30 #include <QtCore/QXmlStreamWriter> 31 #include <QtGui/QStandardItemModel> 32 33 namespace Otter 34 { 35 36 class Feed; 37 38 class BookmarksModel final : public QStandardItemModel 39 { 40 Q_OBJECT 41 42 public: 43 enum BookmarkType 44 { 45 UnknownBookmark = 0, 46 RootBookmark, 47 TrashBookmark, 48 FeedBookmark, 49 FolderBookmark, 50 UrlBookmark, 51 SeparatorBookmark 52 }; 53 54 enum BookmarkRole 55 { 56 TitleRole = Qt::DisplayRole, 57 UrlRole = Qt::StatusTipRole, 58 DescriptionRole = Qt::ToolTipRole, 59 IdentifierRole = Qt::UserRole, 60 TypeRole, 61 KeywordRole, 62 TimeAddedRole, 63 TimeModifiedRole, 64 TimeVisitedRole, 65 VisitsRole, 66 IsTrashedRole, 67 UserRole 68 }; 69 70 enum FormatMode 71 { 72 BookmarksMode = 0, 73 NotesMode 74 }; 75 76 class Bookmark final : public QStandardItem 77 { 78 public: 79 void remove(); 80 void setData(const QVariant &value, int role) override; 81 void setItemData(const QVariant &value, int role); 82 QStandardItem* clone() const override; 83 Bookmark* getChild(int index) const; 84 QString getTitle() const; 85 QString getDescription() const; 86 QString getKeyword() const; 87 QUrl getUrl() const; 88 QDateTime getTimeAdded() const; 89 QDateTime getTimeModified() const; 90 QDateTime getTimeVisited() const; 91 QIcon getIcon() const; 92 QVariant data(int role) const override; 93 QVariant getRawData(int role) const; 94 QVector<QUrl> getUrls() const; 95 quint64 getIdentifier() const; 96 BookmarkType getType() const; 97 int getVisits() const; 98 bool isAncestorOf(Bookmark *child) const; 99 bool operator<(const QStandardItem &other) const override; 100 101 protected: 102 explicit Bookmark(); 103 104 friend class BookmarksModel; 105 }; 106 107 struct BookmarkMatch final 108 { 109 Bookmark *bookmark = nullptr; 110 QString match; 111 }; 112 113 explicit BookmarksModel(const QString &path, FormatMode mode, QObject *parent = nullptr); 114 115 void beginImport(Bookmark *target, int estimatedUrlsAmount = 0, int estimatedKeywordsAmount = 0); 116 void endImport(); 117 void trashBookmark(Bookmark *bookmark); 118 void restoreBookmark(Bookmark *bookmark); 119 void removeBookmark(Bookmark *bookmark); 120 Bookmark* addBookmark(BookmarkType type, const QMap<int, QVariant> &metaData = {}, Bookmark *parent = nullptr, int index = -1); 121 Bookmark* getBookmarkByKeyword(const QString &keyword) const; 122 Bookmark* getBookmarkByPath(const QString &path) const; 123 Bookmark* getBookmark(const QModelIndex &index) const; 124 Bookmark* getBookmark(quint64 identifier) const; 125 Bookmark* getRootItem() const; 126 Bookmark* getTrashItem() const; 127 QMimeData* mimeData(const QModelIndexList &indexes) const override; 128 QStringList mimeTypes() const override; 129 QStringList getKeywords() const; 130 QVector<BookmarkMatch> findBookmarks(const QString &prefix) const; 131 QVector<Bookmark*> findUrls(const QUrl &url, QStandardItem *branch = nullptr) const; 132 QVector<Bookmark*> getBookmarks(const QUrl &url) const; 133 FormatMode getFormatMode() const; 134 bool moveBookmark(Bookmark *bookmark, Bookmark *newParent, int newRow = -1); 135 bool canDropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) const override; 136 bool dropMimeData(const QMimeData *data, Qt::DropAction action, int row, int column, const QModelIndex &parent) override; 137 bool save(const QString &path) const; 138 bool setData(const QModelIndex &index, const QVariant &value, int role) override; 139 bool hasBookmark(const QUrl &url) const; 140 bool hasFeed(const QUrl &url) const; 141 bool hasKeyword(const QString &keyword) const; 142 143 public slots: 144 void emptyTrash(); 145 146 protected: 147 void readBookmark(QXmlStreamReader *reader, Bookmark *parent); 148 void writeBookmark(QXmlStreamWriter *writer, Bookmark *bookmark) const; 149 void removeBookmarkUrl(Bookmark *bookmark); 150 void readdBookmarkUrl(Bookmark *bookmark); 151 void setupFeed(Bookmark *bookmark); 152 void handleKeywordChanged(Bookmark *bookmark, const QString &newKeyword, const QString &oldKeyword = {}); 153 void handleUrlChanged(Bookmark *bookmark, const QUrl &newUrl, const QUrl &oldUrl = {}); 154 static QDateTime readDateTime(QXmlStreamReader *reader, const QString &attribute); 155 156 protected slots: 157 void handleFeedModified(Feed *feed); 158 void notifyBookmarkModified(const QModelIndex &index); 159 160 private: 161 Bookmark *m_rootItem; 162 Bookmark *m_trashItem; 163 Bookmark *m_importTargetItem; 164 QHash<Bookmark*, QPair<QModelIndex, int> > m_trash; 165 QHash<QUrl, QVector<Bookmark*> > m_feeds; 166 QHash<QUrl, QVector<Bookmark*> > m_urls; 167 QHash<QString, Bookmark*> m_keywords; 168 QMap<quint64, Bookmark*> m_identifiers; 169 FormatMode m_mode; 170 171 signals: 172 void bookmarkAdded(Bookmark *bookmark); 173 void bookmarkModified(Bookmark *bookmark); 174 void bookmarkMoved(Bookmark *bookmark, Bookmark *previousParent, int previousRow); 175 void bookmarkTrashed(Bookmark *bookmark, Bookmark *previousParent); 176 void bookmarkRestored(Bookmark *bookmark); 177 void bookmarkRemoved(Bookmark *bookmark, Bookmark *previousParent); 178 void modelModified(); 179 180 friend class Bookmark; 181 }; 182 183 } 184 185 #endif 186