MailTransport

akonadimailtransportplugin.cpp
1 /*
2  SPDX-FileCopyrightText: 2017-2023 Laurent Montel <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "akonadimailtransportplugin.h"
8 #include "mailtransportplugin_akonadi_debug.h"
9 #include "resourcesendjob_p.h"
10 #include <Akonadi/AgentConfigurationDialog>
11 #include <Akonadi/AgentInstanceCreateJob>
12 #include <Akonadi/AgentManager>
13 #include <KPluginFactory>
14 #include <MailTransport/Transport>
15 #include <QPointer>
16 
17 using namespace Akonadi;
18 
19 K_PLUGIN_CLASS_WITH_JSON(AkonadiMailTransportPlugin, "akonadimailtransport.json")
20 
21 AkonadiMailTransportPlugin::AkonadiMailTransportPlugin(QObject *parent, const QList<QVariant> &)
22  : MailTransport::TransportAbstractPlugin(parent)
23 {
24  // Watch for appearing and disappearing types.
25  connect(AgentManager::self(), &AgentManager::typeAdded, this, &AkonadiMailTransportPlugin::slotUpdatePluginList);
26  connect(AgentManager::self(), &AgentManager::typeRemoved, this, &AkonadiMailTransportPlugin::slotUpdatePluginList);
27 }
28 
29 AkonadiMailTransportPlugin::~AkonadiMailTransportPlugin() = default;
30 
31 void AkonadiMailTransportPlugin::slotUpdatePluginList(const Akonadi::AgentType &type)
32 {
33  if (type.capabilities().contains(QLatin1String("MailTransport"))) {
34  Q_EMIT updatePluginList();
35  }
36 }
37 
38 void AkonadiMailTransportPlugin::cleanUp(MailTransport::Transport *t)
39 {
40  const AgentInstance instance = AgentManager::self()->instance(t->host());
41  if (!instance.isValid()) {
42  qCWarning(MAILTRANSPORT_AKONADI_LOG) << "Could not find resource instance for name:" << t->host();
43  }
45 }
46 
47 QVector<MailTransport::TransportAbstractPluginInfo> AkonadiMailTransportPlugin::names() const
48 {
50 
51  const auto types{AgentManager::self()->types()};
52  for (const AgentType &atype : types) {
53  // TODO probably the string "MailTransport" should be #defined somewhere
54  // and used like that in the resources (?)
55  if (atype.capabilities().contains(QLatin1String("MailTransport"))) {
57  info.name = atype.name();
58  info.description = atype.description();
59  info.identifier = atype.identifier();
60  info.isAkonadi = true;
61  lst << info;
62  }
63  }
64  return lst;
65 }
66 
67 bool AkonadiMailTransportPlugin::configureTransport(const QString &identifier, MailTransport::Transport *transport, QWidget *parent)
68 {
69  Q_UNUSED(identifier)
70  AgentInstance instance = AgentManager::self()->instance(transport->host());
71  if (!instance.isValid()) {
72  qCWarning(MAILTRANSPORT_AKONADI_LOG) << "Invalid resource instance" << transport->host();
73  return false;
74  }
75  QPointer<Akonadi::AgentConfigurationDialog> dlg = new Akonadi::AgentConfigurationDialog(instance, parent); // Async...
76  dlg->exec();
77  delete dlg;
78  transport->save();
79  return true; // No way to know here if the user cancelled or not.
80 }
81 
82 MailTransport::TransportJob *AkonadiMailTransportPlugin::createTransportJob(MailTransport::Transport *t, const QString &identifier)
83 {
84  Q_UNUSED(identifier)
85  return new MailTransport::ResourceSendJob(t, this);
86 }
87 
88 void AkonadiMailTransportPlugin::initializeTransport(MailTransport::Transport *t, const QString &identifier)
89 {
90  auto cjob = new AgentInstanceCreateJob(identifier);
91  if (!cjob->exec()) {
92  qCWarning(MAILTRANSPORT_AKONADI_LOG) << "Failed to create agent instance of type" << identifier;
93  return;
94  }
95  t->setHost(cjob->instance().identifier());
96 }
97 
98 #include "akonadimailtransportplugin.moc"
Abstract base class for all mail transport jobs.
Definition: transportjob.h:30
Type type(const QSqlDatabase &db)
QStringList types(Mode mode=Writing)
AgentInstance instance(const QString &identifier) const
void removeInstance(const AgentInstance &instance)
void typeRemoved(const Akonadi::AgentType &type)
The TransportAbstractPluginInfo struct.
Represents the settings of a specific mail transport.
Definition: transport.h:32
static AgentManager * self()
#define K_PLUGIN_CLASS_WITH_JSON(classname, jsonFile)
void typeAdded(const Akonadi::AgentType &type)
AgentType::List types() const
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Thu Mar 23 2023 04:19:12 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.