• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdeedu API Reference
  • KDE Home
  • Contact Us
 

parley

  • sources
  • kde-4.14
  • kdeedu
  • parley
  • src
  • scripts
scriptmanager.cpp
Go to the documentation of this file.
1 /***************************************************************************
2 
3  Copyright 2008 Avgoustinos Kadis <avgoustinos.kadis@kdemail.net>
4 
5  ***************************************************************************/
6 
7 /***************************************************************************
8  * *
9  * This program is free software; you can redistribute it and/or modify *
10  * it under the terms of the GNU General Public License as published by *
11  * the Free Software Foundation; either version 2 of the License, or *
12  * (at your option) any later version. *
13  * *
14  ***************************************************************************/
15 #include "scriptmanager.h"
16 
17 #include <KStandardDirs>
18 #include <KDebug>
19 #include <KPluginInfo>
20 #include <KServiceTypeTrader>
21 #include <QFileInfo>
22 #include <KActionCollection>
23 #include <KPassivePopup>
24 #include <KMessageBox>
25 
26 #include <kross/core/action.h>
27 #include <kross/core/manager.h>
28 
29 using namespace Editor;
30 
31 ScriptManager::ScriptManager(EditorWindow * editor)
32  : m_editor(editor)
33 {
34  //add Scripting::Parley
35  m_scriptingParley = new Scripting::Parley(editor);
36  addObject(m_scriptingParley, "Parley");
37 }
38 
39 
40 ScriptManager::~ScriptManager()
41 {
42 }
43 
44 
45 QStringList ScriptManager::getDesktopFiles()
46 {
47 // QStringList scriptsAvailable;
48  return KGlobal::dirs()->findAllResources(
49  "appdata",
50  QString("plugins/*.desktop"),
51  KStandardDirs::Recursive
52  /*, scriptsAvailable*/
53  );
54 }
55 
56 
57 QMap<QString, QString> ScriptManager::categories()
58 {
59  QMap<QString, QString> categories;
60  categories["translation"] = "Translation";
61  return categories;
62 }
63 
64 
65 QString ScriptManager::getScriptEntry(QString desktopFile)
66 {
67  //open it as a raw configuration file and read the script entry
68  KConfig scriptconfig(desktopFile, KConfig::SimpleConfig);
69  KConfigGroup group = scriptconfig.group("Desktop Entry");
70  return group.readEntry("Script");
71 }
72 
73 
74 QString ScriptManager::getScriptFileName(QString desktopFile)
75 {
76  QFileInfo desktopFileInfo(desktopFile);
77  return desktopFileInfo.absolutePath() + '/' + ScriptManager::getScriptEntry(desktopFile);
78 }
79 
80 
81 QStringList ScriptManager::enabledScripts()
82 {
83  QStringList enabledScripts;
84  // Open parleyrc to read the state of the plugins (enabled/disabled)
85  KConfigGroup cfg(KSharedConfig::openConfig("parleyrc"), "Plugins");
86  // Get list of KPluginInfo for each of the desktop files found
87  QList<KPluginInfo> pluginsInfoList = KPluginInfo::fromFiles(getDesktopFiles());
88  // Find which plugins are enabled and add them to enabledScripts list
89  KPluginInfo inf;
90  foreach(inf, pluginsInfoList) {
91  inf.load(cfg);
92  if (inf.isPluginEnabled())
93  enabledScripts.push_back(inf.entryPath());
94 // kDebug() << inf.name() << inf.isPluginEnabled() << inf.pluginName();
95  }
96  return enabledScripts;
97 }
98 
99 
100 void ScriptManager::disablePlugin(QString desktopFile)
101 {
102  KConfigGroup cfg(KSharedConfig::openConfig("parleyrc"), "Plugins");
103  KPluginInfo inf(desktopFile);
104  //load parleyrc enabled value
105  inf.load(cfg);
106  inf.setPluginEnabled(false);
107  //save enabled=true in parleyrc
108  inf.save(cfg);
109 }
110 
111 
112 void ScriptManager::loadScripts()
113 {
114  QStringList scripts = enabledScripts();
115  QStringList failed;
116  QStringList errorDetails;
117  foreach(const QString & script, scripts) {
118  //create a new Script and add it to the m_scripts list
119  Script * s = new Script(getScriptFileName(script));
120  s->addObjects(m_scriptObjects);
121  s->activate();
122  m_scripts.push_back(s);
123  if (!s->isActivated()) {
124  failed << getScriptFileName(script); //TODO: real name?
125  errorDetails << s->errorMessage();
126  disablePlugin(script);
127  }
128  }
129  //inform with a message box when a script could not be activated
130  if (!failed.empty()) {
131  QString errorMessage = "<p>" + i18np("A script could not be activated and has been disabled.", "%1 scripts could not be activated and have been disabled.", failed.count()) + ' ' + i18n("This probably means that there are errors in the script or that the required packages are not installed.") + "</p>";
132  errorMessage += "<ul><li>" + failed.join("</li><li>") + "</li></ul>";
133  KMessageBox::detailedError(m_editor, errorMessage, errorDetails.join("<hr/>"), i18n("Script Activation"));
134  }
135 }
136 
137 
138 void ScriptManager::addObject(QObject * obj, const QString & name)
139 {
140  m_scriptObjects[name] = obj;
141 }
142 
143 
144 void ScriptManager::reloadScripts()
145 {
146  //deactivate (delete) all the active scripts
147  foreach(Script * s, m_scripts) {
148  if (s) delete s;
149  }
150  m_scripts.clear();
151 
152  //reload the scripts menu
153  m_editor->unplugActionList("scripts_actionlist");
154  m_scriptActions.clear();
155  m_editor->plugActionList("scripts_actionlist", m_scriptActions);
156 
157  //load all the enabled scripts
158  loadScripts();
159 }
160 
161 
162 void ScriptManager::addScriptAction(const QString & name, KAction * action)
163 {
164  //unplug action list (orelse it will add twice the same entries
165  m_editor->unplugActionList("scripts_actionlist");
166 
167  //add to action collection
168  m_editor->actionCollection()->addAction(name, action);
169 
170  //add it to actions menu list
171  m_editor->m_scriptManager->m_scriptActions.push_back(action);
172 
173  //plug the action list
174  m_editor->plugActionList("scripts_actionlist", m_scriptActions);
175 
176 }
Script::addObjects
void addObjects(QMap< QString, QObject * > objects)
Adds more than one scripting Objects to the script.
Definition: script.cpp:116
QList::clear
void clear()
ScriptManager::ScriptManager
ScriptManager(Editor::EditorWindow *editor)
Definition: scriptmanager.cpp:31
ScriptManager::enabledScripts
QStringList enabledScripts()
Returns a list of filenames (full path) of enabled scripts.
Definition: scriptmanager.cpp:81
QList::push_back
void push_back(const T &value)
ScriptManager::addObject
void addObject(QObject *obj, const QString &name)
Adds a QObject as a module for the script.
Definition: scriptmanager.cpp:138
ScriptManager::addScriptAction
void addScriptAction(const QString &name, KAction *action)
Add a KAction to the Scripts menu.
Definition: scriptmanager.cpp:162
Script::isActivated
bool isActivated()
Returns true if the script was successfully activated; false otherwise.
Definition: script.cpp:36
ScriptManager::getScriptEntry
static QString getScriptEntry(QString desktopFile)
Parses the desktop desktopFile given and returns the value of "Script" entry.
Definition: scriptmanager.cpp:65
QMap< QString, QString >
QStringList::join
QString join(const QString &separator) const
ScriptManager::getScriptFileName
QString getScriptFileName(QString desktopFile)
Returns the full path to the script name given in the desktopFile.
Definition: scriptmanager.cpp:74
QObject::name
const char * name() const
QList::count
int count(const T &value) const
Script
This class represents the activated script and is used by the ScriptManager to activate/deactivate sc...
Definition: script.h:31
QList::empty
bool empty() const
ScriptManager::reloadScripts
void reloadScripts()
Reloads all the scripts.
Definition: scriptmanager.cpp:144
QObject
QString
QList< KPluginInfo >
QStringList
QFileInfo
ScriptManager::loadScripts
void loadScripts()
Loads (activates) all the available scripts and notifies the user if any script was not activated (du...
Definition: scriptmanager.cpp:112
Scripting::Parley
Parley scripting class (main entry point of a Parley Kross script)
Definition: parley.h:152
ScriptManager::categories
static QMap< QString, QString > categories()
Returns a QMap (from from categories codenames to categories display label) to be used in KPluginSele...
Definition: scriptmanager.cpp:57
Script::errorMessage
QString errorMessage()
Returns an html error message if there have been errors in the script.
Definition: script.cpp:121
Editor::EditorWindow
Definition: editor.h:50
ScriptManager::~ScriptManager
~ScriptManager()
Definition: scriptmanager.cpp:40
Script::activate
void activate()
Activates the script.
Definition: script.cpp:42
ScriptManager::getDesktopFiles
static QStringList getDesktopFiles()
Finds all the available desktop files in {PARLEY_DATA_FOLDER}/plugins.
Definition: scriptmanager.cpp:45
QFileInfo::absolutePath
QString absolutePath() const
ScriptManager::disablePlugin
void disablePlugin(QString desktopFile)
Modify the parleyrc configuration so it disables the dektopFile plugin.
Definition: scriptmanager.cpp:100
scriptmanager.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Mon Jun 22 2020 13:15:56 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

parley

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

kdeedu API Reference

Skip menu "kdeedu API Reference"
  • Analitza
  •     lib
  • kalgebra
  • kalzium
  •   libscience
  • kanagram
  • kig
  •   lib
  • klettres
  • marble
  • parley
  • rocs
  •   App
  •   RocsCore
  •   VisualEditor
  •   stepcore

Search



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