global.cpp 5.39 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/*
    Copyright (c) 2009 Volker Krause <vkrause@kde.org>

    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 "global.h"

Laurent Montel's avatar
Laurent Montel committed
22
#include "accountwizard_debug.h"
Laurent Montel's avatar
Laurent Montel committed
23
#include <QFileInfo>
Laurent Montel's avatar
Laurent Montel committed
24
#include <QDir>
25 26 27 28 29 30 31 32
#include <kio/copyjob.h>
#include <QStandardPaths>

class GlobalPrivate
{
public:
    QStringList filter;
    QString assistant;
33
    QString poFileName;
34 35 36 37 38 39 40 41 42
};

Q_GLOBAL_STATIC(GlobalPrivate, sInstance)

QString Global::assistant()
{
    return sInstance->assistant;
}

Laurent Montel's avatar
Laurent Montel committed
43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
QStringList Global::assistants()
{
    QStringList list;
    const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("akonadi/accountwizard/"), QStandardPaths::LocateDirectory);
    for (const QString &dir : dirs) {
        const QStringList directories = QDir(dir).entryList(QDir::AllDirs);
        for (const QString &directory : directories) {
            const QString fullPath = dir + QLatin1Char('/') + directory;
            const QStringList fileNames = QDir(fullPath).entryList(QStringList() << QStringLiteral("*.desktop"));
            list.reserve(fileNames.count());
            for (const QString &file : fileNames) {
                list.append(fullPath + QLatin1Char('/') + file);
            }
        }
    }
    QStringList lstAssistants;
Laurent Montel's avatar
Laurent Montel committed
59
    lstAssistants.reserve(list.count());
Laurent Montel's avatar
Laurent Montel committed
60 61 62 63 64 65 66 67
    for (const QString &entry : qAsConst(list)) {
        const QFileInfo info(entry);
        const QDir dir(info.absolutePath());
        lstAssistants.append(dir.dirName());
    }
    return lstAssistants;
}

68 69 70 71 72 73 74 75 76 77
void Global::setAssistant(const QString &assistant)
{
    const QFileInfo info(assistant);
    if (info.isAbsolute()) {
        sInstance->assistant = assistant;
        return;
    }

    QStringList list;
    const QStringList dirs = QStandardPaths::locateAll(QStandardPaths::GenericDataLocation, QStringLiteral("akonadi/accountwizard/"), QStandardPaths::LocateDirectory);
Laurent Montel's avatar
Laurent Montel committed
78
    for (const QString &dir : dirs) {
79
        const QStringList directories = QDir(dir).entryList(QDir::AllDirs);
Laurent Montel's avatar
Laurent Montel committed
80
        for (const QString &directory : directories) {
81 82
            const QString fullPath = dir + QLatin1Char('/') + directory;
            const QStringList fileNames = QDir(fullPath).entryList(QStringList() << QStringLiteral("*.desktop"));
Laurent Montel's avatar
Clazy++  
Laurent Montel committed
83
            list.reserve(fileNames.count());
Laurent Montel's avatar
Laurent Montel committed
84
            for (const QString &file : fileNames) {
85 86
                list.append(fullPath + QLatin1Char('/') + file);
            }
87 88
        }
    }
Laurent Montel's avatar
Laurent Montel committed
89
    for (const QString &entry : qAsConst(list)) {
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
        const QFileInfo info(entry);
        const QDir dir(info.absolutePath());
        if (dir.dirName() == assistant) {
            sInstance->assistant = entry;
            return;
        }
    }

    sInstance->assistant.clear();
}

QStringList Global::typeFilter()
{
    return sInstance->filter;
}

void Global::setTypeFilter(const QStringList &filter)
{
    sInstance->filter = filter;
}

QString Global::assistantBasePath()
{
    const QFileInfo info(assistant());
    if (info.isAbsolute()) {
        return info.absolutePath() + QDir::separator();
    }
    return QString();
}

QString Global::unpackAssistant(const QUrl &remotePackageUrl)
{
    QString localPackageFile;
    if (remotePackageUrl.scheme() == QLatin1String("file")) {
        localPackageFile = remotePackageUrl.path();
    } else {
        QString remoteFileName = QFileInfo(remotePackageUrl.path()).fileName();
Laurent Montel's avatar
Laurent Montel committed
127
        localPackageFile = QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1String("/accountwizard/") + remoteFileName;
128
        KIO::Job *job = KIO::copy(remotePackageUrl, QUrl::fromLocalFile(localPackageFile), KIO::Overwrite | KIO::HideProgressInfo);
Laurent Montel's avatar
Laurent Montel committed
129
        qCDebug(ACCOUNTWIZARD_LOG) << "downloading remote URL" << remotePackageUrl << "to" << localPackageFile;
Volker Krause's avatar
Volker Krause committed
130
        if (!job->exec()) {
131 132 133 134 135 136 137
            return QString();
        }
    }

    const QUrl file(QLatin1String("tar://") + localPackageFile);
    const QFileInfo fi(localPackageFile);
    const QString assistant = fi.baseName();
Laurent Montel's avatar
Laurent Montel committed
138
    const QString dest = QStandardPaths::writableLocation(QStandardPaths::AppLocalDataLocation) + QLatin1Char('/');
139 140
    QDir().mkpath(dest + file.fileName());
    KIO::Job *getJob = KIO::copy(file, QUrl::fromLocalFile(dest), KIO::Overwrite | KIO::HideProgressInfo);
Volker Krause's avatar
Volker Krause committed
141
    if (getJob->exec()) {
Laurent Montel's avatar
Laurent Montel committed
142
        qCDebug(ACCOUNTWIZARD_LOG) << "worked, unpacked in " << dest;
143 144
        return dest + file.fileName() + QLatin1Char('/') + assistant + QLatin1Char('/') + assistant + QLatin1String(".desktop");
    } else {
Laurent Montel's avatar
Laurent Montel committed
145
        qCDebug(ACCOUNTWIZARD_LOG) << "failed" << getJob->errorString();
146 147 148 149
        return QString();
    }
}

150 151 152 153 154 155 156 157 158
void Global::setPoFileName(const QString &poFileName)
{
    sInstance->poFileName = poFileName;
}

QString Global::poFileName()
{
    return sInstance->poFileName;
}