File indexing completed on 2024-05-19 05:11:51

0001 /*
0002   SPDX-FileCopyrightText: 2014-2024 Laurent Montel <montel@kde.org>
0003 
0004   SPDX-License-Identifier: GPL-2.0-or-later
0005 */
0006 
0007 #include "akonadisearchdebugsearchpathcombobox.h"
0008 #include <Akonadi/ServerManager>
0009 #include <QDir>
0010 #include <QStandardPaths>
0011 
0012 using namespace Akonadi::Search;
0013 AkonadiSearchDebugSearchPathComboBox::AkonadiSearchDebugSearchPathComboBox(QWidget *parent)
0014     : QComboBox(parent)
0015 {
0016     initialize();
0017 }
0018 
0019 AkonadiSearchDebugSearchPathComboBox::~AkonadiSearchDebugSearchPathComboBox() = default;
0020 
0021 QString AkonadiSearchDebugSearchPathComboBox::searchPath() const
0022 {
0023     const int currentPathIndex = currentIndex();
0024     if (currentPathIndex > -1) {
0025         const QString value = pathFromEnum(static_cast<Akonadi::Search::AkonadiSearchDebugSearchPathComboBox::SearchType>(itemData(currentPathIndex).toInt()));
0026         return value;
0027     } else {
0028         return {};
0029     }
0030 }
0031 
0032 void AkonadiSearchDebugSearchPathComboBox::initialize()
0033 {
0034     addItem(QStringLiteral("Contacts"), Contacts);
0035     addItem(QStringLiteral("ContactCompleter"), ContactCompleter);
0036     addItem(QStringLiteral("Email"), Emails);
0037     addItem(QStringLiteral("Notes"), Notes);
0038     addItem(QStringLiteral("Calendars"), Calendars);
0039 }
0040 
0041 QString AkonadiSearchDebugSearchPathComboBox::pathFromEnum(SearchType type) const
0042 {
0043     switch (type) {
0044     case Contacts:
0045         return defaultLocations(QStringLiteral("contacts"));
0046     case ContactCompleter:
0047         return defaultLocations(QStringLiteral("emailContacts"));
0048     case Emails:
0049         return defaultLocations(QStringLiteral("email"));
0050     case Notes:
0051         return defaultLocations(QStringLiteral("notes"));
0052     case Calendars:
0053         return defaultLocations(QStringLiteral("calendars"));
0054     }
0055     return {};
0056 }
0057 
0058 void AkonadiSearchDebugSearchPathComboBox::setSearchType(AkonadiSearchDebugSearchPathComboBox::SearchType type)
0059 {
0060     const int indexType = findData(type);
0061     if (indexType >= 0) {
0062         setCurrentIndex(indexType);
0063     }
0064 }
0065 
0066 const QString AkonadiSearchDebugSearchPathComboBox::defaultLocations(const QString &dbName) const
0067 {
0068     // First look into the old location from Baloo times in ~/.local/share/baloo,
0069     // because we don't migrate the database files automatically.
0070     QString basePath;
0071     bool hasInstanceIdentifier = Akonadi::ServerManager::hasInstanceIdentifier();
0072     if (hasInstanceIdentifier) {
0073         basePath = QStringLiteral("baloo/instances/%1").arg(Akonadi::ServerManager::instanceIdentifier());
0074     } else {
0075         basePath = QStringLiteral("baloo");
0076     }
0077     QString dbPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/%1/%2/").arg(basePath, dbName);
0078     if (QDir(dbPath).exists()) {
0079         return dbPath;
0080     }
0081 
0082     // If the database does not exist in old Baloo folders, than use the new
0083     // location in Akonadi's datadir in ~/.local/share/akonadi/search_db.
0084     if (hasInstanceIdentifier) {
0085         basePath = QStringLiteral("akonadi/instance/%1/search_db").arg(Akonadi::ServerManager::instanceIdentifier());
0086     } else {
0087         basePath = QStringLiteral("akonadi/search_db");
0088     }
0089     dbPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/%1/%2/").arg(basePath, dbName);
0090     QDir().mkpath(dbPath);
0091     return dbPath;
0092 }
0093 
0094 #include "moc_akonadisearchdebugsearchpathcombobox.cpp"