summaryrefslogtreecommitdiffstats
path: root/kipi-plugins/batchprocessimages/batchprocessimagesdialog.cpp
blob: bc0bc0a287ee1692779f1976c71058346b38fc6c (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
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
/* ============================================================
 *
 * This file is a part of kipi-plugins project
 * http://www.kipi-plugins.org
 *
 * Date        : 2004-10-01
 * Description : a kipi plugin to batch process images
 *
 * Copyright (C) 2004-2008 by Gilles Caulier <caulier dot gilles at gmail dot com>
 *
 * 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, or (at your option) any later version.
 *
 * This program 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 General Public License for more details.
 *
 * ============================================================ */

// C Ansi includes

extern "C"
{
#include <unistd.h>
}

// Include files for TQt

#include <tqvbox.h>
#include <tqlayout.h>
#include <tqdir.h>
#include <tqwidget.h>
#include <tqlabel.h>
#include <tqgroupbox.h>
#include <tqwhatsthis.h>
#include <tqcombobox.h>
#include <tqcheckbox.h>
#include <tqprocess.h>
#include <tqcolor.h>
#include <tqpainter.h>
#include <tqpalette.h>
#include <tqimage.h>
#include <tqevent.h>
#include <tqdragobject.h>
#include <tqfileinfo.h>
#include <tqhgroupbox.h>
#include <tqvgroupbox.h>
#include <tqframe.h>
#include <tqwmatrix.h>

// Include files for KDE

#include <kstandarddirs.h>
#include <kcolorbutton.h>
#include <klocale.h>
#include <kprogress.h>
#include <kfiledialog.h>
#include <kiconloader.h>
#include <kmessagebox.h>
#include <knuminput.h>
#include <kinstance.h>
#include <kconfig.h>
#include <kapplication.h>
#include <kdebug.h>
#include <kdialogbase.h>
#include <klistview.h>
#include <kimageio.h>
#include <kprocess.h>
#include <klineeditdlg.h>
#include <kio/jobclasses.h>
#include <kio/netaccess.h>
#include <kio/global.h>
#include <kio/previewjob.h>
#include <kbuttonbox.h>
#include <kdiroperator.h>
#include <tdeversion.h>
#include <kurlrequester.h>
#include <klineedit.h>

// KIPI includes

#include <libkipi/uploadwidget.h>
#include <libkipi/imagedialog.h>

// Local includes

#include "pluginsversion.h"
#include "outputdialog.h"
#include "imagepreview.h"
#include "batchprocessimagesdialog.h"
#include "batchprocessimagesdialog.moc"

namespace KIPIBatchProcessImagesPlugin
{

BatchProcessImagesDialog::BatchProcessImagesDialog( KURL::List urlList, KIPI::Interface* interface,
                                                    TQString caption, TQWidget *parent )
                        : KDialogBase( KDialogBase::Plain, caption, Help|User1|Cancel,
                                       Cancel, parent, "BatchProcessImagesDialog", false, false, i18n("&Start")),
                          m_selectedImageFiles( urlList), m_interface( interface )
{
    // Init. Tmp folder

    KStandardDirs dir;
    m_tmpFolder = dir.saveLocation("tmp", "kipi-batchprocessimagesplugin-" +
                                   TQString::number(getpid()) );

    m_convertStatus  = NO_PROCESS;
    m_progressStatus = 0;
    m_ProcessusProc  = 0;
    m_PreviewProc    = 0;

    KImageIO::registerFormats();

    TQWidget* box = plainPage();
    TQVBoxLayout *dvlay = new TQVBoxLayout(box, 0, KDialog::spacingHint());

    //---------------------------------------------

    TQHBoxLayout *hlay = new TQHBoxLayout( dvlay );
    groupBox1 = new TQGroupBox( 0, Qt::Vertical, box );
    groupBox1->layout()->setSpacing(KDialog::spacingHint());
    groupBox1->layout()->setMargin(KDialog::marginHint());
    TQGridLayout* grid = new TQGridLayout( groupBox1->layout(), 2, 3);
    m_labelType = new TQLabel( groupBox1 );
    grid->addMultiCellWidget(m_labelType, 0, 0, 0, 0);

    m_Type = new TQComboBox(false, groupBox1);
    grid->addMultiCellWidget(m_Type, 0, 0, 1, 1);

    m_optionsButton = new TQPushButton (groupBox1, "OptionButton");
    m_optionsButton->setText(i18n("Options"));
    TQWhatsThis::add( m_optionsButton, i18n("<p>You can choose here the options to use "
                                           "for the current process."));
    grid->addMultiCellWidget(m_optionsButton, 0, 0, 2, 2);

    m_smallPreview = new TQCheckBox(i18n("Small preview"), groupBox1);
    TQWhatsThis::add( m_smallPreview, i18n("<p>If you enable this option, "
                                          "all preview effects will be calculated on a small zone "
                                          "of the image (300x300 pixels in the top left corner). "
                                          "Enable this option if you have a slow computer.") );
    m_smallPreview->setChecked( true );
    grid->addMultiCellWidget(m_smallPreview, 1, 1, 0, 1);

    m_previewButton = new TQPushButton (groupBox1, "PreviewButton");
    m_previewButton->setText(i18n("&Preview"));
    TQWhatsThis::add( m_previewButton, i18n("<p>This button builds a process "
                                           "preview for the currently selected image on the list."));
    grid->addMultiCellWidget(m_previewButton, 1, 1, 2, 2);

    hlay->addWidget( groupBox1 );

    //---------------------------------------------

    groupBox2 = new TQGroupBox( 2, Qt::Horizontal, i18n("File Operations"), box );

    m_labelOverWrite = new TQLabel (i18n("Overwrite mode:"), groupBox2);
    m_overWriteMode = new TQComboBox( false, groupBox2 );
    m_overWriteMode->insertItem(i18n("Ask"));
    m_overWriteMode->insertItem(i18n("Always Overwrite"));
    m_overWriteMode->insertItem(i18n("Rename"));
    m_overWriteMode->insertItem(i18n("Skip"));
    m_overWriteMode->setCurrentText (i18n("Rename"));
    TQWhatsThis::add( m_overWriteMode, i18n("<p>Select here the overwrite mode used if your target's image "
                                           "files already exist.") );

    m_removeOriginal = new TQCheckBox(i18n("Remove original"), groupBox2);
    TQWhatsThis::add( m_removeOriginal, i18n("<p>If you enable this option, "
                                            "all original image files will be removed after processing.") );
    m_removeOriginal->setChecked( false );

    hlay->addWidget( groupBox2 );

    //---------------------------------------------

    groupBox3 = new TQHGroupBox( i18n("Target Folder"), box );

    m_destinationURL = new KURLRequester(groupBox3);
	m_destinationURL->setMode(KFile::Directory | KFile::LocalOnly);
	KIPI::ImageCollection album = interface->currentAlbum();
	if (album.isValid())
    {
		TQString url;
		if (album.isDirectory())
        {
			url = album.uploadPath().path();
		}
        else
        {
			url = TQDir::homeDirPath();
		}
		m_destinationURL->lineEdit()->setText(url);
	}
    TQWhatsThis::add( m_destinationURL, i18n("<p>Here you can select the target folder which "
                                            "will used by the process."));

    dvlay->addWidget( groupBox3 );

    //---------------------------------------------

    groupBox4         = new TQHGroupBox( box );
    TQWidget* box41    = new TQWidget( groupBox4 );
    TQHBoxLayout* lay2 = new TQHBoxLayout( box41, 0, spacingHint() );
    m_listFiles       = new BatchProcessImagesList( box41 );
    lay2->addWidget( m_listFiles );

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

    TQVBoxLayout* lay3 = new TQVBoxLayout( lay2 );
    m_addImagesButton = new TQPushButton ( i18n( "&Add..." ), box41 );
    lay3->addWidget( m_addImagesButton );
    TQWhatsThis::add( m_addImagesButton, i18n("<p>Add images to the list.") );

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

    m_imageLabel = new TQLabel( box41 );
    m_imageLabel->setFixedHeight( 80 );
    m_imageLabel->setAlignment( TQt::AlignHCenter | TQt::AlignVCenter );
    m_imageLabel->setSizePolicy( TQSizePolicy( TQSizePolicy::Preferred, TQSizePolicy::Preferred ) );
    lay3->addWidget( m_imageLabel );
    TQWhatsThis::add( m_imageLabel, i18n( "<p>The preview of the selected image on the list." ) );
    lay3->addStretch( 1 );

    dvlay->addWidget( groupBox4 );

    //---------------------------------------------

    m_progress = new KProgress( box, "Progress" );
    m_progress->setTotalSteps(100);
    m_progress->setValue(0);
    TQWhatsThis::add( m_progress, i18n("<p>This is the current percentage of the task completed.") );

    dvlay->addWidget( m_progress );

    //---------------------------------------------

    connect(m_listFiles, TQT_SIGNAL(doubleClicked(TQListViewItem *)),
            this, TQT_SLOT(slotListDoubleClicked(TQListViewItem *)));

    connect(this, TQT_SIGNAL(user1Clicked()),
            this, TQT_SLOT(slotProcessStart()));

    connect(m_optionsButton, TQT_SIGNAL(clicked()),
            this, TQT_SLOT(slotOptionsClicked()));

    connect(m_previewButton, TQT_SIGNAL(clicked()),
            this, TQT_SLOT(slotPreview()));

    connect(m_Type, TQT_SIGNAL(activated(int)),
            this, TQT_SLOT(slotTypeChanged(int)));

    connect(m_listFiles, TQT_SIGNAL( addedDropItems(TQStringList) ),
            this, TQT_SLOT( slotAddDropItems(TQStringList)));

    connect(m_listFiles, TQT_SIGNAL( currentChanged( TQListViewItem * ) ),
            this, TQT_SLOT( slotImageSelected( TQListViewItem * )));

    connect(m_addImagesButton, TQT_SIGNAL(clicked()),
            this, TQT_SLOT(slotImagesFilesButtonAdd()));

    connect(m_remImagesButton, TQT_SIGNAL(clicked()),
            this, TQT_SLOT(slotImagesFilesButtonRem()));

   // Get the image files filters from the hosts app.

    m_ImagesFilesSort = m_interface->fileExtensions();

    dvlay->activate();
}

BatchProcessImagesDialog::~BatchProcessImagesDialog()
{
}

void BatchProcessImagesDialog::slotImagesFilesButtonAdd( void )
{
    TQStringList ImageFilesList;

    KURL::List urls = KIPI::ImageDialog::getImageURLs( this, m_interface );

    for ( KURL::List::Iterator it = urls.begin() ; it != urls.end() ; ++it )
        ImageFilesList << (*it).path(); // PENDING(blackie) handle remote URLS

    if ( urls.isEmpty() ) return;

    slotAddDropItems(ImageFilesList);
}

void BatchProcessImagesDialog::slotImagesFilesButtonRem( void )
{
    BatchProcessImagesItem *pitem = static_cast<BatchProcessImagesItem*>( m_listFiles->currentItem() );

    if ( pitem )
    {
        m_listFiles->takeItem(pitem);
        m_listFiles->setSelected( m_listFiles->currentItem(), true );
        m_selectedImageFiles.remove(m_selectedImageFiles.find(pitem->pathSrc()));
        delete pitem;
        m_nbItem = m_selectedImageFiles.count();

        if (m_nbItem == 0)
            groupBox4->setTitle(i18n("Image Files List"));
        else
            groupBox4->setTitle(i18n("Image File List (1 item)", "Image File List (%n items)", m_nbItem));
    }
}

void BatchProcessImagesDialog::slotImageSelected( TQListViewItem * item )
{
    if ( !item || m_listFiles->childCount() == 0 )
    {
       m_imageLabel->clear();
       return;
    }

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

    m_imageLabel->clear();

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

    KURL url(IdemIndexed);

    TDEIO::PreviewJob* m_thumbJob = TDEIO::filePreview( url, m_imageLabel->height() );

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

void BatchProcessImagesDialog::slotGotPreview(const KFileItem* url, const TQPixmap &pixmap)
{
    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_imageLabel->setPixmap(pix);
}

void BatchProcessImagesDialog::slotAddDropItems(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 findItem = false;
    
        for ( KURL::List::Iterator it2 = m_selectedImageFiles.begin() ; it2 != m_selectedImageFiles.end() ; ++it2 )
        {
            TQString currentFile = (*it2).path(); // PENDING(blackie) Handle URL's
    
            if ( currentFile == currentDropFile )
                findItem = true;
        }
    
        if (findItem == false)
            m_selectedImageFiles.append(currentDropFile);
    }

    listImageFiles();
}

void BatchProcessImagesDialog::closeEvent ( TQCloseEvent *e )
{
    if (!e) return;

    if ( m_PreviewProc != 0 )
       if ( m_PreviewProc->isRunning() ) m_PreviewProc->kill(SIGTERM);

    if ( m_ProcessusProc != 0 )
       if ( m_ProcessusProc->isRunning() ) m_ProcessusProc->kill(SIGTERM);

    e->accept();
}

void BatchProcessImagesDialog::slotProcessStart( void )
{
    if ( m_selectedImageFiles.isEmpty() == true )
       return;

    if ( m_removeOriginal->isChecked() == true )
    {
        if ( KMessageBox::warningContinueCancel(this,
             i18n("All original image files will be removed from the source Album.\nDo you want to continue?"),
             i18n("Delete Original Image Files"), KStdGuiItem::cont(),
             "KIPIplugin-BatchProcessImages-AlwaysRemomveOriginalFiles") != KMessageBox::Continue )
           return;
    }

    m_convertStatus = UNDER_PROCESS;
    disconnect( this, TQT_SIGNAL(user1Clicked()), this, TQT_SLOT(slotProcessStart()));
    showButtonCancel( false );
    setButtonText( User1, i18n("&Stop") );
    connect(this, TQT_SIGNAL(user1Clicked()), this, TQT_SLOT(slotProcessStop()));

    m_labelType->setEnabled(false);
    m_Type->setEnabled(false);
    m_optionsButton->setEnabled(false);
    m_previewButton->setEnabled(false);
    m_smallPreview->setEnabled(false);

    m_labelOverWrite->setEnabled(false);
    m_overWriteMode->setEnabled(false);
    m_removeOriginal->setEnabled(false);

    m_destinationURL->setEnabled(false);
    m_addImagesButton->setEnabled(false);
    m_remImagesButton->setEnabled(false);

    m_listFile2Process_iterator = new TQListViewItemIterator( m_listFiles );
    startProcess();
}

bool BatchProcessImagesDialog::startProcess(void)
{
    if ( m_convertStatus == STOP_PROCESS )
    {
       endProcess();
       return true;
    }

    TQString targetAlbum = m_destinationURL->url();

    //TODO check if it is valid also for remote URL's
    // this is a workarond for bug 117397
    TQFileInfo dirInfo(targetAlbum + "/");
    if (!dirInfo.isDir () || !dirInfo.isWritable())
    {
        KMessageBox::error(this, i18n("You must specify a writable path for your output file."));
        endProcess();
        return true;
    }

    BatchProcessImagesItem *item = static_cast<BatchProcessImagesItem*>( m_listFile2Process_iterator->current() );
    m_listFiles->setCurrentItem(item);

    if ( prepareStartProcess(item, targetAlbum) == false ) // If there is a problem during the
    {                                                   // preparation -> pass to the next item!
        ++*m_listFile2Process_iterator;
        ++m_progressStatus;
        m_progress->setValue((int)((float)m_progressStatus *(float)100 / (float)m_nbItem));
        item = static_cast<BatchProcessImagesItem*>( m_listFile2Process_iterator->current() );
        m_listFiles->setCurrentItem(item);
    
        if ( m_listFile2Process_iterator->current() )
        {
            startProcess();
            return true;
        }
        else
        {
            endProcess();
            return true;
        }
    }

    KURL desturl(targetAlbum + "/" + item->nameDest());

#if TDE_VERSION >= 0x30200
    if ( TDEIO::NetAccess::exists( desturl, false, TQT_TQWIDGET(kapp->activeWindow()) ) == true )
#else
    if ( TDEIO::NetAccess::exists( desturl ) == true )
#endif
    {
       switch (overwriteMode())
       {
          case OVERWRITE_ASK:
          {
             int ValRet = KMessageBox::warningYesNoCancel(this,
                          i18n("The destination file \"%1\" already exists;\n"
                          "do you want overwrite it?").arg(item->nameDest()),
                          i18n("Overwrite Destination Image File"), KStdGuiItem::cont());

             if ( ValRet == KMessageBox::No )
             {
                item->changeResult(i18n("Skipped."));
                item->changeError(i18n("destination image file already exists (skipped by user)."));
                ++*m_listFile2Process_iterator;
                ++m_progressStatus;
                m_progress->setValue((int)((float)m_progressStatus *(float)100 / (float)m_nbItem));

                if ( m_listFile2Process_iterator->current() )
                {
                   startProcess();
                   return true;
                }
                else
                {
                   endProcess();
                   return true;
                }
            }
            else if ( ValRet == KMessageBox::Cancel )
            {
                processAborted(false);
                return false;
            }
            else
            {
                item->setDidOverWrite( true );
            }

             break;
          }

          case OVERWRITE_RENAME:
          {
             TQFileInfo *Target = new TQFileInfo(targetAlbum + "/" + item->nameDest());
             TQString newFileName = RenameTargetImageFile(Target);

             if ( newFileName.isNull() )
             {
                item->changeResult(i18n("Failed."));
                item->changeError(i18n("destination image file already exists and cannot be renamed."));
                ++*m_listFile2Process_iterator;
                ++m_progressStatus;
                m_progress->setValue((int)((float)m_progressStatus *(float)100 / (float)m_nbItem));

                if ( m_listFile2Process_iterator->current() )
                {
                   startProcess();
                   return true;
                }
                else
                {
                   endProcess();
                   return true;
                }
             }
             else
             {
                TQFileInfo *newTarget = new TQFileInfo(newFileName);
                item->changeNameDest(newTarget->fileName());
             }

             break;
          }

          case OVERWRITE_SKIP:
          {
             item->changeResult(i18n("Skipped."));
             item->changeError(i18n("destination image file already exists (skipped automatically)."));
             ++*m_listFile2Process_iterator;
             ++m_progressStatus;
             m_progress->setValue((int)((float)m_progressStatus *(float)100 / (float)m_nbItem));

             if ( m_listFile2Process_iterator->current() )
             {
                startProcess();
                return true;
             }
             else
             {
                endProcess();
                return true;
             }
             break;
          }

          case OVERWRITE_OVER:   // In this case do nothing : 'convert' default mode...
              item->setDidOverWrite( true );
             break;

          default:
          {
             endProcess();
             return true;
             break;
          }
       }
    }

    m_commandLine = TQString();
    m_ProcessusProc = new TDEProcess;
    m_commandLine.append(makeProcess(m_ProcessusProc, item, targetAlbum));

    item->changeOutputMess(m_commandLine + "\n\n");

    connect(m_ProcessusProc, TQT_SIGNAL(processExited(TDEProcess *)),
            this, TQT_SLOT(slotProcessDone(TDEProcess*)));

    connect(m_ProcessusProc, TQT_SIGNAL(receivedStdout(TDEProcess *, char*, int)),
            this, TQT_SLOT(slotReadStd(TDEProcess*, char*, int)));

    connect(m_ProcessusProc, TQT_SIGNAL(receivedStderr(TDEProcess *, char*, int)),
            this, TQT_SLOT(slotReadStd(TDEProcess*, char*, int)));

    bool result = m_ProcessusProc->start(TDEProcess::NotifyOnExit, TDEProcess::All);
    if(!result)
    {
       KMessageBox::error(this, i18n("Cannot start 'convert' program from 'ImageMagick' package;\n"
                                     "please check your installation."));
       return false;
    }

    return true;
}

void BatchProcessImagesDialog::slotReadStd(TDEProcess* /*proc*/, char *buffer, int buflen)
{
    BatchProcessImagesItem *item = static_cast<BatchProcessImagesItem*>( m_listFile2Process_iterator->current() );
    item->changeOutputMess( TQString::fromLocal8Bit(buffer, buflen) );
}

void BatchProcessImagesDialog::slotProcessDone(TDEProcess* proc)
{
    if ( m_convertStatus == PROCESS_DONE )
    {
        // processAborted() has already been called. No need to show the warning.
        return;
    }
    
    BatchProcessImagesItem *item = dynamic_cast<BatchProcessImagesItem*>( m_listFile2Process_iterator->current() );
    m_listFiles->ensureItemVisible(m_listFiles->currentItem());
    
    if ( !m_ProcessusProc->normalExit() )
    {
        int code = KMessageBox::warningContinueCancel( this,
                                i18n("The 'convert' program from 'ImageMagick' package has been stopped abnormally"),
                                i18n("Error running 'convert'") );
    
        if ( code == KMessageBox::Cancel )
        {
            processAborted(true);
        }
        else
        {
            item->changeResult(i18n("Failed."));
            item->changeError(i18n("'convert' program from 'ImageMagick' package has been stopped abnormally."));
            ++*m_listFile2Process_iterator;
            ++m_progressStatus;
            m_progress->setValue((int)((float)m_progressStatus *(float)100 / (float)m_nbItem));
    
            if ( m_listFile2Process_iterator->current() )
                startProcess();
            else
                endProcess();
        }
        return;
    }

    int ValRet = proc->exitStatus();
    kdWarning() << "Convert exit (" << ValRet << ")" << endl;

    switch (ValRet)
    {
        case 0:  // Process finished successfully !
        {
            item->changeResult(i18n("OK"));
            item->changeError(i18n("no processing error"));
            processDone();
    
            // Save the comments for the converted image
            KURL src;
            src.setPath( item->pathSrc() );
            KURL dest = m_destinationURL->url();
            dest.addPath( item->nameDest() );
            TQString errmsg;
    
            KURL::List urlList;
            urlList.append(src);
            urlList.append(dest);
            m_interface->refreshImages( urlList );
    
            if ( !item->overWrote() )
            {
                // Do not add an entry if there was an image at the location already.
                bool ok = m_interface->addImage( dest, errmsg );
    
                if ( !ok )
                {
                    int code = KMessageBox::warningContinueCancel( this,
                                            i18n("<qt>Error adding image to application; error message was: "
                                            "<b>%1</b></qt>").arg( errmsg ),
                                            i18n("Error Adding Image to Application") );
    
                    if ( code == KMessageBox::Cancel )
                    {
                        slotProcessStop();
                        break;
                    }
                    else
                        item->changeResult(i18n("Failed."));
                }
            }

            if ( src != dest )
            {
                KIPI::ImageInfo srcInfo  = m_interface->info( src );
                KIPI::ImageInfo destInfo = m_interface->info( dest );
                destInfo.cloneData( srcInfo );
            }
    
            if ( m_removeOriginal->isChecked() && src != dest )
            {
                KURL deleteImage(item->pathSrc());
    
#if TDE_VERSION >= 0x30200
                if ( TDEIO::NetAccess::del( deleteImage, TQT_TQWIDGET(kapp->activeWindow()) ) == false )
#else
                if ( TDEIO::NetAccess::del( deleteImage ) == false )
#endif
                {
                    item->changeResult(i18n("Warning:"));
                    item->changeError(i18n("cannot remove original image file."));
                }
                else
                    m_interface->delImage( item->pathSrc() );
            }
            break;
        }
        case 15: //  process aborted !
        {
            processAborted(true);
            break;
        }
        default : // Processing error !
        {
            item->changeResult(i18n("Failed."));
            item->changeError(i18n("cannot process original image file."));
            break;
        }
    }

    ++*m_listFile2Process_iterator;
    ++m_progressStatus;
    m_progress->setValue((int)((float)m_progressStatus *(float)100 / (float)m_nbItem));

    if ( m_listFile2Process_iterator->current() )
        startProcess();
    else
        endProcess();
}

void BatchProcessImagesDialog::slotListDoubleClicked(TQListViewItem *itemClicked)
{
    BatchProcessImagesItem *item = static_cast<BatchProcessImagesItem*>( itemClicked );

    if (m_convertStatus == PROCESS_DONE)
    {
       OutputDialog *infoDialog = new OutputDialog(this,
                                                   i18n("Image processing error"),
                                                   item->outputMess(),
                                                   i18n("Image \"%1\": %2\n\nThe output messages are:\n")
                                                        .arg(item->nameSrc()).arg(item->error())
                                                   );
       infoDialog->exec();
    }
}

void BatchProcessImagesDialog::slotPreview(void)
{
    kdWarning() << "BatchProcessImagesDialog::slotPreview" << endl;

    if ( m_listFiles->currentItem() == 0 )
    {
       KMessageBox::error(this, i18n("You must select an item from the list to calculate the preview."));
       return;
    }

    BatchProcessImagesItem *item = static_cast<BatchProcessImagesItem*>( m_listFiles->currentItem() );

    m_listFiles->setEnabled(false);
    m_labelType->setEnabled(false);
    m_Type->setEnabled(false);
    m_optionsButton->setEnabled(false);
    m_previewButton->setEnabled(false);
    m_labelOverWrite->setEnabled(false);
    m_overWriteMode->setEnabled(false);
    m_removeOriginal->setEnabled(false);
    m_smallPreview->setEnabled(false);
    m_destinationURL->setEnabled(false);
    m_addImagesButton->setEnabled(false);
    m_remImagesButton->setEnabled(false);

    disconnect( this, TQT_SIGNAL(user1Clicked()),
                this, TQT_SLOT(slotProcessStart()));

    showButtonCancel( false );
    setButtonText( User1, i18n("&Stop") );

    connect(this, TQT_SIGNAL(user1Clicked()),
            this, TQT_SLOT(slotPreviewStop()));

    m_previewOutput = "";
    m_PreviewProc = new TDEProcess;

    m_previewOutput.append(makeProcess(m_PreviewProc, item, TQString(), true));

    *m_PreviewProc << m_tmpFolder + "/" + TQString::number(getpid()) + "preview.PNG";
    m_previewOutput.append( " "  + m_tmpFolder + "/" + TQString::number(getpid()) + "preview.PNG\n\n");

    connect(m_PreviewProc, TQT_SIGNAL(processExited(TDEProcess *)),
            this, TQT_SLOT(slotPreviewProcessDone(TDEProcess*)));

    connect(m_PreviewProc, TQT_SIGNAL(receivedStdout(TDEProcess *, char*, int)),
            this, TQT_SLOT(slotPreviewReadStd(TDEProcess*, char*, int)));

    connect(m_PreviewProc, TQT_SIGNAL(receivedStderr(TDEProcess *, char*, int)),
            this, TQT_SLOT(slotPreviewReadStd(TDEProcess*, char*, int)));

    bool result = m_PreviewProc->start(TDEProcess::NotifyOnExit, TDEProcess::All);
    if(!result)
    {
        KMessageBox::error(this, i18n("Cannot start 'convert' program from 'ImageMagick' package;\n"
                                      "please check your installation."));
        m_previewButton->setEnabled(true);
        return;
    }
}

void BatchProcessImagesDialog::slotPreviewReadStd(TDEProcess* /*proc*/, char *buffer, int buflen)
{
    m_previewOutput.append( TQString::fromLocal8Bit(buffer, buflen) );
}

void BatchProcessImagesDialog::slotPreviewProcessDone(TDEProcess* proc)
{
    if (!m_PreviewProc->normalExit())
    {
        KMessageBox::error(this, i18n("Cannot run properly 'convert' program from 'ImageMagick' package."));
        m_previewButton->setEnabled(true);
        return;
    }

    BatchProcessImagesItem *item = static_cast<BatchProcessImagesItem*>( m_listFiles->currentItem() );
    int ValRet = proc->exitStatus();

    kdWarning() << "Convert exit (" << ValRet << ")" << endl;

    if ( ValRet == 0 )
    {
       TQString cropTitle = "";

       if ( m_smallPreview->isChecked() )
          cropTitle = i18n(" - small preview");

       ImagePreview *previewDialog = new ImagePreview(
                                         item->pathSrc(),
                                         m_tmpFolder + "/" + TQString::number(getpid()) + "preview.PNG",
                                         m_tmpFolder,
                                         m_smallPreview->isChecked(),
                                         false,
                                         m_Type->currentText() + cropTitle,
                                         item->nameSrc(),
                                         this);
       previewDialog->exec();

       KURL deletePreviewImage(m_tmpFolder + "/" + TQString::number(getpid()) + "preview.PNG");

#if TDE_VERSION >= 0x30200
       TDEIO::NetAccess::del( deletePreviewImage, TQT_TQWIDGET(kapp->activeWindow()) );
#else
       TDEIO::NetAccess::del( deletePreviewImage );
#endif
    }
    else
    {
       OutputDialog *infoDialog = new OutputDialog(this,
                                                   i18n("Preview processing error"),
                                                   m_previewOutput,
                                                   i18n("Cannot process preview for image \"%1\"."
                                                        "\nThe output messages are:\n")
                                                        .arg(item->nameSrc())
                                                   );
       infoDialog->exec();
    }

    endPreview();
}

void BatchProcessImagesDialog::slotPreviewStop( void )
{
    // Try to kill the current preview process !
    if ( m_PreviewProc->isRunning() == true ) m_PreviewProc->kill(SIGTERM);

    endPreview();
}

void BatchProcessImagesDialog::slotProcessStop( void )
{
    // Try to kill the current process !
    if ( m_ProcessusProc->isRunning() == true ) m_ProcessusProc->kill(SIGTERM);

    // If kill operation failed, Stop the process at the next image !
    if ( m_convertStatus == UNDER_PROCESS ) m_convertStatus = STOP_PROCESS;

    processAborted(true);
}

void BatchProcessImagesDialog::slotOk()
{
    close();
    saveSettings();
    delete this;
}

void BatchProcessImagesDialog::listImageFiles(void)
{
    m_nbItem = m_selectedImageFiles.count();

    if (m_nbItem == 0) groupBox4->setTitle(i18n("Image File List"));
    else
        groupBox4->setTitle(i18n("Image File List (1 item)", "Image File List (%n items)", m_nbItem));

    if (m_selectedImageFiles.isEmpty()) return;

    for ( KURL::List::Iterator it = m_selectedImageFiles.begin() ; it != m_selectedImageFiles.end() ; ++it )
    {
        TQString currentFile = (*it).path(); // PENDING(blackie) Handle URLS
        TQFileInfo *fi = new TQFileInfo(currentFile);
    
        // Check if the new item already exist in the list.
    
        bool findItem = false;
    
        TQListViewItemIterator it2( m_listFiles );
    
        while ( it2.current() )
        {
            BatchProcessImagesItem *pitem = static_cast<BatchProcessImagesItem*>(it2.current());
    
            if ( pitem->pathSrc() == currentFile.section('/', 0, -1) )
                findItem = true;
    
            ++it2;
        }
    
        if (findItem == false)
        {
            TQString oldFileName = fi->fileName();
            TQString newFileName = oldFileName2NewFileName(oldFileName);
    
            new BatchProcessImagesItem(m_listFiles,
                                        currentFile.section('/', 0, -1),
                                        oldFileName,
                                        newFileName,
                                        ""
                                        );
        }
    
        delete fi;
    }

    m_listFiles->setCurrentItem( m_listFiles->firstChild());
    m_listFiles->setSelected( m_listFiles->currentItem(), true );
    slotImageSelected(m_listFiles->currentItem());
    m_listFiles->ensureItemVisible(m_listFiles->currentItem());
}

void BatchProcessImagesDialog::endPreview(void)
{
    m_listFiles->setEnabled(true);
    m_labelType->setEnabled(true);
    m_Type->setEnabled(true);
    m_previewButton->setEnabled(true);
    m_labelOverWrite->setEnabled(true);
    m_overWriteMode->setEnabled(true);
    m_destinationURL->setEnabled(true);
    m_addImagesButton->setEnabled(true);
    m_remImagesButton->setEnabled(true);
    m_smallPreview->setEnabled(true);
    m_removeOriginal->setEnabled(true);
    showButtonCancel( true );

    m_optionsButton->setEnabled(true);          // Default status if 'slotTypeChanged' isn't re-implemented.
    slotTypeChanged(m_Type->currentItem());

    setButtonText( User1, i18n("&Start") );

    disconnect(this, TQT_SIGNAL(user1Clicked()),
               this, TQT_SLOT(slotPreviewStop()));

    connect(this, TQT_SIGNAL(user1Clicked()),
            this, TQT_SLOT(slotProcessStart()));
}

int BatchProcessImagesDialog::overwriteMode(void)
{
    TQString OverWrite = m_overWriteMode->currentText();

    if (OverWrite == i18n("Ask"))
        return OVERWRITE_ASK;

    if (OverWrite == i18n("Rename"))
        return OVERWRITE_RENAME;

    if (OverWrite == i18n("Skip"))
        return OVERWRITE_SKIP;

    if (OverWrite == i18n("Always Overwrite"))
        return OVERWRITE_OVER;

    return OVERWRITE_ASK;
}

void BatchProcessImagesDialog::processAborted(bool removeFlag)
{
    kdWarning() << "BatchProcessImagesDialog::processAborted" << endl;

    BatchProcessImagesItem *item = static_cast<BatchProcessImagesItem*>( m_listFile2Process_iterator->current() );
    m_listFiles->ensureItemVisible(m_listFiles->currentItem());

    item->changeResult(i18n("Aborted."));
    item->changeError(i18n("process aborted by user"));

    if (removeFlag == true) // Try to delete de destination !
    {
       KURL deleteImage = m_destinationURL->url();
       deleteImage.addPath(item->nameDest());

#if TDE_VERSION >= 0x30200
       if ( TDEIO::NetAccess::exists( deleteImage, false, TQT_TQWIDGET(kapp->activeWindow()) ) == true )
          TDEIO::NetAccess::del( deleteImage, TQT_TQWIDGET(kapp->activeWindow()) );
#else
       if ( TDEIO::NetAccess::exists( deleteImage ) == true )
          TDEIO::NetAccess::del( deleteImage );
#endif
    }

    endProcess();
}

void BatchProcessImagesDialog::endProcess(void)
{
    m_convertStatus = PROCESS_DONE;
    setButtonText( User1, i18n("&Close") );

    disconnect(this, TQT_SIGNAL(user1Clicked()),
               this, TQT_SLOT(slotProcessStop()));

    connect(this, TQT_SIGNAL(user1Clicked()),
            this, TQT_SLOT(slotOk()));
}

TQString BatchProcessImagesDialog::RenameTargetImageFile(TQFileInfo *fi)
{
    TQString Temp;
    int Enumerator = 0;
    KURL NewDestUrl;

    do
    {
       ++Enumerator;
       Temp = Temp.setNum( Enumerator );
       NewDestUrl = fi->filePath().left( fi->filePath().findRev('.', -1)) + "_" + Temp
                    + "." + fi->filePath().section('.', -1 );
    }
    while ( Enumerator < 100 &&
#if TDE_VERSION >= 0x30200
            TDEIO::NetAccess::exists( NewDestUrl, true, TQT_TQWIDGET(kapp->activeWindow()) )
#else
            TDEIO::NetAccess::exists( NewDestUrl )
#endif
            == true );

    if (Enumerator == 100) return TQString();

    return (NewDestUrl.path());
}

TQString BatchProcessImagesDialog::extractArguments(TDEProcess *proc)
{
    TQString retArguments;
    TQValueList<TQCString> argumentsList = proc->args();

    for ( TQValueList<TQCString>::iterator it = argumentsList.begin() ; it != argumentsList.end() ; ++it )
      retArguments.append(*it + " ");

    return (retArguments);
}

}  // NameSpace KIPIBatchProcessImagesPlugin