Line data Source code
1 : #ifndef FEEDITEM_H
2 : #define FEEDITEM_H
3 :
4 : #include <QString>
5 : #include <QUrl>
6 : #include <QDateTime>
7 : #include <QVariant>
8 : #include <QTextStream>
9 :
10 : #include "DBObject.h"
11 : #include "ListModel.h"
12 : #include "src/models/NewsList.h"
13 : #include "../utilities/ErrorHandling.h"
14 :
15 : class NewsItem;
16 : class NewsList;
17 :
18 : // IDs of special feeds. (Normal feed IDs start at 0.)
19 :
20 : enum SpecialFeedID {
21 : FEED_ID_ALLNEWS = -2,
22 : FEED_ID_PINNED = -1
23 : };
24 :
25 : class FeedItem : public ListItem, DBObject
26 : {
27 : Q_OBJECT
28 :
29 : public:
30 :
31 : enum Roles {
32 : TitleRole = Qt::UserRole+1,
33 : SubtitleRole,
34 : LastUpdatedRole,
35 : MinutesToUpdateRole,
36 : UrlRole,
37 : SiteURLRole,
38 : ImageURLRole,
39 : IsUpdatingRole,
40 : UnreadCountRole,
41 : DropTargetRole,
42 : ErrorFlagRole,
43 : IsSelectedRole,
44 : IsSpecialFeedRole,
45 : IsFolderRole,
46 : ParentFolderRole,
47 : FolderOpenRole,
48 : BookmarksEnabledRole,
49 : UIDRole,
50 : SelfRole
51 : };
52 :
53 : FeedItem(QObject *parent = nullptr);
54 :
55 : // This class is immutable, so this is the c'tor you'll want to use.
56 : explicit FeedItem(
57 : const qint64 id,
58 : const qint32 ordinal,
59 : const QString& title,
60 : const QString& subtitle,
61 : const QDateTime& lastUpdated,
62 : quint32 minutesToUpdate,
63 : const QUrl& url,
64 : const QUrl& siteURL,
65 : const QString& userURL,
66 : const QUrl& imageURL,
67 : const QDateTime& lastIconUpdate,
68 : qint64 parentFolder = -1, // Default values for top level non-folder.
69 : bool folderOpen = true, // TODO: Save folder open state from last session.
70 : QObject *parent = nullptr);
71 :
72 : virtual ~FeedItem();
73 :
74 : /**
75 : * @brief Ordinal-based sort, used for re-sorting the list.
76 : * @param rhs
77 : * @return
78 : */
79 : bool operator<(const FeedItem& rhs);
80 :
81 : QVariant data(int role) const;
82 : bool setData(const QVariant &value, int role);
83 : Qt::ItemFlags flags() const;
84 : QHash<int, QByteArray> roleNames() const;
85 :
86 : void setIsUpdating(bool isUpdating);
87 :
88 : // Unused, required for ListItem base class.
89 0 : inline QString id() const {
90 0 : QString ret;
91 0 : QTextStream output(&ret);
92 0 : output << "FeedItem_" << _id;
93 0 : return ret;
94 0 : }
95 :
96 : public slots:
97 :
98 17 : inline QString getTitle() const { return title; }
99 4 : inline QString getSubtitle() const { return subtitle; }
100 3 : inline QDateTime getLastUpdated() const { return lastUpdated; }
101 4 : inline quint32 getMinutesToUpdate() const { return minutesToUpdate; }
102 29 : inline QUrl getURL() const { return url; }
103 14 : inline QUrl getSiteURL() const { return siteURL; }
104 2 : inline QString getUserURL() const { return userURL; }
105 4 : inline QUrl getImageURL() const { return imageURL; }
106 20 : inline int getIsUpdating() const { return isUpdating; }
107 4 : inline quint32 getUnreadCount() const { return unreadCount; }
108 5 : inline QString getDropTarget() const { return dropTarget; }
109 2 : inline FeedItem* getSelf() const { return const_cast<FeedItem*>(this); }
110 88 : virtual inline qint64 getDbID() const { return _id; }
111 4 : inline bool isSpecialFeed() const { return _id < 0; }
112 :
113 : void setImageURL(const QUrl& url);
114 :
115 : void setTitle(const QString& newTitle);
116 :
117 : void setDropTarget(const QString& dropTarget);
118 :
119 : void setIsSelected(bool s);
120 :
121 : // Override for folders.
122 2 : virtual bool isFolder() const { return false; }
123 0 : virtual void setIsFolder(bool isFolder) {
124 : Q_UNUSED(isFolder);
125 0 : qCritical() << "FeedItem::setIsFolder: Unsupported on base type";
126 0 : }
127 :
128 : void setParentFolder(qint64 parentFolder);
129 : void setFolderOpen(bool folderOpen);
130 :
131 : /**
132 : * @brief Clears all news items. Does NOT signal.
133 : * @return
134 : */
135 : virtual void clearNews();
136 :
137 : /**
138 : * @brief Provides direct access to the news list.
139 : *
140 : * Note: This represents news items that are loaded and can be viewed. As such it will
141 : * slowly grow as more news items are added to the feed.
142 : */
143 1 : inline NewsList* getNewsList() { return &newsList; }
144 :
145 : /**
146 : * @brief Used to set the bookmark internally. External classes shouldn't need to call this.
147 : * @param bookmark Item ID or -1 to clear.t
148 : */
149 : virtual void setBookmark(qint64 toBookmarkID);
150 :
151 : /**
152 : * @param item
153 : * @param allowBackward
154 : * @return True if this item can be bookmarked.
155 : */
156 : virtual bool canBookmark(qint64 proposedBookmarkID, bool allowBackward);
157 :
158 : /**
159 : * @brief Returns the current bookmark ID or -1 if none.
160 : * @return
161 : */
162 3 : inline qint64 getBookmarkID() const { return _bookmarkID; }
163 :
164 : /**
165 : * @brief Detaches the feed ID when this feed is being disconnected.
166 : */
167 0 : inline void clearDbId() { _id = -100; }
168 :
169 : /**
170 : * @return The ID of the very first item in the feed that we have in our database;
171 : * not the ID that's necessarily currently loaded.
172 : */
173 3 : inline qint64 getFirstNewsID() { return firstNewsID; }
174 :
175 : /**
176 : * @brief setFirstNewsID Sets the first item available in the feed. This is used
177 : * to tell if we should draw the top bookmark to unbookmark all.
178 : * @param id
179 : */
180 1 : inline void setFirstNewsID(qint64 id) { firstNewsID = id; }
181 :
182 : /**
183 : * @brief Sets the unread count to the new value and emits a change signal.
184 : * @param unreadCount
185 : */
186 : void setUnreadCount(qint32 unreadCount);
187 :
188 : /**
189 : * @brief Returns the ordinal (note: only really used in resorting the list.)
190 : * @return
191 : */
192 6 : inline int getOrdinal() const { return ordinal; }
193 :
194 : /**
195 : * @brief Sets the ordinal.
196 : */
197 : void setOrdinal(int newOrdinal);
198 :
199 : /**
200 : * @brief Sets the optional error flag. Used in batch import.
201 : * @param errorFlag
202 : */
203 : void setErrorFlag(bool errorFlag);
204 :
205 : /**
206 : * @return True if error flag is set, else false.
207 : */
208 15 : inline bool getErrorFlag() const { return _errorFlag; }
209 :
210 : /**
211 : * @brief Changes the URL.
212 : * @param url
213 : */
214 : void setURL(QUrl url);
215 :
216 : /**
217 : * @return True if this item is selected.
218 : */
219 20 : inline bool getIsSelected() const { return isSelected; }
220 :
221 0 : void setLastIconUpdate(QDateTime last) { lastIconUpdate = last; }
222 1 : QDateTime getLastIconUpdate() { return lastIconUpdate; }
223 :
224 : /**
225 : * @brief If true, the feed supports bookmarks. Otherwise false.
226 : * @return By default, returns true. Override if you want it to return false.
227 : */
228 : virtual bool bookmarksEnabled() const;
229 :
230 : /**
231 : * @brief getParentFolderID
232 : * @return The ID of the parent folder, or -1 if none.
233 : */
234 4 : qint64 getParentFolderID() const {
235 4 : return _parentFolder;
236 : }
237 :
238 : signals:
239 :
240 : void appended(NewsItem* item);
241 : void removed(NewsItem* item);
242 : void titleChanged();
243 : void unreadCountChanged(quint32 unread);
244 : void folderOpenChanged();
245 :
246 : private:
247 : qint64 _id;
248 : qint32 ordinal;
249 : QString title;
250 : QString subtitle;
251 : QDateTime lastUpdated;
252 : quint32 minutesToUpdate;
253 : QUrl url;
254 : QUrl siteURL;
255 : QString userURL;
256 : QUrl imageURL;
257 : NewsList newsList;
258 : int isUpdating;
259 : qint32 unreadCount;
260 : qint64 _bookmarkID;
261 : QString dropTarget;
262 : bool _errorFlag;
263 : bool isSelected;
264 : QDateTime lastIconUpdate;
265 : qint64 firstNewsID;
266 : qint64 _parentFolder;
267 : bool _folderOpen;
268 : };
269 :
270 : #endif // FEEDITEM_H
|