27 #include <QtCore/QFile>
28 #include <QtCore/QFileInfo>
34 using namespace Kross;
99 Private() : script(0),
version(0) {}
108 th->setObjectName(name);
109 #ifdef KROSS_ACTION_DEBUG
110 krossdebug(
QString(
"Action::Action(QObject*,QString,QDir) Ctor name='%1'").arg(th->objectName()) );
112 QObject::connect(th, SIGNAL(triggered(
bool)), th, SLOT(slotTriggered()));
123 d->searchpath=
QStringList(packagepath.absolutePath());
133 QFileInfo fi( url.toLocalFile() );
134 setText( fi.fileName() );
141 #ifdef KROSS_ACTION_DEBUG
160 if( element.isNull() )
164 if( ! file.isEmpty() ) {
165 if( QFileInfo(file).exists() ) {
169 foreach (
const QString& packagepath, searchPath) {
170 QFileInfo fi(QDir(packagepath), file);
172 setFile( fi.absoluteFilePath() );
179 d->version =
QVariant( element.attribute(
"version",
QString(d->version)) ).toInt();
181 setText(
i18n( element.attribute(
"text").toUtf8() ) );
187 QString icon = element.attribute(
"icon");
188 if( icon.isEmpty() && ! d->scriptfile.isNull() )
189 icon = KMimeType::iconNameForUrl(
KUrl(d->scriptfile) );
193 if( ! code.isNull() )
196 for(QDomNode node = element.firstChild(); ! node.isNull(); node = node.nextSibling()) {
197 QDomElement e = node.toElement();
199 if( e.tagName() ==
"property" ) {
202 #ifdef KROSS_ACTION_DEBUG
203 krossdebug(
QString(
"Action::readDomElement: Setting property name=%1 value=%2").arg(n).arg(e.text()));
205 setProperty(n.toLatin1().constData(),
QVariant(e.text()));
220 QDomElement e = doc.createElement(
"script");
221 e.setAttribute(
"name", objectName());
223 e.setAttribute(
"version",
QString(d->version));
224 if( ! text().isNull() )
225 e.setAttribute(
"text", text());
231 e.setAttribute(
"enabled",
"false");
237 if (!searchPath.isEmpty()) {
239 foreach(
const QString& packagepath, searchPath) {
240 QString nfn=QDir(packagepath).relativeFilePath(
file());
241 if (nfn.length()<fileName.length())
246 if( ! fileName.isNull() ) {
247 e.setAttribute(
"file", fileName);
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()));
283 return d->description;
300 setIcon(
KIcon(iconname) );
301 d->iconname = iconname;
308 return QAction::isEnabled();
313 QAction::setEnabled(enabled);
325 if( d->code != code ) {
335 return d->interpretername;
340 if( d->interpretername != interpretername ) {
342 d->interpretername = interpretername;
345 krosswarning(
"Action::setInterpreter: interpreter not found: "+interpretername);
353 return d->scriptfile;
358 if( d->scriptfile != scriptfile ) {
360 if ( scriptfile.isNull() ) {
361 if( ! d->scriptfile.isNull() )
362 d->interpretername.clear();
363 d->scriptfile.clear();
364 d->searchpath.clear();
367 d->scriptfile = scriptfile;
369 if( d->interpretername.isNull() )
393 return this->
object(name);
403 if(d->options.contains(name))
404 return d->options[name];
406 return info ? info->
optionValue(name, defaultvalue) : defaultvalue;
414 d->options.insert(name, value);
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()) );
427 return d->script->functionNames();
436 return d->script->callFunction(name, args);
445 return d->script->evaluate(code);
452 if( ! d->scriptfile.isNull() ) {
453 QFile f( d->scriptfile );
455 setError(
i18n(
"Scriptfile \"%1\" does not exist.", d->scriptfile));
458 if( d->interpretername.isNull() ) {
459 setError(
i18n(
"Failed to determine interpreter for scriptfile \"%1\"", d->scriptfile));
462 if( ! f.open(QIODevice::ReadOnly) ) {
463 setError(
i18n(
"Failed to open scriptfile \"%1\"", d->scriptfile));
466 d->code = f.readAll();
471 if( ! interpreter ) {
474 setError(
i18n(
"Failed to load interpreter \"%1\"", d->interpretername));
476 setError(
i18n(
"No such interpreter \"%1\"", d->interpretername));
482 setError(
i18n(
"Failed to create script for interpreter \"%1\"", d->interpretername));
486 if( d->script->hadError() ) {
506 return d->script == 0;
509 void Action::slotTriggered()
511 #ifdef KROSS_ACTION_DEBUG
523 #ifdef KROSS_ACTION_DEBUG
528 d->script->execute();
529 if( d->script->hadError() ) {
530 #ifdef KROSS_ACTION_DEBUG
550 #include "action.moc"