Plasma
accessappletjob.cpp
Go to the documentation of this file.
00001 /* 00002 * Copyright 2009 Rob Scheepmaker <r.scheepmaker@student.utwente.nl> 00003 * 00004 * This program is free software; you can redistribute it and/or modify 00005 * it under the terms of the GNU Library General Public License as 00006 * published by the Free Software Foundation; either version 2, or 00007 * (at your option) any later version. 00008 * 00009 * This program is distributed in the hope that it will be useful, 00010 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00012 * GNU General Public License for more details 00013 * 00014 * You should have received a copy of the GNU Library General Public 00015 * License along with this program; if not, write to the 00016 * Free Software Foundation, Inc., 00017 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. 00018 */ 00019 00020 #include "accessappletjob.h" 00021 00022 #include "service.h" 00023 #include "servicejob.h" 00024 #include "applet.h" 00025 00026 #include "config-plasma.h" 00027 00028 #include <kzip.h> 00029 #include <kdebug.h> 00030 #include <ktempdir.h> 00031 #include <kdesktopfile.h> 00032 #include "package.h" 00033 #include <qtimer.h> 00034 #include "private/applet_p.h" 00035 00036 namespace Plasma 00037 { 00038 00039 class AccessAppletJobPrivate 00040 { 00041 public: 00042 AccessAppletJobPrivate(const KUrl &location, AccessAppletJob *owner) 00043 : q(owner), 00044 location(location) 00045 { 00046 } 00047 00048 void slotStart() 00049 { 00050 q->start(); 00051 } 00052 00053 void slotServiceReady(Plasma::Service *service) 00054 { 00055 KConfigGroup op = service->operationDescription("GetPackage"); 00056 service->startOperationCall(op); 00057 q->connect(service, SIGNAL(finished(Plasma::ServiceJob*)), 00058 q, SLOT(slotPackageDownloaded(Plasma::ServiceJob*))); 00059 } 00060 00061 void slotPackageDownloaded(Plasma::ServiceJob *job) 00062 { 00063 if (job->error()) { 00064 kDebug() << "Plasmoid Access Job triggers an error."; 00065 q->setError(job->error()); 00066 q->setErrorText(job->errorText()); 00067 } 00068 00069 //TODO: there's some duplication with installPackage, but we don't want to actually install 00070 //the fetched package. Just extract the archive somewhere in a temp directory. 00071 if (job->result().type() == QVariant::String) { 00072 QString pluginName = job->result().toString(); 00073 kDebug() << "Server responded with a pluginname, trying to load: " << pluginName; 00074 00075 applet = Applet::load(pluginName); 00076 if (applet) { 00077 applet->d->remoteLocation = location.prettyUrl(); 00078 } else { 00079 q->setError(-1); 00080 q->setErrorText(i18n("The \"%1\" widget is not installed.", pluginName)); 00081 } 00082 00083 q->emitResult(); 00084 } else { 00085 kDebug() << "Server responded with a plasmoid package"; 00086 //read, and extract the plasmoid package to a temporary directory 00087 QByteArray package = job->result().toByteArray(); 00088 QDataStream stream(&package, QIODevice::ReadOnly); 00089 00090 KZip archive(stream.device()); 00091 if (!archive.open(QIODevice::ReadOnly)) { 00092 kWarning() << "Could not open package file"; 00093 q->setError(-1); 00094 q->setErrorText(i18n("Server sent an invalid plasmoid package.")); 00095 q->emitResult(); 00096 return; 00097 } 00098 00099 const KArchiveDirectory *source = archive.directory(); 00100 00101 KTempDir tempDir; 00102 tempDir.setAutoRemove(false); 00103 QString path = tempDir.name(); 00104 source->copyTo(path); 00105 00114 applet = Applet::loadPlasmoid(path); 00115 applet->d->remoteLocation = location.prettyUrl(); 00116 q->emitResult(); 00117 } 00118 } 00119 00120 void slotTimeout() 00121 { 00122 kWarning() << "Plasmoid access job timed out"; 00123 q->setError(-1); 00124 q->setErrorText(i18n("Timeout")); 00125 q->emitResult(); 00126 } 00127 00128 AccessAppletJob *q; 00129 KUrl location; 00130 Applet *applet; 00131 }; 00132 00133 AccessAppletJob::AccessAppletJob(const KUrl &location, QObject *parent) 00134 : KJob(parent), 00135 d(new AccessAppletJobPrivate(location, this)) 00136 { 00137 QTimer::singleShot(30000, this, SLOT(slotTimeout())); 00138 } 00139 00140 AccessAppletJob::~AccessAppletJob() 00141 { 00142 delete d; 00143 } 00144 00145 Applet *AccessAppletJob::applet() const 00146 { 00147 return d->applet; 00148 } 00149 00150 void AccessAppletJob::start() 00151 { 00152 #ifdef ENABLE_REMOTE_WIDGETS 00153 kDebug() << "fetching a plasmoid from location = " << d->location.prettyUrl(); 00154 Service *service = Service::access(d->location); 00155 connect(service, SIGNAL(serviceReady(Plasma::Service*)), 00156 this, SLOT(slotServiceReady(Plasma::Service*))); 00157 #else 00158 kWarning() << "libplasma was compiled without support for remote services. Accessing remote applet failed because of that."; 00159 setError(-1); 00160 setErrorText(i18n("Your system does not provide support for the 'remote widgets' feature. Access Failed.")); 00161 emitResult(); 00162 #endif 00163 } 00164 00165 } // namespace Plasma 00166 00167 #include "accessappletjob.moc" 00168
KDE 4.6 API Reference