| 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 - Tag Album helpers. |
| 8 |
* |
| 9 |
* SPDX-FileCopyrightText: 2006-2026 by Gilles Caulier <caulier dot gilles at gmail dot com> |
| 10 |
* SPDX-FileCopyrightText: 2006-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de> |
| 11 |
* SPDX-FileCopyrightText: 2015 by Mohamed_Anwer <m_dot_anwer at gmx dot com> |
| 12 |
* |
| 13 |
* SPDX-License-Identifier: GPL-2.0-or-later |
| 14 |
* |
| 15 |
* ============================================================ */ |
| 16 |
|
| 17 |
#include "albummanager_p.h" |
| 18 |
|
| 19 |
namespace Digikam |
| 20 |
{ |
| 21 |
|
| 22 |
void AlbumManager::scanTAlbums() |
| 23 |
{ |
| 24 |
d->scanTAlbumsTimer->stop(); |
| 25 |
|
| 26 |
// list TAlbums directly from the db |
| 27 |
// first insert all the current TAlbums into a map for quick lookup |
| 28 |
|
| 29 |
typedef QMap<int, TAlbum*> TagMap; |
| 30 |
TagMap tmap; |
| 31 |
|
| 32 |
tmap.insert(0, d->rootTAlbum); |
| 33 |
|
| 34 |
AlbumIterator it(d->rootTAlbum); |
| 35 |
|
| 36 |
while (it.current()) |
| 37 |
{ |
| 38 |
TAlbum* const t = (TAlbum*)(*it); // cppcheck-suppress constVariablePointer |
| 39 |
tmap.insert(t->id(), t); |
| 40 |
++it; |
| 41 |
} |
| 42 |
|
| 43 |
// Retrieve the list of tags from the database |
| 44 |
|
| 45 |
TagInfo::List tList = CoreDbAccess().db()->scanTags(); |
| 46 |
|
| 47 |
// sort the list. needed because we want the tags can be read in any order, |
| 48 |
// but we want to make sure that we are ensure to find the parent TAlbum |
| 49 |
// for a new TAlbum |
| 50 |
|
| 51 |
{ |
| 52 |
QHash<int, TAlbum*> tagHash; |
| 53 |
|
| 54 |
// insert items into a dict for quick lookup |
| 55 |
|
| 56 |
for (TagInfo::List::const_iterator iter = tList.constBegin() ; iter != tList.constEnd() ; ++iter) |
| 57 |
{ |
| 58 |
TagInfo info = *iter; |
| 59 |
TAlbum* const album = new TAlbum(info.name, info.id); |
| 60 |
album->m_icon = info.icon; |
| 61 |
album->m_iconId = info.iconId; |
| 62 |
album->m_pid = info.pid; |
| 63 |
tagHash.insert(info.id, album); |
| 64 |
} |
| 65 |
|
| 66 |
tList.clear(); |
| 67 |
|
| 68 |
// also add root tag |
| 69 |
|
| 70 |
TAlbum* const rootTag = new TAlbum(QLatin1String("root"), 0, true); |
| 71 |
tagHash.insert(0, rootTag); |
| 72 |
|
| 73 |
// build tree |
| 74 |
|
| 75 |
for (QHash<int, TAlbum*>::const_iterator iter = tagHash.constBegin() ; iter != tagHash.constEnd() ; ++iter) |
| 76 |
{ |
| 77 |
TAlbum* album = *iter; |
| 78 |
|
| 79 |
if (album->m_id == 0) |
| 80 |
{ |
| 81 |
continue; |
| 82 |
} |
| 83 |
|
| 84 |
TAlbum* const parent = tagHash.value(album->m_pid); |
| 85 |
|
| 86 |
if (parent) |
| 87 |
{ |
| 88 |
album->setParent(parent); |
| 89 |
} |
| 90 |
else |
| 91 |
{ |
| 92 |
qCWarning(DIGIKAM_GENERAL_LOG) << "Failed to find parent tag for tag " |
| 93 |
<< album->m_title |
| 94 |
<< " with pid " |
| 95 |
<< album->m_pid; |
| 96 |
} |
| 97 |
} |
| 98 |
|
| 99 |
tagHash.clear(); |
| 100 |
|
| 101 |
// now insert the items into the list. becomes sorted |
| 102 |
|
| 103 |
AlbumIterator it2(rootTag); |
| 104 |
|
| 105 |
while (it2.current()) |
| 106 |
{ |
| 107 |
const TAlbum* const album = dynamic_cast<TAlbum*>(it2.current()); |
| 108 |
|
| 109 |
if (album) |
| 110 |
{ |
| 111 |
TagInfo info; |
| 112 |
info.id = album->m_id; |
| 113 |
info.pid = album->m_pid; |
| 114 |
info.name = album->m_title; |
| 115 |
info.icon = album->m_icon; |
| 116 |
info.iconId = album->m_iconId; |
| 117 |
tList.append(info); |
| 118 |
} |
| 119 |
|
| 120 |
++it2; |
| 121 |
} |
| 122 |
|
| 123 |
// this will also delete all child albums |
| 124 |
|
| 125 |
delete rootTag; |
| 126 |
} |
| 127 |
|
| 128 |
for (TagInfo::List::const_iterator it3 = tList.constBegin() ; it3 != tList.constEnd() ; ++it3) |
| 129 |
{ |
| 130 |
TagInfo info = *it3; |
| 131 |
|
| 132 |
// check if we have already added this tag |
| 133 |
|
| 134 |
if (tmap.contains(info.id)) |
| 135 |
{ |
| 136 |
continue; |
| 137 |
} |
| 138 |
|
| 139 |
// Its a new album. Find the parent of the album |
| 140 |
|
| 141 |
TagMap::const_iterator iter = tmap.constFind(info.pid); |
| 142 |
|
| 143 |
if (iter == tmap.constEnd()) |
| 144 |
{ |
| 145 |
qCWarning(DIGIKAM_GENERAL_LOG) << "Failed to find parent tag for tag " |
| 146 |
<< info.name |
| 147 |
<< " with pid " |
| 148 |
<< info.pid; |
| 149 |
continue; |
| 150 |
} |
| 151 |
|
| 152 |
TAlbum* const parent = iter.value(); |
| 153 |
|
| 154 |
// Create the new TAlbum |
| 155 |
|
| 156 |
TAlbum* const album = new TAlbum(info.name, info.id, false); |
| 157 |
album->m_icon = info.icon; |
| 158 |
album->m_iconId = info.iconId; |
| 159 |
insertTAlbum(album, parent); |
| 160 |
|
| 161 |
// also insert it in the map we are doing lookup of parent tags |
| 162 |
|
| 163 |
tmap.insert(info.id, album); |
| 164 |
} |
| 165 |
|
| 166 |
if (!tList.isEmpty()) |
| 167 |
{ |
| 168 |
Q_EMIT signalAlbumsUpdated(Album::TAG); |
| 169 |
} |
| 170 |
|
| 171 |
getTagItemsCount(); |
| 172 |
} |
| 173 |
|
| 174 |
void AlbumManager::getTagItemsCount() |
| 175 |
{ |
| 176 |
d->tagItemCountTimer->stop(); |
| 177 |
|
| 178 |
if (!ApplicationSettings::instance()->getShowFolderTreeViewItemsCount()) |
| 179 |
{ |
| 180 |
personItemsCount(); |
| 181 |
return; |
| 182 |
} |
| 183 |
|
| 184 |
tagItemsCount(); |
| 185 |
personItemsCount(); |
| 186 |
} |
| 187 |
|
| 188 |
void AlbumManager::tagItemsCount() |
| 189 |
{ |
| 190 |
if (d->tagListJob) |
| 191 |
{ |
| 192 |
disconnect(d->tagListJob, nullptr, this, nullptr); |
| 193 |
|
| 194 |
d->tagListJob->cancel(); |
| 195 |
d->tagListJob = nullptr; |
| 196 |
} |
| 197 |
|
| 198 |
TagsDBJobInfo jInfo; |
| 199 |
jInfo.setFoldersJob(); |
| 200 |
|
| 201 |
d->tagListJob = DBJobsManager::instance()->startTagsJobThread(jInfo); |
| 202 |
|
| 203 |
connect(d->tagListJob, SIGNAL(signalFinished()), |
| 204 |
this, SLOT(slotTagsJobResult())); |
| 205 |
|
| 206 |
connect(d->tagListJob, SIGNAL(foldersData(QHash<int,int>)), |
| 207 |
this, SLOT(slotTagsJobData(QHash<int,int>))); |
| 208 |
} |
| 209 |
|
| 210 |
AlbumList AlbumManager::allTAlbums() const |
| 211 |
{ |
| 212 |
AlbumList list; |
| 213 |
|
| 214 |
if (d->rootTAlbum) |
| 215 |
{ |
| 216 |
list.append(d->rootTAlbum); |
| 217 |
} |
| 218 |
|
| 219 |
AlbumIterator it(d->rootTAlbum); |
| 220 |
|
| 221 |
while (it.current()) |
| 222 |
{ |
| 223 |
list.append(*it); |
| 224 |
++it; |
| 225 |
} |
| 226 |
|
| 227 |
return list; |
| 228 |
} |
| 229 |
|
| 230 |
/** |
| 231 |
* This method is not yet used |
| 232 |
*/ |
| 233 |
QList<TAlbum*> AlbumManager::currentTAlbums() const |
| 234 |
{ |
| 235 |
QList<TAlbum*> talbums; |
| 236 |
QList<Album*>::iterator it; |
| 237 |
|
| 238 |
for (it = d->currentAlbums.begin() ; it != d->currentAlbums.end() ; ++it) |
| 239 |
{ |
| 240 |
TAlbum* const temp = dynamic_cast<TAlbum*>(*it); |
| 241 |
|
| 242 |
if (temp) |
| 243 |
{ |
| 244 |
talbums.append(temp); |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
return talbums; |
| 249 |
} |
| 250 |
|
| 251 |
TAlbum* AlbumManager::findTAlbum(int id) const |
| 252 |
{ |
| 253 |
if (!d->rootTAlbum) |
| 254 |
{ |
| 255 |
return nullptr; |
| 256 |
} |
| 257 |
|
| 258 |
int gid = d->rootTAlbum->globalID() + id; |
| 259 |
|
| 260 |
return static_cast<TAlbum*>((d->allAlbumsIdHash.value(gid))); |
| 261 |
} |
| 262 |
|
| 263 |
TAlbum* AlbumManager::findTAlbum(const QString& tagPath) const |
| 264 |
{ |
| 265 |
// handle gracefully with or without leading slash |
| 266 |
|
| 267 |
bool withLeadingSlash = tagPath.startsWith(QLatin1Char('/')); |
| 268 |
AlbumIterator it(d->rootTAlbum); |
| 269 |
|
| 270 |
while (it.current()) |
| 271 |
{ |
| 272 |
TAlbum* const talbum = static_cast<TAlbum*>(*it); |
| 273 |
|
| 274 |
if (talbum->tagPath(withLeadingSlash) == tagPath) |
| 275 |
{ |
| 276 |
return talbum; |
| 277 |
} |
| 278 |
|
| 279 |
++it; |
| 280 |
} |
| 281 |
|
| 282 |
return nullptr; |
| 283 |
} |
| 284 |
|
| 285 |
TAlbum* AlbumManager::createTAlbum(TAlbum* parent, const QString& name, |
| 286 |
const QString& iconkde, QString& errMsg) |
| 287 |
{ |
| 288 |
if (!parent) |
| 289 |
{ |
| 290 |
errMsg = i18n("No parent found for tag"); |
| 291 |
return nullptr; |
| 292 |
} |
| 293 |
|
| 294 |
// sanity checks |
| 295 |
|
| 296 |
if (name.isEmpty()) |
| 297 |
{ |
| 298 |
errMsg = i18n("Tag name cannot be empty"); |
| 299 |
return nullptr; |
| 300 |
} |
| 301 |
|
| 302 |
if (name.contains(QLatin1Char('/'))) |
| 303 |
{ |
| 304 |
errMsg = i18n("Tag name cannot contain '/'"); |
| 305 |
return nullptr; |
| 306 |
} |
| 307 |
|
| 308 |
// first check if we have another album with the same name |
| 309 |
|
| 310 |
if (hasDirectChildAlbumWithTitle(parent, name)) |
| 311 |
{ |
| 312 |
errMsg = i18n("Tag name already exists"); |
| 313 |
return nullptr; |
| 314 |
} |
| 315 |
|
| 316 |
ChangingDB changing(d); |
| 317 |
int id = CoreDbAccess().db()->addTag(parent->id(), name, iconkde, 0); |
| 318 |
|
| 319 |
if (id == -1) |
| 320 |
{ |
| 321 |
errMsg = i18n("Failed to add tag to database"); |
| 322 |
return nullptr; |
| 323 |
} |
| 324 |
|
| 325 |
TAlbum* const album = new TAlbum(name, id, false); // cppcheck-suppress constVariablePointer |
| 326 |
album->m_icon = iconkde; |
| 327 |
|
| 328 |
insertTAlbum(album, parent); |
| 329 |
|
| 330 |
const TAlbum* const personParentTag = findTAlbum(FaceTags::personParentTag()); |
| 331 |
|
| 332 |
if (personParentTag && personParentTag->isAncestorOf(album)) |
| 333 |
{ |
| 334 |
FaceTags::ensureIsPerson(album->id()); |
| 335 |
} |
| 336 |
|
| 337 |
Q_EMIT signalAlbumsUpdated(Album::TAG); |
| 338 |
|
| 339 |
return album; |
| 340 |
} |
| 341 |
|
| 342 |
AlbumList AlbumManager::findOrCreateTAlbums(const QStringList& _tagPaths) |
| 343 |
{ |
| 344 |
// find tag ids for tag paths in list, create if they don't exist |
| 345 |
|
| 346 |
QList<int> tagIDs = TagsCache::instance()->getOrCreateTags(_tagPaths); |
| 347 |
|
| 348 |
// create TAlbum objects for the newly created tags |
| 349 |
|
| 350 |
scanTAlbums(); |
| 351 |
|
| 352 |
AlbumList resultList; |
| 353 |
|
| 354 |
for (QList<int>::const_iterator it = tagIDs.constBegin() ; it != tagIDs.constEnd() ; ++it) |
| 355 |
{ |
| 356 |
resultList.append(findTAlbum(*it)); |
| 357 |
} |
| 358 |
|
| 359 |
return resultList; |
| 360 |
} |
| 361 |
|
| 362 |
bool AlbumManager::deleteTAlbum(TAlbum* album, QString& errMsg, QList<qlonglong>* imageIds) |
| 363 |
{ |
| 364 |
if (!album) |
| 365 |
{ |
| 366 |
errMsg = i18n("No such album"); |
| 367 |
return false; |
| 368 |
} |
| 369 |
|
| 370 |
if (album == d->rootTAlbum) |
| 371 |
{ |
| 372 |
errMsg = i18n("Cannot delete root tag"); |
| 373 |
return false; |
| 374 |
} |
| 375 |
|
| 376 |
if ( |
| 377 |
FaceTags::isSystemPersonTagId(album->id()) || |
| 378 |
(FaceTags::personParentTag() == album->id()) |
| 379 |
) |
| 380 |
{ |
| 381 |
errMsg = i18n("Cannot delete required face tag"); |
| 382 |
return false; |
| 383 |
} |
| 384 |
|
| 385 |
QList<int> toBeDeletedTagIds; |
| 386 |
toBeDeletedTagIds << album->id(); |
| 387 |
|
| 388 |
const Album* subAlbum = nullptr; |
| 389 |
AlbumIterator it(album); |
| 390 |
|
| 391 |
while ((subAlbum = it.current()) != nullptr) |
| 392 |
{ |
| 393 |
toBeDeletedTagIds << subAlbum->id(); |
| 394 |
++it; |
| 395 |
} |
| 396 |
|
| 397 |
QApplication::setOverrideCursor(Qt::WaitCursor); |
| 398 |
|
| 399 |
if (imageIds) |
| 400 |
{ |
| 401 |
// Recursively from all tags to delete. |
| 402 |
|
| 403 |
const auto ids = CoreDbAccess().db()->getItemIDsInTag(album->id(), true); |
| 404 |
|
| 405 |
for (const qlonglong& id : ids) |
| 406 |
{ |
| 407 |
if (!imageIds->contains(id)) |
| 408 |
{ |
| 409 |
*imageIds << id; |
| 410 |
} |
| 411 |
} |
| 412 |
} |
| 413 |
|
| 414 |
for (int tagId : std::as_const(toBeDeletedTagIds)) |
| 415 |
{ |
| 416 |
if (FaceTags::isPerson(tagId)) |
| 417 |
{ |
| 418 |
const QList<qlonglong>& imgListIds = CoreDbAccess().db()->getItemIDsInTag(tagId); |
| 419 |
|
| 420 |
for (const qlonglong& imageId : std::as_const(imgListIds)) |
| 421 |
{ |
| 422 |
const QList<FaceTagsIface>& facesList = FaceTagsEditor().databaseFaces(imageId); |
| 423 |
|
| 424 |
for (const FaceTagsIface& face : std::as_const(facesList)) |
| 425 |
{ |
| 426 |
if (face.tagId() == tagId) |
| 427 |
{ |
| 428 |
FaceTagsIface unknownFace(FaceTagsIface::UnknownName, imageId, |
| 429 |
FaceTags::unknownPersonTagId(), face.region()); |
| 430 |
|
| 431 |
FaceTagsEditor().removeFace(face); |
| 432 |
FaceTagsEditor().addManually(unknownFace); |
| 433 |
} |
| 434 |
} |
| 435 |
} |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
QApplication::restoreOverrideCursor(); |
| 440 |
|
| 441 |
{ |
| 442 |
CoreDbAccess access; |
| 443 |
ChangingDB changing(d); |
| 444 |
|
| 445 |
for (int tagId : std::as_const(toBeDeletedTagIds)) |
| 446 |
{ |
| 447 |
access.db()->deleteTag(tagId); |
| 448 |
} |
| 449 |
} |
| 450 |
|
| 451 |
removeTAlbum(album); |
| 452 |
|
| 453 |
Q_EMIT signalAlbumsUpdated(Album::TAG); |
| 454 |
|
| 455 |
return true; |
| 456 |
} |
| 457 |
|
| 458 |
bool AlbumManager::renameTAlbum(TAlbum* album, |
| 459 |
const QString& name, |
| 460 |
QString& errMsg) |
| 461 |
{ |
| 462 |
if (!album) |
| 463 |
{ |
| 464 |
errMsg = i18n("No such album"); |
| 465 |
return false; |
| 466 |
} |
| 467 |
|
| 468 |
if (album == d->rootTAlbum) |
| 469 |
{ |
| 470 |
errMsg = i18n("Cannot edit root tag"); |
| 471 |
return false; |
| 472 |
} |
| 473 |
|
| 474 |
if (name.contains(QLatin1Char('/'))) |
| 475 |
{ |
| 476 |
errMsg = i18n("Tag name cannot contain '/'"); |
| 477 |
return false; |
| 478 |
} |
| 479 |
|
| 480 |
// first check if we have another sibling with the same name |
| 481 |
|
| 482 |
if (hasDirectChildAlbumWithTitle(album->m_parent, name)) |
| 483 |
{ |
| 484 |
errMsg = i18n("Another tag with the same name already exists.\n" |
| 485 |
"Please choose another name."); |
| 486 |
return false; |
| 487 |
} |
| 488 |
|
| 489 |
ChangingDB changing(d); |
| 490 |
CoreDbAccess().db()->setTagName(album->id(), name); |
| 491 |
album->setTitle(name); |
| 492 |
|
| 493 |
Q_EMIT signalAlbumRenamed(album); |
| 494 |
|
| 495 |
askUserForWriteChangedTAlbumToFiles(album); |
| 496 |
|
| 497 |
return true; |
| 498 |
} |
| 499 |
|
| 500 |
bool AlbumManager::moveTAlbum(TAlbum* album, TAlbum* newParent, QString& errMsg) |
| 501 |
{ |
| 502 |
if (!album) |
| 503 |
{ |
| 504 |
errMsg = i18n("No such album"); |
| 505 |
return false; |
| 506 |
} |
| 507 |
|
| 508 |
if (!newParent) |
| 509 |
{ |
| 510 |
errMsg = i18n("Attempt to move TAlbum to nowhere"); |
| 511 |
return false; |
| 512 |
} |
| 513 |
|
| 514 |
if (album == d->rootTAlbum) |
| 515 |
{ |
| 516 |
errMsg = i18n("Cannot move root tag"); |
| 517 |
return false; |
| 518 |
} |
| 519 |
|
| 520 |
if (hasDirectChildAlbumWithTitle(newParent, album->title())) |
| 521 |
{ |
| 522 |
QPointer<QMessageBox> msgBox = new QMessageBox(QMessageBox::Warning, |
| 523 |
qApp->applicationName(), |
| 524 |
i18n("Another tag with the same name already exists.\n" |
| 525 |
"Do you want to merge the tags?"), |
| 526 |
QMessageBox::Yes | QMessageBox::No, |
| 527 |
qApp->activeWindow()); |
| 528 |
|
| 529 |
int result = msgBox->exec(); |
| 530 |
delete msgBox; |
| 531 |
|
| 532 |
if (result == QMessageBox::Yes) |
| 533 |
{ |
| 534 |
QString destPath(newParent->tagPath()); |
| 535 |
|
| 536 |
if (destPath != QLatin1String("/")) |
| 537 |
{ |
| 538 |
destPath += QLatin1Char('/'); |
| 539 |
} |
| 540 |
|
| 541 |
TAlbum* const destAlbum = findTAlbum(destPath + |
| 542 |
album->title()); |
| 543 |
|
| 544 |
return mergeTAlbum(album, destAlbum, false, errMsg); |
| 545 |
} |
| 546 |
else |
| 547 |
{ |
| 548 |
return true; |
| 549 |
} |
| 550 |
} |
| 551 |
|
| 552 |
d->currentlyMovingAlbum = album; |
| 553 |
|
| 554 |
Q_EMIT signalAlbumAboutToBeMoved(album); |
| 555 |
Q_EMIT signalAlbumAboutToBeDeleted(album); |
| 556 |
|
| 557 |
if (album->parent()) |
| 558 |
{ |
| 559 |
album->parent()->removeChild(album); |
| 560 |
} |
| 561 |
|
| 562 |
album->setParent(nullptr); |
| 563 |
|
| 564 |
Q_EMIT signalAlbumDeleted(album); |
| 565 |
Q_EMIT signalAlbumHasBeenDeleted(album); |
| 566 |
Q_EMIT signalAlbumAboutToBeAdded(album, newParent, newParent->lastChild()); |
| 567 |
|
| 568 |
ChangingDB changing(d); |
| 569 |
CoreDbAccess().db()->setTagParentID(album->id(), newParent->id()); |
| 570 |
album->setParent(newParent); |
| 571 |
|
| 572 |
Q_EMIT signalAlbumAdded(album); |
| 573 |
Q_EMIT signalAlbumMoved(album); |
| 574 |
Q_EMIT signalAlbumsUpdated(Album::TAG); |
| 575 |
|
| 576 |
d->currentlyMovingAlbum = nullptr; |
| 577 |
const TAlbum* const personParentTag = findTAlbum(FaceTags::personParentTag()); |
| 578 |
|
| 579 |
if (personParentTag && personParentTag->isAncestorOf(album)) |
| 580 |
{ |
| 581 |
FaceTags::ensureIsPerson(album->id()); |
| 582 |
} |
| 583 |
|
| 584 |
askUserForWriteChangedTAlbumToFiles(album); |
| 585 |
|
| 586 |
return true; |
| 587 |
} |
| 588 |
|
| 589 |
bool AlbumManager::mergeTAlbum(TAlbum* album, TAlbum* destAlbum, bool dialog, QString& errMsg) |
| 590 |
{ |
| 591 |
if (!album || !destAlbum) |
| 592 |
{ |
| 593 |
errMsg = i18n("No such album"); |
| 594 |
return false; |
| 595 |
} |
| 596 |
|
| 597 |
if ((album == d->rootTAlbum) || (destAlbum == d->rootTAlbum)) |
| 598 |
{ |
| 599 |
errMsg = i18n("Cannot merge root tag"); |
| 600 |
return false; |
| 601 |
} |
| 602 |
|
| 603 |
if (FaceTags::isSystemPersonTagId(album->id())) |
| 604 |
{ |
| 605 |
errMsg = i18n("Cannot merge required face tag"); |
| 606 |
return false; |
| 607 |
} |
| 608 |
|
| 609 |
if (album->firstChild()) |
| 610 |
{ |
| 611 |
errMsg = i18n("Only a tag without children can be merged!"); |
| 612 |
return false; |
| 613 |
} |
| 614 |
|
| 615 |
if (dialog) |
| 616 |
{ |
| 617 |
int result = d->askMergeMessageBoxResult; |
| 618 |
|
| 619 |
if (result == -1) |
| 620 |
{ |
| 621 |
QPointer<QMessageBox> msgBox = new QMessageBox(QMessageBox::Warning, |
| 622 |
qApp->applicationName(), |
| 623 |
i18n("Do you want to merge tag '%1' into tag '%2'?", |
| 624 |
album->title(), destAlbum->title()), |
| 625 |
QMessageBox::Yes | QMessageBox::No, |
| 626 |
qApp->activeWindow()); |
| 627 |
QCheckBox* const chkBox = new QCheckBox(i18n("Do not ask again at this session"), msgBox); |
| 628 |
msgBox->setCheckBox(chkBox); |
| 629 |
|
| 630 |
result = msgBox->exec(); |
| 631 |
|
| 632 |
if (chkBox->isChecked()) |
| 633 |
{ |
| 634 |
d->askMergeMessageBoxResult = result; |
| 635 |
} |
| 636 |
|
| 637 |
delete msgBox; |
| 638 |
} |
| 639 |
|
| 640 |
if (result == QMessageBox::No) |
| 641 |
{ |
| 642 |
return true; |
| 643 |
} |
| 644 |
} |
| 645 |
|
| 646 |
int oldId = album->id(); |
| 647 |
int mergeId = destAlbum->id(); |
| 648 |
|
| 649 |
if (oldId == mergeId) |
| 650 |
{ |
| 651 |
return true; |
| 652 |
} |
| 653 |
|
| 654 |
QApplication::setOverrideCursor(Qt::WaitCursor); |
| 655 |
QList<qlonglong> imageIds = CoreDbAccess().db()->getItemIDsInTag(oldId); |
| 656 |
|
| 657 |
{ |
| 658 |
CoreDbOperationGroup group; |
| 659 |
group.setMaximumTime(200); |
| 660 |
|
| 661 |
for (const qlonglong& imageId : std::as_const(imageIds)) |
| 662 |
{ |
| 663 |
QList<FaceTagsIface> facesList = FaceTagsEditor().databaseFaces(imageId); |
| 664 |
bool foundFace = false; |
| 665 |
|
| 666 |
for (const FaceTagsIface& face : std::as_const(facesList)) |
| 667 |
{ |
| 668 |
if (face.tagId() == oldId) |
| 669 |
{ |
| 670 |
foundFace = true; |
| 671 |
FaceTagsEditor().removeFace(face); |
| 672 |
FaceTagsEditor().add(imageId, mergeId, face.region(), false); |
| 673 |
} |
| 674 |
} |
| 675 |
|
| 676 |
if (!foundFace) |
| 677 |
{ |
| 678 |
ItemInfo info(imageId); |
| 679 |
info.removeTag(oldId); |
| 680 |
info.setTag(mergeId); |
| 681 |
group.allowLift(); |
| 682 |
} |
| 683 |
} |
| 684 |
} |
| 685 |
|
| 686 |
QApplication::restoreOverrideCursor(); |
| 687 |
|
| 688 |
if (!deleteTAlbum(album, errMsg)) |
| 689 |
{ |
| 690 |
return false; |
| 691 |
} |
| 692 |
|
| 693 |
askUserForWriteChangedTAlbumToFiles(imageIds); |
| 694 |
|
| 695 |
return true; |
| 696 |
} |
| 697 |
|
| 698 |
bool AlbumManager::updateTAlbumIcon(TAlbum* album, const QString& iconKDE, |
| 699 |
qlonglong iconID, QString& errMsg) |
| 700 |
{ |
| 701 |
if (!album) |
| 702 |
{ |
| 703 |
errMsg = i18n("No such tag"); |
| 704 |
return false; |
| 705 |
} |
| 706 |
|
| 707 |
if (album == d->rootTAlbum) |
| 708 |
{ |
| 709 |
errMsg = i18n("Cannot edit root tag"); |
| 710 |
return false; |
| 711 |
} |
| 712 |
|
| 713 |
{ |
| 714 |
CoreDbAccess access; |
| 715 |
ChangingDB changing(d); |
| 716 |
access.db()->setTagIcon(album->id(), iconKDE, iconID); |
| 717 |
album->m_icon = iconKDE; |
| 718 |
album->m_iconId = iconID; |
| 719 |
} |
| 720 |
|
| 721 |
Q_EMIT signalAlbumIconChanged(album); |
| 722 |
|
| 723 |
return true; |
| 724 |
} |
| 725 |
|
| 726 |
AlbumList AlbumManager::getRecentlyAssignedTags(bool includeInternal) const |
| 727 |
{ |
| 728 |
QList<int> tagIDs = CoreDbAccess().db()->getRecentlyAssignedTags(); |
| 729 |
|
| 730 |
AlbumList resultList; |
| 731 |
|
| 732 |
for (QList<int>::const_iterator it = tagIDs.constBegin() ; it != tagIDs.constEnd() ; ++it) |
| 733 |
{ |
| 734 |
TAlbum* const album = findTAlbum(*it); |
| 735 |
|
| 736 |
if (album) |
| 737 |
{ |
| 738 |
if (!includeInternal && album->isInternalTag()) |
| 739 |
{ |
| 740 |
continue; |
| 741 |
} |
| 742 |
|
| 743 |
resultList.append(album); |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
return resultList; |
| 748 |
} |
| 749 |
|
| 750 |
QStringList AlbumManager::tagPaths(const QList<int>& tagIDs, |
| 751 |
bool leadingSlash, |
| 752 |
bool includeInternal) const |
| 753 |
{ |
| 754 |
QStringList tagPaths; |
| 755 |
|
| 756 |
for (QList<int>::const_iterator it = tagIDs.constBegin() ; it != tagIDs.constEnd() ; ++it) |
| 757 |
{ |
| 758 |
const TAlbum* album = findTAlbum(*it); |
| 759 |
|
| 760 |
if (album) |
| 761 |
{ |
| 762 |
if (!includeInternal && album->isInternalTag()) |
| 763 |
{ |
| 764 |
continue; |
| 765 |
} |
| 766 |
|
| 767 |
tagPaths.append(album->tagPath(leadingSlash)); |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
return tagPaths; |
| 772 |
} |
| 773 |
|
| 774 |
QStringList AlbumManager::tagNames(const QList<int>& tagIDs, bool includeInternal) const |
| 775 |
{ |
| 776 |
QStringList tagNames; |
| 777 |
|
| 778 |
for (int id : std::as_const(tagIDs)) |
| 779 |
{ |
| 780 |
const TAlbum* const album = findTAlbum(id); |
| 781 |
|
| 782 |
if (album) |
| 783 |
{ |
| 784 |
if (!includeInternal && album->isInternalTag()) |
| 785 |
{ |
| 786 |
continue; |
| 787 |
} |
| 788 |
|
| 789 |
tagNames << album->title(); |
| 790 |
} |
| 791 |
} |
| 792 |
|
| 793 |
return tagNames; |
| 794 |
} |
| 795 |
|
| 796 |
QHash<int, QString> AlbumManager::tagPaths(bool leadingSlash, bool includeInternal) const |
| 797 |
{ |
| 798 |
QHash<int, QString> hash; |
| 799 |
AlbumIterator it(d->rootTAlbum); |
| 800 |
|
| 801 |
while (it.current()) |
| 802 |
{ |
| 803 |
const TAlbum* const t = (TAlbum*)(*it); |
| 804 |
|
| 805 |
if (includeInternal || !t->isInternalTag()) |
| 806 |
{ |
| 807 |
hash.insert(t->id(), t->tagPath(leadingSlash)); |
| 808 |
} |
| 809 |
|
| 810 |
++it; |
| 811 |
} |
| 812 |
|
| 813 |
return hash; |
| 814 |
} |
| 815 |
|
| 816 |
QHash<int, QString> AlbumManager::tagNames(bool includeInternal) const |
| 817 |
{ |
| 818 |
QHash<int, QString> hash; |
| 819 |
AlbumIterator it(d->rootTAlbum); |
| 820 |
|
| 821 |
while (it.current()) |
| 822 |
{ |
| 823 |
const TAlbum* const t = (TAlbum*)(*it); |
| 824 |
|
| 825 |
if (includeInternal || !t->isInternalTag()) |
| 826 |
{ |
| 827 |
hash.insert(t->id(), t->title()); |
| 828 |
} |
| 829 |
|
| 830 |
++it; |
| 831 |
} |
| 832 |
|
| 833 |
return hash; |
| 834 |
} |
| 835 |
|
| 836 |
QList<int> AlbumManager::subTags(int tagId, bool recursive) const |
| 837 |
{ |
| 838 |
TAlbum* const album = this->findTAlbum(tagId); |
| 839 |
|
| 840 |
return album->childAlbumIds(recursive); |
| 841 |
} |
| 842 |
|
| 843 |
int AlbumManager::findTopId(int tagId) const |
| 844 |
{ |
| 845 |
const TAlbum* const album = findTAlbum(tagId); |
| 846 |
|
| 847 |
if (!album || album->isInternalTag()) |
| 848 |
{ |
| 849 |
return -1; |
| 850 |
} |
| 851 |
|
| 852 |
int topId = tagId; |
| 853 |
const Album* parent = album; |
| 854 |
|
| 855 |
while (!parent->isRoot()) |
| 856 |
{ |
| 857 |
topId = parent->id(); |
| 858 |
parent = parent->parent(); |
| 859 |
} |
| 860 |
|
| 861 |
return topId; |
| 862 |
} |
| 863 |
|
| 864 |
AlbumList AlbumManager::findTagsWithProperty(const QString& property) |
| 865 |
{ |
| 866 |
AlbumList list; |
| 867 |
|
| 868 |
QList<int> ids = TagsCache::instance()->tagsWithProperty(property); |
| 869 |
|
| 870 |
for (int id : std::as_const(ids)) |
| 871 |
{ |
| 872 |
TAlbum* const album = findTAlbum(id); // cppcheck-suppress constVariablePointer |
| 873 |
|
| 874 |
if (album) |
| 875 |
{ |
| 876 |
list << album; |
| 877 |
} |
| 878 |
} |
| 879 |
|
| 880 |
return list; |
| 881 |
} |
| 882 |
|
| 883 |
AlbumList AlbumManager::findTagsWithProperty(const QString& property, const QString& value) |
| 884 |
{ |
| 885 |
AlbumList list; |
| 886 |
|
| 887 |
AlbumIterator it(d->rootTAlbum); |
| 888 |
|
| 889 |
while (it.current()) |
| 890 |
{ |
| 891 |
if (static_cast<TAlbum*>(*it)->property(property) == value) |
| 892 |
{ |
| 893 |
list << *it; |
| 894 |
} |
| 895 |
|
| 896 |
++it; |
| 897 |
} |
| 898 |
|
| 899 |
return list; |
| 900 |
} |
| 901 |
|
| 902 |
QHash<int, int> AlbumManager::getTAlbumsCount() const |
| 903 |
{ |
| 904 |
return d->tAlbumsCount; |
| 905 |
} |
| 906 |
|
| 907 |
void AlbumManager::insertTAlbum(TAlbum* album, TAlbum* parent) |
| 908 |
{ |
| 909 |
if (!album) |
| 910 |
{ |
| 911 |
return; |
| 912 |
} |
| 913 |
|
| 914 |
Q_EMIT signalAlbumAboutToBeAdded(album, parent, parent ? parent->lastChild() : nullptr); |
| 915 |
|
| 916 |
if (parent) |
| 917 |
{ |
| 918 |
album->setParent(parent); |
| 919 |
} |
| 920 |
|
| 921 |
d->allAlbumsIdHash.insert(album->globalID(), album); |
| 922 |
|
| 923 |
Q_EMIT signalAlbumAdded(album); |
| 924 |
} |
| 925 |
|
| 926 |
void AlbumManager::removeTAlbum(TAlbum* album) |
| 927 |
{ |
| 928 |
if (!album) |
| 929 |
{ |
| 930 |
return; |
| 931 |
} |
| 932 |
|
| 933 |
// remove all children of this album |
| 934 |
|
| 935 |
Album* child = album->firstChild(); |
| 936 |
TAlbum* toBeRemoved = nullptr; |
| 937 |
|
| 938 |
while (child) |
| 939 |
{ |
| 940 |
Album* const next = child->next(); |
| 941 |
toBeRemoved = dynamic_cast<TAlbum*>(child); |
| 942 |
|
| 943 |
if (toBeRemoved) |
| 944 |
{ |
| 945 |
removeTAlbum(toBeRemoved); |
| 946 |
toBeRemoved = nullptr; |
| 947 |
} |
| 948 |
|
| 949 |
child = next; |
| 950 |
} |
| 951 |
|
| 952 |
Q_EMIT signalAlbumAboutToBeDeleted(album); |
| 953 |
|
| 954 |
d->allAlbumsIdHash.remove(album->globalID()); |
| 955 |
|
| 956 |
if (!d->currentAlbums.isEmpty()) |
| 957 |
{ |
| 958 |
if (album == d->currentAlbums.first()) |
| 959 |
{ |
| 960 |
d->currentAlbums.clear(); |
| 961 |
Q_EMIT signalAlbumCurrentChanged(d->currentAlbums); |
| 962 |
} |
| 963 |
} |
| 964 |
|
| 965 |
Q_EMIT signalAlbumDeleted(album); |
| 966 |
|
| 967 |
album->prepareForDeletion(); |
| 968 |
|
| 969 |
Q_EMIT signalAlbumHasBeenDeleted(album); |
| 970 |
|
| 971 |
delete album; |
| 972 |
} |
| 973 |
|
| 974 |
void AlbumManager::slotTagsJobResult() |
| 975 |
{ |
| 976 |
if (!d->tagListJob) |
| 977 |
{ |
| 978 |
return; |
| 979 |
} |
| 980 |
|
| 981 |
if (d->tagListJob->hasErrors()) |
| 982 |
{ |
| 983 |
qCWarning(DIGIKAM_GENERAL_LOG) << "Failed to list face tags"; |
| 984 |
|
| 985 |
// Pop-up a message about the error. |
| 986 |
|
| 987 |
DNotificationWrapper(QString(), d->personListJob->errorsList().first(), |
| 988 |
nullptr, i18n("digiKam")); |
| 989 |
} |
| 990 |
|
| 991 |
d->tagListJob = nullptr; |
| 992 |
} |
| 993 |
|
| 994 |
void AlbumManager::slotTagsJobData(const QHash<int, int>& tagsStatHash) |
| 995 |
{ |
| 996 |
if (tagsStatHash.isEmpty()) |
| 997 |
{ |
| 998 |
return; |
| 999 |
} |
| 1000 |
|
| 1001 |
d->tAlbumsCount = tagsStatHash; |
| 1002 |
|
| 1003 |
Q_EMIT signalTAlbumsDirty(tagsStatHash); |
| 1004 |
} |
| 1005 |
|
| 1006 |
void AlbumManager::slotTagChange(const TagChangeset& changeset) |
| 1007 |
{ |
| 1008 |
if (d->changingDB || !d->rootTAlbum) |
| 1009 |
{ |
| 1010 |
return; |
| 1011 |
} |
| 1012 |
|
| 1013 |
switch (changeset.operation()) |
| 1014 |
{ |
| 1015 |
case TagChangeset::Added: |
| 1016 |
case TagChangeset::Moved: |
| 1017 |
case TagChangeset::Deleted: |
| 1018 |
case TagChangeset::Reparented: |
| 1019 |
{ |
| 1020 |
if (!d->scanTAlbumsTimer->isActive()) |
| 1021 |
{ |
| 1022 |
d->scanTAlbumsTimer->start(); |
| 1023 |
} |
| 1024 |
|
| 1025 |
break; |
| 1026 |
} |
| 1027 |
|
| 1028 |
case TagChangeset::Renamed: |
| 1029 |
case TagChangeset::IconChanged: |
| 1030 |
{ |
| 1031 |
/** |
| 1032 |
* @todo what happens here? |
| 1033 |
*/ |
| 1034 |
break; |
| 1035 |
} |
| 1036 |
|
| 1037 |
case TagChangeset::PropertiesChanged: |
| 1038 |
{ |
| 1039 |
TAlbum* const tag = findTAlbum(changeset.tagId()); |
| 1040 |
|
| 1041 |
if (tag) |
| 1042 |
{ |
| 1043 |
Q_EMIT signalTagPropertiesChanged(tag); |
| 1044 |
} |
| 1045 |
|
| 1046 |
break; |
| 1047 |
} |
| 1048 |
|
| 1049 |
case TagChangeset::Unknown: |
| 1050 |
{ |
| 1051 |
break; |
| 1052 |
} |
| 1053 |
} |
| 1054 |
} |
| 1055 |
|
| 1056 |
void AlbumManager::slotImageTagChange(const ImageTagChangeset& changeset) |
| 1057 |
{ |
| 1058 |
if (!d->rootTAlbum) |
| 1059 |
{ |
| 1060 |
return; |
| 1061 |
} |
| 1062 |
|
| 1063 |
switch (changeset.operation()) |
| 1064 |
{ |
| 1065 |
case ImageTagChangeset::Added: |
| 1066 |
case ImageTagChangeset::Removed: |
| 1067 |
case ImageTagChangeset::RemovedAll: |
| 1068 |
|
| 1069 |
// Add properties changed. |
| 1070 |
// Reason: in people sidebar, the images are not |
| 1071 |
// connected with the ImageTag table but by |
| 1072 |
// ImageTagProperties entries. |
| 1073 |
// Thus, the count of entries in face tags are not |
| 1074 |
// updated. This adoption should fix the problem. |
| 1075 |
|
| 1076 |
case ImageTagChangeset::PropertiesChanged: |
| 1077 |
{ |
| 1078 |
const auto ids = changeset.tags(); |
| 1079 |
|
| 1080 |
for (int id : ids) |
| 1081 |
{ |
| 1082 |
if (!d->toUpdatedFaces.contains(id)) |
| 1083 |
{ |
| 1084 |
d->toUpdatedFaces << id; |
| 1085 |
} |
| 1086 |
} |
| 1087 |
|
| 1088 |
if (!d->tagItemCountTimer->isActive()) |
| 1089 |
{ |
| 1090 |
d->tagItemCountTimer->start(); |
| 1091 |
} |
| 1092 |
|
| 1093 |
break; |
| 1094 |
} |
| 1095 |
|
| 1096 |
default: |
| 1097 |
{ |
| 1098 |
break; |
| 1099 |
} |
| 1100 |
} |
| 1101 |
} |
| 1102 |
|
| 1103 |
void AlbumManager::askUserForWriteChangedTAlbumToFiles(TAlbum* const album) |
| 1104 |
{ |
| 1105 |
QList<qlonglong> imageIds = CoreDbAccess().db()->getItemIDsInTag(album->id(), true); |
| 1106 |
askUserForWriteChangedTAlbumToFiles(imageIds); |
| 1107 |
} |
| 1108 |
|
| 1109 |
void AlbumManager::askUserForWriteChangedTAlbumToFiles(const QList<qlonglong>& imageIds) |
| 1110 |
{ |
| 1111 |
const MetaEngineSettings* const settings = MetaEngineSettings::instance(); |
| 1112 |
|
| 1113 |
if ( |
| 1114 |
(!settings->settings().saveTags && !settings->settings().saveFaceTags) || |
| 1115 |
imageIds.isEmpty() |
| 1116 |
) |
| 1117 |
{ |
| 1118 |
return; |
| 1119 |
} |
| 1120 |
|
| 1121 |
if (settings->settings().useLazySync) |
| 1122 |
{ |
| 1123 |
MetadataHubMngr::instance()->addPendingIds(imageIds); |
| 1124 |
|
| 1125 |
return; |
| 1126 |
} |
| 1127 |
|
| 1128 |
if (imageIds.count() > 100) |
| 1129 |
{ |
| 1130 |
int result = d->longTimeMessageBoxResult; |
| 1131 |
|
| 1132 |
if (result == -1) |
| 1133 |
{ |
| 1134 |
QPointer<QMessageBox> msgBox = new QMessageBox(QMessageBox::Warning, |
| 1135 |
qApp->applicationName(), |
| 1136 |
i18n("This operation can take a long time in the background.\n" |
| 1137 |
"Do you want to write the metadata to %1 files now?", |
| 1138 |
imageIds.count()), |
| 1139 |
QMessageBox::Yes | QMessageBox::No, |
| 1140 |
qApp->activeWindow()); |
| 1141 |
QCheckBox* const chkBox = new QCheckBox(i18n("Do not ask again at this session"), msgBox); |
| 1142 |
msgBox->setCheckBox(chkBox); |
| 1143 |
|
| 1144 |
result = msgBox->exec(); |
| 1145 |
|
| 1146 |
if (chkBox->isChecked()) |
| 1147 |
{ |
| 1148 |
d->longTimeMessageBoxResult = result; |
| 1149 |
} |
| 1150 |
|
| 1151 |
delete msgBox; |
| 1152 |
} |
| 1153 |
|
| 1154 |
if (result != QMessageBox::Yes) |
| 1155 |
{ |
| 1156 |
return; |
| 1157 |
} |
| 1158 |
} |
| 1159 |
|
| 1160 |
ItemInfoList infos(imageIds); |
| 1161 |
MetadataSynchronizer* const tool = new MetadataSynchronizer(infos, |
| 1162 |
MetadataSynchronizer::WriteFromDatabaseToFile); |
| 1163 |
tool->setUseMultiCoreCPU(false); |
| 1164 |
tool->start(); |
| 1165 |
} |
| 1166 |
|
| 1167 |
} // namespace Digikam |