File indexing completed on 2025-01-19 03:53:49

0001 /* ============================================================
0002  *
0003  * This file is a part of digiKam project
0004  * https://www.digikam.org
0005  *
0006  * Date        : 2009-05-28
0007  * Description : database statistics dialog
0008  *
0009  * SPDX-FileCopyrightText: 2009-2024 by Gilles Caulier <caulier dot gilles at gmail dot com>
0010  *
0011  * SPDX-License-Identifier: GPL-2.0-or-later
0012  *
0013  * ============================================================ */
0014 
0015 #include "dbstatdlg.h"
0016 
0017 // Qt includes
0018 
0019 #include <QStringList>
0020 #include <QString>
0021 #include <QFont>
0022 #include <QTreeWidget>
0023 #include <QApplication>
0024 
0025 // KDE includes
0026 
0027 #include <klocalizedstring.h>
0028 
0029 // Local includes
0030 
0031 #include "digikam_config.h"
0032 #include "digikam_globals.h"
0033 #include "daboutdata.h"
0034 #include "coredb.h"
0035 #include "applicationsettings.h"
0036 #include "coredbaccess.h"
0037 
0038 namespace Digikam
0039 {
0040 
0041 DBStatDlg::DBStatDlg(QWidget* const parent)
0042     : InfoDlg(parent)
0043 {
0044     qApp->setOverrideCursor(Qt::WaitCursor);
0045 
0046     setWindowTitle(i18nc("@title:window", "Database Statistics"));
0047     listView()->setHeaderLabels(QStringList() << i18n("Format") << i18n("Count"));
0048 
0049     // get image format statistics
0050 
0051     int totalImages = generateItemsList(DatabaseItem::Image, i18n("Images"));
0052     int totalVideos = generateItemsList(DatabaseItem::Video, i18n("Videos"));
0053     int totalAudio  = generateItemsList(DatabaseItem::Audio, i18n("Audio"));
0054     int total       = totalImages + totalVideos + totalAudio;
0055 
0056     // --------------------------------------------------------
0057 
0058     // To see total count of items at end of list.
0059 
0060     new QTreeWidgetItem(listView(), QStringList() << i18n("Total Items") << QString::number(total));
0061 
0062     // get album statistics
0063 
0064     int albums   = CoreDbAccess().db()->scanAlbums().count();
0065     new QTreeWidgetItem(listView(), QStringList() << i18n("Albums") << QString::number(albums));
0066 
0067     // get tags statistics
0068 
0069     int tags     = CoreDbAccess().db()->scanTags().count();
0070     new QTreeWidgetItem(listView(), QStringList() << i18n("Tags") << QString::number(tags));
0071 
0072     // Database Backend information
0073 
0074     new QTreeWidgetItem(listView(), QStringList());
0075     DbEngineParameters prm = ApplicationSettings::instance()->getDbEngineParameters();
0076     QString dbBe           = prm.databaseType;
0077     new QTreeWidgetItem(listView(), QStringList() << i18n("Database backend") << dbBe);
0078 
0079     if (dbBe == QLatin1String("QSQLITE"))
0080     {
0081         new QTreeWidgetItem(listView(), QStringList() << i18n("Database Path") << prm.getCoreDatabaseNameOrDir());
0082     }
0083     else
0084     {
0085         if (prm.internalServer)
0086         {
0087             new QTreeWidgetItem(listView(), QStringList() << i18n("Database internal server")      << i18nc("@info: database server", "Yes"));
0088             new QTreeWidgetItem(listView(), QStringList() << i18n("Database internal server Path") << prm.internalServerDBPath);
0089         }
0090         else
0091         {
0092             new QTreeWidgetItem(listView(), QStringList() << i18n("Host Name")       << prm.hostName);
0093             new QTreeWidgetItem(listView(), QStringList() << i18n("Host Port")       << QString::number(prm.port));
0094             new QTreeWidgetItem(listView(), QStringList() << i18n("User account")    << prm.userName);
0095             new QTreeWidgetItem(listView(), QStringList() << i18n("Connect options") << prm.connectOptions);
0096             new QTreeWidgetItem(listView(), QStringList() << i18n("Core Db name")    << prm.databaseNameCore);
0097             new QTreeWidgetItem(listView(), QStringList() << i18n("Thumbs Db name")  << prm.databaseNameThumbnails);
0098             new QTreeWidgetItem(listView(), QStringList() << i18n("Face Db name")    << prm.databaseNameFace);
0099         }
0100     }
0101 
0102     QString dbLocale = CoreDbAccess().db()->getSetting(QLatin1String("Locale"));
0103     new QTreeWidgetItem(listView(), QStringList() << i18n("Database locale") << dbLocale);
0104 
0105     qApp->restoreOverrideCursor();
0106 }
0107 
0108 DBStatDlg::~DBStatDlg()
0109 {
0110 }
0111 
0112 int DBStatDlg::generateItemsList(DatabaseItem::Category category, const QString& title)
0113 {
0114     // get image format statistics
0115 
0116     QMap<QString, int> stat = CoreDbAccess().db()->getFormatStatistics(category);
0117 
0118     // do not add items if the map is empty
0119 
0120     if (stat.isEmpty())
0121     {
0122         return 0;
0123     }
0124 
0125     int total = 0;
0126     QMap<QString, QString> map;
0127 
0128     for (QMap<QString, int>::const_iterator it = stat.constBegin() ; it != stat.constEnd() ; ++it)
0129     {
0130         total += it.value();
0131         map.insert(it.key(), QString::number(it.value()));
0132     }
0133 
0134     // --------------------------------------------------------
0135 
0136     QTreeWidgetItem* ti = new QTreeWidgetItem(listView(), QStringList() << title << QString());
0137     QFont ft            = ti->font(0);
0138     ft.setBold(true);
0139     ti->setFont(0, ft);
0140     ti->setFont(1, ft);
0141 
0142     setInfoMap(map);
0143 
0144     ti = new QTreeWidgetItem(listView(), QStringList() << i18nc("@info: total number of items", "total") << QString::number(total));
0145     ti->setFont(0, ft);
0146     ti->setFont(1, ft);
0147 
0148     // Add space.
0149 
0150     new QTreeWidgetItem(listView(), QStringList());
0151 
0152     return total;
0153 }
0154 
0155 void DBStatDlg::slotHelp()
0156 {
0157     openOnlineDocumentation(QLatin1String("setup_application"), QLatin1String("database_settings"), QLatin1String("database_stats"));
0158 }
0159 
0160 } // namespace Digikam
0161 
0162 #include "moc_dbstatdlg.cpp"