~ubuntu-branches/ubuntu/utopic/kde-workspace/utopic-proposed

« back to all changes in this revision

Viewing changes to plasma/generic/dataengines/share/shareservice.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Michał Zając
  • Date: 2011-07-09 08:31:15 UTC
  • Revision ID: james.westby@ubuntu.com-20110709083115-ohyxn6z93mily9fc
Tags: upstream-4.6.90
Import upstream version 4.6.90

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/***************************************************************************
 
2
 *   Copyright 2010 Artur Duque de Souza <asouza@kde.org>                  *
 
3
 *                                                                         *
 
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.                                   *
 
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 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
 ***************************************************************************/
 
19
 
 
20
#include <QtCore/QFile>
 
21
 
 
22
#include <kross/core/action.h>
 
23
#include <kross/core/interpreter.h>
 
24
#include <kross/core/manager.h>
 
25
#include <kstandarddirs.h>
 
26
 
 
27
#include <Plasma/Package>
 
28
 
 
29
#include "shareservice.h"
 
30
#include "shareprovider.h"
 
31
 
 
32
 
 
33
ShareService::ShareService(ShareEngine *engine)
 
34
    : Plasma::Service(engine)
 
35
{
 
36
    setName("share");
 
37
}
 
38
 
 
39
Plasma::ServiceJob *ShareService::createJob(const QString &operation,
 
40
                                            QMap<QString, QVariant> &parameters)
 
41
{
 
42
    return new ShareJob(destination(), operation, parameters, this);
 
43
}
 
44
 
 
45
ShareJob::ShareJob(const QString &destination, const QString &operation,
 
46
                   QMap<QString, QVariant> &parameters, QObject *parent)
 
47
    : Plasma::ServiceJob(destination, operation, parameters, parent),
 
48
      m_action(0), m_provider(0), m_package(0)
 
49
{
 
50
}
 
51
 
 
52
ShareJob::~ShareJob()
 
53
{
 
54
    delete m_action;
 
55
    delete m_provider;
 
56
    delete m_package;
 
57
}
 
58
 
 
59
void ShareJob::start()
 
60
{
 
61
    //KService::Ptr service = KService::serviceByStorageId("plasma-share-pastebincom.desktop");
 
62
    KService::Ptr service = KService::serviceByStorageId(destination());
 
63
    if (!service) {
 
64
        showError(i18n("Could not find the provider with the specified destination"));
 
65
        return;
 
66
    }
 
67
 
 
68
    QString pluginName =
 
69
        service->property("X-KDE-PluginInfo-Name", QVariant::String).toString();
 
70
 
 
71
    const QString path =
 
72
        KStandardDirs::locate("data", "plasma/shareprovider/" + pluginName + '/' );
 
73
 
 
74
    if (path.isEmpty()) {
 
75
        showError(i18n("Invalid path for the requested provider"));
 
76
        return;
 
77
    }
 
78
 
 
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");
 
84
 
 
85
        if (!QFile::exists(mainscript)) {
 
86
            showError(i18n("Selected provider does not have a valid script file"));
 
87
            return;
 
88
        }
 
89
 
 
90
        const QString interpreter =
 
91
            Kross::Manager::self().interpreternameForFile(mainscript);
 
92
 
 
93
        if (interpreter.isEmpty()) {
 
94
            showError(i18n("Selected provider does not provide a supported script file"));
 
95
            return;
 
96
        }
 
97
 
 
98
        m_action = new Kross::Action(parent(), pluginName);
 
99
        if (m_action) {
 
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&)));
 
106
 
 
107
            // automatically connects signals and slots with the script
 
108
            m_action->addObject(m_provider, "provider",
 
109
                                Kross::ChildrenInterface::AutoConnectSignals);
 
110
 
 
111
            // set the main script file and load it
 
112
            m_action->setFile(mainscript);
 
113
            m_action->trigger();
 
114
 
 
115
            // check for any errors
 
116
            if(m_action->hadError()) {
 
117
                showError(i18n("Error trying to execute script"));
 
118
                return;
 
119
            }
 
120
 
 
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"));
 
126
                return;
 
127
            }
 
128
 
 
129
            // call the methods from the plugin
 
130
            const QString url =
 
131
                m_action->callFunction("url", QVariantList()).toString();
 
132
            m_provider->setUrl(url);
 
133
 
 
134
            // setup the method (get/post)
 
135
            QVariant vmethod;
 
136
            if (functions.contains("method")) {
 
137
                vmethod =
 
138
                    m_action->callFunction("method", QVariantList()).toString();
 
139
            }
 
140
 
 
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);
 
144
 
 
145
            // setup the provider
 
146
            QVariant setup = m_action->callFunction("setup", QVariantList());
 
147
 
 
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();
 
152
 
 
153
            const QString content(parameters()["content"].toString());
 
154
            m_provider->addPostFile(contentKey, content);
 
155
        }
 
156
    }
 
157
}
 
158
 
 
159
void ShareJob::publish()
 
160
{
 
161
    m_provider->publish();
 
162
}
 
163
 
 
164
void ShareJob::showResult(const QString &url)
 
165
{
 
166
    setResult(url);
 
167
}
 
168
 
 
169
void ShareJob::showError(const QString &message)
 
170
{
 
171
    QString errorMsg = message;
 
172
    if (errorMsg.isEmpty()) {
 
173
        errorMsg = i18n("Unknown Error");
 
174
    }
 
175
 
 
176
    setError(1);
 
177
    setErrorText(message);
 
178
    emitResult();
 
179
}
 
180
 
 
181
#include "shareservice.moc"