1 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net>
2 Copyright (C) 2012 Mohammed Nafees <nafees.technocool@gmail.com>
3 Copyright (C) 2012 Peter Amidon <peter@picnicpark.org>
4
5 This file is part of the Trojita Qt IMAP e-mail client,
6 http://trojita.flaska.net/
7
8 This program is free software; you can redistribute it and/or
9 modify it under the terms of the GNU General Public License as
10 published by the Free Software Foundation; either version 2 of
11 the License or (at your option) version 3 or any later version
12 accepted by the membership of KDE e.V. (or its successor approved
13 by the membership of KDE e.V.), which shall act as a proxy
14 defined in Section 14 of version 3 of the license.
15
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
20
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 */
24
25 #include "SenderIdentitiesModel.h"
26 #include <QSettings>
27 #include "Common/SettingsNames.h"
28
29 namespace Composer
30 {
31
ItemSenderIdentity(const QString & realName,const QString & emailAddress,const QString & organisation,const QString & signature)32 ItemSenderIdentity::ItemSenderIdentity(const QString &realName, const QString &emailAddress,
33 const QString &organisation, const QString &signature):
34 realName(realName), emailAddress(emailAddress), organisation(organisation), signature(signature)
35 {
36 }
37
ItemSenderIdentity()38 ItemSenderIdentity::ItemSenderIdentity()
39 {
40 }
41
SenderIdentitiesModel(QObject * parent)42 SenderIdentitiesModel::SenderIdentitiesModel(QObject *parent) :
43 QAbstractTableModel(parent)
44 {
45 }
46
columnCount(const QModelIndex & parent) const47 int SenderIdentitiesModel::columnCount(const QModelIndex &parent) const
48 {
49 if (parent.isValid())
50 return 0;
51 return COLUMN_LAST;
52 }
53
rowCount(const QModelIndex & parent) const54 int SenderIdentitiesModel::rowCount(const QModelIndex &parent) const
55 {
56 if (parent.isValid())
57 return 0;
58 return m_identities.size();
59 }
60
data(const QModelIndex & index,int role) const61 QVariant SenderIdentitiesModel::data(const QModelIndex &index, int role) const
62 {
63 if (!index.isValid() || index.row() >= m_identities.size() || index.column() >= COLUMN_LAST)
64 return QVariant();
65
66 if (role != Qt::DisplayRole && role != Qt::EditRole) {
67 // For simplicity, we don't need anything fancy here
68 return QVariant();
69 }
70
71 switch (index.column()) {
72 case COLUMN_NAME:
73 return m_identities[index.row()].realName;
74 case COLUMN_EMAIL:
75 return m_identities[index.row()].emailAddress;
76 case COLUMN_ORGANIZATION:
77 return m_identities[index.row()].organisation;
78 case COLUMN_SIGNATURE:
79 return m_identities[index.row()].signature;
80 }
81 Q_ASSERT(false);
82 return QVariant();
83 }
84
headerData(int section,Qt::Orientation orientation,int role) const85 QVariant SenderIdentitiesModel::headerData(int section, Qt::Orientation orientation, int role) const
86 {
87 if (orientation == Qt::Vertical)
88 return QVariant();
89 if (role != Qt::DisplayRole)
90 return QVariant();
91
92 switch (section) {
93 case COLUMN_NAME:
94 return tr("Name");
95 case COLUMN_EMAIL:
96 return tr("E-mail");
97 case COLUMN_ORGANIZATION:
98 return tr("Organization");
99 case COLUMN_SIGNATURE:
100 return tr("Signature");
101 default:
102 return QVariant();
103 }
104 }
105
setData(const QModelIndex & index,const QVariant & value,int role)106 bool SenderIdentitiesModel::setData(const QModelIndex &index, const QVariant &value, int role)
107 {
108 if (!index.isValid() || role != Qt::EditRole)
109 return false;
110
111 switch (index.column()) {
112 case COLUMN_NAME:
113 m_identities[index.row()].realName = value.toString();
114 break;
115 case COLUMN_EMAIL:
116 m_identities[index.row()].emailAddress = value.toString();
117 break;
118 case COLUMN_ORGANIZATION:
119 m_identities[index.row()].organisation = value.toString();
120 break;
121 case COLUMN_SIGNATURE:
122 m_identities[index.row()].signature = value.toString();
123 break;
124 default:
125 Q_ASSERT(false);
126 return false;
127 }
128 emit dataChanged(index, index);
129 return true;
130 }
131
moveIdentity(const int from,const int to)132 void SenderIdentitiesModel::moveIdentity(const int from, const int to)
133 {
134 Q_ASSERT(to >= 0);
135 Q_ASSERT(from >= 0);
136 Q_ASSERT(to < m_identities.size());
137 Q_ASSERT(from < m_identities.size());
138 Q_ASSERT(from != to);
139
140 int targetOffset = to;
141 if (to > from) {
142 ++targetOffset;
143 }
144
145 bool ok = beginMoveRows(QModelIndex(), from, from, QModelIndex(), targetOffset);
146 Q_ASSERT(ok); Q_UNUSED(ok);
147
148 m_identities.move(from, to);
149 endMoveRows();
150 }
151
appendIdentity(const ItemSenderIdentity & item)152 void SenderIdentitiesModel::appendIdentity(const ItemSenderIdentity &item)
153 {
154 beginInsertRows(QModelIndex(), m_identities.size(), m_identities.size());
155 m_identities << item;
156 endInsertRows();
157 }
158
removeIdentityAt(const int position)159 void SenderIdentitiesModel::removeIdentityAt(const int position)
160 {
161 Q_ASSERT(position >= 0);
162 Q_ASSERT(position < m_identities.size());
163 beginRemoveRows(QModelIndex(), position, position);
164 m_identities.removeAt(position);
165 endRemoveRows();
166 }
167
loadFromSettings(QSettings & s)168 void SenderIdentitiesModel::loadFromSettings(QSettings &s)
169 {
170 beginResetModel();
171 m_identities.clear();
172
173 int num = s.beginReadArray(Common::SettingsNames::identitiesKey);
174 // The new format with multiple identities
175 for (int i = 0; i < num; ++i) {
176 s.setArrayIndex(i);
177 m_identities << ItemSenderIdentity(
178 s.value(Common::SettingsNames::realNameKey).toString(),
179 s.value(Common::SettingsNames::addressKey).toString(),
180 s.value(Common::SettingsNames::organisationKey).toString(),
181 s.value(Common::SettingsNames::signatureKey).toString());
182 }
183 s.endArray();
184 endResetModel();
185 }
186
saveToSettings(QSettings & s) const187 void SenderIdentitiesModel::saveToSettings(QSettings &s) const
188 {
189 s.beginWriteArray(Common::SettingsNames::identitiesKey);
190 for (int i = 0; i < m_identities.size(); ++i) {
191 s.setArrayIndex(i);
192 s.setValue(Common::SettingsNames::realNameKey, m_identities[i].realName);
193 s.setValue(Common::SettingsNames::addressKey, m_identities[i].emailAddress);
194 s.setValue(Common::SettingsNames::organisationKey, m_identities[i].organisation);
195 s.setValue(Common::SettingsNames::signatureKey, m_identities[i].signature);
196 }
197 s.endArray();
198 }
199
200 }
201