1
/***************************************************************************
2
* Copyright 2010 Artur Duque de Souza <asouza@kde.org> *
4
* This program is free software; you can redistribute it and/or modify *
5
* it under the terms of the GNU General Public License as published by *
6
* the Free Software Foundation; either version 2 of the License, or *
7
* (at your option) any later version. *
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. *
14
* You should have received a copy of the GNU General Public License *
15
* 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
***************************************************************************/
20
#include <QtCore/QFile>
22
#include <kross/core/action.h>
23
#include <kross/core/interpreter.h>
24
#include <kross/core/manager.h>
25
#include <kstandarddirs.h>
27
#include <Plasma/Package>
29
#include "shareservice.h"
30
#include "shareprovider.h"
33
ShareService::ShareService(ShareEngine *engine)
34
: Plasma::Service(engine)
39
Plasma::ServiceJob *ShareService::createJob(const QString &operation,
40
QMap<QString, QVariant> ¶meters)
42
return new ShareJob(destination(), operation, parameters, this);
45
ShareJob::ShareJob(const QString &destination, const QString &operation,
46
QMap<QString, QVariant> ¶meters, QObject *parent)
47
: Plasma::ServiceJob(destination, operation, parameters, parent),
48
m_action(0), m_provider(0), m_package(0)
59
void ShareJob::start()
61
//KService::Ptr service = KService::serviceByStorageId("plasma-share-pastebincom.desktop");
62
KService::Ptr service = KService::serviceByStorageId(destination());
64
showError(i18n("Could not find the provider with the specified destination"));
69
service->property("X-KDE-PluginInfo-Name", QVariant::String).toString();
72
KStandardDirs::locate("data", "plasma/shareprovider/" + pluginName + '/' );
75
showError(i18n("Invalid path for the requested provider"));
79
m_package = new Plasma::Package(path, ShareProvider::packageStructure());
80
if (m_package->isValid()) {
81
const QString mainscript =
82
m_package->path() + m_package->structure()->contentsPrefixPaths().at(0) +
83
m_package->structure()->path("mainscript");
85
if (!QFile::exists(mainscript)) {
86
showError(i18n("Selected provider does not have a valid script file"));
90
const QString interpreter =
91
Kross::Manager::self().interpreternameForFile(mainscript);
93
if (interpreter.isEmpty()) {
94
showError(i18n("Selected provider does not provide a supported script file"));
98
m_action = new Kross::Action(parent(), pluginName);
100
m_provider = new ShareProvider(this);
101
connect(m_provider, SIGNAL(readyToPublish()), this, SLOT(publish()));
102
connect(m_provider, SIGNAL(finished(const QString&)),
103
this, SLOT(showResult(const QString&)));
104
connect(m_provider, SIGNAL(finishedError(const QString&)),
105
this, SLOT(showError(const QString&)));
107
// automatically connects signals and slots with the script
108
m_action->addObject(m_provider, "provider",
109
Kross::ChildrenInterface::AutoConnectSignals);
111
// set the main script file and load it
112
m_action->setFile(mainscript);
115
// check for any errors
116
if(m_action->hadError()) {
117
showError(i18n("Error trying to execute script"));
121
// do the work together with the loaded plugin
122
const QStringList functions = m_action->functionNames();
123
if (!functions.contains("url") || !functions.contains("contentKey") ||
124
!functions.contains("setup")) {
125
showError(i18n("Could not find all required functions"));
129
// call the methods from the plugin
131
m_action->callFunction("url", QVariantList()).toString();
132
m_provider->setUrl(url);
134
// setup the method (get/post)
136
if (functions.contains("method")) {
138
m_action->callFunction("method", QVariantList()).toString();
141
// default is POST (if the plugin does not specify one method)
142
const QString method = vmethod.isValid() ? vmethod.toString() : "POST";
143
m_provider->setMethod(method);
145
// setup the provider
146
QVariant setup = m_action->callFunction("setup", QVariantList());
148
// get the content from the parameters, set the url and add the file
149
// then we can wait the signal to publish the information
150
const QString contentKey =
151
m_action->callFunction("contentKey", QVariantList()).toString();
153
const QString content(parameters()["content"].toString());
154
m_provider->addPostFile(contentKey, content);
159
void ShareJob::publish()
161
m_provider->publish();
164
void ShareJob::showResult(const QString &url)
169
void ShareJob::showError(const QString &message)
171
QString errorMsg = message;
172
if (errorMsg.isEmpty()) {
173
errorMsg = i18n("Unknown Error");
177
setErrorText(message);
181
#include "shareservice.moc"