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
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834 | /* ============================================================
*
* This file is a part of digiKam project
* https://www.digikam.org
*
* Date : 2004-06-15
* Description : Albums manager interface - Database 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"
// Qt includes
#if QT_VERSION < QT_VERSION_CHECK(6, 0, 0)
# include <QTextCodec>
#else
# include <QStringConverter>
#endif
// KDE includes
#include <kconfiggroup.h>
#include <ksharedconfig.h>
namespace Digikam
{
bool AlbumManager::setDatabase(const DbEngineParameters& params, bool priority, const QString& suggestedAlbumRoot, bool ignoreDisappearedLocations)
{
// This is to ensure that the setup does not overrule the command line.
// TODO: there is a bug that setup is showing something different here.
if (priority)
{
d->hasPriorizedDbPath = true;
}
else if (d->hasPriorizedDbPath)
{
// ignore change without priority
return true;
}
d->changed = true;
QApplication::setOverrideCursor(Qt::WaitCursor);
DatabaseServerStarter::instance()->stopServerManagerProcess();
// Shutdown possibly running collection scans.
// Must call restartCollectionScan further down.
ScanController::instance()->cancelAllAndSuspendCollectionScan();
disconnect(CollectionManager::instance(), nullptr, this, nullptr);
CollectionManager::instance()->setWatchDisabled();
if (CoreDbAccess::databaseWatch())
{
disconnect(CoreDbAccess::databaseWatch(), nullptr, this, nullptr);
}
ItemAttributesWatch::shutDown();
ItemAttributesWatch::cleanUp();
d->albumWatch->clear();
cleanUp();
d->currentAlbums.clear();
Q_EMIT signalAlbumCurrentChanged(d->currentAlbums);
Q_EMIT signalAlbumsCleared();
d->albumPathHash.clear();
d->allAlbumsIdHash.clear();
d->albumRootAlbumHash.clear();
// deletes all child albums as well
delete d->rootPAlbum;
delete d->rootTAlbum;
delete d->rootDAlbum;
delete d->rootSAlbum;
d->rootPAlbum = nullptr;
d->rootTAlbum = nullptr;
d->rootDAlbum = nullptr;
d->rootSAlbum = nullptr;
// -- Database initialization -------------------------------------------------
// ensure, embedded database is loaded
qCDebug(DIGIKAM_GENERAL_LOG) << params;
QString databaseError;
ApplicationSettings* const settings = ApplicationSettings::instance();<--- Variable 'settings' can be declared as pointer to const
if (params.internalServer && suggestedAlbumRoot.isEmpty())
{
if (!QFileInfo::exists(params.internalServerPath()))
{
databaseError = i18n("The MySQL database directory was not found.");
}
else if (
(
!QFileInfo::exists(params.internalServerMysqlUpgradeCmd) &&
QStandardPaths::findExecutable(params.internalServerMysqlUpgradeCmd).isEmpty()
) ||
(
!QFileInfo::exists(params.internalServerMysqlServerCmd) &&
QStandardPaths::findExecutable(params.internalServerMysqlServerCmd).isEmpty()
) ||
(
!QFileInfo::exists(params.internalServerMysqlAdminCmd) &&
QStandardPaths::findExecutable(params.internalServerMysqlAdminCmd).isEmpty()
)
)
{
databaseError = i18n("The MySQL binary tools was not found.");
}
}
else if (params.isSQLite() && suggestedAlbumRoot.isEmpty() && !settings->getDatabaseDirSetAtCmd())
{
if (!QFileInfo::exists(params.databaseNameCore))
{
databaseError = i18n("The SQLite core database was not found.");
}
}
if (!databaseError.isEmpty())
{
QString configPath = QStandardPaths::locate(QStandardPaths::GenericConfigLocation,
QLatin1String("digikamrc"));
databaseError += i18n("<p>If you want to start with a new configuration and "
"with a first run wizard, delete the file:<br/>%1</p>",
QDir::toNativeSeparators(configPath));
return showDatabaseSetupPage(databaseError, priority, suggestedAlbumRoot);
}
if (params.internalServer)
{
DatabaseServerError result = DatabaseServerStarter::instance()->startServerManagerProcess(params);
if (result.getErrorType() != DatabaseServerError::NoErrors)
{
databaseError = i18n("An error occurred during the internal server start."
"<p>Details:\n%1</p>", result.getErrorText());
return showDatabaseSetupPage(databaseError, priority, suggestedAlbumRoot);
}
}
CoreDbAccess::setParameters(params, CoreDbAccess::MainApplication);
DbEngineGuiErrorHandler* const handler = new DbEngineGuiErrorHandler(CoreDbAccess::parameters());
CoreDbAccess::initDbEngineErrorHandler(handler);
QApplication::restoreOverrideCursor();
if (!handler->checkDatabaseConnection())
{
databaseError = i18n("Failed to open the database.");
if (!showDatabaseSetupPage(databaseError, priority, suggestedAlbumRoot))
{
if (params.isSQLite())
{
QMessageBox::critical(qApp->activeWindow(), qApp->applicationName(),
i18n("<p>digiKam will attempt to start now, "
"but it will <b>not</b> be functional.</p>"));
CoreDbAccess::setParameters(DbEngineParameters(), CoreDbAccess::DatabaseSlave);
return true;
}
return false;
}
return true;
}
QApplication::setOverrideCursor(Qt::WaitCursor);
d->albumWatch->setDbEngineParameters(params);
ScanController::Advice advice = ScanController::instance()->databaseInitialization();
QString errorMsg = CoreDbAccess().lastError();
QApplication::restoreOverrideCursor();
switch (advice)
{
case ScanController::Success:
{
break;
}
case ScanController::ContinueWithoutDatabase:
{
if (errorMsg.isEmpty())
{
QMessageBox::critical(qApp->activeWindow(), qApp->applicationName(),
i18n("<p>Failed to open the database.</p>"
"<p>You cannot use digiKam without a working database. "
"digiKam will attempt to start now, but it will <b>not</b> be functional. "
"Please check the database settings in the <b>configuration menu</b>.</p>"
));
}
else
{
QMessageBox::critical(qApp->activeWindow(), qApp->applicationName(),
i18n("<p>Failed to open the database. Error message from database:</p>"
"<p><b>%1</b></p>"
"<p>You cannot use digiKam without a working database. "
"digiKam will attempt to start now, but it will <b>not</b> be functional. "
"Please check the database settings in the <b>configuration menu</b>.</p>",
errorMsg));
}
return true;
}
case ScanController::AbortImmediately:
{
if (!errorMsg.isEmpty())
{
databaseError = errorMsg;
}
else
{
databaseError = i18n("Failed to initialize the database.");
}
return showDatabaseSetupPage(databaseError, priority, suggestedAlbumRoot);
}
}
// -- Locale Checking ---------------------------------------------------------
QString currLocale = CoreDbAccess().db()->getDatabaseEncoding();
QString dbLocale = CoreDbAccess().db()->getSetting(QLatin1String("Locale"));
if (dbLocale.isEmpty())
{
qCDebug(DIGIKAM_GENERAL_LOG) << "No locale found in database";
CoreDbAccess().db()->setSetting(QLatin1String("Locale"), currLocale);
}
else if (dbLocale != currLocale)
{
// TODO it would be better to replace all yes/no confirmation dialogs with ones that has custom
// buttons that denote the actions directly, i.e.: ["Ignore and Continue"] ["Adjust locale"]
int result = QMessageBox::warning(qApp->activeWindow(), qApp->applicationName(),
i18n("Your database character set has changed since this "
"album was last opened.\n"
"Old character set: %1, new character set: %2\n"
"If you have recently changed your database character set, "
"you need not be concerned.\n"
"Please note that if you switched to a database character set "
"that does not support some of the filenames in your collection, "
"these files may no longer be found in the collection. "
"If you are sure that you want to "
"continue, click 'Yes'. "
"Otherwise, click 'No' and correct your "
"database character set setting before restarting digiKam.",
dbLocale, currLocale),
QMessageBox::Yes | QMessageBox::No);
if (result != QMessageBox::Yes)
{
return false;
}
CoreDbAccess().db()->setSetting(QLatin1String("Locale"), currLocale);
}
// -- UUID Checking ---------------------------------------------------------
QList<CollectionLocation> disappearedLocations = CollectionManager::instance()->checkHardWiredLocations();
for (const CollectionLocation& loc : std::as_const(disappearedLocations))
{
QString locDescription;
QStringList candidateIds, candidateDescriptions;
CollectionManager::instance()->migrationCandidates(loc, &locDescription, &candidateIds, &candidateDescriptions);
qCDebug(DIGIKAM_GENERAL_LOG) << "Migration candidates for" << locDescription << ":" << candidateIds << candidateDescriptions;
QDialog* const dialog = new QDialog;
QWidget* const widget = new QWidget(dialog);
QGridLayout* const mainLayout = new QGridLayout;
mainLayout->setColumnStretch(1, 1);
QLabel* const deviceIconLabel = new QLabel;
deviceIconLabel->setPixmap(QIcon::fromTheme(QLatin1String("drive-harddisk")).pixmap(64));
mainLayout->addWidget(deviceIconLabel, 0, 0);
QLabel* const mainLabel = new QLabel(i18n("<p>The collection </p><p><b>%1</b><br/>(%2)</p><p> is currently "
"not found on your system.<br/> Please choose the most "
"appropriate option to handle this situation:</p>",
loc.label(), QDir::toNativeSeparators(locDescription)));
mainLabel->setWordWrap(true);
mainLayout->addWidget(mainLabel, 0, 1);
QGroupBox* const groupBox = new QGroupBox;
mainLayout->addWidget(groupBox, 1, 0, 1, 2);
QGridLayout* const layout = new QGridLayout;
layout->setColumnStretch(1, 1);
QRadioButton* migrateButton = nullptr;
QComboBox* migrateChoices = nullptr;
if (!candidateIds.isEmpty())
{
migrateButton = new QRadioButton;
QLabel* const migrateLabel = new QLabel(i18n("<p>The collection is still available, but the identifier changed.<br/>"
"This can be caused by restoring a backup, changing the partition layout "
"or the file system settings.<br/>"
"The collection is now located at this place:</p>"));
migrateLabel->setWordWrap(true);
migrateChoices = new QComboBox;
for (int i = 0 ; i < candidateIds.size() ; ++i)
{
migrateChoices->addItem(QDir::toNativeSeparators(candidateDescriptions.at(i)), candidateIds.at(i));
}
layout->addWidget(migrateButton, 0, 0, Qt::AlignTop);
layout->addWidget(migrateLabel, 0, 1);
layout->addWidget(migrateChoices, 1, 1);
}
QRadioButton* const isRemovableButton = new QRadioButton;
QLabel* const isRemovableLabel = new QLabel(i18n("The collection is located on a storage device which is not "
"always attached. Mark the collection as a removable collection."));
isRemovableLabel->setWordWrap(true);
layout->addWidget(isRemovableButton, 2, 0, Qt::AlignTop);
layout->addWidget(isRemovableLabel, 2, 1);
QRadioButton* const solveManuallyButton = new QRadioButton;
QLabel* const solveManuallyLabel = new QLabel(i18n("Take no action now. I would like to solve the problem "
"later using the setup dialog"));
solveManuallyLabel->setWordWrap(true);
layout->addWidget(solveManuallyButton, 3, 0, Qt::AlignTop);
layout->addWidget(solveManuallyLabel, 3, 1);
groupBox->setLayout(layout);
widget->setLayout(mainLayout);
QVBoxLayout* const vbx = new QVBoxLayout(dialog);
QDialogButtonBox* const buttons = new QDialogButtonBox(QDialogButtonBox::Ok, dialog);
vbx->addWidget(widget);
vbx->addWidget(buttons);
dialog->setLayout(vbx);
dialog->setWindowTitle(i18nc("@title:window", "Collection not Found"));
connect(buttons->button(QDialogButtonBox::Ok), SIGNAL(clicked()),
dialog, SLOT(accept()));
// Default option: If there is only one candidate, default to migration.
// Otherwise default to do nothing now.
if (migrateButton && (candidateIds.size() == 1))
{
migrateButton->setChecked(true);
}
else
{
solveManuallyButton->setChecked(true);
}
if (!ignoreDisappearedLocations && dialog->exec())
{
if (migrateButton && migrateButton->isChecked())
{
CollectionManager::instance()->migrateToVolume(loc, migrateChoices->itemData(migrateChoices->currentIndex()).toString());
}
else if (isRemovableButton->isChecked())
{
CollectionManager::instance()->changeType(loc, CollectionLocation::VolumeRemovable);
}
}
delete dialog;
}
// -- ---------------------------------------------------------
// check that we have one album root
if (CollectionManager::instance()->allLocations().isEmpty())
{
if (suggestedAlbumRoot.isEmpty())
{
Setup::execSinglePage(Setup::CollectionsPage);
}
else
{
QUrl albumRoot(QUrl::fromLocalFile(suggestedAlbumRoot));
CollectionManager::instance()->addLocation(albumRoot, albumRoot.fileName());
// Not needed? See bug #188959
/*
ScanController::instance()->completeCollectionScan();
*/
}
}
// -- ---------------------------------------------------------
QApplication::setOverrideCursor(Qt::WaitCursor);
ThumbnailLoadThread::initializeThumbnailDatabase(CoreDbAccess::parameters().thumbnailParameters(),
new ThumbsDbInfoProvider());
DbEngineGuiErrorHandler* const thumbnailsDBHandler = new DbEngineGuiErrorHandler(ThumbsDbAccess::parameters());
ThumbsDbAccess::initDbEngineErrorHandler(thumbnailsDBHandler);
// Activate the similarity database.
SimilarityDbAccess::setParameters(params.similarityParameters());
DbEngineGuiErrorHandler* const similarityHandler = new DbEngineGuiErrorHandler(SimilarityDbAccess::parameters());
SimilarityDbAccess::initDbEngineErrorHandler(similarityHandler);
if (SimilarityDbAccess::checkReadyForUse(nullptr))
{
qCDebug(DIGIKAM_SIMILARITYDB_LOG) << "Similarity database ready for use";
}
else
{
qCDebug(DIGIKAM_SIMILARITYDB_LOG) << "Failed to initialize similarity database";
}
QApplication::restoreOverrideCursor();
// still suspended from above
ScanController::instance()->restartCollectionScan();
return true;
}
void AlbumManager::checkDatabaseDirsAfterFirstRun(const QString& dbPath, const QString& albumPath)
{
// for bug #193522
QDir newDir(dbPath);
QDir albumDir(albumPath);
DbEngineParameters newParams = DbEngineParameters::parametersForSQLiteDefaultFile(newDir.path());
QFileInfo digikam4DB(newParams.SQLiteDatabaseFile());
if (!digikam4DB.exists())
{
QFileInfo digikam3DB(newDir, QLatin1String("digikam3.db"));
QFileInfo digikamVeryOldDB(newDir, QLatin1String("digikam.db"));
if (digikam3DB.exists() || digikamVeryOldDB.exists())
{
QPointer<QMessageBox> msgBox = new QMessageBox(QMessageBox::Warning,
i18nc("@title:window", "Database Folder"),
i18n("<p>You have chosen the folder \"%1\" as the place to store the database. "
"A database file from an older version of digiKam is found in this folder.</p> "
"<p>Would you like to upgrade the old database file - confirming "
"that this database file was indeed created for the pictures located in the folder \"%2\" - "
"or ignore the old file and start with a new database?</p> ",
QDir::toNativeSeparators(newDir.path()),
QDir::toNativeSeparators(albumDir.path())),
QMessageBox::Yes | QMessageBox::No,
qApp->activeWindow());
msgBox->button(QMessageBox::Yes)->setText(i18nc("@action:button", "Upgrade Database"));
msgBox->button(QMessageBox::Yes)->setIcon(QIcon::fromTheme(QLatin1String("view-refresh")));
msgBox->button(QMessageBox::No)->setText(i18nc("@action:button", "Create New Database"));
msgBox->button(QMessageBox::No)->setIcon(QIcon::fromTheme(QLatin1String("document-new")));
msgBox->setDefaultButton(QMessageBox::Yes);
int result = msgBox->exec();
if (result == QMessageBox::Yes)
{
// CoreDbSchemaUpdater expects Album Path to point to the album root of the 0.9 db file.
// Restore this situation.
KSharedConfigPtr config = KSharedConfig::openConfig();
KConfigGroup group = config->group(QLatin1String("Album Settings"));
group.writeEntry("Album Path", albumDir.path());
group.sync();
}
else if (result == QMessageBox::No)
{
moveToBackup(digikam3DB);
moveToBackup(digikamVeryOldDB);
}
delete msgBox;
}
}
}
void AlbumManager::changeDatabase(const DbEngineParameters& newParams)
{
ScanController::instance()->shutDown();
// if there is no file at the new place, copy old one
DbEngineParameters params = CoreDbAccess::parameters();
// New database type SQLITE
if (newParams.isSQLite())
{
const bool dbNameChanged = (params.getCoreDatabaseNameOrDir() != newParams.getCoreDatabaseNameOrDir());
QDir newDir(newParams.getCoreDatabaseNameOrDir());
QFileInfo newFile(newDir, QLatin1String("digikam4.db"));
if (!newFile.exists() && dbNameChanged)
{
QFileInfo digikam3DB(newDir, QLatin1String("digikam3.db"));
QFileInfo digikamVeryOldDB(newDir, QLatin1String("digikam.db"));
if (digikam3DB.exists() || digikamVeryOldDB.exists())
{
int result = -1;
if (params.isSQLite())
{
QPointer<QMessageBox> msgBox = new QMessageBox(QMessageBox::Warning,
i18nc("@title:window", "New Database Folder"),
i18n("<p>You have chosen the folder \"%1\" as the new place to store the database. "
"A database file from an older version of digiKam is found in this folder.</p> "
"<p>Would you like to upgrade the old database file, start with a new database, "
"or copy the current database to this location and continue using it?</p> ",
QDir::toNativeSeparators(newDir.path())),
QMessageBox::Yes | QMessageBox::No | QMessageBox::Cancel,
qApp->activeWindow());
msgBox->button(QMessageBox::Yes)->setText(i18nc("@action:button", "Upgrade Database"));
msgBox->button(QMessageBox::Yes)->setIcon(QIcon::fromTheme(QLatin1String("view-refresh")));
msgBox->button(QMessageBox::No)->setText(i18nc("@action:button", "Create New Database"));
msgBox->button(QMessageBox::No)->setIcon(QIcon::fromTheme(QLatin1String("document-new")));
msgBox->button(QMessageBox::Cancel)->setText(i18nc("@action:button", "Copy Current Database"));
msgBox->button(QMessageBox::Cancel)->setIcon(QIcon::fromTheme(QLatin1String("edit-copy")));
msgBox->setDefaultButton(QMessageBox::Yes);
result = msgBox->exec();
delete msgBox;
}
else
{
QPointer<QMessageBox> msgBox = new QMessageBox(QMessageBox::Warning,
i18nc("@title:window", "New Database Folder"),
i18n("<p>You have chosen the folder \"%1\" as the new place to store the database. "
"A database file from an older version of digiKam is found in this folder.</p> "
"<p>Would you like to upgrade the old database file or start with a new database?</p>",
QDir::toNativeSeparators(newDir.path())),
QMessageBox::Yes | QMessageBox::No,
qApp->activeWindow());
msgBox->button(QMessageBox::Yes)->setText(i18nc("@action:button", "Upgrade Database"));
msgBox->button(QMessageBox::Yes)->setIcon(QIcon::fromTheme(QLatin1String("view-refresh")));
msgBox->button(QMessageBox::No)->setText(i18nc("@action:button", "Create New Database"));
msgBox->button(QMessageBox::No)->setIcon(QIcon::fromTheme(QLatin1String("document-new")));
msgBox->setDefaultButton(QMessageBox::Yes);
result = msgBox->exec();
delete msgBox;
}
if (result == QMessageBox::Yes)
{
// CoreDbSchemaUpdater expects Album Path to point to the album root of the 0.9 db file.
// Restore this situation.
KSharedConfigPtr config = KSharedConfig::openConfig();
KConfigGroup group = config->group(QLatin1String("Album Settings"));
group.writeEntry(QLatin1String("Album Path"), newDir.path());
group.sync();
}
else if (result == QMessageBox::No)
{
moveToBackup(digikam3DB);
moveToBackup(digikamVeryOldDB);
}
else if (result == QMessageBox::Cancel)
{
QFileInfo oldFile(params.SQLiteDatabaseFile());
copyToNewLocation(oldFile, newFile, i18n("Failed to copy the old database file (\"%1\") "
"to its new location (\"%2\"). "
"Trying to upgrade old databases.",
QDir::toNativeSeparators(oldFile.filePath()),
QDir::toNativeSeparators(newFile.filePath())));
}
}
else
{
int result = QMessageBox::Yes;
if (params.isSQLite())
{
QPointer<QMessageBox> msgBox = new QMessageBox(QMessageBox::Warning,
i18nc("@title:window", "New Database Folder"),
i18n("<p>You have chosen the folder \"%1\" as the new place to store the database.</p>"
"<p>Would you like to copy the current database to this location "
"and continue using it, or start with a new database?</p> ",
QDir::toNativeSeparators(newDir.path())),
QMessageBox::Yes | QMessageBox::No,
qApp->activeWindow());
msgBox->button(QMessageBox::Yes)->setText(i18nc("@action:button", "Create New Database"));
msgBox->button(QMessageBox::Yes)->setIcon(QIcon::fromTheme(QLatin1String("document-new")));
msgBox->button(QMessageBox::No)->setText(i18nc("@action:button", "Copy Current Database"));
msgBox->button(QMessageBox::No)->setIcon(QIcon::fromTheme(QLatin1String("edit-copy")));
msgBox->setDefaultButton(QMessageBox::Yes);
result = msgBox->exec();
delete msgBox;
}
if (result == QMessageBox::No)
{
QFileInfo oldFile(params.SQLiteDatabaseFile());
copyToNewLocation(oldFile, newFile);
}
}
}
else if (dbNameChanged)
{
int result = QMessageBox::No;
if (params.isSQLite())
{
QPointer<QMessageBox> msgBox = new QMessageBox(QMessageBox::Warning,
i18nc("@title:window", "New Database Folder"),
i18n("<p>You have chosen the folder \"%1\" as the new place to store the database. "
"There is already a database file in this location.</p> "
"<p>Would you like to use this existing file as the new database, or remove it "
"and copy the current database to this place?</p> ",
QDir::toNativeSeparators(newDir.path())),
QMessageBox::Yes | QMessageBox::No,
qApp->activeWindow());
msgBox->button(QMessageBox::Yes)->setText(i18nc("@action:button", "Copy Current Database"));
msgBox->button(QMessageBox::Yes)->setIcon(QIcon::fromTheme(QLatin1String("edit-copy")));
msgBox->button(QMessageBox::No)->setText(i18nc("@action:button", "Use Existing File"));
msgBox->button(QMessageBox::No)->setIcon(QIcon::fromTheme(QLatin1String("document-open")));
msgBox->setDefaultButton(QMessageBox::Yes);
result = msgBox->exec();
delete msgBox;
}
if (result == QMessageBox::Yes)
{
// first backup
if (moveToBackup(newFile))
{
QFileInfo oldFile(params.SQLiteDatabaseFile());
// then copy
copyToNewLocation(oldFile, newFile);
}
}
}
}
ScanController::instance()->restart();
if (setDatabase(newParams, false))
{
QApplication::setOverrideCursor(Qt::WaitCursor);
startScan();
QApplication::restoreOverrideCursor();
ScanController::instance()->completeCollectionScan();
}
}
bool AlbumManager::databaseEqual(const DbEngineParameters& parameters) const
{
DbEngineParameters params = CoreDbAccess::parameters();
return (params == parameters);
}
bool AlbumManager::moveToBackup(const QFileInfo& info)
{
if (info.exists())
{
QFileInfo backup(info.dir(), info.fileName() +
QLatin1String("-backup-") +
QDateTime::currentDateTime().toString(Qt::ISODate));
bool ret = QDir().rename(info.filePath(), backup.filePath());
if (!ret)
{
QMessageBox::critical(qApp->activeWindow(), qApp->applicationName(),
i18n("Failed to backup the existing database file (\"%1\"). "
"Refusing to replace file without backup, using the existing file.",
QDir::toNativeSeparators(info.filePath())));
return false;
}
}
return true;
}
bool AlbumManager::copyToNewLocation(const QFileInfo& oldFile,
const QFileInfo& newFile,
const QString& otherMessage)
{
QString message = otherMessage;
if (message.isNull())
{
message = i18n("Failed to copy the old database file (\"%1\") "
"to its new location (\"%2\"). "
"Starting with an empty database.",
QDir::toNativeSeparators(oldFile.filePath()),
QDir::toNativeSeparators(newFile.filePath()));
}
bool ret = QFile::copy(oldFile.filePath(), newFile.filePath());
if (!ret)
{
QMessageBox::critical(qApp->activeWindow(), qApp->applicationName(), message);
return false;
}
return true;
}
bool AlbumManager::showDatabaseSetupPage(const QString& error, bool priority, const QString& suggestedAlbumRoot)
{
QApplication::restoreOverrideCursor();
QString errorMsg = error + i18n("<p><b>Please check the database settings in this dialog.</b></p>");
// We cannot use Setup::execSinglePage() as this already requires a core database.
QPointer<QDialog> setup = new QDialog(qApp->activeWindow());
QVBoxLayout* const layout = new QVBoxLayout(setup);
QLabel* const errorLabel = new QLabel(errorMsg, setup);
errorLabel->setWordWrap(true);
errorLabel->setTextFormat(Qt::RichText);
errorLabel->setAlignment(Qt::AlignCenter);
errorLabel->setFrameStyle(QFrame::StyledPanel | QFrame::Raised);
errorLabel->setStyleSheet(QLatin1String("QLabel { background-color: #FFDDDD; color: black; }"));
DatabaseSettingsWidget* const dbsettings = new DatabaseSettingsWidget(setup);
QDialogButtonBox* const buttons = new QDialogButtonBox(QDialogButtonBox::Ok |
QDialogButtonBox::Reset |
QDialogButtonBox::Cancel, setup);
buttons->button(QDialogButtonBox::Reset)->setText(i18nc("@action:button", "New database"));
buttons->button(QDialogButtonBox::Ok)->setDefault(true);
layout->addWidget(errorLabel);
layout->addWidget(dbsettings);
layout->addStretch(10);
layout->addWidget(buttons);
bool newDatabase = false;
connect(buttons->button(QDialogButtonBox::Ok), SIGNAL(clicked()),
setup, SLOT(accept()));
connect(buttons->button(QDialogButtonBox::Cancel), SIGNAL(clicked()),
setup, SLOT(reject()));
connect(buttons->button(QDialogButtonBox::Reset), &QPushButton::clicked,
this, [&newDatabase, setup]() // clazy:exclude=lambda-in-connect
{
newDatabase = true;
setup->accept();
}
);
ApplicationSettings* const settings = ApplicationSettings::instance();
dbsettings->setParametersFromSettings(settings);
if (setup->exec() != QDialog::Accepted)
{
delete setup;
return false;
}
DbEngineParameters dbParams = dbsettings->getDbEngineParameters();
settings->setDbEngineParameters(dbParams);
settings->saveSettings();
delete setup;
if (newDatabase)
{
if (dbParams.internalServer)
{
DatabaseServerError result = DatabaseServerStarter::instance()->startServerManagerProcess(dbParams);
if (result.getErrorType() != DatabaseServerError::NoErrors)
{
return false;
}
}
CoreDbAccess::setParameters(dbParams);
if (!CoreDbAccess::checkReadyForUse())
{
return false;
}
}
return (setDatabase(dbParams, priority, suggestedAlbumRoot));
}
} // namespace Digikam
|