1
/****************************************************************************************
2
 * Copyright (c) 2008 Alejandro Wainzinger <aikawarazuni@gmail.com>                     *
3
 *                                                                                      *
4
 * This program is free software; you can redistribute it and/or modify it under        *
5
 * the terms of the GNU General Public License as published by the Free Software        *
6
 * Foundation; either version 2 of the License, or (at your option) any later           *
7
 * version.                                                                             *
8
 *                                                                                      *
9
 * This program is distributed in the hope that it will be useful, but WITHOUT ANY      *
10
 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A      *
11
 * PARTICULAR PURPOSE. See the GNU General Public License for more details.             *
12
 *                                                                                      *
13
 * You should have received a copy of the GNU General Public License along with         *
14
 * this program.  If not, see <http://www.gnu.org/licenses/>.                           *
15
 ****************************************************************************************/
16
17
/*
18
19
Description:
20
21
The MediaDeviceMonitor connects to the MediaDeviceCache, monitoring the connection and disconnection of devices.  It tests
22
for devices known to Amarok, and if it finds them, sends a signal that the appropriate CollectionFactory is connected to,
23
which triggers the creation of the associated Collection.  Similar behaviour for when a device is disconnected.
24
25
All new MediaDeviceCollection-type classes must register their ConnectionAssistant of their device with this class, and have
26
it connect to the right signals to properly build/delete the associated Collection.  An example of this is seen in the
27
IpodCollectionFactory.
28
29
*/
30
31
#ifndef AMAROK_MEDIADEVICEMONITOR_H
32
#define AMAROK_MEDIADEVICEMONITOR_H
33
34
//#include "MediaDeviceInfo.h"
35
36
#include "amarok_export.h"
37
#include "core-impl/collections/mediadevicecollection/support/ConnectionAssistant.h"
38
#include "core/support/Debug.h"
39
40
#include <QHash>
41
#include <QList>
42
#include <QObject>
43
44
class ConnectionAssistant;
45
class MediaDeviceInfo;
46
47
class QStringList;
48
49
class AMAROK_EXPORT MediaDeviceMonitor : public QObject
50
{
51
    Q_OBJECT
52
53
    public:
54
55
    static MediaDeviceMonitor* instance() { return s_instance ? s_instance : new MediaDeviceMonitor(); }
56
57
    MediaDeviceMonitor();
58
    ~MediaDeviceMonitor();
59
60
    void init(); // connect to MediaDeviceCache
61
62
    QStringList getDevices(); // get list of devices
63
    /* TODO: checking for cd etc. goes in cdcollection dir now, needs porting
64
    void checkDevicesForCd();
65
66
    QString isCdPresent();
67
    void ejectCd( const QString &udi );
68
69
    
70
    QString currentCdId();
71
    void setCurrentCdId( const QString &id );
72
73
    signals:
74
75
        void audioCdDetected( const QString &udi );
76
77
    private:
78
        
79
        bool isAudioCd( const QString &udi );
80
81
        QString m_currentCdId;
82
*/
83
    /// Get assistant for a given udi
84
85
    ConnectionAssistant* getUdiAssistant( const QString &udi )
86
    {
87
        return m_udiAssistants[ udi ];
88
    }
89
90
    /**
91
92
    registerDeviceType adds the device type described by @param assistant to the list
93
    of known device types by the MDM, and then checks the list of known devices
94
    for a match with this type
95
96
    */
97
    void registerDeviceType( ConnectionAssistant *assistant );
98
99
    public slots:
100
101
    /**
102
103
    checkDevice checks if @param udi is a known device
104
    and if so attempts to connect it
105
106
    checkOneDevice runs an identify check using the given
107
    assistant and udi
108
109
    checkDevicesFor checks if the device type described
110
    by @param assistant matches any of the udi's in the
111
    MediaDeviceCache, and if so, attempts to connect to
112
    it
113
114
    */
115
116
    void checkDevice( const QString &udi );
117
    void checkOneDevice( ConnectionAssistant* assistant, const QString& udi );
118
    void checkDevicesFor( ConnectionAssistant* assistant );
119
120
121
    signals:
122
        void deviceDetected( const MediaDeviceInfo &deviceinfo );
123
        void deviceRemoved( const QString &udi );
124
125
126
    private slots:
127
128
        void deviceAdded( const QString &udi );
129
        void slotDeviceRemoved( const QString &udi );
130
        void slotAccessibilityChanged( bool accessible, const QString & udi );
131
	
132
	void slotDequeueWaitingAssistant();
133
134
135
    private:
136
137
        static MediaDeviceMonitor* s_instance;
138
139
        // keeps track of which CA to contact for which udi
140
        QHash<QString,ConnectionAssistant*> m_udiAssistants;
141
        // holds all registered assistants
142
        QList<ConnectionAssistant*> m_assistants;
143
	// holds all waiting assistants
144
        QList<ConnectionAssistant*> m_waitingassistants;
145
	// holds index of next waiting assistant to check
146
	// devices with, during initialization of device
147
	// factories
148
	int m_nextassistant;
149
150
151
};
152
153
#endif /* AMAROK_MEDIADEVICEMONITOR_H */