MailTransport

transportmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2024 Laurent Montel <montel@kde.org>
3
4 SPDX-License-Identifier: LGPL-2.0-or-later
5*/
6
7#include "transportmodel.h"
8#include "mailtransport_debug.h"
9#include "transportmanager.h"
10#include <KLocalizedString>
11
12using namespace MailTransport;
13TransportModel::TransportModel(QObject *parent)
14 : QAbstractListModel{parent}
15 , mTransportManager(TransportManager::self())
16{
17 updateComboboxList();
18 connect(mTransportManager, &TransportManager::transportsChanged, this, &TransportModel::updateComboboxList);
19}
20
21TransportModel::~TransportModel() = default;
22
23QVariant TransportModel::data(const QModelIndex &index, int role) const
24{
25 if (!index.isValid()) {
26 return {};
27 }
28 const auto transport = mTransportManager->transportById(mTransportIds[index.row()]);
29 if (role == Qt::FontRole) {
30 if (static_cast<TransportRoles>(index.column()) == NameRole) {
31 if (TransportManager::self()->defaultTransportId() == transport->id()) {
32 QFont f;
33 f.setBold(true);
34 return f;
35 }
36 }
37 }
38 if (role != Qt::DisplayRole) {
39 return {};
40 }
41 switch (static_cast<TransportRoles>(index.column())) {
42 case NameRole:
43 return generateTransportName(transport);
44 case TransportNameRole:
45 return transport->transportType().name();
46 case TransportIdentifierRole:
47 return transport->id();
48 case ActivitiesRole:
49 return transport->activities();
50 case EnabledActivitiesRole:
51 return transport->activitiesEnabled();
52 case DefaultRole:
54 }
55
56 return {};
57}
58
59QString TransportModel::generateTransportName(Transport *t) const
60{
61 QString str = t->name();
62 if (mShowDefault && TransportManager::self()->defaultTransportId() == t->id()) {
63 str += QLatin1Char(' ') + i18nc("Default transport", " (default)");
64 }
65 return str;
66}
67
68int TransportModel::rowCount(const QModelIndex &parent) const
69{
70 Q_UNUSED(parent);
71 return mTransportIds.count();
72}
73int TransportModel::columnCount(const QModelIndex &parent) const
74{
75 Q_UNUSED(parent)
76 constexpr int nbCol = static_cast<int>(TransportRoles::LastColumn) + 1;
77 return nbCol;
78}
79
80QVariant TransportModel::headerData(int section, Qt::Orientation orientation, int role) const
81{
82 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
83 switch (static_cast<TransportRoles>(section)) {
84 case NameRole:
85 return i18nc("@title:column email transport name", "Name");
86 case TransportNameRole:
87 return i18nc("@title:column email transport type", "Type");
88 case TransportIdentifierRole:
89 case DefaultRole:
90 case ActivitiesRole:
91 case EnabledActivitiesRole:
92 return {};
93 }
94 }
95 return {};
96}
97
98int TransportModel::transportId(int index) const
99{
100 return mTransportIds.at(index);
101}
102
103int TransportModel::indexOf(int transportId) const
104{
105 return mTransportIds.indexOf(transportId);
106}
107
108void TransportModel::updateComboboxList()
109{
111 mTransportIds = mTransportManager->transportIds();
113}
114
115Qt::ItemFlags TransportModel::flags(const QModelIndex &index) const
116{
117 if (!index.isValid())
118 return Qt::NoItemFlags;
119
120 if (static_cast<TransportRoles>(index.column()) == NameRole) {
122 }
124}
125
126bool TransportModel::setData(const QModelIndex &modelIndex, const QVariant &value, int role)
127{
128 if (!modelIndex.isValid()) {
129 qCWarning(MAILTRANSPORT_LOG) << "ERROR: invalid index";
130 return false;
131 }
132 if (role != Qt::EditRole) {
133 return false;
134 }
135 const int idx = modelIndex.row();
136 const auto transport = mTransportManager->transportById(mTransportIds[idx]);
137 switch (static_cast<TransportRoles>(modelIndex.column())) {
138 case NameRole: {
139 const QModelIndex newIndex = index(modelIndex.row(), NameRole);
140 Q_EMIT dataChanged(newIndex, newIndex);
141 transport->setName(value.toString());
142 transport->forceUniqueName();
143 transport->save();
144 return true;
145 }
146 default:
147 break;
148 }
149 return false;
150}
151
152void TransportModel::setShowDefault(bool show)
153{
154 mShowDefault = show;
155}
156
157#include "moc_transportmodel.cpp"
Central transport management interface.
Transport * transportById(Transport::Id id, bool def=true) const
Returns the Transport object with the given id.
Q_SCRIPTABLE void transportsChanged()
Emitted when transport settings have changed (by this or any other TransportManager instance).
static TransportManager * self()
Returns the TransportManager instance.
Q_SCRIPTABLE int defaultTransportId() const
Returns the default transport identifier.
Q_SCRIPTABLE QList< int > transportIds() const
Returns a list of transport identifiers.
Represents the settings of a specific mail transport.
Definition transport.h:33
QString i18nc(const char *context, const char *text, const TYPE &arg...)
void dataChanged(const QModelIndex &topLeft, const QModelIndex &bottomRight, const QList< int > &roles)
virtual Qt::ItemFlags flags(const QModelIndex &index) const const
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
void setBold(bool enable)
const_reference at(qsizetype i) const const
qsizetype count() const const
qsizetype indexOf(const AT &value, qsizetype from) const const
int column() const const
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
QObject * parent() const const
FontRole
typedef ItemFlags
Orientation
QFuture< ArgsType< Signal > > connect(Sender *sender, Signal signal)
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2024 The KDE developers.
Generated on Mon Nov 4 2024 16:35:38 by doxygen 1.12.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.