lib

scriptaction.cpp

00001 /***************************************************************************
00002  * scriptaction.cpp
00003  * This file is part of the KDE project
00004  * copyright (C) 2005 by Sebastian Sauer (mail@dipe.org)
00005  *
00006  * This program is free software; you can redistribute it and/or
00007  * modify it under the terms of the GNU Library General Public
00008  * License as published by the Free Software Foundation; either
00009  * version 2 of the License, or (at your option) any later version.
00010  * This program is distributed in the hope that it will be useful,
00011  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00012  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013  * Library General Public License for more details.
00014  * You should have received a copy of the GNU Library General Public License
00015  * along with this program; see the file COPYING.  If not, write to
00016  * the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
00017  * Boston, MA 02110-1301, USA.
00018  ***************************************************************************/
00019 
00020 #include "scriptaction.h"
00021 #include "manager.h"
00022 
00023 #include <qstylesheet.h>
00024 #include <qdir.h>
00025 #include <qfile.h>
00026 #include <qfileinfo.h>
00027 #include <kurl.h>
00028 #include <kstandarddirs.h>
00029 #include <kmimetype.h>
00030 #include <kdebug.h>
00031 
00032 using namespace Kross::Api;
00033 
00034 namespace Kross { namespace Api {
00035 
00037     class ScriptActionPrivate
00038     {
00039         public:
00046             QString packagepath;
00047 
00055             QStringList logs;
00056 
00064             int version;
00065 
00070             QString description;
00071 
00076             QValueList<ScriptActionCollection*> collections;
00077 
00081             explicit ScriptActionPrivate() : version(0) {}
00082     };
00083 
00084 }}
00085 
00086 ScriptAction::ScriptAction(const QString& file)
00087     : KAction(0, file.latin1())
00088     , Kross::Api::ScriptContainer(file)
00089     , d( new ScriptActionPrivate() ) // initialize d-pointer class
00090 {
00091     //kdDebug() << QString("Kross::Api::ScriptAction::ScriptAction(const char*, const QString&) name='%1' text='%2'").arg(name).arg(text) << endl;
00092 
00093     KURL url(file);
00094     if(url.isLocalFile()) {
00095         setFile(file);
00096         setText(url.fileName());
00097         setIcon(KMimeType::iconForURL(url));
00098     }
00099     else {
00100         setText(file);
00101     }
00102 
00103     setDescription(file);
00104     setEnabled(false);
00105 }
00106 
00107 ScriptAction::ScriptAction(const QString& scriptconfigfile, const QDomElement& element)
00108     : KAction()
00109     , Kross::Api::ScriptContainer()
00110     , d( new ScriptActionPrivate() ) // initialize d-pointer class
00111 {
00112     //kdDebug() << "Kross::Api::ScriptAction::ScriptAction(const QDomElement&)" << endl;
00113 
00114     QString name = element.attribute("name");
00115     QString text = element.attribute("text");
00116     QString description = element.attribute("description");
00117     QString file = element.attribute("file");
00118     QString icon = element.attribute("icon");
00119 
00120     QString version = element.attribute("version");
00121     bool ok;
00122     int v = version.toInt(&ok);
00123     if(ok) d->version = v;
00124 
00125     if(file.isEmpty()) {
00126         if(text.isEmpty())
00127             text = name;
00128     }
00129     else {
00130         if(name.isEmpty())
00131             name = file;
00132         if(text.isEmpty())
00133             text = file;
00134     }
00135 
00136     //d->scriptcontainer = Manager::scriptManager()->getScriptContainer(name);
00137 
00138     QString interpreter = element.attribute("interpreter");
00139     if(interpreter.isNull())
00140         setEnabled(false);
00141     else
00142         setInterpreterName( interpreter );
00143 
00144     if(file.isNull()) {
00145         setCode( element.text().stripWhiteSpace() );
00146         if(description.isNull())
00147             description = text;
00148         ScriptContainer::setName(name);
00149     }
00150     else {
00151         QDir dir = QFileInfo(scriptconfigfile).dir(true);
00152         d->packagepath = dir.absPath();
00153         QFileInfo fi(dir, file);
00154         file = fi.absFilePath();
00155         setEnabled(fi.exists());
00156         setFile(file);
00157         if(icon.isNull())
00158             icon = KMimeType::iconForURL( KURL(file) );
00159         if(description.isEmpty())
00160             description = QString("%1<br>%2").arg(text.isEmpty() ? name : text).arg(file);
00161         else
00162             description += QString("<br>%1").arg(file);
00163         ScriptContainer::setName(file);
00164     }
00165 
00166     KAction::setName(name.latin1());
00167     KAction::setText(text);
00168     setDescription(description);
00169     KAction::setIcon(icon);
00170 
00171     // connect signal
00172     connect(this, SIGNAL(activated()), this, SLOT(activate()));
00173 }
00174 
00175 ScriptAction::~ScriptAction()
00176 {
00177     //kdDebug() << QString("Kross::Api::ScriptAction::~ScriptAction() name='%1' text='%2'").arg(name()).arg(text()) << endl;
00178     detachAll();
00179     delete d;
00180 }
00181 
00182 int ScriptAction::version() const
00183 {
00184     return d->version;
00185 }
00186 
00187 const QString ScriptAction::getDescription() const
00188 {
00189     return d->description;
00190 }
00191 
00192 void ScriptAction::setDescription(const QString& description)
00193 {
00194     d->description = description;
00195     setToolTip( description );
00196     setWhatsThis( description );
00197 }
00198 
00199 void ScriptAction::setInterpreterName(const QString& name)
00200 {
00201     setEnabled( Manager::scriptManager()->hasInterpreterInfo(name) );
00202     Kross::Api::ScriptContainer::setInterpreterName(name);
00203 }
00204 
00205 const QString ScriptAction::getPackagePath() const
00206 {
00207     return d->packagepath;
00208 }
00209 
00210 const QStringList& ScriptAction::getLogs() const
00211 {
00212     return d->logs;
00213 }
00214 
00215 void ScriptAction::attach(ScriptActionCollection* collection)
00216 {
00217     d->collections.append( collection );
00218 }
00219 
00220 void ScriptAction::detach(ScriptActionCollection* collection)
00221 {
00222     d->collections.remove( collection );
00223 }
00224 
00225 void ScriptAction::detachAll()
00226 {
00227     for(QValueList<ScriptActionCollection*>::Iterator it = d->collections.begin(); it != d->collections.end(); ++it)
00228         (*it)->detach( this );
00229 }
00230 
00231 void ScriptAction::activate()
00232 {
00233     emit activated(this);
00234     Kross::Api::ScriptContainer::execute();
00235     if( Kross::Api::ScriptContainer::hadException() ) {
00236         QString errormessage = Kross::Api::ScriptContainer::getException()->getError();
00237         QString tracedetails = Kross::Api::ScriptContainer::getException()->getTrace();
00238         d->logs << QString("<b>%1</b><br>%2")
00239                    .arg( QStyleSheet::escape(errormessage) )
00240                    .arg( QStyleSheet::escape(tracedetails) );
00241         emit failed(errormessage, tracedetails);
00242     }
00243     else {
00244         emit success();
00245     }
00246 }
00247 
00248 void ScriptAction::finalize()
00249 {
00250     Kross::Api::ScriptContainer::finalize();
00251 }
00252 
00253 #include "scriptaction.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys