Debian Codegraph @

core/libs/album/manager/albummanager_salbum.cpp

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