Revert previous commit, was incorrect
[amarok.git] / src / MediaDevice.h
blob59cf4e795d2fb1f3cdb1688f3485bd4fa7e7d711
1 // (c) 2004 Christian Muehlhaeuser <chris@chris.de>
2 // (c) 2005 Martin Aumueller <aumuell@reserv.at>
3 // (c) 2005 Seb Ruiz <ruiz@kde.org>
4 // (c) 2006 T.R.Shashwath <trshash84@gmail.com>
5 // (c) 2007 Jeff Mitchell <kde-dev@emailgoeshere.com>
6 // See COPYING file for licensing information
8 #ifndef AMAROK_MEDIADEVICE_H
9 #define AMAROK_MEDIADEVICE_H
11 #include "amarok_export.h"
12 #include "amarok.h"
13 #include "mediabrowser.h"
14 #include "Meta.h"
16 #include <QObject>
18 #include <kio/global.h> //filesize_t
19 #include <KUrl> //stack allocated
21 class MediaDevice;
22 class MediaItem;
23 class TransferDialog;
24 class MediaView;
25 class ShellProcess;
27 /* at least the pure virtual functions have to be implemented by a media device,
28 all items are stored in a hierarchy of MediaItems,
29 when items are manipulated the MediaItems have to be updated accordingly */
31 class AMAROK_EXPORT MediaDevice : public QObject, public Amarok::Plugin
33 Q_OBJECT
34 friend class DeviceConfigureDialog;
35 friend class MediaBrowser;
36 friend class MediaDeviceConfig;
37 friend class MediaView;
38 friend class MediaQueue;
39 friend class TransferDialog;
41 public:
42 enum Flags
44 None = 0,
45 OnlyPlayed = 1,
46 DeleteTrack = 2,
47 Recursing = 4
50 MediaDevice();
51 virtual void init( MediaBrowser* parent );
52 virtual ~MediaDevice();
54 MediaView *view();
56 /**
57 * @return a KAction that will be plugged into the media device browser toolbar
59 virtual KAction *customAction() { return 0; }
61 virtual void rmbPressed( Q3ListViewItem *item, const QPoint &point, int ) { (void)item; (void) point; }
63 /**
64 * @return list of filetypes playable on this device
65 * (empty list is interpreted as all types are good)
67 virtual QStringList supportedFiletypes() { return QStringList(); }
69 /**
70 * @param track describes track that should be checked
71 * @return true if the device is capable of playing the track referred to by track
73 virtual bool isPlayable( const Meta::TrackPtr track );
75 /**
76 * @param track describes track that should be checked
77 * @return true if the track is in the preferred (first in list) format of the device
79 virtual bool isPreferredFormat( const Meta::TrackPtr track );
81 /**
82 * @return true if the device is connected
84 virtual bool isConnected() = 0;
86 /**
87 * Adds particular tracks to a playlist
88 * @param playlist parent playlist for tracks to be added to
89 * @param after insert following this item
90 * @param items tracks to add to playlist
92 virtual void addToPlaylist(MediaItem *playlist, MediaItem *after, QList<MediaItem*> items) { Q_UNUSED(playlist); Q_UNUSED(after); Q_UNUSED(items); }
94 /**
95 * Create a new playlist
96 * @param name playlist title
97 * @param parent parent MediaItem of the new playlist
98 * @param items tracks to add to the new playlist
99 * @return the newly created playlist
101 virtual MediaItem *newPlaylist(const QString &name, MediaItem *parent, QList<MediaItem*> items) { Q_UNUSED(name); Q_UNUSED(parent); Q_UNUSED(items); return 0; }
104 * Move items to a directory
105 * @param directory new parent of dropped items
106 * @param items tracks to add to the directory
108 virtual void addToDirectory( MediaItem *directory, QList<MediaItem*> items ) { Q_UNUSED(directory); Q_UNUSED(items); }
111 * Create a new directory
112 * @param name directory title
113 * @param parent parent MediaItem of the new directory
114 * @param items tracks to add to the new directory
115 * @return the newly created directory
117 virtual MediaItem *newDirectory( const QString &name, MediaItem *parent ) { Q_UNUSED(name); Q_UNUSED(parent); return 0; }
120 * Notify device of changed tags
121 * @param item item to be updated
122 * @param changed meta containing new tags
123 * @return the changed MediaItem
125 virtual MediaItem *tagsChanged( MediaItem *item, const Meta::TrackPtr changed ) { Q_UNUSED(item); Q_UNUSED(changed); return 0; }
128 * Indicate whether the device has a custom transfer dialog
129 * @return whether there is a custom dialog
131 virtual bool hasTransferDialog() { return false; }
134 * Run the transfer dialog to be used when Transfer is clicked
136 virtual void runTransferDialog() {}
139 * Get the transfer dialog, if any
140 * @return the transfer dialog, if any, else NULL;
142 virtual TransferDialog *getTransferDialog() { return NULL; }
145 * Can be used to explicitly indicate whether a device needs manual configuration
146 * @return whether manual configuration is needed
148 virtual bool needsManualConfig() { return true; }
150 virtual void addConfigElements( QWidget * /*parent*/ ) {}
151 virtual void removeConfigElements( QWidget * /*parent*/ ) {}
152 virtual void applyConfig() {}
153 virtual void loadConfig();
155 QString configString( const QString &name, const QString &defValue = QString() );
156 void setConfigString( const QString &name, const QString &value );
157 bool configBool( const QString &name, bool defValue=false );
158 void setConfigBool( const QString &name, bool value );
160 void setRequireMount( const bool b ) { m_requireMount = b; }
161 bool hasMountPoint() { return m_hasMountPoint; }
162 void setDeviceType( const QString &type ) { m_type = type; }
163 QString type() { return m_type; }
164 virtual bool autoConnect() { return false; }
165 virtual bool asynchronousTransfer() { return false; }
166 bool isTransferring() { return m_transferring; }
167 bool isDeleting() { return m_deleting; }
168 bool isCanceled() { return m_canceled; }
169 void setCanceled( const bool b ) { m_canceled = b; }
171 int progress() const;
172 void setProgress( const int progress, const int total = -1 /* leave total unchanged by default */ );
173 void hideProgress();
177 * @return a unique identifier that is constant across sessions
179 QString udi() const { return m_udi; }
182 * @return the name for the device that should be presented to the user
184 QString name() const { return m_name; }
187 * @return the filesystem type
189 QString fsType() const { return m_fsType; }
192 * @return the device node
194 QString deviceNode() const { return m_deviceNode; }
197 * @return the device mount point (or empty if non-applicable or unknown)
199 QString mountPoint() const { return m_mountPoint; }
201 QString getTransferDir() { return m_transferDir; }
203 void setSpacesToUnderscores( bool yesno ) { m_spacesToUnderscores = yesno;
204 setConfigBool( "spacesToUnderscores", yesno); }
205 bool getSpacesToUnderscores() { return m_spacesToUnderscores; }
207 void setFirstSort( QString text ) { m_firstSort = text;
208 setConfigString( "firstGrouping", text ); }
209 void setSecondSort( QString text ) { m_secondSort = text;
210 setConfigString( "secondGrouping", text ); }
211 void setThirdSort( QString text ) { m_thirdSort = text;
212 setConfigString( "thirdGrouping", text ); }
214 virtual KUrl getProxyUrl( const KUrl& /*url*/) { return KUrl(); }
215 virtual void customClicked() { return; }
217 //BundleList bundlesToSync( const QString &playlistName, const QString &sql );
218 Meta::TrackList tracksToSync( const QString &playlistName, const KUrl &url );
219 void preparePlaylistForSync( const QString &playlistName, const Meta::TrackList &list );
221 * @return true if track is on any playlist other than playlistToAvoid
223 bool isOnOtherPlaylist( const QString &playlistToAvoid, const Meta::TrackPtr track );
224 bool isOnPlaylist( const MediaItem &playlist, const Meta::TrackPtr track );
225 bool isInTrackList( const Meta::TrackList &list, const Meta::TrackPtr track );
226 bool trackMatch( Meta::TrackPtr track1, Meta::TrackPtr track2 );
228 public slots:
229 void abortTransfer();
230 void transferFiles();
231 virtual void renameItem( Q3ListViewItem *item ) {(void)item; }
232 virtual void expandItem( Q3ListViewItem *item ) {(void)item; }
233 bool connectDevice( bool silent=false );
234 bool disconnectDevice( bool postdisconnecthook=true );
235 void scheduleDisconnect() { m_scheduledDisconnect = true; }
237 protected slots:
238 void fileTransferred( KIO::Job *job );
239 void fileTransferFinished();
241 private:
242 int sysCall(const QString & command);
243 int runPreConnectCommand();
244 int runPostDisconnectCommand();
245 QString replaceVariables( const QString &cmd ); // replace %m with mount point and %d with device node
247 void setUid( const QString &udi ) { m_udi = udi; }
250 * Find a particular track
251 * @param track The meta::track of the requested media item
252 * @return The MediaItem of the item if found, otherwise NULL
253 * @note This may not be worth implementing for non database driven devices, as it could be slow
255 virtual MediaItem *trackExists( Meta::TrackPtr track ) { Q_UNUSED(track); return 0; }
257 protected:
259 * Get the capacity and freespace available on the device, in bytes
260 * @return true if successful
262 virtual bool getCapacity( KIO::filesize_t *total, KIO::filesize_t *available ) { Q_UNUSED(total); Q_UNUSED(available); return false; }
265 * Lock device for exclusive access if possible
267 virtual bool lockDevice( bool tryOnly = false ) = 0;
270 * Unlock device
272 virtual void unlockDevice() = 0;
275 * Connect to device, and populate m_view with MediaItems
276 * @return true if successful
278 virtual bool openDevice( bool silent=false ) = 0;
281 * Wrap up any loose ends and close the device
282 * @return true if successful
284 virtual bool closeDevice() = 0;
287 * Write any pending changes to the device, such as database changes
289 virtual void synchronizeDevice() = 0;
292 * Copy a track to the device
293 * @param track The Meta::TrackPtr of the item to transfer. Will move the item specified by track->url()
294 * @return If successful, the created MediaItem in the media device view, else 0
296 virtual MediaItem *copyTrackToDevice(const Meta::TrackPtr track) = 0;
299 * Copy track from device to computer
300 * @param item The MediaItem of the track to transfer.
301 * @param url The URL to transfer the track to.
302 * @return The MediaItem transfered.
304 virtual void copyTrackFromDevice(MediaItem *item);
307 * Recursively remove MediaItem from the tracklist and the device
308 * @param item MediaItem to remove
309 * @param onlyPlayed True if item should be deleted only if it has been played
310 * @return -1 on failure, number of files deleted otherwise
312 virtual int deleteItemFromDevice( MediaItem *item, int flags=DeleteTrack ) = 0;
315 * Abort the currently active track transfer
317 virtual void cancelTransfer() { /* often checking m_cancel is enough */ }
319 virtual void updateRootItems();
321 virtual bool isSpecialItem( MediaItem *item );
323 int deleteFromDevice( MediaItem *item=0, int flags=DeleteTrack );
325 void purgeEmptyItems( MediaItem *root=0 );
326 void syncStatsFromDevice( MediaItem *root=0 );
327 void syncStatsToDevice( MediaItem *root=0 );
329 bool kioCopyTrack( const KUrl &src, const KUrl &dst );
331 QString m_name;
333 bool m_hasMountPoint;
335 QString m_preconnectcmd;
336 QString m_postdisconnectcmd;
337 bool m_autoDeletePodcasts;
338 bool m_syncStats;
340 bool m_transcode;
341 bool m_transcodeAlways;
342 bool m_transcodeRemove;
344 ShellProcess *sysProc;
345 MediaBrowser *m_parent;
346 MediaView *m_view;
347 QString m_transferDir;
348 QString m_firstSort;
349 QString m_secondSort;
350 QString m_thirdSort;
351 QString m_udi;
352 QString m_deviceNode;
353 QString m_mountPoint;
354 QString m_fsType;
355 bool m_wait;
356 bool m_waitForDeletion;
357 bool m_copyFailed;
358 bool m_requireMount;
359 bool m_canceled;
360 bool m_transferring;
361 bool m_deleting;
362 bool m_deferredDisconnect;
363 bool m_scheduledDisconnect;
364 bool m_runDisconnectHook;
365 bool m_spacesToUnderscores;
366 bool m_transfer;
367 bool m_configure;
368 bool m_customButton;
370 QString m_type;
372 // root listview items
373 MediaItem *m_playlistItem;
374 MediaItem *m_podcastItem;
375 // items not on the master playlist and not on the podcast playlist are not visible on the ipod
376 MediaItem *m_invisibleItem;
377 // items in the database for which the file is missing
378 MediaItem *m_staleItem;
379 // files without database entry
380 MediaItem *m_orphanedItem;
382 // stow away all items below m_rootItems when device is not current
383 QList<Q3ListViewItem*> m_rootItems;
387 #endif /* AMAROK_MEDIADEVICE_H */