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"
25 #include "Common/SettingsNames.h"
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
47 int SenderIdentitiesModel::rowCount(const QModelIndex
&parent
) const
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
)
59 if (role
!= Qt::DisplayRole
&& role
!= Qt::EditRole
) {
60 // For simplicity, we don't need anything fancy here
64 switch (index
.column()) {
66 return m_identities
[index
.row()].realName
;
68 return m_identities
[index
.row()].emailAddress
;
74 QVariant
SenderIdentitiesModel::headerData(int section
, Qt::Orientation orientation
, int role
) const
76 if (orientation
== Qt::Vertical
)
78 if (role
!= Qt::DisplayRole
)
91 bool SenderIdentitiesModel::setData(const QModelIndex
&index
, const QVariant
&value
, int role
)
93 if (!index
.isValid() || role
!= Qt::EditRole
)
96 switch (index
.column()) {
98 m_identities
[index
.row()].realName
= value
.toString();
101 m_identities
[index
.row()].emailAddress
= value
.toString();
107 emit
dataChanged(index
, index
);
111 void SenderIdentitiesModel::appendIdentity(const ItemSenderIdentity
&item
)
113 beginInsertRows(QModelIndex(), m_identities
.size(), m_identities
.size());
114 m_identities
<< item
;
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
);
127 void SenderIdentitiesModel::loadFromSettings(const QSettings
&s
)
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());
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
);