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

parley

  • sources
  • kde-4.14
  • kdeedu
  • parley
  • src
  • scripts
script.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  Copyright 2008 Avgoustinos Kadis <avgoustinos.kadis@kdemail.net>
4 
5  ***************************************************************************/
6 
7 /***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #include "script.h"
16 
17 #include <KDebug>
18 #include <KLocale>
19 
20 #include <kross/core/action.h>
21 
22 
23 Script::Script(QString file)
24 {
25  m_file = file;
26  m_activated = false;
27 }
28 
29 
30 Script::~Script()
31 {
32  delete m_object;
33 }
34 
35 
36 bool Script::isActivated()
37 {
38  return m_activated;
39 }
40 
41 
42 void Script::activate()
43 {
44  kDebug() << "Activating Script" << fileName();
45  if (isActivated()) {
46  kDebug() << "Script already activated";
47  return;
48  }
49  if (!exists()) {
50  kDebug() << "Script file given does not exist";
51  m_errorMessage = i18n("The script file does not exist.");
52  return;
53  }
54 
55  // Create the script container. m_object is the parent QObject,
56  // so that our action instance will be destroyed once the m_object
57  // is destroyed.
58  m_object = new QObject();
59  Kross::Action* action = new Kross::Action(m_object, m_file);
60  // Publish our myobject instance and connect signals with
61  // scripting functions.
62  QMapIterator<QString, QObject*> i(m_scriptObjects);
63  while (i.hasNext()) {
64  i.next();
65  kDebug() << i.key();
66  action->addObject(i.value() , i.key(), Kross::ChildrenInterface::AutoConnectSignals);
67  }
68 
69  // Set the file to be execute
70  action->setFile(m_file);
71 
72  // Execute the script.
73  action->trigger();
74 
75  m_activated = !action->isFinalized();
76  if (!m_activated) {
77  kDebug() << "Script not activated";
78  QString msg = action->errorMessage();
79  QString trace = action->errorTrace();
80  msg.replace('<', "&lt;").replace('\n', "<br/>");
81  trace.replace('<', "&lt;").replace('\n', "<br/>");
82  m_errorMessage = "<p><strong>" + i18n("Error in file %1 at line %2:", fileName(),
83  action->errorLineNo()) + "</strong><br/>" + msg + "<br/><strong>"
84  + i18nc("debug information in error message", "Backtrace:") +
85  "</strong><br/>" + trace + "</p>";
86  }
87 }
88 
89 
90 void Script::deactivate()
91 {
92  delete m_object;
93  m_activated = false;
94 }
95 
96 
97 bool Script::exists()
98 {
99  QFileInfo fileInfo(m_file);
100  return fileInfo.exists();
101 }
102 
103 
104 QString Script::fileName()
105 {
106  return m_file;
107 }
108 
109 
110 void Script::addObject(QString name, QObject * object)
111 {
112  m_scriptObjects.insert(name, object);
113 }
114 
115 
116 void Script::addObjects(QMap<QString, QObject*> objects)
117 {
118  m_scriptObjects.unite(objects);
119 }
120 
121 QString Script::errorMessage()
122 {
123  return m_errorMessage;
124 }
Script::addObjects
void addObjects(QMap< QString, QObject * > objects)
Adds more than one scripting Objects to the script.
Definition: script.cpp:116
QMap::unite
QMap< Key, T > & unite(const QMap< Key, T > &other)
Script::isActivated
bool isActivated()
Returns true if the script was successfully activated; false otherwise.
Definition: script.cpp:36
QMap< QString, QObject * >
script.h
QMapIterator
QMapIterator::next
Item next()
QObject
QString
QMapIterator::key
const Key & key() const
QMapIterator::value
const T & value() const
QFileInfo
QFileInfo::exists
bool exists() const
Script::deactivate
void deactivate()
Deactivate the script.
Definition: script.cpp:90
QString::replace
QString & replace(int position, int n, QChar after)
QObject::QObject
QObject(QObject *parent)
Script::errorMessage
QString errorMessage()
Returns an html error message if there have been errors in the script.
Definition: script.cpp:121
Script::~Script
~Script()
Definition: script.cpp:30
QMap::insert
iterator insert(const Key &key, const T &value)
Script::fileName
QString fileName()
Returns the file that was given as parameter to the constructor.
Definition: script.cpp:104
Script::activate
void activate()
Activates the script.
Definition: script.cpp:42
Script::addObject
void addObject(QString name, QObject *object)
Add an object to be accessible by the script.
Definition: script.cpp:110
QMapIterator::hasNext
bool hasNext() const
Script::exists
bool exists()
Checks if the script file assigned to the Script object exists as a file on the given path...
Definition: script.cpp:97
Script::Script
Script(QString file)
Script class constructor.
Definition: script.cpp:23
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:15:56 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

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

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

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