File indexing completed on 2024-05-19 15:53:26

0001 /****************************************************************************************
0002  * Copyright (c) 2004-2010 Mark Kretschmann <kretschmann@kde.org>                       *
0003  * Copyright (c) 2005 Seb Ruiz <ruiz@kde.org>                                           *
0004  * Copyright (c) 2008 Peter ZHOU <peterzhoulei@gmail.com>                               *
0005  *                                                                                      *
0006  * This program is free software; you can redistribute it and/or modify it under        *
0007  * the terms of the GNU General Public License as published by the Free Software        *
0008  * Foundation; either version 2 of the License, or (at your option) any later           *
0009  * version.                                                                             *
0010  *                                                                                      *
0011  * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
0012  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
0013  * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
0014  *                                                                                      *
0015  * You should have received a copy of the GNU General Public License along with         *
0016  * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
0017  ****************************************************************************************/
0018 
0019 #ifndef AMAROK_SCRIPTMANAGER_H
0020 #define AMAROK_SCRIPTMANAGER_H
0021 
0022 #include "amarok_export.h"
0023 #include "core/meta/forward_declarations.h"
0024 #include "ScriptItem.h"
0025 
0026 #include <KPluginInfo>
0027 #include <KPluginMetaData>
0028 #include <QUrl>
0029 
0030 #include <QSemaphore>
0031 
0032 namespace AmarokScript {
0033     class AmarokScript;
0034 }
0035 class QJSValue;
0036 
0037 class AMAROK_EXPORT ScriptManager : public QObject
0038 {
0039     Q_OBJECT
0040 
0041     public:
0042         static ScriptManager* instance();
0043         static void destroy();
0044         /** Reads plugin info from legacy .desktop format */
0045         static KPluginMetaData createMetadaFromSpec( const QString &specPath );
0046 
0047         /**
0048          * Runs the script with the given name.
0049          * @param name The pluginName of the script.
0050          * @param silent Whether to suppress the script output.
0051          * @return True if successful.
0052          */
0053         bool runScript( const QString& name, bool silent = false );
0054 
0055         /**
0056          * Stops the script with the given name.
0057          * @param name The name of the script.
0058          * @return True if successful.
0059          */
0060         bool stopScript( const QString& name );
0061 
0062         void configChanged( bool changed );
0063 
0064         KPluginInfo::List scripts( const QString &category ) const;
0065 
0066         /** Returns a list of all currently running scripts. Used by the DCOP handler. */
0067         QStringList listRunningScripts() const;
0068 
0069         /** Returns the path of the spec file of the given script */
0070         QString specForScript( const QString& name ) const;
0071 
0072         /** Returns whether or not there is a lyrics script running */
0073         bool lyricsScriptRunning() const;
0074 
0075         QString scriptNameForEngine( const QJSEngine *engine ) const;
0076 
0077         /** Notifies any running lyric scripts to fetch desired lyric from given URL */
0078         void notifyFetchLyrics( const QString& artist, const QString& title, const QString& url, const Meta::TrackPtr &track );
0079 
0080         void ServiceScriptPopulate( const QString &name,
0081                                     int level,
0082                                     int parent_id,
0083                                     const QString &callbackString,
0084                                     const QString &filter );
0085 
0086         void ServiceScriptRequestInfo( const QString &name, int level, const QString &callbackString );
0087 
0088         void ServiceScriptCustomize( const QString &name );
0089 
0090         typedef QHash<QString, ScriptItem*> ScriptMap;
0091         ScriptMap      m_scripts;
0092         QString        m_lyricsScript;
0093 
0094     public Q_SLOTS:
0095         /** Finds installed scripts, updates them, and loads them */
0096         void updateAllScripts();
0097 
0098     Q_SIGNALS:
0099         // needed so the lyrics script can connect to this
0100         void fetchLyrics( const QString&, const QString&, const QString& url, Meta::TrackPtr );
0101         void lyricsScriptStarted();
0102 
0103         /**
0104          * Emitted when a script is added, removed or updated
0105          */
0106         void scriptsChanged();
0107 
0108     private Q_SLOTS:
0109         bool slotRunScript( const QString &name, bool silent = false );
0110         void handleException( const QJSValue &value );
0111 
0112         void updaterFinished( const QString &scriptPath );
0113         void slotConfigChanged();
0114 
0115     private:
0116         explicit ScriptManager( QObject* parent );
0117         ~ScriptManager() override;
0118 
0119         /// \return false if loadScript failed.
0120         bool loadScript( const QString& path );
0121 
0122         /////////////////////////////////////////////////////////////////////////////////////
0123         // DATA MEMBERS
0124         /////////////////////////////////////////////////////////////////////////////////////
0125         static ScriptManager*  s_instance;
0126 
0127         bool           m_configChanged;
0128         QStringList    m_changedScripts;
0129 
0130         // count returning ScriptUpdaters in a thread-safe way
0131         QSemaphore     m_updateSemaphore;
0132         // memorize how many scripts were found and tried to be updated
0133         int            m_nScripts;
0134 
0135 };
0136 
0137 #endif /* AMAROK_SCRIPTMANAGER_H */