• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdelibs API Reference
  • KDE Home
  • Contact Us
 

Kross

  • sources
  • kde-4.12
  • kdelibs
  • kross
  • core
action.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  * action.cpp
3  * This file is part of the KDE project
4  * copyright (C)2004-2006 by Sebastian Sauer (mail@dipe.org)
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU Library General Public
8  * License as published by the Free Software Foundation; either
9  * version 2 of the License, or (at your option) any later version.
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Library General Public License for more details.
14  * You should have received a copy of the GNU Library General Public License
15  * along with this program; see the file COPYING. If not, write to
16  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17  * Boston, MA 02110-1301, USA.
18  ***************************************************************************/
19 
20 #include "action.h"
21 #include "actioncollection.h"
22 #include "interpreter.h"
23 #include "script.h"
24 #include "manager.h"
25 #include "wrapperinterface.h"
26 
27 #include <QtCore/QFile>
28 #include <QtCore/QFileInfo>
29 
30 #include <klocale.h>
31 #include <kicon.h>
32 #include <kmimetype.h>
33 
34 using namespace Kross;
35 
36 namespace Kross {
37 
39  class Action::Private
40  {
41  public:
42 
48  Script* script;
49 
54  int version;
55 
60  QString description;
61 
65  QString iconname;
66 
70  QByteArray code;
71 
76  QString interpretername;
77 
84  QString scriptfile;
85 
91  QStringList searchpath;
92 
97  QMap< QString, QVariant > options;
98 
99  Private() : script(0), version(0) {}
100  };
101 
102 }
103 
104 enum InitOptions{Enable=1};
105 void static init(Action* th, const QString& name, int options=0)
106 {
107  th->setEnabled(options&Enable);
108  th->setObjectName(name);
109  #ifdef KROSS_ACTION_DEBUG
110  krossdebug( QString("Action::Action(QObject*,QString,QDir) Ctor name='%1'").arg(th->objectName()) );
111  #endif
112  QObject::connect(th, SIGNAL(triggered(bool)), th, SLOT(slotTriggered()));
113 }
114 
115 Action::Action(QObject* parent, const QString& name, const QDir& packagepath)
116  : QAction(parent)
117  , QScriptable()
118  , ChildrenInterface()
119  , ErrorInterface()
120  , d( new Private() )
121 {
122  init(this,name);
123  d->searchpath=QStringList(packagepath.absolutePath());
124 }
125 
126 Action::Action(QObject* parent, const QUrl& url)
127  : QAction(parent)
128  , ChildrenInterface()
129  , ErrorInterface()
130  , d( new Private() )
131 {
132  init(this,url.path(),Enable);
133  QFileInfo fi( url.toLocalFile() );
134  setText( fi.fileName() );
135  setIconName( KMimeType::iconNameForUrl(url) );
136  setFile( url.toLocalFile() );
137 }
138 
139 Action::~Action()
140 {
141  #ifdef KROSS_ACTION_DEBUG
142  krossdebug( QString("Action::~Action() Dtor name='%1'").arg(objectName()) );
143  #endif
144  finalize();
145  ActionCollection *coll = qobject_cast<ActionCollection*>(parent());
146  if ( coll ) {
147  coll->removeAction(this);
148  }
149  delete d;
150 }
151 
152 
153 void Action::fromDomElement(const QDomElement& element)
154 {
155  fromDomElement(element, d->searchpath);
156 }
157 
158 void Action::fromDomElement(const QDomElement& element, const QStringList& searchPath)
159 {
160  if( element.isNull() )
161  return;
162 
163  QString file = element.attribute("file");
164  if( ! file.isEmpty() ) {
165  if( QFileInfo(file).exists() ) {
166  setFile(file);
167  }
168  else {
169  foreach (const QString& packagepath, searchPath) {
170  QFileInfo fi(QDir(packagepath), file);
171  if( fi.exists() ) {
172  setFile( fi.absoluteFilePath() );
173  break;
174  }
175  }
176  }
177  }
178 
179  d->version = QVariant( element.attribute("version",QString(d->version)) ).toInt();
180 
181  setText( i18n( element.attribute("text").toUtf8() ) );
182  setDescription( i18n( element.attribute("comment").toUtf8() ) );
183  setEnabled( true );
184  setInterpreter( element.attribute("interpreter") );
185  setEnabled( QVariant(element.attribute("enabled","true")).toBool() && isEnabled() );
186 
187  QString icon = element.attribute("icon");
188  if( icon.isEmpty() && ! d->scriptfile.isNull() )
189  icon = KMimeType::iconNameForUrl( KUrl(d->scriptfile) );
190  setIconName( icon );
191 
192  const QString code = element.attribute("code");
193  if( ! code.isNull() )
194  setCode(code.toUtf8());
195 
196  for(QDomNode node = element.firstChild(); ! node.isNull(); node = node.nextSibling()) {
197  QDomElement e = node.toElement();
198  if( ! e.isNull() ) {
199  if( e.tagName() == "property" ) {
200  const QString n = e.attribute("name", QString());
201  if( ! n.isNull() ) {
202  #ifdef KROSS_ACTION_DEBUG
203  krossdebug(QString("Action::readDomElement: Setting property name=%1 value=%2").arg(n).arg(e.text()));
204  #endif
205  setProperty(n.toLatin1().constData(), QVariant(e.text()));
206  }
207  }
208  }
209  }
210 }
211 
212 QDomElement Action::toDomElement() const
213 {
214  return toDomElement(QStringList());
215 }
216 
217 QDomElement Action::toDomElement(const QStringList& searchPath) const
218 {
219  QDomDocument doc;
220  QDomElement e = doc.createElement("script");
221  e.setAttribute("name", objectName());
222  if( d->version > 0 )
223  e.setAttribute("version", QString(d->version));
224  if( ! text().isNull() )
225  e.setAttribute("text", text());
226  if( ! description().isNull() )
227  e.setAttribute("comment", description());
228  if( ! iconName().isNull() )
229  e.setAttribute("icon", iconName());
230  if( ! isEnabled() )
231  e.setAttribute("enabled", "false");
232  if( ! interpreter().isNull() )
233  e.setAttribute("interpreter", interpreter());
234 
235 
236  QString fileName=file();
237  if (!searchPath.isEmpty()) {
238  //fileName=QDir(searchPath.first()).relativeFilePath(fileName); //prefer absname if it is short?
239  foreach(const QString& packagepath, searchPath) {
240  QString nfn=QDir(packagepath).relativeFilePath(file());
241  if (nfn.length()<fileName.length())
242  fileName=nfn;
243  }
244  }
245 
246  if( ! fileName.isNull() ) {
247  e.setAttribute("file", fileName);
248  }
249 
250  QList<QByteArray> props=dynamicPropertyNames();
251  foreach(const QByteArray& prop, props) {
252  QDomElement p = doc.createElement("property");
253  p.setAttribute("name", QString::fromLatin1(prop));
254  p.appendChild(doc.createTextNode(property(prop.constData()).toString()));
255  e.appendChild(p);
256  }
257  /*
258  else if( ! code().isNull() ) {
259  e.setAttribute("code", code());
260  }
261  */
262 
263  return e;
264 }
265 
266 Kross::Script* Action::script() const
267 {
268  return d->script;
269 }
270 
271 QString Action::name() const
272 {
273  return objectName();
274 }
275 
276 int Action::version() const
277 {
278  return d->version;
279 }
280 
281 QString Action::description() const
282 {
283  return d->description;
284 }
285 
286 void Action::setDescription(const QString& description)
287 {
288  d->description = description;
289  emit dataChanged(this);
290  emit updated();
291 }
292 
293 QString Action::iconName() const
294 {
295  return d->iconname;
296 }
297 
298 void Action::setIconName(const QString& iconname)
299 {
300  setIcon( KIcon(iconname) );
301  d->iconname = iconname;
302  emit dataChanged(this);
303  emit updated();
304 }
305 
306 bool Action::isEnabled() const
307 {
308  return QAction::isEnabled();
309 }
310 
311 void Action::setEnabled(bool enabled)
312 {
313  QAction::setEnabled(enabled);
314  emit dataChanged(this);
315  emit updated();
316 }
317 
318 QByteArray Action::code() const
319 {
320  return d->code;
321 }
322 
323 void Action::setCode(const QByteArray& code)
324 {
325  if( d->code != code ) {
326  finalize();
327  d->code = code;
328  emit dataChanged(this);
329  emit updated();
330  }
331 }
332 
333 QString Action::interpreter() const
334 {
335  return d->interpretername;
336 }
337 
338 void Action::setInterpreter(const QString& interpretername)
339 {
340  if( d->interpretername != interpretername ) {
341  finalize();
342  d->interpretername = interpretername;
343  setEnabled( Manager::self().interpreters().contains(interpretername) );
344  if (!isEnabled())
345  krosswarning("Action::setInterpreter: interpreter not found: "+interpretername);
346  emit dataChanged(this);
347  emit updated();
348  }
349 }
350 
351 QString Action::file() const
352 {
353  return d->scriptfile;
354 }
355 
356 bool Action::setFile(const QString& scriptfile)
357 {
358  if( d->scriptfile != scriptfile ) {
359  finalize();
360  if ( scriptfile.isNull() ) {
361  if( ! d->scriptfile.isNull() )
362  d->interpretername.clear();
363  d->scriptfile.clear();
364  d->searchpath.clear();
365  }
366  else {
367  d->scriptfile = scriptfile;
368  d->interpretername = Manager::self().interpreternameForFile(scriptfile);
369  if( d->interpretername.isNull() )
370  return false;
371  }
372  }
373  return true;
374 }
375 
376 QString Action::currentPath() const
377 {
378  return file().isEmpty()?QString():QFileInfo(file()).absolutePath();//obey Qt docs and don't cheat
379 }
380 
381 QVariantMap Action::options() const
382 {
383  return d->options;
384 }
385 
386 void Action::addQObject(QObject* obj, const QString &name)
387 {
388  this->addObject(obj, name);
389 }
390 
391 QObject* Action::qobject(const QString &name) const
392 {
393  return this->object(name);
394 }
395 
396 QStringList Action::qobjectNames() const
397 {
398  return this->objects().keys();
399 }
400 
401 QVariant Action::option(const QString& name, const QVariant& defaultvalue)
402 {
403  if(d->options.contains(name))
404  return d->options[name];
405  InterpreterInfo* info = Manager::self().interpreterInfo( d->interpretername );
406  return info ? info->optionValue(name, defaultvalue) : defaultvalue;
407 }
408 
409 bool Action::setOption(const QString& name, const QVariant& value)
410 {
411  InterpreterInfo* info = Manager::self().interpreterInfo( d->interpretername );
412  if(info) {
413  if(info->hasOption(name)) {
414  d->options.insert(name, value);
415  return true;
416  } else krosswarning( QString("Kross::Action::setOption(%1, %2): No such option").arg(name).arg(value.toString()) );
417  } else krosswarning( QString("Kross::Action::setOption(%1, %2): No such interpreterinfo").arg(name).arg(value.toString()) );
418  return false;
419 }
420 
421 QStringList Action::functionNames()
422 {
423  if(! d->script) {
424  if(! initialize())
425  return QStringList();
426  }
427  return d->script->functionNames();
428 }
429 
430 QVariant Action::callFunction(const QString& name, const QVariantList& args)
431 {
432  if(! d->script) {
433  if(! initialize())
434  return QVariant();
435  }
436  return d->script->callFunction(name, args);
437 }
438 
439 QVariant Action::evaluate(const QByteArray& code)
440 {
441  if(! d->script) {
442  if(! initialize())
443  return QVariant();
444  }
445  return d->script->evaluate(code);
446 }
447 
448 bool Action::initialize()
449 {
450  finalize();
451 
452  if( ! d->scriptfile.isNull() ) {
453  QFile f( d->scriptfile );
454  if( ! f.exists() ) {
455  setError(i18n("Scriptfile \"%1\" does not exist.", d->scriptfile));
456  return false;
457  }
458  if( d->interpretername.isNull() ) {
459  setError(i18n("Failed to determine interpreter for scriptfile \"%1\"", d->scriptfile));
460  return false;
461  }
462  if( ! f.open(QIODevice::ReadOnly) ) {
463  setError(i18n("Failed to open scriptfile \"%1\"", d->scriptfile));
464  return false;
465  }
466  d->code = f.readAll();
467  f.close();
468  }
469 
470  Interpreter* interpreter = Manager::self().interpreter(d->interpretername);
471  if( ! interpreter ) {
472  InterpreterInfo* info = Manager::self().interpreterInfo(d->interpretername);
473  if( info )
474  setError(i18n("Failed to load interpreter \"%1\"", d->interpretername));
475  else
476  setError(i18n("No such interpreter \"%1\"", d->interpretername));
477  return false;
478  }
479 
480  d->script = interpreter->createScript(this);
481  if( ! d->script ) {
482  setError(i18n("Failed to create script for interpreter \"%1\"", d->interpretername));
483  return false;
484  }
485 
486  if( d->script->hadError() ) {
487  setError(d->script);
488  finalize();
489  return false;
490  }
491 
492  clearError(); // clear old exception
493  return true;
494 }
495 
496 void Action::finalize()
497 {
498  if( d->script )
499  emit finalized(this);
500  delete d->script;
501  d->script = 0;
502 }
503 
504 bool Action::isFinalized() const
505 {
506  return d->script == 0;
507 }
508 
509 void Action::slotTriggered()
510 {
511  #ifdef KROSS_ACTION_DEBUG
512  krossdebug( QString("Action::slotTriggered() name=%1").arg(objectName()) );
513  #endif
514 
515  emit started(this);
516 
517  if( ! d->script ) {
518  if( ! initialize() )
519  Q_ASSERT( hadError() );
520  }
521 
522  if( hadError() ) {
523  #ifdef KROSS_ACTION_DEBUG
524  krossdebug( QString("Action::slotTriggered() on init, errorMessage=%2").arg(errorMessage()) );
525  #endif
526  }
527  else {
528  d->script->execute();
529  if( d->script->hadError() ) {
530  #ifdef KROSS_ACTION_DEBUG
531  krossdebug( QString("Action::slotTriggered() after exec, errorMessage=%2").arg(errorMessage()) );
532  #endif
533  setError(d->script);
534  //emit finished(this);
535  finalize();
536  //return;
537  }
538  }
539 
540  emit finished(this);
541 }
542 
543 // --------
544 
545 // interface files
546 WrapperInterface::~WrapperInterface()
547 {
548 }
549 
550 #include "action.moc"
QVariant
Kross::Action::initialize
bool initialize()
Initialize the Script instance.
Definition: action.cpp:448
i18n
QString i18n(const char *text)
Kross::ErrorInterface::hadError
bool hadError() const
Definition: errorinterface.h:48
Kross::Action::setEnabled
void setEnabled(bool enabled)
Set the enable state of this Action to enabled .
Definition: action.cpp:311
Kross::ChildrenInterface::addObject
void addObject(QObject *object, const QString &name=QString(), Options options=NoOption)
Add a QObject to the list of children.
Definition: childreninterface.h:80
Kross::Action::description
QString description() const
Definition: action.cpp:281
Kross::Action::option
QVariant option(const QString &name, const QVariant &defaultvalue=QVariant())
Definition: action.cpp:401
Kross::Script
Base class for interpreter dependent functionality each script provides.
Definition: core/script.h:43
kmimetype.h
Kross::ErrorInterface
Interface for error-handling.
Definition: errorinterface.h:32
Kross::Action::qobject
QObject * qobject(const QString &name) const
Definition: action.cpp:391
Kross::Action::~Action
virtual ~Action()
Destructor.
Definition: action.cpp:139
Kross::Action::started
void started(Kross::Action *)
This signal is emitted before the script got executed.
Kross::Action::toDomElement
QDomElement toDomElement() const
Definition: action.cpp:212
Kross::Action::iconName
QString iconName() const
Return the name of the icon.
Definition: action.cpp:293
Kross::Action::file
QString file() const
Definition: action.cpp:351
Kross::Action::updated
void updated()
This signal is emitted if the content of the Action was changed.
QUrl
Kross::Action::callFunction
QVariant callFunction(const QString &name, const QVariantList &args=QVariantList())
Call a function in the script.
Definition: action.cpp:430
Kross::Manager::interpreterInfo
InterpreterInfo * interpreterInfo(const QString &interpretername) const
Definition: manager.cpp:250
QString
Kross::Action::Action
Action(QObject *parent, const QString &name, const QDir &packagepath=QDir())
Constructor.
Definition: action.cpp:115
Kross::Action::interpreter
QString interpreter() const
Definition: action.cpp:333
QObject
klocale.h
Kross::Action::name
QString name() const
Definition: action.cpp:271
KUrl
Kross::InterpreterInfo
The InterpreterInfo class provides abstract information about a Interpreter before the interpreter-ba...
Definition: core/interpreter.h:43
Kross::InterpreterInfo::hasOption
bool hasOption(const QString &name) const
Definition: core/interpreter.cpp:91
interpreter.h
QScriptable
Kross::ErrorInterface::errorMessage
const QString errorMessage() const
Definition: errorinterface.h:53
Kross::Action::setDescription
void setDescription(const QString &description)
Set the optional description for this Action.
Definition: action.cpp:286
init
static void init(Action *th, const QString &name, int options=0)
Definition: action.cpp:105
Kross::Action::dataChanged
void dataChanged(Action *)
This signal is emitted when the data of the Action is changed.
Kross::ChildrenInterface::objects
QHash< QString, QObject * > objects() const
Definition: childreninterface.h:104
Kross::Interpreter::createScript
virtual Script * createScript(Action *Action)=0
Create and return a new interpreter dependent Script instance.
script.h
action.h
Kross::ErrorInterface::clearError
void clearError()
Clear the error.
Definition: errorinterface.h:88
Kross::Action::script
Script * script() const
Definition: action.cpp:266
QStringList
Kross::ChildrenInterface
Interface for managing Object collections.
Definition: childreninterface.h:38
wrapperinterface.h
Kross::Action::code
QByteArray code() const
Definition: action.cpp:318
KIcon
Kross::Manager::interpreternameForFile
const QString interpreternameForFile(const QString &file)
Return the name of the Interpreter that feels responsible for the defined file .
Definition: manager.cpp:255
Kross::InterpreterInfo::optionValue
const QVariant optionValue(const QString &name, const QVariant &defaultvalue=QVariant()) const
Definition: core/interpreter.cpp:106
Kross::Action::options
QVariantMap options() const
Definition: action.cpp:381
Kross::Action::finalized
void finalized(Kross::Action *)
This signal is emitted once a script finalized.
Kross::Action::fromDomElement
void fromDomElement(const QDomElement &element)
Method to read settings from the QDomElement element that contains details about e.g.
Definition: action.cpp:153
manager.h
Kross::Interpreter
Base class for interpreter implementations.
Definition: core/interpreter.h:177
Kross::krosswarning
void krosswarning(const QString &s)
Warning function.
Definition: krossconfig.cpp:34
Kross::Manager::interpreter
Interpreter * interpreter(const QString &interpretername) const
Return the Interpreter instance defined by the interpretername.
Definition: manager.cpp:271
Kross::ChildrenInterface::object
QObject * object(const QString &name) const
Definition: childreninterface.h:97
Kross::Action::evaluate
QVariant evaluate(const QByteArray &code)
Evaluate some scripting code.
Definition: action.cpp:439
Kross::Action::finished
void finished(Kross::Action *)
This signal is emitted after the script got executed.
Kross::Action::setFile
bool setFile(const QString &scriptfile)
Set the script file that should be executed.
Definition: action.cpp:356
Kross::Manager::self
static Manager & self()
Return the Manager instance.
Definition: manager.cpp:73
Kross::Action::qobjectNames
QStringList qobjectNames() const
Definition: action.cpp:396
Kross::ErrorInterface::setError
void setError(const QString &errormessage, const QString &tracemessage=QString(), long lineno=-1)
Set the error message.
Definition: errorinterface.h:69
Kross::Action::finalize
void finalize()
Finalize the Script instance and frees any cached or still running executions.
Definition: action.cpp:496
Kross::Action::setInterpreter
void setInterpreter(const QString &interpretername)
Set the name of the interpreter (javascript, python or ruby).
Definition: action.cpp:338
Kross::Action::isEnabled
bool isEnabled() const
Return true if this Action is enabled else false is returned.
Definition: action.cpp:306
Kross::Action::setCode
void setCode(const QByteArray &code)
Set the scriptcode code this Action should execute.
Definition: action.cpp:323
InitOptions
InitOptions
Definition: action.cpp:104
Kross::Action::isFinalized
bool isFinalized() const
Definition: action.cpp:504
Kross::Action
The Action class is an abstract container to deal with scripts like a single standalone script file...
Definition: action.h:94
Kross::Action::version
int version() const
Definition: action.cpp:276
Kross::Action::currentPath
QString currentPath() const
Definition: action.cpp:376
Kross::ActionCollection
The ActionCollection class manages collections of Action instances.
Definition: actioncollection.h:45
kicon.h
actioncollection.h
Kross::krossdebug
void krossdebug(const QString &s)
Debugging function.
Definition: krossconfig.cpp:28
Kross::Action::functionNames
QStringList functionNames()
Definition: action.cpp:421
QAction
QMap< QString, QVariant >
Kross::WrapperInterface::~WrapperInterface
virtual ~WrapperInterface()
Destructor.
Definition: action.cpp:546
Kross::Action::addQObject
void addQObject(QObject *obj, const QString &name=QString())
Add a QObject instance to the action.
Definition: action.cpp:386
QList< QByteArray >
Kross::Action::setOption
bool setOption(const QString &name, const QVariant &value)
Set the Interpreter::Option value.
Definition: action.cpp:409
Enable
Definition: action.cpp:104
Kross::Action::setIconName
void setIconName(const QString &iconname)
Set the name of the icon to iconname .
Definition: action.cpp:298
Kross::ActionCollection::removeAction
void removeAction(const QString &name)
Definition: actioncollection.cpp:193
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:49:54 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kross

Skip menu "Kross"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdelibs API Reference

Skip menu "kdelibs API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  • kjsembed
  •   WTF
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Nepomuk-Core
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal