1 /* ============================================================
2  *
3  * This file is a part of kipi-plugins project
4  * https://www.digikam.org
5  *
6  * Date        : 2012-12-24
7  * Description : DNG Converter thread
8  *
9  * Copyright (C) 2012 by Smit Mehta <smit dot meh at gmail dot com>
10  *
11  * This program is free software; you can redistribute it
12  * and/or modify it under the terms of the GNU General
13  * Public License as published by the Free Software Foundation;
14  * either version 2, or (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19  * GNU General Public License for more details.
20  *
21  * ============================================================ */
22 
23 #include "dngconvertertask.h"
24 
25 // Qt includes
26 
27 #include <QFileInfo>
28 
29 // KDE includes
30 
31 #include <klocalizedstring.h>
32 
33 // Local includes
34 
35 #include "drawinfo.h"
36 #include "drawdecoder.h"
37 #include "dngconverteractions.h"
38 #include "dmetadata.h"
39 #include "digikam_debug.h"
40 
41 namespace DigikamGenericDNGConverterPlugin
42 {
43 
44 class DNGConverterTask::Private
45 {
46 public:
47 
Private()48     Private()
49       : backupOriginalRawFile(false),
50         compressLossLess     (true),
51         updateFileDate       (false),
52         cancel               (false),
53         previewMode          (DNGWriter::MEDIUM),
54         action               (NONE)
55     {
56     }
57 
58     bool               backupOriginalRawFile;
59     bool               compressLossLess;
60     bool               updateFileDate;
61     bool               cancel;
62 
63     int                previewMode;
64 
65     QUrl               url;
66     DNGConverterAction action;
67 
68     DNGWriter          dngProcessor;
69 };
70 
DNGConverterTask(QObject * const parent,const QUrl & fileUrl,const DNGConverterAction & action)71 DNGConverterTask::DNGConverterTask(QObject* const parent, const QUrl& fileUrl, const DNGConverterAction& action)
72     : ActionJob(parent),
73       d        (new Private)
74 {
75     d->url    = fileUrl;
76     d->action = action;
77 }
78 
~DNGConverterTask()79 DNGConverterTask::~DNGConverterTask()
80 {
81     slotCancel();
82     delete d;
83 }
84 
setBackupOriginalRawFile(bool b)85 void DNGConverterTask::setBackupOriginalRawFile(bool b)
86 {
87     d->backupOriginalRawFile = b;
88 }
89 
setCompressLossLess(bool b)90 void DNGConverterTask::setCompressLossLess(bool b)
91 {
92     d->compressLossLess = b;
93 }
94 
setUpdateFileDate(bool b)95 void DNGConverterTask::setUpdateFileDate(bool b)
96 {
97     d->updateFileDate = b;
98 }
99 
setPreviewMode(int mode)100 void DNGConverterTask::setPreviewMode(int mode)
101 {
102     d->previewMode = mode;
103 }
104 
run()105 void DNGConverterTask::run()
106 {
107     if (d->cancel)
108     {
109         return;
110     }
111 
112     switch (d->action)
113     {
114         case IDENTIFY:
115         {
116             // Identify Camera model.
117 
118             DRawInfo info;
119             DRawDecoder::rawFileIdentify(info, d->url.toLocalFile());
120 
121             QString identify = i18n("Cannot identify Raw image");
122 
123             if (info.isDecodable)
124             {
125                 identify = info.make + QLatin1String("-") + info.model;
126             }
127 
128             DNGConverterActionData ad;
129             ad.action        = d->action;
130             ad.fileUrl       = d->url;
131             ad.message       = identify;
132             ad.result        = DNGWriter::PROCESS_COMPLETE;
133             emit signalFinished(ad);
134             break;
135         }
136 
137         case PROCESS:
138         {
139             DNGConverterActionData ad1;
140             ad1.action   = PROCESS;
141             ad1.fileUrl  = d->url;
142             ad1.starting = true;
143             emit signalStarting(ad1);
144 
145             QString destPath;
146 
147             QFileInfo fi(d->url.toLocalFile());
148             destPath     = fi.absolutePath() + QLatin1String("/.digikam-dngconverter-tmp-") +
149                            QString::number(QDateTime::currentDateTimeUtc().toTime_t()) + QString(d->url.fileName());
150 
151             d->dngProcessor.reset();
152             d->dngProcessor.setInputFile(d->url.toLocalFile());
153             d->dngProcessor.setOutputFile(destPath);
154             d->dngProcessor.setBackupOriginalRawFile(d->backupOriginalRawFile);
155             d->dngProcessor.setCompressLossLess(d->compressLossLess);
156             d->dngProcessor.setUpdateFileDate(d->updateFileDate);
157             d->dngProcessor.setPreviewMode(d->previewMode);
158             int ret      = d->dngProcessor.convert();
159 
160             DNGConverterActionData ad2;
161             ad2.action   = PROCESS;
162             ad2.fileUrl  = d->url;
163             ad2.destPath = destPath;
164             ad2.result   = ret;
165             emit signalFinished(ad2);
166             break;
167         }
168 
169         default:
170         {
171             qCCritical(DIGIKAM_DPLUGIN_GENERIC_LOG) << "Unknown action specified";
172             break;
173         }
174     }
175 
176     emit signalDone();
177 }
178 
slotCancel()179 void DNGConverterTask::slotCancel()
180 {
181     d->cancel = true;
182     d->dngProcessor.cancel();
183 }
184 
185 } // namespace DigikamGenericDNGConverterPlugin
186 
187