1 // For license of this file, see <project-root-folder>/LICENSE.md.
2 
3 #ifndef MESSAGEOBJECT_H
4 #define MESSAGEOBJECT_H
5 
6 #include <QObject>
7 
8 #include "services/abstract/label.h"
9 
10 class MessageObject : public QObject {
11   Q_OBJECT
12 
13   Q_PROPERTY(QList<Label*> assignedLabels READ assignedLabels)
14   Q_PROPERTY(QList<Label*> availableLabels READ availableLabels)
15   Q_PROPERTY(QString feedCustomId READ feedCustomId)
16   Q_PROPERTY(int accountId READ accountId)
17   Q_PROPERTY(int id READ id)
18   Q_PROPERTY(QString customId READ customId)
19   Q_PROPERTY(QString title READ title WRITE setTitle)
20   Q_PROPERTY(QString url READ url WRITE setUrl)
21   Q_PROPERTY(QString author READ author WRITE setAuthor)
22   Q_PROPERTY(QString contents READ contents WRITE setContents)
23   Q_PROPERTY(QString rawContents READ rawContents WRITE setRawContents)
24   Q_PROPERTY(QDateTime created READ created WRITE setCreated)
25   Q_PROPERTY(double score READ score WRITE setScore)
26   Q_PROPERTY(bool isRead READ isRead WRITE setIsRead)
27   Q_PROPERTY(bool isImportant READ isImportant WRITE setIsImportant)
28   Q_PROPERTY(bool isDeleted READ isDeleted WRITE setIsDeleted)
29   Q_PROPERTY(bool runningFilterWhenFetching READ runningFilterWhenFetching)
30 
31   public:
32     enum class FilteringAction {
33       // Message is normally accepted and stored in DB or updated.
34       Accept = 1,
35 
36       // Message is ignored and will not be stored in DB but is not purged if it already exists.
37       Ignore = 2,
38 
39       // Message is purged from DB if it already exists.
40       Purge = 4
41     };
42 
43     Q_ENUM(FilteringAction)
44 
45     enum class DuplicationAttributeCheck {
46       // Message with same title in DB.
47       SameTitle = 1,
48 
49       // Message with same URL in DB.
50       SameUrl = 2,
51 
52       // Message with same author in DB.
53       SameAuthor = 4,
54 
55       // Messages with same creation date in DB.
56       SameDateCreated = 8,
57 
58       // Compare with all messages from the account not only with messages from same feed.
59       // Note that this value must be used via bitwise OR with other values,
60       // for example 2 | 4 | 16.
61       AllFeedsSameAccount = 16,
62 
63       // Messages with same custom ID as provided by feed/service.
64       SameCustomId = 32
65     };
66 
67     Q_ENUM(DuplicationAttributeCheck)
68 
69     explicit MessageObject(QSqlDatabase* db,
70                            const QString& feed_custom_id,
71                            int account_id,
72                            const QList<Label*>& available_labels,
73                            bool is_new_message,
74                            QObject* parent = nullptr);
75 
76     void setMessage(Message* message);
77 
78     // Check if message is duplicate with another messages in DB.
79     // Parameter "attribute_check" is DuplicationAttributeCheck enum
80     // value casted to int.
81     Q_INVOKABLE bool isDuplicate(MessageObject::DuplicationAttributeCheck attribute_check) const;
82     Q_INVOKABLE bool isDuplicateWithAttribute(MessageObject::DuplicationAttributeCheck attribute_check) const;
83 
84     // Adds given label to list of assigned labels to this message.
85     // Returns true if label was assigned now or if the message already has it assigned.
86     Q_INVOKABLE bool assignLabel(const QString& label_custom_id) const;
87 
88     // Removes given label from list of assigned labels of this message.
89     // Returns true if label was now removed or if it is not assigned to the message at all.
90     Q_INVOKABLE bool deassignLabel(const QString& label_custom_id) const;
91 
92     // Returns list of assigned and available messages.
93     QList<Label*> assignedLabels() const;
94     QList<Label*> availableLabels() const;
95 
96     bool runningFilterWhenFetching() const;
97 
98     // Generic Message's properties bindings.
99     QString feedCustomId() const;
100 
101     int accountId() const;
102 
103     QString customId() const;
104 
105     int id() const;
106 
107     QString title() const;
108     void setTitle(const QString& title);
109 
110     QString url() const;
111     void setUrl(const QString& url);
112 
113     QString author() const;
114     void setAuthor(const QString& author);
115 
116     QString contents() const;
117     void setContents(const QString& contents);
118 
119     QString rawContents() const;
120     void setRawContents(const QString& raw_contents);
121 
122     QDateTime created() const;
123     void setCreated(const QDateTime& created);
124 
125     bool isRead() const;
126     void setIsRead(bool is_read);
127 
128     bool isImportant() const;
129     void setIsImportant(bool is_important);
130 
131     bool isDeleted() const;
132     void setIsDeleted(bool is_deleted);
133 
134     double score() const;
135     void setScore(double score);
136 
137   private:
138     QSqlDatabase* m_db;
139     QString m_feedCustomId;
140     int m_accountId;
141     Message* m_message;
142     QList<Label*> m_availableLabels;
143     bool m_runningAfterFetching;
144 };
145 
146 inline MessageObject::DuplicationAttributeCheck operator|(MessageObject::DuplicationAttributeCheck lhs,
147                                                           MessageObject::DuplicationAttributeCheck rhs) {
148   return static_cast<MessageObject::DuplicationAttributeCheck>(int(lhs) | int(rhs));
149 }
150 
151 inline MessageObject::DuplicationAttributeCheck operator&(MessageObject::DuplicationAttributeCheck lhs,
152                                                           MessageObject::DuplicationAttributeCheck rhs) {
153   return static_cast<MessageObject::DuplicationAttributeCheck>(int(lhs) & int(rhs));
154 }
155 
156 #endif // MESSAGEOBJECT_H
157