Revert previous commit, was incorrect
[amarok.git] / src / scriptmanager.h
blobce26ac715688275a10dd74a2ff1eaef582355c3c
1 /***************************************************************************
2 * Copyright (C) 2004-2007 by Mark Kretschmann <markey@web.de> *
3 * 2005 by Seb Ruiz <ruiz@kde.org> *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, write to the *
17 * Free Software Foundation, Inc., *
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
19 ***************************************************************************/
21 #ifndef AMAROK_SCRIPTMANAGER_H
22 #define AMAROK_SCRIPTMANAGER_H
24 #include "engineobserver.h" //baseclass
25 #include "MainWindow.h"
26 #include "ui_scriptmanagerbase.h"
28 #include <KDialog> //baseclass
29 #include <KUrl>
31 #include <QMap>
34 class MetaBundle;
35 class KArchiveDirectory;
36 class Process;
37 class ProcIO;
40 /**
41 * @class ScriptManager
42 * @short Script management widget and backend
43 * @author Mark Kretschmann <markey@web.de>
45 * Script notifications, sent to stdin:
46 * configure
47 * engineStateChange: {empty|idle|paused|playing}
48 * trackChange
49 * volumeChange: newVolume (range: 0-100)
50 * fetchLyrics: artist title
51 * fetchLyricsByUrl: url
53 * @see http://amarok.kde.org/amarokwiki/index.php/Script-Writing_HowTo
56 class AMAROK_EXPORT ScriptManager : public KDialog, public EngineObserver
58 Q_OBJECT
60 // friend class AmarokScriptNewStuff;
62 public:
63 explicit ScriptManager( QWidget *parent = 0, const char *name = 0 );
64 virtual ~ScriptManager();
66 static ScriptManager* instance() { return s_instance ? s_instance : new ScriptManager( MainWindow::self() ); }
68 /**
69 * Runs the script with the given name. Used by the DCOP handler.
70 * @param name The name of the script.
71 * @return True if successful.
73 bool runScript( const QString& name, bool silent = false );
75 /**
76 * Stops the script with the given name. Used by the DCOP handler.
77 * @param name The name of the script.
78 * @return True if successful.
80 bool stopScript( const QString& name );
82 /** Returns a list of all currently running scripts. Used by the DCOP handler. */
83 QStringList listRunningScripts();
85 /** Custom Menu Click */
86 void customMenuClicked( const QString& message );
88 /** Returns the path of the spec file of the given script */
89 QString specForScript( const QString& name );
91 /** Return name of the lyrics script currently running, or QString::null if none */
92 QString lyricsScriptRunning() const;
94 /** Returns a list of all lyrics scripts */
95 QStringList lyricsScripts() const;
97 /** Sends a fetchLyrics notification to all scripts */
98 void notifyFetchLyrics( const QString& artist, const QString& title );
100 /** Sends a fetchLyrics notification to retrieve lyrics from a specific page */
101 void notifyFetchLyricsByUrl( const QString& url );
103 /** Return name of the transcode script currently running, or QString::null if none */
104 QString transcodeScriptRunning() const;
106 /** Sends a transcode notification to all scripts */
107 void notifyTranscode( const QString& srcUrl, const QString& filetype );
109 /** Return name of the scoring script currently running, or QString::null if none */
110 QString scoreScriptRunning() const;
112 /** Returns a list of all scoring scripts */
113 QStringList scoreScripts() const;
115 /** Asks the current score script to give a new score based on the parameters. */
116 void requestNewScore( const QString &url, double prevscore, int playcount, int length, float percentage, const QString &reason );
118 signals:
119 /** Emitted when the lyrics script changes, so that a lyrics retry can be made */
120 void lyricsScriptChanged();
122 private slots:
123 /** Finds all installed scripts and adds them to the listview */
124 void findScripts();
126 /** Enables/disables the buttons */
127 void slotCurrentChanged( QTreeWidgetItem* );
129 bool slotInstallScript( const QString& path = QString() );
130 void slotRetrieveScript();
131 void slotUninstallScript();
132 bool slotRunScript( bool silent = false );
133 void slotStopScript();
134 void slotConfigureScript();
135 void slotAboutScript();
136 void slotShowContextMenu( const QPoint& );
138 void slotReceivedStdout( Process* );
139 void slotReceivedStderr( Process* );
140 void scriptFinished( Process* process );
142 private:
143 /** Returns all scripts of the given \p type */
144 QStringList scriptsOfType( const QString &type ) const;
146 /** Returns the first running script found of \p type */
147 QString scriptRunningOfType( const QString &type ) const;
149 QString ensureScoreScriptRunning();
151 /** Terminates a process with SIGTERM and deletes the ProcIO object */
152 void terminateProcess( ProcIO** proc );
154 /** Sends a string message to all running scripts */
155 void notifyScripts( const QString& message );
157 /** Adds a script to the listview */
158 void loadScript( const QString& path );
160 /** Copies the file permissions from the tarball and loads the script */
161 void recurseInstall( const KArchiveDirectory* archiveDir, const QString& destination );
163 /** EngineObserver reimplementations **/
164 void engineStateChanged( Engine::State state, Engine::State oldState = Engine::Empty );
165 void engineNewMetaData( const MetaBundle& /*bundle*/, bool /*trackChanged*/ );
166 void engineVolumeChanged( int newVolume );
168 /////////////////////////////////////////////////////////////////////////////////////
169 // DATA MEMBERS
170 /////////////////////////////////////////////////////////////////////////////////////
171 static ScriptManager* s_instance;
172 Ui::ScriptManagerBase* m_gui;
174 QTreeWidgetItem* m_generalCategory;
175 QTreeWidgetItem* m_lyricsCategory;
176 QTreeWidgetItem* m_scoreCategory;
177 QTreeWidgetItem* m_transcodeCategory;
178 QTreeWidgetItem* m_contextCategory;
180 bool m_installSuccess;
182 struct ScriptItem {
183 KUrl url;
184 QString type;
185 ProcIO* process;
186 QTreeWidgetItem* li;
187 QString log;
188 ScriptItem() : process( 0 ), li( 0 ) {}
191 typedef QMap<QString, ScriptItem> ScriptMap;
193 ScriptMap m_scripts;
197 inline QStringList ScriptManager::lyricsScripts() const { return scriptsOfType( "lyrics" ); }
199 inline QString ScriptManager::lyricsScriptRunning() const { return scriptRunningOfType( "lyrics" ); }
201 inline QString ScriptManager::transcodeScriptRunning() const { return scriptRunningOfType( "transcode" ); }
203 inline QStringList ScriptManager::scoreScripts() const { return scriptsOfType( "score" ); }
205 inline QString ScriptManager::scoreScriptRunning() const { return scriptRunningOfType( "score" ); }
207 #endif /* AMAROK_SCRIPTMANAGER_H */