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"
13 #include "mediabrowser.h"
14 #include "metabundle.h"
18 #include <kio/global.h> //filesize_t
19 #include <KUrl> //stack allocated
26 /* at least the pure virtual functions have to be implemented by a media device,
27 all items are stored in a hierarchy of MediaItems,
28 when items are manipulated the MediaItems have to be updated accordingly */
30 class AMAROK_EXPORT MediaDevice
: public QObject
, public Amarok::Plugin
33 friend class DeviceConfigureDialog
;
34 friend class MediaBrowser
;
35 friend class MediaDeviceConfig
;
36 friend class MediaView
;
37 friend class MediaQueue
;
38 friend class TransferDialog
;
50 virtual void init( MediaBrowser
* parent
);
51 virtual ~MediaDevice();
56 * @return a KAction that will be plugged into the media device browser toolbar
58 virtual KAction
*customAction() { return 0; }
60 virtual void rmbPressed( Q3ListViewItem
*item
, const QPoint
&point
, int ) { (void)item
; (void) point
; }
63 * @return list of filetypes playable on this device
64 * (empty list is interpreted as all types are good)
66 virtual QStringList
supportedFiletypes() { return QStringList(); }
69 * @param bundle describes track that should be checked
70 * @return true if the device is capable of playing the track referred to by bundle
72 virtual bool isPlayable( const MetaBundle
&bundle
);
75 * @param bundle describes track that should be checked
76 * @return true if the track is in the preferred (first in list) format of the device
78 virtual bool isPreferredFormat( const MetaBundle
&bundle
);
81 * @return true if the device is connected
83 virtual bool isConnected() = 0;
86 * Adds particular tracks to a playlist
87 * @param playlist parent playlist for tracks to be added to
88 * @param after insert following this item
89 * @param items tracks to add to playlist
91 virtual void addToPlaylist(MediaItem
*playlist
, MediaItem
*after
, QList
<MediaItem
*> items
) { Q_UNUSED(playlist
); Q_UNUSED(after
); Q_UNUSED(items
); }
94 * Create a new playlist
95 * @param name playlist title
96 * @param parent parent MediaItem of the new playlist
97 * @param items tracks to add to the new playlist
98 * @return the newly created playlist
100 virtual MediaItem
*newPlaylist(const QString
&name
, MediaItem
*parent
, QList
<MediaItem
*> items
) { Q_UNUSED(name
); Q_UNUSED(parent
); Q_UNUSED(items
); return 0; }
103 * Move items to a directory
104 * @param directory new parent of dropped items
105 * @param items tracks to add to the directory
107 virtual void addToDirectory( MediaItem
*directory
, QList
<MediaItem
*> items
) { Q_UNUSED(directory
); Q_UNUSED(items
); }
110 * Create a new directory
111 * @param name directory title
112 * @param parent parent MediaItem of the new directory
113 * @param items tracks to add to the new directory
114 * @return the newly created directory
116 virtual MediaItem
*newDirectory( const QString
&name
, MediaItem
*parent
) { Q_UNUSED(name
); Q_UNUSED(parent
); return 0; }
119 * Notify device of changed tags
120 * @param item item to be updated
121 * @param changed bundle containing new tags
122 * @return the changed MediaItem
124 virtual MediaItem
*tagsChanged( MediaItem
*item
, const MetaBundle
&changed
) { Q_UNUSED(item
); Q_UNUSED(changed
); return 0; }
127 * Indicate whether the device has a custom transfer dialog
128 * @return whether there is a custom dialog
130 virtual bool hasTransferDialog() { return false; }
133 * Run the transfer dialog to be used when Transfer is clicked
135 virtual void runTransferDialog() {}
138 * Get the transfer dialog, if any
139 * @return the transfer dialog, if any, else NULL;
141 virtual TransferDialog
*getTransferDialog() { return NULL
; }
144 * Can be used to explicitly indicate whether a device needs manual configuration
145 * @return whether manual configuration is needed
147 virtual bool needsManualConfig() { return true; }
149 virtual void addConfigElements( QWidget
* /*parent*/ ) {}
150 virtual void removeConfigElements( QWidget
* /*parent*/ ) {}
151 virtual void applyConfig() {}
152 virtual void loadConfig();
154 QString
configString( const QString
&name
, const QString
&defValue
= QString() );
155 void setConfigString( const QString
&name
, const QString
&value
);
156 bool configBool( const QString
&name
, bool defValue
=false );
157 void setConfigBool( const QString
&name
, bool value
);
159 void setRequireMount( const bool b
) { m_requireMount
= b
; }
160 bool hasMountPoint() { return m_hasMountPoint
; }
161 void setDeviceType( const QString
&type
) { m_type
= type
; }
162 QString
type() { return m_type
; }
163 virtual bool autoConnect() { return false; }
164 virtual bool asynchronousTransfer() { return false; }
165 bool isTransferring() { return m_transferring
; }
166 bool isDeleting() { return m_deleting
; }
167 bool isCanceled() { return m_canceled
; }
168 void setCanceled( const bool b
) { m_canceled
= b
; }
170 int progress() const;
171 void setProgress( const int progress
, const int total
= -1 /* leave total unchanged by default */ );
176 * @return a unique identifier that is constant across sessions
178 QString
udi() const { return m_udi
; }
181 * @return the name for the device that should be presented to the user
183 QString
name() const { return m_name
; }
186 * @return the filesystem type
188 QString
fsType() const { return m_fsType
; }
191 * @return the device node
193 QString
deviceNode() const { return m_deviceNode
; }
196 * @return the device mount point (or empty if non-applicable or unknown)
198 QString
mountPoint() const { return m_mountPoint
; }
200 QString
getTransferDir() { return m_transferDir
; }
202 void setSpacesToUnderscores( bool yesno
) { m_spacesToUnderscores
= yesno
;
203 setConfigBool( "spacesToUnderscores", yesno
); }
204 bool getSpacesToUnderscores() { return m_spacesToUnderscores
; }
206 void setFirstSort( QString text
) { m_firstSort
= text
;
207 setConfigString( "firstGrouping", text
); }
208 void setSecondSort( QString text
) { m_secondSort
= text
;
209 setConfigString( "secondGrouping", text
); }
210 void setThirdSort( QString text
) { m_thirdSort
= text
;
211 setConfigString( "thirdGrouping", text
); }
213 virtual KUrl
getProxyUrl( const KUrl
& /*url*/) { return KUrl(); }
214 virtual void customClicked() { return; }
216 BundleList
bundlesToSync( const QString
&playlistName
, const QString
&sql
);
217 BundleList
bundlesToSync( const QString
&playlistName
, const KUrl
&url
);
218 void preparePlaylistForSync( const QString
&playlistName
, const BundleList
&bundles
);
219 bool isOnOtherPlaylist( const QString
&playlistToAvoid
, const MetaBundle
&bundle
);
220 bool isOnPlaylist( const MediaItem
&playlist
, const MetaBundle
&bundle
);
221 bool isInBundleList( const BundleList
&bundles
, const MetaBundle
&bundle
);
222 bool bundleMatch( const MetaBundle
&b1
, const MetaBundle
&b2
);
225 void abortTransfer();
226 void transferFiles();
227 virtual void renameItem( Q3ListViewItem
*item
) {(void)item
; }
228 virtual void expandItem( Q3ListViewItem
*item
) {(void)item
; }
229 bool connectDevice( bool silent
=false );
230 bool disconnectDevice( bool postdisconnecthook
=true );
231 void scheduleDisconnect() { m_scheduledDisconnect
= true; }
234 void fileTransferred( KIO::Job
*job
);
235 void fileTransferFinished();
238 int sysCall(const QString
& command
);
239 int runPreConnectCommand();
240 int runPostDisconnectCommand();
241 QString
replaceVariables( const QString
&cmd
); // replace %m with mount point and %d with device node
243 void setUid( const QString
&udi
) { m_udi
= udi
; }
246 * Find a particular track
247 * @param bundle The metabundle of the requested media item
248 * @return The MediaItem of the item if found, otherwise NULL
249 * @note This may not be worth implementing for non database driven devices, as it could be slow
251 virtual MediaItem
*trackExists( const MetaBundle
& bundle
) = 0;
255 * Get the capacity and freespace available on the device, in bytes
256 * @return true if successful
258 virtual bool getCapacity( KIO::filesize_t
*total
, KIO::filesize_t
*available
) { Q_UNUSED(total
); Q_UNUSED(available
); return false; }
261 * Lock device for exclusive access if possible
263 virtual bool lockDevice( bool tryOnly
= false ) = 0;
268 virtual void unlockDevice() = 0;
271 * Connect to device, and populate m_view with MediaItems
272 * @return true if successful
274 virtual bool openDevice( bool silent
=false ) = 0;
277 * Wrap up any loose ends and close the device
278 * @return true if successful
280 virtual bool closeDevice() = 0;
283 * Write any pending changes to the device, such as database changes
285 virtual void synchronizeDevice() = 0;
288 * Copy a track to the device
289 * @param bundle The MetaBundle of the item to transfer. Will move the item specified by bundle().url().path()
290 * @return If successful, the created MediaItem in the media device view, else 0
292 virtual MediaItem
*copyTrackToDevice(const MetaBundle
& bundle
) = 0;
295 * Copy track from device to computer
296 * @param item The MediaItem of the track to transfer.
297 * @param url The URL to transfer the track to.
298 * @return The MediaItem transfered.
300 virtual void copyTrackFromDevice(MediaItem
*item
);
303 * Recursively remove MediaItem from the tracklist and the device
304 * @param item MediaItem to remove
305 * @param onlyPlayed True if item should be deleted only if it has been played
306 * @return -1 on failure, number of files deleted otherwise
308 virtual int deleteItemFromDevice( MediaItem
*item
, int flags
=DeleteTrack
) = 0;
311 * Abort the currently active track transfer
313 virtual void cancelTransfer() { /* often checking m_cancel is enough */ }
315 virtual void updateRootItems();
317 virtual bool isSpecialItem( MediaItem
*item
);
319 int deleteFromDevice( MediaItem
*item
=0, int flags
=DeleteTrack
);
321 void purgeEmptyItems( MediaItem
*root
=0 );
322 void syncStatsFromDevice( MediaItem
*root
=0 );
323 void syncStatsToDevice( MediaItem
*root
=0 );
325 bool kioCopyTrack( const KUrl
&src
, const KUrl
&dst
);
329 bool m_hasMountPoint
;
331 QString m_preconnectcmd
;
332 QString m_postdisconnectcmd
;
333 bool m_autoDeletePodcasts
;
337 bool m_transcodeAlways
;
338 bool m_transcodeRemove
;
340 K3ShellProcess
*sysProc
;
341 MediaBrowser
*m_parent
;
343 QString m_transferDir
;
345 QString m_secondSort
;
348 QString m_deviceNode
;
349 QString m_mountPoint
;
352 bool m_waitForDeletion
;
358 bool m_deferredDisconnect
;
359 bool m_scheduledDisconnect
;
360 bool m_runDisconnectHook
;
361 bool m_spacesToUnderscores
;
368 // root listview items
369 MediaItem
*m_playlistItem
;
370 MediaItem
*m_podcastItem
;
371 // items not on the master playlist and not on the podcast playlist are not visible on the ipod
372 MediaItem
*m_invisibleItem
;
373 // items in the database for which the file is missing
374 MediaItem
*m_staleItem
;
375 // files without database entry
376 MediaItem
*m_orphanedItem
;
378 // stow away all items below m_rootItems when device is not current
379 QList
<Q3ListViewItem
*> m_rootItems
;
383 #endif /* AMAROK_MEDIADEVICE_H */