2009-09-02 04:27:16 +02:00
|
|
|
/*
|
|
|
|
* Copyright 2009 Rob Scheepmaker <r.scheepmaker@student.utwente.nl>
|
|
|
|
*
|
|
|
|
* This program 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, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program 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 General Public License for more details
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU Library General Public
|
|
|
|
* License along with this program; if not, write to the
|
|
|
|
* Free Software Foundation, Inc.,
|
|
|
|
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "accessappletjob.h"
|
|
|
|
|
2011-07-19 22:04:22 +02:00
|
|
|
#include <qtimer.h>
|
2009-09-09 15:40:16 +02:00
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
#include <kdebug.h>
|
2011-07-19 22:04:22 +02:00
|
|
|
#include <kdesktopfile.h>
|
2011-04-29 20:45:36 +02:00
|
|
|
#include <kmessagebox.h>
|
2009-09-02 04:27:16 +02:00
|
|
|
#include <ktempdir.h>
|
2011-07-19 22:04:22 +02:00
|
|
|
#include <kzip.h>
|
|
|
|
|
|
|
|
#include "config-plasma.h"
|
|
|
|
|
|
|
|
#include "applet.h"
|
2009-09-02 04:27:16 +02:00
|
|
|
#include "package.h"
|
2011-07-19 22:04:22 +02:00
|
|
|
#include "pluginloader.h"
|
2009-09-02 04:27:16 +02:00
|
|
|
#include "private/applet_p.h"
|
2011-07-19 22:04:22 +02:00
|
|
|
#include "service.h"
|
|
|
|
#include "servicejob.h"
|
2009-09-02 04:27:16 +02:00
|
|
|
|
|
|
|
namespace Plasma
|
|
|
|
{
|
|
|
|
|
|
|
|
class AccessAppletJobPrivate
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
AccessAppletJobPrivate(const KUrl &location, AccessAppletJob *owner)
|
|
|
|
: q(owner),
|
2011-04-28 21:48:43 +02:00
|
|
|
location(location),
|
|
|
|
applet(0)
|
2009-09-02 04:27:16 +02:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
void slotStart()
|
|
|
|
{
|
|
|
|
q->start();
|
|
|
|
}
|
|
|
|
|
|
|
|
void slotServiceReady(Plasma::Service *service)
|
|
|
|
{
|
|
|
|
KConfigGroup op = service->operationDescription("GetPackage");
|
|
|
|
service->startOperationCall(op);
|
|
|
|
q->connect(service, SIGNAL(finished(Plasma::ServiceJob*)),
|
|
|
|
q, SLOT(slotPackageDownloaded(Plasma::ServiceJob*)));
|
|
|
|
}
|
|
|
|
|
|
|
|
void slotPackageDownloaded(Plasma::ServiceJob *job)
|
|
|
|
{
|
|
|
|
if (job->error()) {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2009-09-02 04:27:16 +02:00
|
|
|
kDebug() << "Plasmoid Access Job triggers an error.";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2009-09-02 04:27:16 +02:00
|
|
|
q->setError(job->error());
|
|
|
|
q->setErrorText(job->errorText());
|
|
|
|
}
|
|
|
|
|
|
|
|
//TODO: there's some duplication with installPackage, but we don't want to actually install
|
|
|
|
//the fetched package. Just extract the archive somewhere in a temp directory.
|
|
|
|
if (job->result().type() == QVariant::String) {
|
|
|
|
QString pluginName = job->result().toString();
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2009-09-02 04:27:16 +02:00
|
|
|
kDebug() << "Server responded with a pluginname, trying to load: " << pluginName;
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2009-09-02 04:27:16 +02:00
|
|
|
|
2011-07-19 22:04:22 +02:00
|
|
|
applet = PluginLoader::self()->loadApplet(pluginName);
|
2009-09-02 04:27:16 +02:00
|
|
|
if (applet) {
|
2011-05-31 00:04:03 +02:00
|
|
|
applet->d->remoteLocation = location;
|
2009-09-02 04:27:16 +02:00
|
|
|
} else {
|
|
|
|
q->setError(-1);
|
2009-09-03 22:33:02 +02:00
|
|
|
q->setErrorText(i18n("The \"%1\" widget is not installed.", pluginName));
|
2009-09-02 04:27:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
q->emitResult();
|
|
|
|
} else {
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2009-09-02 04:27:16 +02:00
|
|
|
kDebug() << "Server responded with a plasmoid package";
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2009-09-02 04:27:16 +02:00
|
|
|
//read, and extract the plasmoid package to a temporary directory
|
|
|
|
QByteArray package = job->result().toByteArray();
|
|
|
|
QDataStream stream(&package, QIODevice::ReadOnly);
|
|
|
|
|
|
|
|
KZip archive(stream.device());
|
|
|
|
if (!archive.open(QIODevice::ReadOnly)) {
|
|
|
|
kWarning() << "Could not open package file";
|
|
|
|
q->setError(-1);
|
|
|
|
q->setErrorText(i18n("Server sent an invalid plasmoid package."));
|
|
|
|
q->emitResult();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const KArchiveDirectory *source = archive.directory();
|
|
|
|
|
|
|
|
KTempDir tempDir;
|
|
|
|
tempDir.setAutoRemove(false);
|
|
|
|
QString path = tempDir.name();
|
|
|
|
source->copyTo(path);
|
|
|
|
|
2011-04-29 20:45:36 +02:00
|
|
|
KDesktopFile metadata(path + "/metadata.desktop");
|
|
|
|
KConfigGroup group = metadata.desktopGroup();
|
|
|
|
|
|
|
|
QString iconName = group.readEntry("Icon");
|
|
|
|
QString message = i18n("You are about to open a remote widget on your system.<br>");
|
|
|
|
message+= i18n("<table width=\"100%\">");
|
|
|
|
message+= i18n("<tr><td align=\"right\"><b>Name:</b></td><td> %1</td></tr>", group.readEntry("Name"));
|
|
|
|
message+= i18n("<tr><td align=\"right\"><b>Description:</b></td><td> %1</td></tr>", group.readEntry("Comment"));
|
|
|
|
message+= i18n("<tr><td align=\"right\"><b>Author:</b></td><td> %1 <%2></td></tr>",
|
|
|
|
group.readEntry("X-KDE-PluginInfo-Author"),
|
|
|
|
group.readEntry("X-KDE-PluginInfo-Email"));
|
|
|
|
message+= i18n("<tr><td align=\"right\"><b>Server:</b></td><td> %1</td></tr>", location.host());
|
|
|
|
message+= i18n("</table>");
|
|
|
|
message+= i18n("<br><br>Are you sure you want to open this widget on your system?");
|
|
|
|
|
|
|
|
KDialog *dialog = new KDialog;
|
|
|
|
dialog->setWindowTitle(i18n("Remote Widget"));
|
|
|
|
dialog->setButtons(KDialog::Yes|KDialog::No);
|
|
|
|
dialog->setButtonText(KDialog::Yes, i18n("Open Widget"));
|
|
|
|
dialog->setButtonText(KDialog::No, i18n("Reject Widget"));
|
|
|
|
|
|
|
|
int answer = KMessageBox::createKMessageBox(dialog, KIcon(iconName), message,
|
|
|
|
QStringList(), QString(), 0,
|
|
|
|
KMessageBox::Dangerous);
|
|
|
|
|
|
|
|
if (answer!=KDialog::Yes) {
|
|
|
|
q->setError(-1);
|
2011-08-28 10:38:09 +02:00
|
|
|
q->setErrorText(i18nc("A remote widget was rejected by the user.", "User rejected"));
|
2011-04-29 20:45:36 +02:00
|
|
|
q->emitResult();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
applet = Applet::loadPlasmoid(path);
|
2011-04-28 21:48:43 +02:00
|
|
|
if (applet) {
|
2011-05-31 00:04:03 +02:00
|
|
|
applet->d->remoteLocation = location;
|
2011-04-28 21:48:43 +02:00
|
|
|
} else {
|
|
|
|
q->setError(-1);
|
|
|
|
}
|
|
|
|
|
2009-09-02 04:27:16 +02:00
|
|
|
q->emitResult();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void slotTimeout()
|
|
|
|
{
|
|
|
|
kWarning() << "Plasmoid access job timed out";
|
|
|
|
q->setError(-1);
|
|
|
|
q->setErrorText(i18n("Timeout"));
|
|
|
|
q->emitResult();
|
|
|
|
}
|
|
|
|
|
|
|
|
AccessAppletJob *q;
|
|
|
|
KUrl location;
|
|
|
|
Applet *applet;
|
|
|
|
};
|
|
|
|
|
|
|
|
AccessAppletJob::AccessAppletJob(const KUrl &location, QObject *parent)
|
|
|
|
: KJob(parent),
|
|
|
|
d(new AccessAppletJobPrivate(location, this))
|
|
|
|
{
|
|
|
|
QTimer::singleShot(30000, this, SLOT(slotTimeout()));
|
|
|
|
}
|
|
|
|
|
|
|
|
AccessAppletJob::~AccessAppletJob()
|
|
|
|
{
|
|
|
|
delete d;
|
|
|
|
}
|
|
|
|
|
|
|
|
Applet *AccessAppletJob::applet() const
|
|
|
|
{
|
|
|
|
return d->applet;
|
|
|
|
}
|
|
|
|
|
|
|
|
void AccessAppletJob::start()
|
|
|
|
{
|
2009-09-09 15:40:16 +02:00
|
|
|
#ifdef ENABLE_REMOTE_WIDGETS
|
2011-07-29 15:46:52 +02:00
|
|
|
#ifndef NDEBUG
|
2009-09-02 04:27:16 +02:00
|
|
|
kDebug() << "fetching a plasmoid from location = " << d->location.prettyUrl();
|
2011-07-29 15:46:52 +02:00
|
|
|
#endif
|
2009-09-02 04:27:16 +02:00
|
|
|
Service *service = Service::access(d->location);
|
|
|
|
connect(service, SIGNAL(serviceReady(Plasma::Service*)),
|
|
|
|
this, SLOT(slotServiceReady(Plasma::Service*)));
|
2009-09-09 15:40:16 +02:00
|
|
|
#else
|
|
|
|
kWarning() << "libplasma was compiled without support for remote services. Accessing remote applet failed because of that.";
|
|
|
|
setError(-1);
|
2009-10-09 22:07:44 +02:00
|
|
|
setErrorText(i18n("Your system does not provide support for the 'remote widgets' feature. Access Failed."));
|
2009-09-09 15:40:16 +02:00
|
|
|
emitResult();
|
|
|
|
#endif
|
2009-09-02 04:27:16 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Plasma
|
|
|
|
|
|
|
|
#include "accessappletjob.moc"
|
|
|
|
|