File indexing completed on 2024-11-24 04:52:56
0001 /* Copyright (C) 2006 - 2014 Jan Kundrát <jkt@flaska.net> 0002 Copyright (C) 2012 Mohammed Nafees <nafees.technocool@gmail.com> 0003 Copyright (C) 2012 Peter Amidon <peter@picnicpark.org> 0004 0005 This file is part of the Trojita Qt IMAP e-mail client, 0006 http://trojita.flaska.net/ 0007 0008 This program is free software; you can redistribute it and/or 0009 modify it under the terms of the GNU General Public License as 0010 published by the Free Software Foundation; either version 2 of 0011 the License or (at your option) version 3 or any later version 0012 accepted by the membership of KDE e.V. (or its successor approved 0013 by the membership of KDE e.V.), which shall act as a proxy 0014 defined in Section 14 of version 3 of the license. 0015 0016 This program is distributed in the hope that it will be useful, 0017 but WITHOUT ANY WARRANTY; without even the implied warranty of 0018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 0019 GNU General Public License for more details. 0020 0021 You should have received a copy of the GNU General Public License 0022 along with this program. If not, see <http://www.gnu.org/licenses/>. 0023 */ 0024 0025 #include "SenderIdentitiesModel.h" 0026 #include <QSettings> 0027 #include "Common/SettingsNames.h" 0028 0029 namespace Composer 0030 { 0031 0032 ItemSenderIdentity::ItemSenderIdentity(const QString &realName, const QString &emailAddress, 0033 const QString &organisation, const QString &signature): 0034 realName(realName), emailAddress(emailAddress), organisation(organisation), signature(signature) 0035 { 0036 } 0037 0038 ItemSenderIdentity::ItemSenderIdentity() 0039 { 0040 } 0041 0042 SenderIdentitiesModel::SenderIdentitiesModel(QObject *parent) : 0043 QAbstractTableModel(parent) 0044 { 0045 } 0046 0047 int SenderIdentitiesModel::columnCount(const QModelIndex &parent) const 0048 { 0049 if (parent.isValid()) 0050 return 0; 0051 return COLUMN_LAST; 0052 } 0053 0054 int SenderIdentitiesModel::rowCount(const QModelIndex &parent) const 0055 { 0056 if (parent.isValid()) 0057 return 0; 0058 return m_identities.size(); 0059 } 0060 0061 QVariant SenderIdentitiesModel::data(const QModelIndex &index, int role) const 0062 { 0063 if (!index.isValid() || index.row() >= m_identities.size() || index.column() >= COLUMN_LAST) 0064 return QVariant(); 0065 0066 if (role != Qt::DisplayRole && role != Qt::EditRole) { 0067 // For simplicity, we don't need anything fancy here 0068 return QVariant(); 0069 } 0070 0071 switch (index.column()) { 0072 case COLUMN_NAME: 0073 return m_identities[index.row()].realName; 0074 case COLUMN_EMAIL: 0075 return m_identities[index.row()].emailAddress; 0076 case COLUMN_ORGANIZATION: 0077 return m_identities[index.row()].organisation; 0078 case COLUMN_SIGNATURE: 0079 return m_identities[index.row()].signature; 0080 } 0081 Q_ASSERT(false); 0082 return QVariant(); 0083 } 0084 0085 QVariant SenderIdentitiesModel::headerData(int section, Qt::Orientation orientation, int role) const 0086 { 0087 if (orientation == Qt::Vertical) 0088 return QVariant(); 0089 if (role != Qt::DisplayRole) 0090 return QVariant(); 0091 0092 switch (section) { 0093 case COLUMN_NAME: 0094 return tr("Name"); 0095 case COLUMN_EMAIL: 0096 return tr("E-mail"); 0097 case COLUMN_ORGANIZATION: 0098 return tr("Organization"); 0099 case COLUMN_SIGNATURE: 0100 return tr("Signature"); 0101 default: 0102 return QVariant(); 0103 } 0104 } 0105 0106 bool SenderIdentitiesModel::setData(const QModelIndex &index, const QVariant &value, int role) 0107 { 0108 if (!index.isValid() || role != Qt::EditRole) 0109 return false; 0110 0111 switch (index.column()) { 0112 case COLUMN_NAME: 0113 m_identities[index.row()].realName = value.toString(); 0114 break; 0115 case COLUMN_EMAIL: 0116 m_identities[index.row()].emailAddress = value.toString(); 0117 break; 0118 case COLUMN_ORGANIZATION: 0119 m_identities[index.row()].organisation = value.toString(); 0120 break; 0121 case COLUMN_SIGNATURE: 0122 m_identities[index.row()].signature = value.toString(); 0123 break; 0124 default: 0125 Q_ASSERT(false); 0126 return false; 0127 } 0128 emit dataChanged(index, index); 0129 return true; 0130 } 0131 0132 void SenderIdentitiesModel::moveIdentity(const int from, const int to) 0133 { 0134 Q_ASSERT(to >= 0); 0135 Q_ASSERT(from >= 0); 0136 Q_ASSERT(to < m_identities.size()); 0137 Q_ASSERT(from < m_identities.size()); 0138 Q_ASSERT(from != to); 0139 0140 int targetOffset = to; 0141 if (to > from) { 0142 ++targetOffset; 0143 } 0144 0145 bool ok = beginMoveRows(QModelIndex(), from, from, QModelIndex(), targetOffset); 0146 Q_ASSERT(ok); Q_UNUSED(ok); 0147 0148 m_identities.move(from, to); 0149 endMoveRows(); 0150 } 0151 0152 void SenderIdentitiesModel::appendIdentity(const ItemSenderIdentity &item) 0153 { 0154 beginInsertRows(QModelIndex(), m_identities.size(), m_identities.size()); 0155 m_identities << item; 0156 endInsertRows(); 0157 } 0158 0159 void SenderIdentitiesModel::removeIdentityAt(const int position) 0160 { 0161 Q_ASSERT(position >= 0); 0162 Q_ASSERT(position < m_identities.size()); 0163 beginRemoveRows(QModelIndex(), position, position); 0164 m_identities.removeAt(position); 0165 endRemoveRows(); 0166 } 0167 0168 void SenderIdentitiesModel::loadFromSettings(QSettings &s) 0169 { 0170 beginResetModel(); 0171 m_identities.clear(); 0172 0173 int num = s.beginReadArray(Common::SettingsNames::identitiesKey); 0174 // The new format with multiple identities 0175 for (int i = 0; i < num; ++i) { 0176 s.setArrayIndex(i); 0177 m_identities << ItemSenderIdentity( 0178 s.value(Common::SettingsNames::realNameKey).toString(), 0179 s.value(Common::SettingsNames::addressKey).toString(), 0180 s.value(Common::SettingsNames::organisationKey).toString(), 0181 s.value(Common::SettingsNames::signatureKey).toString()); 0182 } 0183 s.endArray(); 0184 endResetModel(); 0185 } 0186 0187 void SenderIdentitiesModel::saveToSettings(QSettings &s) const 0188 { 0189 s.beginWriteArray(Common::SettingsNames::identitiesKey); 0190 for (int i = 0; i < m_identities.size(); ++i) { 0191 s.setArrayIndex(i); 0192 s.setValue(Common::SettingsNames::realNameKey, m_identities[i].realName); 0193 s.setValue(Common::SettingsNames::addressKey, m_identities[i].emailAddress); 0194 s.setValue(Common::SettingsNames::organisationKey, m_identities[i].organisation); 0195 s.setValue(Common::SettingsNames::signatureKey, m_identities[i].signature); 0196 } 0197 s.endArray(); 0198 } 0199 0200 }