2
Copyright 2008 Google Inc.
4
Licensed under the Apache License, Version 2.0 (the "License");
5
you may not use this file except in compliance with the License.
6
You may obtain a copy of the License at
8
http://www.apache.org/licenses/LICENSE-2.0
10
Unless required by applicable law or agreed to in writing, software
11
distributed under the License is distributed on an "AS IS" BASIS,
12
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13
See the License for the specific language governing permissions and
14
limitations under the License.
16
#include "ggl_applet_script.h"
21
#include <QtCore/QTimer>
22
#include <QtCore/QDir>
23
#include <QtCore/QMutex>
24
#include <QtCore/QMutexLocker>
25
#include <QtCore/QFileInfo>
26
#include <QtGui/QPainter>
27
#include <QtGui/QColor>
28
#include <QtGui/QGraphicsSceneMouseEvent>
32
#include <Plasma/Applet>
33
#include <Plasma/Package>
35
#include <ggadget/logger.h>
36
#include <ggadget/script_runtime_interface.h>
37
#include <ggadget/qt/qt_view_widget.h>
38
#include <ggadget/qt/qt_view_host.h>
39
#include <ggadget/qt/qt_menu.h>
40
#include <ggadget/qt/utilities.h>
41
#include <ggadget/qt/qt_main_loop.h>
42
#include <ggadget/extension_manager.h>
43
#include <ggadget/script_runtime_manager.h>
44
#include <ggadget/gadget.h>
45
#include <ggadget/gadget_consts.h>
46
#include <ggadget/system_utils.h>
47
#include <ggadget/host_utils.h>
48
#include <ggadget/view_interface.h>
49
#include <ggadget/view.h>
50
#include <ggadget/host_interface.h>
51
#include <ggadget/decorated_view_host.h>
52
#include "plasma_host.h"
53
#include "ggl_extensions.h"
55
K_EXPORT_PLASMA_APPLETSCRIPTENGINE(googlegadget, GglAppletScript)
57
class GglAppletScript::Private {
65
// Must set applet to null so other components could know the applet is
75
GglAppletScript::GglAppletScript(QObject *parent, const QVariantList &args)
76
: Plasma::AppletScript(parent), d(new Private) {
78
d->info.script = this;
81
GglAppletScript::~GglAppletScript() {
82
kWarning() << "GGL applet script destroied";
86
bool GglAppletScript::init() {
90
std::string profile_dir =
91
ggadget::BuildFilePath(ggadget::GetHomeDirectory().c_str(),
92
".google/gadgets-plasma", NULL);
95
if (!ggadget::qt::InitGGL(NULL, "ggl-plasma", profile_dir.c_str(),
97
ggadget::qt::GGL_INIT_FLAG_COLLECTOR, &error)) {
98
kError() << "Failed to init GGL system:" << error;
102
QFile config_file(package()->path() + "/config.txt");
103
if (!config_file.open(QIODevice::ReadOnly)) {
104
kError() << "Failed to open google gadget's config file at "
105
<< package()->path();
108
QTextStream in(&config_file);
109
d->gg_file_ = in.readLine();
110
d->options_ = in.readLine();
111
if (d->options_.isNull() || d->options_.isEmpty())
114
applet()->setAspectRatioMode(Plasma::ConstrainedSquare);
115
QTimer::singleShot(50, this, SLOT(loadGadget()));
119
void GglAppletScript::loadGadget() {
121
kDebug() << "Loading gadget " << d->gg_file_
122
<< "with options " << d->options_;
124
d->info.location = applet()->location();
125
d->info.applet = applet();
126
d->info.host = new ggadget::PlasmaHost(&d->info);
127
d->info.gadget = d->info.host->LoadGadget(d->gg_file_.toUtf8(),
128
d->options_.toUtf8(),
132
void GglAppletScript::paintInterface(QPainter *p,
133
const QStyleOptionGraphicsItem *option,
134
const QRect &contentsRect) {
137
Q_UNUSED(contentsRect);
139
QRect r = contentsRect;
140
p->setPen(QColor(0, 0, 255));
141
p->drawLine(r.left(), r.top(), r.right(), r.bottom());
142
p->drawLine(r.left(), r.bottom(), r.right(), r.top());
147
void GglAppletScript::mousePressEvent(QGraphicsSceneMouseEvent *event) {
148
// FIXME: AppletScript has no way to handle mousePressEvent right now
149
if (event->button() == Qt::RightButton) {
150
kDebug() << "Right button pressed";
152
ggadget::qt::QtMenu qt_menu(&d->menu_);
153
ggadget::ViewInterface *view = d->info.main_view_host->GetViewDecorator();
154
if (!view->OnAddContextMenuItems(&qt_menu)) {
155
if (!d->menu_.isEmpty()) {
156
kDebug() << "Show my own menu";
157
d->menu_.exec(event->screenPos());
164
QList<QAction*> GglAppletScript::contextualActions() {
166
if (d->info.main_view_host) {
167
ggadget::ViewInterface *view = d->info.main_view_host->GetViewDecorator();
169
ggadget::qt::QtMenu qt_menu(&d->menu_);
170
view->OnAddContextMenuItems(&qt_menu);
173
return d->menu_.actions();
176
void GglAppletScript::constraintsEvent(Plasma::Constraints constraints) {
178
d->info.host->onConstraintsEvent(constraints);
181
void GglAppletScript::showConfigurationInterface() {
183
d->info.gadget->ShowOptionsDialog();
186
#include "ggl_applet_script.moc"