MailTransport

transportmodel.cpp
1/*
2 SPDX-FileCopyrightText: 2024-2025 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 if (parent.isValid()) // flat model
71 return 0;
72 return mTransportIds.count();
73}
74int TransportModel::columnCount(const QModelIndex &parent) const
75{
76 Q_UNUSED(parent)
77 constexpr int nbCol = static_cast<int>(TransportRoles::LastColumn) + 1;
78 return nbCol;
79}
80
81QVariant TransportModel::headerData(int section, Qt::Orientation orientation, int role) const
82{
83 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
84 switch (static_cast<TransportRoles>(section)) {
85 case NameRole:
86 return i18nc("@title:column email transport name", "Name");
87 case TransportNameRole:
88 return i18nc("@title:column email transport type", "Type");
89 case TransportIdentifierRole:
90 case DefaultRole:
91 case ActivitiesRole:
92 case EnabledActivitiesRole:
93 return {};
94 }
95 }
96 return {};
97}
98
99int TransportModel::transportId(int index) const
100{
101 return mTransportIds.at(index);
102}
103
104int TransportModel::indexOf(int transportId) const
105{
106 return mTransportIds.indexOf(transportId);
107}
108
109void TransportModel::updateComboboxList()
110{
112 mTransportIds = mTransportManager->transportIds();
114}
115
116Qt::ItemFlags TransportModel::flags(const QModelIndex &index) const
117{
118 if (!index.isValid())
119 return Qt::NoItemFlags;
120
121 if (static_cast<TransportRoles>(index.column()) == NameRole) {
123 }
125}
126
127bool TransportModel::setData(const QModelIndex &modelIndex, const QVariant &value, int role)
128{
129 if (!modelIndex.isValid()) {
130 qCWarning(MAILTRANSPORT_LOG) << "ERROR: invalid index";
131 return false;
132 }
133 if (role != Qt::EditRole) {
134 return false;
135 }
136 const int idx = modelIndex.row();
137 const auto transport = mTransportManager->transportById(mTransportIds[idx]);
138 switch (static_cast<TransportRoles>(modelIndex.column())) {
139 case NameRole: {
140 const QModelIndex newIndex = index(modelIndex.row(), NameRole);
141 Q_EMIT dataChanged(newIndex, newIndex);
142 transport->setName(value.toString());
143 transport->forceUniqueName();
144 transport->save();
145 return true;
146 }
147 default:
148 break;
149 }
150 return false;
151}
152
153void TransportModel::setShowDefault(bool show)
154{
155 mShowDefault = show;
156}
157
158#include "moc_transportmodel.cpp"
Central transport management interface.
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.
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=0
virtual QModelIndex parent(const QModelIndex &index) const const=0
virtual QModelIndex index(int row, int column, const QModelIndex &parent) const const override
void setBold(bool enable)
int column() const const
bool isValid() const const
int row() const const
Q_EMITQ_EMIT
qsizetype count() 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-2025 The KDE developers.
Generated on Fri Apr 4 2025 11:57:53 by doxygen 1.13.2 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.