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 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2009-02-06
* Description : Thread actions task.
*
* SPDX-FileCopyrightText: 2009-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
* SPDX-FileCopyrightText: 2012 by Pankaj Kumar <me at panks dot me>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "actiontask.h"
// Qt includes
#include <QFileInfo>
// KDE includes
#include <klocalizedstring.h>
// Local includes
#include "digikam_debug.h"
#include "digikam_config.h"
#include "dimg.h"
#include "dmetadata.h"
#include "iteminfo.h"
#include "batchtool.h"
#include "batchtoolsfactory.h"
#include "dfileoperations.h"
namespace Digikam
{
class Q_DECL_HIDDEN ActionTask::Private
{
public:
Private() = default;
bool cancel = false;
BatchTool* tool = nullptr;
QueueSettings settings;
AssignedBatchTools tools;
};
// -------------------------------------------------------
ActionTask::ActionTask()
: ActionJob(),
d (new Private)
{
}
ActionTask::~ActionTask()
{
slotCancel();
delete d;
}
void ActionTask::setSettings(const QueueSettings& settings)
{
d->settings = settings;
}
void ActionTask::setItem(const AssignedBatchTools& tools)
{
d->tools = tools;
}
void ActionTask::slotCancel()
{
if (d->tool)
{
d->tool->cancel();
}
d->cancel = true;
}
void ActionTask::removeTempFiles(const QList<QUrl>& tmpList)
{
for (const QUrl& url : std::as_const(tmpList))
{
QString tmpPath(url.toLocalFile());
QFile::remove(tmpPath);
tmpPath = DMetadata::sidecarPath(tmpPath);
if (QFile::exists(tmpPath))
{
QFile::remove(tmpPath);
}
}
}
void ActionTask::emitActionData(ActionData::ActionStatus st,
const QString& mess,
const QUrl& dest,
bool noWrite)
{
ActionData ad;
ad.fileUrl = d->tools.m_itemUrl;
ad.status = st;
ad.message = mess;
ad.destUrl = dest;
ad.noWrite = noWrite;
Q_EMIT signalFinished(ad);
}
void ActionTask::run()
{
if (d->cancel)
{
return;
}
emitActionData(ActionData::BatchStarted);
// Loop with all batch tools operations to apply on item.
bool success = false;
int index = 0;
QUrl outUrl = d->tools.m_itemUrl;
QUrl workUrl = !d->settings.useOrgAlbum ? d->settings.workingUrl
: d->tools.m_itemUrl.adjusted(QUrl::RemoveFilename);
workUrl = workUrl.adjusted(QUrl::StripTrailingSlash);
QUrl inUrl;
QList<QUrl> tmp2del;
DImg tmpImage;
QString errMsg;
// ItemInfo must be tread-safe.
ItemInfo source = ItemInfo::fromUrl(d->tools.m_itemUrl);
bool noWriteMetadata = false;
bool timeAdjust = false;
for (const BatchToolSet& set : std::as_const(d->tools.m_toolsList))
{
BatchTool* const tool = BatchToolsFactory::instance()->findTool(set.name, set.group);<--- Variable 'tool' can be declared as pointer to const
if (!tool)
{
emitActionData(ActionData::BatchFailed, i18n("Failed to find tool..."));
removeTempFiles(tmp2del);
Q_EMIT signalDone();
return;
}
d->tool = tool->clone();
d->tool->setToolIcon(tool->toolIcon());
d->tool->setToolTitle(tool->toolTitle());
d->tool->setToolDescription(tool->toolDescription());
// Only true if it is also the last tool
noWriteMetadata |= (
(set.group == BatchTool::MetadataTool) &&
(set.name != QLatin1String("TimeAdjust"))
);
timeAdjust |= (set.name == QLatin1String("TimeAdjust"));
inUrl = outUrl;
index = set.index + 1;
qCDebug(DIGIKAM_GENERAL_LOG) << "Tool : index= " << index
<< " :: name= " << set.name
<< " :: group= " << set.group
<< " :: wurl= " << workUrl;
d->tool->setImageData(tmpImage);
d->tool->setItemInfo(source);
d->tool->setInputUrl(inUrl);
d->tool->setWorkingUrl(workUrl);
d->tool->setSettings(set.settings);
d->tool->setIOFileSettings(d->settings.ioFileSettings);
d->tool->setRawLoadingRules(d->settings.rawLoadingRule);
d->tool->setDRawDecoderSettings(d->settings.rawDecodingSettings);
d->tool->setResetExifOrientationAllowed(d->settings.exifSetOrientation);
if (index == d->tools.m_toolsList.count())
{
d->tool->setLastChainedTool(true);
}
else if (d->tools.m_toolsList[index].group == BatchTool::CustomTool)
{
// If the next tool is under the custom group (user script)
// treat as the last chained tool, i.e. save image to file
d->tool->setLastChainedTool(true);
}
else
{
d->tool->setLastChainedTool(false);
}
d->tool->setSaveAsNewVersion(d->settings.saveAsNewVersion);
d->tool->setOutputUrlFromInputUrl();
d->tool->setBranchHistory(true);
outUrl = d->tool->outputUrl();
success = d->tool->apply();
tmpImage = d->tool->imageData();
errMsg = d->tool->errorDescription();
tmp2del.append(outUrl);
delete d->tool;
d->tool = nullptr;
if (d->cancel)
{
emitActionData(ActionData::BatchCanceled);
removeTempFiles(tmp2del);
Q_EMIT signalDone();
return;
}
else if (!success)
{
emitActionData(ActionData::BatchFailed, errMsg);
break;
}
}
// Clean up all tmp url.
// We don't remove last output tmp url.
tmp2del.removeAll(outUrl);
removeTempFiles(tmp2del);
// Move processed temp file to target
QString renameMess;
QUrl dest = workUrl;
dest.setPath(dest.path() + QLatin1Char('/') + d->tools.m_destFileName);
if (QFileInfo::exists(dest.toLocalFile()))
{
if (d->settings.conflictRule == FileSaveConflictBox::OVERWRITE)
{
renameMess = i18n("(overwritten)");
}
else if (d->settings.conflictRule == FileSaveConflictBox::DIFFNAME)
{
dest = DFileOperations::getUniqueFileUrl(dest);
renameMess = i18n("(renamed to %1)", dest.fileName());
}
else
{
emitActionData(ActionData::BatchSkipped,
i18n("Item exists and was skipped"), dest);
removeTempFiles(QList<QUrl>() << outUrl);
Q_EMIT signalDone();
return;
}
}
if (QFileInfo(outUrl.toLocalFile()).size() == 0)
{
removeTempFiles(QList<QUrl>() << outUrl);
dest.clear();
}
if (!dest.isEmpty())
{
if (DMetadata::hasSidecar(outUrl.toLocalFile()))
{
if (!DFileOperations::localFileRename(d->tools.m_itemUrl.toLocalFile(),
DMetadata::sidecarPath(outUrl.toLocalFile()),
DMetadata::sidecarPath(dest.toLocalFile()),
timeAdjust))
{
emitActionData(ActionData::BatchFailed, i18n("Failed to create sidecar file..."), dest);
}
}
if (DFileOperations::localFileRename(d->tools.m_itemUrl.toLocalFile(),
outUrl.toLocalFile(),
dest.toLocalFile(),
timeAdjust))
{
emitActionData(ActionData::BatchDone, i18n("Item processed successfully %1", renameMess),
dest, noWriteMetadata);
}
else
{
emitActionData(ActionData::BatchFailed, i18n("Failed to create file..."), dest);
}
}
else
{
emitActionData(ActionData::BatchFailed, i18n("Failed to create file..."), dest);
}
Q_EMIT signalDone();
}
} // namespace Digikam
#include "moc_actiontask.cpp"
|