| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #ifndef ROOTITEM_H |
| 4 | #define ROOTITEM_H |
| 5 | |
| 6 | #include "core/message.h" |
| 7 | |
| 8 | #include <QDateTime> |
| 9 | #include <QFont> |
| 10 | #include <QIcon> |
| 11 | |
| 12 | class Category; |
| 13 | class Feed; |
| 14 | class Label; |
| 15 | class Search; |
| 16 | class ServiceRoot; |
| 17 | class QAction; |
| 18 | |
| 19 | // Represents ROOT item of FeedsModel. |
| 20 | // NOTE: This class is derived to add functionality for |
| 21 | // all other non-root items of FeedsModel. |
| 22 | class RSSGUARD_DLLSPEC RootItem : public QObject { |
| 23 | Q_OBJECT |
| 24 | |
| 25 | // Added for message filtering with labels. |
| 26 | Q_PROPERTY(QString title READ title) |
| 27 | Q_PROPERTY(int id READ id) |
| 28 | Q_PROPERTY(QString customId READ customId) |
| 29 | |
| 30 | public: |
| 31 | enum class ReadStatus { |
| 32 | Unread = 0, |
| 33 | Read = 1, |
| 34 | Unknown = 256 |
| 35 | }; |
| 36 | |
| 37 | // Holds statuses for messages |
| 38 | // to be switched importance (starred). |
| 39 | enum class Importance { |
| 40 | NotImportant = 0, |
| 41 | Important = 1, |
| 42 | Unknown = 256 |
| 43 | }; |
| 44 | |
| 45 | // Describes the kind of the item. |
| 46 | enum class Kind { |
| 47 | Root = 1, |
| 48 | Bin = 2, |
| 49 | Feed = 4, |
| 50 | Category = 8, |
| 51 | ServiceRoot = 16, |
| 52 | Labels = 32, |
| 53 | Important = 64, |
| 54 | Label = 128, |
| 55 | Unread = 256, |
| 56 | Probes = 512, |
| 57 | Probe = 1024 |
| 58 | }; |
| 59 | |
| 60 | // Constructors and destructors. |
| 61 | explicit RootItem(RootItem* parent_item = nullptr); |
| 62 | explicit RootItem(const RootItem& other); |
| 63 | virtual ~RootItem(); |
| 64 | |
| 65 | virtual QString hashCode() const; |
| 66 | virtual QString additionalTooltip() const; |
| 67 | |
| 68 | // Returns list of specific actions which can be done with the item. |
| 69 | // Do not include general actions here like actions: Mark as read, Update, ... |
| 70 | // NOTE: Ownership of returned actions is not switched to caller, free them when needed. |
| 71 | virtual QList<QAction*> contextMenuFeedsList(); |
| 72 | |
| 73 | // Can properties of this item be edited? |
| 74 | virtual bool canBeEdited() const; |
| 75 | |
| 76 | // Can the item be deleted? |
| 77 | virtual bool canBeDeleted() const; |
| 78 | |
| 79 | // Performs deletion of the item, this |
| 80 | // method should NOT display any additional dialogs. |
| 81 | // Returns result status. |
| 82 | virtual bool deleteItem(); |
| 83 | |
| 84 | virtual bool isFetching() const; |
| 85 | |
| 86 | // Performs all needed steps (DB update, remote server update) |
| 87 | // to mark this item as read/unread. |
| 88 | virtual bool markAsReadUnread(ReadStatus status); |
| 89 | |
| 90 | // Get ALL undeleted messages from this item in one single list. |
| 91 | // This is currently used for displaying items in "newspaper mode". |
| 92 | virtual QList<Message> undeletedMessages() const; |
| 93 | |
| 94 | // This method should "clean" all messages it contains. |
| 95 | // |
| 96 | // NOTE: What "clean" means? It means delete messages -> move them to recycle bin |
| 97 | // or eventually remove them completely if there is no recycle bin functionality. |
| 98 | // |
| 99 | // If this method is called on "recycle bin" instance of your |
| 100 | // service account, it should "empty" the recycle bin. |
| 101 | virtual bool cleanMessages(bool clear_only_read); |
| 102 | |
| 103 | // Reloads current counts of articles in this item from DB and |
| 104 | // sets. |
| 105 | virtual void updateCounts(bool including_total_count); |
| 106 | virtual int row() const; |
| 107 | virtual QVariant data(int column, int role) const; |
| 108 | virtual Qt::ItemFlags additionalFlags() const; |
| 109 | virtual bool performDragDropChange(RootItem* target_item); |
| 110 | |
| 111 | // Each item offers "counts" of messages. |
| 112 | // Returns counts of messages of all child items summed up. |
| 113 | virtual int countOfUnreadMessages() const; |
| 114 | virtual int countOfAllMessages() const; |
| 115 | |
| 116 | RootItem* parent() const; |
| 117 | void setParent(RootItem* parent_item); |
| 118 | |
| 119 | // Access to children. |
| 120 | RootItem* child(int row); |
| 121 | int childCount() const; |
| 122 | void appendChild(RootItem* child); |
| 123 | |
| 124 | QList<RootItem*> childItems(RootItem::Kind kind) const; |
| 125 | QList<RootItem*> childItems() const; |
| 126 | QList<RootItem*>& childItems(); |
| 127 | |
| 128 | void clearChildren(); |
| 129 | void setChildItems(const QList<RootItem*>& child_items); |
| 130 | |
| 131 | // Removes particular child at given index. |
| 132 | // NOTE: Child is NOT freed from the memory. |
| 133 | bool removeChild(int index); |
| 134 | bool removeChild(RootItem* child); |
| 135 | |
| 136 | // Checks whether "this" object is child (direct or indirect) |
| 137 | // of the given root. |
| 138 | bool isChildOf(const RootItem* root) const; |
| 139 | |
| 140 | // Is "this" item parent (direct or indirect) if given child? |
| 141 | bool isParentOf(const RootItem* child) const; |
| 142 | |
| 143 | // Returns flat list of all items from subtree where this item is a root. |
| 144 | // Returned list includes this item too. |
| 145 | template <typename T> |
| 146 | QList<T*> getSubTree(std::function<bool(const RootItem*)> tester = nullptr) const; |
| 147 | |
| 148 | QList<RootItem*> getSubTree(RootItem::Kind kind_of_item) const; |
| 149 | QList<Category*> getSubTreeCategories() const; |
| 150 | |
| 151 | RootItem* getItemFromSubTree(std::function<bool(const RootItem*)> tester) const; |
| 152 | |
| 153 | // Returns list of categories complemented by their own integer primary ID. |
| 154 | QHash<int, Category*> getSubTreeCategoriesForAssemble() const; |
| 155 | |
| 156 | // Returns list of categories complemented by their own string CUSTOM ID. |
| 157 | QHash<QString, Category*> getHashedSubTreeCategories() const; |
| 158 | |
| 159 | // Returns list of feeds complemented by their own string CUSTOM ID. |
| 160 | QHash<QString, Feed*> getHashedSubTreeFeeds() const; |
| 161 | |
| 162 | QList<Feed*> getSubTreeFeeds(bool recursive = true) const; |
| 163 | QList<Feed*> getSubTreeAutoFetchingWithManualIntervalsFeeds() const; |
| 164 | QList<Feed*> getSubAutoFetchingEnabledFeeds() const; |
| 165 | |
| 166 | // Returns the service root node which is direct or indirect parent of current item. |
| 167 | ServiceRoot* getParentServiceRoot() const; |
| 168 | |
| 169 | RootItem::Kind kind() const; |
| 170 | void setKind(RootItem::Kind kind); |
| 171 | |
| 172 | // Each item can have icon. |
| 173 | QIcon icon() const; |
| 174 | void setIcon(const QIcon& icon); |
| 175 | |
| 176 | // Returns icon, even if item has "default" icon set, then |
| 177 | // this icon is extra loaded and returned. |
| 178 | QIcon fullIcon() const; |
| 179 | |
| 180 | // This ALWAYS represents primary column number/ID under which |
| 181 | // the item is stored in DB. |
| 182 | int id() const; |
| 183 | |
| 184 | // WARNING: Do not EVER call this method if your "this" object is derived |
| 185 | // from "ServiceRoot"; |
| 186 | void setId(int id); |
| 187 | |
| 188 | // Each item has its title. |
| 189 | QString title() const; |
| 190 | QString sanitizedTitle() const; |
| 191 | void setTitle(const QString& title); |
| 192 | |
| 193 | // This should be in UTC and should be converted to localtime when needed. |
| 194 | QDateTime creationDate() const; |
| 195 | void setCreationDate(const QDateTime& creation_date); |
| 196 | |
| 197 | QString description() const; |
| 198 | void setDescription(const QString& description); |
| 199 | |
| 200 | // NOTE: For standard feed/category, this WILL equal to id(). |
| 201 | QString customId() const; |
| 202 | int customNumericId() const; |
| 203 | void setCustomId(const QString& custom_id); |
| 204 | |
| 205 | // Converters |
| 206 | Category* toCategory() const; |
| 207 | Feed* toFeed() const; |
| 208 | Label* toLabel() const; |
| 209 | Search* toProbe() const; |
| 210 | ServiceRoot* toServiceRoot() const; |
| 211 | |
| 212 | bool keepOnTop() const; |
| 213 | void setKeepOnTop(bool keep_on_top); |
| 214 | |
| 215 | // Sort order, when items in feeds list are sorted manually. |
| 216 | // |
| 217 | // NOTE: This is only used for "Account", "Category" and "Feed" classes |
| 218 | // which can be manually sorted. Other types like "Label" cannot be |
| 219 | // automatically sorted and are always sorted by title. |
| 220 | // |
| 221 | // Sort order number cannot be negative but order of list of items with same |
| 222 | // parent MUST form continuous series AND start with zero, for example: |
| 223 | // 0, 1, 2, 3, 4, ... |
| 224 | // |
| 225 | // NOTE: This is checked with DatabaseQueries::fixupOrders() method on app startup. |
| 226 | int sortOrder() const; |
| 227 | void setSortOrder(int sort_order); |
| 228 | |
| 229 | private: |
| 230 | RootItem::Kind m_kind; |
| 231 | int m_id; |
| 232 | QString m_customId; |
| 233 | QString m_title; |
| 234 | QString m_description; |
| 235 | QIcon m_icon; |
| 236 | QDateTime m_creationDate; |
| 237 | bool m_keepOnTop; |
| 238 | int m_sortOrder; |
| 239 | QList<RootItem*> m_childItems; |
| 240 | RootItem* m_parentItem; |
| 241 | }; |
| 242 | |
| 243 | template <typename T> |
| 244 | QList<T*> RootItem::getSubTree(std::function<bool(const RootItem*)> tester) const { |
| 245 | QList<T*> children; |
| 246 | QList<RootItem*> traversable_items; |
| 247 | |
| 248 | traversable_items.append(const_cast<RootItem* const>(this)); |
| 249 | |
| 250 | // Iterate all nested items. |
| 251 | while (!traversable_items.isEmpty()) { |
| 252 | RootItem* active_item = traversable_items.takeFirst(); |
| 253 | |
| 254 | if (tester) { |
| 255 | if (tester(active_item)) { |
| 256 | children.append(dynamic_cast<T*>(active_item)); |
| 257 | } |
| 258 | } |
| 259 | else { |
| 260 | children.append(dynamic_cast<T*>(active_item)); |
| 261 | } |
| 262 | |
| 263 | traversable_items.append(active_item->childItems()); |
| 264 | } |
| 265 | |
| 266 | return children; |
| 267 | } |
| 268 | |
| 269 | inline RootItem* RootItem::parent() const { |
| 270 | return m_parentItem; |
| 271 | } |
| 272 | |
| 273 | inline void RootItem::setParent(RootItem* parent_item) { |
| 274 | m_parentItem = parent_item; |
| 275 | } |
| 276 | |
| 277 | inline RootItem* RootItem::child(int row) { |
| 278 | return m_childItems.value(row); |
| 279 | } |
| 280 | |
| 281 | inline int RootItem::childCount() const { |
| 282 | return m_childItems.size(); |
| 283 | } |
| 284 | |
| 285 | inline void RootItem::appendChild(RootItem* child) { |
| 286 | if (child != nullptr) { |
| 287 | m_childItems.append(child); |
| 288 | child->setParent(this); |
| 289 | } |
| 290 | } |
| 291 | |
| 292 | inline QList<RootItem*> RootItem::childItems() const { |
| 293 | return m_childItems; |
| 294 | } |
| 295 | |
| 296 | inline QList<RootItem*>& RootItem::childItems() { |
| 297 | return m_childItems; |
| 298 | } |
| 299 | |
| 300 | inline void RootItem::clearChildren() { |
| 301 | m_childItems.clear(); |
| 302 | } |
| 303 | |
| 304 | inline void RootItem::setChildItems(const QList<RootItem*>& child_items) { |
| 305 | clearChildren(); |
| 306 | |
| 307 | for (RootItem* ch : child_items) { |
| 308 | appendChild(ch); |
| 309 | } |
| 310 | } |
| 311 | |
| 312 | RootItem::Kind operator|(RootItem::Kind a, RootItem::Kind b); |
| 313 | RootItem::Kind operator&(RootItem::Kind a, RootItem::Kind b); |
| 314 | |
| 315 | QDataStream& operator<<(QDataStream& out, const RootItem::Importance& myObj); |
| 316 | QDataStream& operator>>(QDataStream& in, RootItem::Importance& myObj); |
| 317 | QDataStream& operator<<(QDataStream& out, const RootItem::ReadStatus& myObj); |
| 318 | QDataStream& operator>>(QDataStream& in, RootItem::ReadStatus& myObj); |
| 319 | |
| 320 | #endif // ROOTITEM_H |