2 Copyright (c) 2009 Volker Krause <vkrause@kde.org>
4 This library is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Library General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or (at your
7 option) any later version.
9 This library is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12 License for more details.
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to the
16 Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22 #include "accountwizard_debug.h"
23 #include <qfileinfo.h>
25 #include <kio/copyjob.h>
27 #include <QStandardPaths>
36 Q_GLOBAL_STATIC(GlobalPrivate
, sInstance
)
38 QString
Global::assistant()
40 return sInstance
->assistant
;
43 void Global::setAssistant(const QString
&assistant
)
45 const QFileInfo
info(assistant
);
46 if (info
.isAbsolute()) {
47 sInstance
->assistant
= assistant
;
53 const QStringList dirs
= QStandardPaths::locateAll(QStandardPaths::GenericDataLocation
, QStringLiteral("akonadi/accountwizard/"), QStandardPaths::LocateDirectory
);
54 Q_FOREACH (const QString
&dir
, dirs
) {
55 const QStringList directories
= QDir(dir
).entryList(QDir::AllDirs
);
56 Q_FOREACH (const QString
&directory
, directories
) {
57 const QString fullPath
= dir
+ QLatin1Char('/') + directory
;
58 const QStringList fileNames
= QDir(fullPath
).entryList(QStringList() << QStringLiteral("*.desktop"));
59 list
.reserve(fileNames
.count());
60 Q_FOREACH (const QString
&file
, fileNames
) {
61 list
.append(fullPath
+ QLatin1Char('/') + file
);
66 foreach (const QString
&entry
, list
) {
67 const QFileInfo
info(entry
);
68 const QDir
dir(info
.absolutePath());
69 if (dir
.dirName() == assistant
) {
70 sInstance
->assistant
= entry
;
75 sInstance
->assistant
.clear();
78 QStringList
Global::typeFilter()
80 return sInstance
->filter
;
83 void Global::setTypeFilter(const QStringList
&filter
)
85 sInstance
->filter
= filter
;
88 QString
Global::assistantBasePath()
90 const QFileInfo
info(assistant());
91 if (info
.isAbsolute()) {
92 return info
.absolutePath() + QDir::separator();
97 QString
Global::unpackAssistant(const QUrl
&remotePackageUrl
)
99 QString localPackageFile
;
100 if (remotePackageUrl
.scheme() == QLatin1String("file")) {
101 localPackageFile
= remotePackageUrl
.path();
103 QString remoteFileName
= QFileInfo(remotePackageUrl
.path()).fileName();
104 localPackageFile
= QStandardPaths::writableLocation(QStandardPaths::CacheLocation
) + QLatin1String("/accountwizard/") + remoteFileName
;
105 KIO::Job
*job
= KIO::copy(remotePackageUrl
, QUrl::fromLocalFile(localPackageFile
), KIO::Overwrite
| KIO::HideProgressInfo
);
106 qCDebug(ACCOUNTWIZARD_LOG
) << "downloading remote URL" << remotePackageUrl
<< "to" << localPackageFile
;
112 const QUrl
file(QLatin1String("tar://") + localPackageFile
);
113 const QFileInfo
fi(localPackageFile
);
114 const QString assistant
= fi
.baseName();
115 const QString dest
= QStandardPaths::writableLocation(QStandardPaths::DataLocation
) + QLatin1String("/");
116 QDir().mkpath(dest
+ file
.fileName());
117 KIO::Job
*getJob
= KIO::copy(file
, QUrl::fromLocalFile(dest
), KIO::Overwrite
| KIO::HideProgressInfo
);
118 if (getJob
->exec()) {
119 qCDebug(ACCOUNTWIZARD_LOG
) << "worked, unpacked in " << dest
;
120 return dest
+ file
.fileName() + QLatin1Char('/') + assistant
+ QLatin1Char('/') + assistant
+ QLatin1String(".desktop");
122 qCDebug(ACCOUNTWIZARD_LOG
) << "failed" << getJob
->errorString();