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.
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
{
63 // Must set applet to null so other components could know the applet is
73 GglAppletScript::GglAppletScript(QObject
*parent
, const QVariantList
&args
)
74 : Plasma::AppletScript(parent
), d(new Private
) {
76 d
->info
.script
= this;
79 GglAppletScript::~GglAppletScript() {
80 kWarning() << "GGL applet script destroied";
84 bool GglAppletScript::init() {
88 std::string profile_dir
=
89 ggadget::BuildFilePath(ggadget::GetHomeDirectory().c_str(),
90 ".google/gadgets-plasma", NULL
);
93 if (!ggadget::qt::InitGGL(NULL
, "ggl-plasma", profile_dir
.c_str(),
95 ggadget::qt::GGL_INIT_FLAG_COLLECTOR
, &error
)) {
96 kError() << "Failed to init GGL system:" << error
;
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();
106 QTextStream
in(&config_file
);
107 d
->gg_file_
= in
.readLine();
108 d
->options_
= in
.readLine();
109 if (d
->options_
.isNull() || d
->options_
.isEmpty())
112 applet()->setAspectRatioMode(Plasma::ConstrainedSquare
);
113 QTimer::singleShot(50, this, SLOT(loadGadget()));
117 void GglAppletScript::loadGadget() {
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(),
130 void GglAppletScript::paintInterface(QPainter
*p
,
131 const QStyleOptionGraphicsItem
*option
,
132 const QRect
&contentsRect
) {
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());
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";
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());
159 QList
<QAction
*> GglAppletScript::contextualActions() {
161 if (d
->info
.main_view_host
) {
162 ggadget::ViewInterface
*view
= d
->info
.main_view_host
->GetViewDecorator();
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
) {
173 d
->info
.host
->onConstraintsEvent(constraints
);
176 void GglAppletScript::showConfigurationInterface() {
178 d
->info
.gadget
->ShowOptionsDialog();
181 #include "ggl_applet_script.moc"