~ubuntu-branches/ubuntu/feisty/digikam/feisty

« back to all changes in this revision

Viewing changes to digikam/utilities/cameragui/cameracontroller.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Achim Bohnet
  • Date: 2005-03-10 02:39:02 UTC
  • mfrom: (1.2.1 upstream) (2.1.1 hoary)
  • Revision ID: james.westby@ubuntu.com-20050310023902-023nymfst5mg696c
Tags: 0.7.2-2
* debian/TODO: clean
* digikam manpage: better --detect-camera description

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* ============================================================
 
2
 * Author: Renchi Raju <renchi@pooh.tam.uiuc.edu>
 
3
 * Date  : 2004-09-17
 
4
 * Description : 
 
5
 * 
 
6
 * Copyright 2004 by Renchi Raju
 
7
 
 
8
 * This program is free software; you can redistribute it
 
9
 * and/or modify it under the terms of the GNU General
 
10
 * Public License as published by the Free Software Foundation;
 
11
 * either version 2, or (at your option)
 
12
 * any later version.
 
13
 * 
 
14
 * This program is distributed in the hope that it will be useful,
 
15
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 
16
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
17
 * GNU General Public License for more details.
 
18
 * 
 
19
 * ============================================================ */
 
20
 
 
21
#include <qthread.h>
 
22
#include <qmutex.h>
 
23
#include <qwaitcondition.h>
 
24
#include <qevent.h>
 
25
#include <qapplication.h>
 
26
#include <qdeepcopy.h>
 
27
#include <qvariant.h>
 
28
#include <qimage.h>
 
29
#include <qdatastream.h>
 
30
#include <qfile.h>
 
31
#include <qtimer.h>
 
32
 
 
33
#include <klocale.h>
 
34
#include <kurl.h>
 
35
#include <kmessagebox.h>
 
36
#include <kio/renamedlg.h>
 
37
#include <kdebug.h>
 
38
#include <kstandarddirs.h>
 
39
 
 
40
#include <typeinfo>
 
41
#include <libkexif/kexifdialog.h>
 
42
 
 
43
extern "C"
 
44
{
 
45
#include <sys/types.h>
 
46
#include <sys/stat.h>
 
47
#include <unistd.h>
 
48
#include <stdio.h>
 
49
}
 
50
 
 
51
#include <imagewindow.h>
 
52
#include "gpcamera.h"
 
53
#include "umscamera.h"
 
54
#include "exifrotate.h"
 
55
#include "mtqueue.h"
 
56
#include "cameracontroller.h"
 
57
 
 
58
class CameraThread;
 
59
 
 
60
class CameraCommand
 
61
{
 
62
public:
 
63
 
 
64
    enum Action
 
65
    {
 
66
        gp_none = 0,
 
67
        gp_connect,
 
68
        gp_cancel,
 
69
        gp_listfolders,
 
70
        gp_listfiles,
 
71
        gp_download,
 
72
        gp_upload,
 
73
        gp_delete,
 
74
        gp_thumbnail,
 
75
        gp_exif,
 
76
        gp_open
 
77
    };
 
78
    
 
79
    Action                 action;
 
80
    QMap<QString,QVariant> map; 
 
81
};    
 
82
 
 
83
class CameraEvent : public QCustomEvent
 
84
{
 
85
public:
 
86
 
 
87
    enum State
 
88
    {
 
89
        gp_connected = 0,
 
90
        gp_busy,
 
91
        gp_listedfolders,
 
92
        gp_listedfiles,
 
93
        gp_downloaded,
 
94
        gp_downloadFailed,
 
95
        gp_opened,
 
96
        gp_uploaded,
 
97
        gp_uploadFailed,
 
98
        gp_deleted,
 
99
        gp_deleteFailed,
 
100
        gp_thumbnailed,
 
101
        gp_exif,
 
102
        gp_infomsg,
 
103
        gp_errormsg
 
104
    };
 
105
    
 
106
    CameraEvent(State state) :
 
107
        QCustomEvent(QEvent::User+state)
 
108
        {}
 
109
 
 
110
    bool                   result;
 
111
    QString                msg;
 
112
    QMap<QString,QVariant> map; 
 
113
};
 
114
    
 
115
class CameraControllerPriv
 
116
{
 
117
public:
 
118
 
 
119
    QWidget*               parent;
 
120
    CameraThread*          thread;
 
121
    DKCamera*              camera;
 
122
    MTQueue<CameraCommand> cmdQueue;
 
123
    QTimer*                timer;
 
124
    bool                   close;
 
125
 
 
126
    bool                   overwriteAll;
 
127
    bool                   skipAll;
 
128
    int                    downloadTotal;
 
129
};
 
130
 
 
131
class CameraThread : public QThread
 
132
{
 
133
public:
 
134
 
 
135
    CameraThread(CameraController* controller);
 
136
    ~CameraThread();
 
137
 
 
138
    void sendBusy(bool busy);
 
139
    void sendError(const QString& msg);
 
140
    void sendInfo(const QString& msg);
 
141
    
 
142
protected:
 
143
 
 
144
    void run();
 
145
 
 
146
private:
 
147
    
 
148
    CameraControllerPriv* d;
 
149
    QObject*              parent;
 
150
};
 
151
 
 
152
CameraThread::CameraThread(CameraController* controller)
 
153
    : d(controller->d), parent(controller)
 
154
{
 
155
}
 
156
 
 
157
CameraThread::~CameraThread()
 
158
{
 
159
}
 
160
 
 
161
void CameraThread::run()
 
162
{
 
163
    if (d->close)
 
164
        return;
 
165
 
 
166
    sendBusy(true);
 
167
 
 
168
 
 
169
    CameraCommand* cmd = d->cmdQueue.dequeue();
 
170
    if (cmd)
 
171
    {
 
172
 
 
173
        switch (cmd->action)
 
174
        {
 
175
        case(CameraCommand::gp_connect):
 
176
        {
 
177
            sendInfo(i18n("Connecting to camera..."));
 
178
        
 
179
            bool result = d->camera->connect();
 
180
 
 
181
            CameraEvent* event = new CameraEvent(CameraEvent::gp_connected);
 
182
            event->result = result;
 
183
            QApplication::postEvent(parent, event);
 
184
 
 
185
            if (result)
 
186
                sendInfo(i18n("Connection established"));
 
187
            else
 
188
                sendInfo(i18n("Connection failed"));
 
189
 
 
190
            break;
 
191
        }
 
192
        case(CameraCommand::gp_listfolders):
 
193
        {
 
194
            sendInfo(i18n("Listing folders..."));
 
195
 
 
196
            QStringList folderList;
 
197
            folderList.append(d->camera->path());
 
198
            d->camera->getAllFolders(d->camera->path(), folderList);
 
199
 
 
200
            /* TODO: ugly hack since qt <= 3.1.2 does not define
 
201
               QStringList with QDeepCopy as a friend. */
 
202
            QValueList<QString> flist(folderList);
 
203
            
 
204
            CameraEvent* event = new CameraEvent(CameraEvent::gp_listedfolders);
 
205
            event->map.insert("folders", QVariant(flist));
 
206
            QApplication::postEvent(parent, event);
 
207
        
 
208
            sendInfo(i18n("Finished listing folders..."));
 
209
 
 
210
            break;
 
211
        }
 
212
        case(CameraCommand::gp_listfiles):
 
213
        {
 
214
            QString folder = cmd->map["folder"].asString();
 
215
            
 
216
            sendInfo(i18n("Listing files in %1...")
 
217
                     .arg(folder));
 
218
 
 
219
            GPItemInfoList itemsList;
 
220
            if (!d->camera->getItemsInfoList(folder, itemsList))
 
221
            {
 
222
                sendError(i18n("Failed to list files in %1")
 
223
                          .arg(folder));
 
224
            }
 
225
 
 
226
            if (!itemsList.isEmpty())
 
227
            {
 
228
                CameraEvent* event = new CameraEvent(CameraEvent::gp_listedfiles);
 
229
                event->map.insert("folder", QVariant(folder));
 
230
                
 
231
                QByteArray  ba;
 
232
                QDataStream ds(ba, IO_WriteOnly);
 
233
                ds << itemsList;
 
234
                
 
235
                event->map.insert("files", QVariant(ba));
 
236
                QApplication::postEvent(parent, event);
 
237
            }
 
238
 
 
239
            sendInfo(i18n("Finished listing files in %1")
 
240
                     .arg(folder));
 
241
            
 
242
            break;
 
243
        }
 
244
        case(CameraCommand::gp_thumbnail):
 
245
        {
 
246
            QString folder = cmd->map["folder"].asString();
 
247
            QString file   = cmd->map["file"].asString();
 
248
 
 
249
            sendInfo(i18n("Getting thumbnail for %1/%2...")
 
250
                     .arg(folder)
 
251
                     .arg(file));
 
252
 
 
253
            QImage thumbnail;
 
254
            d->camera->getThumbnail(folder, file,
 
255
                                    thumbnail);
 
256
 
 
257
            if (!thumbnail.isNull())
 
258
            {
 
259
                thumbnail = thumbnail.smoothScale(128,128,QImage::ScaleMin);
 
260
        
 
261
                CameraEvent* event = new CameraEvent(CameraEvent::gp_thumbnailed);
 
262
                event->map.insert("folder", QVariant(folder));
 
263
                event->map.insert("file", QVariant(file));
 
264
                event->map.insert("thumbnail", QVariant(thumbnail));
 
265
                QApplication::postEvent(parent, event);
 
266
            }
 
267
 
 
268
            break;
 
269
        }
 
270
        case(CameraCommand::gp_exif):
 
271
        {
 
272
            QString folder = cmd->map["folder"].asString();
 
273
            QString file   = cmd->map["file"].asString();
 
274
 
 
275
            sendInfo(i18n("Getting EXIF information for %1/%2...")
 
276
                     .arg(folder)
 
277
                     .arg(file));
 
278
 
 
279
            char* edata = 0;
 
280
            int   esize;
 
281
            d->camera->getExif(folder, file, &edata, esize);
 
282
 
 
283
            if (!edata || !esize)
 
284
            {
 
285
                sendError(i18n("Failed to retrieve EXIF information for %1")
 
286
                          .arg(file));
 
287
            }
 
288
            else
 
289
            {
 
290
                QByteArray  ba;
 
291
                QDataStream ds(ba, IO_WriteOnly);
 
292
                ds.writeRawBytes(edata, esize);
 
293
                delete [] edata;
 
294
        
 
295
                CameraEvent* event = new CameraEvent(CameraEvent::gp_exif);
 
296
                event->map.insert("folder", QVariant(folder));
 
297
                event->map.insert("file", QVariant(file));
 
298
                event->map.insert("exifSize", QVariant(esize));
 
299
                event->map.insert("exifData", QVariant(ba));
 
300
                QApplication::postEvent(parent, event);
 
301
            }
 
302
            break;
 
303
        }
 
304
        case(CameraCommand::gp_download):
 
305
        {
 
306
            QString folder     = cmd->map["folder"].asString();
 
307
            QString file       = cmd->map["file"].asString();
 
308
            QString dest       = cmd->map["dest"].asString();
 
309
            bool    autoRotate = cmd->map["autoRotate"].asBool();
 
310
 
 
311
            sendInfo(i18n("Downloading file %1...")
 
312
                     .arg(file));
 
313
 
 
314
            // download to a temp file
 
315
            KURL tempURL(dest);
 
316
            tempURL = tempURL.upURL();
 
317
            tempURL.addPath( QString(".digikam-camera-%1")
 
318
                             .arg(getpid()));
 
319
 
 
320
            bool result = d->camera->downloadItem(folder, file,
 
321
                                                  tempURL.path());
 
322
 
 
323
            if (result)
 
324
            {
 
325
                if (autoRotate)
 
326
                {
 
327
                    sendInfo(i18n("EXIF rotating file %1...")
 
328
                             .arg(file));
 
329
                    Digikam::exifRotate(tempURL.path());
 
330
                }
 
331
 
 
332
                // move the file to the destination file
 
333
                if (rename(QFile::encodeName(tempURL.path()),
 
334
                           QFile::encodeName(dest)) != 0)
 
335
                {
 
336
                    // rename failed. delete the temp file
 
337
                    unlink(QFile::encodeName(tempURL.path()));
 
338
                    result = false;
 
339
                }
 
340
            }
 
341
 
 
342
            
 
343
            if (result)
 
344
            {
 
345
                CameraEvent* event = new CameraEvent(CameraEvent::gp_downloaded);
 
346
                event->map.insert("folder", QVariant(folder));
 
347
                event->map.insert("file", QVariant(file));
 
348
                event->map.insert("dest", QVariant(dest));
 
349
                QApplication::postEvent(parent, event);
 
350
            }
 
351
            else
 
352
            {
 
353
                CameraEvent* event = new CameraEvent(CameraEvent::gp_downloadFailed);
 
354
                event->map.insert("folder", QVariant(folder));
 
355
                event->map.insert("file", QVariant(file));
 
356
                event->map.insert("dest", QVariant(dest));
 
357
                QApplication::postEvent(parent, event);
 
358
            }                
 
359
            break;
 
360
        }
 
361
        case(CameraCommand::gp_open):
 
362
        {
 
363
            QString folder     = cmd->map["folder"].asString();
 
364
            QString file       = cmd->map["file"].asString();
 
365
            QString dest       = cmd->map["dest"].asString();
 
366
 
 
367
            sendInfo(i18n("Retrieving file %1 from camera...")
 
368
                     .arg(file));
 
369
 
 
370
            bool result = d->camera->downloadItem(folder, file, dest);
 
371
 
 
372
            if (result)
 
373
            {
 
374
                CameraEvent* event = new CameraEvent(CameraEvent::gp_opened);
 
375
                event->map.insert("folder", QVariant(folder));
 
376
                event->map.insert("file", QVariant(file));
 
377
                event->map.insert("dest", QVariant(dest));
 
378
                QApplication::postEvent(parent, event);
 
379
            }
 
380
            else
 
381
            {
 
382
                sendError(i18n("Failed to retrieve file %1 from camera")
 
383
                          .arg(file));
 
384
            }                
 
385
            break;
 
386
        }
 
387
        case(CameraCommand::gp_upload):
 
388
        {
 
389
            QString folder = cmd->map["folder"].asString();
 
390
            QString file   = cmd->map["file"].asString();
 
391
            QString src    = cmd->map["src"].asString();
 
392
 
 
393
            sendInfo(i18n("Uploading file %1...")
 
394
                     .arg(file));
 
395
 
 
396
            bool result = d->camera->uploadItem(folder, file, src);
 
397
 
 
398
            if (result)
 
399
            {
 
400
                CameraEvent* event = new CameraEvent(CameraEvent::gp_uploaded);
 
401
                event->map.insert("folder", QVariant(folder));
 
402
                event->map.insert("file", QVariant(file));
 
403
                event->map.insert("src", QVariant(src));
 
404
                QApplication::postEvent(parent, event);
 
405
            }
 
406
            else
 
407
            {
 
408
                CameraEvent* event = new CameraEvent(CameraEvent::gp_uploadFailed);
 
409
                event->map.insert("folder", QVariant(folder));
 
410
                event->map.insert("file", QVariant(file));
 
411
                event->map.insert("src", QVariant(src));
 
412
                QApplication::postEvent(parent, event);
 
413
            }                
 
414
            break;
 
415
        }
 
416
        case(CameraCommand::gp_delete):
 
417
        {
 
418
            QString folder = cmd->map["folder"].asString();
 
419
            QString file   = cmd->map["file"].asString();
 
420
 
 
421
            sendInfo(i18n("Deleting file %1...")
 
422
                     .arg(file));
 
423
 
 
424
            bool result = d->camera->deleteItem(folder, file);
 
425
 
 
426
            if (result)
 
427
            {
 
428
                CameraEvent* event = new CameraEvent(CameraEvent::gp_deleted);
 
429
                event->map.insert("folder", QVariant(folder));
 
430
                event->map.insert("file", QVariant(file));
 
431
                QApplication::postEvent(parent, event);
 
432
            }
 
433
            else
 
434
            {
 
435
                CameraEvent* event = new CameraEvent(CameraEvent::gp_deleteFailed);
 
436
                event->map.insert("folder", QVariant(folder));
 
437
                event->map.insert("file", QVariant(file));
 
438
                QApplication::postEvent(parent, event);
 
439
            }                
 
440
            break;
 
441
        }
 
442
        default:
 
443
            kdWarning() << k_funcinfo << " unknown action specified" << endl;
 
444
        }    
 
445
 
 
446
        delete cmd;
 
447
 
 
448
    }
 
449
 
 
450
    sendBusy(false);
 
451
}
 
452
 
 
453
void CameraThread::sendBusy(bool val)
 
454
{
 
455
    CameraEvent* event = new CameraEvent(CameraEvent::gp_busy);
 
456
    event->result = val;
 
457
    QApplication::postEvent(parent, event);
 
458
}
 
459
 
 
460
void CameraThread::sendError(const QString& msg)
 
461
{
 
462
    CameraEvent* event = new CameraEvent(CameraEvent::gp_errormsg);
 
463
    event->msg = msg;
 
464
    QApplication::postEvent(parent, event);
 
465
}
 
466
 
 
467
 
 
468
void CameraThread::sendInfo(const QString& msg)
 
469
{
 
470
    CameraEvent* event = new CameraEvent(CameraEvent::gp_infomsg);
 
471
    event->msg = msg;
 
472
    QApplication::postEvent(parent, event);
 
473
}
 
474
 
 
475
// -- Camera Controller ------------------------------------------------------
 
476
 
 
477
CameraController::CameraController(QWidget* parent, const QString& model,
 
478
                                   const QString& port,
 
479
                                   const QString& path)
 
480
    : QObject(parent)
 
481
{
 
482
    d = new CameraControllerPriv;       
 
483
    d->parent        = parent;
 
484
    d->close         = false;
 
485
 
 
486
    d->overwriteAll  = false;
 
487
    d->skipAll       = false;
 
488
    d->downloadTotal = 0;
 
489
 
 
490
    if (model.lower() == "directory browse")
 
491
        d->camera = new UMSCamera(model, port, path);
 
492
    else
 
493
        d->camera = new GPCamera(model, port, path);
 
494
        
 
495
    d->thread = new CameraThread(this);
 
496
    d->timer  = new QTimer();
 
497
 
 
498
    connect(d->timer, SIGNAL(timeout()),
 
499
            SLOT(slotProcessNext()));
 
500
 
 
501
    d->timer->start(50, false);
 
502
}
 
503
 
 
504
CameraController::~CameraController()
 
505
{
 
506
    delete d->timer;
 
507
    
 
508
    d->camera->cancel();
 
509
    d->close = true;
 
510
 
 
511
    while (d->thread->running())
 
512
        d->thread->wait();
 
513
    delete d->thread;
 
514
    delete d->camera;
 
515
    delete d;
 
516
}
 
517
 
 
518
void CameraController::slotConnect()
 
519
{
 
520
    CameraCommand *cmd = new CameraCommand;
 
521
    cmd->action = CameraCommand::gp_connect;
 
522
    d->cmdQueue.enqueue(cmd);
 
523
}
 
524
 
 
525
void CameraController::listFolders()
 
526
{
 
527
    CameraCommand *cmd = new CameraCommand;
 
528
    cmd->action = CameraCommand::gp_listfolders;
 
529
    d->cmdQueue.enqueue(cmd);
 
530
}
 
531
 
 
532
void CameraController::listFiles(const QString& folder)
 
533
{
 
534
    CameraCommand *cmd = new CameraCommand;
 
535
    cmd->action = CameraCommand::gp_listfiles;
 
536
    cmd->map.insert("folder", QVariant(folder));
 
537
    d->cmdQueue.enqueue(cmd);
 
538
}
 
539
 
 
540
void CameraController::getThumbnail(const QString& folder, const QString& file)
 
541
{
 
542
    CameraCommand *cmd = new CameraCommand;
 
543
    cmd->action = CameraCommand::gp_thumbnail;
 
544
    cmd->map.insert("folder", QVariant(folder));
 
545
    cmd->map.insert("file", QVariant(file));
 
546
    d->cmdQueue.enqueue(cmd);
 
547
}
 
548
 
 
549
void CameraController::getExif(const QString& folder, const QString& file)
 
550
{
 
551
    CameraCommand *cmd = new CameraCommand;
 
552
    cmd->action = CameraCommand::gp_exif;
 
553
    cmd->map.insert("folder", QVariant(folder));
 
554
    cmd->map.insert("file", QVariant(file));
 
555
    d->cmdQueue.enqueue(cmd);
 
556
}
 
557
 
 
558
void CameraController::downloadPrep()
 
559
{
 
560
    d->overwriteAll  = false;
 
561
    d->skipAll       = false;
 
562
    d->downloadTotal = 0;
 
563
}
 
564
 
 
565
void CameraController::download(const QString& folder, const QString& file,
 
566
                                const QString& dest, bool autoRotate)
 
567
{
 
568
    CameraCommand *cmd = new CameraCommand;
 
569
    cmd->action = CameraCommand::gp_download;
 
570
    cmd->map.insert("folder", QVariant(folder));
 
571
    cmd->map.insert("file", QVariant(file));
 
572
    cmd->map.insert("dest", QVariant(dest));
 
573
    cmd->map.insert("autoRotate", QVariant(autoRotate, 0));
 
574
    d->cmdQueue.enqueue(cmd);
 
575
}
 
576
 
 
577
void CameraController::deleteFile(const QString& folder, const QString& file)
 
578
{
 
579
    CameraCommand *cmd = new CameraCommand;
 
580
    cmd->action = CameraCommand::gp_delete;
 
581
    cmd->map.insert("folder", QVariant(folder));
 
582
    cmd->map.insert("file", QVariant(file));
 
583
    d->cmdQueue.enqueue(cmd);
 
584
}
 
585
 
 
586
void CameraController::openFile(const QString& folder, const QString& file)
 
587
{
 
588
    CameraCommand *cmd = new CameraCommand;
 
589
    cmd->action = CameraCommand::gp_open;
 
590
    cmd->map.insert("folder", QVariant(folder));
 
591
    cmd->map.insert("file", QVariant(file));
 
592
    cmd->map.insert("dest", QVariant(locateLocal("tmp", file)));
 
593
    d->cmdQueue.enqueue(cmd);
 
594
}
 
595
 
 
596
void CameraController::slotCancel()
 
597
{
 
598
    d->cmdQueue.flush();   
 
599
    d->camera->cancel();
 
600
}
 
601
 
 
602
void CameraController::customEvent(QCustomEvent* e)
 
603
{
 
604
    CameraEvent* event = dynamic_cast<CameraEvent*>(e);
 
605
    if (!event)
 
606
    {
 
607
        return;
 
608
    }
 
609
    
 
610
    switch(event->type()-QEvent::User)
 
611
    {
 
612
    case (CameraEvent::gp_connected) :
 
613
    {
 
614
        emit signalConnected(event->result);
 
615
        break;
 
616
    }
 
617
    case (CameraEvent::gp_errormsg) :
 
618
    {
 
619
        emit signalErrorMsg(QDeepCopy<QString>(event->msg));
 
620
        break;
 
621
    }
 
622
    case (CameraEvent::gp_busy) :
 
623
    {
 
624
        if (event->result)
 
625
            emit signalBusy(true);
 
626
        break;
 
627
    }
 
628
    case (CameraEvent::gp_infomsg) :
 
629
    {
 
630
        emit signalInfoMsg(QDeepCopy<QString>(event->msg));
 
631
        break;
 
632
    }
 
633
    case (CameraEvent::gp_listedfolders) :
 
634
    {
 
635
        /* TODO: ugly hack since qt <= 3.1.2 does not define
 
636
           QStringList with QDeepCopy as a friend. */
 
637
        QValueList<QVariant> flist =
 
638
            QDeepCopy< QValueList<QVariant> >(event->map["folders"].toList());
 
639
 
 
640
        QStringList folderList;
 
641
        QValueList<QVariant>::Iterator it;
 
642
        for (it = flist.begin(); it != flist.end(); ++it )
 
643
        {
 
644
            folderList.append(QDeepCopy<QString>((*it).asString()));
 
645
        }
 
646
        
 
647
        emit signalFolderList(folderList);
 
648
        break;
 
649
    }
 
650
    case (CameraEvent::gp_listedfiles) :
 
651
    {
 
652
        QString    folder = QDeepCopy<QString>(event->map["folder"].asString());
 
653
        QByteArray ba     = QDeepCopy<QByteArray>(event->map["files"].asByteArray());
 
654
        QDataStream ds(ba, IO_ReadOnly);
 
655
        GPItemInfoList items;
 
656
        ds >> items;
 
657
        emit signalFileList(items);
 
658
        break;
 
659
    }
 
660
    case (CameraEvent::gp_thumbnailed) :
 
661
    {
 
662
        QString folder = QDeepCopy<QString>(event->map["folder"].asString());
 
663
        QString file   = QDeepCopy<QString>(event->map["file"].asString());
 
664
        QImage  thumb  = QDeepCopy<QImage>(event->map["thumbnail"].asImage());
 
665
        emit signalThumbnail(folder, file, thumb);
 
666
        break;
 
667
    }
 
668
    case (CameraEvent::gp_exif) :
 
669
    {
 
670
        QString folder = QDeepCopy<QString>(event->map["folder"].asString());
 
671
        QString file   = QDeepCopy<QString>(event->map["file"].asString());
 
672
        QByteArray ba  = QDeepCopy<QByteArray>(event->map["exifData"].asByteArray());
 
673
 
 
674
        KExifDialog exif(d->parent);
 
675
        if (exif.loadData(file, ba.data(), ba.size()))
 
676
        {
 
677
            exif.exec();
 
678
        }
 
679
        else 
 
680
        {
 
681
            KMessageBox::sorry(0, i18n("Failed to retrieve EXIF information for %1")
 
682
                               .arg(file));
 
683
        }
 
684
        
 
685
        break;
 
686
    }
 
687
    case (CameraEvent::gp_downloaded) :
 
688
    {
 
689
        QString folder = QDeepCopy<QString>(event->map["folder"].asString());
 
690
        QString file   = QDeepCopy<QString>(event->map["file"].asString());
 
691
        emit signalDownloaded(folder, file);
 
692
        break;
 
693
    }
 
694
    case (CameraEvent::gp_downloadFailed) :
 
695
    {
 
696
        QString folder = QDeepCopy<QString>(event->map["folder"].asString());
 
697
        QString file   = QDeepCopy<QString>(event->map["file"].asString());
 
698
 
 
699
        d->timer->stop();
 
700
 
 
701
        QString msg = i18n("Failed to download file %1.")
 
702
                      .arg(file);
 
703
        
 
704
        if (d->cmdQueue.isEmpty())
 
705
        {
 
706
            KMessageBox::error(d->parent, msg);
 
707
        }
 
708
        else
 
709
        {
 
710
            msg += i18n(" Do you want to continue?");
 
711
            int result = KMessageBox::warningContinueCancel(d->parent, msg);
 
712
            if (result != KMessageBox::Continue)
 
713
                slotCancel();
 
714
        }
 
715
 
 
716
        d->timer->start(50);
 
717
        emit signalDownloaded(folder, file);
 
718
        break;
 
719
    }
 
720
    case (CameraEvent::gp_deleted) :
 
721
    {
 
722
        QString folder = QDeepCopy<QString>(event->map["folder"].asString());
 
723
        QString file   = QDeepCopy<QString>(event->map["file"].asString());
 
724
        emit signalDeleted(folder, file);
 
725
        break;
 
726
    }
 
727
    case (CameraEvent::gp_deleteFailed) :
 
728
    {
 
729
        QString folder = QDeepCopy<QString>(event->map["folder"].asString());
 
730
        QString file   = QDeepCopy<QString>(event->map["file"].asString());
 
731
 
 
732
        d->timer->stop();
 
733
 
 
734
        QString msg = i18n("Failed to delete file %1.")
 
735
                      .arg(file);
 
736
        
 
737
        if (d->cmdQueue.isEmpty())
 
738
        {
 
739
            KMessageBox::error(d->parent, msg);
 
740
        }
 
741
        else
 
742
        {
 
743
            msg += i18n(" Do you want to continue?");
 
744
            int result = KMessageBox::warningContinueCancel(d->parent, msg);
 
745
            if (result != KMessageBox::Continue)
 
746
                slotCancel();
 
747
        }
 
748
 
 
749
        d->timer->start(50);
 
750
        break;
 
751
    }
 
752
    case (CameraEvent::gp_opened) :
 
753
    {
 
754
        QString file   = QDeepCopy<QString>(event->map["file"].asString());
 
755
        QString dest   = QDeepCopy<QString>(event->map["dest"].asString());
 
756
 
 
757
        KURL url(dest);
 
758
        KURL::List urlList;
 
759
        urlList << url;
 
760
 
 
761
        ImageWindow *im = ImageWindow::imagewindow();
 
762
        im->loadURL(urlList, url, file, false);
 
763
        if (im->isHidden())
 
764
            im->show();
 
765
        else
 
766
            im->raise();
 
767
        im->setFocus();
 
768
        break;
 
769
    }
 
770
    default:
 
771
        kdWarning() << k_funcinfo << "Unknown event" << endl;
 
772
    }
 
773
}
 
774
 
 
775
void CameraController::slotProcessNext()
 
776
{
 
777
    if (d->thread->running())
 
778
        return;
 
779
 
 
780
    if (d->cmdQueue.isEmpty())
 
781
    {
 
782
        emit signalBusy(false);
 
783
        return;
 
784
    }
 
785
 
 
786
    d->timer->stop();
 
787
    emit signalBusy(true);
 
788
    
 
789
    CameraCommand* cmd = d->cmdQueue.head();
 
790
 
 
791
    bool skip      = false;
 
792
    bool cancel    = false;
 
793
    bool overwrite = false;
 
794
 
 
795
    QString folder;
 
796
    QString file;
 
797
    QString dest;
 
798
 
 
799
    if ((cmd->action == CameraCommand::gp_exif) &&
 
800
        (typeid(*(d->camera)) == typeid(UMSCamera)))
 
801
    {
 
802
        folder = QDeepCopy<QString>(cmd->map["folder"].asString());
 
803
        file   = QDeepCopy<QString>(cmd->map["file"].asString());
 
804
 
 
805
        KExifDialog exif(d->parent);
 
806
        exif.loadFile(folder + "/" + file);
 
807
        exif.exec();
 
808
 
 
809
        d->cmdQueue.dequeue();
 
810
        d->timer->start(50, false);
 
811
        return;
 
812
    }
 
813
        
 
814
    if (cmd->action == CameraCommand::gp_download)
 
815
    {
 
816
        folder = QDeepCopy<QString>(cmd->map["folder"].asString());
 
817
        file   = QDeepCopy<QString>(cmd->map["file"].asString());
 
818
        dest   = QDeepCopy<QString>(cmd->map["dest"].asString());
 
819
 
 
820
        if (!d->overwriteAll)
 
821
        {
 
822
            struct stat info;
 
823
            while (::stat(QFile::encodeName(dest), &info) == 0)
 
824
            {
 
825
                if (d->skipAll)
 
826
                {
 
827
                    skip = true;
 
828
                    break;
 
829
                }
 
830
 
 
831
                KIO::RenameDlg dlg(d->parent, i18n("Rename File"), file, dest,
 
832
                                   KIO::RenameDlg_Mode(KIO::M_MULTI |
 
833
                                                       KIO::M_OVERWRITE |
 
834
                                                       KIO::M_SKIP));
 
835
            
 
836
                int result = dlg.exec();
 
837
                dest       = dlg.newDestURL().path();
 
838
 
 
839
                switch (result)
 
840
                {
 
841
                case KIO::R_CANCEL:
 
842
                {
 
843
                    cancel = true;
 
844
                    break;
 
845
                }
 
846
                case KIO::R_SKIP:
 
847
                {
 
848
                    skip = true;
 
849
                    break;
 
850
                }
 
851
                case KIO::R_AUTO_SKIP:
 
852
                {
 
853
                    d->skipAll = true;
 
854
                    skip       = true;
 
855
                    break;
 
856
                }
 
857
                case KIO::R_OVERWRITE:
 
858
                {
 
859
                    overwrite       = true;
 
860
                    break;
 
861
                }
 
862
                case KIO::R_OVERWRITE_ALL:
 
863
                {
 
864
                    d->overwriteAll = true;
 
865
                    overwrite       = true;
 
866
                    break;
 
867
                }
 
868
                default:
 
869
                    break;
 
870
                }
 
871
 
 
872
                if (cancel || skip || overwrite)
 
873
                    break;
 
874
            }
 
875
        }
 
876
 
 
877
        cmd->map["dest"] = QVariant(QDeepCopy<QString>(dest));
 
878
        
 
879
    }
 
880
 
 
881
    if (cancel)
 
882
    {
 
883
        slotCancel();
 
884
        d->timer->start(50, false);
 
885
        return;
 
886
    }
 
887
    else if (skip)
 
888
    {
 
889
        d->cmdQueue.dequeue();
 
890
        emit signalInfoMsg(i18n("Skipped file %1")
 
891
                           .arg(file));
 
892
        emit signalSkipped(folder, file);        
 
893
        d->timer->start(50, false);
 
894
        return;
 
895
    }
 
896
 
 
897
    d->thread->start();
 
898
    d->timer->start(50, false);
 
899
}
 
900
 
 
901
#include "cameracontroller.moc"