File indexing completed on 2024-04-21 15:55:47

0001 /**************************************************************************
0002 *   Copyright (C) 2006-2020 by Michel Ludwig (michel.ludwig@kdemail.net)  *
0003 ***************************************************************************/
0004 
0005 /**************************************************************************
0006 *                                                                         *
0007 *   This program is free software; you can redistribute it and/or modify  *
0008 *   it under the terms of the GNU General Public License as published by  *
0009 *   the Free Software Foundation; either version 2 of the License, or     *
0010 *   (at your option) any later version.                                   *
0011 *                                                                         *
0012 ***************************************************************************/
0013 
0014 #ifndef SCRIPTMANAGER_H
0015 #define SCRIPTMANAGER_H
0016 
0017 #include <QList>
0018 #include <QMap>
0019 #include <QObject>
0020 
0021 #include <QAction>
0022 #include <KActionCollection>
0023 #include <KConfig>
0024 #include <KDirWatch>
0025 
0026 #include <KTextEditor/Range>
0027 #include <KTextEditor/Cursor>
0028 
0029 #include "scripting/kilescriptobject.h"
0030 #include "scripting/kilescriptview.h"
0031 #include "scripting/kilescriptdocument.h"
0032 
0033 class KileInfo;
0034 
0035 namespace KileScript {
0036 
0037 class Script;
0038 
0039 /**
0040  * This class handles the scripting functionality in Kile.
0041  **/
0042 class Manager : public QObject {
0043     Q_OBJECT
0044 
0045 public:
0046     /**
0047      * Constructs a new Manager object.
0048      **/
0049     Manager(KileInfo *info, KConfig *config, KActionCollection *actionCollection, QObject *parent = 0, const char *name = 0);
0050     virtual ~Manager();
0051 
0052     /**
0053      * Executes a script in Kile's scripting environment.
0054      * @param script the script that should be executed
0055      **/
0056     void executeScript(const Script *script);
0057 
0058     /**
0059      * Executes a script in Kile's scripting environment.
0060      * @param id the id of the script that should be executed
0061      **/
0062     void executeScript(unsigned int id);
0063 
0064     /**
0065      * Retrieves a list of all the scripts that are currently available.
0066      **/
0067     QList<Script*> getScripts();
0068 
0069     /**
0070      * Writes the key sequence-to-script bindings to the KConfig object that has
0071      * passed in the constructor.
0072      **/
0073     void writeConfig();
0074 
0075     /**
0076      * Assigns a key sequence to a script. If the parameter "keySequence" is empty,
0077      * then nothing is done.
0078      * @param script the script that is considered
0079      * @param keySequence the key sequence that is assigned
0080      **/
0081     void setEditorKeySequence(Script* script, int type, const QString& keySequence);
0082 
0083     /**
0084      * Assigns a shortcut to a script. If the parameter "keySequence" is empty,
0085      * then nothing is done.
0086      * @param script the script that is considered
0087      * @param keySequence the shortcut that is assigned
0088      **/
0089     void setShortcut(Script* script, const QKeySequence& keySequence);
0090 
0091     /**
0092      * Removes an assigned key sequence from a script.
0093      * @param script the script that is considered
0094      **/
0095     void removeEditorKeySequence(Script* script);
0096 
0097     /**
0098      * Returns the directory that can be used by the used to store Kile's scripts.
0099      * Usually $HOME/.kde/share/apps/kile/scripts
0100      **/
0101     QString getLocalScriptDirectory() const;
0102 
0103     /**
0104      * Returns the script object that corresponds to a script id.
0105      * @param id the id of the script
0106      **/
0107     const Script* getScript(unsigned int id);
0108 
0109     void initScriptActions();
0110 
0111 Q_SIGNALS:
0112     /**
0113      * Signal emitted whenever the managed scripts haved changed, for example if the
0114      * watched directories have been scanned for scripts and thus, the potentially
0115      * available scripts (could) have changed.
0116      * The signal is also emitted when the currently available scripts have been
0117      * deleted internally in Kile (for example, after disabling the scripting feature).
0118      **/
0119     void scriptsChanged();
0120 
0121 public Q_SLOTS:
0122     /**
0123      * Does nothing if scripting has been disabled.
0124      **/
0125     void scanScriptDirectories();
0126 
0127     /**
0128      * Reads and assigns the key sequence-to-script bindings from the KConfig
0129      * object that has been passed in the constructor.
0130      **/
0131     void readConfig();
0132 
0133 protected:
0134     QString m_localScriptDir;
0135     QList<Script*> m_jScriptList;
0136     QMap<unsigned int, Script*> m_idScriptMap;
0137     KDirWatch *m_jScriptDirWatch;
0138 
0139     KileInfo *m_kileInfo;
0140     KConfig *m_config;
0141     KActionCollection *m_actionCollection;
0142 
0143     /**
0144      * Registers the script contained in a file.
0145      * @param fileName the file that contains the script
0146      **/
0147     void registerScript(const QString& fileName, QMap<QString, unsigned int>& pathIDMap, QMap<unsigned int, bool>& takenIDMap, unsigned int &maxID);
0148 
0149     /**
0150      * (Re-)Creates and initialises the KDirWatch object.
0151      **/
0152     void populateDirWatch();
0153 
0154     /**
0155      * Deletes all the scripts that are handled by this manager.
0156      **/
0157     void deleteScripts();
0158 
0159     /**
0160      * Finds the next free ID.
0161      * @param takenIDMap map describing which IDs are already in use
0162      * @param maxID the maximum ID that is currently in use (if there is no ID assigned, then
0163      *              any value can be passed here, but typically '0')
0164      **/
0165     unsigned int findFreeID(const QMap<unsigned int, bool>& takenIDMap, unsigned int maxID);
0166 
0167     /**
0168      * Writes the ID to file name mappings that are currently in use to the local
0169      * KConfig object.
0170      **/
0171     void writeIDs();
0172 
0173     /**
0174      * read the plugin to initialize Cursor and Range objects
0175      **/
0176     void readEnginePlugin();
0177 
0178 private:
0179     /**
0180      * Recursively adds a directory to a KDirWatch object.
0181      * @param dir the directory that should be added
0182      **/
0183     void addDirectoryToDirWatch(const QString& dir);
0184 
0185     KileScriptObject *m_kileScriptObject;
0186     KileScriptView *m_kileScriptView;
0187     KileScriptDocument *m_kileScriptDocument;
0188 
0189     QString m_enginePlugin;
0190     QMap<QString,QAction *> *m_scriptActionMap;
0191 };
0192 
0193 class ScriptExecutionAction : public QAction {
0194     Q_OBJECT
0195 
0196 public:
0197     ScriptExecutionAction(unsigned int scriptID, Manager *manager, QObject* parent = 0);
0198 
0199     virtual ~ScriptExecutionAction();
0200 
0201 protected Q_SLOTS:
0202     void executeScript();
0203 
0204 protected:
0205     KileScript::Manager *m_manager;
0206     unsigned int m_id;
0207 };
0208 
0209 }
0210 
0211 
0212 
0213 
0214 #endif
0215