~ubuntu-branches/ubuntu/saucy/plasma-mobile/saucy

« back to all changes in this revision

Viewing changes to contourd/recommendation/RecommendationScriptEngine.cpp

  • Committer: Package Import Robot
  • Author(s): Jonathan Riddell
  • Date: 2013-09-17 14:08:58 UTC
  • mfrom: (1.1.8)
  • Revision ID: package-import@ubuntu.com-20130917140858-wv7n3z6t95iy1fs9
Tags: 1:0.5-0ubuntu1
* New upstream release LP: #1227602
* Use epoch to sync with upstream version number
* Build-dep on libxcursor-dev
* Add kubuntu_no_contourd.diff to disable contourd from building,
  does not compile with 4.11 and not used by default
* Depend on renamed package ksplash-theme-active
* Add kubuntu_no_dirmodel.diff to prevert compile of dirmodel,
  included in kde-runtime 4.11

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 *   Copyright (C) 2011 Ivan Cukic <ivan.cukic(at)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 version 2,
 
6
 *   or (at your option) any later version, as published by the Free
 
7
 *   Software Foundation
 
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
 
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 "RecommendationScriptEngine.h"
 
21
#include "RecommendationScriptEngine_p.h"
 
22
 
 
23
#include <QScriptEngine>
 
24
#include <QScriptValue>
 
25
#include <QTextStream>
 
26
#include <QFile>
 
27
#include <QTimer>
 
28
#include <QDesktopServices>
 
29
#include <QUrl>
 
30
 
 
31
// #include <QtSensors/QSensor>
 
32
 
 
33
#include <KDebug>
 
34
#include <KStandardDirs>
 
35
 
 
36
#include "sensors/dbus/DBusSensor.h"
 
37
 
 
38
namespace Contour {
 
39
 
 
40
RecommendationScriptEngineConfig::RecommendationScriptEngineConfig(QObject * parent, KConfigGroup * config)
 
41
    : QObject(parent), m_config(config)
 
42
{
 
43
}
 
44
 
 
45
RecommendationScriptEngineConfig::~RecommendationScriptEngineConfig()
 
46
{
 
47
    delete m_config;
 
48
}
 
49
 
 
50
#define CREATE_CONFIG_METHODS(TYPE, TYPECASE) \
 
51
    TYPE RecommendationScriptEngineConfig::TYPECASE##Value(const QString & field, TYPE defaultValue) const \
 
52
    { return m_config->readEntry(field, defaultValue); } \
 
53
    void RecommendationScriptEngineConfig::Set##TYPECASE##Value(const QString & field, TYPE newValue) const \
 
54
    { kDebug() << field << newValue; m_config->writeEntry(field, newValue); m_config->sync(); }
 
55
 
 
56
    CREATE_CONFIG_METHODS(bool, Bool)
 
57
    CREATE_CONFIG_METHODS(int, Int)
 
58
    CREATE_CONFIG_METHODS(QString, String)
 
59
    CREATE_CONFIG_METHODS(QStringList, StringList)
 
60
#undef CREATE_CONFIG_METHODS
 
61
 
 
62
 
 
63
/**
 
64
 *
 
65
 */
 
66
RecommendationScriptEngine::RecommendationScriptEngine(QObject * parent, const QString & script)
 
67
    : RecommendationEngine(parent), d(new Private())
 
68
{
 
69
    d->script = script;
 
70
 
 
71
    kDebug() << "RecommendationScriptEngine()" << script << name();
 
72
 
 
73
    d->delay.setInterval(300);
 
74
    d->delay.setSingleShot(true);
 
75
 
 
76
    d->autoremove = config()->readEntry("autoRemoveChosenRecommendation", true);
 
77
 
 
78
    connect(&(d->delay), SIGNAL(timeout()),
 
79
            this, SLOT(sendUpdateNotification()));
 
80
 
 
81
}
 
82
 
 
83
RecommendationScriptEngine::~RecommendationScriptEngine()
 
84
{
 
85
    delete d;
 
86
}
 
87
 
 
88
void RecommendationScriptEngine::init()
 
89
{
 
90
    Contour::RecommendationEngine::init();
 
91
 
 
92
    QFile file(KStandardDirs::locate("data", "contour/scripts/" + d->script + "/main.js"));
 
93
    if (!file.open(QIODevice::ReadOnly)) {
 
94
        kDebug() << "Failed to open main.js for" << d->script;
 
95
        return;
 
96
    }
 
97
 
 
98
    d->engine = new QScriptEngine(this);
 
99
    connect(d->engine, SIGNAL(signalHandlerException(QScriptValue)),
 
100
            this, SLOT(signalHandlerException(QScriptValue)));
 
101
 
 
102
    d->engine->globalObject().setProperty("self",
 
103
            d->engine->newQObject(this));
 
104
 
 
105
    const QScriptValue & result = d->engine->evaluate(QTextStream(&file).readAll());
 
106
    if (d->engine->hasUncaughtException()) {
 
107
        int line = d->engine->uncaughtExceptionLineNumber();
 
108
        kDebug() << "uncaught exception at line" << line << ":" << result.toString();
 
109
    }
 
110
 
 
111
}
 
112
 
 
113
void RecommendationScriptEngine::activate(const QString & id, const QString & action)
 
114
{
 
115
    if (d->autoremove)
 
116
        removeRecommendation(id);
 
117
 
 
118
    emit activationRequested(id, action);
 
119
}
 
120
 
 
121
void RecommendationScriptEngine::setAutoRemoveChosenRecommendation(bool remove)
 
122
{
 
123
    d->autoremove = remove;
 
124
    KConfigGroup * cg = config();
 
125
    cg->writeEntry("autoRemoveChosenRecommendation", d->autoremove);
 
126
    cg->sync();
 
127
}
 
128
 
 
129
QString RecommendationScriptEngine::name() const
 
130
{
 
131
    kDebug() << d->script;
 
132
    return d->script;
 
133
}
 
134
 
 
135
QScriptValue RecommendationScriptEngine::getSensor(const QString & sensor)
 
136
{
 
137
    kDebug() << "sensor" << sensor;
 
138
 
 
139
    QObject * result = NULL;
 
140
 
 
141
    if (sensor == "DBus") {
 
142
        kDebug() << "Returning the D-Bus sensor. This is not in QtMobility";
 
143
        result = new DBusSensor();
 
144
    } else {
 
145
        // result = new QtMobility::QSensor(sensor.toAscii());
 
146
    }
 
147
 
 
148
    return d->engine->newQObject(result, QScriptEngine::AutoOwnership);
 
149
}
 
150
 
 
151
QScriptValue RecommendationScriptEngine::getConfig()
 
152
{
 
153
    return d->engine->newQObject(new RecommendationScriptEngineConfig(this, config()));
 
154
}
 
155
 
 
156
QScriptValue RecommendationScriptEngine::getTimer(int msec)
 
157
{
 
158
    kDebug() << "timer" << msec;
 
159
 
 
160
    QTimer * timer = new QTimer();
 
161
    timer->setSingleShot(false);
 
162
    timer->setInterval(msec);
 
163
    timer->start();
 
164
 
 
165
    return d->engine->newQObject(timer, QScriptEngine::AutoOwnership);
 
166
}
 
167
 
 
168
void RecommendationScriptEngine::openUrl(const QString & url)
 
169
{
 
170
    QDesktopServices::openUrl(QUrl(url));
 
171
}
 
172
 
 
173
void RecommendationScriptEngine::signalHandlerException(const QScriptValue & exception)
 
174
{
 
175
    kDebug() << "exception" << exception.toVariant();
 
176
}
 
177
 
 
178
void RecommendationScriptEngine::addRecommendation(
 
179
        double score,
 
180
        const QString & id,
 
181
        const QString & title,
 
182
        const QString & description,
 
183
        const QString & icon
 
184
    )
 
185
{
 
186
    // kDebug() << d->script << score << id << title;
 
187
 
 
188
    int i = 0;
 
189
 
 
190
    while (d->recommendations.size() > i &&
 
191
           d->recommendations[i].score > score) {
 
192
        ++i;
 
193
    }
 
194
 
 
195
    RecommendationItem ri;
 
196
 
 
197
    ri.score       = score;
 
198
    ri.id          = id;
 
199
    ri.title       = title;
 
200
    ri.description = description;
 
201
    ri.icon        = icon;
 
202
 
 
203
    d->recommendations.insert(i, ri);
 
204
 
 
205
    delayedUpdateNotification();
 
206
}
 
207
 
 
208
void RecommendationScriptEngine::removeRecommendation(const QString & id)
 
209
{
 
210
    QMutableListIterator < RecommendationItem > i(d->recommendations);
 
211
    while (i.hasNext()) {
 
212
        RecommendationItem ri = i.next();
 
213
 
 
214
        if (ri.id == id) {
 
215
            i.remove();
 
216
        }
 
217
    }
 
218
 
 
219
    delayedUpdateNotification();
 
220
}
 
221
 
 
222
void RecommendationScriptEngine::removeRecommendations()
 
223
{
 
224
    d->recommendations.clear();
 
225
 
 
226
    delayedUpdateNotification();
 
227
}
 
228
 
 
229
void RecommendationScriptEngine::delayedUpdateNotification()
 
230
{
 
231
    d->delay.start();
 
232
}
 
233
 
 
234
void RecommendationScriptEngine::sendUpdateNotification()
 
235
{
 
236
    // just in case, although it is a single-shot
 
237
    d->delay.stop();
 
238
 
 
239
    emit recommendationsUpdated(d->recommendations);
 
240
}
 
241
 
 
242
 
 
243
} // namespace Contour
 
244
 
 
245
// class RecommendationScriptEngine
 
246