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 
00036     /// @internal
00037     class ScriptActionPrivate
00038     {
00039         public:
00040 
00041             QString packagepath;
00042 
00043             /**
00044             * List of all kind of logs this \a ScriptAction does
00045             * remember.
00046             */
00047             QStringList logs;
00048 
00049             QString description;
00050 
00051             QValueList<ScriptActionCollection*> collections;
00052     };
00053 
00054 }}
00055 
00056 ScriptAction::ScriptAction(const QString& file)
00057     : KAction(0, file.latin1())
00058     , Kross::Api::ScriptContainer(file)
00059     , d( new ScriptActionPrivate() ) // initialize d-pointer class
00060 {
00061     //kdDebug() << QString("Kross::Api::ScriptAction::ScriptAction(const char*, const QString&) name='%1' text='%2'").arg(name).arg(text) << endl;
00062 
00063     KURL url(file);
00064     if(url.isLocalFile()) {
00065         setFile(file);
00066         setText(url.fileName());
00067         setIcon(KMimeType::iconForURL(url));
00068     }
00069     else {
00070         setText(file);
00071     }
00072 
00073     setDescription(file);
00074     setEnabled(false);
00075 }
00076 
00077 ScriptAction::ScriptAction(const QString& scriptconfigfile, const QDomElement& element)
00078     : KAction()
00079     , Kross::Api::ScriptContainer()
00080     , d( new ScriptActionPrivate() ) // initialize d-pointer class
00081 {
00082     //kdDebug() << "Kross::Api::ScriptAction::ScriptAction(const QDomElement&)" << endl;
00083 
00084     QString name = element.attribute("name");
00085     QString text = element.attribute("text");
00086     QString description = element.attribute("description");
00087     QString file = element.attribute("file");
00088     QString icon = element.attribute("icon");
00089 
00090     if(file.isEmpty()) {
00091         if(text.isEmpty())
00092             text = name;
00093     }
00094     else {
00095         if(name.isEmpty())
00096             name = file;
00097         if(text.isEmpty())
00098             text = file;
00099     }
00100 
00101     //d->scriptcontainer = Manager::scriptManager()->getScriptContainer(name);
00102 
00103     QString interpreter = element.attribute("interpreter");
00104     if(interpreter.isNull())
00105         setEnabled(false);
00106     else
00107         setInterpreterName( interpreter );
00108 
00109     if(file.isNull()) {
00110         setCode( element.text().stripWhiteSpace() );
00111         if(description.isNull())
00112             description = text;
00113         ScriptContainer::setName(name);
00114     }
00115     else {
00116         QDir dir = QFileInfo(scriptconfigfile).dir(true);
00117         d->packagepath = dir.absPath();
00118         QFileInfo fi(dir, file);
00119         file = fi.absFilePath();
00120         setEnabled(fi.exists());
00121         setFile(file);
00122         if(icon.isNull())
00123             icon = KMimeType::iconForURL( KURL(file) );
00124         if(description.isEmpty())
00125             description = QString("%1<br>%2").arg(text.isEmpty() ? name : text).arg(file);
00126         else
00127             description += QString("<br>%1").arg(file);
00128         ScriptContainer::setName(file);
00129     }
00130 
00131     KAction::setName(name.latin1());
00132     KAction::setText(text);
00133     setDescription(description);
00134     KAction::setIcon(icon);
00135 
00136     // connect signal
00137     connect(this, SIGNAL(activated()), this, SLOT(activate()));
00138 }
00139 
00140 ScriptAction::~ScriptAction()
00141 {
00142     //kdDebug() << QString("Kross::Api::ScriptAction::~ScriptAction() name='%1' text='%2'").arg(name()).arg(text()) << endl;
00143     detachAll();
00144     delete d;
00145 }
00146 
00147 const QString ScriptAction::getDescription() const
00148 {
00149     return d->description;
00150 }
00151 
00152 void ScriptAction::setDescription(const QString& description)
00153 {
00154     d->description = description;
00155     setToolTip( description );
00156     setWhatsThis( description );
00157 }
00158 
00159 void ScriptAction::setInterpreterName(const QString& name)
00160 {
00161     setEnabled( Manager::scriptManager()->hasInterpreterInfo(name) );
00162     Kross::Api::ScriptContainer::setInterpreterName(name);
00163 }
00164 
00165 const QString ScriptAction::getPackagePath()
00166 {
00167     return d->packagepath;
00168 }
00169 
00170 const QStringList& ScriptAction::getLogs() const
00171 {
00172     return d->logs;
00173 }
00174 
00175 void ScriptAction::attach(ScriptActionCollection* collection)
00176 {
00177     d->collections.append( collection );
00178 }
00179 
00180 void ScriptAction::detach(ScriptActionCollection* collection)
00181 {
00182     d->collections.remove( collection );
00183 }
00184 
00185 void ScriptAction::detachAll()
00186 {
00187     for(QValueList<ScriptActionCollection*>::Iterator it = d->collections.begin(); it != d->collections.end(); ++it)
00188         (*it)->detach( this );
00189 }
00190 
00191 void ScriptAction::activate()
00192 {
00193     Kross::Api::ScriptContainer::execute();
00194     if( Kross::Api::ScriptContainer::hadException() ) {
00195         QString errormessage = Kross::Api::ScriptContainer::getException()->getError();
00196         QString tracedetails = Kross::Api::ScriptContainer::getException()->getTrace();
00197         d->logs << QString("<b>%1</b><br>%2")
00198                    .arg( QStyleSheet::escape(errormessage) )
00199                    .arg( QStyleSheet::escape(tracedetails) );
00200         emit failed(errormessage, tracedetails);
00201     }
00202     else {
00203         emit success();
00204     }
00205 }
00206 
00207 void ScriptAction::finalize()
00208 {
00209     Kross::Api::ScriptContainer::finalize();
00210 }
00211 
00212 #include "scriptaction.moc"

Generated on Thu Feb 9 17:59:11 2006 for Kross by  doxygen 1.4.6