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 
00031 using namespace Kross::Api;
00032 
00033 namespace Kross { namespace Api {
00034 
00036     class ScriptActionPrivate
00037     {
00038         public:
00045             QString packagepath;
00046 
00054             QStringList logs;
00055 
00063             int version;
00064 
00069             QString description;
00070 
00075             QValueList<ScriptActionCollection*> collections;
00076 
00080             explicit ScriptActionPrivate() : version(0) {}
00081     };
00082 
00083 }}
00084 
00085 ScriptAction::ScriptAction(const QString& file)
00086     : KAction(0, file.latin1())
00087     , Kross::Api::ScriptContainer(file)
00088     , d( new ScriptActionPrivate() ) // initialize d-pointer class
00089 {
00090     KURL url(file);
00091     if(url.isLocalFile()) {
00092         setFile(file);
00093         setText(url.fileName());
00094         setIcon(KMimeType::iconForURL(url));
00095     }
00096     else {
00097         setText(file);
00098     }
00099 
00100     setDescription(file);
00101     setEnabled(false);
00102 }
00103 
00104 ScriptAction::ScriptAction(const QString& scriptconfigfile, const QDomElement& element)
00105     : KAction()
00106     , Kross::Api::ScriptContainer()
00107     , d( new ScriptActionPrivate() ) // initialize d-pointer class
00108 {
00109     QString name = element.attribute("name");
00110     QString text = element.attribute("text");
00111     QString description = element.attribute("description");
00112     QString file = element.attribute("file");
00113     QString icon = element.attribute("icon");
00114 
00115     QString version = element.attribute("version");
00116     bool ok;
00117     int v = version.toInt(&ok);
00118     if(ok) d->version = v;
00119 
00120     if(file.isEmpty()) {
00121         if(text.isEmpty())
00122             text = name;
00123     }
00124     else {
00125         if(name.isEmpty())
00126             name = file;
00127         if(text.isEmpty())
00128             text = file;
00129     }
00130 
00131     //d->scriptcontainer = Manager::scriptManager()->getScriptContainer(name);
00132 
00133     QString interpreter = element.attribute("interpreter");
00134     if(interpreter.isNull())
00135         setEnabled(false);
00136     else
00137         setInterpreterName( interpreter );
00138 
00139     if(file.isNull()) {
00140         setCode( element.text().stripWhiteSpace() );
00141         if(description.isNull())
00142             description = text;
00143         ScriptContainer::setName(name);
00144     }
00145     else {
00146         QDir dir = QFileInfo(scriptconfigfile).dir(true);
00147         d->packagepath = dir.absPath();
00148         QFileInfo fi(dir, file);
00149         file = fi.absFilePath();
00150         setEnabled(fi.exists());
00151         setFile(file);
00152         if(icon.isNull())
00153             icon = KMimeType::iconForURL( KURL(file) );
00154         if(description.isEmpty())
00155             description = QString("%1<br>%2").arg(text.isEmpty() ? name : text).arg(file);
00156         else
00157             description += QString("<br>%1").arg(file);
00158         ScriptContainer::setName(file);
00159     }
00160 
00161     KAction::setName(name.latin1());
00162     KAction::setText(text);
00163     setDescription(description);
00164     KAction::setIcon(icon);
00165 
00166     // connect signal
00167     connect(this, SIGNAL(activated()), this, SLOT(activate()));
00168 }
00169 
00170 ScriptAction::~ScriptAction()
00171 {
00172     detachAll();
00173     delete d;
00174 }
00175 
00176 int ScriptAction::version() const
00177 {
00178     return d->version;
00179 }
00180 
00181 const QString ScriptAction::getDescription() const
00182 {
00183     return d->description;
00184 }
00185 
00186 void ScriptAction::setDescription(const QString& description)
00187 {
00188     d->description = description;
00189     setToolTip( description );
00190     setWhatsThis( description );
00191 }
00192 
00193 void ScriptAction::setInterpreterName(const QString& name)
00194 {
00195     setEnabled( Manager::scriptManager()->hasInterpreterInfo(name) );
00196     Kross::Api::ScriptContainer::setInterpreterName(name);
00197 }
00198 
00199 const QString ScriptAction::getPackagePath() const
00200 {
00201     return d->packagepath;
00202 }
00203 
00204 const QStringList& ScriptAction::getLogs() const
00205 {
00206     return d->logs;
00207 }
00208 
00209 void ScriptAction::attach(ScriptActionCollection* collection)
00210 {
00211     d->collections.append( collection );
00212 }
00213 
00214 void ScriptAction::detach(ScriptActionCollection* collection)
00215 {
00216     d->collections.remove( collection );
00217 }
00218 
00219 void ScriptAction::detachAll()
00220 {
00221     for(QValueList<ScriptActionCollection*>::Iterator it = d->collections.begin(); it != d->collections.end(); ++it)
00222         (*it)->detach( this );
00223 }
00224 
00225 void ScriptAction::activate()
00226 {
00227     emit activated(this);
00228     Kross::Api::ScriptContainer::execute();
00229     if( Kross::Api::ScriptContainer::hadException() ) {
00230         QString errormessage = Kross::Api::ScriptContainer::getException()->getError();
00231         QString tracedetails = Kross::Api::ScriptContainer::getException()->getTrace();
00232         d->logs << QString("<b>%1</b><br>%2")
00233                    .arg( QStyleSheet::escape(errormessage) )
00234                    .arg( QStyleSheet::escape(tracedetails) );
00235         emit failed(errormessage, tracedetails);
00236     }
00237     else {
00238         emit success();
00239     }
00240 }
00241 
00242 void ScriptAction::finalize()
00243 {
00244     Kross::Api::ScriptContainer::finalize();
00245 }
00246 
00247 #include "scriptaction.moc"
KDE Home | KDE Accessibility Home | Description of Access Keys