add mp3 and ogg torrent url info to JamendoAlbum
[amarok.git] / src / mountpointmanager.h
blobebfd5adc4c78ecd2a690df05183408912c35934b
1 /*
2 * Copyright (c) 2006-2007 Maximilian Kossick <maximilian.kossick@googlemail.com>
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18 #ifndef AMAROK_MOUNTPOINTMANAGER_H
19 #define AMAROK_MOUNTPOINTMANAGER_H
21 #include "amarok.h"
22 #include "amarok_export.h"
23 #include "medium.h"
24 #include "plugin/plugin.h"
25 #include "pluginmanager.h"
26 #include "threadmanager.h"
28 #include <KConfig>
29 #include <KUrl>
31 #include <QMap>
32 #include <QMutex>
33 #include <QStringList>
35 class DeviceHandler;
36 class DeviceHandlerFactory;
38 typedef QList<int> IdList;
39 typedef QList<DeviceHandlerFactory*> FactoryList;
40 typedef QMap<int, DeviceHandler*> HandlerMap;
43 class AMAROK_EXPORT DeviceHandlerFactory : public Amarok::Plugin
45 public:
46 DeviceHandlerFactory() {}
47 virtual ~DeviceHandlerFactory() {}
49 /**
50 * checks whether a DeviceHandler subclass can handle a given Medium.
51 * @param m the connected medium
52 * @return true if the DeviceHandler implementation can handle the medium,
53 * false otherwise
55 virtual bool canHandle( const Medium* m ) const = 0;
57 /**
58 * tells the MountPointManager whether it makes sense to ask the factory to
59 * create a Devicehandler when a new Medium was connected
60 * @return true if the factory can create DeviceHandlers from Medium instances
62 virtual bool canCreateFromMedium() const = 0;
64 /**
65 * creates a DeviceHandler which represents the Medium.
66 * @param c the Medium for which a DeviceHandler is required
67 * @return a DeviceHandler or 0 if the factory cannot handle the Medium
69 virtual DeviceHandler* createHandler( const Medium* m ) const = 0;
71 virtual bool canCreateFromConfig() const = 0;
73 virtual DeviceHandler* createHandler( KSharedConfigPtr c ) const = 0;
75 /**
76 * returns the type of the DeviceHandler. Should be the same as the value used in
77 * ~/.kde/share/config/amarokrc
78 * @return a QString describing the type of the DeviceHandler
80 virtual QString type() const = 0;
84 /**
88 class DeviceHandler
90 public:
91 DeviceHandler() {}
92 virtual ~DeviceHandler() {}
95 virtual bool isAvailable() const = 0;
97 /**
98 * returns the type of the DeviceHandler. Should be the same as the value used in
99 * ~/.kde/share/config/amarokrc
100 * @return a QString describing the type of the DeviceHandler
102 virtual QString type() const = 0;
105 * returns an absolute path which is guaranteed to be playable by amarok's current engine. (based on an
106 * idea by andrewt512: this method would only be called when we actually want to play the file, not when we
107 * simply want to show it to the user. It could for example download a file using KIO and return a path to a
108 * temporary file. Needs some more thought and is not actually used at the moment.
109 * @param absolutePath
110 * @param relativePath
112 virtual void getPlayableURL( KUrl &absolutePath, const KUrl &relativePath ) = 0;
115 * builds an absolute path from a relative path and DeviceHandler specific information. The absolute path
116 * is not necessarily playable! (based on an idea by andrewt512: allows better handling of files stored in remote * collections. this method would return a "pretty" URL which might not be playable by amarok's engines.
117 * @param absolutePath the not necessarily playbale absolute path
118 * @param relativePath the device specific relative path
120 virtual void getURL( KUrl &absolutePath, const KUrl &relativePath ) = 0;
123 * retrieves the unique database id of a given Medium. Implementations are responsible
124 * for generating a (sufficiently) unique value which identifies the Medium.
125 * Additionally, implementations must recognize unknown mediums and store the necessary
126 * information to recognize them the next time they are connected in the database.
127 * @return unique identifier which can be used as a foreign key to the media table.
129 virtual int getDeviceID() = 0;
131 virtual const QString &getDevicePath() const = 0;
134 * allows MountPointManager to check if a device handler handles a specific medium.
135 * @param m
136 * @return true if the device handler handles the Medium m
138 virtual bool deviceIsMedium( const Medium *m ) const = 0;
142 * @author Maximilian Kossick <maximilian.kossick@googlemail.com>
144 class MountPointManager : public QObject {
145 Q_OBJECT
147 signals:
148 void mediumConnected( int deviceid );
149 void mediumRemoved( int deviceid );
151 public:
152 //the methods of this class a called *very* often. make sure they are as fast as possible
153 // (inline them?)
156 * factory method.
157 * @return a MountPointManager instance
159 AMAROK_EXPORT static MountPointManager *instance();
163 * @param url
164 * @return
166 int getIdForUrl( const KUrl &url );
167 AMAROK_EXPORT int getIdForUrl( const QString &url );
170 * @param id
171 * @return
173 QString getMountPointForId( const int id ) const;
175 * builds the absolute path from the mount point of the medium and the given relative
176 * path.
177 * @param deviceId the medium(device)'s unique id
178 * @param relativePath relative path on the medium
179 * @return the absolute path
181 void getAbsolutePath( const int deviceId, const KUrl& relativePath, KUrl& absolutePath ) const;
182 AMAROK_EXPORT QString getAbsolutePath ( const int deviceId, const QString& relativePath ) const;
184 * calculates a file's/directory's relative path on a given device.
185 * @param deviceId the unique id which identifies the device the file/directory is supposed to be on
186 * @param absolutePath the file's/directory's absolute path
187 * @param relativePath the calculated relative path
189 void getRelativePath( const int deviceId, const KUrl& absolutePath, KUrl& relativePath ) const;
190 AMAROK_EXPORT QString getRelativePath( const int deviceId, const QString& absolutePath ) const;
192 * allows calling code to access the ids of all active devices
193 * @return the ids of all devices which are currently mounted or otherwise accessible
195 AMAROK_EXPORT IdList getMountedDeviceIds() const;
197 AMAROK_EXPORT QStringList collectionFolders();
198 void setCollectionFolders( const QStringList &folders );
200 public slots:
201 void mediumAdded( const Medium *m );
203 * initiates the update of the class' internal list of mounted mediums.
204 * @param m the medium whose status changed
206 void mediumChanged( const Medium* m );
207 void mediumRemoved( const Medium* m );
209 void updateStatisticsURLs( bool changed = true );
211 private slots:
212 void migrateStatistics();
213 void checkDeviceAvailability();
214 void startStatisticsUpdateJob();
216 private:
217 MountPointManager();
219 ~MountPointManager();
222 * checks whether a medium identified by its unique database id is currently mounted.
223 * Note: does not handle deviceId = -1! It only checks real devices
224 * @param deviceId the mediums unique id
225 * @return true if the medium is mounted, false otherwise
227 bool isMounted ( const int deviceId ) const;
228 void init();
230 * maps a device id to a mount point. does only work for mountable filesystems and needs to be
231 * changed for the real Dynamic Collection implementation.
233 HandlerMap m_handlerMap;
234 mutable QMutex m_handlerMapMutex;
235 FactoryList m_mediumFactories;
236 FactoryList m_remoteFactories;
238 //Solid specific
239 private slots:
240 void deviceAdded( const QString &udi );
241 void deviceRemoved( const QString &udi );
245 class UrlUpdateJob : public ThreadManager::DependentJob
247 public:
248 UrlUpdateJob( QObject *dependent ) : DependentJob( dependent, "UrlUpdateJob" ) {}
250 virtual bool doJob();
252 virtual void completeJob() {}
254 private:
255 void updateStatistics();
256 void updateLabels();
259 #endif