summaryrefslogtreecommitdiffstats
path: root/amarok/src/mediadevice/njb/njbmediadevice.cpp
blob: ec2cf48ea39981c5d44bdcb3098e3a48d69401a4 (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
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
/***************************************************************************
    copyright            : (C) 2006 by Andres Oton
    email                : andres.oton@gmail.com

    copyright            : (C) 2006 by T.R.Shashwath
    email                : trshash84@gmail.com
 ***************************************************************************/

/***************************************************************************
 *   This library is free software; you can redistribute it and/or modify  *
 *   it  under the terms of the GNU General Public License version 2 as    *
 *   published by the Free Software Foundation.                            *
 *                                                                         *
 *   This library is distributed in the hope that it will be useful, but   *
 *   WITHOUT ANY WARRANTY; without even the implied warranty of            *
 *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU     *
 *   Lesser General Public License for more details.                       *
 *                                                                         *
 *   You should have received a copy of the GNU General Public License     *
 *   along with this library; if not, write to the Free Software           *
 *   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,            *
 *   MA  02110-1301  USA                                                   *
 ***************************************************************************/

#include "njbmediadevice.h"

AMAROK_EXPORT_PLUGIN( NjbMediaDevice )


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


// KDE
#include <kapplication.h>
#include <kdebug.h>
#include <kfiledialog.h>
#include <kiconloader.h>       //smallIcon
#include <kinstance.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kpopupmenu.h>
#include <ktempdir.h>
#include <ktoolbarbutton.h>
#include <kurl.h>
#include <kurlrequester.h>     //downloadSelectedItems()
#include <kurlrequesterdlg.h>  //downloadSelectedItems()


// TQt
#include <tqdir.h>
#include <tqlistview.h>
#include <tqregexp.h>
#include <tqtooltip.h>
#include <tquuid.h>

// posix
#include <stdint.h>
#include <sys/stat.h>
#include <time.h>
#include <unistd.h>

namespace Amarok { extern TDEConfig *config( const TQString& ); }
njb_t *NjbMediaDevice::m_njb = 0;
// This function has NOT handled the request, so other functions may be called
// upon to do so
const int NJB_NOTHANDLED = 0;

// This function has handled the request, so no further processing is needed.
const int NJB_HANDLED = -1;

NjbMediaDevice::NjbMediaDevice(): MediaDevice()
{

    //	listAmarokPlayLists = new TQListView();
    m_name = i18n("NJB Media device");
    m_njb = 0;
    m_connected = false;
    m_libcount = 0;
    m_connected = false;
    m_customButton = true;
    m_td = 0;
    NJB_Set_Debug(0); // or try DD_SUBTRACE
    KToolBarButton* customButton = MediaBrowser::instance()->getToolBar()->getButton( MediaBrowser::CUSTOM );
    customButton->setText( i18n("Special device functions") );
    TQToolTip::remove( customButton );
    TQToolTip::add( customButton, i18n( "Special functions of your jukebox" ) );
}


NjbMediaDevice::~NjbMediaDevice()
{

}

bool
NjbMediaDevice::closeDevice()
{
    DEBUG_BLOCK

    if(m_connected)
    {
        NJB_Release( m_njb);
        m_connected = false;
    }
    m_connected = false;

    if( m_njb )
    {
        NJB_Close( m_njb);

        m_njb = 0;

    }

    debug()<< "Disconnected NJB device" << endl;

    clearItems();

    m_name = i18n("NJB Media device");
    debug() << "Done" << endl;
    return true;
}

void
NjbMediaDevice::unlockDevice()
{
}

bool
NjbMediaDevice::getCapacity(TDEIO::filesize_t* total, TDEIO::filesize_t* available)
{
    if(!m_connected)
	return false;


    u_int64_t itotal;
    u_int64_t ifree;

    if(NJB_Get_Disk_Usage(m_njb, &itotal, &ifree) == -1)
	return false;

    *total = itotal;
    *available = ifree;

    return true;

}


bool
NjbMediaDevice::isConnected()
{
    return m_connected;

}

bool
NjbMediaDevice::isPlayable(const MetaBundle& bundle)
{
    DEBUG_BLOCK
            ;
    if(bundle.fileType() == MetaBundle::mp3
       || bundle.fileType() == MetaBundle::wma)
        return true;

    return false;
}

bool
NjbMediaDevice::isPreferredFormat(const MetaBundle& bundle)
{
    DEBUG_BLOCK

    if(bundle.fileType() == MetaBundle::mp3)
        return true;
    else
        return false;
}

bool
NjbMediaDevice::lockDevice(bool tryOnly)
{
    // The device is "locked" upon connection - there's very little else we can do here.
    Q_UNUSED(tryOnly);
    return true;
}

bool
NjbMediaDevice::openDevice(bool)
{
    DEBUG_BLOCK


    if( m_njb )
        return true;

    TQString genericError = i18n( "Could not connect to Nomad device" );
    NJB_Set_Unicode( NJB_UC_UTF8 ); // I assume that UTF-8 is fine with everyone...

    int n;
    if( NJB_Discover( njbs, 0, &n) == -1 || n == 0 )
    {
        Amarok::StatusBar::instance()->shortLongMessage( genericError, i18n("A suitable Nomad device could not be found"), KDE::StatusBar::Error );
        debug() << ": no NJBs found\n";

        return false;
    }
    m_njb = &njbs[0];


    if( NJB_Open( m_njb ) == -1 )
    {
        Amarok::StatusBar::instance()->shortLongMessage( genericError, i18n("Nomad device could not be opened"), KDE::StatusBar::Error );


        return false;
    }

    TQString deviceName = NJB_Get_Device_Name( m_njb, 1 );
    TQString owner = NJB_Get_Owner_String( m_njb );
    m_name = deviceName + " (Owned by " + owner + ')';


    if( NJB_Capture(m_njb) == -1)
    {
        debug() << ": couldn't capture\n";
        m_connected = false;
    }
    else
    {
        m_connected = true;
        readJukeboxMusic();
    }

    return true;

}

int
NjbMediaDevice::deleteFromDevice(unsigned id)
{
    int status = NJB_Delete_Track( m_njb, id );

    if( status != NJB_SUCCESS)
    {
        debug() << ": NJB_Delete_Track failed" << endl;
        return -1;
    }

    // remove from the cache
    trackList.remove(trackList.findTrackById( id ) );

    return 1;
}

int
NjbMediaDevice::deleteItemFromDevice(MediaItem* item, int flags )
{
    DEBUG_BLOCK
    int result = 0;
    if ( isCanceled() || !item )
    {
        return -1;
    }
    MediaItem *next = 0;

    switch( item->type() )
    {
        case MediaItem::TRACK:
            if( isCanceled() )
                break;
            if( item )
            {
                deleteTrack( dynamic_cast<NjbMediaItem *> (item) );
                result++;
            }
            break;
        case MediaItem::ALBUM:
        case MediaItem::ARTIST:
            // Recurse through the lists, slashing and burning.

            if( isCanceled() )
                break;

            expandItem( dynamic_cast<TQListViewItem *>(item) );

            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;
}

int
NjbMediaDevice::deleteTrack(NjbMediaItem *trackItem)
{
    int status = NJB_Delete_Track( m_njb, trackItem->track()->id() );

    if( status != NJB_SUCCESS)
    {
        debug() << ": NJB_Delete_Track failed" << endl;
        Amarok::StatusBar::instance()->shortLongMessage( i18n( "Deleting failed" ), i18n( "Deleting track(s) failed." ), KDE::StatusBar::Error );
        return -1;
    }

    debug() << ": NJB_Delete_Track track deleted" << endl;

    // remove from the cache
    trackList.remove(trackList.findTrackById( trackItem->track()->id() ) );
    delete trackItem;
    return 1;
}

int
NjbMediaDevice::downloadSelectedItems()
{
    /* Copied from ifpmediadevice */
    TQString save = TQString();

    KURLRequesterDlg dialog( save, 0, 0 );
    dialog.setCaption( kapp->makeStdCaption( i18n( "Choose a Download Directory" ) ) );
    dialog.urlRequester()->setMode( KFile::Directory | KFile::ExistingOnly );
    dialog.exec();

    KURL destDir = dialog.selectedURL();
    if( destDir.isEmpty() )
	    return -1;

    destDir.adjustPath( 1 ); //add trailing slash
    TQDir dir;
    TQString path;

    TQPtrList<MediaItem> items;
    m_view->getSelectedLeaves( 0, &items );
    int result = 0;

    for( MediaItem *it = items.first(); it && !(m_canceled); it = items.next() )
    {
        path = destDir.path();
        if( it->type() == MediaItem::TRACK )
        {
            dynamic_cast<MediaBrowser *>( parent() )->queue()->addURL(path, dynamic_cast<MediaItem *>(it) );

        }
    }
    return result;

}


/**
 * Download the selected items and put them into the collection DB.
 * @return The number of files downloaded.
*/
int
NjbMediaDevice::downloadToCollection()
{
    // We will first download all files into a temp dir, and then call move to collection.

    TQPtrList<MediaItem> items;
    m_view->getSelectedLeaves( 0, &items );

    KTempDir tempdir( TQString::null ); // Default prefix is fine with us
    tempdir.setAutoDelete( true ); // We don't need it once the work is done.
    TQString path = tempdir.name(), filepath;
    KURL::List urls;
    for( MediaItem *it = items.first(); it && !(m_canceled); it = items.next() )
    {
        if( (it->type() == MediaItem::TRACK) )
        {
            NjbMediaItem* auxItem = dynamic_cast<NjbMediaItem *>( (it) );
	    if (!auxItem) {
		debug() << "Dynamic cast to NJB media item failed. " << endl;
		return -1;
	    }
            TQString track_id;
            track_id.setNum( auxItem->track()->id() );
            filepath = path + auxItem->bundle()->url().path();

            if( NJB_Get_Track( m_njb, auxItem->track()->id(), auxItem->bundle()->filesize(), filepath.utf8(), progressCallback, this)
                != NJB_SUCCESS )
            {
                debug() << "Get Track failed. " << endl;
                if( NJB_Error_Pending(m_njb) )
                {
                    const char *njbError;
                    while( (njbError = NJB_Error_Geterror(m_njb) ) )
                        error() << njbError << endl;
                }
                else
                    debug() << "No reason to report for failure" << endl;
            }
            urls << filepath;
        }
    }
    // Now, call the collection organizer.
    CollectionView::instance()->organizeFiles( urls, i18n( "Move Files To Collection" ), false );
    return 0;
}

MediaItem*
NjbMediaDevice::copyTrackToDevice(const MetaBundle& bundle)
{
    DEBUG_BLOCK

    if( m_canceled )
            return 0;
    trackValueList::const_iterator it_track = trackList.findTrackByName( bundle.filename() );
    if( it_track != trackList.end() )
    {
        deleteFromDevice( (*it_track)->id() );
    }

    // read the mp3 header
    int duration = bundle.length();

    if( !duration )
    {
        m_errMsg = i18n( "Not a valid mp3 file");
        return 0;
    }
    MetaBundle temp( bundle );

    NjbTrack *taggedTrack = new NjbTrack();

    taggedTrack->setBundle( temp );

    u_int32_t id;
    m_progressStart = time( 0);
    m_progressMessage = i18n("Copying / Sent %1%...");

    njb_songid_t* songid = NJB_Songid_New();
    taggedTrack->writeToSongid( songid );
    m_busy = true;
    kapp->processEvents( 100 );
    if(NJB_Send_Track (m_njb, bundle.url().path().utf8(), songid, progressCallback, this, &id) != NJB_SUCCESS)
    {
        debug() << ": NJB_Send_Track failed\n";
        if (NJB_Error_Pending(m_njb))
        {
            const char* njbError;
            while ((njbError = NJB_Error_Geterror(m_njb)))
                warning() << ": " << njbError << endl;
        }
        else
            debug() << ": No reason for failure reported.\n";

        m_busy = false;
        NJB_Songid_Destroy( songid );

        return 0;
    }

    m_busy = false;
    NJB_Songid_Destroy( songid );

    taggedTrack->setId( id );

    // cache the track
    trackList.append( taggedTrack );;

    return addTrackToView( taggedTrack );
}

void
NjbMediaDevice::copyTrackFromDevice( MediaItem *item )
{
    DEBUG_BLOCK
    trackValueList::iterator it;
    for( it = trackList.begin(); it != trackList.end(); it++)
        if( (*(*it)->bundle()) == *(item->bundle()) )
            break;

    NjbTrack *track((*it));

    TQString filename = item->bundle()->directory() + track->bundle()->filename();
    if( NJB_Get_Track( m_njb, track->id(), track->bundle()->filesize(), filename.utf8(), progressCallback, this)
        != NJB_SUCCESS )
    {
        debug() << "Get Track failed. " << endl;
        if( NJB_Error_Pending(m_njb) )
        {
            const char *njbError;
            while( (njbError = NJB_Error_Geterror(m_njb) ) )
                error() << njbError << endl;
        }
        else
            debug() << "No reason to report for failure" << endl;
    }
}

MediaItem*
NjbMediaDevice::newPlaylist(const TQString& name, MediaItem* parent, TQPtrList< MediaItem > items)
{
    DEBUG_BLOCK

    Q_UNUSED(parent);
    //MediaItem* newplaylist = new MediaItem(parent);

    NjbPlaylist playlist;
    int status = playlist.setName( name );

    if(status == NJB_SUCCESS)
    {
        //NjbMediaItem *newNjbPlayList = new NjbMediaItem(listAmarokPlayLists);
        for(MediaItem *item=items.first();item;item=items.next())
        {

            status = playlist.addTrack( item->bundle()->filename() );
            if( status == NJB_FAILURE)
            {
                //TODO: Show a message with the error.
            }
            else if( status != NJB_SUCCESS)
            {
                return 0;
            }


            //NjbMediaItem *nitem = dynamic_cast<NjbMediaItem *>(item);

        }

        status = playlist.update();
        if( status != NJB_SUCCESS)
        {
            return 0;
        }

    }

    //TODO:Crear un conversor de Playlists a las listas de Amarok
    return 0;
}

TQStringList
NjbMediaDevice::supportedFiletypes()
{
    TQStringList supportedFiles;
    supportedFiles << "mp3";
    supportedFiles << "wav";
    supportedFiles << "wma";
    return supportedFiles;
}

TransferDialog*
NjbMediaDevice::getTransferDialog()
{
    return m_td;
}

void
NjbMediaDevice::addConfigElements(TQWidget* arg1)
{
    MediaDevice::addConfigElements(arg1);
}

void
NjbMediaDevice::addToPlaylist(MediaItem* playlist, MediaItem* after, TQPtrList< MediaItem > items)
{
    MediaDevice::addToPlaylist(playlist, after, items);
}

void
NjbMediaDevice::applyConfig()
{
    MediaDevice::applyConfig();
}

void
NjbMediaDevice::cancelTransfer()
{
    m_canceled = true;
}

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

void
NjbMediaDevice::loadConfig()
{
    MediaDevice::loadConfig();
}

void
NjbMediaDevice::removeConfigElements(TQWidget* arg1)
{
    MediaDevice::removeConfigElements(arg1);
}

MediaItem *
NjbMediaDevice::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 )
        {
            return dynamic_cast<MediaItem *>( album->findItem( bundle.title() ) );
        }
    }
    return 0;
}

void
NjbMediaDevice::rmbPressed(TQListViewItem* qitem, const TQPoint& point, int )
{

    enum Actions { DOWNLOAD, DOWNLOAD_TO_COLLECTION, RENAME, DELETE};

    NjbMediaItem *item = static_cast<NjbMediaItem *>(qitem);
    if ( item )
    {
        KPopupMenu menu( m_view);
        menu.insertItem( SmallIconSet( Amarok::icon( "collection" ) ), i18n("Download file"), DOWNLOAD );
        menu.insertItem( SmallIconSet( Amarok::icon( "collection" ) ), i18n("Download to collection"), DOWNLOAD_TO_COLLECTION );
        menu.insertSeparator();
        //menu.insertItem( SmallIconSet( Amarok::icon( "edit" ) ), i18n( "Rename" ), RENAME );
        menu.insertItem( SmallIconSet( Amarok::icon( "remove" ) ), i18n( "Delete from device" ), DELETE );


        int id =  menu.exec( point );
        MediaItem *i;
        TQPtrList<MediaItem> items;
        switch( id )
        {
        case DOWNLOAD:
            downloadSelectedItems();

            break;

        case RENAME:
            //TODO: Implement rename tracks
            //m_view->rename( item, 0 );
            break;

        case DELETE:
            m_view->getSelectedLeaves( 0, &items );
            while( !items.isEmpty() )
            {
                i = items.first();
                MediaDevice::deleteFromDevice( i );
                items.remove(i);
            }
            readJukeboxMusic();
            break;
        case DOWNLOAD_TO_COLLECTION:
            downloadToCollection();
            break;
        }
        return;
    }

}

void
NjbMediaDevice::runTransferDialog()
{
    m_td = new TransferDialog( this );
    m_td->exec();
}

int
NjbMediaDevice::progressCallback(  u_int64_t sent, u_int64_t total, const char* /*buf*/, unsigned /*len*/, void* data)
{
    kapp->processEvents( 100 );

    NjbMediaDevice *njb_media = reinterpret_cast<NjbMediaDevice*>(data);

    if( njb_media->isCanceled() )
    {
        debug() << "Canceling transfer operation" << endl;
        njb_media->setCanceled( false );
        njb_media->setProgress( sent, total );
        return 1;
    }

    njb_media->setProgress( sent, total);

    return 0;
}

void
NjbMediaDevice::clearItems()
{
    m_view->clear();
}

/** Transfer musical info from the njb to local structures */
int
NjbMediaDevice::readJukeboxMusic( void )
{
//    DEBUG_BLOCK
    int result = NJB_SUCCESS;

    // First, read jukebox tracks
    if(trackList.isEmpty())
    {

        result = trackList.readFromDevice();
    }

    if(result == NJB_SUCCESS)
    {
        clearItems();

        /*m_playlistItem = new NjbMediaItem( m_view );
        m_playlistItem->setText( 0, i18n("Playlists") );
        m_playlistItem->m_order = -5;
        m_playlistItem->setType( MediaItem::PLAYLISTSROOT );*/

        kapp->processEvents( 100 );

        for( trackValueList::iterator it = trackList.begin(); it != trackList.end(); it++ )
        {
            if( m_view->findItem( ((*it)->bundle()->artist().string()), 0 ) == 0 )
            {
                NjbMediaItem *artist = new NjbMediaItem( m_view );
                artist->setText( 0, (*it)->bundle()->artist() );
                artist->setType( MediaItem::ARTIST );
                artist->setExpandable( true );
                artist->setBundle( (*it)->bundle() );
                artist->m_device = this;
            }
        }
    }
    debug() << ": return " << result << endl;
    return result;
}

NjbMediaItem *
NjbMediaDevice::addTrackToView( NjbTrack *track, NjbMediaItem *item )
{
    TQString artistName = track->bundle()->artist();

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

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

    if( item )
        album->insertItem( item );
    else
    {
        item = new NjbMediaItem( album );
        item->m_device = this;
        TQString titleName = track->bundle()->title();
        item->setTrack( track );

        item->setText( 0, titleName );
        item->setType( MediaItem::TRACK );
        item->setBundle( track->bundle() );
        item->track()->setId( track->id() );
    }
    return item;

}

njb_t *
NjbMediaDevice::theNjb()
{
    return NjbMediaDevice::m_njb;
}

void
NjbMediaDevice::expandItem( TQListViewItem *item )
{
    DEBUG_BLOCK
    // First clear the item's children to repopulate.
    while( item->firstChild() )
        delete item->firstChild();

    NjbMediaItem *it = dynamic_cast<NjbMediaItem *>( item );

    if (!it) {
	debug() << "Dynamic cast to NJB media item failed" << endl;
	return;
    }

    switch( it->type() )
    {
        case MediaItem::ARTIST:
            if( it->childCount() == 0 ) // Just to be sure
                addAlbums( item->text( 0 ), it );
            break;
        case MediaItem::ALBUM:
            if( it->childCount() == 0 )
                addTracks( it->bundle()->artist(), item->text( 0 ), it );
            break;
        default:
            break;
    }
}

NjbMediaItem*
NjbMediaDevice::addAlbums(const TQString &artist, NjbMediaItem *item)
{
    for( trackValueList::iterator it = trackList.begin(); it != trackList.end(); it++ )
    {
        if( item->findItem( (*it)->bundle()->album() ) == 0 && ( (*it)->bundle()->artist().string() == artist ) )
        {
            NjbMediaItem *album = new NjbMediaItem( item );
            album->setText( 0, (*it)->bundle()->album() );
            album->setType( MediaItem::ALBUM );
            album->setExpandable( true );
            album->setBundle( (*it)->bundle() );
            album->m_device = this;
        }
    }
    return item;
}

NjbMediaItem*
NjbMediaDevice::addTracks(const TQString &artist, const TQString &album, NjbMediaItem *item)
{
    for( trackValueList::iterator it = trackList.begin(); it != trackList.end(); it++ )
    {
        if( ( (*it)->bundle()->album().string() == album ) && ( (*it)->bundle()->artist().string() == artist ))
        {
            NjbMediaItem *track = new NjbMediaItem( item );
            track->setText( 0, (*it)->bundle()->title() );
            track->setType( MediaItem::TRACK );
            track->setBundle( (*it)->bundle() );
            track->setTrack( (*it) );
            track->m_device = this;
        }
    }
    return item;
}

NjbMediaItem*
NjbMediaDevice::addArtist( NjbTrack *track )
{
    if( m_view->findItem( track->bundle()->artist().string(), 0 ) == 0 )
    {
        NjbMediaItem *artist = new NjbMediaItem( m_view );
        artist->setText( 0, track->bundle()->artist() );
        artist->setType( MediaItem::ARTIST );
        artist->setExpandable( true );
        artist->setBundle( track->bundle() );
        artist->m_device = this;
    }
    return dynamic_cast<NjbMediaItem *>( m_view->findItem( track->bundle()->artist().string(), 0 ) );
}

void
NjbMediaDevice::customClicked()
{
    TQString Information;
    TQString tracksFound;
    TQString powerStatus;
    TQString batteryLevel;
    TQString batteryCharging;

    if( m_connected )
    {
        tracksFound = i18n( "1 track found on device",
                            "%n tracks found on device ", trackList.size() );
        powerStatus = ( (NJB_Get_Auxpower( m_njb ) == 1) ? i18n("On auxiliary power") : i18n("On main power") );
        batteryCharging = ( (NJB_Get_Battery_Charging( m_njb ) == 1) ? i18n("Battery charging") : i18n("Battery not charging") );
        batteryLevel = (i18n("Battery level: ") + TQString::number( NJB_Get_Battery_Level( m_njb ) ) );

        Information = ( i18n("Player Information for ") + m_name +'\n' +
                        i18n("Power status: ") + powerStatus + '\n' +
                        i18n("Battery status: ") + batteryLevel + " (" +
                        batteryCharging + ')' );
    }
    else
    {
        Information = i18n("Player not connected");
    }

    KMessageBox::information(0, Information, i18n("Device information") );
}

#include "njbmediadevice.moc"