~ubuntu-branches/ubuntu/lucid/amarok/lucid-backports

« back to all changes in this revision

Viewing changes to src/core-impl/collections/mediadevicecollection/MediaDeviceMeta.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Scott Kitterman
  • Date: 2011-03-03 10:27:39 UTC
  • mfrom: (114.1.20 natty)
  • Revision ID: james.westby@ubuntu.com-20110303102739-ar67wpa6mllo59n2
Tags: 2:2.4.0-0ubuntu4~lucid1
* Source backport to lucid (LP: #728447)
  - Drop version requirement on libindicate-qt-dev build-dep

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/****************************************************************************************
 
2
 * Copyright (c) 2009 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
#include "MediaDeviceMeta.h"
 
18
#include "MediaDeviceCollection.h"
 
19
#include "MediaDeviceHandler.h"
 
20
 
 
21
#include "handler/capabilities/ArtworkCapability.h"
 
22
 
 
23
// HACK: used to test disconnect
 
24
#include "MediaDeviceMonitor.h"
 
25
 
 
26
#include "covermanager/CoverFetchingActions.h"
 
27
#include "core/support/Debug.h"
 
28
#include "SvgHandler.h"
 
29
#include "core/capabilities/ActionsCapability.h"
 
30
#include "core/capabilities/EditCapability.h"
 
31
#include "core/capabilities/UpdateCapability.h"
 
32
 
 
33
#include <KIcon>
 
34
#include <KUrl>
 
35
 
 
36
#include <QAction>
 
37
 
 
38
using namespace Meta;
 
39
// Currently complaining about some vtable issue
 
40
 
 
41
class EditCapabilityMediaDevice : public Capabilities::EditCapability
 
42
{
 
43
    Q_OBJECT
 
44
    public:
 
45
        EditCapabilityMediaDevice( MediaDeviceTrack *track )
 
46
            : Capabilities::EditCapability()
 
47
            , m_track( track ) {}
 
48
 
 
49
        virtual bool isEditable() const { return m_track->isEditable(); }
 
50
        virtual void setAlbum( const QString &newAlbum ) { m_track->setAlbum( newAlbum ); }
 
51
        virtual void setAlbumArtist( const QString &newAlbumArtist ) { m_track->setAlbumArtist( newAlbumArtist ); }
 
52
        virtual void setArtist( const QString &newArtist ) { m_track->setArtist( newArtist ); }
 
53
        virtual void setComposer( const QString &newComposer ) { m_track->setComposer( newComposer ); }
 
54
        virtual void setGenre( const QString &newGenre ) { m_track->setGenre( newGenre ); }
 
55
        virtual void setYear( int newYear ) { m_track->setYear( newYear ); }
 
56
        virtual void setBpm( const qreal newBpm ) { m_track->setBpm( newBpm ); }
 
57
        virtual void setTitle( const QString &newTitle ) { m_track->setTitle( newTitle ); }
 
58
        virtual void setComment( const QString &newComment ) { m_track->setComment( newComment ); }
 
59
        virtual void setTrackNumber( int newTrackNumber ) { m_track->setTrackNumber( newTrackNumber ); }
 
60
        virtual void setDiscNumber( int newDiscNumber ) { m_track->setDiscNumber( newDiscNumber ); }
 
61
        virtual void setUidUrl( const QString &newUidUrl ) { m_track->setUidUrl( newUidUrl ); }
 
62
        virtual void beginMetaDataUpdate() { m_track->beginMetaDataUpdate(); }
 
63
        virtual void endMetaDataUpdate() { m_track->endMetaDataUpdate(); }
 
64
 
 
65
    private:
 
66
        KSharedPtr<MediaDeviceTrack> m_track;
 
67
};
 
68
 
 
69
class UpdateCapabilityMediaDevice : public Capabilities::UpdateCapability
 
70
{
 
71
    Q_OBJECT
 
72
    public:
 
73
        UpdateCapabilityMediaDevice( Collections::MediaDeviceCollection *coll )
 
74
            : Capabilities::UpdateCapability()
 
75
            , m_coll( coll )
 
76
        {}
 
77
 
 
78
        virtual void collectionUpdated() const
 
79
        {
 
80
            m_coll->collectionUpdated();
 
81
            m_coll->writeDatabase();
 
82
        }
 
83
 
 
84
    private:
 
85
        Collections::MediaDeviceCollection *m_coll;
 
86
};
 
87
 
 
88
 
 
89
MediaDeviceTrack::MediaDeviceTrack( Collections::MediaDeviceCollection *collection )
 
90
    : Meta::Track()
 
91
    , m_collection( collection )
 
92
    , m_artist( 0 )
 
93
    , m_album( 0 )
 
94
    , m_genre( 0 )
 
95
    , m_composer( 0 )
 
96
    , m_year( 0 )
 
97
    , m_image()
 
98
    , m_comment()
 
99
    , m_name()
 
100
    , m_type()
 
101
    , m_bitrate( 0 )
 
102
    , m_filesize( 0 )
 
103
    , m_length( 0 )
 
104
    , m_discNumber( 0 )
 
105
    , m_samplerate( 0 )
 
106
    , m_trackNumber( 0 )
 
107
    , m_playCount( 0 )
 
108
    , m_rating( 0 )
 
109
    , m_bpm( 0 )
 
110
    , m_playableUrl()
 
111
{
 
112
}
 
113
 
 
114
MediaDeviceTrack::~MediaDeviceTrack()
 
115
{
 
116
    //nothing to do
 
117
}
 
118
 
 
119
QString
 
120
MediaDeviceTrack::name() const
 
121
{
 
122
    return m_name;
 
123
}
 
124
 
 
125
KUrl
 
126
MediaDeviceTrack::playableUrl() const
 
127
{
 
128
    return m_playableUrl;
 
129
}
 
130
 
 
131
QString
 
132
MediaDeviceTrack::uidUrl() const
 
133
{
 
134
    return m_playableUrl.isLocalFile() ? m_playableUrl.toLocalFile() : m_playableUrl.url();
 
135
}
 
136
 
 
137
void
 
138
MediaDeviceTrack::setUidUrl( const QString &newUidUrl ) const
 
139
{
 
140
    Q_UNUSED( newUidUrl )
 
141
}
 
142
 
 
143
QString
 
144
MediaDeviceTrack::prettyUrl() const
 
145
{
 
146
    return m_playableUrl.isLocalFile() ? m_playableUrl.toLocalFile() : QString( collection()->prettyName() + ": " + artist()->prettyName() + " - " + prettyName() );
 
147
}
 
148
 
 
149
bool
 
150
MediaDeviceTrack::isPlayable() const
 
151
{
 
152
    return true;
 
153
}
 
154
 
 
155
bool
 
156
MediaDeviceTrack::isEditable() const
 
157
{
 
158
    // TODO: Should only be true if disk mounted read/write, implement check later
 
159
    return true;
 
160
}
 
161
 
 
162
AlbumPtr
 
163
MediaDeviceTrack::album() const
 
164
{
 
165
    return AlbumPtr::staticCast( m_album );
 
166
}
 
167
 
 
168
ArtistPtr
 
169
MediaDeviceTrack::artist() const
 
170
{
 
171
    return ArtistPtr::staticCast( m_artist );
 
172
}
 
173
 
 
174
GenrePtr
 
175
MediaDeviceTrack::genre() const
 
176
{
 
177
    return GenrePtr::staticCast( m_genre );
 
178
}
 
179
 
 
180
ComposerPtr
 
181
MediaDeviceTrack::composer() const
 
182
{
 
183
    return ComposerPtr::staticCast( m_composer );
 
184
}
 
185
 
 
186
YearPtr
 
187
MediaDeviceTrack::year() const
 
188
{
 
189
    return YearPtr::staticCast( m_year );
 
190
}
 
191
 
 
192
QString
 
193
MediaDeviceTrack::comment() const
 
194
{
 
195
    return m_comment;
 
196
}
 
197
 
 
198
void
 
199
MediaDeviceTrack::setComment( const QString &newComment )
 
200
{
 
201
    m_comment = newComment;
 
202
}
 
203
 
 
204
double
 
205
MediaDeviceTrack::score() const
 
206
{
 
207
    return 0.0;
 
208
}
 
209
 
 
210
void
 
211
MediaDeviceTrack::setScore( double newScore )
 
212
{
 
213
    Q_UNUSED( newScore )
 
214
}
 
215
 
 
216
int
 
217
MediaDeviceTrack::rating() const
 
218
{
 
219
    return m_rating;
 
220
}
 
221
 
 
222
void
 
223
MediaDeviceTrack::setRating( int newRating )
 
224
{
 
225
    m_rating = newRating;
 
226
    notifyObservers();
 
227
}
 
228
 
 
229
qint64
 
230
MediaDeviceTrack::length() const
 
231
{
 
232
    return m_length;
 
233
}
 
234
 
 
235
void
 
236
MediaDeviceTrack::setFileSize( int newFileSize )
 
237
{
 
238
    m_filesize = newFileSize;
 
239
}
 
240
 
 
241
int
 
242
MediaDeviceTrack::filesize() const
 
243
{
 
244
    // TODO: NYI, seems to cause crashing on transferring tracks to mediadevice
 
245
    return m_filesize;
 
246
}
 
247
 
 
248
int
 
249
MediaDeviceTrack::bitrate() const
 
250
{
 
251
    return m_bitrate;
 
252
}
 
253
 
 
254
void
 
255
MediaDeviceTrack::setBitrate( int newBitrate )
 
256
{
 
257
    m_bitrate = newBitrate;
 
258
}
 
259
 
 
260
int
 
261
MediaDeviceTrack::sampleRate() const
 
262
{
 
263
    return m_samplerate;
 
264
}
 
265
 
 
266
void
 
267
MediaDeviceTrack::setSamplerate( int newSamplerate )
 
268
{
 
269
    m_samplerate = newSamplerate;
 
270
}
 
271
 
 
272
qreal
 
273
MediaDeviceTrack::bpm() const
 
274
{
 
275
    return m_bpm;
 
276
}
 
277
void
 
278
MediaDeviceTrack::setBpm( const qreal newBpm )
 
279
{
 
280
    m_bpm = newBpm;
 
281
}
 
282
 
 
283
int
 
284
MediaDeviceTrack::trackNumber() const
 
285
{
 
286
    return m_trackNumber;
 
287
}
 
288
 
 
289
void
 
290
MediaDeviceTrack::setTrackNumber( int newTrackNumber )
 
291
{
 
292
    m_trackNumber = newTrackNumber;
 
293
}
 
294
 
 
295
int
 
296
MediaDeviceTrack::discNumber() const
 
297
{
 
298
    return m_discNumber;
 
299
}
 
300
 
 
301
void
 
302
MediaDeviceTrack::setDiscNumber( int newDiscNumber )
 
303
{
 
304
    m_discNumber = newDiscNumber;
 
305
}
 
306
 
 
307
int
 
308
MediaDeviceTrack::playCount() const
 
309
{
 
310
    return m_playCount;
 
311
}
 
312
 
 
313
void
 
314
MediaDeviceTrack::setPlayCount( const int newCount )
 
315
{
 
316
    m_playCount = newCount;
 
317
}
 
318
 
 
319
QDateTime
 
320
MediaDeviceTrack::lastPlayed() const
 
321
{
 
322
    return m_lastPlayed;
 
323
}
 
324
 
 
325
void
 
326
MediaDeviceTrack::setLastPlayed( const QDateTime &newTime )
 
327
{
 
328
    m_lastPlayed = newTime;
 
329
}
 
330
 
 
331
QString
 
332
MediaDeviceTrack::type() const
 
333
{
 
334
    if( m_type.isEmpty() && !m_playableUrl.path().isEmpty() )
 
335
    {
 
336
        QString path = m_playableUrl.path();
 
337
        return path.mid( path.lastIndexOf( '.' ) + 1 );
 
338
    }
 
339
    return m_type;
 
340
}
 
341
 
 
342
void
 
343
MediaDeviceTrack::setType( const QString & type )
 
344
{
 
345
    m_type = type;
 
346
}
 
347
 
 
348
void
 
349
MediaDeviceTrack::prepareToPlay()
 
350
{
 
351
    Meta::MediaDeviceTrackPtr ptr = Meta::MediaDeviceTrackPtr( this );
 
352
 
 
353
    if( m_collection && m_collection.data()->handler() )
 
354
        m_collection.data()->handler()->prepareToPlay( ptr );
 
355
}
 
356
 
 
357
// TODO: employ observers (e.g. Handler) to take care of updated
 
358
// data
 
359
/*
 
360
void
 
361
MediaDeviceTrack::subscribe( Observer *observer )
 
362
{
 
363
    Q_UNUSED( observer )    //read only
 
364
}
 
365
 
 
366
void
 
367
MediaDeviceTrack::unsubscribe( Observer *observer )
 
368
{
 
369
    Q_UNUSED( observer )    //read only
 
370
}
 
371
*/
 
372
// TODO: implement this for MediaDeviceCollectionLocation
 
373
bool
 
374
MediaDeviceTrack::inCollection() const
 
375
{
 
376
    return true;
 
377
}
 
378
 
 
379
Collections::Collection*
 
380
MediaDeviceTrack::collection() const
 
381
{
 
382
    return m_collection.data();
 
383
}
 
384
 
 
385
bool
 
386
MediaDeviceTrack::hasCapabilityInterface( Capabilities::Capability::Type type ) const
 
387
{
 
388
    switch( type )
 
389
    {
 
390
        case Capabilities::Capability::Editable:
 
391
            return m_collection ? m_collection.data()->isWritable() : false;
 
392
 
 
393
        case Capabilities::Capability::Updatable:
 
394
            return m_collection ? m_collection.data()->isWritable() : false;
 
395
 
 
396
        default:
 
397
            return false;
 
398
    }
 
399
}
 
400
 
 
401
Capabilities::Capability*
 
402
MediaDeviceTrack::createCapabilityInterface( Capabilities::Capability::Type type )
 
403
{
 
404
    switch( type )
 
405
    {
 
406
        case Capabilities::Capability::Editable:
 
407
            return new EditCapabilityMediaDevice( this );
 
408
        case Capabilities::Capability::Updatable:
 
409
            return new UpdateCapabilityMediaDevice( m_collection.data() );
 
410
 
 
411
        default:
 
412
            return 0;
 
413
    }
 
414
}
 
415
 
 
416
void
 
417
MediaDeviceTrack::setAlbum( const QString &newAlbum )
 
418
{
 
419
    MediaDeviceAlbumPtr albumPtr;
 
420
    MediaDeviceTrackPtr track( this );
 
421
    AlbumMap albumMap = m_collection.data()->memoryCollection()->albumMap();
 
422
 
 
423
    // do cleanup of soon to be previous album
 
424
 
 
425
    albumPtr = m_album;
 
426
    if ( !albumPtr.isNull() )
 
427
    {
 
428
        // remove track from previous album's tracklist
 
429
        albumPtr->remTrack( track );
 
430
        // if album's tracklist is empty, remove album from albummap
 
431
        if( albumPtr->tracks().isEmpty() )
 
432
            albumMap.remove( albumPtr->name() );
 
433
    }
 
434
 
 
435
    // change to a new album
 
436
 
 
437
    // check for the existence of the album to be set to,
 
438
    // if album exists, reuse, else create
 
439
 
 
440
    if ( albumMap.contains( newAlbum ) )
 
441
    {
 
442
        albumPtr = MediaDeviceAlbumPtr::staticCast( albumMap.value( newAlbum ) );
 
443
    }
 
444
    else
 
445
    {
 
446
        albumPtr = MediaDeviceAlbumPtr( new MediaDeviceAlbum( m_collection.data(), newAlbum ) );
 
447
        albumMap.insert( newAlbum, AlbumPtr::staticCast( albumPtr ) );
 
448
    }
 
449
 
 
450
    // add track to album's tracklist
 
451
    albumPtr->addTrack( track );
 
452
    // set track's album to the new album
 
453
    setAlbum( albumPtr );
 
454
 
 
455
    m_collection.data()->memoryCollection()->acquireWriteLock();
 
456
    m_collection.data()->memoryCollection()->setAlbumMap( albumMap );
 
457
    m_collection.data()->memoryCollection()->releaseLock();
 
458
}
 
459
 
 
460
void
 
461
MediaDeviceTrack::setAlbumArtist( const QString &newAlbumArtist )
 
462
{
 
463
    DEBUG_BLOCK
 
464
 
 
465
    if( m_album.isNull() || newAlbumArtist.isEmpty() )
 
466
        return;
 
467
 
 
468
    MediaDeviceArtistPtr artistPtr;
 
469
    ArtistMap artistMap = m_collection.data()->memoryCollection()->artistMap();
 
470
    artistPtr = MediaDeviceArtistPtr::staticCast( m_album->albumArtist() );
 
471
 
 
472
    if( !artistPtr.isNull() )
 
473
    {
 
474
        artistPtr->remAlbum( m_album );
 
475
        if( artistPtr->tracks().isEmpty() && artistPtr->albums().isEmpty() )
 
476
            artistMap.remove( artistPtr->name() );
 
477
    }
 
478
 
 
479
    if( artistMap.contains( newAlbumArtist ) )
 
480
        artistPtr = MediaDeviceArtistPtr::staticCast( artistMap.value( newAlbumArtist ) );
 
481
    else
 
482
    {
 
483
        artistPtr = MediaDeviceArtistPtr( new MediaDeviceArtist( newAlbumArtist ) );
 
484
        artistMap.insert( newAlbumArtist, ArtistPtr::staticCast( artistPtr ) );
 
485
    }
 
486
 
 
487
    artistPtr->addAlbum( m_album );
 
488
    m_album->setAlbumArtist( artistPtr );
 
489
 
 
490
    m_collection.data()->memoryCollection()->acquireWriteLock();
 
491
    m_collection.data()->memoryCollection()->setArtistMap( artistMap );
 
492
    m_collection.data()->memoryCollection()->releaseLock();
 
493
}
 
494
 
 
495
void
 
496
MediaDeviceTrack::setArtist( const QString &newArtist )
 
497
{
 
498
    DEBUG_BLOCK
 
499
 
 
500
    MediaDeviceArtistPtr artistPtr;
 
501
    MediaDeviceTrackPtr track( this );
 
502
    ArtistMap artistMap = m_collection.data()->memoryCollection()->artistMap();
 
503
 
 
504
    // do cleanup of soon to be previous artist
 
505
 
 
506
    artistPtr = m_artist;
 
507
    // remove track from previous artist's tracklist
 
508
    if ( !artistPtr.isNull() )
 
509
    {
 
510
        artistPtr->remTrack( track );
 
511
        // if artist's tracklist is empty, remove artist from artistmap
 
512
        if( artistPtr->tracks().isEmpty() && artistPtr->albums().isEmpty() )
 
513
            artistMap.remove( artistPtr->name() );
 
514
    }
 
515
 
 
516
    // change to a new artist
 
517
 
 
518
    // check for the existence of the artist to be set to,
 
519
    // if artist exists, reuse, else create
 
520
 
 
521
    if ( artistMap.contains( newArtist ) )
 
522
    {
 
523
        artistPtr = MediaDeviceArtistPtr::staticCast( artistMap.value( newArtist ) );
 
524
    }
 
525
    else
 
526
    {
 
527
        artistPtr = MediaDeviceArtistPtr( new MediaDeviceArtist( newArtist ) );
 
528
        artistMap.insert( newArtist, ArtistPtr::staticCast( artistPtr ) );
 
529
    }
 
530
 
 
531
    // add track to artist's tracklist
 
532
    artistPtr->addTrack( track );
 
533
    // set track's artist to the new artist
 
534
    setArtist( artistPtr );
 
535
 
 
536
    m_collection.data()->memoryCollection()->acquireWriteLock();
 
537
    m_collection.data()->memoryCollection()->setArtistMap( artistMap );
 
538
    m_collection.data()->memoryCollection()->releaseLock();
 
539
}
 
540
 
 
541
void
 
542
MediaDeviceTrack::setGenre( const QString &newGenre )
 
543
{
 
544
    DEBUG_BLOCK
 
545
 
 
546
    MediaDeviceGenrePtr genrePtr;
 
547
    MediaDeviceTrackPtr track( this );
 
548
    GenreMap genreMap = m_collection.data()->memoryCollection()->genreMap();
 
549
 
 
550
    // do cleanup of soon to be previous genre
 
551
 
 
552
    genrePtr = m_genre;
 
553
    if ( !genrePtr.isNull() )
 
554
    {
 
555
        // remove track from previous genre's tracklist
 
556
        genrePtr->remTrack( track );
 
557
        // if genre's tracklist is empty, remove genre from genremap
 
558
        if( genrePtr->tracks().isEmpty() )
 
559
            genreMap.remove( genrePtr->name() );
 
560
    }
 
561
 
 
562
    // change to a new genre
 
563
 
 
564
    // check for the existence of the genre to be set to,
 
565
    // if genre exists, reuse, else create
 
566
 
 
567
    if ( genreMap.contains( newGenre ) )
 
568
    {
 
569
        genrePtr = MediaDeviceGenrePtr::staticCast( genreMap.value( newGenre ) );
 
570
    }
 
571
    else
 
572
    {
 
573
        genrePtr = MediaDeviceGenrePtr( new MediaDeviceGenre( newGenre ) );
 
574
        genreMap.insert( newGenre, GenrePtr::staticCast( genrePtr ) );
 
575
    }
 
576
 
 
577
    // add track to genre's tracklist
 
578
    genrePtr->addTrack( track );
 
579
    // set track's genre to the new genre
 
580
    setGenre( genrePtr );
 
581
 
 
582
    m_collection.data()->memoryCollection()->acquireWriteLock();
 
583
    m_collection.data()->memoryCollection()->setGenreMap( genreMap );
 
584
    m_collection.data()->memoryCollection()->releaseLock();
 
585
}
 
586
 
 
587
void
 
588
MediaDeviceTrack::setComposer( const QString &newComposer )
 
589
{
 
590
    DEBUG_BLOCK
 
591
 
 
592
    MediaDeviceComposerPtr composerPtr;
 
593
    MediaDeviceTrackPtr track( this );
 
594
    ComposerMap composerMap = m_collection.data()->memoryCollection()->composerMap();
 
595
 
 
596
    // do cleanup of soon to be previous composer
 
597
 
 
598
    composerPtr = m_composer;
 
599
    if ( !composerPtr.isNull() )
 
600
    {
 
601
        // remove track from previous composer's tracklist
 
602
        composerPtr->remTrack( track );
 
603
        // if composer's tracklist is empty, remove composer from composermap
 
604
        if( composerPtr->tracks().isEmpty() )
 
605
            composerMap.remove( composerPtr->name() );
 
606
    }
 
607
 
 
608
    // change to a new composer
 
609
 
 
610
    // check for the existence of the composer to be set to,
 
611
    // if composer exists, reuse, else create
 
612
 
 
613
    if ( composerMap.contains( newComposer ) )
 
614
    {
 
615
        composerPtr = MediaDeviceComposerPtr::staticCast( composerMap.value( newComposer ) );
 
616
    }
 
617
    else
 
618
    {
 
619
        composerPtr = MediaDeviceComposerPtr( new MediaDeviceComposer( newComposer ) );
 
620
        composerMap.insert( newComposer, ComposerPtr::staticCast( composerPtr ) );
 
621
    }
 
622
 
 
623
    // add track to composer's tracklist
 
624
    composerPtr->addTrack( track );
 
625
    // set track's composer to the new composer
 
626
    setComposer( composerPtr );
 
627
 
 
628
    m_collection.data()->memoryCollection()->acquireWriteLock();
 
629
    m_collection.data()->memoryCollection()->setComposerMap( composerMap );
 
630
    m_collection.data()->memoryCollection()->releaseLock();
 
631
}
 
632
 
 
633
void
 
634
MediaDeviceTrack::setYear( int newYear )
 
635
{
 
636
    DEBUG_BLOCK
 
637
 
 
638
    MediaDeviceYearPtr yearPtr;
 
639
    MediaDeviceTrackPtr track( this );
 
640
    YearMap yearMap = m_collection.data()->memoryCollection()->yearMap();
 
641
 
 
642
    // do cleanup of soon to be previous year
 
643
 
 
644
    yearPtr = m_year;
 
645
    if ( !yearPtr.isNull() )
 
646
    {
 
647
        // remove track from previous year's tracklist
 
648
        yearPtr->remTrack( track );
 
649
        // if year's tracklist is empty, remove year from yearmap
 
650
        if( yearPtr->tracks().isEmpty() )
 
651
            yearMap.remove( yearPtr->year() );
 
652
    }
 
653
 
 
654
    // change to a new year
 
655
 
 
656
    // check for the existence of the year to be set to,
 
657
    // if year exists, reuse, else create
 
658
 
 
659
    if ( yearMap.contains( newYear ) )
 
660
    {
 
661
        yearPtr = MediaDeviceYearPtr::staticCast( yearMap.value( newYear ) );
 
662
    }
 
663
    else
 
664
    {
 
665
        yearPtr = MediaDeviceYearPtr( new MediaDeviceYear( QString::number(newYear) ) );
 
666
        yearMap.insert( newYear, YearPtr::staticCast( yearPtr ) );
 
667
    }
 
668
 
 
669
    // add track to year's tracklist
 
670
    yearPtr->addTrack( track );
 
671
    // set track's year to the new year
 
672
    setYear( yearPtr );
 
673
 
 
674
    m_collection.data()->memoryCollection()->acquireWriteLock();
 
675
    m_collection.data()->memoryCollection()->setYearMap( yearMap );
 
676
    m_collection.data()->memoryCollection()->releaseLock();
 
677
}
 
678
 
 
679
void
 
680
MediaDeviceTrack::setAlbum( MediaDeviceAlbumPtr album )
 
681
{
 
682
    m_album = album;
 
683
}
 
684
 
 
685
void
 
686
MediaDeviceTrack::setArtist( MediaDeviceArtistPtr artist )
 
687
{
 
688
    m_artist = artist;
 
689
}
 
690
 
 
691
void
 
692
MediaDeviceTrack::setGenre( MediaDeviceGenrePtr genre )
 
693
{
 
694
    m_genre = genre;
 
695
}
 
696
 
 
697
void
 
698
MediaDeviceTrack::setComposer( MediaDeviceComposerPtr composer )
 
699
{
 
700
    m_composer = composer;
 
701
}
 
702
 
 
703
void
 
704
MediaDeviceTrack::setYear( MediaDeviceYearPtr year )
 
705
{
 
706
    m_year = year;
 
707
}
 
708
 
 
709
QString
 
710
MediaDeviceTrack::title() const
 
711
{
 
712
    return m_name;
 
713
}
 
714
 
 
715
void
 
716
MediaDeviceTrack::setTitle( const QString &title )
 
717
{
 
718
    m_name = title;
 
719
}
 
720
 
 
721
void
 
722
MediaDeviceTrack::setLength( qint64 length )
 
723
{
 
724
    m_length = length;
 
725
}
 
726
 
 
727
void
 
728
MediaDeviceTrack::endMetaDataUpdate()
 
729
{
 
730
    DEBUG_BLOCK
 
731
    // Update info in local mediadevice database struct
 
732
    debug() << "Observer number: " << m_observers.count();
 
733
    notifyObservers();
 
734
}
 
735
 
 
736
//MediaDeviceArtist
 
737
 
 
738
MediaDeviceArtist::MediaDeviceArtist( const QString &name )
 
739
    : Meta::Artist()
 
740
    , m_name( name )
 
741
    , m_tracks()
 
742
{
 
743
    //nothing to do
 
744
}
 
745
 
 
746
MediaDeviceArtist::~MediaDeviceArtist()
 
747
{
 
748
    //nothing to do
 
749
}
 
750
 
 
751
QString
 
752
MediaDeviceArtist::name() const
 
753
{
 
754
    return m_name;
 
755
}
 
756
 
 
757
TrackList
 
758
MediaDeviceArtist::tracks()
 
759
{
 
760
    return m_tracks;
 
761
}
 
762
 
 
763
AlbumList
 
764
MediaDeviceArtist::albums()
 
765
{
 
766
    return m_albums;
 
767
}
 
768
 
 
769
void
 
770
MediaDeviceArtist::addTrack( MediaDeviceTrackPtr track )
 
771
{
 
772
    m_tracks.append( TrackPtr::staticCast( track ) );
 
773
}
 
774
 
 
775
void
 
776
MediaDeviceArtist::remTrack( MediaDeviceTrackPtr track )
 
777
{
 
778
    m_tracks.removeOne( TrackPtr::staticCast( track ) );
 
779
}
 
780
 
 
781
void
 
782
MediaDeviceArtist::addAlbum( MediaDeviceAlbumPtr album )
 
783
{
 
784
    m_albums.append( AlbumPtr::staticCast( album ) );
 
785
}
 
786
 
 
787
void
 
788
MediaDeviceArtist::remAlbum( MediaDeviceAlbumPtr album )
 
789
{
 
790
    m_albums.removeOne( AlbumPtr::staticCast( album ) );
 
791
}
 
792
 
 
793
//---------------MediaDeviceAlbum-----------------------------------
 
794
 
 
795
MediaDeviceAlbum::MediaDeviceAlbum( Collections::MediaDeviceCollection *collection, const QString &name )
 
796
    : Meta::Album()
 
797
    , m_collection( collection )
 
798
    , m_artworkCapability( 0 )
 
799
    , m_name( name )
 
800
    , m_tracks()
 
801
    , m_isCompilation( false )
 
802
    , m_hasImage( true ) // assume it has a cover until proven otherwise
 
803
    , m_hasImageChecked( false )
 
804
    , m_image( QImage() )
 
805
    , m_albumArtist( 0 )
 
806
{
 
807
    MediaDeviceHandler *handler = m_collection->handler();
 
808
    if( handler && handler->hasCapabilityInterface( Handler::Capability::Artwork ) )
 
809
        m_artworkCapability = handler->create<Handler::ArtworkCapability>();
 
810
}
 
811
 
 
812
MediaDeviceAlbum::~MediaDeviceAlbum()
 
813
{
 
814
    //nothing to do
 
815
}
 
816
 
 
817
QString
 
818
MediaDeviceAlbum::name() const
 
819
{
 
820
    return m_name;
 
821
}
 
822
 
 
823
bool
 
824
MediaDeviceAlbum::isCompilation() const
 
825
{
 
826
    return m_isCompilation;
 
827
}
 
828
 
 
829
void
 
830
MediaDeviceAlbum::setIsCompilation( bool compilation )
 
831
{
 
832
    m_isCompilation = compilation;
 
833
}
 
834
 
 
835
bool
 
836
MediaDeviceAlbum::hasAlbumArtist() const
 
837
{
 
838
    return !m_albumArtist.isNull();
 
839
}
 
840
 
 
841
ArtistPtr
 
842
MediaDeviceAlbum::albumArtist() const
 
843
{
 
844
    return ArtistPtr::staticCast( m_albumArtist );
 
845
}
 
846
 
 
847
TrackList
 
848
MediaDeviceAlbum::tracks()
 
849
{
 
850
    return m_tracks;
 
851
}
 
852
 
 
853
bool
 
854
MediaDeviceAlbum::hasImage( int size ) const
 
855
{
 
856
    Q_UNUSED( size )
 
857
 
 
858
    if( !m_hasImageChecked )
 
859
        m_hasImage = ! const_cast<MediaDeviceAlbum*>( this )->image().isNull();
 
860
    return m_hasImage;
 
861
}
 
862
 
 
863
QPixmap
 
864
MediaDeviceAlbum::image( int size )
 
865
{
 
866
    if( m_name.isEmpty() || !m_hasImage )
 
867
        return Meta::Album::image( size );
 
868
 
 
869
    if( !m_image.isNull() )
 
870
    {
 
871
        if( !size )
 
872
            return QPixmap::fromImage(m_image);
 
873
        return QPixmap::fromImage(m_image).scaled( QSize( size, size ), Qt::KeepAspectRatio );
 
874
    }
 
875
    if( m_artworkCapability )
 
876
    {
 
877
        MediaDeviceTrackPtr track = MediaDeviceTrackPtr::dynamicCast( m_tracks.first() );
 
878
        QImage cover = m_artworkCapability->getCover( track );
 
879
 
 
880
        if( !cover.isNull() )
 
881
        {
 
882
            m_hasImage = true;
 
883
            m_image = cover;
 
884
            if( !size )
 
885
                return QPixmap::fromImage(cover);
 
886
            return QPixmap::fromImage(cover.scaled( QSize( size, size ), Qt::KeepAspectRatio ));
 
887
        }
 
888
        else
 
889
            m_hasImage = false;
 
890
        m_hasImageChecked = true;
 
891
    }
 
892
    return Meta::Album::image( size );
 
893
}
 
894
 
 
895
bool
 
896
MediaDeviceAlbum::canUpdateImage() const
 
897
{
 
898
    if( m_artworkCapability )
 
899
        return m_artworkCapability->canUpdateCover();
 
900
    return false;
 
901
}
 
902
 
 
903
// TODO: forward setImage calls to handler
 
904
void
 
905
MediaDeviceAlbum::setImage( const QImage &image )
 
906
{
 
907
    if( m_artworkCapability && m_artworkCapability->canUpdateCover() )
 
908
    {
 
909
        m_image = image;
 
910
        m_hasImage = true;
 
911
        m_artworkCapability->setCover( MediaDeviceAlbumPtr( this ), image );
 
912
    }
 
913
}
 
914
 
 
915
void
 
916
MediaDeviceAlbum::setImagePath( const QString &path )
 
917
{
 
918
    if( m_artworkCapability && m_artworkCapability->canUpdateCover() )
 
919
    {
 
920
        m_hasImage = true;
 
921
        m_artworkCapability->setCoverPath( MediaDeviceAlbumPtr( this ), path );
 
922
    }
 
923
}
 
924
 
 
925
// TODO: forward call to handler to remove image, etc.
 
926
void
 
927
MediaDeviceAlbum::removeImage()
 
928
{
 
929
    Meta::Album::removeImage();
 
930
}
 
931
 
 
932
bool
 
933
MediaDeviceAlbum::hasCapabilityInterface( Capabilities::Capability::Type type ) const
 
934
{
 
935
    switch( type )
 
936
    {
 
937
        case Capabilities::Capability::Actions:
 
938
            return true;
 
939
        default:
 
940
            return false;
 
941
    }
 
942
}
 
943
 
 
944
Capabilities::Capability*
 
945
MediaDeviceAlbum::createCapabilityInterface( Capabilities::Capability::Type type )
 
946
{
 
947
    switch( type )
 
948
    {
 
949
        case Capabilities::Capability::Actions:
 
950
        {
 
951
            QList<QAction*> actions;
 
952
            if( canUpdateImage() )
 
953
            {
 
954
                QAction *separator          = new QAction( m_collection );
 
955
                QAction *displayCoverAction = new DisplayCoverAction( m_collection, AlbumPtr::dynamicCast( MediaDeviceAlbumPtr(this) ) );
 
956
                QAction *unsetCoverAction   = new UnsetCoverAction( m_collection, AlbumPtr::dynamicCast( MediaDeviceAlbumPtr(this) ) );
 
957
 
 
958
                separator->setSeparator( true );
 
959
 
 
960
                actions.append( separator );
 
961
                actions.append( displayCoverAction );
 
962
                actions.append( new FetchCoverAction( m_collection, AlbumPtr::staticCast( MediaDeviceAlbumPtr(this) ) ) );
 
963
                actions.append( new SetCustomCoverAction( m_collection, AlbumPtr::staticCast( MediaDeviceAlbumPtr(this) ) ) );
 
964
                if( !hasImage() )
 
965
                {
 
966
                    displayCoverAction->setEnabled( false );
 
967
                    unsetCoverAction->setEnabled( false );
 
968
                }
 
969
                actions.append( unsetCoverAction );
 
970
            }
 
971
            return new Capabilities::ActionsCapability( actions );
 
972
        }
 
973
 
 
974
        default:
 
975
            return 0;
 
976
    }
 
977
}
 
978
 
 
979
void
 
980
MediaDeviceAlbum::addTrack( MediaDeviceTrackPtr track )
 
981
{
 
982
    m_tracks.append( TrackPtr::staticCast( track ) );
 
983
}
 
984
 
 
985
void
 
986
MediaDeviceAlbum::remTrack( MediaDeviceTrackPtr track )
 
987
{
 
988
    m_tracks.removeOne( TrackPtr::staticCast( track ) );
 
989
}
 
990
 
 
991
void
 
992
MediaDeviceAlbum::setAlbumArtist( MediaDeviceArtistPtr artist )
 
993
{
 
994
    m_albumArtist = artist;
 
995
}
 
996
 
 
997
//MediaDeviceComposer
 
998
 
 
999
MediaDeviceComposer::MediaDeviceComposer( const QString &name )
 
1000
    : Meta::Composer()
 
1001
    , m_name( name )
 
1002
    , m_tracks()
 
1003
{
 
1004
    //nothing to do
 
1005
}
 
1006
 
 
1007
MediaDeviceComposer::~MediaDeviceComposer()
 
1008
{
 
1009
    //nothing to do
 
1010
}
 
1011
 
 
1012
QString
 
1013
MediaDeviceComposer::name() const
 
1014
{
 
1015
    return m_name;
 
1016
}
 
1017
 
 
1018
TrackList
 
1019
MediaDeviceComposer::tracks()
 
1020
{
 
1021
    return m_tracks;
 
1022
}
 
1023
 
 
1024
void
 
1025
MediaDeviceComposer::addTrack( MediaDeviceTrackPtr track )
 
1026
{
 
1027
    m_tracks.append( TrackPtr::staticCast( track ) );
 
1028
}
 
1029
 
 
1030
void
 
1031
MediaDeviceComposer::remTrack( MediaDeviceTrackPtr track )
 
1032
{
 
1033
    m_tracks.removeOne( TrackPtr::staticCast( track ) );
 
1034
}
 
1035
 
 
1036
//---------------MediaDeviceGenre-----------------------------------
 
1037
 
 
1038
MediaDeviceGenre::MediaDeviceGenre( const QString &name )
 
1039
    : Meta::Genre()
 
1040
    , m_name( name )
 
1041
    , m_tracks()
 
1042
{
 
1043
    //nothing to do
 
1044
}
 
1045
 
 
1046
MediaDeviceGenre::~MediaDeviceGenre()
 
1047
{
 
1048
    //nothing to do
 
1049
}
 
1050
 
 
1051
QString
 
1052
MediaDeviceGenre::name() const
 
1053
{
 
1054
    return m_name;
 
1055
}
 
1056
 
 
1057
TrackList
 
1058
MediaDeviceGenre::tracks()
 
1059
{
 
1060
    return m_tracks;
 
1061
}
 
1062
 
 
1063
void
 
1064
MediaDeviceGenre::addTrack( MediaDeviceTrackPtr track )
 
1065
{
 
1066
    m_tracks.append( TrackPtr::staticCast( track ) );
 
1067
}
 
1068
 
 
1069
void
 
1070
MediaDeviceGenre::remTrack( MediaDeviceTrackPtr track )
 
1071
{
 
1072
    m_tracks.removeOne( TrackPtr::staticCast( track ) );
 
1073
}
 
1074
 
 
1075
 
 
1076
//MediaDeviceYear
 
1077
 
 
1078
MediaDeviceYear::MediaDeviceYear( const QString &name )
 
1079
    : Meta::Year()
 
1080
    , m_name( name )
 
1081
    , m_tracks()
 
1082
{
 
1083
    //nothing to do
 
1084
}
 
1085
 
 
1086
MediaDeviceYear::~MediaDeviceYear()
 
1087
{
 
1088
    //nothing to do
 
1089
}
 
1090
 
 
1091
QString
 
1092
MediaDeviceYear::name() const
 
1093
{
 
1094
    return m_name;
 
1095
}
 
1096
 
 
1097
TrackList
 
1098
MediaDeviceYear::tracks()
 
1099
{
 
1100
    return m_tracks;
 
1101
}
 
1102
 
 
1103
void
 
1104
MediaDeviceYear::addTrack( MediaDeviceTrackPtr track )
 
1105
{
 
1106
    m_tracks.append( TrackPtr::staticCast( track ) );
 
1107
}
 
1108
 
 
1109
void
 
1110
MediaDeviceYear::remTrack( MediaDeviceTrackPtr track )
 
1111
{
 
1112
    m_tracks.removeOne( TrackPtr::staticCast( track ) );
 
1113
}
 
1114
 
 
1115
#include "mediadevicemeta.moc"