| 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 - Date 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::scanDAlbums() |
| 23 |
{ |
| 24 |
d->scanDAlbumsTimer->stop(); |
| 25 |
|
| 26 |
if (d->dateListJob) |
| 27 |
{ |
| 28 |
disconnect(d->dateListJob, nullptr, this, nullptr); |
| 29 |
|
| 30 |
d->dateListJob->cancel(); |
| 31 |
d->dateListJob = nullptr; |
| 32 |
} |
| 33 |
|
| 34 |
DatesDBJobInfo jInfo; |
| 35 |
jInfo.setFoldersJob(); |
| 36 |
d->dateListJob = DBJobsManager::instance()->startDatesJobThread(jInfo); |
| 37 |
|
| 38 |
connect(d->dateListJob, SIGNAL(signalFinished()), |
| 39 |
this, SLOT(slotDatesJobResult())); |
| 40 |
|
| 41 |
connect(d->dateListJob, SIGNAL(foldersData(QHash<QDateTime,int>)), |
| 42 |
this, SLOT(slotDatesJobData(QHash<QDateTime,int>))); |
| 43 |
} |
| 44 |
|
| 45 |
AlbumList AlbumManager::allDAlbums() const |
| 46 |
{ |
| 47 |
AlbumList list; |
| 48 |
|
| 49 |
if (d->rootDAlbum) |
| 50 |
{ |
| 51 |
list.append(d->rootDAlbum); |
| 52 |
} |
| 53 |
|
| 54 |
AlbumIterator it(d->rootDAlbum); |
| 55 |
|
| 56 |
while (it.current()) |
| 57 |
{ |
| 58 |
list.append(*it); |
| 59 |
++it; |
| 60 |
} |
| 61 |
|
| 62 |
return list; |
| 63 |
} |
| 64 |
|
| 65 |
DAlbum* AlbumManager::findDAlbum(int id) const |
| 66 |
{ |
| 67 |
if (!d->rootDAlbum) |
| 68 |
{ |
| 69 |
return nullptr; |
| 70 |
} |
| 71 |
|
| 72 |
int gid = d->rootDAlbum->globalID() + id; |
| 73 |
|
| 74 |
return static_cast<DAlbum*>((d->allAlbumsIdHash.value(gid))); |
| 75 |
} |
| 76 |
|
| 77 |
QMap<YearMonth, int> AlbumManager::getDAlbumsCount() const |
| 78 |
{ |
| 79 |
return d->dAlbumsCount; |
| 80 |
} |
| 81 |
|
| 82 |
void AlbumManager::slotDatesJobResult() |
| 83 |
{ |
| 84 |
if (!d->dateListJob) |
| 85 |
{ |
| 86 |
return; |
| 87 |
} |
| 88 |
|
| 89 |
if (d->dateListJob->hasErrors()) |
| 90 |
{ |
| 91 |
qCWarning(DIGIKAM_GENERAL_LOG) << "Failed to list dates"; |
| 92 |
|
| 93 |
// Pop-up a message about the error. |
| 94 |
|
| 95 |
DNotificationWrapper(QString(), d->dateListJob->errorsList().first(), |
| 96 |
nullptr, i18n("digiKam")); |
| 97 |
} |
| 98 |
|
| 99 |
d->dateListJob = nullptr; |
| 100 |
|
| 101 |
Q_EMIT signalAllDAlbumsLoaded(); |
| 102 |
} |
| 103 |
|
| 104 |
void AlbumManager::slotDatesJobData(const QHash<QDateTime, int>& datesStatHash) |
| 105 |
{ |
| 106 |
if (datesStatHash.isEmpty() || !d->rootDAlbum) |
| 107 |
{ |
| 108 |
return; |
| 109 |
} |
| 110 |
|
| 111 |
// insert all the DAlbums into a qmap for quick access |
| 112 |
|
| 113 |
QMap<QDate, DAlbum*> mAlbumMap; |
| 114 |
QMap<int, DAlbum*> yAlbumMap; |
| 115 |
|
| 116 |
AlbumIterator it(d->rootDAlbum); |
| 117 |
|
| 118 |
while (it.current()) |
| 119 |
{ |
| 120 |
DAlbum* const a = (DAlbum*)(*it); // cppcheck-suppress constVariablePointer |
| 121 |
|
| 122 |
if (a->range() == DAlbum::Month) |
| 123 |
{ |
| 124 |
mAlbumMap.insert(a->date(), a); |
| 125 |
} |
| 126 |
else |
| 127 |
{ |
| 128 |
yAlbumMap.insert(a->date().year(), a); |
| 129 |
} |
| 130 |
|
| 131 |
++it; |
| 132 |
} |
| 133 |
|
| 134 |
QMap<YearMonth, int> yearMonthMap; |
| 135 |
|
| 136 |
for (QHash<QDateTime, int>::const_iterator it3 = datesStatHash.constBegin() ; |
| 137 |
it3 != datesStatHash.constEnd() ; ++it3) |
| 138 |
{ |
| 139 |
YearMonth yearMonth = YearMonth(it3.key().date().year(), it3.key().date().month()); |
| 140 |
|
| 141 |
QMap<YearMonth, int>::iterator it2 = yearMonthMap.find(yearMonth); |
| 142 |
|
| 143 |
if (it2 == yearMonthMap.end()) |
| 144 |
{ |
| 145 |
yearMonthMap.insert(yearMonth, *it3); |
| 146 |
} |
| 147 |
else |
| 148 |
{ |
| 149 |
*it2 += *it3; |
| 150 |
} |
| 151 |
} |
| 152 |
|
| 153 |
int year, month; |
| 154 |
|
| 155 |
for (QMap<YearMonth, int>::const_iterator it4 = yearMonthMap.constBegin() ; |
| 156 |
it4 != yearMonthMap.constEnd() ; ++it4) |
| 157 |
{ |
| 158 |
year = it4.key().first; |
| 159 |
month = it4.key().second; |
| 160 |
|
| 161 |
QDate md(year, month, 1); |
| 162 |
|
| 163 |
// Do we already have this Month album |
| 164 |
|
| 165 |
if (mAlbumMap.contains(md)) |
| 166 |
{ |
| 167 |
// already there. remove Month album from map |
| 168 |
|
| 169 |
mAlbumMap.remove(md); |
| 170 |
|
| 171 |
if (yAlbumMap.contains(year)) |
| 172 |
{ |
| 173 |
// already there. remove from map |
| 174 |
|
| 175 |
yAlbumMap.remove(year); |
| 176 |
} |
| 177 |
|
| 178 |
continue; |
| 179 |
} |
| 180 |
|
| 181 |
// Check if Year Album already exist. |
| 182 |
|
| 183 |
DAlbum* yAlbum = nullptr; |
| 184 |
AlbumIterator it5(d->rootDAlbum); |
| 185 |
|
| 186 |
while (it5.current()) |
| 187 |
{ |
| 188 |
DAlbum* const a = (DAlbum*)(*it5); |
| 189 |
|
| 190 |
if ((a->date() == QDate(year, 1, 1)) && (a->range() == DAlbum::Year)) |
| 191 |
{ |
| 192 |
yAlbum = a; |
| 193 |
break; |
| 194 |
} |
| 195 |
|
| 196 |
++it5; |
| 197 |
} |
| 198 |
|
| 199 |
// If no, create Year album. |
| 200 |
|
| 201 |
if (!yAlbum) |
| 202 |
{ |
| 203 |
yAlbum = new DAlbum(QDate(year, 1, 1), false, DAlbum::Year); |
| 204 |
|
| 205 |
Q_EMIT signalAlbumAboutToBeAdded(yAlbum, d->rootDAlbum, d->rootDAlbum->lastChild()); |
| 206 |
|
| 207 |
yAlbum->setParent(d->rootDAlbum); |
| 208 |
d->allAlbumsIdHash.insert(yAlbum->globalID(), yAlbum); |
| 209 |
|
| 210 |
Q_EMIT signalAlbumAdded(yAlbum); |
| 211 |
} |
| 212 |
|
| 213 |
// Create Month album |
| 214 |
|
| 215 |
DAlbum* const mAlbum = new DAlbum(md); |
| 216 |
|
| 217 |
Q_EMIT signalAlbumAboutToBeAdded(mAlbum, yAlbum, yAlbum->lastChild()); |
| 218 |
|
| 219 |
mAlbum->setParent(yAlbum); |
| 220 |
d->allAlbumsIdHash.insert(mAlbum->globalID(), mAlbum); |
| 221 |
|
| 222 |
Q_EMIT signalAlbumAdded(mAlbum); |
| 223 |
} |
| 224 |
|
| 225 |
// Now the items contained in the maps are the ones which |
| 226 |
// have been deleted. |
| 227 |
|
| 228 |
for (QMap<QDate, DAlbum*>::const_iterator it6 = mAlbumMap.constBegin() ; |
| 229 |
it6 != mAlbumMap.constEnd() ; ++it6) |
| 230 |
{ |
| 231 |
DAlbum* const album = it6.value(); |
| 232 |
|
| 233 |
Q_EMIT signalAlbumAboutToBeDeleted(album); |
| 234 |
|
| 235 |
d->allAlbumsIdHash.remove(album->globalID()); |
| 236 |
|
| 237 |
Q_EMIT signalAlbumDeleted(album); |
| 238 |
|
| 239 |
album->prepareForDeletion(); |
| 240 |
|
| 241 |
Q_EMIT signalAlbumHasBeenDeleted(album); |
| 242 |
|
| 243 |
delete album; |
| 244 |
} |
| 245 |
|
| 246 |
for (QMap<int, DAlbum*>::const_iterator it7 = yAlbumMap.constBegin() ; |
| 247 |
it7 != yAlbumMap.constEnd() ; ++it7) |
| 248 |
{ |
| 249 |
DAlbum* const album = it7.value(); |
| 250 |
|
| 251 |
Q_EMIT signalAlbumAboutToBeDeleted(album); |
| 252 |
|
| 253 |
d->allAlbumsIdHash.remove(album->globalID()); |
| 254 |
|
| 255 |
Q_EMIT signalAlbumDeleted(album); |
| 256 |
|
| 257 |
album->prepareForDeletion(); |
| 258 |
|
| 259 |
Q_EMIT signalAlbumHasBeenDeleted(album); |
| 260 |
|
| 261 |
delete album; |
| 262 |
} |
| 263 |
|
| 264 |
d->dAlbumsCount = yearMonthMap; |
| 265 |
|
| 266 |
Q_EMIT signalDAlbumsDirty(yearMonthMap); |
| 267 |
Q_EMIT signalDatesHashDirty(datesStatHash); |
| 268 |
} |
| 269 |
|
| 270 |
void AlbumManager::scanDAlbumsScheduled() |
| 271 |
{ |
| 272 |
// Avoid a cycle of killing a job which takes longer than the timer interval |
| 273 |
|
| 274 |
if (d->dateListJob) |
| 275 |
{ |
| 276 |
d->scanDAlbumsTimer->start(); |
| 277 |
return; |
| 278 |
} |
| 279 |
|
| 280 |
scanDAlbums(); |
| 281 |
} |
| 282 |
|
| 283 |
} // namespace Digikam |