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 "amarok_export.h"
35 
36 #include <QHash>
37 #include <QList>
38 #include <QObject>
39 
40 class ConnectionAssistant;
41 class MediaDeviceInfo;
42 
43 class QStringList;
44 
45 class AMAROK_EXPORT MediaDeviceMonitor : public QObject
46 {
47     Q_OBJECT
48 
49     public:
50 
instance()51     static MediaDeviceMonitor* instance() { return s_instance ? s_instance : new MediaDeviceMonitor(); }
52 
53     MediaDeviceMonitor();
54     ~MediaDeviceMonitor() override;
55 
56     void init(); // connect to MediaDeviceCache
57 
58     QStringList getDevices(); // get list of devices
59 
60     /// Get assistant for a given udi
getUdiAssistant(const QString & udi)61     ConnectionAssistant *getUdiAssistant( const QString &udi )
62     {
63         return m_udiAssistants[ udi ];
64     }
65 
66     /**
67 
68     registerDeviceType adds the device type described by @param assistant to the list
69     of known device types by the MDM, and then checks the list of known devices
70     for a match with this type
71 
72     */
73     void registerDeviceType( ConnectionAssistant *assistant );
74 
75     public Q_SLOTS:
76 
77     /**
78 
79     checkDevice checks if @p udi is a known device
80     and if so attempts to connect it
81 
82     checkOneDevice runs an identify check using the given
83     assistant and udi
84 
85     checkDevicesFor checks if the device type described
86     by @p assistant matches any of the udi's in the
87     MediaDeviceCache, and if so, attempts to connect to
88     it
89 
90     */
91 
92     void checkDevice( const QString &udi );
93     void checkOneDevice( ConnectionAssistant* assistant, const QString& udi );
94     void checkDevicesFor( ConnectionAssistant* assistant );
95 
96 
97     Q_SIGNALS:
98         void deviceDetected( const MediaDeviceInfo &deviceinfo );
99         void deviceRemoved( const QString &udi );
100 
101 
102     private Q_SLOTS:
103 
104         void deviceAdded( const QString &udi );
105         void slotDeviceRemoved( const QString &udi );
106         void slotAccessibilityChanged( bool accessible, const QString & udi );
107 
108         void slotDequeueWaitingAssistant();
109 
110 
111     private:
112         static MediaDeviceMonitor *s_instance;
113 
114         // keeps track of which CA to contact for which udi
115         QHash<QString,ConnectionAssistant*> m_udiAssistants;
116         // holds all registered assistants
117         QList<ConnectionAssistant*> m_assistants;
118         // holds all waiting assistants
119         QList<ConnectionAssistant*> m_waitingassistants;
120         // holds index of next waiting assistant to check
121         // devices with, during initialization of device
122         // factories
123         int m_nextassistant;
124 };
125 
126 #endif /* AMAROK_MEDIADEVICEMONITOR_H */
127 
128