1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2004-06-15
* Description : Albums manager interface - Search Album helpers.
*
* SPDX-FileCopyrightText: 2006-2025 by Gilles Caulier <caulier dot gilles at gmail dot com>
* SPDX-FileCopyrightText: 2006-2011 by Marcel Wiesweg <marcel dot wiesweg at gmx dot de>
* SPDX-FileCopyrightText: 2015 by Mohamed_Anwer <m_dot_anwer at gmx dot com>
*
* SPDX-License-Identifier: GPL-2.0-or-later
*
* ============================================================ */
#include "albummanager_p.h"
namespace Digikam
{
AlbumList AlbumManager::allSAlbums() const
{
AlbumList list;
if (d->rootSAlbum)
{
list.append(d->rootSAlbum);
}
AlbumIterator it(d->rootSAlbum);
while (it.current())
{
list.append(*it);
++it;
}
return list;
}
void AlbumManager::scanSAlbums()
{
d->scanSAlbumsTimer->stop();
// list SAlbums directly from the db
// first insert all the current SAlbums into a map for quick lookup
QMap<int, SAlbum*> oldSearches;
AlbumIterator it(d->rootSAlbum);
while (it.current())
{
SAlbum* const search = (SAlbum*)(*it);
oldSearches[search->id()] = search;
++it;
}
// scan db and get a list of all albums
QList<SearchInfo> currentSearches = CoreDbAccess().db()->scanSearches();
QList<SearchInfo> newSearches;
// go through all the Albums and see which ones are already present
for (const SearchInfo& info : std::as_const(currentSearches))
{
if (oldSearches.contains(info.id))
{
SAlbum* const album = oldSearches[info.id];
if (
(info.name != album->title()) ||
(info.type != album->searchType()) ||
(info.query != album->query())
)
{
QString oldName = album->title();
album->setSearch(info.type, info.query);
album->setTitle(info.name);
if (oldName != album->title())
{
Q_EMIT signalAlbumRenamed(album);
}
Q_EMIT signalSearchUpdated(album);
}
oldSearches.remove(info.id);
}
else
{
newSearches << info;
}
}
// remove old albums that have been deleted
for (SAlbum* const album : std::as_const(oldSearches))
{
Q_EMIT signalAlbumAboutToBeDeleted(album);
d->allAlbumsIdHash.remove(album->globalID());
Q_EMIT signalAlbumDeleted(album);
album->prepareForDeletion();
Q_EMIT signalAlbumHasBeenDeleted(album);
delete album;
}
// add new albums
for (const SearchInfo& info : std::as_const(newSearches))
{
SAlbum* const album = new SAlbum(info.name, info.id);
album->setSearch(info.type, info.query);
Q_EMIT signalAlbumAboutToBeAdded(album, d->rootSAlbum, d->rootSAlbum->lastChild());
album->setParent(d->rootSAlbum);
d->allAlbumsIdHash[album->globalID()] = album;
Q_EMIT signalAlbumAdded(album);
}
}
SAlbum* AlbumManager::findSAlbum(int id) const
{
if (!d->rootSAlbum)
{
return nullptr;
}
int gid = d->rootSAlbum->globalID() + id;
return (static_cast<SAlbum*>((d->allAlbumsIdHash.value(gid))));
}
SAlbum* AlbumManager::findSAlbum(const QString& name) const
{
for (Album* album = d->rootSAlbum->firstChild() ;
album ; album = album->next())
{
if (album->title() == name)
{
return (dynamic_cast<SAlbum*>(album));
}
}
return nullptr;
}
QList<SAlbum*> AlbumManager::findSAlbumsBySearchType(int searchType) const
{
QList<SAlbum*> albums;
for (Album* album = d->rootSAlbum->firstChild() ;
album ; album = album->next())
{
SAlbum* const sAlbum = dynamic_cast<SAlbum*>(album);<--- Variable 'sAlbum' can be declared as pointer to const
if ((sAlbum) && (sAlbum->searchType() == searchType))
{
albums.append(sAlbum);
}
}
return albums;
}
SAlbum* AlbumManager::createSAlbum(const QString& name,
DatabaseSearch::Type type,
const QString& query)
{
// first iterate through all the search albums and see if there's an existing
// SAlbum with same name. (Remember, SAlbums are arranged in a flat list)
SAlbum* album = findSAlbum(name);
ChangingDB changing(d);
if (album)
{
updateSAlbum(album, query, name, type);
return album;
}
int id = CoreDbAccess().db()->addSearch(type, name, query);
if (id == -1)
{
return nullptr;
}
album = new SAlbum(name, id);
Q_EMIT signalAlbumAboutToBeAdded(album, d->rootSAlbum, d->rootSAlbum->lastChild());
album->setSearch(type, query);
album->setParent(d->rootSAlbum);
d->allAlbumsIdHash.insert(album->globalID(), album);
Q_EMIT signalAlbumAdded(album);
return album;
}
bool AlbumManager::updateSAlbum(SAlbum* album,
const QString& changedQuery,
const QString& changedName,
DatabaseSearch::Type type)
{
if (!album)
{
return false;
}
QString newName = changedName.isNull() ? album->title() : changedName;
DatabaseSearch::Type newType = (type == DatabaseSearch::UndefinedType) ? album->searchType() : type;
ChangingDB changing(d);
CoreDbAccess().db()->updateSearch(album->id(), newType, newName, changedQuery);
QString oldName = album->title();
album->setSearch(newType, changedQuery);
album->setTitle(newName);
if (oldName != album->title())
{
Q_EMIT signalAlbumRenamed(album);
}
if (!d->currentAlbums.isEmpty())
{
if (d->currentAlbums.first() == album)
{
Q_EMIT signalAlbumCurrentChanged(d->currentAlbums);
}
}
return true;
}
bool AlbumManager::deleteSAlbum(SAlbum* album)
{
if (!album)
{
return false;
}
Q_EMIT signalAlbumAboutToBeDeleted(album);
ChangingDB changing(d);
CoreDbAccess().db()->deleteSearch(album->id());
d->allAlbumsIdHash.remove(album->globalID());
Q_EMIT signalAlbumDeleted(album);
album->prepareForDeletion();
Q_EMIT signalAlbumHasBeenDeleted(album);
delete album;
return true;
}
void AlbumManager::slotSearchChange(const SearchChangeset& changeset)
{
if (d->changingDB || !d->rootSAlbum)
{
return;
}
switch (changeset.operation())
{
case SearchChangeset::Added:
case SearchChangeset::Deleted:
{
if (!d->scanSAlbumsTimer->isActive())
{
d->scanSAlbumsTimer->start();
}
break;
}
case SearchChangeset::Changed:
{
if (!d->currentAlbums.isEmpty())
{
Album* const currentAlbum = d->currentAlbums.first();<--- Variable 'currentAlbum' can be declared as pointer to const
if (
currentAlbum &&
(currentAlbum->type() == Album::SEARCH) &&
(currentAlbum->id() == changeset.searchId())
)
{
// the pointer is the same, but the contents changed
Q_EMIT signalAlbumCurrentChanged(d->currentAlbums);
}
}
break;
}
case SearchChangeset::Unknown:
{
break;
}
}
}
} // namespace Digikam
|