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 DefaultRole:
52 }
53
54 return {};
55}
56
57QString TransportModel::generateTransportName(Transport *t) const
58{
59 QString str = t->name();
60 if (mShowDefault && TransportManager::self()->defaultTransportId() == t->id()) {
61 str += QLatin1Char(' ') + i18nc("Default transport", " (default)");
62 }
63 return str;
64}
65
66int TransportModel::rowCount(const QModelIndex &parent) const
67{
68 Q_UNUSED(parent);
69 return mTransportIds.count();
70}
71int TransportModel::columnCount(const QModelIndex &parent) const
72{
73 Q_UNUSED(parent)
74 constexpr int nbCol = static_cast<int>(TransportRoles::LastColumn) + 1;
75 return nbCol;
76}
77
78QVariant TransportModel::headerData(int section, Qt::Orientation orientation, int role) const
79{
80 if (role == Qt::DisplayRole && orientation == Qt::Horizontal) {
81 switch (static_cast<TransportRoles>(section)) {
82 case NameRole:
83 return i18nc("@title:column email transport name", "Name");
84 case TransportNameRole:
85 return i18nc("@title:column email transport type", "Type");
86 case TransportIdentifierRole:
87 case DefaultRole:
88 case ActivitiesRole:
89 return {};
90 }
91 }
92 return {};
93}
94
95int TransportModel::transportId(int index) const
96{
97 return mTransportIds.at(index);
98}
99
100int TransportModel::indexOf(int transportId) const
101{
102 return mTransportIds.indexOf(transportId);
103}
104
105void TransportModel::updateComboboxList()
106{
108 mTransportIds = mTransportManager->transportIds();
110}
111
112Qt::ItemFlags TransportModel::flags(const QModelIndex &index) const
113{
114 if (!index.isValid())
115 return Qt::NoItemFlags;
116
117 if (static_cast<TransportRoles>(index.column()) == NameRole) {
119 }
121}
122
123bool TransportModel::setData(const QModelIndex &modelIndex, const QVariant &value, int role)
124{
125 if (!modelIndex.isValid()) {
126 qCWarning(MAILTRANSPORT_LOG) << "ERROR: invalid index";
127 return false;
128 }
129 if (role != Qt::EditRole) {
130 return false;
131 }
132 const int idx = modelIndex.row();
133 const auto transport = mTransportManager->transportById(mTransportIds[idx]);
134 switch (static_cast<TransportRoles>(modelIndex.column())) {
135 case NameRole: {
136 const QModelIndex newIndex = index(modelIndex.row(), NameRole);
137 Q_EMIT dataChanged(newIndex, newIndex);
138 transport->setName(value.toString());
139 transport->forceUniqueName();
140 transport->save();
141 return true;
142 }
143 default:
144 break;
145 }
146 return false;
147}
148
149void TransportModel::setShowDefault(bool show)
150{
151 mShowDefault = show;
152}
153
154#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 Fri Jul 26 2024 11:56:31 by doxygen 1.11.0 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.