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

parley

  • sources
  • kde-4.12
  • 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  {
92  inf.load ( cfg );
93  if ( inf.isPluginEnabled() )
94  enabledScripts.push_back ( inf.entryPath() );
95 // kDebug() << inf.name() << inf.isPluginEnabled() << inf.pluginName();
96  }
97  return enabledScripts;
98 }
99 
100 
101 void ScriptManager::disablePlugin ( QString desktopFile )
102 {
103  KConfigGroup cfg ( KSharedConfig::openConfig ( "parleyrc" ),"Plugins" );
104  KPluginInfo inf ( desktopFile );
105  //load parleyrc enabled value
106  inf.load ( cfg );
107  inf.setPluginEnabled ( false );
108  //save enabled=true in parleyrc
109  inf.save ( cfg );
110 }
111 
112 
113 void ScriptManager::loadScripts()
114 {
115  QStringList scripts = enabledScripts();
116  QStringList failed;
117  QStringList errorDetails;
118  foreach ( const QString& script, scripts )
119  {
120  //create a new Script and add it to the m_scripts list
121  Script * s = new Script ( getScriptFileName (script) );
122  s->addObjects ( m_scriptObjects );
123  s->activate();
124  m_scripts.push_back ( s );
125  if ( !s->isActivated() ) {
126  failed << getScriptFileName (script); //TODO: real name?
127  errorDetails << s->errorMessage();
128  disablePlugin(script);
129  }
130  }
131  //inform with a message box when a script could not be activated
132  if (!failed.empty()) {
133  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>";
134  errorMessage += "<ul><li>"+failed.join("</li><li>")+"</li></ul>";
135  KMessageBox::detailedError(m_editor, errorMessage, errorDetails.join("<hr/>"), i18n("Script Activation"));
136  }
137 }
138 
139 
140 void ScriptManager::addObject ( QObject * obj, const QString & name )
141 {
142  m_scriptObjects[name] = obj;
143 }
144 
145 
146 void ScriptManager::reloadScripts()
147 {
148  //deactivate (delete) all the active scripts
149  foreach ( Script * s, m_scripts )
150  {
151  if ( s ) delete s;
152  }
153  m_scripts.clear();
154 
155  //reload the scripts menu
156  m_editor->unplugActionList ( "scripts_actionlist" );
157  m_scriptActions.clear();
158  m_editor->plugActionList ( "scripts_actionlist",m_scriptActions );
159 
160  //load all the enabled scripts
161  loadScripts();
162 }
163 
164 
165 void ScriptManager::addScriptAction ( const QString & name, KAction * action )
166 {
167  //unplug action list (orelse it will add twice the same entries
168  m_editor->unplugActionList ( "scripts_actionlist" );
169 
170  //add to action collection
171  m_editor->actionCollection()->addAction ( name,action );
172 
173  //add it to actions menu list
174  m_editor->m_scriptManager->m_scriptActions.push_back ( action );
175 
176  //plug the action list
177  m_editor->plugActionList ( "scripts_actionlist",m_scriptActions );
178 
179 }
Script::addObjects
void addObjects(QMap< QString, QObject * > objects)
Adds more than one scripting Objects to the script.
Definition: script.cpp:119
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
ScriptManager::addObject
void addObject(QObject *obj, const QString &name)
Adds a QObject as a module for the script.
Definition: scriptmanager.cpp:140
ScriptManager::addScriptAction
void addScriptAction(const QString &name, KAction *action)
Add a KAction to the Scripts menu.
Definition: scriptmanager.cpp:165
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
QObject
ScriptManager::getScriptFileName
QString getScriptFileName(QString desktopFile)
Returns the full path to the script name given in the desktopFile.
Definition: scriptmanager.cpp:74
Script
This class represents the activated script and is used by the ScriptManager to activate/deactivate sc...
Definition: script.h:31
ScriptManager::reloadScripts
void reloadScripts()
Reloads all the scripts.
Definition: scriptmanager.cpp:146
ScriptManager::loadScripts
void loadScripts()
Loads (activates) all the available scripts and notifies the user if any script was not activated (du...
Definition: scriptmanager.cpp:113
Scripting::Parley
Parley scripting class (main entry point of a Parley Kross script)
Definition: parley.h:151
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:124
Editor::EditorWindow
Definition: editor.h:46
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
ScriptManager::disablePlugin
void disablePlugin(QString desktopFile)
Modify the parleyrc configuration so it disables the dektopFile plugin.
Definition: scriptmanager.cpp:101
scriptmanager.h
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 22:42:06 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
  • kstars
  • libkdeedu
  •   keduvocdocument
  • 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