| 1 |
/* ============================================================ |
| 2 |
* |
| 3 |
* This file is a part of digiKam project |
| 4 |
* https://www.digikam.org |
| 5 |
* |
| 6 |
* Date : 2004-06-15 |
| 7 |
* Description : Albums manager interface. |
| 8 |
* |
| 9 |
* SPDX-FileCopyrightText: 2004 by Renchi Raju <renchi dot raju at gmail dot com> |
| 10 |
* SPDX-FileCopyrightText: 2006-2026 by Gilles Caulier <caulier dot gilles at gmail dot com> |
| 11 |
* SPDX-FileCopyrightText: 2006-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de> |
| 12 |
* SPDX-FileCopyrightText: 2015 by Mohamed_Anwer <m_dot_anwer at gmx dot com> |
| 13 |
* |
| 14 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 15 |
* |
| 16 |
* ============================================================ */ |
| 17 |
|
| 18 |
#include "albummanager_p.h" |
| 19 |
|
| 20 |
namespace Digikam |
| 21 |
{ |
| 22 |
|
| 23 |
class Q_DECL_HIDDEN AlbumManagerCreator |
| 24 |
{ |
| 25 |
public: |
| 26 |
|
| 27 |
AlbumManager object; |
| 28 |
}; |
| 29 |
|
| 30 |
Q_GLOBAL_STATIC(AlbumManagerCreator, creator) |
| 31 |
|
| 32 |
AlbumManager* AlbumManager::instance() |
| 33 |
{ |
| 34 |
return &creator->object; |
| 35 |
} |
| 36 |
|
| 37 |
// ----------------------------------------------------------------------------------- |
| 38 |
|
| 39 |
AlbumManager::AlbumManager() |
| 40 |
: QObject(), |
| 41 |
d (new Private) |
| 42 |
{ |
| 43 |
qRegisterMetaType<QHash<QDateTime,int>>("QHash<QDateTime,int>"); |
| 44 |
qRegisterMetaType<QMap<int,int>>("QMap<int,int>"); |
| 45 |
qRegisterMetaType<QHash<int,int>>("QHash<int,int>"); |
| 46 |
qRegisterMetaType<QMap<QString,QHash<int,int> >>("QMap<QString,QHash<int,int> >"); |
| 47 |
|
| 48 |
d->albumWatch = new AlbumWatch(this); |
| 49 |
|
| 50 |
// these operations are pretty fast, no need for long queuing |
| 51 |
|
| 52 |
d->scanPAlbumsTimer = new QTimer(this); |
| 53 |
d->scanPAlbumsTimer->setInterval(150); |
| 54 |
d->scanPAlbumsTimer->setSingleShot(true); |
| 55 |
|
| 56 |
connect(d->scanPAlbumsTimer, SIGNAL(timeout()), |
| 57 |
this, SLOT(scanPAlbums())); |
| 58 |
|
| 59 |
d->scanTAlbumsTimer = new QTimer(this); |
| 60 |
d->scanTAlbumsTimer->setInterval(150); |
| 61 |
d->scanTAlbumsTimer->setSingleShot(true); |
| 62 |
|
| 63 |
connect(d->scanTAlbumsTimer, SIGNAL(timeout()), |
| 64 |
this, SLOT(scanTAlbums())); |
| 65 |
|
| 66 |
d->scanSAlbumsTimer = new QTimer(this); |
| 67 |
d->scanSAlbumsTimer->setInterval(150); |
| 68 |
d->scanSAlbumsTimer->setSingleShot(true); |
| 69 |
|
| 70 |
connect(d->scanSAlbumsTimer, SIGNAL(timeout()), |
| 71 |
this, SLOT(scanSAlbums())); |
| 72 |
|
| 73 |
d->updatePAlbumsTimer = new QTimer(this); |
| 74 |
d->updatePAlbumsTimer->setInterval(150); |
| 75 |
d->updatePAlbumsTimer->setSingleShot(true); |
| 76 |
|
| 77 |
connect(d->updatePAlbumsTimer, SIGNAL(timeout()), |
| 78 |
this, SLOT(updateChangedPAlbums())); |
| 79 |
|
| 80 |
// this operation is much more expensive than the other scan methods |
| 81 |
|
| 82 |
d->scanDAlbumsTimer = new QTimer(this); |
| 83 |
d->scanDAlbumsTimer->setInterval(30 * 1000); |
| 84 |
d->scanDAlbumsTimer->setSingleShot(true); |
| 85 |
|
| 86 |
connect(d->scanDAlbumsTimer, SIGNAL(timeout()), |
| 87 |
this, SLOT(scanDAlbumsScheduled())); |
| 88 |
|
| 89 |
// moderately expensive |
| 90 |
|
| 91 |
d->albumItemCountTimer = new QTimer(this); |
| 92 |
d->albumItemCountTimer->setInterval(1000); |
| 93 |
d->albumItemCountTimer->setSingleShot(true); |
| 94 |
|
| 95 |
connect(d->albumItemCountTimer, SIGNAL(timeout()), |
| 96 |
this, SLOT(getAlbumItemsCount())); |
| 97 |
|
| 98 |
// more expensive |
| 99 |
|
| 100 |
d->tagItemCountTimer = new QTimer(this); |
| 101 |
d->tagItemCountTimer->setInterval(2500); |
| 102 |
d->tagItemCountTimer->setSingleShot(true); |
| 103 |
|
| 104 |
connect(d->tagItemCountTimer, SIGNAL(timeout()), |
| 105 |
this, SLOT(getTagItemsCount())); |
| 106 |
} |
| 107 |
|
| 108 |
AlbumManager::~AlbumManager() |
| 109 |
{ |
| 110 |
delete d->rootPAlbum; |
| 111 |
delete d->rootTAlbum; |
| 112 |
delete d->rootDAlbum; |
| 113 |
delete d->rootSAlbum; |
| 114 |
|
| 115 |
delete d; |
| 116 |
} |
| 117 |
|
| 118 |
void AlbumManager::cleanUp() |
| 119 |
{ |
| 120 |
// This is what we prefer to do before Application destruction |
| 121 |
|
| 122 |
if (d->dateListJob) |
| 123 |
{ |
| 124 |
disconnect(d->dateListJob, nullptr, |
| 125 |
this, nullptr); |
| 126 |
|
| 127 |
d->dateListJob->cancel(); |
| 128 |
d->dateListJob = nullptr; |
| 129 |
} |
| 130 |
|
| 131 |
if (d->albumListJob) |
| 132 |
{ |
| 133 |
disconnect(d->albumListJob, nullptr, |
| 134 |
this, nullptr); |
| 135 |
|
| 136 |
d->albumListJob->cancel(); |
| 137 |
d->albumListJob = nullptr; |
| 138 |
} |
| 139 |
|
| 140 |
if (d->tagListJob) |
| 141 |
{ |
| 142 |
disconnect(d->tagListJob, nullptr, |
| 143 |
this, nullptr); |
| 144 |
|
| 145 |
d->tagListJob->cancel(); |
| 146 |
d->tagListJob = nullptr; |
| 147 |
} |
| 148 |
|
| 149 |
if (d->personListJob) |
| 150 |
{ |
| 151 |
disconnect(d->personListJob, nullptr, |
| 152 |
this, nullptr); |
| 153 |
|
| 154 |
d->personListJob->cancel(); |
| 155 |
d->personListJob = nullptr; |
| 156 |
} |
| 157 |
|
| 158 |
d->scanPAlbumsTimer->stop(); |
| 159 |
d->scanTAlbumsTimer->stop(); |
| 160 |
d->scanSAlbumsTimer->stop(); |
| 161 |
d->scanDAlbumsTimer->stop(); |
| 162 |
d->tagItemCountTimer->stop(); |
| 163 |
d->updatePAlbumsTimer->stop(); |
| 164 |
d->albumItemCountTimer->stop(); |
| 165 |
} |
| 166 |
|
| 167 |
void AlbumManager::startScan() |
| 168 |
{ |
| 169 |
if (!d->changed) |
| 170 |
{ |
| 171 |
return; |
| 172 |
} |
| 173 |
|
| 174 |
d->changed = false; |
| 175 |
|
| 176 |
// create root albums |
| 177 |
|
| 178 |
d->rootPAlbum = new PAlbum(i18n("Albums")); |
| 179 |
insertPAlbum(d->rootPAlbum, nullptr); |
| 180 |
|
| 181 |
d->rootTAlbum = new TAlbum(i18n("Tags"), 0, true); |
| 182 |
insertTAlbum(d->rootTAlbum, nullptr); |
| 183 |
|
| 184 |
d->rootSAlbum = new SAlbum(i18n("Searches"), 0, true); |
| 185 |
|
| 186 |
Q_EMIT signalAlbumAboutToBeAdded(d->rootSAlbum, nullptr, nullptr); |
| 187 |
|
| 188 |
d->allAlbumsIdHash[d->rootSAlbum->globalID()] = d->rootSAlbum; |
| 189 |
|
| 190 |
Q_EMIT signalAlbumAdded(d->rootSAlbum); |
| 191 |
|
| 192 |
d->rootDAlbum = new DAlbum(QDate(), true); |
| 193 |
|
| 194 |
Q_EMIT signalAlbumAboutToBeAdded(d->rootDAlbum, nullptr, nullptr); |
| 195 |
|
| 196 |
d->allAlbumsIdHash[d->rootDAlbum->globalID()] = d->rootDAlbum; |
| 197 |
|
| 198 |
Q_EMIT signalAlbumAdded(d->rootDAlbum); |
| 199 |
|
| 200 |
// Create albums for album roots. Reuse logic implemented in the method |
| 201 |
|
| 202 |
const auto locs = CollectionManager::instance()->allLocations(); |
| 203 |
|
| 204 |
for (const CollectionLocation& location : locs) |
| 205 |
{ |
| 206 |
handleCollectionStatusChange(location, CollectionLocation::LocationNull); |
| 207 |
} |
| 208 |
|
| 209 |
// listen to location status changes |
| 210 |
|
| 211 |
connect(CollectionManager::instance(), SIGNAL(locationStatusChanged(CollectionLocation,int)), |
| 212 |
this, SLOT(slotCollectionLocationStatusChanged(CollectionLocation,int))); |
| 213 |
|
| 214 |
connect(CollectionManager::instance(), SIGNAL(locationPropertiesChanged(CollectionLocation)), |
| 215 |
this, SLOT(slotCollectionLocationPropertiesChanged(CollectionLocation))); |
| 216 |
|
| 217 |
// reload albums |
| 218 |
|
| 219 |
refresh(); |
| 220 |
|
| 221 |
// listen to album database changes |
| 222 |
|
| 223 |
connect(CoreDbAccess::databaseWatch(), SIGNAL(albumChange(AlbumChangeset)), |
| 224 |
this, SLOT(slotAlbumChange(AlbumChangeset))); |
| 225 |
|
| 226 |
connect(CoreDbAccess::databaseWatch(), SIGNAL(tagChange(TagChangeset)), |
| 227 |
this, SLOT(slotTagChange(TagChangeset))); |
| 228 |
|
| 229 |
connect(CoreDbAccess::databaseWatch(), SIGNAL(searchChange(SearchChangeset)), |
| 230 |
this, SLOT(slotSearchChange(SearchChangeset))); |
| 231 |
|
| 232 |
// listen to collection image changes |
| 233 |
|
| 234 |
connect(CoreDbAccess::databaseWatch(), SIGNAL(collectionImageChange(CollectionImageChangeset)), |
| 235 |
this, SLOT(slotCollectionImageChange(CollectionImageChangeset))); |
| 236 |
|
| 237 |
connect(CoreDbAccess::databaseWatch(), SIGNAL(imageTagChange(ImageTagChangeset)), |
| 238 |
this, SLOT(slotImageTagChange(ImageTagChangeset))); |
| 239 |
|
| 240 |
// listen to image attribute changes |
| 241 |
|
| 242 |
connect(ItemAttributesWatch::instance(), SIGNAL(signalImageDateChanged(qlonglong)), |
| 243 |
d->scanDAlbumsTimer, SLOT(start())); |
| 244 |
|
| 245 |
Q_EMIT signalAllAlbumsLoaded(); |
| 246 |
} |
| 247 |
|
| 248 |
bool AlbumManager::isShowingOnlyAvailableAlbums() const |
| 249 |
{ |
| 250 |
return d->showOnlyAvailableAlbums; |
| 251 |
} |
| 252 |
|
| 253 |
void AlbumManager::setShowOnlyAvailableAlbums(bool onlyAvailable) |
| 254 |
{ |
| 255 |
if (d->showOnlyAvailableAlbums == onlyAvailable) |
| 256 |
{ |
| 257 |
return; |
| 258 |
} |
| 259 |
|
| 260 |
d->showOnlyAvailableAlbums = onlyAvailable; |
| 261 |
|
| 262 |
Q_EMIT signalShowOnlyAvailableAlbumsChanged(d->showOnlyAvailableAlbums); |
| 263 |
|
| 264 |
// We need to update the unavailable locations. |
| 265 |
// We assume the handleCollectionStatusChange does the right thing (even though old status == current status) |
| 266 |
|
| 267 |
const auto locs = CollectionManager::instance()->allLocations(); |
| 268 |
|
| 269 |
for (const CollectionLocation& location : locs) |
| 270 |
{ |
| 271 |
if (location.status() == CollectionLocation::LocationUnavailable) |
| 272 |
{ |
| 273 |
handleCollectionStatusChange(location, CollectionLocation::LocationUnavailable); |
| 274 |
} |
| 275 |
} |
| 276 |
} |
| 277 |
|
| 278 |
void AlbumManager::refresh() |
| 279 |
{ |
| 280 |
scanPAlbums(); |
| 281 |
scanTAlbums(); |
| 282 |
scanSAlbums(); |
| 283 |
scanDAlbums(); |
| 284 |
} |
| 285 |
|
| 286 |
void AlbumManager::prepareItemCounts() |
| 287 |
{ |
| 288 |
// There is no way to find out if any data we had collected |
| 289 |
// previously is still valid - recompute |
| 290 |
|
| 291 |
scanDAlbums(); |
| 292 |
getAlbumItemsCount(); |
| 293 |
getTagItemsCount(); |
| 294 |
} |
| 295 |
|
| 296 |
void AlbumManager::slotImagesDeleted(const QList<qlonglong>& imageIds) |
| 297 |
{ |
| 298 |
qCDebug(DIGIKAM_GENERAL_LOG) << "Got image deletion notification from ItemViewUtilities for " << imageIds.size() << " images."; |
| 299 |
|
| 300 |
QSet<SAlbum*> sAlbumsToUpdate; |
| 301 |
QSet<qlonglong> deletedImages(imageIds.begin(), imageIds.end()); |
| 302 |
|
| 303 |
QList<SAlbum*> sAlbums = findSAlbumsBySearchType(DatabaseSearch::DuplicatesSearch); |
| 304 |
|
| 305 |
for (SAlbum* const sAlbum : std::as_const(sAlbums)) |
| 306 |
{ |
| 307 |
// Read the search query XML and save the image ids |
| 308 |
|
| 309 |
SearchXmlReader reader(sAlbum->query()); |
| 310 |
SearchXml::Element element; |
| 311 |
QSet<qlonglong> images; |
| 312 |
|
| 313 |
while ((element = reader.readNext()) != SearchXml::End) |
| 314 |
{ |
| 315 |
if ((element == SearchXml::Field) && (reader.fieldName().compare(QLatin1String("imageid")) == 0)) |
| 316 |
{ |
| 317 |
const auto list = reader.valueToLongLongList(); |
| 318 |
images = QSet<qlonglong>(list.begin(), list.end()); |
| 319 |
} |
| 320 |
} |
| 321 |
|
| 322 |
// If the deleted images are part of the SAlbum, |
| 323 |
// mark the album as ready for deletion and the images as ready for rescan. |
| 324 |
|
| 325 |
if (images.intersects(deletedImages)) |
| 326 |
{ |
| 327 |
sAlbumsToUpdate.insert(sAlbum); |
| 328 |
} |
| 329 |
} |
| 330 |
|
| 331 |
if (!sAlbumsToUpdate.isEmpty()) |
| 332 |
{ |
| 333 |
Q_EMIT signalUpdateDuplicatesAlbums(sAlbumsToUpdate.values(), deletedImages.values()); |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
} // namespace Digikam |
| 338 |
|
| 339 |
#include "moc_albummanager.cpp" |