• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • API Reference
  • Sitemap
  • Contact Us
 

Plasma

ggl_applet_script.cpp

Go to the documentation of this file.
00001 /*
00002   Copyright 2008 Google Inc.
00003 
00004   Licensed under the Apache License, Version 2.0 (the "License");
00005   you may not use this file except in compliance with the License.
00006   You may obtain a copy of the License at
00007 
00008        http://www.apache.org/licenses/LICENSE-2.0
00009 
00010   Unless required by applicable law or agreed to in writing, software
00011   distributed under the License is distributed on an "AS IS" BASIS,
00012   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00013   See the License for the specific language governing permissions and
00014   limitations under the License.
00015 */
00016 
00017 #include <sys/time.h>
00018 #include <time.h>
00019 
00020 #include <QtCore/QTimer>
00021 #include <QtCore/QDir>
00022 #include <QtCore/QMutex>
00023 #include <QtCore/QMutexLocker>
00024 #include <QtCore/QFileInfo>
00025 #include <QtGui/QPainter>
00026 #include <QtGui/QColor>
00027 #include <QtGui/QGraphicsSceneMouseEvent>
00028 
00029 #include <Plasma/Applet>
00030 #include <Plasma/Package>
00031 
00032 #include <ggadget/logger.h>
00033 #include <ggadget/script_runtime_interface.h>
00034 #include <ggadget/qt/qt_view_widget.h>
00035 #include <ggadget/qt/qt_view_host.h>
00036 #include <ggadget/qt/qt_menu.h>
00037 #include <ggadget/qt/utilities.h>
00038 #include <ggadget/qt/qt_main_loop.h>
00039 #include <ggadget/extension_manager.h>
00040 #include <ggadget/script_runtime_manager.h>
00041 #include <ggadget/gadget.h>
00042 #include <ggadget/gadget_consts.h>
00043 #include <ggadget/system_utils.h>
00044 #include <ggadget/host_utils.h>
00045 #include <ggadget/view_interface.h>
00046 #include <ggadget/view.h>
00047 #include <ggadget/host_interface.h>
00048 #include <ggadget/decorated_view_host.h>
00049 #include "plasma_host.h"
00050 #include "ggl_extensions.h"
00051 #include "ggl_applet_script.h"
00052 
00053 K_EXPORT_PLASMA_APPLETSCRIPTENGINE(googlegadget, GglAppletScript)
00054 
00055 class GglAppletScript::Private {
00056  public:
00057   QString gg_file_;
00058   QString options_;
00059   QMenu menu_;
00060   QStringList errors_;
00061   GadgetInfo info;
00062   ~Private() {
00063     // Must set applet to null so other components could know the applet is
00064     // exiting.
00065     info.applet = NULL;
00066     delete info.host;
00067     info.host = NULL;
00068     delete info.gadget;
00069     info.gadget = NULL;
00070   }
00071 };
00072 
00073 GglAppletScript::GglAppletScript(QObject *parent, const QVariantList &args)
00074   : Plasma::AppletScript(parent), d(new Private) {
00075   Q_UNUSED(args);
00076   d->info.script = this;
00077 }
00078 
00079 GglAppletScript::~GglAppletScript() {
00080   kWarning() << "GGL applet script destroied";
00081   delete d;
00082 }
00083 
00084 bool GglAppletScript::init() {
00085   Q_ASSERT(applet());
00086   Q_ASSERT(package());
00087 
00088   std::string profile_dir =
00089           ggadget::BuildFilePath(ggadget::GetHomeDirectory().c_str(),
00090                                  ".google/gadgets-plasma", NULL);
00091 
00092   QString error;
00093   if (!ggadget::qt::InitGGL(NULL, "ggl-plasma", profile_dir.c_str(),
00094                             kGlobalExtensions, 0,
00095                             ggadget::qt::GGL_INIT_FLAG_COLLECTOR, &error)) {
00096     kError() << "Failed to init GGL system:" << error;
00097     return false;
00098   }
00099 
00100   QFile config_file(package()->path() + "/config.txt");
00101   if (!config_file.open(QIODevice::ReadOnly)) {
00102     kError() << "Failed to open google gadget's config file at "
00103              << package()->path();
00104     return false;
00105   }
00106   QTextStream in(&config_file);
00107   d->gg_file_ = in.readLine();
00108   d->options_ = in.readLine();
00109   if (d->options_.isNull() || d->options_.isEmpty())
00110     return false;
00111 
00112   applet()->setAspectRatioMode(Plasma::ConstrainedSquare);
00113   QTimer::singleShot(50, this, SLOT(loadGadget()));
00114   return true;
00115 }
00116 
00117 void GglAppletScript::loadGadget() {
00118   d->errors_.clear();
00119   kDebug() << "Loading gadget " << d->gg_file_
00120            << "with options " << d->options_;
00121 
00122   d->info.location = applet()->location();
00123   d->info.applet = applet();
00124   d->info.host = new ggadget::PlasmaHost(&d->info);
00125   d->info.gadget = d->info.host->LoadGadget(d->gg_file_.toUtf8(),
00126                                             d->options_.toUtf8(),
00127                                             0, false);
00128 }
00129 
00130 void GglAppletScript::paintInterface(QPainter *p,
00131                                      const QStyleOptionGraphicsItem *option,
00132                                      const QRect &contentsRect) {
00133 #if 0
00134   QRect r = contentsRect;
00135   p->setPen(QColor(0, 0, 255));
00136   p->drawLine(r.left(), r.top(), r.right(), r.bottom());
00137   p->drawLine(r.left(), r.bottom(), r.right(), r.top());
00138   p->drawRect(r);
00139 #endif
00140 }
00141 
00142 void GglAppletScript::mousePressEvent(QGraphicsSceneMouseEvent *event) {
00143   // FIXME: AppletScript has no way to handle mousePressEvent right now
00144   if (event->button() == Qt::RightButton) {
00145     kDebug() << "Right button pressed";
00146     d->menu_.clear();
00147     ggadget::qt::QtMenu qt_menu(&d->menu_);
00148     ggadget::ViewInterface *view = d->info.main_view_host->GetViewDecorator();
00149     if (!view->OnAddContextMenuItems(&qt_menu)) {
00150       if (!d->menu_.isEmpty()) {
00151         kDebug() << "Show my own menu";
00152         d->menu_.exec(event->screenPos());
00153         event->accept();
00154       }
00155     }
00156   }
00157 }
00158 
00159 QList<QAction*> GglAppletScript::contextualActions() {
00160   d->menu_.clear();
00161   if (d->info.main_view_host) {
00162     ggadget::ViewInterface *view = d->info.main_view_host->GetViewDecorator();
00163     if (view) {
00164       ggadget::qt::QtMenu qt_menu(&d->menu_);
00165       view->OnAddContextMenuItems(&qt_menu);
00166     }
00167   }
00168   return d->menu_.actions();
00169 }
00170 
00171 void GglAppletScript::constraintsEvent(Plasma::Constraints constraints) {
00172   if (d->info.host)
00173     d->info.host->onConstraintsEvent(constraints);
00174 }
00175 
00176 void GglAppletScript::showConfigurationInterface() {
00177   if (d->info.gadget)
00178     d->info.gadget->ShowOptionsDialog();
00179 }
00180 
00181 #include "ggl_applet_script.moc"

Plasma

Skip menu "Plasma"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members

API Reference

Skip menu "API Reference"
  • KWin
  •   KWin Libraries
  • Libraries
  •   libkworkspace
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal