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

Kross

plugin.cpp

Go to the documentation of this file.
00001 /* This file is part of the KDE project
00002    Copyright (C) 2008 Paulo Moura Guedes <moura@kdewebdev.org>
00003 
00004    This library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Library General Public
00006    License as published by the Free Software Foundation; either
00007    version 2 of the License, or (at your option) any later version.
00008 
00009    This library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Library General Public License for more details.
00013 
00014    You should have received a copy of the GNU Library General Public License
00015    along with this library; see the file COPYING.LIB.  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 "plugin.h"
00021 
00022 #include <kdebug.h>
00023 #include <kstandarddirs.h>
00024 #include <krun.h>
00025 #include <kxmlguifactory.h>
00026 #include <kactioncollection.h>
00027 #include <kross/core/manager.h>
00028 #include <kross/core/actioncollection.h>
00029 #include <kio/netaccess.h>
00030 
00031 #include <QPointer>
00032 
00033 using namespace Kross;
00034 
00036 class ScriptingPlugin::ScriptingPluginPrivate
00037 {
00038 public:
00039     QString userActionsFile;
00040     QHash<QString, QPointer<QObject> > objects;
00041 
00042     QDomElement menuFromName(QString const& name, const QDomDocument& document)
00043     {
00044         QDomElement menuBar = document.documentElement().firstChildElement("MenuBar");
00045         QDomElement menu = menuBar.firstChildElement("Menu");
00046         for(; !menu.isNull(); menu = menu.nextSiblingElement("Menu")) {
00047             if(menu.attribute("name") == name) {
00048                 return menu;
00049             }
00050         }
00051         return QDomElement();
00052     }
00053 };
00054 
00055 ScriptingPlugin::ScriptingPlugin(QObject* parent)
00056     : KParts::Plugin(parent)
00057     , d(new ScriptingPluginPrivate())
00058 {
00059     d->userActionsFile = KGlobal::dirs()->locateLocal("appdata", "scripts/scriptactions.rc");
00060 }
00061 
00062 ScriptingPlugin::~ScriptingPlugin()
00063 {
00064     delete d;
00065 }
00066 
00067 void ScriptingPlugin::setDOMDocument(const QDomDocument &document, bool merge)
00068 {
00069     QDomDocument doc = buildDomDocument(document);
00070     KXMLGUIClient::setDOMDocument(doc, merge);
00071 }
00072 
00073 void ScriptingPlugin::addObject(QObject* object, const QString& name)
00074 {
00075     QString n = name.isNull() ? object->objectName() : name;
00076     d->objects.insert(n, object);
00077 }
00078 
00079 QDomDocument ScriptingPlugin::buildDomDocument(const QDomDocument& document)
00080 {
00081     QStringList allActionFiles = KGlobal::dirs()->findAllResources("appdata", "scripts/*.rc");
00082 
00083     Kross::Manager::self().setProperty("configfile", d->userActionsFile);
00084     Kross::Manager::self().setProperty("configfiles", allActionFiles);
00085 
00086     if(KIO::NetAccess::exists(KUrl(d->userActionsFile), KIO::NetAccess::SourceSide, 0)) {
00087         Kross::Manager::self().actionCollection()->readXmlFile(d->userActionsFile);
00088     }
00089     else {
00090         foreach(const QString &f, allActionFiles) {
00091             Kross::Manager::self().actionCollection()->readXmlFile(f);
00092         }
00093     }
00094 
00095     QDomDocument doc(document);
00096     buildDomDocument(doc, Kross::Manager::self().actionCollection());
00097 
00098     return doc;
00099 }
00100 
00101 void ScriptingPlugin::buildDomDocument(QDomDocument& document,
00102     Kross::ActionCollection* collection)
00103 {
00104     QDomElement menuElement = d->menuFromName(collection->name(), document);
00105 
00106     foreach(Kross::Action* action, collection->actions()) {
00107         QHashIterator<QString, QPointer<QObject> > i(d->objects);
00108         while(i.hasNext()) {
00109             i.next();
00110             action->addObject(i.value(), i.key());
00111         }
00112         
00113         // Create and append new Menu element if doesn't exist
00114         if(menuElement.isNull()) {
00115             menuElement = document.createElement("Menu");
00116             menuElement.setAttribute("name", collection->name());
00117             menuElement.setAttribute("noMerge", "0");
00118 
00119             QDomElement textElement = document.createElement("text");
00120             textElement.appendChild(document.createTextNode(collection->text()));
00121             menuElement.appendChild(textElement);
00122 
00123             Kross::ActionCollection* parentCollection = collection->parentCollection();
00124             if(!parentCollection) {
00125                 document.documentElement().firstChildElement("MenuBar").appendChild(menuElement);
00126             }
00127             else {
00128                 QDomElement parentMenuElement = d->menuFromName(parentCollection->name(), document);
00129 
00130                 if(!parentMenuElement.isNull()) {
00131                     parentMenuElement.appendChild(menuElement);
00132                 }
00133                 else {
00134                     document.documentElement().firstChildElement("MenuBar").appendChild(menuElement);
00135                 }
00136             }
00137         }
00138 
00139         // Create and append new Action element
00140         QDomElement newActionElement = document.createElement("Action");
00141         newActionElement.setAttribute("name", action->name());
00142 
00143         menuElement.appendChild(newActionElement);
00144 
00145         actionCollection()->addAction(action->name(), action);
00146     }
00147 
00148     foreach(const QString &collectionname, collection->collections()) {
00149         Kross::ActionCollection* c = collection->collection(collectionname);
00150         if(c->isEnabled()) {
00151             buildDomDocument(document, c);
00152         }
00153     }
00154 }
00155 
00156 void ScriptingPlugin::slotEditScriptActions()
00157 {
00158     if(!KIO::NetAccess::exists(KUrl(d->userActionsFile), KIO::NetAccess::SourceSide, 0)) {
00159         KUrl dir = KUrl(d->userActionsFile).directory();
00160         KIO::NetAccess::mkdir(dir, 0);
00161 
00162         QFile f(d->userActionsFile);
00163         if(f.open(QIODevice::WriteOnly)) {
00164 
00165             bool collectionEmpty = true;
00166             if(!Kross::Manager::self().actionCollection()->actions().empty()
00167                 || !Kross::Manager::self().actionCollection()->collections().empty()) {
00168                 collectionEmpty = false;
00169             }
00170 
00171             if( !collectionEmpty ) {
00172                 if( Kross::Manager::self().actionCollection()->writeXml(&f) ) {
00173                     kDebug() << "Successfully saved file: " << d->userActionsFile;
00174                 }
00175             }
00176             else {
00177                 QTextStream out(&f);
00178                 QString xml("<!-- ");
00179                 xml.append("\n");
00180                 xml.append("Collection name attribute represents the name of the menu, e.g., to use menu \"File\" use \"file\" or \"Help\" use \"help\". One can add new menus also.");
00181                 xml.append("\n\n\n");
00182                 xml.append("If you type a relative script file beware the this script is located in  $KDEHOME/share/apps/applicationname/");
00183                 xml.append("\n\n");
00184                 xml.append("The following example add an action with the text \"Export...\" into the \"File\" menu");
00185                 xml.append("\n");
00186                 xml.append("-->");
00187                 xml.append("\n\n");
00188                 xml.append("<KrossScripting>");
00189                 xml.append("\n");
00190                 xml.append("<collection name=\"file\" text=\"File\" comment=\"File menu\">");
00191                 xml.append("\n");
00192                 xml.append("<script name=\"export\" text=\"Export...\" comment=\"Export content\" file=\"export.py\" />");
00193                 xml.append("\n");
00194                 xml.append("</collection>");
00195                 xml.append("\n");
00196                 xml.append("</KrossScripting>");;
00197 
00198                 out << xml;
00199             }
00200         }
00201         f.close();
00202     }
00203 
00204     KRun::runUrl(KUrl(d->userActionsFile), QString("text/plain"), 0, false);
00205 }
00206 
00207 void ScriptingPlugin::slotResetScriptActions()
00208 {
00209     KIO::NetAccess::del(KUrl(d->userActionsFile), 0);
00210 }
00211 
00212 #include "plugin.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