1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2013-08-14
7  * Description : Thread actions task for thumbs generator.
8  *
9  * Copyright (C) 2013-2021 by Gilles Caulier <caulier dot gilles 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)
15  * any later version.
16  *
17  * This program is distributed in the hope that it will be useful,
18  * but WITHOUT ANY WARRANTY; without even the implied warranty of
19  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
20  * GNU General Public License for more details.
21  *
22  * ============================================================ */
23 
24 #include "thumbstask.h"
25 
26 // Qt includes
27 #include <QQueue>
28 
29 // Local includes
30 
31 #include "digikam_debug.h"
32 #include "thumbnailloadthread.h"
33 #include "thumbnailsize.h"
34 #include "maintenancedata.h"
35 
36 namespace Digikam
37 {
38 
39 class Q_DECL_HIDDEN ThumbsTask::Private
40 {
41 public:
42 
Private()43     explicit Private()
44         : catcher(nullptr),
45           data   (nullptr)
46     {
47     }
48 
49     ThumbnailImageCatcher* catcher;
50 
51     MaintenanceData*       data;
52 };
53 
54 // -------------------------------------------------------
55 
ThumbsTask()56 ThumbsTask::ThumbsTask()
57     : ActionJob(),
58       d        (new Private)
59 {
60     ThumbnailLoadThread* const thread = new ThumbnailLoadThread;
61     thread->setPixmapRequested(false);
62     thread->setThumbnailSize(ThumbnailLoadThread::maximumThumbnailSize());
63     d->catcher                        = new ThumbnailImageCatcher(thread, this);
64 }
65 
~ThumbsTask()66 ThumbsTask::~ThumbsTask()
67 {
68     cancel();
69 
70     d->catcher->setActive(false);
71     d->catcher->thread()->stopAllTasks();
72 
73     delete d->catcher->thread();
74     delete d->catcher;
75     delete d;
76 }
77 
setMaintenanceData(MaintenanceData * const data)78 void ThumbsTask::setMaintenanceData(MaintenanceData* const data)
79 {
80     d->data = data;
81 }
82 
run()83 void ThumbsTask::run()
84 {
85     d->catcher->setActive(true);
86 
87     // While we have data (using this as check for non-null)
88     while (d->data)
89     {
90         if (m_cancel)
91         {
92             d->catcher->setActive(false);
93             d->catcher->thread()->stopAllTasks();
94             return;
95         }
96 
97         QString path = d->data->getImagePath();
98 
99         if (path.isEmpty())
100         {
101             break;
102         }
103 
104         // TODO Should be improved by some update procedure
105         d->catcher->thread()->deleteThumbnail(path);
106         d->catcher->thread()->find(ThumbnailIdentifier(path));
107         d->catcher->enqueue();
108         QList<QImage> images = d->catcher->waitForThumbnails();
109         emit signalFinished(images.first());
110     }
111 
112     emit signalDone();
113 
114     d->catcher->setActive(false);
115 }
116 
117 } // namespace Digikam
118