lib

scriptaction.cpp
1/***************************************************************************
2 * scriptaction.cpp
3 * This file is part of the KDE project
4 * copyright (C) 2005 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 "scriptaction.h"
21#include "manager.h"
22
23#include <tqstylesheet.h>
24#include <tqdir.h>
25#include <tqfile.h>
26#include <tqfileinfo.h>
27#include <kurl.h>
28#include <kstandarddirs.h>
29#include <kmimetype.h>
30
31using namespace Kross::Api;
32
33namespace Kross { namespace Api {
34
36 class ScriptActionPrivate
37 {
38 public:
45 TQString packagepath;
46
54 TQStringList logs;
55
63 int version;
64
69 TQString description;
70
75 TQValueList<ScriptActionCollection*> collections;
76
80 explicit ScriptActionPrivate() : version(0) {}
81 };
82
83}}
84
85ScriptAction::ScriptAction(const TQString& file)
86 : TDEAction(0, file.latin1())
87 , Kross::Api::ScriptContainer(file)
88 , d( new ScriptActionPrivate() ) // initialize d-pointer class
89{
90 KURL url(file);
91 if(url.isLocalFile()) {
92 setFile(file);
93 setText(url.fileName());
94 setIcon(KMimeType::iconForURL(url));
95 }
96 else {
97 setText(file);
98 }
99
100 setDescription(file);
101 setEnabled(false);
102}
103
104ScriptAction::ScriptAction(const TQString& scriptconfigfile, const TQDomElement& element)
105 : TDEAction()
107 , d( new ScriptActionPrivate() ) // initialize d-pointer class
108{
109 TQString name = element.attribute("name");
110 TQString text = element.attribute("text");
111 TQString description = element.attribute("description");
112 TQString file = element.attribute("file");
113 TQString icon = element.attribute("icon");
114
115 TQString version = element.attribute("version");
116 bool ok;
117 int v = version.toInt(&ok);
118 if(ok) d->version = v;
119
120 if(file.isEmpty()) {
121 if(text.isEmpty())
122 text = name;
123 }
124 else {
125 if(name.isEmpty())
126 name = file;
127 if(text.isEmpty())
128 text = file;
129 }
130
131 //d->scriptcontainer = Manager::scriptManager()->getScriptContainer(name);
132
133 TQString interpreter = element.attribute("interpreter");
134 if(interpreter.isNull())
135 setEnabled(false);
136 else
137 setInterpreterName( interpreter );
138
139 if(file.isNull()) {
140 setCode( element.text().stripWhiteSpace() );
141 if(description.isNull())
142 description = text;
144 }
145 else {
146 TQDir dir = TQFileInfo(scriptconfigfile).dir(true);
147 d->packagepath = dir.absPath();
148 TQFileInfo fi(dir, file);
149 file = fi.absFilePath();
150 setEnabled(fi.exists());
151 setFile(file);
152 if(icon.isNull())
153 icon = KMimeType::iconForURL( KURL(file) );
154 if(description.isEmpty())
155 description = TQString("%1<br>%2").arg(text.isEmpty() ? name : text).arg(file);
156 else
157 description += TQString("<br>%1").arg(file);
159 }
160
161 TDEAction::setName(name.latin1());
163 setDescription(description);
164 TDEAction::setIcon(icon);
165
166 // connect signal
167 connect(this, TQ_SIGNAL(activated()), this, TQ_SLOT(activate()));
168}
169
171{
172 detachAll();
173 delete d;
174}
175
177{
178 return d->version;
179}
180
181const TQString ScriptAction::getDescription() const
182{
183 return d->description;
184}
185
186void ScriptAction::setDescription(const TQString& description)
187{
188 d->description = description;
189 setToolTip( description );
190 setWhatsThis( description );
191}
192
193void ScriptAction::setInterpreterName(const TQString& name)
194{
195 setEnabled( Manager::scriptManager()->hasInterpreterInfo(name) );
197}
198
199const TQString ScriptAction::getPackagePath() const
200{
201 return d->packagepath;
202}
203
204const TQStringList& ScriptAction::getLogs() const
205{
206 return d->logs;
207}
208
210{
211 d->collections.append( collection );
212}
213
215{
216 d->collections.remove( collection );
217}
218
220{
221 for(TQValueList<ScriptActionCollection*>::Iterator it = d->collections.begin(); it != d->collections.end(); ++it)
222 (*it)->detach( this );
223}
224
226{
227 emit activated(this);
230 TQString errormessage = Kross::Api::ScriptContainer::getException()->getError();
231 TQString tracedetails = Kross::Api::ScriptContainer::getException()->getTrace();
232 d->logs << TQString("<b>%1</b><br>%2")
233 .arg( TQStyleSheet::escape(errormessage) )
234 .arg( TQStyleSheet::escape(tracedetails) );
235 emit failed(errormessage, tracedetails);
236 }
237 else {
238 emit success();
239 }
240}
241
246
247#include "scriptaction.moc"
Exception::Ptr getException()
static Manager * scriptManager()
Return the Manager instance.
Definition manager.cpp:67
A collection to store ScriptAction shared pointers.
void detach(ScriptActionCollection *collection)
Detach this ScriptAction from the ScriptActionCollection collection .
void failed(const TQString &errormessage, const TQString &tracedetails)
This signal got emitted after the try to execute this ScriptAction failed.
void finalize()
This slot finalizes the ScriptContainer and tries to clean any still running script.
void attach(ScriptActionCollection *collection)
Attach this ScriptAction to the ScriptActionCollection collection .
const TQString getDescription() const
void setInterpreterName(const TQString &name)
Set the name of the interpreter which will be used on activation to execute the scripting code.
void detachAll()
Detach this ScriptAction from all ScriptActionCollection instance his ScriptAction is attached to.
const TQStringList & getLogs() const
void setDescription(const TQString &description)
Set the description description for this ScriptAction .
ScriptAction(const TQString &file)
A list of ScriptAction instances.
virtual ~ScriptAction()
Destructor.
virtual void activate()
If the ScriptAction got activated the ScriptContainer got executed.
const TQString getPackagePath() const
void success()
This signal got emitted after this ScriptAction got executed successfully.
void setFile(const TQString &scriptfile)
Set the filename which will be executed on execute.
ScriptContainer(const TQString &name=TQString())
Constructor.
void setName(const TQString &name)
Set the name this ScriptContainer is reachable as.
Object::Ptr execute()
Execute the script container.
void setInterpreterName(const TQString &interpretername)
Set the name of the interpreter used on execute.
void setCode(const TQString &code)
Set the scriptcode this ScriptContainer holds.
void finalize()
Finalize the Script instance and free's any cached or still running executions.
virtual void setToolTip(const TQString &)
virtual void setWhatsThis(const TQString &text)
virtual void setEnabled(bool enable)
TDEAction(const TQString &text, const TDEShortcut &cut, const TQObject *receiver, const char *slot, TDEActionCollection *parent, const char *name)
virtual TQString text() const
virtual void setText(const TQString &text)
void activated()
The common Kross API used as common codebase.
Definition callable.h:31
The Kross scripting bridge to embed scripting functionality into an application.
Definition callable.h:31