• Skip to content
  • Skip to link menu
KDE 4.2 API Reference
  • KDE API Reference
  • kdelibs
  • Sitemap
  • Contact Us
 

Kross

actioncollection.cpp

Go to the documentation of this file.
00001 /***************************************************************************
00002  * actioncollection.cpp
00003  * This file is part of the KDE project
00004  * copyright (C)2004-2006 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 "actioncollection.h"
00021 #include "action.h"
00022 #include "manager.h"
00023 
00024 #include <QtCore/QHash>
00025 #include <QtCore/QStringList>
00026 #include <QtCore/QPointer>
00027 #include <QtCore/QIODevice>
00028 #include <QtCore/QFile>
00029 #include <QtCore/QFileInfo>
00030 #include <QtXml/QDomAttr>
00031 
00032 #include <kicon.h>
00033 #include <klocalizedstring.h>
00034 
00035 using namespace Kross;
00036 
00037 namespace Kross {
00038 
00040     class ActionCollection::Private
00041     {
00042         public:
00043             QPointer<ActionCollection> parent;
00044             QHash< QString, QPointer<ActionCollection> > collections;
00045             QStringList collectionnames;
00046 
00047             QList< Action* > actionList;
00048             QHash< QString, Action* > actionMap;
00049 
00050             QString text;
00051             QString description;
00052             QString iconname;
00053             bool enabled;
00054             bool blockupdated;
00055 
00056             Private(ActionCollection* const p) : parent(p) {}
00057     };
00058 
00059 }
00060 
00061 ActionCollection::ActionCollection(const QString& name, ActionCollection* parent)
00062     : QObject(0)
00063     , d( new Private(0) )
00064 {
00065     setObjectName(name);
00066     d->text = name;
00067     d->enabled = true;
00068     d->blockupdated = false;
00069 
00070     setParentCollection(parent);
00071 }
00072 
00073 ActionCollection::~ActionCollection()
00074 {
00075     if ( d->parent ) {
00076         emit d->parent->collectionToBeRemoved(this, d->parent);
00077         d->parent->unregisterCollection( objectName() );
00078         emit d->parent->collectionRemoved( this, d->parent );
00079     }
00080     delete d;
00081 }
00082 
00083 QString ActionCollection::name() const { return objectName(); }
00084 
00085 QString ActionCollection::text() const { return d->text; }
00086 void ActionCollection::setText(const QString& text) { d->text = text; emit dataChanged(this); emitUpdated(); }
00087 
00088 QString ActionCollection::description() const { return d->description; }
00089 void ActionCollection::setDescription(const QString& description) { d->description = description; emit dataChanged(this); emitUpdated(); }
00090 
00091 QString ActionCollection::iconName() const { return d->iconname; }
00092 void ActionCollection::setIconName(const QString& iconname) { d->iconname = iconname; emit dataChanged(this); }
00093 QIcon ActionCollection::icon() const { return KIcon(d->iconname); }
00094 
00095 bool ActionCollection::isEnabled() const { return d->enabled; }
00096 void ActionCollection::setEnabled(bool enabled) { d->enabled = enabled; emit dataChanged(this); emitUpdated(); }
00097 
00098 ActionCollection* ActionCollection::parentCollection() const
00099 {
00100     return d->parent;
00101 }
00102 
00103 void ActionCollection::setParentCollection( ActionCollection *parent )
00104 {
00105     if ( d->parent ) {
00106         emit d->parent->collectionToBeRemoved(this, d->parent);
00107         d->parent->unregisterCollection( objectName() );
00108         setParent( 0 );
00109         emit d->parent->collectionRemoved( this, d->parent );
00110         d->parent = 0;
00111     }
00112     setParent(0);
00113     if ( parent ) {
00114         emit parent->collectionToBeInserted(this, parent);
00115         setParent( parent );
00116         d->parent = parent;
00117         parent->registerCollection( this );
00118         emit parent->collectionInserted( this, parent );
00119     }
00120     emitUpdated();
00121 }
00122 
00123 bool ActionCollection::hasCollection(const QString& name) const
00124 {
00125     return d->collections.contains(name);
00126 }
00127 
00128 ActionCollection* ActionCollection::collection(const QString& name) const
00129 {
00130     return d->collections.contains(name) ? d->collections[name] : QPointer<ActionCollection>(0);
00131 }
00132 
00133 QStringList ActionCollection::collections() const
00134 {
00135     return d->collectionnames;
00136 }
00137 
00138 void ActionCollection::registerCollection(ActionCollection* collection)
00139 {
00140     Q_ASSERT(collection);
00141     const QString name = collection->objectName();
00142     //Q_ASSERT( !name.isNull() );
00143     d->collections.insert(name, collection);
00144     d->collectionnames.append(name);
00145     connectSignals(collection, true);
00146     emitUpdated();
00147 }
00148 
00149 void ActionCollection::unregisterCollection(const QString& name)
00150 {
00151     if( ! d->collections.contains(name) )
00152         return;
00153     ActionCollection* collection = d->collections[name];
00154     d->collectionnames.removeAll(name);
00155     d->collections.remove(name);
00156     connectSignals(collection, false);
00157     emitUpdated();
00158 }
00159 
00160 QList<Action*> ActionCollection::actions() const
00161 {
00162     return d->actionList;
00163 }
00164 
00165 Action* ActionCollection::action(const QString& name) const
00166 {
00167     return d->actionMap.contains(name) ? d->actionMap[name] : 0;
00168 }
00169 
00170 void ActionCollection::addAction(Action* action)
00171 {
00172     Q_ASSERT( action && ! action->objectName().isEmpty() );
00173     addAction(action->objectName(), action);
00174 }
00175 
00176 void ActionCollection::addAction(const QString& name, Action* action)
00177 {
00178     Q_ASSERT( action && ! name.isEmpty() );
00179     emit actionToBeInserted(action, this);
00180     if( d->actionMap.contains(name) )
00181         d->actionList.removeAll( d->actionMap[name] );
00182     d->actionMap.insert(name, action);
00183     d->actionList.append(action);
00184     action->setParent(this); // in case it is not set
00185     connectSignals(action, true);
00186     emit actionInserted(action, this);
00187     emitUpdated();
00188 }
00189 
00190 void ActionCollection::removeAction(const QString& name)
00191 {
00192     if( ! d->actionMap.contains(name) )
00193         return;
00194     Action* action = d->actionMap[name];
00195     connectSignals(action, false);
00196     emit actionToBeRemoved(action, this);
00197     d->actionList.removeAll(action);
00198     d->actionMap.remove(name);
00199     //krossdebug( QString("ActionCollection::removeAction: %1 %2").arg(action->name()).arg(action->parent()->objectName()) );
00200     action->setParent( 0 );
00201     emit actionRemoved(action, this);
00202     emitUpdated();
00203 }
00204 
00205 void ActionCollection::removeAction(Action* action)
00206 {
00207     Q_ASSERT( action && ! action->objectName().isEmpty() );
00208     if( ! d->actionMap.contains(action->objectName()) ) {
00209         Q_ASSERT( ! d->actionList.contains(action) );
00210         return;
00211     }
00212     removeAction( action->objectName() );
00213 }
00214 
00215 void ActionCollection::connectSignals(Action *action, bool conn)
00216 {
00217     if ( conn ) {
00218         connect(action, SIGNAL(dataChanged(Action*)), this, SIGNAL(dataChanged(Action*)));
00219         connect(action, SIGNAL(updated()), this, SLOT(emitUpdated()));
00220     } else {
00221         disconnect(action, SIGNAL(dataChanged(Action*)), this, SIGNAL(dataChanged(Action*)));
00222         disconnect(action, SIGNAL(updated()), this, SLOT(emitUpdated()));
00223     }
00224 }
00225 
00226 void ActionCollection::connectSignals(ActionCollection *collection, bool conn)
00227 {
00228     if ( conn ) {
00229         connect(collection, SIGNAL(dataChanged(Action*)), this, SIGNAL(dataChanged(Action*)));
00230         connect(collection, SIGNAL(dataChanged(ActionCollection*)), this, SIGNAL(dataChanged(ActionCollection*)));
00231 
00232         connect(collection, SIGNAL(collectionToBeInserted(ActionCollection*, ActionCollection*)), this, SIGNAL(collectionToBeInserted(ActionCollection*, ActionCollection*)));
00233         connect(collection, SIGNAL(collectionInserted(ActionCollection*, ActionCollection*)), this, SIGNAL(collectionInserted(ActionCollection*, ActionCollection*)));
00234         connect(collection, SIGNAL(collectionToBeRemoved(ActionCollection*, ActionCollection*)), this, SIGNAL(collectionToBeRemoved(ActionCollection*, ActionCollection*)));
00235         connect(collection, SIGNAL(collectionRemoved(ActionCollection*, ActionCollection*)), this, SIGNAL(collectionRemoved(ActionCollection*, ActionCollection*)));
00236 
00237         connect(collection, SIGNAL(actionToBeInserted(Action*, ActionCollection*)), this, SIGNAL(actionToBeInserted(Action*, ActionCollection*)));
00238         connect(collection, SIGNAL(actionInserted(Action*, ActionCollection*)), this, SIGNAL(actionInserted(Action*, ActionCollection*)));
00239         connect(collection, SIGNAL(actionToBeRemoved(Action*, ActionCollection*)), this, SIGNAL(actionToBeRemoved(Action*, ActionCollection*)));
00240         connect(collection, SIGNAL(actionRemoved(Action*, ActionCollection*)), this, SIGNAL(actionRemoved(Action*, ActionCollection*)));
00241         connect(collection, SIGNAL(updated()), this, SLOT(emitUpdated()));
00242     } else {
00243         disconnect(collection, SIGNAL(dataChanged(ActionCollection*)), this, SIGNAL(dataChanged(ActionCollection*)));
00244 
00245         disconnect(collection, SIGNAL(collectionToBeInserted(ActionCollection*, ActionCollection*)), this, SIGNAL(collectionToBeInserted(ActionCollection*, ActionCollection*)));
00246         disconnect(collection, SIGNAL(collectionInserted(ActionCollection*, ActionCollection*)), this, SIGNAL(collectionInserted(ActionCollection*, ActionCollection*)));
00247         disconnect(collection, SIGNAL(collectionToBeRemoved(ActionCollection*, ActionCollection*)), this, SIGNAL(collectionToBeRemoved(ActionCollection*, ActionCollection*)));
00248         disconnect(collection, SIGNAL(collectionRemoved(ActionCollection*, ActionCollection*)), this, SIGNAL(collectionRemoved(ActionCollection*, ActionCollection*)));
00249 
00250         disconnect(collection, SIGNAL(actionToBeInserted(Action*, ActionCollection*)), this, SIGNAL(actionToBeInserted(Action*, ActionCollection*)));
00251         disconnect(collection, SIGNAL(actionInserted(Action*, ActionCollection*)), this, SIGNAL(actionInserted(Action*, ActionCollection*)));
00252         disconnect(collection, SIGNAL(actionToBeRemoved(Action*, ActionCollection*)), this, SIGNAL(actionToBeRemoved(Action*, ActionCollection*)));
00253         disconnect(collection, SIGNAL(actionRemoved(Action*, ActionCollection*)), this, SIGNAL(actionRemoved(Action*, ActionCollection*)));
00254         disconnect(collection, SIGNAL(updated()), this, SLOT(emitUpdated()));
00255     }
00256 }
00257 
00258 void ActionCollection::emitUpdated()
00259 {
00260     if (!d->blockupdated) emit updated();
00261 }
00262 
00263 /*********************************************************************
00264  * Unserialize from XML / QIODevice / file / resource to child
00265  * ActionCollection's and Action's this ActionCollection has.
00266  */
00267 
00268 bool ActionCollection::readXml(const QDomElement& element, const QDir& directory)
00269 {
00270     #ifdef KROSS_ACTIONCOLLECTION_DEBUG
00271         krossdebug( QString("ActionCollection::readXml tagName=\"%1\"").arg(element.tagName()) );
00272     #endif
00273 
00274     d->blockupdated = true; // block updated() signals and emit it only once if everything is done
00275     bool ok = true;
00276     QDomNodeList list = element.childNodes();
00277     const int size = list.size();
00278     for(int i = 0; i < size; ++i) {
00279         QDomElement elem = list.item(i).toElement();
00280         if( elem.isNull() ) continue;
00281 
00282         #ifdef KROSS_ACTIONCOLLECTION_DEBUG
00283             krossdebug( QString("  ActionCollection::readXml child=%1 tagName=\"%2\"").arg(i).arg(elem.tagName()) );
00284         #endif
00285 
00286         if( elem.tagName() == "collection") {
00287             const QString name = elem.attribute("name");
00288             const QByteArray text = elem.attribute("text").toUtf8();
00289             const QByteArray description = elem.attribute("comment").toUtf8();
00290             const QString iconname = elem.attribute("icon");
00291             bool enabled = QVariant(elem.attribute("enabled","true")).toBool();
00292             ActionCollection* c = d->collections.contains(name) ? d->collections[name] : QPointer<ActionCollection>(0);
00293             if( ! c )
00294                 c = new ActionCollection(name, this);
00295 
00296             c->setText( text.isEmpty() ? name : i18n( text ) );
00297             c->setDescription( description.isEmpty() ? c->text() : i18n( description ) );
00298             c->setIconName( iconname );
00299 
00300             if( ! enabled )
00301                 c->setEnabled(false);
00302             if( ! c->readXml(elem, directory) )
00303                 ok = false;
00304         }
00305         else if( elem.tagName() == "script") {
00306             QString name = elem.attribute("name");
00307             Action* a = dynamic_cast< Action* >( action(name) );
00308             if( a ) {
00309                 #ifdef KROSS_ACTIONCOLLECTION_DEBUG
00310                     krossdebug( QString("  ActionCollection::readXml Updating Action \"%1\"").arg(a->objectName()) );
00311                 #endif
00312             }
00313             else {
00314                 #ifdef KROSS_ACTIONCOLLECTION_DEBUG
00315                     krossdebug( QString("  ActionCollection::readXml Creating Action \"%1\"").arg(name) );
00316                 #endif
00317 
00318                 a = new Action(this, name, directory);
00319                 addAction(name, a);
00320                 connect(a, SIGNAL( started(Kross::Action*) ), &Manager::self(), SIGNAL( started(Kross::Action*)) );
00321                 connect(a, SIGNAL( finished(Kross::Action*) ), &Manager::self(), SIGNAL( finished(Kross::Action*) ));
00322             }
00323             a->fromDomElement(elem);
00324         }
00325         //else if( ! fromXml(elem) ) ok = false;
00326     }
00327 
00328     d->blockupdated = false; // unblock signals
00329     emitUpdated();
00330     return ok;
00331 }
00332 
00333 bool ActionCollection::readXml(QIODevice* device, const QDir& directory)
00334 {
00335     QString errMsg;
00336     int errLine, errCol;
00337     QDomDocument document;
00338     bool ok = document.setContent(device, false, &errMsg, &errLine, &errCol);
00339     if( ! ok ) {
00340         #ifdef KROSS_ACTIONCOLLECTION_DEBUG
00341             krosswarning( QString("ActionCollection::readXml Error at line %1 in col %2: %3").arg(errLine).arg(errCol).arg(errMsg) );
00342         #endif
00343         return false;
00344     }
00345     return readXml(document.documentElement(), directory);
00346 }
00347 
00348 bool ActionCollection::readXmlFile(const QString& file)
00349 {
00350     #ifdef KROSS_ACTIONCOLLECTION_DEBUG
00351         krossdebug( QString("ActionCollection::readXmlFile file=\"%1\"").arg(file) );
00352     #endif
00353 
00354     QFile f(file);
00355     if( ! f.open(QIODevice::ReadOnly) ) {
00356         #ifdef KROSS_ACTIONCOLLECTION_DEBUG
00357             krosswarning( QString("ActionCollection::readXmlFile reading file \"%1\" failed.").arg(file) );
00358         #endif
00359         return false;
00360     }
00361     bool ok = readXml(&f, QFileInfo(file).dir());
00362     f.close();
00363 
00364     #ifdef KROSS_ACTIONCOLLECTION_DEBUG
00365         if( ! ok )
00366             krosswarning( QString("ActionCollection::readXmlFile parsing XML content of file \"%1\" failed.").arg(file) );
00367     #endif
00368     return ok;
00369 }
00370 
00371 /*********************************************************************
00372  * Serialize from child ActionCollection's and Action's this
00373  * ActionCollection has to XML / QIODevice / file / resource.
00374  */
00375 
00376 QDomElement ActionCollection::writeXml()
00377 {
00378     #ifdef KROSS_ACTIONCOLLECTION_DEBUG
00379         krossdebug( QString("ActionCollection::writeXml collection.objectName=\"%1\"").arg(objectName()) );
00380     #endif
00381 
00382     QDomDocument document;
00383     QDomElement element = document.createElement("collection");
00384     if( ! objectName().isNull() )
00385         element.setAttribute("name", objectName());
00386     if( ! text().isNull() && text() != objectName() )
00387         element.setAttribute("text", text());
00388     if( ! d->description.isNull() )
00389         element.setAttribute("comment", d->description);
00390     if( ! d->iconname.isNull() )
00391         element.setAttribute("icon", d->iconname);
00392     if( ! d->enabled )
00393         element.setAttribute("enabled", d->enabled);
00394 
00395     foreach(Action* a, actions()) {
00396         Q_ASSERT(a);
00397         #ifdef KROSS_ACTIONCOLLECTION_DEBUG
00398             krossdebug( QString("  ActionCollection::writeXml action.objectName=\"%1\" action.file=\"%2\"").arg(a->objectName()).arg(a->file()) );
00399         #endif
00400         QDomElement e = a->toDomElement();
00401         if( ! e.isNull() )
00402             element.appendChild(e);
00403     }
00404 
00405     foreach(const QString &name, d->collectionnames) {
00406         ActionCollection* c = d->collections[name];
00407         if( ! c ) continue;
00408         QDomElement e = c->writeXml();
00409         if( ! e.isNull() )
00410             element.appendChild(e);
00411     }
00412 
00413     return element;
00414 }
00415 
00416 bool ActionCollection::writeXml(QIODevice* device, int indent)
00417 {
00418     QDomDocument document;
00419     QDomElement root = document.createElement("KrossScripting");
00420 
00421     foreach(Action* a, actions()) {
00422         QDomElement e = a->toDomElement();
00423         if( ! e.isNull() )
00424             root.appendChild(e);
00425     }
00426 
00427     foreach(const QString &name, d->collectionnames) {
00428         ActionCollection* c = d->collections[name];
00429         if( ! c ) continue;
00430         QDomElement e = c->writeXml();
00431         if( ! e.isNull() )
00432             root.appendChild(e);
00433     }
00434 
00435     document.appendChild(root);
00436     return device->write( document.toByteArray(indent) ) != -1;
00437 }
00438 
00439 #include "actioncollection.moc"

Kross

Skip menu "Kross"
  • Main Page
  • Namespace List
  • Class Hierarchy
  • Alphabetical List
  • Class List
  • File List
  • Namespace Members
  • Class Members
  • Related Pages

kdelibs

Skip menu "kdelibs"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • Kate
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • Kross
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Generated for kdelibs by doxygen 1.5.7
This website is maintained by Adriaan de Groot and Allen Winter.
KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal