Plasma
ggl_applet_script.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
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
00064
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
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"