1/*
2 Copyright (c) 2009 Volker Krause <vkrause@kde.org>
3
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.
8
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.
13
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
17 02110-1301, USA.
18*/
19
20#include "global.h"
21
22#include <kdebug.h>
23#include <KGlobal>
24#include <KStandardDirs>
25#include <QtCore/qfileinfo.h>
26#include <QtCore/QDir>
27#include <kio/copyjob.h>
28#include <kio/netaccess.h>
29
30class GlobalPrivate
31{
32 public:
33 QStringList filter;
34 QString assistant;
35};
36
37K_GLOBAL_STATIC( GlobalPrivate, sInstance )
38
39
40QString Global::assistant()
41{
42 return sInstance->assistant;
43}
44
45void Global::setAssistant(const QString& assistant)
46{
47 const QFileInfo info( assistant );
48 if ( info.isAbsolute() ) {
49 sInstance->assistant = assistant;
50 return;
51 }
52
53 const QStringList list = KGlobal::dirs()->findAllResources(
54 "data", QLatin1String( "akonadi/accountwizard/*.desktop" ),
55 KStandardDirs::Recursive | KStandardDirs::NoDuplicates );
56 foreach ( const QString &entry, list ) {
57 const QFileInfo info( entry );
58 const QDir dir( info.absolutePath() );
59 kDebug() << dir.dirName();
60 if ( dir.dirName() == assistant ) {
61 sInstance->assistant = entry;
62 return;
63 }
64 }
65
66 sInstance->assistant.clear();
67}
68
69
70QStringList Global::typeFilter()
71{
72 return sInstance->filter;
73}
74
75void Global::setTypeFilter(const QStringList& filter)
76{
77 sInstance->filter = filter;
78}
79
80QString Global::assistantBasePath()
81{
82 const QFileInfo info( assistant() );
83 if ( info.isAbsolute() )
84 return info.absolutePath() + QDir::separator();
85 return QString();
86}
87
88QString Global::unpackAssistant( const KUrl& remotePackageUrl )
89{
90 QString localPackageFile;
91 if ( remotePackageUrl.protocol() == QLatin1String( "file" ) ) {
92 localPackageFile = remotePackageUrl.path();
93 } else {
94 QString remoteFileName = QFileInfo( remotePackageUrl.path() ).fileName();
95 localPackageFile = KStandardDirs::locateLocal( "cache", QLatin1String("accountwizard/") + remoteFileName );
96 KIO::Job* job = KIO::copy( remotePackageUrl, localPackageFile, KIO::Overwrite | KIO::HideProgressInfo );
97 kDebug() << "downloading remote URL" << remotePackageUrl << "to" << localPackageFile;
98 if ( !KIO::NetAccess::synchronousRun( job, 0 ) )
99 return QString();
100 }
101
102 const KUrl file( QLatin1String("tar://") + localPackageFile );
103 const QFileInfo fi( localPackageFile );
104 const QString assistant = fi.baseName();
105 const QString dest = KStandardDirs::locateLocal( "appdata", QLatin1String("/") );
106 KStandardDirs::makeDir( dest + file.fileName() );
107 KIO::Job* getJob = KIO::copy( file, dest, KIO::Overwrite | KIO::HideProgressInfo );
108 if ( KIO::NetAccess::synchronousRun( getJob, 0 ) ) {
109 kDebug() << "worked, unpacked in " << dest;
110 return dest + file.fileName() + QLatin1Char('/') + assistant + QLatin1Char('/') + assistant + QLatin1String(".desktop");
111 } else {
112 kDebug() << "failed" << getJob->errorString();
113 return QString();
114 }
115}
116