parley
script.cpp
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
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
00059
00060
00061 m_object = new QObject();
00062 Kross::Action* action = new Kross::Action ( m_object, m_file );
00063
00064
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
00074 action->setFile ( m_file );
00075
00076
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('<', "<").replace('\n',"<br/>");
00085 trace.replace('<', "<").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 }