| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #ifndef SERVICEROOT_H |
| 4 | #define SERVICEROOT_H |
| 5 | |
| 6 | #include "core/message.h" |
| 7 | #include "definitions/typedefs.h" |
| 8 | #include "services/abstract/rootitem.h" |
| 9 | |
| 10 | #include <QJsonDocument> |
| 11 | #include <QNetworkProxy> |
| 12 | #include <QPair> |
| 13 | |
| 14 | class QAction; |
| 15 | class QMutex; |
| 16 | class FeedsModel; |
| 17 | class RecycleBin; |
| 18 | class ImportantNode; |
| 19 | class UnreadNode; |
| 20 | class SearchsNode; |
| 21 | class LabelsNode; |
| 22 | class Label; |
| 23 | class MessagesModel; |
| 24 | class CustomMessagePreviewer; |
| 25 | class CacheForServiceRoot; |
| 26 | class FormAccountDetails; |
| 27 | |
| 28 | // THIS IS the root node of the service. |
| 29 | // NOTE: The root usually contains some core functionality of the |
| 30 | // service like service account username/password etc. |
| 31 | class RSSGUARD_DLLSPEC ServiceRoot : public RootItem { |
| 32 | Q_OBJECT |
| 33 | |
| 34 | public: |
| 35 | enum class LabelOperation { |
| 36 | Adding = 1, |
| 37 | Editing = 2, |
| 38 | Deleting = 4, |
| 39 | |
| 40 | // NOTE: Service fetches list of labels from remote source |
| 41 | // and does not use local offline labels. |
| 42 | Synchronised = 8 |
| 43 | }; |
| 44 | |
| 45 | enum class BagOfMessages { |
| 46 | Read, |
| 47 | Unread, |
| 48 | Starred |
| 49 | }; |
| 50 | |
| 51 | public: |
| 52 | explicit ServiceRoot(RootItem* parent = nullptr); |
| 53 | virtual ~ServiceRoot(); |
| 54 | |
| 55 | // These methods bellow are part of "interface". |
| 56 | RecycleBin* recycleBin() const; |
| 57 | ImportantNode* importantNode() const; |
| 58 | LabelsNode* labelsNode() const; |
| 59 | SearchsNode* probesNode() const; |
| 60 | UnreadNode* unreadNode() const; |
| 61 | |
| 62 | virtual FormAccountDetails* accountSetupDialog() const; |
| 63 | virtual void onDatabaseCleanup(); |
| 64 | virtual void updateCounts(bool including_total_count); |
| 65 | virtual bool canBeDeleted() const; |
| 66 | virtual bool deleteItem(); |
| 67 | virtual void editItems(const QList<RootItem*>& items); |
| 68 | virtual bool markAsReadUnread(ReadStatus status); |
| 69 | virtual QList<Message> undeletedMessages() const; |
| 70 | virtual bool supportsFeedAdding() const; |
| 71 | virtual bool supportsCategoryAdding() const; |
| 72 | virtual LabelOperation supportedLabelOperations() const; |
| 73 | virtual QString additionalTooltip() const; |
| 74 | virtual void saveAccountDataToDatabase(); |
| 75 | virtual QVariantHash customDatabaseData() const; |
| 76 | virtual void setCustomDatabaseData(const QVariantHash& data); |
| 77 | virtual bool wantsBaggedIdsOfExistingMessages() const; |
| 78 | virtual bool displaysEnclosures() const; |
| 79 | virtual void aboutToBeginFeedFetching(const QList<Feed*>& feeds, |
| 80 | const QHash<QString, QHash<ServiceRoot::BagOfMessages, QStringList>>& |
| 81 | stated_messages, |
| 82 | const QHash<QString, QStringList>& tagged_messages); |
| 83 | |
| 84 | // Returns list of specific actions for "Add new item" main window menu. |
| 85 | // So typical list of returned actions could look like: |
| 86 | // a) Add new feed |
| 87 | // b) Add new category |
| 88 | // c) ... |
| 89 | // NOTE: Caller does NOT take ownership of created menu/actions! |
| 90 | virtual QList<QAction*> addItemMenu(); |
| 91 | |
| 92 | // NOTE: Caller does NOT take ownership of created menu/actions! |
| 93 | virtual QList<QAction*> contextMenuFeedsList(); |
| 94 | |
| 95 | // NOTE: Caller does NOT take ownership of created menu/actions! |
| 96 | virtual QList<QAction*> contextMenuMessagesList(const QList<Message>& messages); |
| 97 | |
| 98 | // Returns list of specific actions to be shown in main window menu |
| 99 | // bar in sections "Services -> 'this service'". |
| 100 | // NOTE: Caller does NOT take ownership of created menu! |
| 101 | virtual QList<QAction*> serviceMenu(); |
| 102 | |
| 103 | // If plugin uses online synchronization of feeds/labels/etc, then returns true. |
| 104 | virtual bool isSyncable() const; |
| 105 | |
| 106 | // Start/stop services. |
| 107 | // Start method is called when feed model gets initialized OR after user adds new service. |
| 108 | // Account should synchronously initialize its children (load them from DB is recommended |
| 109 | // here). |
| 110 | // |
| 111 | // Stop method is called just before application exits OR when |
| 112 | // user explicitly deletes existing service instance. |
| 113 | virtual void start(bool freshly_activated); |
| 114 | virtual void stop(); |
| 115 | |
| 116 | // Obtains list of messages. |
| 117 | // Throws exception subclassed from ApplicationException, preferably FeedFetchException |
| 118 | // if any problems arise. |
| 119 | virtual QList<Message> obtainNewMessages(Feed* feed, |
| 120 | const QHash<ServiceRoot::BagOfMessages, QStringList>& stated_messages, |
| 121 | const QHash<QString, QStringList>& tagged_messages) = 0; |
| 122 | |
| 123 | // Returns special widget to display articles of this account type. |
| 124 | // Caller does NOT free returned previewer after usage from memory, |
| 125 | // it only may hide it. |
| 126 | // Thus, account is responsible to free any custom previewers. |
| 127 | virtual CustomMessagePreviewer* customMessagePreviewer(); |
| 128 | |
| 129 | // This method should load messages for given "item" into model. |
| 130 | virtual bool loadMessagesForItem(RootItem* item, MessagesModel* model); |
| 131 | |
| 132 | // Called BEFORE this read status update (triggered by user in message list) is stored in DB, |
| 133 | // when false is returned, change is aborted. |
| 134 | // This is the place to make some other changes like updating |
| 135 | // some ONLINE service or something. |
| 136 | // |
| 137 | // "read" is status which is ABOUT TO BE SET. |
| 138 | virtual bool onBeforeSetMessagesRead(RootItem* selected_item, const QList<Message>& messages, ReadStatus read); |
| 139 | |
| 140 | // Called AFTER this read status update (triggered by user in message list) is stored in DB, |
| 141 | // when false is returned, change is aborted. |
| 142 | // Here service root should inform (via signals) |
| 143 | // which items are actually changed. |
| 144 | // |
| 145 | // "read" is status which is ABOUT TO BE SET. |
| 146 | virtual bool onAfterSetMessagesRead(RootItem* selected_item, const QList<Message>& messages, ReadStatus read); |
| 147 | |
| 148 | // Called BEFORE this importance switch update is stored in DB, |
| 149 | // when false is returned, change is aborted. |
| 150 | // This is the place to make some other changes like updating |
| 151 | // some ONLINE service or something. |
| 152 | // |
| 153 | // "changes" - list of pairs - <message (integer id), new status> |
| 154 | virtual bool onBeforeSwitchMessageImportance(RootItem* selected_item, const QList<ImportanceChange>& changes); |
| 155 | |
| 156 | // Called AFTER this importance switch update is stored in DB, |
| 157 | // when false is returned, change is aborted. |
| 158 | // Here service root should inform (via signals) |
| 159 | // which items are actually changed. |
| 160 | // |
| 161 | // "changes" - list of pairs - <message (integer id), new status> |
| 162 | virtual bool onAfterSwitchMessageImportance(RootItem* selected_item, const QList<ImportanceChange>& changes); |
| 163 | |
| 164 | // Called BEFORE the list of messages is about to be deleted |
| 165 | // by the user from message list. |
| 166 | virtual bool onBeforeMessagesDelete(RootItem* selected_item, const QList<Message>& messages); |
| 167 | |
| 168 | // Called AFTER the list of messages was deleted |
| 169 | // by the user from message list. |
| 170 | virtual bool onAfterMessagesDelete(RootItem* selected_item, const QList<Message>& messages); |
| 171 | |
| 172 | // Called BEFORE some labels are assigned/deassigned from/to messages. |
| 173 | virtual bool onBeforeLabelMessageAssignmentChanged(const QList<Label*>& labels, |
| 174 | const QList<Message>& messages, |
| 175 | bool assign); |
| 176 | |
| 177 | // Called AFTER some labels are assigned/deassigned from/to messages. |
| 178 | virtual bool onAfterLabelMessageAssignmentChanged(const QList<Label*>& labels, |
| 179 | const QList<Message>& messages, |
| 180 | bool assign); |
| 181 | |
| 182 | // Called BEFORE the list of messages is about to be restored from recycle bin |
| 183 | // by the user from message list. |
| 184 | // Selected item is naturally recycle bin. |
| 185 | virtual bool onBeforeMessagesRestoredFromBin(RootItem* selected_item, const QList<Message>& messages); |
| 186 | |
| 187 | // Called AFTER the list of messages was restored from recycle bin |
| 188 | // by the user from message list. |
| 189 | // Selected item is naturally recycle bin. |
| 190 | virtual bool onAfterMessagesRestoredFromBin(RootItem* selected_item, const QList<Message>& messages); |
| 191 | |
| 192 | // Called AFTER articles of these feeds are purged from the database. |
| 193 | virtual void onAfterFeedsPurged(const QList<Feed*>& feeds); |
| 194 | |
| 195 | // Returns the UNIQUE code of the given service. |
| 196 | // NOTE: Keep in sync with ServiceEntryRoot::code(). |
| 197 | virtual QString code() const = 0; |
| 198 | |
| 199 | public: |
| 200 | CacheForServiceRoot* toCache() const; |
| 201 | |
| 202 | // Account ID corresponds with DB attribute Accounts (id). |
| 203 | int accountId() const; |
| 204 | void setAccountId(int account_id); |
| 205 | |
| 206 | QNetworkProxy networkProxy() const; |
| 207 | void setNetworkProxy(const QNetworkProxy& network_proxy); |
| 208 | |
| 209 | // Removes all data associated with this account from DB |
| 210 | // and from model. |
| 211 | void completelyRemoveAllData(); |
| 212 | |
| 213 | // Returns counts of updated messages <unread, all>. |
| 214 | UpdatedArticles updateMessages(QList<Message>& messages, Feed* feed, bool force_update, QMutex* db_mutex); |
| 215 | |
| 216 | QIcon feedIconForMessage(const QString& feed_custom_id) const; |
| 217 | |
| 218 | // Removes all/read only messages from given underlying feeds. |
| 219 | bool cleanFeeds(const QList<Feed*>& items, bool clean_read_only); |
| 220 | |
| 221 | // Marks all messages from feeds read/unread. |
| 222 | bool markFeedsReadUnread(const QList<Feed*>& items, ReadStatus read); |
| 223 | |
| 224 | // Obvious methods to wrap signals. |
| 225 | void itemChanged(const QList<RootItem*>& items); |
| 226 | void requestReloadMessageList(bool mark_selected_messages_read); |
| 227 | void requestItemExpand(const QList<RootItem*>& items, bool expand); |
| 228 | void requestItemExpandStateSave(RootItem* subtree_root); |
| 229 | void requestItemReassignment(RootItem* item, RootItem* new_parent); |
| 230 | void requestItemsReassignment(const QList<RootItem*>& items, RootItem* new_parent); |
| 231 | void requestItemRemoval(RootItem* item); |
| 232 | |
| 233 | // Some message/feed attribute selectors. |
| 234 | QStringList textualFeedUrls(const QList<Feed*>& feeds) const; |
| 235 | QStringList textualFeedIds(const QList<Feed*>& feeds) const; |
| 236 | QStringList customIDsOfMessages(const QList<ImportanceChange>& changes); |
| 237 | QStringList customIDsOfMessages(const QList<Message>& messages); |
| 238 | |
| 239 | // Returns list of article IDs depending on what target operation is. |
| 240 | // NOTE: So if we want to mark some articles as read, |
| 241 | // then we only return UNREAD IDs here to really return |
| 242 | // only IDs when the change makes sense. |
| 243 | // NOTE: Importance is not dealt here because it was not needed |
| 244 | // yet. |
| 245 | QStringList customIDSOfMessagesForItem(RootItem* item, |
| 246 | RootItem::ReadStatus target_read = RootItem::ReadStatus::Unknown); |
| 247 | |
| 248 | void performInitialAssembly(const Assignment& categories, |
| 249 | const Assignment& feeds, |
| 250 | const QList<Label*>& labels, |
| 251 | const QList<Search*>& probes); |
| 252 | |
| 253 | bool nodeShowUnread() const; |
| 254 | void setNodeShowUnread(bool enabled); |
| 255 | |
| 256 | bool nodeShowImportant() const; |
| 257 | void setNodeShowImportant(bool enabled); |
| 258 | |
| 259 | bool nodeShowLabels() const; |
| 260 | void setNodeShowLabels(bool enabled); |
| 261 | |
| 262 | bool nodeShowProbes() const; |
| 263 | void setNodeShowProbes(bool enabled); |
| 264 | |
| 265 | public slots: |
| 266 | virtual void addNewFeed(RootItem* selected_item, const QString& url = QString()); |
| 267 | virtual void addNewCategory(RootItem* selected_item); |
| 268 | virtual void syncIn(); |
| 269 | |
| 270 | protected: |
| 271 | // This method should obtain new tree of feed/categories/whatever to perform sync in. |
| 272 | virtual RootItem* obtainNewTreeForSyncIn() const; |
| 273 | |
| 274 | // Removes all messages/categories/feeds which are |
| 275 | // associated with this account. |
| 276 | void removeOldAccountFromDatabase(bool delete_messages_too, bool delete_labels_too); |
| 277 | void cleanAllItemsFromModel(bool clean_labels_too); |
| 278 | void appendCommonNodes(); |
| 279 | |
| 280 | // Removes messages which do not belong to any |
| 281 | // existing feed. |
| 282 | // |
| 283 | // NOTE: This situation may happen if user deletes some feed |
| 284 | // from another machine and then performs sync-in on this machine. |
| 285 | void removeLeftOverMessages(); |
| 286 | |
| 287 | // Removes all msg. filter assignments which are (within this account) |
| 288 | // assigned to feed (via custom ID) which does not exist anymore. |
| 289 | // |
| 290 | // NOTE: This situation may happen if user deletes some feed |
| 291 | // from another machine and then performs sync-in on this machine. |
| 292 | void removeLeftOverMessageFilterAssignments(); |
| 293 | |
| 294 | // Takes lists of feeds/categories and assembles them into the tree structure. |
| 295 | void assembleCategories(const Assignment& categories); |
| 296 | void assembleFeeds(const Assignment& feeds); |
| 297 | |
| 298 | signals: |
| 299 | void proxyChanged(QNetworkProxy proxy); |
| 300 | void dataChanged(QList<RootItem*> items); |
| 301 | void reloadMessageListRequested(bool mark_selected_messages_read); |
| 302 | void itemExpandRequested(QList<RootItem*> items, bool expand); |
| 303 | void itemExpandStateSaveRequested(RootItem* subtree_root); |
| 304 | |
| 305 | void itemReassignmentRequested(RootItem* item, RootItem* new_parent); |
| 306 | void itemRemovalRequested(RootItem* item); |
| 307 | |
| 308 | private: |
| 309 | void resortAccountTree(RootItem* tree, |
| 310 | const QMap<QString, QVariantMap>& custom_category_data, |
| 311 | const QMap<QString, QVariantMap>& custom_feed_data) const; |
| 312 | |
| 313 | // Key is feed's custom ID. |
| 314 | virtual QMap<QString, QVariantMap> storeCustomFeedsData(); |
| 315 | |
| 316 | // Key is category's custom ID. |
| 317 | virtual QMap<QString, QVariantMap> storeCustomCategoriesData(); |
| 318 | |
| 319 | virtual void restoreCustomFeedsData(const QMap<QString, QVariantMap>& data, const QHash<QString, Feed*>& feeds); |
| 320 | virtual void restoreCustomCategoriesData(const QMap<QString, QVariantMap>& data, |
| 321 | const QHash<QString, Category*>& cats); |
| 322 | |
| 323 | protected: |
| 324 | RecycleBin* m_recycleBin; |
| 325 | ImportantNode* m_importantNode; |
| 326 | LabelsNode* m_labelsNode; |
| 327 | SearchsNode* m_probesNode; |
| 328 | UnreadNode* m_unreadNode; |
| 329 | int m_accountId; |
| 330 | QList<QAction*> m_serviceMenu; |
| 331 | QNetworkProxy m_networkProxy; |
| 332 | bool m_nodeShowUnread; |
| 333 | bool m_nodeShowImportant; |
| 334 | bool m_nodeShowLabels; |
| 335 | bool m_nodeShowProbes; |
| 336 | }; |
| 337 | |
| 338 | #if QT_VERSION_MAJOR == 6 |
| 339 | inline size_t qHash(ServiceRoot::BagOfMessages key, size_t seed) { |
| 340 | return ::qHash(static_cast<uint>(key), seed); |
| 341 | } |
| 342 | |
| 343 | #else |
| 344 | inline uint qHash(ServiceRoot::BagOfMessages key, uint seed) { |
| 345 | return ::qHash(static_cast<uint>(key), seed); |
| 346 | } |
| 347 | |
| 348 | #endif |
| 349 | |
| 350 | ServiceRoot::LabelOperation operator|(ServiceRoot::LabelOperation lhs, ServiceRoot::LabelOperation rhs); |
| 351 | ServiceRoot::LabelOperation operator&(ServiceRoot::LabelOperation lhs, ServiceRoot::LabelOperation rhs); |
| 352 | |
| 353 | #endif // SERVICEROOT_H |