summaryrefslogtreecommitdiffstats
path: root/amarok/src/scriptmanager.h
blob: 948ef0cd88920e567fa5fb17fdafeaaa5d58dbda (plain)
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
/***************************************************************************
 *   Copyright (C) 2004-2006 by Mark Kretschmann <markey@web.de>           *
 *                      2005 by Seb Ruiz <me@sebruiz.net>                  *
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 *   This program is distributed in the hope that it will be useful,       *
 *   but WITHOUT ANY WARRANTY; without even the implied warranty of        *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         *
 *   GNU General Public License for more details.                          *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this program; if not, write to the                         *
 *   Free Software Foundation, Inc.,                                       *
 *   51 Franklin Steet, Fifth Floor, Boston, MA  02110-1301, USA.          *
 ***************************************************************************/

#ifndef AMAROK_SCRIPTMANAGER_H
#define AMAROK_SCRIPTMANAGER_H

#include "engineobserver.h"   //baseclass
#include "playlistwindow.h"

#include <tqmap.h>

#include <kdialogbase.h>      //baseclass
#include <kurl.h>

class MetaBundle;
class ScriptManagerBase;
class TQListViewItem;
class KArchiveDirectory;
class TDEProcess;
class KProcIO;


/**
 * @class ScriptManager
 * @short Script management widget and backend
 * @author Mark Kretschmann <markey@web.de>
 *
 * Script notifications, sent to stdin:
 *   configure
 *   engineStateChange: {empty|idle|paused|playing}
 *   trackChange
 *   volumeChange: newVolume (range: 0-100)
 *   fetchLyrics: artist title
 *   fetchLyricsByUrl: url
 *
 * @see http://amarok.kde.org/amarokwiki/index.php/Script-Writing_HowTo
 */

class ScriptManager : public KDialogBase, public EngineObserver
{
    Q_OBJECT
  

    friend class AmarokScriptNewStuff;

    public:
        ScriptManager( TQWidget *parent = 0, const char *name = 0 );
        virtual ~ScriptManager();

        static ScriptManager* instance() { return s_instance ? s_instance : new ScriptManager( PlaylistWindow::self() ); }

        /**
         * Runs the script with the given name. Used by the DCOP handler.
         * @param name The name of the script.
         * @return True if successful.
         */
        bool runScript( const TQString& name, bool silent = false );

        /**
         * Stops the script with the given name. Used by the DCOP handler.
         * @param name The name of the script.
         * @return True if successful.
         */
        bool stopScript( const TQString& name );

        /** Returns a list of all currently running scripts. Used by the DCOP handler. */
        TQStringList listRunningScripts();

       /** Custom Menu Click */
       void customMenuClicked( const TQString& message );

       /** Returns the path of the spec file of the given script */
       TQString specForScript( const TQString& name );

       /** Return name of the lyrics script currently running, or TQString() if none */
       TQString lyricsScriptRunning() const;

       /** Returns a list of all lyrics scripts */
       TQStringList lyricsScripts() const;

       /** Sends a fetchLyrics notification to all scripts */
       void notifyFetchLyrics( const TQString& artist, const TQString& title );

       /** Sends a fetchLyrics notification to retrieve lyrics from a specific page */
       void notifyFetchLyricsByUrl( const TQString& url );

       /** Sends a playlistChange notification to all scripts */
       void notifyPlaylistChange( const TQString& change );

       /** Return name of the transcode script currently running, or TQString() if none */
       TQString transcodeScriptRunning() const;

       /** Sends a transcode notification to all scripts */
       void notifyTranscode( const TQString& srcUrl, const TQString& filetype );

       /** Return name of the scoring script currently running, or TQString() if none */
       TQString scoreScriptRunning() const;

       /** Returns a list of all scoring scripts */
       TQStringList scoreScripts() const;

        /** Asks the current score script to give a new score based on the parameters. */
       void requestNewScore( const TQString &url, double prevscore, int playcount, int length, float percentage, const TQString &reason );

    signals:
        /** Emitted when the lyrics script changes, so that a lyrics retry can be made */
        void lyricsScriptChanged();

    private slots:
        /** Finds all installed scripts and adds them to the listview */
        void findScripts();

        /** Enables/disables the buttons */
        void slotCurrentChanged( TQListViewItem* );

        bool slotInstallScript( const TQString& path = TQString() );
        void slotRetrieveScript();
        void slotUninstallScript();
        bool slotRunScript( bool silent = false );
        void slotStopScript();
        void slotConfigureScript();
        void slotAboutScript();
        void slotShowContextMenu( TQListViewItem*, const TQPoint& );

        void slotReceivedStdout( TDEProcess*, char*, int );
        void slotReceivedStderr( TDEProcess*, char*, int );
        void scriptFinished( TDEProcess* process );

    private:
        /** Returns all scripts of the given \p type */
        TQStringList scriptsOfType( const TQString &type ) const;

        /** Returns the first running script found of \p type */
        TQString scriptRunningOfType( const TQString &type ) const;

        TQString ensureScoreScriptRunning();

        /** Terminates a process with SIGTERM and deletes the KProcIO object */
        void terminateProcess( KProcIO** proc );

        /** Sends a string message to all running scripts */
        void notifyScripts( const TQString& message );

        /** Adds a script to the listview */
        void loadScript( const TQString& path );

        /** Copies the file permissions from the tarball and loads the script */
        void recurseInstall( const KArchiveDirectory* archiveDir, const TQString& destination );

        /** EngineObserver reimplementations **/
        void engineStateChanged( Engine::State state, Engine::State oldState = Engine::Empty );
        void engineNewMetaData( const MetaBundle& /*bundle*/, bool /*trackChanged*/ );
        void engineVolumeChanged( int newVolume );

        /////////////////////////////////////////////////////////////////////////////////////
        // DATA MEMBERS
        /////////////////////////////////////////////////////////////////////////////////////
        static ScriptManager* s_instance;
        ScriptManagerBase*    m_gui;

        TQListViewItem*        m_generalCategory;
        TQListViewItem*        m_lyricsCategory;
        TQListViewItem*        m_scoreCategory;
        TQListViewItem*        m_transcodeCategory;

        bool                  m_installSuccess;

        struct ScriptItem {
            KURL           url;
            TQString        type;
            KProcIO*       process;
            TQListViewItem* li;
            TQString        log;
            ScriptItem() : process( 0 ), li( 0 ) {}
        };

        typedef TQMap<TQString, ScriptItem> ScriptMap;

        ScriptMap m_scripts;
};


inline TQStringList ScriptManager::lyricsScripts() const { return scriptsOfType( "lyrics" ); }

inline TQString ScriptManager::lyricsScriptRunning() const { return scriptRunningOfType( "lyrics" ); }

inline TQString ScriptManager::transcodeScriptRunning() const { return scriptRunningOfType( "transcode" ); }

inline TQStringList ScriptManager::scoreScripts() const { return scriptsOfType( "score" ); }

inline TQString ScriptManager::scoreScriptRunning() const { return scriptRunningOfType( "score" ); }

#endif /* AMAROK_SCRIPTMANAGER_H */