• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.14.38 API Reference
  • KDE Home
  • Contact Us
 

Kross

  • kross
  • ui
ui/plugin.cpp
Go to the documentation of this file.
1/* This file is part of the KDE project
2 Copyright (C) 2008 Paulo Moura Guedes <moura@kdewebdev.org>
3
4 This library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public
6 License as published by the Free Software Foundation; either
7 version 2 of the License, or (at your option) any later version.
8
9 This library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
13
14 You should have received a copy of the GNU Library General Public License
15 along with this library; see the file COPYING.LIB. If not, write to
16 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 Boston, MA 02110-1301, USA.
18*/
19
20#include "plugin.h"
21
22#include <kaction.h>
23#include <kdebug.h>
24#include <kstandarddirs.h>
25#include <krun.h>
26#include <kxmlguifactory.h>
27#include <kactioncollection.h>
28#include <kross/core/manager.h>
29#include <kross/core/actioncollection.h>
30#include <kio/netaccess.h>
31
32#include <QPointer>
33
34using namespace Kross;
35
36struct Object
37{
38 QPointer<QObject> object;
39 ChildrenInterface::Options options;
40 Object(QObject* obj, ChildrenInterface::Options opt):object(obj),options(opt){}
41};
42
43
45class ScriptingPlugin::ScriptingPluginPrivate
46{
47public:
48 QString collectionName;
49 QString userActionsFile;
50 QString referenceActionsDir;
51 QHash<QString, Object> objects;
52
53 QDomElement menuFromName(QString const& name, const QDomDocument& document)
54 {
55 QDomElement menuBar = document.documentElement().firstChildElement("MenuBar");
56 QDomElement menu = menuBar.firstChildElement("Menu");
57 for(; !menu.isNull(); menu = menu.nextSiblingElement("Menu")) {
58 if(menu.attribute("name") == name) {
59 return menu;
60 }
61 }
62 return QDomElement();
63 }
64};
65
66ScriptingPlugin::ScriptingPlugin(QObject* parent)
67 : KParts::Plugin(parent)
68 , d(new ScriptingPluginPrivate())
69{
70 d->userActionsFile = KGlobal::dirs()->locateLocal("appdata", "scripts/scriptactions.rc");
71 d->collectionName="scripting-plugin";
72}
73
74ScriptingPlugin::ScriptingPlugin(const QString& collectionName, const QString& userActionsFile, const QString& referenceActionsDir, QObject* parent)
75 : KParts::Plugin(parent)
76 , d(new ScriptingPluginPrivate())
77{
78 d->collectionName=collectionName;
79 d->userActionsFile = userActionsFile;
80 d->referenceActionsDir = referenceActionsDir;
81}
82
83ScriptingPlugin::~ScriptingPlugin()
84{
85 if (QFile::exists(d->userActionsFile))
86 save();
87
88 Kross::ActionCollection* collection=Kross::Manager::self().actionCollection()->collection(d->collectionName);
89 if (collection) {
90 collection->setParentCollection(0);
91 collection->deleteLater();
92 }
93
94 delete d;
95}
96
97void ScriptingPlugin::setDOMDocument(const QDomDocument &document, bool merge)
98{
99 QDomDocument doc = buildDomDocument(document);
100 KXMLGUIClient::setDOMDocument(doc, merge);
101}
102
103void ScriptingPlugin::addObject(QObject* object, const QString& name)
104{
105 QString n = name.isNull() ? object->objectName() : name;
106 d->objects.insert(n, Object(object,ChildrenInterface::NoOption));
107}
108
109void ScriptingPlugin::addObject(QObject* object, const QString& name, ChildrenInterface::Options options)
110{
111 QString n = name.isNull() ? object->objectName() : name;
112 d->objects.insert(n, Object(object,options));
113}
114
115QDomDocument ScriptingPlugin::buildDomDocument(const QDomDocument& document)
116{
117 Kross::ActionCollection* collection=Kross::Manager::self().actionCollection()->collection(d->collectionName);
118 if (!collection) {
119 collection=new Kross::ActionCollection(d->collectionName, Kross::Manager::self().actionCollection());
120 }
121
122 QStringList allActionFiles = KGlobal::dirs()->findAllResources("appdata", "scripts/"+d->referenceActionsDir+"/*.rc");
123 //move userActionsFile to the end so that it updates existing actions and adds new ones.
124 int pos=allActionFiles.indexOf(d->userActionsFile);
125 if (pos!=-1)
126 allActionFiles.append(allActionFiles.takeAt(pos));
127 else if (QFile::exists(d->userActionsFile)) //in case d->userActionsFile isn't in the standard local dir
128 allActionFiles.append(d->userActionsFile);
129
130 QStringList searchPath=KGlobal::dirs()->findDirs("appdata", "scripts/"+d->referenceActionsDir);
131 foreach(const QString &file, allActionFiles) {
132 QFile f(file);
133 if (!f.open(QIODevice::ReadOnly))
134 continue;
135
136 collection->readXml(&f, searchPath+QStringList(QFileInfo(f).absolutePath()));
137 f.close();
138
139 }
140
141 QDomDocument doc(document);
142 buildDomDocument(doc, collection);
143
144 return doc;
145}
146
147void ScriptingPlugin::buildDomDocument(QDomDocument& document,
148 Kross::ActionCollection* collection)
149{
150 QDomElement menuElement = d->menuFromName(collection->name(), document);
151
152 foreach(Kross::Action* action, collection->actions()) {
153 QHashIterator<QString, Object> i(d->objects);
154 while(i.hasNext()) {
155 i.next();
156 action->addObject(i.value().object, i.key(), i.value().options);
157 }
158
159 // Create and append new Menu element if doesn't exist
160 if(menuElement.isNull()) {
161 menuElement = document.createElement("Menu");
162 menuElement.setAttribute("name", collection->name());
163 menuElement.setAttribute("noMerge", "0");
164
165 QDomElement textElement = document.createElement("text");
166 textElement.appendChild(document.createTextNode(collection->text()));
167 menuElement.appendChild(textElement);
168
169 Kross::ActionCollection* parentCollection = collection->parentCollection();
170 QDomElement root;
171 if(parentCollection) {
172 QDomElement parentMenuElement = d->menuFromName(parentCollection->name(), document);
173 if(!parentMenuElement.isNull())
174 root=parentMenuElement;
175 }
176 if (root.isNull())
177 root=document.documentElement().firstChildElement("MenuBar");
178 root.appendChild(menuElement);
179 }
180
181 // Create and append new Action element
182 QDomElement newActionElement = document.createElement("Action");
183 newActionElement.setAttribute("name", action->name());
184
185 menuElement.appendChild(newActionElement);
186
187
188 KAction* adaptor=new KAction(action->text(), action);
189 connect (adaptor,SIGNAL(triggered()),action,SLOT(trigger()));
190 adaptor->setEnabled(action->isEnabled());
191 adaptor->setIcon(action->icon());
192 actionCollection()->addAction(action->name(), adaptor);
193 }
194
195 foreach(const QString &collectionname, collection->collections()) {
196 Kross::ActionCollection* c = collection->collection(collectionname);
197 if(c->isEnabled()) {
198 buildDomDocument(document, c);
199 }
200 }
201}
202
203void ScriptingPlugin::save()
204{
205 QFile f(d->userActionsFile);
206 if(!f.open(QIODevice::WriteOnly))
207 return;
208
209 Kross::ActionCollection* collection=Kross::Manager::self().actionCollection()->collection(d->collectionName);
210 bool collectionEmpty = !collection||(collection->actions().empty()&&collection->collections().empty());
211
212 if( !collectionEmpty ) {
213 QStringList searchPath=KGlobal::dirs()->findDirs("appdata", "scripts/"+d->referenceActionsDir);
214 searchPath.append(QFileInfo(d->userActionsFile).absolutePath());
215 if( collection->writeXml(&f, 2, searchPath) ) {
216 kDebug() << "Successfully saved file: " << d->userActionsFile;
217 }
218 }
219 else {
220 QTextStream out(&f);
221 QString xml=
222 "<!-- "
223 "\n"
224 "Collection name attribute represents the name of the menu, e.g., to use menu \"File\" use \"file\" or \"Help\" use \"help\". You can add new menus."
225 "\n\n\n"
226 "If you type a relative script file beware the this script is located in $KDEHOME/share/apps/applicationname/"
227 "\n\n"
228 "The following example adds an action with the text \"Export...\" into the \"File\" menu"
229 "\n\n"
230 "<KrossScripting>"
231 "\n"
232 "<collection name=\"file\" text=\"File\" comment=\"File menu\">"
233 "\n"
234 "<script name=\"export\" text=\"Export...\" comment=\"Export content\" file=\"export.py\" />"
235 "\n"
236 "</collection>"
237 "\n"
238 "</KrossScripting>"
239 "\n"
240 "-->";
241
242
243 out << xml;
244 }
245 f.close();
246}
247
248void ScriptingPlugin::slotEditScriptActions()
249{
250 if(!KIO::NetAccess::exists(KUrl(d->userActionsFile), KIO::NetAccess::SourceSide, 0)) {
251 KUrl dir = KUrl(d->userActionsFile).directory();
252 KIO::NetAccess::mkdir(dir, 0);
253
254 save();
255 }
256
257 //TODO very funny! this should use ui/view.h instead --Nick
258 KRun::runUrl(KUrl(d->userActionsFile), QString("text/plain"), 0, false);
259}
260
261void ScriptingPlugin::slotResetScriptActions()
262{
263 KIO::NetAccess::del(KUrl(d->userActionsFile), 0);
264}
265
266#include "plugin.moc"
actioncollection.h
KAction
KStandardDirs::findAllResources
QStringList findAllResources(const char *type, const QString &filter, SearchOptions options, QStringList &relPaths) const
KStandardDirs::findDirs
QStringList findDirs(const char *type, const QString &reldir) const
KStandardDirs::locateLocal
static QString locateLocal(const char *type, const QString &filename, bool createDir, const KComponentData &cData=KGlobal::mainComponent())
KUrl
KUrl::directory
QString directory(const DirectoryOptions &options=IgnoreTrailingSlash) const
KXMLGUIClient::setDOMDocument
virtual void setDOMDocument(const QDomDocument &document, bool merge=false)
Kross::ActionCollection
The ActionCollection class manages collections of Action instances.
Definition: actioncollection.h:46
Kross::ActionCollection::setParentCollection
void setParentCollection(ActionCollection *parent)
Set the parent to parent. NOTE: Do not use setParent().
Definition: actioncollection.cpp:103
Kross::ActionCollection::name
QString name() const
Definition: actioncollection.cpp:83
Kross::ActionCollection::actions
QList< Action * > actions() const
Definition: actioncollection.cpp:163
Kross::ActionCollection::parentCollection
ActionCollection * parentCollection() const
Definition: actioncollection.cpp:98
Kross::ActionCollection::text
QString text() const
Definition: actioncollection.cpp:85
Kross::ActionCollection::isEnabled
bool isEnabled() const
Return the enable this ActionCollection has.
Definition: actioncollection.cpp:95
Kross::ActionCollection::writeXml
QDomElement writeXml()
Definition: actioncollection.cpp:389
Kross::ActionCollection::readXml
bool readXml(const QDomElement &element, const QDir &directory=QDir())
Load child Action and ActionCollection instances this collection has from the element .
Definition: actioncollection.cpp:271
Kross::ActionCollection::collection
ActionCollection * collection(const QString &name) const
Definition: actioncollection.cpp:128
Kross::ActionCollection::collections
QStringList collections() const
Definition: actioncollection.cpp:133
Kross::Action
The Action class is an abstract container to deal with scripts like a single standalone script file.
Definition: action.h:99
Kross::Action::name
QString name() const
Kross::Action::isEnabled
bool isEnabled() const
Return true if this Action is enabled else false is returned.
Definition: action.cpp:306
Kross::ChildrenInterface::addObject
void addObject(QObject *object, const QString &name=QString(), Options options=NoOption)
Add a QObject to the list of children.
Definition: childreninterface.h:80
Kross::ChildrenInterface::Options
Options
Additional options that could be defined for a QObject instance.
Definition: childreninterface.h:45
Kross::ChildrenInterface::NoOption
@ NoOption
No additional options. This is the default.
Definition: childreninterface.h:46
Kross::Manager::self
static Manager & self()
Return the Manager instance.
Definition: manager.cpp:73
Kross::Manager::actionCollection
ActionCollection * actionCollection() const
Definition: manager.cpp:285
Kross::Object
The class Object does provide us scripting objects like class instances to the C++ world.
Definition: object.h:70
Kross::ScriptingPlugin::slotEditScriptActions
virtual void slotEditScriptActions()
This slot will open/create a scriptactions.rc file at $KDEHOME/share/apps/application/scripts/ which ...
Definition: ui/plugin.cpp:248
Kross::ScriptingPlugin::addObject
void addObject(QObject *object, const QString &name, ChildrenInterface::Options options)
Add a QObject to the list of children.
Definition: ui/plugin.cpp:109
Kross::ScriptingPlugin::setDOMDocument
virtual void setDOMDocument(const QDomDocument &document, bool merge=false)
Re-implement in order to load additional kross scripting rc files.
Definition: ui/plugin.cpp:97
Kross::ScriptingPlugin::slotResetScriptActions
virtual void slotResetScriptActions()
Deletes the user rc file, which has the effect of falling back to the default script actions (if any)...
Definition: ui/plugin.cpp:261
Kross::ScriptingPlugin::~ScriptingPlugin
virtual ~ScriptingPlugin()
Destructor.
Definition: ui/plugin.cpp:83
Kross::ScriptingPlugin::ScriptingPlugin
ScriptingPlugin(QObject *parent=0)
Constructor.
Definition: ui/plugin.cpp:66
QHash
QObject
kDebug
#define kDebug
kaction.h
kactioncollection.h
kdebug.h
kstandarddirs.h
kxmlguifactory.h
manager.h
KGlobal::dirs
KStandardDirs * dirs()
KParts
name
const char * name(StandardAction id)
Kross
Definition: action.cpp:36
plugin.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Mon Feb 20 2023 00:00:00 by doxygen 1.9.6 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

Kross

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

kdelibs-4.14.38 API Reference

Skip menu "kdelibs-4.14.38 API Reference"
  • DNSSD
  • Interfaces
  •   KHexEdit
  •   KMediaPlayer
  •   KSpeech
  •   KTextEditor
  • kconf_update
  • KDE3Support
  •   KUnitTest
  • KDECore
  • KDED
  • KDEsu
  • KDEUI
  • KDEWebKit
  • KDocTools
  • KFile
  • KHTML
  • KImgIO
  • KInit
  • kio
  • KIOSlave
  • KJS
  •   KJS-API
  •   WTF
  • kjsembed
  • KNewStuff
  • KParts
  • KPty
  • Kross
  • KUnitConversion
  • KUtils
  • Nepomuk
  • Plasma
  • Solid
  • Sonnet
  • ThreadWeaver
Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal