Not crap after all...
[amarok.git] / src / scriptmanager.h
blob793c0ac41c946cac1f61f2a0c7cff68bf2dcfd58
1 /***************************************************************************
2 * Copyright (C) 2004-2007 by Mark Kretschmann <markey@web.de> *
3 * 2005 by Seb Ruiz <me@sebruiz.net> *
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 Steet, 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 "playlistwindow.h"
26 #include "ui_scriptmanagerbase.h"
28 #include <QMap>
30 #include <kdialog.h> //baseclass
31 #include <kurl.h>
33 class MetaBundle;
34 class KArchiveDirectory;
35 class K3Process;
36 class K3ProcIO;
39 /**
40 * @class ScriptManager
41 * @short Script management widget and backend
42 * @author Mark Kretschmann <markey@web.de>
44 * Script notifications, sent to stdin:
45 * configure
46 * engineStateChange: {empty|idle|paused|playing}
47 * trackChange
48 * volumeChange: newVolume (range: 0-100)
49 * fetchLyrics: artist title
50 * fetchLyricsByUrl: url
52 * @see http://amarok.kde.org/amarokwiki/index.php/Script-Writing_HowTo
55 class ScriptManager : public KDialog, public EngineObserver
57 Q_OBJECT
59 // friend class AmarokScriptNewStuff;
61 public:
62 explicit ScriptManager( QWidget *parent = 0, const char *name = 0 );
63 virtual ~ScriptManager();
65 static ScriptManager* instance() { return s_instance ? s_instance : new ScriptManager( PlaylistWindow::self() ); }
67 /**
68 * Runs the script with the given name. Used by the DCOP handler.
69 * @param name The name of the script.
70 * @return True if successful.
72 bool runScript( const QString& name, bool silent = false );
74 /**
75 * Stops the script with the given name. Used by the DCOP handler.
76 * @param name The name of the script.
77 * @return True if successful.
79 bool stopScript( const QString& name );
81 /** Returns a list of all currently running scripts. Used by the DCOP handler. */
82 QStringList listRunningScripts();
84 /** Custom Menu Click */
85 void customMenuClicked( const QString& message );
87 /** Returns the path of the spec file of the given script */
88 QString specForScript( const QString& name );
90 /** Return name of the lyrics script currently running, or QString::null if none */
91 QString lyricsScriptRunning() const;
93 /** Returns a list of all lyrics scripts */
94 QStringList lyricsScripts() const;
96 /** Sends a fetchLyrics notification to all scripts */
97 void notifyFetchLyrics( const QString& artist, const QString& title );
99 /** Sends a fetchLyrics notification to retrieve lyrics from a specific page */
100 void notifyFetchLyricsByUrl( const QString& url );
102 /** Return name of the transcode script currently running, or QString::null if none */
103 QString transcodeScriptRunning() const;
105 /** Sends a transcode notification to all scripts */
106 void notifyTranscode( const QString& srcUrl, const QString& filetype );
108 /** Return name of the scoring script currently running, or QString::null if none */
109 QString scoreScriptRunning() const;
111 /** Returns a list of all scoring scripts */
112 QStringList scoreScripts() const;
114 /** Asks the current score script to give a new score based on the parameters. */
115 void requestNewScore( const QString &url, double prevscore, int playcount, int length, float percentage, const QString &reason );
117 signals:
118 /** Emitted when the lyrics script changes, so that a lyrics retry can be made */
119 void lyricsScriptChanged();
121 private slots:
122 /** Finds all installed scripts and adds them to the listview */
123 void findScripts();
125 /** Enables/disables the buttons */
126 void slotCurrentChanged( QTreeWidgetItem* );
128 bool slotInstallScript( const QString& path = QString() );
129 void slotRetrieveScript();
130 void slotUninstallScript();
131 bool slotRunScript( bool silent = false );
132 void slotStopScript();
133 void slotConfigureScript();
134 void slotAboutScript();
135 void slotShowContextMenu( const QPoint& );
137 void slotReceivedStdout( K3Process*, char*, int );
138 void slotReceivedStderr( K3Process*, char*, int );
139 void scriptFinished( K3Process* process );
141 private:
142 /** Returns all scripts of the given \p type */
143 QStringList scriptsOfType( const QString &type ) const;
145 /** Returns the first running script found of \p type */
146 QString scriptRunningOfType( const QString &type ) const;
148 QString ensureScoreScriptRunning();
150 /** Terminates a process with SIGTERM and deletes the K3ProcIO object */
151 void terminateProcess( K3ProcIO** proc );
153 /** Sends a string message to all running scripts */
154 void notifyScripts( const QString& message );
156 /** Adds a script to the listview */
157 void loadScript( const QString& path );
159 /** Copies the file permissions from the tarball and loads the script */
160 void recurseInstall( const KArchiveDirectory* archiveDir, const QString& destination );
162 /** EngineObserver reimplementations **/
163 void engineStateChanged( Engine::State state, Engine::State oldState = Engine::Empty );
164 void engineNewMetaData( const MetaBundle& /*bundle*/, bool /*trackChanged*/ );
165 void engineVolumeChanged( int newVolume );
167 /////////////////////////////////////////////////////////////////////////////////////
168 // DATA MEMBERS
169 /////////////////////////////////////////////////////////////////////////////////////
170 static ScriptManager* s_instance;
171 Ui::ScriptManagerBase* m_gui;
173 QTreeWidgetItem* m_generalCategory;
174 QTreeWidgetItem* m_lyricsCategory;
175 QTreeWidgetItem* m_scoreCategory;
176 QTreeWidgetItem* m_transcodeCategory;
178 bool m_installSuccess;
180 struct ScriptItem {
181 KUrl url;
182 QString type;
183 K3ProcIO* process;
184 QTreeWidgetItem* li;
185 QString log;
186 ScriptItem() : process( 0 ), li( 0 ) {}
189 typedef QMap<QString, ScriptItem> ScriptMap;
191 ScriptMap m_scripts;
195 inline QStringList ScriptManager::lyricsScripts() const { return scriptsOfType( "lyrics" ); }
197 inline QString ScriptManager::lyricsScriptRunning() const { return scriptRunningOfType( "lyrics" ); }
199 inline QString ScriptManager::transcodeScriptRunning() const { return scriptRunningOfType( "transcode" ); }
201 inline QStringList ScriptManager::scoreScripts() const { return scriptsOfType( "score" ); }
203 inline QString ScriptManager::scoreScriptRunning() const { return scriptRunningOfType( "score" ); }
205 #endif /* AMAROK_SCRIPTMANAGER_H */