1 /*
2   SPDX-FileCopyrightText: 2014-2021 Laurent Montel <montel@kde.org>
3 
4   SPDX-License-Identifier: GPL-2.0-or-later
5 */
6 
7 #include "akonadisearchdebugsearchpathcombobox.h"
8 #include <Akonadi/ServerManager>
9 #include <QDir>
10 #include <QStandardPaths>
11 
12 using namespace Akonadi::Search;
AkonadiSearchDebugSearchPathComboBox(QWidget * parent)13 AkonadiSearchDebugSearchPathComboBox::AkonadiSearchDebugSearchPathComboBox(QWidget *parent)
14     : QComboBox(parent)
15 {
16     initialize();
17 }
18 
~AkonadiSearchDebugSearchPathComboBox()19 AkonadiSearchDebugSearchPathComboBox::~AkonadiSearchDebugSearchPathComboBox()
20 {
21 }
22 
searchPath()23 QString AkonadiSearchDebugSearchPathComboBox::searchPath()
24 {
25     const int currentPathIndex = currentIndex();
26     if (currentPathIndex > -1) {
27         const QString value = pathFromEnum(static_cast<Akonadi::Search::AkonadiSearchDebugSearchPathComboBox::SearchType>(itemData(currentPathIndex).toInt()));
28         return value;
29     } else {
30         return QString();
31     }
32 }
33 
initialize()34 void AkonadiSearchDebugSearchPathComboBox::initialize()
35 {
36     addItem(QStringLiteral("Contacts"), Contacts);
37     addItem(QStringLiteral("ContactCompleter"), ContactCompleter);
38     addItem(QStringLiteral("Email"), Emails);
39     addItem(QStringLiteral("Notes"), Notes);
40     addItem(QStringLiteral("Calendars"), Calendars);
41 }
42 
pathFromEnum(SearchType type)43 QString AkonadiSearchDebugSearchPathComboBox::pathFromEnum(SearchType type)
44 {
45     switch (type) {
46     case Contacts:
47         return defaultLocations(QStringLiteral("contacts"));
48     case ContactCompleter:
49         return defaultLocations(QStringLiteral("emailContacts"));
50     case Emails:
51         return defaultLocations(QStringLiteral("email"));
52     case Notes:
53         return defaultLocations(QStringLiteral("notes"));
54     case Calendars:
55         return defaultLocations(QStringLiteral("calendars"));
56     }
57     return QString();
58 }
59 
setSearchType(AkonadiSearchDebugSearchPathComboBox::SearchType type)60 void AkonadiSearchDebugSearchPathComboBox::setSearchType(AkonadiSearchDebugSearchPathComboBox::SearchType type)
61 {
62     const int indexType = findData(type);
63     if (indexType >= 0) {
64         setCurrentIndex(indexType);
65     }
66 }
67 
defaultLocations(const QString & dbName)68 QString AkonadiSearchDebugSearchPathComboBox::defaultLocations(const QString &dbName)
69 {
70     // First look into the old location from Baloo times in ~/.local/share/baloo,
71     // because we don't migrate the database files automatically.
72     QString basePath;
73     bool hasInstanceIdentifier = Akonadi::ServerManager::hasInstanceIdentifier();
74     if (hasInstanceIdentifier) {
75         basePath = QStringLiteral("baloo/instances/%1").arg(Akonadi::ServerManager::instanceIdentifier());
76     } else {
77         basePath = QStringLiteral("baloo");
78     }
79     QString dbPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/%1/%2/").arg(basePath, dbName);
80     if (QDir(dbPath).exists()) {
81         return dbPath;
82     }
83 
84     // If the database does not exist in old Baloo folders, than use the new
85     // location in Akonadi's datadir in ~/.local/share/akonadi/search_db.
86     if (hasInstanceIdentifier) {
87         basePath = QStringLiteral("akonadi/instance/%1/search_db").arg(Akonadi::ServerManager::instanceIdentifier());
88     } else {
89         basePath = QStringLiteral("akonadi/search_db");
90     }
91     dbPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/%1/%2/").arg(basePath, dbName);
92     QDir().mkpath(dbPath);
93     return dbPath;
94 }
95