summaryrefslogtreecommitdiffstats
path: root/kipi-plugins/ipodexport/ipodexportdialog.cpp
blob: b65dd5718ba7c122700da1a69eff2c9d5c1cf8bc (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
/***************************************************************************
 * copyright            : (C) 2006 Seb Ruiz <me@sebruiz.net>               *
 **************************************************************************/

/***************************************************************************
 *                                                                         *
 *   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.                                   *
 *                                                                         *
 ***************************************************************************/

#include "ipodheader.h"
#include "imagelist.h"
#include "imagelistitem.h"
#include "ipodexportdialog.h"
#include "ipodlistitem.h"

#include <tqdir.h>
#include <tqfile.h>
#include <tqfileinfo.h>
#include <tqframe.h>
#include <tqhgroupbox.h>
#include <tqimage.h>
#include <tqlabel.h>
#include <tqlayout.h>
#include <tqpixmap.h>
#include <tqpushbutton.h>
#include <tqvgroupbox.h>
#include <tqwhatsthis.h>
#include <tqwmatrix.h>

#include <kdebug.h>
#include <kfileitem.h>
#include <kfiledialog.h> // add images
#include <kiconloader.h>
#include <kinputdialog.h> //new album
#include <kio/previewjob.h>
#include <klocale.h>
#include <kmessagebox.h>
#include <kmountpoint.h>
#include <kstandarddirs.h>
#include <kurl.h>

#if KIPI_PLUGIN
#include <libkipi/imagedialog.h>
#endif

#define debug() kdDebug()

using namespace IpodExport;

UploadDialog *UploadDialog::s_instance = 0;

UploadDialog::UploadDialog(
                            #if KIPI_PLUGIN
                            KIPI::Interface* interface,
                            #endif
                            TQString caption, TQWidget *parent )
    : KDialogBase( KDialogBase::Plain, caption, /*Help|*/Close,
                   Cancel, parent, "TripodDialog", false, false )
#if KIPI_PLUGIN
    , m_interface( interface )
#endif
    , m_itdb( 0 )
    , m_ipodInfo( 0 )
    , m_ipodHeader( 0 )
    , m_transferring( false )
    , m_destinationAlbum( 0 )
    , m_ipodAlbumList( 0 )
    , m_mountPoint( TQString() )
    , m_deviceNode( TQString() )
{
    s_instance = this;

    TQWidget       *box = plainPage();
    TQVBoxLayout *dvlay = new TQVBoxLayout( box, 6 );

    dvlay->setMargin( 2 );

    m_ipodHeader = new IpodHeader( box );
    dvlay->addWidget( m_ipodHeader );

    m_destinationBox = new TQHGroupBox( i18n("iPod"), box );

    m_ipodAlbumList = new ImageList( ImageList::IpodType, m_destinationBox );
    m_ipodAlbumList->setMinimumHeight( 80 );

    TQWidget          *buttons = new TQWidget( m_destinationBox );
    TQVBoxLayout *buttonLayout = new TQVBoxLayout( buttons, 0, spacingHint() );

    m_createAlbumButton = new TQPushButton( i18n("&New..."), buttons, "addAlbumButton");
    TQWhatsThis::add( m_createAlbumButton, i18n("Create a new photo album on the iPod."));

    m_removeAlbumButton = new TQPushButton( i18n("&Remove"), buttons, "remAlbumButton");
    m_renameAlbumButton = new TQPushButton( i18n("R&ename..."), buttons, "renameAlbumsButton");

    m_removeAlbumButton->setEnabled( false );
    m_renameAlbumButton->setEnabled( false );

    TQWhatsThis::add( m_removeAlbumButton, i18n("Remove the selected photos or albums from the iPod."));
    TQWhatsThis::add( m_renameAlbumButton, i18n("Rename the selected photo album on the iPod."));

    TQLabel *ipod_icon = new TQLabel( buttons );
    ipod_icon->setPixmap( TDEGlobal::iconLoader()->loadIcon( "ipod", KIcon::Desktop, KIcon::SizeHuge ) );

    m_ipodPreview = new TQLabel( buttons );
    m_ipodPreview->setFixedHeight( 80 );
    m_ipodPreview->setAlignment( TQt::AlignHCenter | TQt::AlignVCenter );
    m_ipodPreview->setSizePolicy( TQSizePolicy( TQSizePolicy::Preferred, TQSizePolicy::Preferred ) );

    buttonLayout->addWidget( m_createAlbumButton );
    buttonLayout->addWidget( m_removeAlbumButton );
    buttonLayout->addWidget( m_renameAlbumButton );
    buttonLayout->addWidget( m_ipodPreview );
    buttonLayout->addStretch( 1 );
    buttonLayout->addWidget( ipod_icon );

    dvlay->addWidget( m_destinationBox );

    m_urlListBox    = new TQHGroupBox( i18n("Hard Disk"), box );
    TQWidget* urlBox = new TQWidget( m_urlListBox );
    TQHBoxLayout* urlLayout = new TQHBoxLayout( urlBox, 0, spacingHint() );
    m_uploadList = new ImageList( ImageList::UploadType, urlBox );
    m_uploadList->setMinimumHeight( 80 );
    urlLayout->addWidget( m_uploadList );

    m_uploadList->setSizePolicy( TQSizePolicy::Expanding, TQSizePolicy::MinimumExpanding );

    TQVBoxLayout* uploadPaneLayout = new TQVBoxLayout( urlLayout );
    m_addImagesButton = new TQPushButton ( i18n( "&Add..." ), urlBox );
    uploadPaneLayout->addWidget( m_addImagesButton );
    TQWhatsThis::add( m_addImagesButton, i18n("Add images to be queued for the iPod.") );

    m_remImagesButton = new TQPushButton ( i18n( "&Remove" ), urlBox );
    uploadPaneLayout->addWidget( m_remImagesButton );
    TQWhatsThis::add( m_remImagesButton, i18n("Remove selected image from the list.") );

    m_transferImagesButton = new TQPushButton( i18n( "&Transfer" ), urlBox );
    uploadPaneLayout->addWidget( m_transferImagesButton );
    TQWhatsThis::add( m_transferImagesButton, i18n("Transfer images to the selected iPod album.") );

    m_imagePreview = new TQLabel( urlBox );
    m_imagePreview->setFixedHeight( 80 );
    m_imagePreview->setAlignment( TQt::AlignHCenter | TQt::AlignVCenter );
    m_imagePreview->setSizePolicy( TQSizePolicy( TQSizePolicy::Preferred, TQSizePolicy::Preferred ) );
    TQWhatsThis::add( m_imagePreview, i18n( "The preview of the selected image in the list." ) );

    TQLabel *hdd_icon = new TQLabel( urlBox );
    hdd_icon->setPixmap( TDEGlobal::iconLoader()->loadIcon( "system", KIcon::Desktop, KIcon::SizeHuge ) );

    uploadPaneLayout->addWidget( m_imagePreview );
    uploadPaneLayout->addStretch( 1 );
    uploadPaneLayout->addWidget( hdd_icon );

    dvlay->addWidget( m_urlListBox );

    /// populate the ipod view with a list of albums etc
    refreshDevices();

#if KIPI_PLUGIN
    /// add selected items to the ImageList
    KIPI::ImageCollection images = interface->currentSelection();

    if ( images.isValid() )
    {
        KURL::List selected = images.images();
        for( KURL::List::Iterator it = selected.begin(); it != selected.end(); ++it )
        {
            addUrlToList( (*it).path() );
        }
    }
#endif

    enableButtons();

    /// connect the signals & slots

    connect( m_createAlbumButton, TQT_SIGNAL( clicked() ), TQT_SLOT( createIpodAlbum() ) );
    connect( m_removeAlbumButton, TQT_SIGNAL( clicked() ), TQT_SLOT( deleteIpodAlbum() ) );
    connect( m_renameAlbumButton, TQT_SIGNAL( clicked() ), TQT_SLOT( renameIpodAlbum() ) );

    connect( m_uploadList,     TQT_SIGNAL( addedDropItems(TQStringList) ),    TQT_SLOT( addDropItems(TQStringList) ) );
    connect( m_uploadList,     TQT_SIGNAL( currentChanged(TQListViewItem*) ), TQT_SLOT( imageSelected(TQListViewItem*) ) );
    connect( m_ipodAlbumList, TQT_SIGNAL( currentChanged(TQListViewItem*) ), TQT_SLOT( ipodItemSelected(TQListViewItem*) ) );

    connect( m_addImagesButton,      TQT_SIGNAL( clicked() ), TQT_SLOT( imagesFilesButtonAdd() ) );
    connect( m_remImagesButton,      TQT_SIGNAL( clicked() ), TQT_SLOT( imagesFilesButtonRem() ) );
    connect( m_transferImagesButton, TQT_SIGNAL( clicked() ), TQT_SLOT( startTransfer()        ) );
}

void
UploadDialog::getIpodAlbums()
{
    if( !m_itdb ) return;

    debug() << "populating ipod view" << endl;

    // clear cache
    while( m_ipodAlbumList->firstChild() )
        delete m_ipodAlbumList->firstChild();

    IpodAlbumItem *last = 0;
    for( GList *it = m_itdb->photoalbums; it; it = it->next )
    {
        Itdb_PhotoAlbum *ipodAlbum = (Itdb_PhotoAlbum *)it->data;
        debug() << "   found album: " << ipodAlbum->name << endl;
        last = new IpodAlbumItem( m_ipodAlbumList, last, ipodAlbum );
        last->setPixmap( 0, TDEGlobal::iconLoader()->loadIcon( "folder", KIcon::Toolbar, KIcon::SizeSmall ) );
        getIpodAlbumPhotos( last, ipodAlbum );
    }
}

void
UploadDialog::getIpodAlbumPhotos( IpodAlbumItem *item, Itdb_PhotoAlbum *album )
{
    if( !item || !album || !m_itdb )
        return;

    IpodPhotoItem *last = 0;
    for( GList *it = album->members; it; it = it->next )
    {
        Itdb_Artwork *photo = (Itdb_Artwork*)it->data;
        gint photo_id = photo->id;
        last = new IpodPhotoItem( item, last, photo );
        last->setText( 0, TQString::number( photo_id ) );
        last->setPixmap( 0, TDEGlobal::iconLoader()->loadIcon( "image", KIcon::Toolbar, KIcon::SizeSmall ) );
    }
}

void
UploadDialog::reloadIpodAlbum( IpodAlbumItem *item, Itdb_PhotoAlbum *album )
{
    if( !item ) return;

    while( item->firstChild() )
        delete item->firstChild(); // clear the items, so we can reload them again

    Itdb_PhotoAlbum *ipodAlbum = 0;
    for( GList *it = m_itdb->photoalbums; it; it = it->next )
    {
        ipodAlbum = (Itdb_PhotoAlbum *)it->data;
        if( strcmp( ipodAlbum->name, album->name ) == 0 )
            break; // we found the album
    }

    dynamic_cast<IpodAlbumItem*>(item)->setPhotoAlbum( ipodAlbum );

    getIpodAlbumPhotos( item, ipodAlbum );
}

void
UploadDialog::enableButtons()
{
    // enable the start button only if there are albums to transfer to, items to transfer
    // and a database to add to!
    const bool transfer = m_uploadList->childCount()     > 0 && // we have items to transfer
                          m_ipodAlbumList->childCount() > 0 && // the ipod has albums
                          !m_transferring                   && // we aren't transferring
                          m_ipodAlbumList->selectedItem()   && // selected a destination album
                          m_itdb;

    m_transferImagesButton->setEnabled( transfer );

    enableButton( KDialogBase::Close, !m_transferring );

    const TQListViewItem *ipodSelection = m_ipodAlbumList->selectedItem();
    const bool isMasterLibrary = ( ipodSelection == m_ipodAlbumList->firstChild() );

    m_removeAlbumButton->setEnabled( ipodSelection && !isMasterLibrary );
    m_renameAlbumButton->setEnabled( ipodSelection && !isMasterLibrary && ipodSelection->depth() == 0 );
}

void
UploadDialog::startTransfer()
{
    if( !m_itdb || !m_uploadList->childCount() )
        return;

    TQListViewItem *selected = m_ipodAlbumList->selectedItem();
    if( !selected || selected->depth() != 0 /*not album*/)
        return;

    m_transferring = true;

#define selected static_cast<IpodAlbumItem*>( selected )

    Itdb_PhotoAlbum *album = selected->photoAlbum();

    enableButton( KDialogBase::User1, false );
    enableButton( KDialogBase::Close, false );

    GError *err = 0;

    while( TQListViewItem *item = m_uploadList->firstChild() )
    {
#define item static_cast<ImageListItem*>(item)
        debug() << "Uploading "      << item->pathSrc()
                << " to ipod album " << album->name << endl;
        Itdb_Artwork *art = itdb_photodb_add_photo( m_itdb, TQFile::encodeName( item->pathSrc() ), 0, 0, &err );
        if( !art )
        {
            if( err )
            {
                debug() << "Error adding photo " << item->pathSrc() << " to database:"
                        << err->message << endl;
                err = 0;
            }
        }
        else
            itdb_photodb_photoalbum_add_photo( m_itdb, album, art, 0 );

        delete item;
#undef  item
    }

    itdb_photodb_write( m_itdb, &err );
    if( err ) debug() << "Failed with error: " << err->message << endl;

    reloadIpodAlbum( selected, album );

    IpodAlbumItem *library = static_cast<IpodAlbumItem*>( m_ipodAlbumList->firstChild() );
    reloadIpodAlbum( library, library->photoAlbum() );

    m_transferring = false;

    enableButtons();
#undef selected
}

void
UploadDialog::ipodItemSelected( TQListViewItem *item )
{
    m_ipodPreview->clear();

    if( m_ipodAlbumList->currentItem() )
        m_ipodAlbumList->currentItem()->setSelected( true );

    enableButtons();

#define item dynamic_cast<IpodPhotoItem*>(item)
    if( !item )
        return;

    Itdb_Artwork *artwork = item->artwork();
//    Itdb_Thumb *thumb = itdb_artwork_get_thumb_by_type( artwork, ITDB_THUMB_PHOTO_SMALL );

//    if( !thumb )
//    {
        debug() << "no thumb was found" << endl;
        return;
//    }
#undef item

//     GdkPixbuf *buf = itdb_thumb_get_gdk_pixbuf( m_itdb->device, thumb );
//     int size = 0;
//     TQImage *image = buf->convertToImage();
//     debug() << "image size: " << image->size() << endl;
//
//     TQPixmap pix;
//     pix.convertFromImage( image );
//     m_ipodPreview->setPixmap( pix );
}

void
UploadDialog::imageSelected( TQListViewItem *item )
{
    if( !item || m_uploadList->childCount() == 0 || m_transferring )
    {
        m_imagePreview->clear();
        return;
    }

    ImageListItem *pitem = static_cast<ImageListItem*>( item );
    if ( !pitem ) return;

    m_imagePreview->clear();

    TQString IdemIndexed = "file:" + pitem->pathSrc();

    KURL url( IdemIndexed );

    KIO::PreviewJob* m_thumbJob = KIO::filePreview( url, m_imagePreview->height() );

    connect( m_thumbJob, TQT_SIGNAL( gotPreview(const KFileItem*, const TQPixmap&) ),
                   this,   TQT_SLOT( gotImagePreview(const KFileItem*, const TQPixmap&) ) );
}

void
UploadDialog::gotImagePreview( const KFileItem* url, const TQPixmap &pixmap )
{
#if KIPI_PLUGIN
    TQPixmap pix( pixmap );

    // Rotate the thumbnail compared to the angle the host application dictate
    KIPI::ImageInfo info = m_interface->info( url->url() );
    if ( info.angle() != 0 )
    {
        TQImage img = pix.convertToImage();
        TQWMatrix matrix;

        matrix.rotate( info.angle() );
        img = img.xForm( matrix );
        pix.convertFromImage( img );
    }

    m_imagePreview->setPixmap(pix);
#else
    Q_UNUSED( url );
    m_imagePreview->setPixmap( pixmap );
#endif
}

void
UploadDialog::imagesFilesButtonAdd()
{
    TQStringList fileList;
    KURL::List urls;
#if KIPI_PLUGIN
    urls = KIPI::ImageDialog::getImageURLs( this, m_interface );
#else
    const TQString filter = TQString( "*.jpg *.jpeg *.jpe *.tiff *.gif *.png *.bmp|" + i18n("Image files") );
    KFileDialog dlg( TQString(), filter, this, "addImagesDlg", true );
    dlg.setCaption( i18n("Add Images") );
    dlg.setMode( KFile::Files | KFile::Directory );
    dlg.exec();
    urls = dlg.selectedURLs();
#endif
    for( KURL::List::Iterator it = urls.begin() ; it != urls.end() ; ++it )
        fileList << (*it).path();

    if ( urls.isEmpty() ) return;

    addDropItems( fileList );
}

void
UploadDialog::imagesFilesButtonRem()
{
    TQPtrList<TQListViewItem> selected = m_uploadList->selectedItems();

    for( TQListViewItem *it = selected.first(); it; it = selected.next() )
        delete it;

    enableButton( KDialogBase::User1, m_uploadList->childCount() > 0 );
}

void
UploadDialog::createIpodAlbum()
{
    TQString helper;
    #if KIPI_PLUGIN
    KIPI::ImageCollection album = m_interface->currentAlbum();
    if( album.isValid() )
        helper = album.name();
    #endif

    bool ok = false;
    TQString newAlbum = KInputDialog::getText( i18n("New iPod Photo Album"),
                                              i18n("Create a new album:"),
                                              helper, &ok, this );
    if( ok )
    {
        debug() << "creating album " << newAlbum << endl;

        IpodAlbumItem *last = static_cast<IpodAlbumItem*>(m_ipodAlbumList->lastItem()); // FIXME?? O(n)

        Itdb_PhotoAlbum *photoAlbum = itdb_photodb_photoalbum_create( m_itdb, TQFile::encodeName( newAlbum ), -1/*end*/ );
        // add the new album to the list view
        IpodAlbumItem *i = new IpodAlbumItem( m_ipodAlbumList, last, photoAlbum );
        i->setPixmap( 0, TDEGlobal::iconLoader()->loadIcon( "folder", KIcon::Toolbar, KIcon::SizeSmall ) );
        m_ipodAlbumList->clearSelection();
        m_ipodAlbumList->setSelected( i, true );

        // commit the changes to the iPod
        GError *err = 0;
        itdb_photodb_write( m_itdb, &err );
    }
}

void
UploadDialog::renameIpodAlbum()
{
    IpodAlbumItem *selected = dynamic_cast<IpodAlbumItem*>(m_ipodAlbumList->selectedItem());

    // only allow renaming of album items
    if( !selected || selected->depth() != 0 ) return;

    bool ok = false;
    TQString newName = KInputDialog::getText( i18n("Rename iPod Photo Album"),
                                             i18n("New album title:"),
                                             selected->text(0), &ok, this );
    if( ok )
    {
        // change the name on the ipod, and rename the listviewitem
        selected->setName( newName );
        // commit changes to the iPod
        GError *err = 0;
        itdb_photodb_write( m_itdb, &err );
    }
}

bool UploadDialog::deleteIpodPhoto( IpodPhotoItem *photo )
{
    if( !photo )
        return false;

    IpodAlbumItem *album = static_cast<IpodAlbumItem *>( photo->parent() );

    if( !album )
        return false;

    Itdb_Artwork *artwork = photo->artwork();

    if( !artwork )
    {
        debug() << "Could not find photo artwork with id: " << photo->text(0) << endl;
        return false;
    }

    Itdb_PhotoAlbum *photo_album = album->photoAlbum();
    itdb_photodb_remove_photo( m_itdb, photo_album, artwork );

    // if we remove from the library, remove from all sub albums too
    if( photo_album->album_type == 0x01 ) // master album
    {
        for( TQListViewItem *albumIt = m_ipodAlbumList->firstChild()->nextSibling(); //skip library
             albumIt; albumIt = albumIt->nextSibling() )
        {
            for( TQListViewItem *photoIt = albumIt->firstChild();
                 photoIt; photoIt = photoIt->nextSibling() )
            {
                if( photoIt->text(0) == photo->text(0) )
                {
                    debug() << "removing reference to photo from album " << albumIt->text(0) << endl;
                    delete photoIt;
                }
            }
        }
    }
    return true;
}

bool UploadDialog::deleteIpodAlbum( IpodAlbumItem *album )
{
    debug() << "deleting album: " << album->name() << ", and removing all photos" << endl;
    itdb_photodb_photoalbum_remove( m_itdb, album->photoAlbum(), true/*remove photos*/);

    return true;
}

void
UploadDialog::deleteIpodAlbum()
{
    TQListViewItem *selected = m_ipodAlbumList->selectedItem();
    if( !selected ) return;

    bool result = false;
    switch( selected->depth() )
    {
        case 0: //album
            result = deleteIpodAlbum( dynamic_cast<IpodAlbumItem*>( selected ) );
            break;

        case 1: //image
            result = deleteIpodPhoto( dynamic_cast<IpodPhotoItem*>( selected ) );
            break;
    }

    if( result ) //selected item may have been deleted by deleteIpodPhoto
        delete selected;

    GError *err = 0;
    itdb_photodb_write( m_itdb, &err );
}

void
UploadDialog::addDropItems( TQStringList filesPath )
{
    if( filesPath.isEmpty() ) return;

    for( TQStringList::Iterator it = filesPath.begin() ; it != filesPath.end() ; ++it )
    {
        TQString currentDropFile = *it;

        // Check if the new item already exist in the list.

        bool itemExists = false;

        TQListViewItemIterator it2( m_uploadList );

        while( it2.current() )
        {
            ImageListItem *item = static_cast<ImageListItem*>(it2.current());

            if( item->pathSrc() == currentDropFile.section('/', 0, -1) )
            {
                itemExists = true;
                break;
            }
            ++it2;
        }

        if( !itemExists )
            addUrlToList( currentDropFile );
    }

    enableButton( KDialogBase::User1, m_uploadList->childCount() > 0 );
}

void
UploadDialog::addUrlToList( TQString file )
{
    TQFileInfo *fi = new TQFileInfo( file );

    new ImageListItem( m_uploadList, file.section('/', 0, -1), fi->fileName() );

    delete fi;
}


bool
UploadDialog::openDevice()
{
    if( m_itdb )
    {
        debug() <<  "ipod at " << m_mountPoint << " already opened" << endl;
        return false;
    }

    // try to find a mounted ipod
    bool ipodFound = false;

    KMountPoint::List currentmountpoints = KMountPoint::currentMountPoints();
    for( KMountPoint::List::Iterator mountiter = currentmountpoints.begin();
         mountiter != currentmountpoints.end();
         ++mountiter )
    {
        TQString devicenode = (*mountiter)->mountedFrom();
        TQString mountpoint = (*mountiter)->mountPoint();

        if( !m_mountPoint.isEmpty() &&
             mountpoint != m_mountPoint )
            continue;

        if( mountpoint.startsWith( "/proc" ) ||
            mountpoint.startsWith( "/sys" )  ||
            mountpoint.startsWith( "/dev" )  ||
            mountpoint.startsWith( "/boot" ) )
            continue;

        if( !m_deviceNode.isEmpty() &&
             devicenode != m_deviceNode )
            continue;

        /// Detecting whether an iPod exists.
        TQString path = TQString( itdb_get_control_dir( TQFile::encodeName( mountpoint ) ) );
        TQDir d( path );

        if( path.isEmpty() || !d.exists() )
            continue;

        if( m_mountPoint.isEmpty() )
            m_mountPoint = mountpoint;

        /// Here, we have found an ipod, but we are not sure if the photo db exists.
        /// Try and parse it to determine whether we have initialised the iPod.
        ipodFound = true;
        GError *err = 0;
        m_itdb = itdb_photodb_parse( TQFile::encodeName( mountpoint ), &err );
        if( err )
        {
            g_error_free( err );
            if( m_itdb )
            {
                itdb_photodb_free( m_itdb );
                m_itdb = 0;
            }
        }
        break;
    }

    if( !ipodFound )
    {
        debug() << "no mounted ipod found" << endl;
        if( m_itdb )
        {
            itdb_photodb_free( m_itdb );
            m_itdb = 0;
        }
        return false;
    }

    debug() << "ipod found mounted at " << m_mountPoint << endl;

    /// No photodb was able to be parsed, so offer to initialise the ipod for the user.
    if( !m_itdb )
    {
        debug() << "could not find iTunesDB on device mounted at " << m_mountPoint << endl;

        TQString msg = i18n( "An iPod photo database could not be found on device mounted at %1. "
                "Should I try to initialize your iPod photo database?" ).arg( m_mountPoint );

        if( KMessageBox::warningContinueCancel( this, msg, i18n( "Initialize iPod Photo Database?" ),
                    KGuiItem(i18n("&Initialize"), "new") ) == KMessageBox::Continue )
        {

            m_itdb = itdb_photodb_create( TQFile::encodeName( m_mountPoint ) );
            itdb_device_set_mountpoint( m_itdb->device, TQFile::encodeName( m_mountPoint ) );

            if( !m_itdb )
            {
                debug() << "Could not initialise photodb..." << endl;
                return false;
            }

            GError *err = 0;
            itdb_photodb_write( m_itdb, &err );
        }
        else
            return false;
    }

    return true;
}

Itdb_Artwork *
UploadDialog::photoFromId( const uint id )
{
    if( !m_itdb )
        return 0;

    for( GList *it = m_itdb->photos; it; it=it->next )
    {
        Itdb_Artwork *photo = (Itdb_Artwork*)it->data;
        if( !photo )
            return 0;

        if( photo->id == id )
            return photo;
    }
    return 0;
}

TQString UploadDialog::ipodModel() const
{
    if( m_ipodInfo )
        return TQString( itdb_info_get_ipod_model_name_string( m_ipodInfo->ipod_model ) );

    return TQString();
}


void UploadDialog::refreshDevices()
{
    debug() << "refreshing ipod devices" << endl;

    if( !m_ipodHeader )
        return;

    m_ipodHeader->disconnect();

    if( !openDevice() )
    {
        m_ipodHeader->setViewType( IpodHeader::NoIpod );
        connect( m_ipodHeader, TQT_SIGNAL( refreshDevices() ), TQT_SLOT( refreshDevices() ) );
    }
    else //device opened! hooray!
    {
        m_ipodInfo = const_cast<Itdb_IpodInfo*>( itdb_device_get_ipod_info( m_itdb->device ) );
        const TQString model = ipodModel();

        if( !m_ipodInfo || model.isEmpty() || model == "Invalid" )
        {
            debug() << "the ipod model must be set before photos can be added" << endl;
            m_ipodHeader->setViewType( IpodHeader::IncompatibleIpod );
            connect( m_ipodHeader, TQT_SIGNAL( updateSysInfo() ), TQT_SLOT( updateSysInfo() ) );
            return;
        }
        else
        {
            m_ipodHeader->setViewType( IpodHeader::ValidIpod );
        }
    }

    if( m_ipodAlbumList )
        getIpodAlbums();

    m_destinationBox->setEnabled( m_itdb );
    m_urlListBox->setEnabled( m_itdb );
}

void UploadDialog::updateSysInfo()
{
    debug() << "updateSysInfo()" << endl;
}