summaryrefslogtreecommitdiffstats
path: root/amarok/src/mediadevice/riokarma/riokarmamediadevice.cpp
blob: c055661200d097fd37cc1b8096fe5b6015de7044 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
/***************************************************************************
 * copyright            : (C) 2006 Andy Kelk <andy@mopoke.co.uk>           *
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 2 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/

 /**
  *  Rio Karma media device
  *  @author Andy Kelk <andy@mopoke.co.uk>
  *  @see http://linux-karma.sourceforge.net/
  */

#define DEBUG_PREFIX "RioKarmaMediaDevice"

#include <config.h>
#include "riokarmamediadevice.h"

AMAROK_EXPORT_PLUGIN( RioKarmaMediaDevice )

// Amarok
#include <debug.h>
#include <metabundle.h>
#include <statusbar/statusbar.h>
#include <statusbar/popupMessage.h>

// KDE
#include <kapplication.h>
#include <kiconloader.h>
#include <kpopupmenu.h>

// Qt
#include <tqdir.h>
#include <tqlistview.h>
#include <tqmap.h>


/**
 * RioKarmaMediaDevice Class
 */

RioKarmaMediaDevice::RioKarmaMediaDevice() : MediaDevice()
{
    m_name = "Rio Karma";
    setDisconnected();
    m_hasMountPoint = true;
    m_syncStats = false;
    m_transcode = false;
    m_transcodeAlways = false;
    m_transcodeRemove = false;
    m_configure = false;
    m_customButton = false;
    m_transfer = true;
}

void
RioKarmaMediaDevice::init( MediaBrowser *parent )
{
    MediaDevice::init( parent );
}

bool
RioKarmaMediaDevice::isConnected()
{
    return m_rio >= 0 ? true : false;
}

/**
 * File types that we support
 */
QStringList
RioKarmaMediaDevice::supportedFiletypes()
{
    TQStringList supportedFiles;
    supportedFiles << "mp3";
    supportedFiles << "ogg";
    supportedFiles << "wma";
    supportedFiles << "flac";
    return supportedFiles;
}

/**
 * Copy a track to the device
 */
MediaItem
*RioKarmaMediaDevice::copyTrackToDevice( const MetaBundle &bundle )
{
    DEBUG_BLOCK

    TQString genericError = i18n( "Could not send track" );

    if( m_fileNameToItem[ bundle.filename() ] != 0 )
    {
        // track already exists. don't do anything (for now).
        debug() << "Track already exists on device." << endl;
        Amarok::StatusBar::instance()->shortLongMessage(
            genericError,
            i18n( "Track already exists on device" ),
            KDE::StatusBar::Error
        );
        return 0;
    }

    int fid = lk_rio_write( m_rio, bundle.url().path().utf8() );

    if( fid < 0 )
    {
        debug() << "Could not write file" << fid << endl;
        return 0;
    }

    MetaBundle temp( bundle );
    RioKarmaTrack *taggedTrack = new RioKarmaTrack( fid );
    taggedTrack->setBundle( temp );

    // cache the track
    updateRootItems();
    return addTrackToView( taggedTrack );
}

/**
 * Write any pending changes to the device, such as database changes
 */
void
RioKarmaMediaDevice::synchronizeDevice()
{
    DEBUG_BLOCK
    int ret;
    ret = lk_karma_write_smalldb();
    if( ret )
        debug() << "error writing smalldb file" << endl;
}

/**
 * Find an existing track
 */
MediaItem
*RioKarmaMediaDevice::trackExists( const MetaBundle &bundle )
{
    MediaItem *artist = dynamic_cast<MediaItem *>( m_view->findItem( bundle.artist(), 0 ) );
    if( artist )
    {
        MediaItem *album = dynamic_cast<MediaItem *>( artist->findItem( bundle.album() ) );
        if( album )
        {
            MediaItem *track =  dynamic_cast<MediaItem *>( album->findItem( bundle.title() ) );
            if( track )
            {
                if( track->bundle()->track() == bundle.track() )
                    return track;
            }
        }
    }
    return 0;
}

/**
 * Create a new playlist
 * @note Playlists not implemented yet... :-)
 */
RioKarmaMediaItem
*RioKarmaMediaDevice::newPlaylist( const TQString &name, MediaItem *parent, TQPtrList<MediaItem> items )
{
    Q_UNUSED( name );
    Q_UNUSED( parent );
    Q_UNUSED( items );
    return 0;
}

/**
 * Add an item to a playlist
 * @note Playlists not implemented yet... :-)
 */
void
RioKarmaMediaDevice::addToPlaylist( MediaItem *mlist, MediaItem *after, TQPtrList<MediaItem> items )
{
    Q_UNUSED( mlist );
    Q_UNUSED( after );
    Q_UNUSED( items );
}

/**
 * Recursively remove MediaItem from the device
 */
int
RioKarmaMediaDevice::deleteItemFromDevice(MediaItem* item, int flags )
{

    int result = 0;
    if( isCanceled() )
    {
        return -1;
    }
    MediaItem *next = 0;

    switch( item->type() )
    {
        case MediaItem::TRACK:
            if( isCanceled() )
                break;
            if( item )
            {
                int res = deleteRioTrack( dynamic_cast<RioKarmaMediaItem *> ( item ) );
                if( res >=0 && result >= 0 )
                    result += res;
                else
                    result = -1;
            }
            break;
        case MediaItem::ALBUM:
        case MediaItem::ARTIST:
            // Recurse through the lists
            next = 0;

            if( isCanceled() )
                break;

            for( MediaItem *it = dynamic_cast<MediaItem *>( item->firstChild() ); it ; it = next )
            {
                next = dynamic_cast<MediaItem *>( it->nextSibling() );
                int res = deleteItemFromDevice( it, flags );
                if( res >= 0 && result >= 0 )
                    result += res;
                else
                    result = -1;

            }
            if( item )
                delete dynamic_cast<MediaItem *>( item );
            break;
        default:
            result = 0;
    }
    return result;
}

/**
 * Actually delete a track from the Rio
 */
int
RioKarmaMediaDevice::deleteRioTrack( RioKarmaMediaItem *trackItem )
{

    DEBUG_BLOCK

    debug() << "delete this fid : " << trackItem->track()->id() << endl;

    // delete the file
    int status = lk_karma_delete_file( m_rio, trackItem->track()->id() );
    if( status < 0 ) {
        debug() << "delete track failed" << endl;
        return -1;
    }
    debug() << "track deleted" << endl;

    // delete the properties (db entry)
    status = lk_properties_del_property( trackItem->track()->id() );
    if( status < 0 ) {
        debug() << "delete property failed" << endl;
        return -1;
    }
    debug() << "property deleted" << endl;

    // remove from the listview
    delete trackItem;
    kapp->processEvents( 100 );

    return 1;
}

/**
 * Connect to device, and populate m_view with MediaItems
 */
bool
RioKarmaMediaDevice::openDevice( bool silent )
{
    DEBUG_BLOCK

    Q_UNUSED( silent );

    TQDir dir( mountPoint() );
    if( !dir.exists() )
    {
        Amarok::StatusBar::instance()->longMessage(
                i18n( "Media device: Mount point %1 does not exist" ).arg( mountPoint() ),
                KDE::StatusBar::Error );
        return false;
    }

    if( m_rio >= 0 )
        return true;

    TQString genericError = i18n( "Could not connect to Rio Karma" );

    char *mount = qstrdup( mountPoint().utf8() );
    m_rio = lk_karma_connect( mount );

    debug() << "Rio karma : " << m_rio << endl;

    if( m_rio < 0 )
    {
        debug()<< "Error connecting" << endl;
        Amarok::StatusBar::instance()->shortLongMessage( genericError, i18n( "Rio Karma could not be opened" ), KDE::StatusBar::Error );
        setDisconnected();
        return false;
    }

    lk_karma_use_smalldb();

    lk_karma_write_dupes( 1 );

    RioKarmaMediaDevice::readKarmaMusic();

    return true;
}

/**
 * Wrap up any loose ends and close the device
 */
bool
RioKarmaMediaDevice::closeDevice()
{
    DEBUG_BLOCK
    clearItems();
    setDisconnected();
    return true;
}

/**
 * Get the capacity and freespace available on the device, in KB
 */
bool
RioKarmaMediaDevice::getCapacity( KIO::filesize_t *total, KIO::filesize_t *available )
{
    if( !isConnected() )
        return false;

    uint32_t numfiles;
    uint64_t disksize;
    uint64_t freespace;
    uint32_t maxfileid;

    if( lk_karma_get_storage_details( m_rio, 0, &numfiles, &disksize, &freespace, &maxfileid ) == 0 )
    {
        *total = disksize;
        *available = freespace;

        return true;
    }
    else
    {
        return false;
    }
}

/**
 * Current device ID (usually starts at 0)
 */
int
RioKarmaMediaDevice::current_id()
{
    return m_rio;
}

/**
 * We use -1 device ID to show a disconnected device.
 * This just sets the device ID to that.
 */
void
RioKarmaMediaDevice::setDisconnected()
{
    m_rio = -1;
}

/**
 * Handle clicking of the right mouse button
 */
void
RioKarmaMediaDevice::rmbPressed( TQListViewItem *qitem, const TQPoint &point, int )
{

    enum Actions {DELETE};

    RioKarmaMediaItem *item = static_cast<RioKarmaMediaItem *>( qitem );
    if( item )
    {
        KPopupMenu menu( m_view );
        menu.insertItem( SmallIconSet( Amarok::icon( "remove" ) ), i18n( "Delete from device" ), DELETE );

        int id =  menu.exec( point );
        switch( id )
        {
        case DELETE:
            MediaDevice::deleteFromDevice();
            break;
        }
        return;
    }

}

/**
 * Add a track to the current list view
 */
RioKarmaMediaItem
*RioKarmaMediaDevice::addTrackToView( RioKarmaTrack *track, RioKarmaMediaItem *item )
{
    TQString artistName = track->bundle()->artist();

    RioKarmaMediaItem *artist = dynamic_cast<RioKarmaMediaItem *>( m_view->findItem( artistName, 0 ) );
    if( !artist )
    {
        artist = new RioKarmaMediaItem( m_view );
        artist->m_device = this;
        artist->setText( 0, artistName );
        artist->setType( MediaItem::ARTIST );
    }

    TQString albumName = track->bundle()->album();
    RioKarmaMediaItem *album = dynamic_cast<RioKarmaMediaItem *>( artist->findItem( albumName ) );
    if( !album )
    {
        album = new RioKarmaMediaItem( artist );
        album->setText( 0, albumName );
        album->setType( MediaItem::ALBUM );
        album->m_device = this;
    }

    if( item )
        album->insertItem( item );
    else
    {
        item = new RioKarmaMediaItem( album );
        item->m_device = this;
        TQString titleName = track->bundle()->title();
        item->setTrack( track );
        item->m_order = track->bundle()->track();
        item->setText( 0, titleName );
        item->setType( MediaItem::TRACK );
        item->setBundle( track->bundle() );
        item->track()->setId( track->id() );
        m_fileNameToItem[ track->bundle()->filename() ] = item;
    }
    return item;
}

/**
 * Get karma tracks and add them to the listview
 */
int
RioKarmaMediaDevice::readKarmaMusic()
{

    DEBUG_BLOCK

    clearItems();


    TQString genericError = i18n( "Could not get music from Rio Karma" );

    int total = 100;
    int progress = 0;
    setProgress( progress, total ); // we don't know how many tracks. fake progress bar.

    kapp->processEvents( 100 );

    lk_karma_load_database( m_rio );

    int i;
    uint32_t *ret;

    kapp->processEvents( 100 );

    ret = lk_properties_andOrSearch( 0, 0, "fid", "" );

    if( ret == 0 )
    {
        debug()<< "Error reading tracks. NULL returned." << endl;
        Amarok::StatusBar::instance()->shortLongMessage( genericError, i18n( "Could not read Rio Karma tracks" ), KDE::StatusBar::Error );
        setDisconnected();
        hideProgress();
        return -1;
    }

    total = 0;
    // spin through once to determine size of the list
    for( i=0; ret[i] != 0; i++ )
    {
        total++;
    }
    setProgress( progress, total );
    // now process the tracks
    for( i=0; ret[i] != 0; i++ )
    {
        // check playlist
        if( qstrcmp( "playlist", lk_properties_get_property( ret[i], "type" ) ) == 0 )
        {
            // nothing for now...
            debug() << "Found a playlist at fid " << ret[i] << ". Skipping." << endl;
        }
        else
        {
            RioKarmaTrack *track = new RioKarmaTrack( ret[i] );
            track->readMetaData();
            addTrackToView( track );
        }
        progress++;
        setProgress( progress );
        if( progress % 50 == 0 )
            kapp->processEvents( 100 );
    }
    setProgress( total );
    hideProgress();

    return 0;
}

/**
 * Clear the current listview
 */
void
RioKarmaMediaDevice::clearItems()
{
    m_view->clear();
}

/**
 * RioKarmaTrack Class
 */
RioKarmaTrack::RioKarmaTrack( int Fid )
{
    m_id = Fid;
}


RioKarmaTrack::~RioKarmaTrack()
{
    m_itemList.setAutoDelete( true );
    while( m_itemList.count() > 0 )
    {
        delete m_itemList.first();
    }
}

/**
 * Read track properties from the Karma and set it on the track
 */
void
RioKarmaTrack::readMetaData()
{
    MetaBundle *bundle = new MetaBundle();

    bundle->setGenre( AtomicString( TQString::fromUtf8( lk_properties_get_property( m_id, "genre" ) ) ) );
    bundle->setArtist( AtomicString( TQString::fromUtf8( lk_properties_get_property( m_id, "artist" ) ) ) );
    bundle->setAlbum( AtomicString( TQString::fromUtf8( lk_properties_get_property( m_id, "source" ) ) ) );
    bundle->setTitle( AtomicString( TQString::fromUtf8( lk_properties_get_property( m_id, "title" ) ) ) );

    // translate codecs to file types
    TQString codec = TQCString( lk_properties_get_property( m_id, "codec" ) );
    if( codec == "mp3" )
        bundle->setFileType( MetaBundle::mp3 );
    else if( codec == "wma" )
        bundle->setFileType( MetaBundle::wma );
    else if( codec == "flac" )
        bundle->setFileType( MetaBundle::flac );
    else if( codec == "vorbis" )
        bundle->setFileType( MetaBundle::ogg );
    else
        bundle->setFileType( MetaBundle::other );

    bundle->setYear( TQString( lk_properties_get_property( m_id, "year" ) ).toUInt() );
    bundle->setTrack( TQString( lk_properties_get_property( m_id, "tracknr" ) ).toUInt() );
    bundle->setLength( TQString( lk_properties_get_property( m_id, "duration" ) ).toUInt() );

    this->setBundle( *bundle );
}

/**
 * Set this track's metabundle
 */
void
RioKarmaTrack::setBundle( MetaBundle &bundle )
{
    m_bundle = bundle;
}

/**
 * Add a child item
 */
void
RioKarmaTrack::addItem( const RioKarmaMediaItem *item )
{
    m_itemList.append( item );
}

/**
 * Remove a child item
 */
bool
RioKarmaTrack::removeItem( const RioKarmaMediaItem *item )
{
    m_itemList.remove( item );
    return m_itemList.isEmpty();
}