• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Plasma

  • sources
  • kde-4.12
  • kdelibs
  • plasma
  • remote
accessappletjob.cpp
Go to the documentation of this file.
1 /*
2  * Copyright 2009 Rob Scheepmaker <r.scheepmaker@student.utwente.nl>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU Library General Public License as
6  * published by the Free Software Foundation; either version 2, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  * GNU General Public License for more details
13  *
14  * You should have received a copy of the GNU Library General Public
15  * License along with this program; if not, write to the
16  * Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19 
20 #include "accessappletjob.h"
21 
22 #include "service.h"
23 #include "servicejob.h"
24 #include "applet.h"
25 
26 #include "config-plasma.h"
27 
28 #include <kzip.h>
29 #include <kdebug.h>
30 #include <kmessagebox.h>
31 #include <ktempdir.h>
32 #include <kdesktopfile.h>
33 #include "package.h"
34 #include <qtimer.h>
35 #include "private/applet_p.h"
36 
37 namespace Plasma
38 {
39 
40 class AccessAppletJobPrivate
41 {
42 public:
43  AccessAppletJobPrivate(const KUrl &location, AccessAppletJob *owner)
44  : q(owner),
45  location(location),
46  applet(0)
47  {
48  }
49 
50  void slotStart()
51  {
52  q->start();
53  }
54 
55  void slotServiceReady(Plasma::Service *service)
56  {
57  KConfigGroup op = service->operationDescription("GetPackage");
58  service->startOperationCall(op);
59  q->connect(service, SIGNAL(finished(Plasma::ServiceJob*)),
60  q, SLOT(slotPackageDownloaded(Plasma::ServiceJob*)));
61  }
62 
63  void slotPackageDownloaded(Plasma::ServiceJob *job)
64  {
65  if (job->error()) {
66  kDebug() << "Plasmoid Access Job triggers an error.";
67  q->setError(job->error());
68  q->setErrorText(job->errorText());
69  }
70 
71  //TODO: there's some duplication with installPackage, but we don't want to actually install
72  //the fetched package. Just extract the archive somewhere in a temp directory.
73  if (job->result().type() == QVariant::String) {
74  QString pluginName = job->result().toString();
75  kDebug() << "Server responded with a pluginname, trying to load: " << pluginName;
76 
77  applet = Applet::load(pluginName);
78  if (applet) {
79  applet->d->remoteLocation = location.prettyUrl();
80  } else {
81  q->setError(-1);
82  q->setErrorText(i18n("The \"%1\" widget is not installed.", pluginName));
83  }
84 
85  q->emitResult();
86  } else {
87  kDebug() << "Server responded with a plasmoid package";
88  //read, and extract the plasmoid package to a temporary directory
89  QByteArray package = job->result().toByteArray();
90  QDataStream stream(&package, QIODevice::ReadOnly);
91 
92  KZip archive(stream.device());
93  if (!archive.open(QIODevice::ReadOnly)) {
94  kWarning() << "Could not open package file";
95  q->setError(-1);
96  q->setErrorText(i18n("Server sent an invalid plasmoid package."));
97  q->emitResult();
98  return;
99  }
100 
101  const KArchiveDirectory *source = archive.directory();
102 
103  KTempDir tempDir;
104  tempDir.setAutoRemove(false);
105  QString path = tempDir.name();
106  source->copyTo(path);
107 
108  KDesktopFile metadata(path + "/metadata.desktop");
109  KConfigGroup group = metadata.desktopGroup();
110 
111  QString iconName = group.readEntry("Icon");
112  QString message = i18n("You are about to open a remote widget on your system.<br>");
113  message+= i18n("<table width=\"100%\">");
114  message+= i18n("<tr><td align=\"right\"><b>Name:</b></td><td>&nbsp; %1</td></tr>", group.readEntry("Name"));
115  message+= i18n("<tr><td align=\"right\"><b>Description:</b></td><td>&nbsp; %1</td></tr>", group.readEntry("Comment"));
116  message+= i18n("<tr><td align=\"right\"><b>Author:</b></td><td>&nbsp; %1 &lt;%2&gt;</td></tr>",
117  group.readEntry("X-KDE-PluginInfo-Author"),
118  group.readEntry("X-KDE-PluginInfo-Email"));
119  message+= i18n("<tr><td align=\"right\"><b>Server:</b></td><td>&nbsp; %1</td></tr>", location.host());
120  message+= i18n("</table>");
121  message+= i18n("<br><br>Are you sure you want to open this widget on your system?");
122 
123  KDialog *dialog = new KDialog;
124  dialog->setWindowTitle(i18n("Remote Widget"));
125  dialog->setButtons(KDialog::Yes|KDialog::No);
126  dialog->setButtonText(KDialog::Yes, i18n("Open Widget"));
127  dialog->setButtonText(KDialog::No, i18n("Reject Widget"));
128 
129  int answer = KMessageBox::createKMessageBox(dialog, KIcon(iconName), message,
130  QStringList(), QString(), 0,
131  KMessageBox::Dangerous);
132  //int answer = KMessageBox::questionYesNo(0, message, i18n("Remote Widget"));
133 
134  if (answer!=KDialog::Yes) {
135  q->setError(-1);
136  q->setErrorText(i18n("User rejected"));
137  q->emitResult();
138  return;
139  }
140 
149  applet = Applet::loadPlasmoid(path);
150  if (applet) {
151  applet->d->remoteLocation = location.prettyUrl();
152  } else {
153  q->setError(-1);
154  }
155 
156  q->emitResult();
157  }
158  }
159 
160  void slotTimeout()
161  {
162  kWarning() << "Plasmoid access job timed out";
163  q->setError(-1);
164  q->setErrorText(i18n("Timeout"));
165  q->emitResult();
166  }
167 
168  AccessAppletJob *q;
169  KUrl location;
170  Applet *applet;
171 };
172 
173 AccessAppletJob::AccessAppletJob(const KUrl &location, QObject *parent)
174  : KJob(parent),
175  d(new AccessAppletJobPrivate(location, this))
176 {
177  QTimer::singleShot(30000, this, SLOT(slotTimeout()));
178 }
179 
180 AccessAppletJob::~AccessAppletJob()
181 {
182  delete d;
183 }
184 
185 Applet *AccessAppletJob::applet() const
186 {
187  return d->applet;
188 }
189 
190 void AccessAppletJob::start()
191 {
192 #ifdef ENABLE_REMOTE_WIDGETS
193  kDebug() << "fetching a plasmoid from location = " << d->location.prettyUrl();
194  Service *service = Service::access(d->location);
195  connect(service, SIGNAL(serviceReady(Plasma::Service*)),
196  this, SLOT(slotServiceReady(Plasma::Service*)));
197 #else
198  kWarning() << "libplasma was compiled without support for remote services. Accessing remote applet failed because of that.";
199  setError(-1);
200  setErrorText(i18n("Your system does not provide support for the 'remote widgets' feature. Access Failed."));
201  emitResult();
202 #endif
203 }
204 
205 } // namespace Plasma
206 
207 #include "accessappletjob.moc"
208 
Plasma::ServiceJob
This class provides jobs for use with Plasma::Service.
Definition: servicejob.h:54
Plasma::Service::startOperationCall
Q_INVOKABLE ServiceJob * startOperationCall(const KConfigGroup &description, QObject *parent=0)
Called to create a ServiceJob which is associated with a given operation and parameter set...
Definition: service.cpp:215
Plasma::AccessAppletJob::applet
Applet * applet() const
Definition: accessappletjob.cpp:185
Plasma::ServiceJob::result
QVariant result
Definition: servicejob.h:59
QObject
Plasma::Service::operationDescription
Q_INVOKABLE KConfigGroup operationDescription(const QString &operationName)
Retrieves the parameters for a given operation.
Definition: service.cpp:181
Plasma::AccessAppletJob::start
void start()
Definition: accessappletjob.cpp:190
Plasma::Service
This class provides a generic API for write access to settings or services.
Definition: service.h:91
Plasma::Applet
The base Applet class.
Definition: applet.h:77
Plasma::AccessAppletJob::~AccessAppletJob
~AccessAppletJob()
Definition: accessappletjob.cpp:180
applet.h
servicejob.h
Plasma::Applet::loadPlasmoid
static Applet * loadPlasmoid(const QString &path, uint appletId=0, const QVariantList &args=QVariantList())
Attempts to load an applet from a package.
Definition: applet.cpp:2404
service.h
package.h
Plasma::Service::access
static Service * access(const KUrl &url, QObject *parent=0)
Used to access a service from an url.
Definition: service.cpp:80
accessappletjob.h
KJob
Plasma::Applet::load
static Applet * load(const QString &name, uint appletId=0, const QVariantList &args=QVariantList())
Attempts to load an applet.
Definition: applet.cpp:2422
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:48:32 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal