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

parley

script.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002 
00003     Copyright 2008 Avgoustinos Kadis <avgoustinos.kadis@kdemail.net>
00004 
00005  ***************************************************************************/
00006 
00007 /***************************************************************************
00008  *                                                                         *
00009  *   This program is free software; you can redistribute it and/or modify  *
00010  *   it under the terms of the GNU General Public License as published by  *
00011  *   the Free Software Foundation; either version 2 of the License, or     *
00012  *   (at your option) any later version.                                   *
00013  *                                                                         *
00014  ***************************************************************************/
00015 #include "script.h"
00016 
00017 #include <KDebug>
00018 #include <KLocale>
00019 
00020 #include <kross/core/action.h>
00021 
00022 
00023 Script::Script ( QString file )
00024 {
00025     m_file = file;
00026     m_activated = false;
00027 }
00028 
00029 
00030 Script::~Script()
00031 {
00032     if (m_object)
00033         delete  m_object;
00034 }
00035 
00036 
00037 bool Script::isActivated()
00038 {
00039     return m_activated;
00040 }
00041 
00042 
00043 void Script::activate()
00044 {
00045     kDebug() << "Activating Script" << fileName();
00046     if ( isActivated() )
00047     {
00048         kDebug() << "Script already activated";
00049         return;
00050     }
00051     if ( !exists() )
00052     {
00053         kDebug() << "Script file given does not exist";
00054         m_errorMessage = i18n("The script file does not exist.");
00055         return;
00056     }
00057 
00058     // Create the script container. m_object is the parent QObject,
00059     // so that our action instance will be destroyed once the m_object
00060     // is destroyed.
00061     m_object = new QObject();
00062     Kross::Action* action = new Kross::Action ( m_object, m_file );
00063     // Publish our myobject instance and connect signals with
00064     // scripting functions.
00065     QMapIterator<QString,QObject*> i ( m_scriptObjects );
00066     while ( i.hasNext() )
00067     {
00068         i.next();
00069         kDebug() << i.key();
00070         action->addObject ( i.value() , i.key(), Kross::ChildrenInterface::AutoConnectSignals );
00071     }
00072 
00073     // Set the file to be execute
00074     action->setFile ( m_file );
00075 
00076     // Execute the script.
00077     action->trigger();
00078 
00079     m_activated = !action->isFinalized();
00080     if (!m_activated) {
00081         kDebug() << "Script not activated";
00082         QString msg = action->errorMessage();
00083         QString trace = action->errorTrace();
00084         msg.replace('<', "&lt;").replace('\n',"<br/>");
00085         trace.replace('<', "&lt;").replace('\n',"<br/>");
00086         m_errorMessage = "<p><strong>"+i18n("Error in file %1 at line %2:", fileName(),
00087             action->errorLineNo())+"</strong><br/>"+msg+"<br/><strong>"
00088             +i18nc("debug information in error message", "Backtrace:")+
00089             "</strong><br/>"+trace+"</p>";
00090     }
00091 }
00092 
00093 
00094 void Script::deactivate()
00095 {
00096     if ( m_object )
00097         delete m_object;
00098     m_activated = false;
00099 }
00100 
00101 
00102 bool Script::exists()
00103 {
00104     QFileInfo fileInfo ( m_file );
00105     return fileInfo.exists();
00106 }
00107 
00108 
00109 QString Script::fileName()
00110 {
00111     return m_file;
00112 }
00113 
00114 
00115 void Script::addObject ( QString name, QObject * object )
00116 {
00117     m_scriptObjects.insert ( name,object );
00118 }
00119 
00120 
00121 void Script::addObjects ( QMap<QString,QObject*> objects )
00122 {
00123     m_scriptObjects.unite ( objects );
00124 }
00125 
00126 QString Script::errorMessage()
00127 {
00128     return m_errorMessage;
00129 }

parley

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

kdeedu

Skip menu "kdeedu"
  •     lib
  • kalzium
  • kanagram
  • kig
  •   lib
  • klettres
  • kstars
  • libkdeedu
  •   keduvocdocument
  • marble
  • parley
  •   stepcore
Generated for kdeedu by doxygen 1.5.9-20090814
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