1 /*
2  *  Copyright (C) 2012 Felix Geyer <debfx@fobos.de>
3  *
4  *  This program is free software: you can redistribute it and/or modify
5  *  it under the terms of the GNU General Public License as published by
6  *  the Free Software Foundation, either version 2 or (at your option)
7  *  version 3 of the License.
8  *
9  *  This program is distributed in the hope that it will be useful,
10  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  *  GNU General Public License for more details.
13  *
14  *  You should have received a copy of the GNU General Public License
15  *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
16  */
17 
18 #include "EntryAttachments.h"
19 
20 #include "core/Global.h"
21 
22 #include <QSet>
23 #include <QStringList>
24 
EntryAttachments(QObject * parent)25 EntryAttachments::EntryAttachments(QObject* parent)
26     : QObject(parent)
27 {
28 }
29 
keys() const30 QList<QString> EntryAttachments::keys() const
31 {
32     return m_attachments.keys();
33 }
34 
hasKey(const QString & key) const35 bool EntryAttachments::hasKey(const QString& key) const
36 {
37     return m_attachments.contains(key);
38 }
39 
values() const40 QSet<QByteArray> EntryAttachments::values() const
41 {
42     return asConst(m_attachments).values().toSet();
43 }
44 
value(const QString & key) const45 QByteArray EntryAttachments::value(const QString& key) const
46 {
47     return m_attachments.value(key);
48 }
49 
set(const QString & key,const QByteArray & value)50 void EntryAttachments::set(const QString& key, const QByteArray& value)
51 {
52     bool emitModified = false;
53     bool addAttachment = !m_attachments.contains(key);
54 
55     if (addAttachment) {
56         emit aboutToBeAdded(key);
57     }
58 
59     if (addAttachment || m_attachments.value(key) != value) {
60         m_attachments.insert(key, value);
61         emitModified = true;
62     }
63 
64     if (addAttachment) {
65         emit added(key);
66     } else {
67         emit keyModified(key);
68     }
69 
70     if (emitModified) {
71         emit entryAttachmentsModified();
72     }
73 }
74 
remove(const QString & key)75 void EntryAttachments::remove(const QString& key)
76 {
77     if (!m_attachments.contains(key)) {
78         Q_ASSERT_X(false, "EntryAttachments::remove", qPrintable(QString("Can't find attachment for key %1").arg(key)));
79         return;
80     }
81 
82     emit aboutToBeRemoved(key);
83 
84     m_attachments.remove(key);
85 
86     emit removed(key);
87     emit entryAttachmentsModified();
88 }
89 
remove(const QStringList & keys)90 void EntryAttachments::remove(const QStringList& keys)
91 {
92     if (keys.isEmpty()) {
93         return;
94     }
95 
96     bool isModified = false;
97     for (const QString& key : keys) {
98         if (!m_attachments.contains(key)) {
99             Q_ASSERT_X(
100                 false, "EntryAttachments::remove", qPrintable(QString("Can't find attachment for key %1").arg(key)));
101             continue;
102         }
103 
104         isModified = true;
105         emit aboutToBeRemoved(key);
106         m_attachments.remove(key);
107         emit removed(key);
108     }
109 
110     if (isModified) {
111         emit entryAttachmentsModified();
112     }
113 }
114 
isEmpty() const115 bool EntryAttachments::isEmpty() const
116 {
117     return m_attachments.isEmpty();
118 }
119 
clear()120 void EntryAttachments::clear()
121 {
122     if (m_attachments.isEmpty()) {
123         return;
124     }
125 
126     emit aboutToBeReset();
127 
128     m_attachments.clear();
129 
130     emit reset();
131     emit entryAttachmentsModified();
132 }
133 
copyDataFrom(const EntryAttachments * other)134 void EntryAttachments::copyDataFrom(const EntryAttachments* other)
135 {
136     if (*this != *other) {
137         emit aboutToBeReset();
138 
139         m_attachments = other->m_attachments;
140 
141         emit reset();
142         emit entryAttachmentsModified();
143     }
144 }
145 
operator ==(const EntryAttachments & other) const146 bool EntryAttachments::operator==(const EntryAttachments& other) const
147 {
148     return m_attachments == other.m_attachments;
149 }
150 
operator !=(const EntryAttachments & other) const151 bool EntryAttachments::operator!=(const EntryAttachments& other) const
152 {
153     return m_attachments != other.m_attachments;
154 }
155 
attachmentsSize() const156 int EntryAttachments::attachmentsSize() const
157 {
158     int size = 0;
159     for (auto it = m_attachments.constBegin(); it != m_attachments.constEnd(); ++it) {
160         size += it.key().toUtf8().size() + it.value().size();
161     }
162     return size;
163 }
164