add more spacing
[personal-kdebase.git] / workspace / plasma / scriptengines / google_gadgets / ggl_applet_script.cpp
blob9fd39c6d7ecb35890498846f6ef59dfa3138b2ec
1 /*
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.
17 #include <sys/time.h>
18 #include <time.h>
20 #include <QtCore/QTimer>
21 #include <QtCore/QDir>
22 #include <QtCore/QMutex>
23 #include <QtCore/QMutexLocker>
24 #include <QtCore/QFileInfo>
25 #include <QtGui/QPainter>
26 #include <QtGui/QColor>
27 #include <QtGui/QGraphicsSceneMouseEvent>
29 #include <Plasma/Applet>
30 #include <Plasma/Package>
32 #include <ggadget/logger.h>
33 #include <ggadget/script_runtime_interface.h>
34 #include <ggadget/qt/qt_view_widget.h>
35 #include <ggadget/qt/qt_view_host.h>
36 #include <ggadget/qt/qt_menu.h>
37 #include <ggadget/qt/utilities.h>
38 #include <ggadget/qt/qt_main_loop.h>
39 #include <ggadget/extension_manager.h>
40 #include <ggadget/script_runtime_manager.h>
41 #include <ggadget/gadget.h>
42 #include <ggadget/gadget_consts.h>
43 #include <ggadget/system_utils.h>
44 #include <ggadget/host_utils.h>
45 #include <ggadget/view_interface.h>
46 #include <ggadget/view.h>
47 #include <ggadget/host_interface.h>
48 #include <ggadget/decorated_view_host.h>
49 #include "plasma_host.h"
50 #include "ggl_extensions.h"
51 #include "ggl_applet_script.h"
53 K_EXPORT_PLASMA_APPLETSCRIPTENGINE(googlegadget, GglAppletScript)
55 class GglAppletScript::Private {
56 public:
57 QString gg_file_;
58 QString options_;
59 QMenu menu_;
60 QStringList errors_;
61 GadgetInfo info;
62 ~Private() {
63 // Must set applet to null so other components could know the applet is
64 // exiting.
65 info.applet = NULL;
66 delete info.host;
67 info.host = NULL;
68 delete info.gadget;
69 info.gadget = NULL;
73 GglAppletScript::GglAppletScript(QObject *parent, const QVariantList &args)
74 : Plasma::AppletScript(parent), d(new Private) {
75 Q_UNUSED(args);
76 d->info.script = this;
79 GglAppletScript::~GglAppletScript() {
80 kWarning() << "GGL applet script destroied";
81 delete d;
84 bool GglAppletScript::init() {
85 Q_ASSERT(applet());
86 Q_ASSERT(package());
88 std::string profile_dir =
89 ggadget::BuildFilePath(ggadget::GetHomeDirectory().c_str(),
90 ".google/gadgets-plasma", NULL);
92 QString error;
93 if (!ggadget::qt::InitGGL(NULL, "ggl-plasma", profile_dir.c_str(),
94 kGlobalExtensions, 0,
95 ggadget::qt::GGL_INIT_FLAG_COLLECTOR, &error)) {
96 kError() << "Failed to init GGL system:" << error;
97 return false;
100 QFile config_file(package()->path() + "/config.txt");
101 if (!config_file.open(QIODevice::ReadOnly)) {
102 kError() << "Failed to open google gadget's config file at "
103 << package()->path();
104 return false;
106 QTextStream in(&config_file);
107 d->gg_file_ = in.readLine();
108 d->options_ = in.readLine();
109 if (d->options_.isNull() || d->options_.isEmpty())
110 return false;
112 applet()->setAspectRatioMode(Plasma::ConstrainedSquare);
113 QTimer::singleShot(50, this, SLOT(loadGadget()));
114 return true;
117 void GglAppletScript::loadGadget() {
118 d->errors_.clear();
119 kDebug() << "Loading gadget " << d->gg_file_
120 << "with options " << d->options_;
122 d->info.location = applet()->location();
123 d->info.applet = applet();
124 d->info.host = new ggadget::PlasmaHost(&d->info);
125 d->info.gadget = d->info.host->LoadGadget(d->gg_file_.toUtf8(),
126 d->options_.toUtf8(),
127 0, false);
130 void GglAppletScript::paintInterface(QPainter *p,
131 const QStyleOptionGraphicsItem *option,
132 const QRect &contentsRect) {
133 #if 0
134 QRect r = contentsRect;
135 p->setPen(QColor(0, 0, 255));
136 p->drawLine(r.left(), r.top(), r.right(), r.bottom());
137 p->drawLine(r.left(), r.bottom(), r.right(), r.top());
138 p->drawRect(r);
139 #endif
142 void GglAppletScript::mousePressEvent(QGraphicsSceneMouseEvent *event) {
143 // FIXME: AppletScript has no way to handle mousePressEvent right now
144 if (event->button() == Qt::RightButton) {
145 kDebug() << "Right button pressed";
146 d->menu_.clear();
147 ggadget::qt::QtMenu qt_menu(&d->menu_);
148 ggadget::ViewInterface *view = d->info.main_view_host->GetViewDecorator();
149 if (!view->OnAddContextMenuItems(&qt_menu)) {
150 if (!d->menu_.isEmpty()) {
151 kDebug() << "Show my own menu";
152 d->menu_.exec(event->screenPos());
153 event->accept();
159 QList<QAction*> GglAppletScript::contextualActions() {
160 d->menu_.clear();
161 if (d->info.main_view_host) {
162 ggadget::ViewInterface *view = d->info.main_view_host->GetViewDecorator();
163 if (view) {
164 ggadget::qt::QtMenu qt_menu(&d->menu_);
165 view->OnAddContextMenuItems(&qt_menu);
168 return d->menu_.actions();
171 void GglAppletScript::constraintsEvent(Plasma::Constraints constraints) {
172 if (d->info.host)
173 d->info.host->onConstraintsEvent(constraints);
176 void GglAppletScript::showConfigurationInterface() {
177 if (d->info.gadget)
178 d->info.gadget->ShowOptionsDialog();
181 #include "ggl_applet_script.moc"