Debian Codegraph @

core/libs/album/engine/albummodificationhelper.cpp

raw
1 /* ============================================================
2 *
3 * This file is a part of digiKam project
4 * https://www.digikam.org
5 *
6 * Date : 2000-12-05
7 * Description : helper class used to modify physical albums in views
8 *
9 * SPDX-FileCopyrightText: 2009-2011 by Johannes Wienke <languitar at semipol dot de>
10 * SPDX-FileCopyrightText: 2014-2015 by Mohamed_Anwer <m_dot_anwer at gmx dot com>
11 *
12 * SPDX-License-Identifier: GPL-2.0-or-later
13 *
14 * ============================================================ */
15
16 #include "albummodificationhelper.h"
17
18 // Qt includes
19
20 #include <QApplication>
21 #include <QDirIterator>
22 #include <QInputDialog>
23 #include <QMessageBox>
24 #include <QAction>
25 #include <QUrl>
26
27 // KDE includes
28
29 #include <klocalizedstring.h>
30
31 // Local includes
32
33 #include "digikam_debug.h"
34 #include "albummanager.h"
35 #include "albumpropsedit.h"
36 #include "albumpointer.h"
37 #include "applicationsettings.h"
38 #include "collectionmanager.h"
39 #include "deletedialog.h"
40 #include "dio.h"
41 #include "itemiconview.h"
42 #include "digikamapp.h"
43 #include "coredb.h"
44 #include "coredbaccess.h"
45
46 namespace Digikam
47 {
48
49 class Q_DECL_HIDDEN AlbumModificationHelper::Private
50 {
51 public:
52
53 Private() = default;
54
55 public:
56
57 QWidget* dialogParent = nullptr;
58 };
59
60 AlbumModificationHelper::AlbumModificationHelper(QObject* const parent, QWidget* const dialogParent)
61 : QObject(parent),
62 d (new Private)
63 {
64 d->dialogParent = dialogParent;
65 }
66
67 AlbumModificationHelper::~AlbumModificationHelper()
68 {
69 delete d;
70 }
71
72 void AlbumModificationHelper::bindAlbum(QAction* const action, PAlbum* const album) const
73 {
74 action->setData(QVariant::fromValue(AlbumPointer<PAlbum>(album)));
75 }
76
77 PAlbum* AlbumModificationHelper::boundAlbum(QObject* const sender) const
78 {
79 QAction* action = nullptr;
80
81 if ((action = qobject_cast<QAction*>(sender)))
82 {
83 return action->data().value<AlbumPointer<PAlbum> >();
84 }
85
86 return nullptr;
87 }
88
89 PAlbum* AlbumModificationHelper::slotAlbumNew()
90 {
91 return slotAlbumNew(boundAlbum(sender()));
92 }
93
94 PAlbum* AlbumModificationHelper::slotAlbumNew(PAlbum* parent)
95 {
96 if (!parent)
97 {
98 qCWarning(DIGIKAM_GENERAL_LOG) << "No parent album given";
99 return nullptr;
100 }
101
102 const ApplicationSettings* settings = ApplicationSettings::instance();
103
104 if (!settings)
105 {
106 qCWarning(DIGIKAM_GENERAL_LOG) << "could not get Album Settings";
107 return nullptr;
108 }
109
110 /*
111 QDir libraryDir(settings->getAlbumLibraryPath());
112
113 if (!libraryDir.exists())
114 {
115 QMessageBox::critical(qApp->activeWindow(), qApp->applicationName(),
116 i18n("The album library has not been set correctly.\n"
117 "Select \"Configure Digikam\" from the Settings "
118 "menu and choose a folder to use for the album "
119 "library."));
120 return;
121 }
122 */
123
124 // if we create an album under root, need to supply the album root path.
125
126 QString albumRootPath;
127
128 albumRootPath = CollectionManager::instance()->albumRootPath(parent->albumRootId());
129
130 QString title;
131 QString comments;
132 QString category;
133 QDate date;
134 QStringList albumCategories;
135 int parentSelector;
136
137 if (!AlbumPropsEdit::createNew(parent, title, comments, date, category,
138 albumCategories, parentSelector))
139 {
140 return nullptr;
141 }
142
143 QStringList oldAlbumCategories(ApplicationSettings::instance()->getAlbumCategoryNames());
144
145 if (albumCategories != oldAlbumCategories)
146 {
147 ApplicationSettings::instance()->setAlbumCategoryNames(albumCategories);
148 }
149
150 QString errMsg;
151 PAlbum* album = nullptr;
152
153 if (parent->isRoot() || (parentSelector == 1))
154 {
155 album = AlbumManager::instance()->createPAlbum(albumRootPath, title, comments,
156 date, category, errMsg);
157 }
158 else
159 {
160 album = AlbumManager::instance()->createPAlbum(parent, title, comments,
161 date, category, errMsg);
162 }
163
164 if (!album)
165 {
166 QMessageBox::critical(qApp->activeWindow(), qApp->applicationName(), errMsg);
167
168 return nullptr;
169 }
170
171 return album;
172 }
173
174 void AlbumModificationHelper::slotAlbumDelete()
175 {
176 slotAlbumDelete(boundAlbum(sender()));
177 }
178
179 void AlbumModificationHelper::slotAlbumDelete(PAlbum* album)
180 {
181 if (!album || album->isRoot() || album->isAlbumRoot())
182 {
183 return;
184 }
185
186 // find subalbums
187
188 QList<QUrl> childrenList;
189 addAlbumChildrenToList(childrenList, album);
190
191 DeleteDialog dialog(d->dialogParent);
192
193 // All subalbums will be presented in the list as well
194
195 if (!dialog.confirmDeleteList(childrenList,
196 (childrenList.size() == 1) ? DeleteDialogMode::Albums
197 : DeleteDialogMode::Subalbums,
198 DeleteDialogMode::UserPreference))
199 {
200 return;
201 }
202
203 bool useTrash = !dialog.shouldDelete();
204 QFileInfo fileInfo(album->folderPath());
205
206 // If the trash is used no check is necessary, as the trash lists all files
207 // and only perform this check if the album is a directory
208
209 if (!useTrash && fileInfo.isDir())
210 {
211 QStringList imageTypes, audioTypes, videoTypes, allTypes, foundTypes;
212 QDirIterator it(fileInfo.path(), QDir::Files, QDirIterator::Subdirectories);
213
214 CoreDbAccess().db()->getFilterSettings(&imageTypes, &videoTypes, &audioTypes);
215 allTypes << imageTypes << audioTypes << videoTypes;
216 bool notEmpty = false;
217
218 while (it.hasNext())
219 {
220 it.next();
221 QString ext = it.fileInfo().suffix().toLower();
222
223 if (!allTypes.contains(ext) && !foundTypes.contains(ext))
224 {
225 foundTypes << ext;
226 }
227 else if (allTypes.contains(ext) && !notEmpty)
228 {
229 notEmpty = true;
230 }
231 }
232
233 if (!foundTypes.isEmpty())
234 {
235 foundTypes.sort();
236
237 QString found = foundTypes.join(QLatin1String(", "));
238
239 int result = QMessageBox::warning(qApp->activeWindow(), qApp->applicationName(),
240 i18n("<p>The album you want to delete contains files "
241 "(%1) which are not displayed in digiKam.</p>"
242 "<p>Do you want to continue?</p>", found),
243 QMessageBox::Yes | QMessageBox::No);
244
245 if (result != QMessageBox::Yes)
246 {
247 return;
248 }
249 }
250 else if (notEmpty)
251 {
252 int result = QMessageBox::warning(qApp->activeWindow(), qApp->applicationName(),
253 i18n("<p>The album you want to delete is not empty.</p>"
254 "<p>Do you want to continue?</p>"),
255 QMessageBox::Yes | QMessageBox::No);
256
257 if (result != QMessageBox::Yes)
258 {
259 return;
260 }
261 }
262 }
263
264 DIO::del(album, useTrash);
265 }
266
267 void AlbumModificationHelper::slotAlbumRename()
268 {
269 slotAlbumRename(boundAlbum(sender()));
270 }
271
272 void AlbumModificationHelper::slotAlbumRename(PAlbum* album)
273 {
274 if (!album)
275 {
276 return;
277 }
278
279 QString oldTitle(album->title());
280
281 QPointer<QInputDialog> textDlg = new QInputDialog(d->dialogParent);
282
283 if (!album->isAlbumRoot())
284 {
285 textDlg->setWindowTitle(i18nc("@title:window", "Rename Album (%1)", oldTitle));
286 }
287 else
288 {
289 textDlg->setWindowTitle(i18nc("@title:window", "Rename Album Root (%1)", oldTitle));
290 }
291
292 textDlg->setLabelText(i18n("Enter new album name:"));
293 textDlg->resize(450, textDlg->sizeHint().height());
294 textDlg->setInputMode(QInputDialog::TextInput);
295 textDlg->setTextEchoMode(QLineEdit::Normal);
296 textDlg->setTextValue(oldTitle);
297
298 if (textDlg->exec() != QDialog::Accepted)
299 {
300 delete textDlg;
301 return;
302 }
303
304 QString title = textDlg->textValue().trimmed();
305 delete textDlg;
306
307 if (title != oldTitle)
308 {
309 if (album->isAlbumRoot())
310 {
311 CollectionLocation location = CollectionManager::instance()->
312 locationForAlbumRootId(album->albumRootId());
313
314 if (!location.isNull())
315 {
316 CollectionManager::instance()->setLabel(location, title);
317 }
318 }
319 else
320 {
321 QString errMsg;
322
323 if (!AlbumManager::instance()->renamePAlbum(album, title, errMsg))
324 {
325 QMessageBox::critical(qApp->activeWindow(), qApp->applicationName(), errMsg);
326 }
327 }
328
329 // Resorting the tree View after changing metadata
330
331 DigikamApp::instance()->view()->slotSortAlbums(ApplicationSettings::instance()->getAlbumSortRole());
332 }
333 }
334
335 void AlbumModificationHelper::addAlbumChildrenToList(QList<QUrl>& list, Album* const album)
336 {
337 // simple recursive helper function
338
339 if (album)
340 {
341 if (!list.contains(album->databaseUrl()))
342 {
343 list.append(album->databaseUrl());
344 }
345
346 AlbumIterator it(album);
347
348 while (it.current())
349 {
350 addAlbumChildrenToList(list, *it);
351 ++it;
352 }
353 }
354 }
355
356 void AlbumModificationHelper::slotAlbumEdit()
357 {
358 slotAlbumEdit(boundAlbum(sender()));
359 }
360
361 void AlbumModificationHelper::slotAlbumEdit(PAlbum* album)
362 {
363 if (!album || album->isRoot() || album->isAlbumRoot())
364 {
365 return;
366 }
367
368 QString oldTitle(album->title());
369 QString oldComments(album->caption());
370 QString oldCategory(album->category());
371 QDate oldDate(album->date());
372 QString title, comments, category;
373 QDate date;
374 QStringList albumCategories;
375
376 if (AlbumPropsEdit::editProps(album, title, comments, date,
377 category, albumCategories))
378 {
379 if (comments != oldComments)
380 {
381 album->setCaption(comments);
382 }
383
384 if ((date != oldDate) && date.isValid())
385 {
386 album->setDate(date);
387 }
388
389 if (category != oldCategory)
390 {
391 album->setCategory(category);
392 }
393
394 ApplicationSettings::instance()->setAlbumCategoryNames(albumCategories);
395
396 // Do this last : so that if anything else changed we can
397 // successfully save to the db with the old name
398
399 if (title != oldTitle)
400 {
401 QString errMsg;
402
403 if (!AlbumManager::instance()->renamePAlbum(album, title, errMsg))
404 {
405 QMessageBox::critical(d->dialogParent, qApp->applicationName(), errMsg);
406 }
407 }
408
409 // Resorting the tree View after changing metadata
410
411 DigikamApp::instance()->view()->slotSortAlbums(ApplicationSettings::instance()->getAlbumSortRole());
412 }
413 }
414
415 void AlbumModificationHelper::slotAlbumResetIcon(PAlbum* album)
416 {
417 if (!album)
418 {
419 return;
420 }
421
422 QString err;
423 AlbumManager::instance()->updatePAlbumIcon(album, 0, err);
424 }
425
426 void AlbumModificationHelper::slotAlbumResetIcon()
427 {
428 slotAlbumResetIcon(boundAlbum(sender()));
429 }
430
431 } // namespace Digikam
432
433 #include "moc_albummodificationhelper.cpp"