Introduce the SenderIdentitiesModel for, well, managing multiple sender identities
[trojita.git] / src / Composer / SenderIdentitiesModel.cpp
blob5e05773a3974c7e6ae7ddb3c1176dd3dbcd3dbc4
1 /* Copyright (C) 2006 - 2012 Jan Kundrát <jkt@flaska.net>
3 This file is part of the Trojita Qt IMAP e-mail client,
4 http://trojita.flaska.net/
6 This program is free software; you can redistribute it and/or
7 modify it under the terms of the GNU General Public License as
8 published by the Free Software Foundation; either version 2 of
9 the License or (at your option) version 3 or any later version
10 accepted by the membership of KDE e.V. (or its successor approved
11 by the membership of KDE e.V.), which shall act as a proxy
12 defined in Section 14 of version 3 of the license.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include "SenderIdentitiesModel.h"
24 #include <QSettings>
25 #include "Common/SettingsNames.h"
27 namespace Composer
30 ItemSenderIdentity::ItemSenderIdentity(const QString &realName, const QString &emailAddress):
31 realName(realName), emailAddress(emailAddress)
35 SenderIdentitiesModel::SenderIdentitiesModel(QObject *parent) :
36 QAbstractTableModel(parent)
40 int SenderIdentitiesModel::columnCount(const QModelIndex &parent) const
42 if (parent.isValid())
43 return 0;
44 return COLUMN_LAST;
47 int SenderIdentitiesModel::rowCount(const QModelIndex &parent) const
49 if (parent.isValid())
50 return 0;
51 return m_identities.size();
54 QVariant SenderIdentitiesModel::data(const QModelIndex &index, const int role) const
56 if (!index.isValid() || index.row() >= m_identities.size() || index.column() >= COLUMN_LAST)
57 return QVariant();
59 if (role != Qt::DisplayRole && role != Qt::EditRole) {
60 // For simplicity, we don't need anything fancy here
61 return QVariant();
64 switch (index.column()) {
65 case COLUMN_NAME:
66 return m_identities[index.row()].realName;
67 case COLUMN_EMAIL:
68 return m_identities[index.row()].emailAddress;
70 Q_ASSERT(false);
71 return QVariant();
74 QVariant SenderIdentitiesModel::headerData(int section, Qt::Orientation orientation, int role) const
76 if (orientation == Qt::Vertical)
77 return QVariant();
78 if (role != Qt::DisplayRole)
79 return QVariant();
81 switch (section) {
82 case COLUMN_NAME:
83 return tr("Name");
84 case COLUMN_EMAIL:
85 return tr("E-mail");
86 default:
87 return QVariant();
91 bool SenderIdentitiesModel::setData(const QModelIndex &index, const QVariant &value, int role)
93 if (!index.isValid() || role != Qt::EditRole)
94 return false;
96 switch (index.column()) {
97 case COLUMN_NAME:
98 m_identities[index.row()].realName = value.toString();
99 break;
100 case COLUMN_EMAIL:
101 m_identities[index.row()].emailAddress = value.toString();
102 break;
103 default:
104 Q_ASSERT(false);
105 return false;
107 emit dataChanged(index, index);
108 return true;
111 void SenderIdentitiesModel::appendIdentity(const ItemSenderIdentity &item)
113 beginInsertRows(QModelIndex(), m_identities.size(), m_identities.size());
114 m_identities << item;
115 endInsertRows();
118 void SenderIdentitiesModel::removeIdentityAt(const int position)
120 Q_ASSERT(position >= 0);
121 Q_ASSERT(position < m_identities.size());
122 beginRemoveRows(QModelIndex(), position, position);
123 m_identities.removeAt(position);
124 endRemoveRows();
127 void SenderIdentitiesModel::loadFromSettings(const QSettings &s)
129 beginResetModel();
130 m_identities.clear();
132 // FIXME: replace this block with support for multiple identities
133 m_identities << ItemSenderIdentity(
134 s.value(Common::SettingsNames::realNameKey).toString(),
135 s.value(Common::SettingsNames::addressKey).toString());
136 endResetModel();
139 void SenderIdentitiesModel::saveToSettings(QSettings &s) const
141 // FIXME: replace this with support for multiple identities
143 if (m_identities.size() == 1) {
144 s.setValue(Common::SettingsNames::realNameKey, m_identities[0].realName);
145 s.setValue(Common::SettingsNames::addressKey, m_identities[0].emailAddress);