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

« back to all changes in this revision

Viewing changes to plasma/generic/scriptengines/google_gadgets/ggl_applet_script.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 2008 Google Inc.
 
3
 
 
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
 
7
 
 
8
       http://www.apache.org/licenses/LICENSE-2.0
 
9
 
 
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.
 
15
*/
 
16
#include "ggl_applet_script.h"
 
17
 
 
18
#include <sys/time.h>
 
19
#include <time.h>
 
20
 
 
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>
 
29
 
 
30
#include <KDebug>
 
31
 
 
32
#include <Plasma/Applet>
 
33
#include <Plasma/Package>
 
34
 
 
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"
 
54
 
 
55
K_EXPORT_PLASMA_APPLETSCRIPTENGINE(googlegadget, GglAppletScript)
 
56
 
 
57
class GglAppletScript::Private {
 
58
 public:
 
59
  QString gg_file_;
 
60
  QString options_;
 
61
  QMenu menu_;
 
62
  QStringList errors_;
 
63
  GadgetInfo info;
 
64
  ~Private() {
 
65
    // Must set applet to null so other components could know the applet is
 
66
    // exiting.
 
67
    info.applet = NULL;
 
68
    delete info.host;
 
69
    info.host = NULL;
 
70
    delete info.gadget;
 
71
    info.gadget = NULL;
 
72
  }
 
73
};
 
74
 
 
75
GglAppletScript::GglAppletScript(QObject *parent, const QVariantList &args)
 
76
  : Plasma::AppletScript(parent), d(new Private) {
 
77
  Q_UNUSED(args);
 
78
  d->info.script = this;
 
79
}
 
80
 
 
81
GglAppletScript::~GglAppletScript() {
 
82
  kWarning() << "GGL applet script destroied";
 
83
  delete d;
 
84
}
 
85
 
 
86
bool GglAppletScript::init() {
 
87
  Q_ASSERT(applet());
 
88
  Q_ASSERT(package());
 
89
 
 
90
  std::string profile_dir =
 
91
          ggadget::BuildFilePath(ggadget::GetHomeDirectory().c_str(),
 
92
                                 ".google/gadgets-plasma", NULL);
 
93
 
 
94
  QString error;
 
95
  if (!ggadget::qt::InitGGL(NULL, "ggl-plasma", profile_dir.c_str(),
 
96
                            kGlobalExtensions, 0,
 
97
                            ggadget::qt::GGL_INIT_FLAG_COLLECTOR, &error)) {
 
98
    kError() << "Failed to init GGL system:" << error;
 
99
    return false;
 
100
  }
 
101
 
 
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();
 
106
    return false;
 
107
  }
 
108
  QTextStream in(&config_file);
 
109
  d->gg_file_ = in.readLine();
 
110
  d->options_ = in.readLine();
 
111
  if (d->options_.isNull() || d->options_.isEmpty())
 
112
    return false;
 
113
 
 
114
  applet()->setAspectRatioMode(Plasma::ConstrainedSquare);
 
115
  QTimer::singleShot(50, this, SLOT(loadGadget()));
 
116
  return true;
 
117
}
 
118
 
 
119
void GglAppletScript::loadGadget() {
 
120
  d->errors_.clear();
 
121
  kDebug() << "Loading gadget " << d->gg_file_
 
122
           << "with options " << d->options_;
 
123
 
 
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(),
 
129
                                            0, false);
 
130
}
 
131
 
 
132
void GglAppletScript::paintInterface(QPainter *p,
 
133
                                     const QStyleOptionGraphicsItem *option,
 
134
                                     const QRect &contentsRect) {
 
135
  Q_UNUSED(p);
 
136
  Q_UNUSED(option);
 
137
  Q_UNUSED(contentsRect);
 
138
#if 0
 
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());
 
143
  p->drawRect(r);
 
144
#endif
 
145
}
 
146
 
 
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";
 
151
    d->menu_.clear();
 
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());
 
158
        event->accept();
 
159
      }
 
160
    }
 
161
  }
 
162
}
 
163
 
 
164
QList<QAction*> GglAppletScript::contextualActions() {
 
165
  d->menu_.clear();
 
166
  if (d->info.main_view_host) {
 
167
    ggadget::ViewInterface *view = d->info.main_view_host->GetViewDecorator();
 
168
    if (view) {
 
169
      ggadget::qt::QtMenu qt_menu(&d->menu_);
 
170
      view->OnAddContextMenuItems(&qt_menu);
 
171
    }
 
172
  }
 
173
  return d->menu_.actions();
 
174
}
 
175
 
 
176
void GglAppletScript::constraintsEvent(Plasma::Constraints constraints) {
 
177
  if (d->info.host)
 
178
    d->info.host->onConstraintsEvent(constraints);
 
179
}
 
180
 
 
181
void GglAppletScript::showConfigurationInterface() {
 
182
  if (d->info.gadget)
 
183
    d->info.gadget->ShowOptionsDialog();
 
184
}
 
185
 
 
186
#include "ggl_applet_script.moc"