Akonadi Search

akonadisearchdebugsearchpathcombobox.cpp
1 /*
2  SPDX-FileCopyrightText: 2014-2023 Laurent Montel <[email protected]>
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;
13 AkonadiSearchDebugSearchPathComboBox::AkonadiSearchDebugSearchPathComboBox(QWidget *parent)
14  : QComboBox(parent)
15 {
16  initialize();
17 }
18 
19 AkonadiSearchDebugSearchPathComboBox::~AkonadiSearchDebugSearchPathComboBox() = default;
20 
21 QString AkonadiSearchDebugSearchPathComboBox::searchPath() const
22 {
23  const int currentPathIndex = currentIndex();
24  if (currentPathIndex > -1) {
25  const QString value = pathFromEnum(static_cast<Akonadi::Search::AkonadiSearchDebugSearchPathComboBox::SearchType>(itemData(currentPathIndex).toInt()));
26  return value;
27  } else {
28  return {};
29  }
30 }
31 
32 void AkonadiSearchDebugSearchPathComboBox::initialize()
33 {
34  addItem(QStringLiteral("Contacts"), Contacts);
35  addItem(QStringLiteral("ContactCompleter"), ContactCompleter);
36  addItem(QStringLiteral("Email"), Emails);
37  addItem(QStringLiteral("Notes"), Notes);
38  addItem(QStringLiteral("Calendars"), Calendars);
39 }
40 
41 QString AkonadiSearchDebugSearchPathComboBox::pathFromEnum(SearchType type) const
42 {
43  switch (type) {
44  case Contacts:
45  return defaultLocations(QStringLiteral("contacts"));
46  case ContactCompleter:
47  return defaultLocations(QStringLiteral("emailContacts"));
48  case Emails:
49  return defaultLocations(QStringLiteral("email"));
50  case Notes:
51  return defaultLocations(QStringLiteral("notes"));
52  case Calendars:
53  return defaultLocations(QStringLiteral("calendars"));
54  }
55  return {};
56 }
57 
58 void AkonadiSearchDebugSearchPathComboBox::setSearchType(AkonadiSearchDebugSearchPathComboBox::SearchType type)
59 {
60  const int indexType = findData(type);
61  if (indexType >= 0) {
62  setCurrentIndex(indexType);
63  }
64 }
65 
66 const QString AkonadiSearchDebugSearchPathComboBox::defaultLocations(const QString &dbName) const
67 {
68  // First look into the old location from Baloo times in ~/.local/share/baloo,
69  // because we don't migrate the database files automatically.
70  QString basePath;
71  bool hasInstanceIdentifier = Akonadi::ServerManager::hasInstanceIdentifier();
72  if (hasInstanceIdentifier) {
73  basePath = QStringLiteral("baloo/instances/%1").arg(Akonadi::ServerManager::instanceIdentifier());
74  } else {
75  basePath = QStringLiteral("baloo");
76  }
77  QString dbPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/%1/%2/").arg(basePath, dbName);
78  if (QDir(dbPath).exists()) {
79  return dbPath;
80  }
81 
82  // If the database does not exist in old Baloo folders, than use the new
83  // location in Akonadi's datadir in ~/.local/share/akonadi/search_db.
84  if (hasInstanceIdentifier) {
85  basePath = QStringLiteral("akonadi/instance/%1/search_db").arg(Akonadi::ServerManager::instanceIdentifier());
86  } else {
87  basePath = QStringLiteral("akonadi/search_db");
88  }
89  dbPath = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + QStringLiteral("/%1/%2/").arg(basePath, dbName);
90  QDir().mkpath(dbPath);
91  return dbPath;
92 }
93 
94 #include "moc_akonadisearchdebugsearchpathcombobox.cpp"
int findData(const QVariant &data, int role, Qt::MatchFlags flags) const const
QString writableLocation(QStandardPaths::StandardLocation type)
void initialize(StandardShortcut id)
Akonadi search infrastructure.
Definition: core/query.h:20
bool mkpath(const QString &dirPath) const const
static QString instanceIdentifier()
QVariant itemData(int index, int role) const const
QString arg(qlonglong a, int fieldWidth, int base, QChar fillChar) const const
void addItem(const QString &text, const QVariant &userData)
static bool hasInstanceIdentifier()
This file is part of the KDE documentation.
Documentation copyright © 1996-2023 The KDE developers.
Generated on Tue Nov 28 2023 04:11:42 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.