scriptaction.h

00001 /***************************************************************************
00002  * scriptaction.h
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 #ifndef KROSS_API_SCRIPTACTION_H
00021 #define KROSS_API_SCRIPTACTION_H
00022 
00023 #include <qdom.h>
00024 #include <kaction.h>
00025 
00026 #include "scriptcontainer.h"
00027 
00028 namespace Kross { namespace Api {
00029 
00030     // Forward declarations.
00031     class ScriptContainer;
00032     class ScriptActionCollection;
00033     class ScriptActionPrivate;
00034 
00035     /**
00036      * A ScriptAction extends a KAction by providing a wrapper around
00037      * a \a ScriptContainer to execute scripting code on activation.
00038      */
00039     class ScriptAction
00040         : public KAction
00041         , public Kross::Api::ScriptContainer
00042     {
00043             Q_OBJECT
00044 
00045             /// The name of the interpreter used to execute the scripting code.
00046             //Q_PROPERTY(QString interpretername READ getInterpreterName WRITE setInterpreterName)
00047 
00048             /// The scripting code which should be executed.
00049             //Q_PROPERTY(QString code READ getCode WRITE setCode)
00050 
00051             /// The scriptfile which should be executed.
00052             //Q_PROPERTY(QString file READ getFile WRITE setFile)
00053 
00054             /// The description for this \a ScriptAction .
00055             Q_PROPERTY(QString description READ getDescription WRITE setDescription)
00056 
00057         public:
00058 
00059             /// Shared pointer to implement reference-counting.
00060             typedef KSharedPtr<ScriptAction> Ptr;
00061 
00062             /// A list of \a ScriptAction instances.
00063             //typedef QValueList<ScriptAction::Ptr> List;
00064 
00065             /**
00066              * Constructor.
00067              *
00068              * \param file The KURL scriptfile this \a ScriptAction
00069              *        points to.
00070              */
00071             explicit ScriptAction(const QString& file);
00072 
00073             /**
00074              * Constructor.
00075              *
00076              * \param element The QDomElement which will be used
00077              *        to setup the \a ScriptAction attributes.
00078              */
00079             explicit ScriptAction(const QString& scriptconfigfile, const QDomElement& element);
00080 
00081             /**
00082              * Destructor.
00083              */
00084             virtual ~ScriptAction();
00085 
00086             /**
00087              * \return the description for this \a ScriptAction has.
00088              */
00089             const QString getDescription() const;
00090 
00091             /**
00092              * Set the description \p description for this \a ScriptAction .
00093              */
00094             void setDescription(const QString& description);
00095 
00096             /**
00097              * Set the name of the interpreter which will be used
00098              * on activation to execute the scripting code.
00099              *
00100              * \param name The name of the \a Interpreter . This
00101              *        could be e.g. "python".
00102              */
00103             void setInterpreterName(const QString& name);
00104 
00105             /**
00106              * \return the path of the package this \a ScriptAction
00107              * belongs to or QString::null if it doesn't belong to
00108              * any package.
00109              */
00110             const QString getPackagePath();
00111 
00112             /**
00113              * \return a list of all kind of logs this \a ScriptAction
00114              * does remember.
00115              */
00116             const QStringList& getLogs() const;
00117 
00118             /**
00119              * Attach this \a ScriptAction to the \a ScriptActionCollection
00120              * \p collection .
00121              */
00122             void attach(ScriptActionCollection* collection);
00123 
00124             /**
00125              * Detach this \a ScriptAction from the \a ScriptActionCollection
00126              * \p collection .
00127              */
00128             void detach(ScriptActionCollection* collection);
00129 
00130             /**
00131              * Detach this \a ScriptAction from all \a ScriptActionCollection
00132              * instance his \a ScriptAction is attached to.
00133              */
00134             void detachAll();
00135 
00136         public slots:
00137 
00138             /**
00139              * If the \a ScriptAction got activated the \a ScriptContainer
00140              * got executed. Once this slot got executed it will emit a
00141              * \a success() or \a failed() signal.
00142              */
00143             virtual void activate();
00144 
00145             /**
00146              * This slot finalizes the \a ScriptContainer and tries to clean
00147              * any still running script.
00148              */
00149             void finalize();
00150 
00151         signals:
00152 
00153             /**
00154             * This signal got emitted after this \a ScriptAction got
00155             * executed successfully.
00156             */
00157             void success();
00158 
00159             /**
00160             * This signal got emitted after the try to execute this
00161             * \a ScriptAction failed. The \p errormessage contains
00162             * the error message.
00163             */
00164             void failed(const QString& errormessage, const QString& tracedetails);
00165 
00166         private:
00167             /// Internaly used private d-pointer.
00168             ScriptActionPrivate* d;
00169     };
00170 
00171     /**
00172      * A collection to store \a ScriptAction shared pointers.
00173      *
00174      * A \a ScriptAction instance could be stored within
00175      * multiple \a ScriptActionCollection instances.
00176      */
00177     class ScriptActionCollection
00178     {
00179         private:
00180 
00181             /**
00182              * The list of \a ScriptAction shared pointers.
00183              */
00184             QValueList<ScriptAction::Ptr> m_list;
00185 
00186             /**
00187              * A map of \a ScriptAction shared pointers used to access
00188              * the actions with there name.
00189              */
00190             QMap<QCString, ScriptAction::Ptr> m_actions;
00191 
00192             /**
00193              * A KActionMenu which could be used to display the
00194              * content of this \a ScriptActionCollection instance.
00195              */
00196             KActionMenu* m_actionmenu;
00197 
00198             /**
00199              * Boolean value used to represent the modified-state. Will
00200              * be true if this \a ScriptActionCollection is modified
00201              * aka dirty and e.g. the \a m_actionmenu needs to be
00202              * updated else its false.
00203              */
00204             bool m_dirty;
00205 
00206         public:
00207 
00208             /**
00209              * Constructor.
00210              *
00211              * \param text The text used to display some describing caption.
00212              * \param ac The KActionCollection which should be used to as
00213              *        initial content for the KActionMenu \a m_actionmenu .
00214              * \param name The internal name.
00215              */
00216             ScriptActionCollection(const QString& text, KActionCollection* ac, const char* name)
00217                 : m_actionmenu( new KActionMenu(text, ac, name) )
00218                 , m_dirty(true) {}
00219 
00220 
00221             /**
00222              * Destructor.
00223              */
00224             ~ScriptActionCollection() {
00225                 for(QValueList<ScriptAction::Ptr>::Iterator it = m_list.begin(); it != m_list.end(); ++it)
00226                     (*it)->detach(this);
00227             }
00228 
00229             /**
00230              * \return the \a ScriptAction instance which has the name \p name
00231              * or NULL if there exists no such action.
00232              */
00233             ScriptAction::Ptr action(const QCString& name) { return m_actions[name]; }
00234 
00235             /**
00236              * \return a list of actions.
00237              */
00238             QValueList<ScriptAction::Ptr> actions() { return m_list; }
00239 
00240             /**
00241              * \return the KActionMenu \a m_actionmenu .
00242              */
00243             KActionMenu* actionMenu() { return m_actionmenu; }
00244 
00245             /**
00246              * Attach a \a ScriptAction instance to this \a ScriptActionCollection .
00247              */
00248             void attach(ScriptAction::Ptr action) {
00249                 m_dirty = true;
00250                 m_actions[ action->name() ] = action;
00251                 m_list.append(action);
00252                 m_actionmenu->insert(action);
00253                 action->attach(this);
00254             }
00255 
00256             /**
00257              * Detach a \a ScriptAction instance from this \a ScriptActionCollection .
00258              */
00259             void detach(ScriptAction::Ptr action) {
00260                 m_dirty = true;
00261                 m_actions.remove(action->name());
00262                 m_list.remove(action);
00263                 m_actionmenu->remove(action);
00264                 action->detach(this);
00265             }
00266 
00267             /**
00268              * Clear this \a ScriptActionCollection . The collection
00269              * will be empty and there are no actions attach any longer.
00270              */
00271             void clear() {
00272                 for(QValueList<ScriptAction::Ptr>::Iterator it = m_list.begin(); it != m_list.end(); ++it) {
00273                     m_actionmenu->remove(*it);
00274                     (*it)->detach(this);
00275                 }
00276                 m_list.clear();
00277                 m_actions.clear();
00278             }
00279 
00280     };
00281 
00282 }}
00283 
00284 #endif
00285 

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