1
/***************************************************************************
2
                      EngineObserver.h  -  Observer pattern for engine
3
                         -------------------
4
begin                : Mar 14 2003
5
copyright            : (C) 2003 by Frederik Holljen
6
email                : fh@ez.no
7
***************************************************************************/
8
9
/***************************************************************************
10
 *                                                                         *
11
 *   This program is free software; you can redistribute it and/or modify  *
12
 *   it under the terms of the GNU General Public License as published by  *
13
 *   the Free Software Foundation; either version 2 of the License, or     *
14
 *   (at your option) any later version.                                   *
15
 *                                                                         *
16
 ***************************************************************************/
17
18
#ifndef AMAROK_ENGINEOBSERVER_H
19
#define AMAROK_ENGINEOBSERVER_H
20
21
#include "amarok_export.h"
22
23
#include <Phonon/MediaObject>
24
#include <QHash>
25
#include <QSet>
26
27
class EngineSubject;
28
class QString;
29
30
/**
31
 * If you want to observe the engine, inherit from this class.
32
 * Note that all positional information and times are in milliseconds
33
 */
34
class AMAROK_EXPORT EngineObserver
35
{
36
public:
37
    enum PlaybackEndedReason
38
    {
39
        EndedStopped = 0,
40
        EndedQuit    = 1
41
    };
42
43
    EngineObserver( EngineSubject* );
44
    virtual ~EngineObserver();
45
    virtual void engineStateChanged( Phonon::State currentState, Phonon::State oldState = Phonon::StoppedState );
46
    virtual void enginePlaybackEnded( int finalPosition, int trackLength, PlaybackEndedReason reason );
47
    virtual void engineNewTrackPlaying();
48
    virtual void engineNewMetaData( const QHash<qint64, QString> &newMetaData, bool trackChanged );
49
    virtual void engineVolumeChanged( int percent );
50
    virtual void engineMuteStateChanged( bool mute );
51
    virtual void engineTrackPositionChanged( long position, bool userSeek );
52
    virtual void engineTrackLengthChanged( long seconds );
53
54
private:
55
    EngineSubject *m_subject;
56
};
57
58
/**
59
 * Inherited by EngineController.
60
 * Notify observer functionality is captured in this class.
61
 */
62
class EngineSubject
63
{
64
    friend class EngineObserver;
65
66
protected:
67
    EngineSubject();
68
    virtual ~EngineSubject();
69
    void stateChangedNotify( Phonon::State newState, Phonon::State oldState );
70
    void playbackEnded( int /*finalPosition*/, int /*trackLength*/, EngineObserver::PlaybackEndedReason reason );
71
    void newMetaDataNotify( const QHash<qint64, QString> &newMetaData, bool trackChanged );
72
    void volumeChangedNotify( int /*percent*/ );
73
    void muteStateChangedNotify( bool /*mute*/ );
74
    /* userSeek means the position didn't change due to normal playback */
75
    void trackPositionChangedNotify( long /*position*/ , bool userSeek=false );
76
    void trackLengthChangedNotify( long /*seconds*/ );
77
    void newTrackPlaying() const;
78
79
private:
80
    void attach( EngineObserver *observer );
81
    void detach( EngineObserver *observer );
82
    bool isMetaDataSpam( QHash<qint64, QString> newMetaData );
83
84
    QList<QHash<qint64, QString> > m_metaDataHistory;
85
    QSet<EngineObserver*> Observers;
86
    Phonon::State m_realState; // To work around the buffering issue
87
};
88
89
#endif // AMAROK_ENGINEOBSERVER_H