kioservices.cpp 3.73 KB
Newer Older
1 2 3 4 5
/*
 *  SPDX-FileCopyrightText: 2013 Alejandro Fiestas Olivares <afiestas@kde.org>
 *
 *  SPDX-License-Identifier: GPL-2.0-or-later
 */
6 7 8 9 10 11

#include "kioservices.h"
#include "createnetattachjob.h"
#include "createkioservice.h"
#include "removekioservice.h"

12 13
#include <QFile>
#include <QDirIterator>
Martin Klapetek's avatar
Martin Klapetek committed
14
#include <QStandardPaths>
Martin Klapetek's avatar
Martin Klapetek committed
15
#include <QDebug>
16

Nicolas Fella's avatar
Nicolas Fella committed
17 18 19 20 21 22
#include <KPluginFactory>

K_PLUGIN_CLASS_WITH_JSON(KIOServices, "kio-webdav.json")

KIOServices::KIOServices(QObject *parent, const QVariantList &args)
    : KAccountsDPlugin(parent, args)
23 24 25
{
}

Nicolas Fella's avatar
Nicolas Fella committed
26 27
KIOServices::~KIOServices() = default;

28
void KIOServices::onAccountCreated(const Accounts::AccountId accId, const Accounts::ServiceList &serviceList)
29
{
Martin Klapetek's avatar
Martin Klapetek committed
30
    qDebug();
Nicolas Fella's avatar
Nicolas Fella committed
31
    for (const Accounts::Service &service : serviceList) {
32
        if (service.serviceType() != QLatin1String("dav-storage")) {
Martin Klapetek's avatar
Martin Klapetek committed
33
            qDebug() << "Ignoring: " << service.serviceType();
34 35
            continue;
        }
36
        if (isEnabled(accId, service.name())) {
Martin Klapetek's avatar
Martin Klapetek committed
37
            qDebug() << "Already configured: " << service.name();
38 39
            continue;
        }
40

Martin Klapetek's avatar
Martin Klapetek committed
41
        qDebug() << "Creating: " << service.name() << "Of type: " << service.serviceType();
42
        enableService(accId, service);
43 44 45
    }
}

46
void KIOServices::onAccountRemoved(const Accounts::AccountId accId)
47
{
Martin Klapetek's avatar
Martin Klapetek committed
48
    qDebug();
Nicolas Fella's avatar
Nicolas Fella committed
49
    const QString accountId = QString::number(accId) + QStringLiteral("_");
50

Nicolas Fella's avatar
Nicolas Fella committed
51
    const QString path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QLatin1String("/remoteview/");
52 53

    QDirIterator i(path, QDir::NoDotAndDotDot | QDir::Files);
54 55
    while (i.hasNext()) {
        i.next();
56
        if (!i.fileName().startsWith(accountId)) {
57 58
            continue;
        }
59

60
        QString serviceName = i.fileName();
Martin Klapetek's avatar
Martin Klapetek committed
61
        qDebug() << "Removing: " << serviceName;
62
        serviceName = serviceName.mid(accountId.count(), serviceName.indexOf(QLatin1String(".desktop")) - accountId.count());
Martin Klapetek's avatar
Martin Klapetek committed
63
        qDebug() << "Removing N: " << serviceName;
64
        disableService(accId, serviceName);
65
    }
66 67
}

68
void KIOServices::onServiceEnabled(const Accounts::AccountId accId, const Accounts::Service &service)
69
{
70
    if (service.serviceType() != QLatin1String("dav-storage")) {
Martin Klapetek's avatar
Martin Klapetek committed
71
        qDebug() << "Ignoring: " << service.serviceType();
72 73 74
        return;
    }
    if (isEnabled(accId, service.name())) {
Martin Klapetek's avatar
Martin Klapetek committed
75
        qDebug() << "Already configured: " << service.name();
76 77 78
        return;
    }

79 80 81
    enableService(accId, service);
}

82
void KIOServices::onServiceDisabled(const Accounts::AccountId accId, const Accounts::Service &service)
83
{
84
    if (service.serviceType() != QLatin1String("dav-storage")) {
Martin Klapetek's avatar
Martin Klapetek committed
85
        qDebug() << "Ignoring: " << service.serviceType();
86 87 88
        return;
    }
    if (!isEnabled(accId, service.name())) {
Martin Klapetek's avatar
Martin Klapetek committed
89
        qDebug() << "Already not configured: " << service.name();
90 91 92
        return;
    }

93 94 95
    disableService(accId, service.name());
}

96
void KIOServices::enableService(const Accounts::AccountId accId, const Accounts::Service &service)
97
{
98 99 100 101 102
    CreateKioService *job = new CreateKioService(this);
    job->setAccountId(accId);
    job->setServiceName(service.name());
    job->setServiceType(service.serviceType());
    job->start();
103 104
}

Martin Klapetek's avatar
Martin Klapetek committed
105
void KIOServices::disableService(const Accounts::AccountId accId, const QString &serviceName)
106
{
107 108 109 110
    RemoveKioService *job = new RemoveKioService(this);
    job->setServiceName(serviceName);
    job->setAccountId(accId);
    job->start();
111
}
112

113
bool KIOServices::isEnabled(const Accounts::AccountId accId, const QString &serviceName)
114
{
Nicolas Fella's avatar
Nicolas Fella committed
115
    const QString uniqueId(QString::number(accId) + QStringLiteral("_") + serviceName);
116 117 118

    QString path = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation);
    path += QStringLiteral("/remoteview/") + uniqueId + QStringLiteral(".desktop");
119 120 121

    return QFile::exists(path);
}
Nicolas Fella's avatar
Nicolas Fella committed
122 123

#include "kioservices.moc"