• Skip to content
  • Skip to link menu
  • KDE API Reference
  • kdelibs-4.11.5 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 
34 using namespace Kross;
35 
36 struct Object
37 {
38  QPointer<QObject> object;
39  ChildrenInterface::Options options;
40  Object(QObject* obj, ChildrenInterface::Options opt):object(obj),options(opt){}
41 };
42 
43 
45 class ScriptingPlugin::ScriptingPluginPrivate
46 {
47 public:
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 
66 ScriptingPlugin::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 
74 ScriptingPlugin::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 
83 ScriptingPlugin::~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 
97 void ScriptingPlugin::setDOMDocument(const QDomDocument &document, bool merge)
98 {
99  QDomDocument doc = buildDomDocument(document);
100  KXMLGUIClient::setDOMDocument(doc, merge);
101 }
102 
103 void 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 
109 void 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 
115 QDomDocument 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 
147 void 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 
203 void 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 
248 void 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 
261 void ScriptingPlugin::slotResetScriptActions()
262 {
263  KIO::NetAccess::del(KUrl(d->userActionsFile), 0);
264 }
265 
266 #include "plugin.moc"
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Sep 23 2014 09:58:38 by doxygen 1.8.3.1 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.11.5 API Reference

Skip menu "kdelibs-4.11.5 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