1 /* ============================================================
2  *
3  * This file is a part of digiKam project
4  * https://www.digikam.org
5  *
6  * Date        : 2008-05-16
7  * Description : fingerprints generator
8  *
9  * Copyright (C)      2018 by Mario Frank    <mario dot frank at uni minus potsdam dot de>
10  * Copyright (C) 2008-2021 by Gilles Caulier <caulier dot gilles at gmail dot com>
11  * Copyright (C) 2012      by Andi Clemens <andi dot clemens at gmail dot com>
12  *
13  * This program is free software; you can redistribute it
14  * and/or modify it under the terms of the GNU General
15  * Public License as published by the Free Software Foundation;
16  * either version 2, or (at your option)
17  * any later version.
18  *
19  * This program is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
22  * GNU General Public License for more details.
23  *
24  * ============================================================ */
25 
26 #include "fingerprintsgenerator.h"
27 
28 // Qt includes
29 
30 #include <QApplication>
31 #include <QString>
32 #include <QIcon>
33 
34 // KDE includes
35 
36 #include <kconfiggroup.h>
37 #include <klocalizedstring.h>
38 #include <ksharedconfig.h>
39 
40 // Local includes
41 
42 #include "dimg.h"
43 #include "coredb.h"
44 #include "albummanager.h"
45 #include "coredbaccess.h"
46 #include "maintenancethread.h"
47 
48 namespace Digikam
49 {
50 
51 class Q_DECL_HIDDEN FingerPrintsGenerator::Private
52 {
53 public:
54 
Private()55     explicit Private()
56       : rebuildAll(true),
57         thread(nullptr)
58     {
59     }
60 
61     bool               rebuildAll;
62 
63     AlbumList          albumList;
64 
65     MaintenanceThread* thread;
66 };
67 
FingerPrintsGenerator(const bool rebuildAll,const AlbumList & list,ProgressItem * const parent)68 FingerPrintsGenerator::FingerPrintsGenerator(const bool rebuildAll, const AlbumList& list, ProgressItem* const parent)
69     : MaintenanceTool(QLatin1String("FingerPrintsGenerator"), parent),
70       d(new Private)
71 {
72     setLabel(i18n("Finger-prints"));
73     ProgressManager::addProgressItem(this);
74 
75     d->albumList  = list;
76     d->rebuildAll = rebuildAll;
77     d->thread     = new MaintenanceThread(this);
78 
79     connect(d->thread, SIGNAL(signalCompleted()),
80             this, SLOT(slotDone()));
81 
82     connect(d->thread, SIGNAL(signalAdvance(QImage)),
83             this, SLOT(slotAdvance(QImage)));
84 }
85 
~FingerPrintsGenerator()86 FingerPrintsGenerator::~FingerPrintsGenerator()
87 {
88     delete d;
89 }
90 
setUseMultiCoreCPU(bool b)91 void FingerPrintsGenerator::setUseMultiCoreCPU(bool b)
92 {
93     d->thread->setUseMultiCore(b);
94 }
95 
slotCancel()96 void FingerPrintsGenerator::slotCancel()
97 {
98     d->thread->cancel();
99     MaintenanceTool::slotCancel();
100 }
101 
slotStart()102 void FingerPrintsGenerator::slotStart()
103 {
104     MaintenanceTool::slotStart();
105 
106     QApplication::setOverrideCursor(Qt::WaitCursor);
107 
108     if (d->albumList.isEmpty())
109     {
110         d->albumList = AlbumManager::instance()->allPAlbums();
111     }
112 
113     // Get all item IDs from albums.
114 
115     QList<qlonglong> itemIds;
116 
117     for (AlbumList::ConstIterator it = d->albumList.constBegin() ;
118          !canceled() && (it != d->albumList.constEnd()) ; ++it)
119     {
120         if ((*it)->type() == Album::PHYSICAL)
121         {
122             itemIds << CoreDbAccess().db()->getItemIDsInAlbum((*it)->id());
123         }
124         else if ((*it)->type() == Album::TAG)
125         {
126             itemIds << CoreDbAccess().db()->getItemIDsInTag((*it)->id());
127         }
128     }
129 
130     QApplication::restoreOverrideCursor();
131 
132     if (itemIds.isEmpty())
133     {
134         slotDone();
135         return;
136     }
137 
138     setTotalItems(itemIds.count());
139 
140     d->thread->generateFingerprints(itemIds, d->rebuildAll);
141     d->thread->start();
142 }
143 
slotAdvance(const QImage & img)144 void FingerPrintsGenerator::slotAdvance(const QImage& img)
145 {
146     setThumbnail(QIcon(QPixmap::fromImage(img)));
147     advance(1);
148 }
149 
slotDone()150 void FingerPrintsGenerator::slotDone()
151 {
152     // Switch on scanned for finger-prints flag on digiKam config file.
153     KSharedConfig::openConfig()->group(QLatin1String("General Settings")).writeEntry(QLatin1String("Finger Prints Generator First Run"), true);
154 
155     MaintenanceTool::slotDone();
156 }
157 
158 } // namespace Digikam
159