akonadimailtransportplugin.cpp 4.15 KB
Newer Older
1
/*
Laurent Montel's avatar
Laurent Montel committed
2
    Copyright (C) 2017-2019 Laurent Montel <montel@kde.org>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20

    This library is free software; you can redistribute it and/or modify it
    under the terms of the GNU Library General Public License as published by
    the Free Software Foundation; either version 2 of the License, or (at your
    option) any later version.

    This library is distributed in the hope that it will be useful, but WITHOUT
    ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Library General Public
    License for more details.

    You should have received a copy of the GNU Library General Public License
    along with this library; see the file COPYING.LIB.  If not, write to the
    Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
    02110-1301, USA.
*/

#include "akonadimailtransportplugin.h"
21
#include "resourcesendjob_p.h"
Laurent Montel's avatar
Laurent Montel committed
22
#include "mailtransportplugin_akonadi_debug.h"
23
#include <KPluginFactory>
24 25
#include <AkonadiCore/AgentManager>
#include <MailTransport/TransportAbstractPlugin>
Laurent Montel's avatar
Laurent Montel committed
26 27
#include <AkonadiCore/AgentInstanceCreateJob>
#include <MailTransport/Transport>
28

29
using namespace Akonadi;
30 31

K_PLUGIN_CLASS_WITH_JSON(AkonadiMailTransportPlugin, "akonadimailtransport.json")
32 33 34 35

AkonadiMailTransportPlugin::AkonadiMailTransportPlugin(QObject *parent, const QList<QVariant> &)
    : MailTransport::TransportAbstractPlugin(parent)
{
Laurent Montel's avatar
Laurent Montel committed
36
    // Watch for appearing and disappearing types.
Laurent Montel's avatar
Laurent Montel committed
37 38
    connect(AgentManager::self(), &AgentManager::typeAdded, this, &AkonadiMailTransportPlugin::slotUpdatePluginList);
    connect(AgentManager::self(), &AgentManager::typeRemoved, this, &AkonadiMailTransportPlugin::slotUpdatePluginList);
39 40 41 42 43 44
}

AkonadiMailTransportPlugin::~AkonadiMailTransportPlugin()
{
}

Laurent Montel's avatar
Laurent Montel committed
45 46 47 48 49 50 51
void AkonadiMailTransportPlugin::slotUpdatePluginList(const Akonadi::AgentType &type)
{
    if (type.capabilities().contains(QLatin1String("MailTransport"))) {
        Q_EMIT updatePluginList();
    }
}

Laurent Montel's avatar
Laurent Montel committed
52
void AkonadiMailTransportPlugin::cleanUp(MailTransport::Transport *t)
53
{
Laurent Montel's avatar
Laurent Montel committed
54 55
    const AgentInstance instance = AgentManager::self()->instance(t->host());
    if (!instance.isValid()) {
Laurent Montel's avatar
Laurent Montel committed
56
        qCWarning(MAILTRANSPORT_AKONADI_LOG) << "Could not find resource instance for name:" << t->host();
Laurent Montel's avatar
Laurent Montel committed
57 58
    }
    AgentManager::self()->removeInstance(instance);
59 60
}

Laurent Montel's avatar
Fix api  
Laurent Montel committed
61
QVector<MailTransport::TransportAbstractPluginInfo> AkonadiMailTransportPlugin::names() const
62
{
63 64 65 66 67
    QVector<MailTransport::TransportAbstractPluginInfo> lst;

    for (const AgentType &atype : AgentManager::self()->types()) {
        // TODO probably the string "MailTransport" should be #defined somewhere
        // and used like that in the resources (?)
Laurent Montel's avatar
Laurent Montel committed
68
        if (atype.capabilities().contains(QLatin1String("MailTransport"))) {
69 70 71 72
            MailTransport::TransportAbstractPluginInfo info;
            info.name = atype.name();
            info.description = atype.description();
            info.identifier = atype.identifier();
73
            info.isAkonadi = true;
74 75 76 77
            lst << info;
        }
    }
    return lst;
78 79
}

Laurent Montel's avatar
Fix api  
Laurent Montel committed
80
bool AkonadiMailTransportPlugin::configureTransport(const QString &identifier, MailTransport::Transport *transport, QWidget *parent)
81
{
Laurent Montel's avatar
Laurent Montel committed
82
    Q_UNUSED(identifier);
Laurent Montel's avatar
Laurent Montel committed
83 84
    AgentInstance instance = AgentManager::self()->instance(transport->host());
    if (!instance.isValid()) {
Laurent Montel's avatar
Laurent Montel committed
85
        qCWarning(MAILTRANSPORT_AKONADI_LOG) << "Invalid resource instance" << transport->host();
86
        return false;
87
    }
Laurent Montel's avatar
Laurent Montel committed
88
    instance.configure(parent);   // Async...
89
    transport->save();
90
    return true; // No way to know here if the user cancelled or not.
91 92
}

93 94
MailTransport::TransportJob *AkonadiMailTransportPlugin::createTransportJob(MailTransport::Transport *t, const QString &identifier)
{
Laurent Montel's avatar
Laurent Montel committed
95
    Q_UNUSED(identifier);
96 97 98
    return new MailTransport::ResourceSendJob(t, this);
}

Laurent Montel's avatar
Laurent Montel committed
99 100
void AkonadiMailTransportPlugin::initializeTransport(MailTransport::Transport *t, const QString &identifier)
{
Laurent Montel's avatar
Minor  
Laurent Montel committed
101
    AgentInstanceCreateJob *cjob = new AgentInstanceCreateJob(identifier);
Laurent Montel's avatar
Laurent Montel committed
102
    if (!cjob->exec()) {
Laurent Montel's avatar
Laurent Montel committed
103
        qCWarning(MAILTRANSPORT_AKONADI_LOG) << "Failed to create agent instance of type" << identifier;
Laurent Montel's avatar
Laurent Montel committed
104 105
        return;
    }
Laurent Montel's avatar
Laurent Montel committed
106
    t->setHost(cjob->instance().identifier());
Laurent Montel's avatar
Laurent Montel committed
107 108
}

109
#include "akonadimailtransportplugin.moc"