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

Plasma

runnerscriptqscript.cpp

Go to the documentation of this file.
00001 /*
00002  *   Copyright 2008 Richard J. Moore <rich@kde.org>
00003  *
00004  *   This program is free software; you can redistribute it and/or modify
00005  *   it under the terms of the GNU Library General Public License version 2 as
00006  *   published by the Free Software Foundation
00007  *
00008  *   This program is distributed in the hope that it will be useful,
00009  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00010  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011  *   GNU General Public License for more details
00012  *
00013  *   You should have received a copy of the GNU Library General Public
00014  *   License along with this program; if not, write to the
00015  *   Free Software Foundation, Inc.,
00016  *   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00017  */
00018 
00019 #include "runnerscriptqscript.h"
00020 
00021 #include <QScriptEngine>
00022 #include <QFile>
00023 
00024 #include <plasma/abstractrunner.h>
00025 #include <plasma/querymatch.h>
00026 
00027 
00028 typedef const Plasma::RunnerContext* ConstRunnerContextStar;
00029 typedef const Plasma::QueryMatch* ConstSearchMatchStar;
00030 
00031 Q_DECLARE_METATYPE(Plasma::QueryMatch*)
00032 Q_DECLARE_METATYPE(Plasma::RunnerContext*)
00033 Q_DECLARE_METATYPE(ConstRunnerContextStar)
00034 Q_DECLARE_METATYPE(ConstSearchMatchStar)
00035 
00036 RunnerScriptQScript::RunnerScriptQScript(QObject *parent, const QVariantList &args)
00037     : RunnerScript(parent)
00038 {
00039     m_engine = new QScriptEngine( this );
00040     importExtensions();
00041 }
00042 
00043 RunnerScriptQScript::~RunnerScriptQScript()
00044 {
00045 }
00046 
00047 Plasma::AbstractRunner* RunnerScriptQScript::runner() const
00048 {
00049     return RunnerScript::runner();
00050 }
00051 
00052 bool RunnerScriptQScript::init()
00053 {
00054     setupObjects();
00055 
00056     QFile file(mainScript());
00057     if ( !file.open(QIODevice::ReadOnly | QIODevice::Text) ) {
00058         kWarning() << "Unable to load script file";
00059         return false;
00060     }
00061 
00062     QString script = file.readAll();
00063     kDebug() << "Script says" << script;
00064 
00065     m_engine->evaluate( script );
00066     if ( m_engine->hasUncaughtException() ) {
00067         reportError();
00068         return false;
00069     }
00070 
00071     return true;
00072 }
00073 
00074 void RunnerScriptQScript::match(Plasma::RunnerContext *search)
00075 {
00076     QScriptValue fun = m_self.property( "match" );
00077     if ( !fun.isFunction() ) {
00078     kDebug() << "Script: match is not a function, " << fun.toString();
00079     return;
00080     }
00081 
00082     QScriptValueList args;
00083     args << m_engine->toScriptValue(search);
00084 
00085     QScriptContext *ctx = m_engine->pushContext();
00086     ctx->setActivationObject( m_self );
00087     fun.call( m_self, args );
00088     m_engine->popContext();
00089 
00090     if ( m_engine->hasUncaughtException() ) {
00091     reportError();
00092     }
00093 }
00094 
00095 void RunnerScriptQScript::exec(const Plasma::RunnerContext *search, const Plasma::QueryMatch *action)
00096 {
00097     QScriptValue fun = m_self.property( "exec" );
00098     if ( !fun.isFunction() ) {
00099     kDebug() << "Script: exec is not a function, " << fun.toString();
00100     return;
00101     }
00102 
00103     QScriptValueList args;
00104     args << m_engine->toScriptValue(search);
00105     args << m_engine->toScriptValue(action);
00106 
00107     QScriptContext *ctx = m_engine->pushContext();
00108     ctx->setActivationObject( m_self );
00109     fun.call( m_self, args );
00110     m_engine->popContext();
00111 
00112     if ( m_engine->hasUncaughtException() ) {
00113     reportError();
00114     }
00115 }
00116 
00117 void RunnerScriptQScript::setupObjects()
00118 {
00119     QScriptValue global = m_engine->globalObject();
00120 
00121     // Expose an applet
00122     m_self = m_engine->newQObject( this );
00123     m_self.setScope( global );    
00124 
00125     global.setProperty("runner", m_self);
00126 }
00127 
00128 void RunnerScriptQScript::importExtensions()
00129 {
00130     QStringList extensions;
00131     extensions << "qt.core" << "qt.gui" << "qt.svg" << "qt.xml" << "org.kde.plasma";
00132     for (int i = 0; i < extensions.size(); ++i) {
00133         QString ext = extensions.at(i);
00134         kDebug() << "importing " << ext << "...";
00135         QScriptValue ret = m_engine->importExtension(ext);
00136         if (ret.isError())
00137             kDebug() << "failed to import extension" << ext << ":" << ret.toString();
00138     }
00139     kDebug() << "done importing extensions.";
00140 }
00141 
00142 void RunnerScriptQScript::reportError()
00143 {
00144     kDebug() << "Error: " << m_engine->uncaughtException().toString()
00145          << " at line " << m_engine->uncaughtExceptionLineNumber() << endl;
00146     kDebug() << m_engine->uncaughtExceptionBacktrace();
00147 }
00148 
00149 #include "runnerscriptqscript.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
  •   libplasma
  •   libsolidcontrol
  •   libtaskmanager
  • Plasma
  •   Animators
  •   Applets
  •   Engines
  • Solid Modules
Generated for API Reference by doxygen 1.5.4
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