File: PluginManager.h

package info (click to toggle)
kwave 25.04.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 23,272 kB
  • sloc: cpp: 56,173; xml: 817; perl: 688; sh: 57; makefile: 11
file content (387 lines) | stat: -rw-r--r-- 13,489 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
/***************************************************************************
            PluginManager.h  -  manager class for kwave's plugins
                             -------------------
    begin                : Sun Aug 27 2000
    copyright            : (C) 2000 by Thomas Eschenbacher
    email                : Thomas.Eschenbacher@gmx.de
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

#ifndef PLUGIN_MANAGER_H
#define PLUGIN_MANAGER_H

#include "config.h"
#include "libkwave_export.h"

#include <QtGlobal>
#include <QList>
#include <QListIterator>
#include <QMap>
#include <QMutableListIterator>
#include <QObject>
#include <QPointer>
#include <QWidget>

#include "libkwave/InsertMode.h"
#include "libkwave/Sample.h"
#include "libkwave/ViewManager.h"

class QString;

class KPluginFactory;

namespace Kwave
{
    class PlaybackController;
    class PlayBackParam;
    class Plugin;
    class SampleSink;
    class SignalManager;

    /**
     * Manages the loading, initializing, starting, running and closing
     * of the plugins of kwave. Each instance of a TopWidget creates a
     * new instance of the PluginManager to be independent from other
     * toplevel widgets.
     */
    class LIBKWAVE_EXPORT PluginManager: public QObject
    {
        Q_OBJECT

    public:

        /**
         * Constructor.
         * @param parent reference to the toplevel widget (our parent)
         * @param signal_manager reference to a SignalManager
         */
        PluginManager(QWidget *parent, Kwave::SignalManager &signal_manager);

        /**
         * Default destructor
         */
        ~PluginManager() override;

        /**
         * Tries to load all plugins. If a pesistent plugin is found,
         * it will stay loaded in memory, all other (non-persistent)
         * plugins will be unloaded afterwards. This also filters out
         * all plugins that do not correctly load.
         * @internal used once by each toplevel window at startup
         * @return true if at least one plugin was loaded, false if none
         */
        bool loadAllPlugins();

        /**
         * Stops all currently running plugins
         */
        void stopAllPlugins();

        /**
         * Executes a plugin in the context of a given parent widget.
         * @param name the name of the plugin
         * @param params pointer to a parameter list
         *               or null if defaults should be used
         * @return zero on success or negative error code
         */
        int executePlugin(const QString &name, QStringList *params);

        /**
         * Returns true if there is no running plugin that blocks
         * a "close" operation.
         */
        bool canClose();

        /** Returns true if at least one plugin is currently running */
        bool onePluginRunning();

        /**
         * Waits until all currently running actions have completed.
         */
        void sync();

        /**
         * Loads a plugin, calls it's setup function and then closes
         * it again. The parameters will be loaded before the setup
         * and saved if the setup has not been aborted.
         * @param name the name of the plugin
         * @param params pointer to a parameter list
         *               or null if defaults should be used
         * @retval  0 if succeeded and accepted
         * @retval  1 if canceled
         * @retval -1 if failed
         */
        int setupPlugin(const QString &name, const QStringList &params);

        /**
         * loads a plugin's default parameters from the user's
         * configuration file. If nothing is found in the config file,
         * the return value will be 0. If the current version number of
         * the plugin does not match the version number in the config file,
         * the return value will also be 0.
         * @param name the name of the plugin
         * @return list of strings
         */
        QStringList defaultParams(const QString &name);

        /**
         * Returns the length of the current signal in samples.
         * If no signal is present the return value will be 0.
         */
        sample_index_t signalLength();

        /**
         * Returns the current sample rate in samples per second.
         * If no signal is present the return value will be 0.
         */
        double signalRate();

        /**
         * Returns the start of the selection. If nothing is currently
         * selected this will be the first sample (0).
         */
        sample_index_t selectionStart();

        /**
         * Returns the end of the selection. If nothing is currently
         * selected this will be the last sample (length-1).
         */
        sample_index_t selectionEnd();

        /**
         * Sets the current start and length of the selection to new values.
         * @param offset index of the first sample
         * @param length number of samples
         */
        void selectRange(sample_index_t offset, sample_index_t length);

        /**
         * Opens a Kwave::MultiTrackSink for playback purposes.
         * @param tracks number of tracks
         * @param playback_params points to a class that holds all playback
         *                        parameters. If null, the default parameters
         *                        of the current signal will be used
         * @return a multitrack sink that receives the playback stream
         */
        Kwave::SampleSink *openMultiTrackPlayback(
            unsigned int tracks,
            const Kwave::PlayBackParam *playback_params = nullptr
        );

        /**
         * Returns a reference to the current playback controller. This is
         * only needed for plugins doing playback.
         */
        Kwave::PlaybackController &playbackController();

        /**
         * assigns a new parent widget, to be used for messages
         * @param new_parent pointer to a QWidget
         */
        inline void setParentWidget(QWidget *new_parent) {
            m_parent_widget = new_parent;
        }

        /** returns a pointer to the parent widget */
        inline QPointer<QWidget> parentWidget()
        {
            return m_parent_widget;
        }

        /** returns a reference to our signal manager */
        inline Kwave::SignalManager &signalManager()
        {
            return m_signal_manager;
        }

        /**
         * Insert a new signal view into this widget (or the upper/lower
         * dock area.
         * @param view the signal view, must not be a null pointer
         * @param controls a widget with controls, optionally, can be null
         */
        void insertView(Kwave::SignalView *view, QWidget *controls);

        /**
         * registers a view manager, must only be called once!
         */
        void registerViewManager(Kwave::ViewManager *view_manager);

        /**
         * Enqueues a command that will be processed threadsafe in the X11
         * thread.
         */
        void enqueueCommand(const QString &command);

        /**
         * Searches the standard KDE data directories for plugins (through
         * the KDE's standard search algorithm) and creates a map of
         * plugin names and file names. First it collects a list of
         * filenames and then filters it to sort out invalid entries.
         */
        void searchPluginModules();

        /** structure with information about a plugin */
        typedef struct  {
            QString            m_name;        /**< name of the plugin   */
            QString            m_author;      /**< name of the author   */
            QString            m_description; /**< short description    */
            QString            m_version;     /**< settings version     */
            KPluginFactory    *m_factory;     /**< plugin factory       */
            int                m_use_count;   /**< usage counter        */
        } PluginModule;

        /**
         * returns a list with info of all known plugins
         * @todo rename to pluginModuleList
         */
        const QList<PluginModule> pluginInfoList() const;

        /**
         * Migrate a plugin to the currently active file context (which
         * might be different from the one that is currently executing
         * the plugin). The plugin will be removed from our lists and
         * inserted into the currently active plugin manager instance.
         * @param plugin the plugin to migrate
         */
        void migratePluginToActiveContext(Kwave::Plugin *plugin);

        /** Let this instance be the active one */
        void setActive() { m_active_instance = this; }

    signals:

        /**
         * Forwards commands to the parent TopWidget execute a command
         */
        void sigCommand(const QString &command);

        /**
         * Informs all plugins and client windows that we close down
         */
        void sigClosed();

        /**
         * Informs the plugins that the name of the signal has changed.
         * This might be used to update the caption of a window.
         */
        void sigSignalNameChanged(const QString &name);

        /**
         * informs about progress, e.g. for showing a message in
         * a splash screen or status bar.
         */
        void sigProgress(const QString &message);

    public slots:

        /**
         * Notify all plugins that the signal or file is to be closed
         */
        void signalClosed();

        /**
         * Called if the name of the current signal has changed. This will be
         * forwarded to all plugins by emitting the signal sigSignalNameChanged.
         * @see sigSignalNameChanged()
         */
        void setSignalName(const QString &name);

    private slots:

        /**
         * Will be connected to the plugin's "closed" signal.
         * @param p pointer to the plugin to be closed
         */
        void pluginClosed(Kwave::Plugin *p);

        /** called when a plugin has started (running) it's worker thread */
        void pluginStarted(Kwave::Plugin *p);

        /** called when a plugin has finished it's worker thread */
        void pluginDone(Kwave::Plugin *p);

    private:

        /** typedef: QPointer to a Kwave::Plugin */
        typedef QPointer<Kwave::Plugin> KwavePluginPointer;

        /** typedef: list of pointers to kwave plugins */
        typedef QList< KwavePluginPointer > PluginList;

        /** typedef: mutable iterator for PluginList */
        typedef QMutableListIterator< KwavePluginPointer >
            PluginListMutableIterator;

        /** typedef: const iterator for PluginList */
        typedef QListIterator< KwavePluginPointer >
            PluginListIterator;

    private:

        /**
         * Creates an instance of a plugin.
         * @param name the name of the plugin (filename)
         * @return pointer to the loaded plugin or zero if the
         *         plugin was not found or invalid
         */
        Kwave::Plugin *createPluginInstance(const QString &name);

        /**
         * Saves a plugin's default parameters to the user's configuration
         * file. The whole section in the configuration file will be deleted
         * before saving the new settings in order to wipe out invalid
         * entries and settings that belong to an older version.
         * @param name the name of the plugin
         * @param params a list of configuration strings
         */
        void savePluginDefaults(const QString &name,
                                QStringList &params);

        /** connects all signals of and for a plugin */
        void connectPlugin(Kwave::Plugin *plugin);

        /** connects all signals from and to a plugin */
        void disconnectPlugin(Kwave::Plugin *plugin);

    private:

        /** pointer to the currently active instance */
        static Kwave::PluginManager *m_active_instance;

        /**
         * map with plugin information: key = short name of the plugin,
         * data = plugin info (description, author, version etc...)
         */
        static QMap<QString, PluginModule> m_plugin_modules;

        /** list of all plugins that were loaded by this instance */
        PluginList m_plugin_instances;

        /** list of currently running plugins */
        PluginList m_running_plugins;

        /** reference to our parent toplevel widget */
        QPointer<QWidget> m_parent_widget;

        /** reference to our signal manager */
        Kwave::SignalManager &m_signal_manager;

        /** interface for registering a SignalView */
        ViewManager *m_view_manager;

    };
}

#endif /* PLUGIN_MANAGER_H */

//***************************************************************************
//***************************************************************************