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
|
/* ============================================================
*
* This file is a part of digiKam project
* http://www.digikam.org
*
* Date : 2006-01-20
* Description : image file IO threaded interface.
*
* Copyright (C) 2005-2007 by Marcel Wiesweg <marcel.wiesweg@gmx.de>
* Copyright (C) 2005-2007 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.
*
* ============================================================ */
// Local includes.
#include "ddebug.h"
#include "managedloadsavethread.h"
#include "loadsavetask.h"
#include "previewtask.h"
namespace Digikam
{
ManagedLoadSaveThread::ManagedLoadSaveThread()
{
m_terminationPolicy = TerminationPolicyTerminateLoading;
}
ManagedLoadSaveThread::~ManagedLoadSaveThread()
{
LoadingTask *loadingTask;
switch (m_terminationPolicy)
{
case TerminationPolicyTerminateLoading:
{
TQMutexLocker lock(&m_mutex);
if ( (loadingTask = checkLoadingTask(m_currentTask, LoadingTaskFilterAll)) )
loadingTask->setStatus(LoadingTask::LoadingTaskStatusStopping);
removeLoadingTasks(LoadingDescription(TQString()), LoadingTaskFilterAll);
break;
}
case TerminationPolicyTerminatePreloading:
{
TQMutexLocker lock(&m_mutex);
if ( (loadingTask = checkLoadingTask(m_currentTask, LoadingTaskFilterPreloading)) )
loadingTask->setStatus(LoadingTask::LoadingTaskStatusStopping);
removeLoadingTasks(LoadingDescription(TQString()), LoadingTaskFilterPreloading);
break;
}
case TerminationPolicyWait:
break;
}
}
LoadingTask *ManagedLoadSaveThread::checkLoadingTask(LoadSaveTask *task, LoadingTaskFilter filter)
{
if (task && task->type() == LoadSaveTask::TaskTypeLoading)
{
LoadingTask *loadingTask = (LoadingTask *)task;
if (filter == LoadingTaskFilterAll)
return loadingTask;
else if (filter == LoadingTaskFilterPreloading)
if (loadingTask->status() == LoadingTask::LoadingTaskStatusPreloading)
return loadingTask;
}
return 0;
}
LoadingTask *ManagedLoadSaveThread::findExistingTask(const LoadingDescription &loadingDescription)
{
LoadingTask *loadingTask;
if (m_currentTask)
{
if (m_currentTask->type() == LoadSaveTask::TaskTypeLoading)
{
loadingTask = (LoadingTask *)m_currentTask;
LoadingDescription taskDescription = loadingTask->loadingDescription();
if (taskDescription == loadingDescription)
return loadingTask;
}
}
for (LoadSaveTask *task = m_todo.first(); task; task = m_todo.next())
{
if (task->type() == LoadSaveTask::TaskTypeLoading)
{
loadingTask = (LoadingTask *)task;
if (loadingTask->loadingDescription() == loadingDescription)
return loadingTask;
}
}
return 0;
}
void ManagedLoadSaveThread::setTerminationPolicy(TerminationPolicy terminationPolicy)
{
m_terminationPolicy = terminationPolicy;
}
void ManagedLoadSaveThread::load(LoadingDescription description, LoadingPolicy policy)
{
load(description, LoadingModeNormal, policy);
}
void ManagedLoadSaveThread::load(LoadingDescription description, LoadingMode loadingMode, LoadingPolicy policy, AccessMode accessMode)
{
TQMutexLocker lock(&m_mutex);
LoadingTask *loadingTask = 0;
LoadingTask *existingTask = findExistingTask(description);
//DDebug() << "ManagedLoadSaveThread::load " << description.filePath << ", policy " << policy << endl;
switch(policy)
{
case LoadingPolicyFirstRemovePrevious:
// reuse task if it exists
if (existingTask)
{
existingTask->setStatus(LoadingTask::LoadingTaskStatusLoading);
}
// stop current task
if (m_currentTask && m_currentTask != existingTask)
{
if ( (loadingTask = checkLoadingTask(m_currentTask, LoadingTaskFilterAll)) )
loadingTask->setStatus(LoadingTask::LoadingTaskStatusStopping);
}
//DDebug() << "LoadingPolicyFirstRemovePrevious, Existing task " << existingTask <<
//", m_currentTask " << m_currentTask << ", loadingTask " << loadingTask << endl;
// remove all loading tasks
for (LoadSaveTask *task = m_todo.first(); task; task = m_todo.next())
{
if (task != existingTask && checkLoadingTask(task, LoadingTaskFilterAll))
{
//DDebug() << "Removing task " << task << " from list" << endl;
m_todo.remove();
m_todo.prev();
}
}
// append new, exclusive loading task
if (existingTask)
break;
m_todo.append(createLoadingTask(description, false, loadingMode, accessMode));
break;
case LoadingPolicyPrepend:
if (existingTask)
{
existingTask->setStatus(LoadingTask::LoadingTaskStatusLoading);
}
// stop and postpone current task if it is a preloading task
if (m_currentTask)
{
if ( (loadingTask = checkLoadingTask(m_currentTask, LoadingTaskFilterPreloading)) )
{
loadingTask->setStatus(LoadingTask::LoadingTaskStatusStopping);
load(loadingTask->filePath(), LoadingPolicyPreload);
}
}
//DDebug() << "LoadingPolicyPrepend, Existing task " << existingTask << ", m_currentTask " << m_currentTask << endl;
// prepend new loading task
if (existingTask)
break;
m_todo.prepend(createLoadingTask(description, false, loadingMode, accessMode));
break;
case LoadingPolicyAppend:
if (existingTask)
{
existingTask->setStatus(LoadingTask::LoadingTaskStatusLoading);
}
// stop and postpone current task if it is a preloading task
if (m_currentTask)
{
if ( (loadingTask = checkLoadingTask(m_currentTask, LoadingTaskFilterPreloading)) )
{
loadingTask->setStatus(LoadingTask::LoadingTaskStatusStopping);
load(loadingTask->filePath(), LoadingPolicyPreload);
}
}
if (existingTask)
break;
//DDebug() << "LoadingPolicyAppend, Existing task " << existingTask << ", m_currentTask " << m_currentTask << endl;
// append new loading task, put it in front of preloading tasks
for (uint i = 0; i<m_todo.count(); i++)
{
LoadSaveTask *task = m_todo.at(i);
if ( (loadingTask = checkLoadingTask(task, LoadingTaskFilterPreloading)) )
{
m_todo.insert(i, createLoadingTask(description, false, loadingMode, accessMode));
break;
}
}
break;
case LoadingPolicyPreload:
// append to the very end of the list
//DDebug() << "LoadingPolicyPreload, Existing task " << existingTask << endl;
if (existingTask)
break;
m_todo.append(createLoadingTask(description, true, loadingMode, accessMode));
break;
}
m_condVar.wakeAll();
}
void ManagedLoadSaveThread::loadPreview(LoadingDescription description)
{
// This is similar to the LoadingPolicyFirstRemovePrevious policy with normal loading tasks.
// Preview threads typically only support preview tasks,
// so no need to differentiate with normal loading tasks.
TQMutexLocker lock(&m_mutex);
LoadingTask *loadingTask = 0;
LoadingTask *existingTask = findExistingTask(description);
// reuse task if it exists
if (existingTask)
{
existingTask->setStatus(LoadingTask::LoadingTaskStatusLoading);
}
// stop current task
if (m_currentTask && m_currentTask != existingTask)
{
if ( (loadingTask = checkLoadingTask(m_currentTask, LoadingTaskFilterAll)) )
loadingTask->setStatus(LoadingTask::LoadingTaskStatusStopping);
}
// remove all loading tasks
for (LoadSaveTask *task = m_todo.first(); task; task = m_todo.next())
{
if (task != existingTask && checkLoadingTask(task, LoadingTaskFilterAll))
{
m_todo.remove();
m_todo.prev();
}
}
//DDebug()<<"loadPreview for " << description.filePath << " existingTask " << existingTask << " currentTask " << m_currentTask << endl;
// append new loading task
if (existingTask)
return;
m_todo.append(new PreviewLoadingTask(this, description));
m_condVar.wakeAll();
}
LoadingTask *ManagedLoadSaveThread::createLoadingTask(const LoadingDescription &description,
bool preloading, LoadingMode loadingMode, AccessMode accessMode)
{
if (loadingMode == LoadingModeShared)
{
if (preloading)
return new SharedLoadingTask(this, description, accessMode, LoadingTask::LoadingTaskStatusPreloading);
else
return new SharedLoadingTask(this, description, accessMode);
}
else
{
if (preloading)
return new LoadingTask(this, description, LoadingTask::LoadingTaskStatusPreloading);
else
return new LoadingTask(this, description);
}
}
void ManagedLoadSaveThread::stopLoading(const TQString& filePath, LoadingTaskFilter filter)
{
TQMutexLocker lock(&m_mutex);
removeLoadingTasks(LoadingDescription(filePath), filter);
}
void ManagedLoadSaveThread::stopLoading(const LoadingDescription& desc, LoadingTaskFilter filter)
{
TQMutexLocker lock(&m_mutex);
removeLoadingTasks(desc, filter);
}
void ManagedLoadSaveThread::stopSaving(const TQString& filePath)
{
TQMutexLocker lock(&m_mutex);
// stop current task if it is matching the criteria
if (m_currentTask && m_currentTask->type() == LoadSaveTask::TaskTypeSaving)
{
SavingTask *savingTask = (SavingTask *)m_currentTask;
if (filePath.isNull() || savingTask->filePath() == filePath)
{
savingTask->setStatus(SavingTask::SavingTaskStatusStopping);
}
}
// remove relevant tasks from list
for (LoadSaveTask *task = m_todo.first(); task; task = m_todo.next())
{
if (task->type() == LoadSaveTask::TaskTypeSaving)
{
SavingTask *savingTask = (SavingTask *)m_currentTask;
if (filePath.isNull() || savingTask->filePath() == filePath)
{
m_todo.remove();
m_todo.prev();
}
}
}
}
void ManagedLoadSaveThread::removeLoadingTasks(const LoadingDescription &description, LoadingTaskFilter filter)
{
LoadingTask *loadingTask;
// stop current task if it is matching the criteria
if ( (loadingTask = checkLoadingTask(m_currentTask, filter)) )
{
if (description.filePath.isNull() || loadingTask->loadingDescription() == description)
{
loadingTask->setStatus(LoadingTask::LoadingTaskStatusStopping);
}
}
// remove relevant tasks from list
for (LoadSaveTask *task = m_todo.first(); task; task = m_todo.next())
{
if ( (loadingTask = checkLoadingTask(task, filter)) )
{
if (description.filePath.isNull() || loadingTask->loadingDescription() == description)
{
m_todo.remove();
m_todo.prev();
}
}
}
}
void ManagedLoadSaveThread::save(DImg &image, const TQString& filePath, const TQString &format)
{
TQMutexLocker lock(&m_mutex);
LoadingTask *loadingTask;
// stop and postpone current task if it is a preloading task
if (m_currentTask && (loadingTask = checkLoadingTask(m_currentTask, LoadingTaskFilterPreloading)))
{
loadingTask->setStatus(LoadingTask::LoadingTaskStatusStopping);
load(loadingTask->filePath(), LoadingPolicyPreload);
}
// append new loading task, put it in front of preloading tasks
uint i;
for (i = 0; i<m_todo.count(); i++)
{
LoadSaveTask *task = m_todo.at(i);
if ( (loadingTask = checkLoadingTask(task, LoadingTaskFilterPreloading)) )
break;
}
m_todo.insert(i, new SavingTask(this, image, filePath, format));
}
} // namespace Digikam
|