Akonadi

dbconfig.cpp
1 /*
2  SPDX-FileCopyrightText: 2010 Tobias Koenig <[email protected]>
3 
4  SPDX-License-Identifier: LGPL-2.0-or-later
5 */
6 
7 #include "dbconfig.h"
8 
9 #include "akonadiserver_debug.h"
10 #include "dbconfigmysql.h"
11 #include "dbconfigpostgresql.h"
12 #include "dbconfigsqlite.h"
13 
14 #include <config-akonadi.h>
15 
16 #include <private/instance_p.h>
17 #include <private/standarddirs_p.h>
18 
19 #include <QProcess>
20 #include <memory>
21 
22 using namespace Akonadi;
23 using namespace Akonadi::Server;
24 
25 // TODO: make me Q_GLOBAL_STATIC
26 static DbConfig *s_DbConfigInstance = nullptr;
27 
28 DbConfig::DbConfig()
29 {
30  const QString serverConfigFile = StandardDirs::serverConfigFile(StandardDirs::ReadWrite);
31  QSettings settings(serverConfigFile, QSettings::IniFormat);
32 
33  mSizeThreshold = 4096;
34  const QVariant value = settings.value(QStringLiteral("General/SizeThreshold"), mSizeThreshold);
35  if (value.canConvert<qint64>()) {
36  mSizeThreshold = value.value<qint64>();
37  if (mSizeThreshold < 0) {
38  mSizeThreshold = 0;
39  }
40  } else {
41  mSizeThreshold = 0;
42  }
43 }
44 
45 DbConfig::~DbConfig()
46 {
47 }
48 
50 {
51  return s_DbConfigInstance;
52 }
53 
54 QString DbConfig::defaultAvailableDatabaseBackend(QSettings &settings)
55 {
56  QString driverName = QStringLiteral(AKONADI_DATABASE_BACKEND);
57 
58  std::unique_ptr<DbConfig> dbConfigFallbackTest;
59  if (driverName == QLatin1String("QMYSQL")) {
60  dbConfigFallbackTest = std::make_unique<DbConfigMysql>();
61  } else if (driverName == QLatin1String("QPSQL")) {
62  dbConfigFallbackTest = std::make_unique<DbConfigPostgresql>();
63  }
64 
65  if (dbConfigFallbackTest && !dbConfigFallbackTest->isAvailable(settings) && DbConfigSqlite(DbConfigSqlite::Custom).isAvailable(settings)) {
66  qCWarning(AKONADISERVER_LOG) << driverName << " requirements not available. Falling back to using QSQLITE3.";
67  driverName = QStringLiteral("QSQLITE3");
68  }
69 
70  return driverName;
71 }
72 
74 {
75  if (!s_DbConfigInstance) {
76  const QString serverConfigFile = StandardDirs::serverConfigFile(StandardDirs::ReadWrite);
77  QSettings settings(serverConfigFile, QSettings::IniFormat);
78 
79  // determine driver to use
80  QString driverName = settings.value(QStringLiteral("General/Driver")).toString();
81  if (driverName.isEmpty()) {
82  driverName = defaultAvailableDatabaseBackend(settings);
83 
84  // when using the default, write it explicitly, in case the default changes later
85  settings.setValue(QStringLiteral("General/Driver"), driverName);
86  settings.sync();
87  }
88 
89  if (driverName == QLatin1String("QMYSQL")) {
90  s_DbConfigInstance = new DbConfigMysql;
91  } else if (driverName == QLatin1String("QSQLITE")) {
92  s_DbConfigInstance = new DbConfigSqlite(DbConfigSqlite::Default);
93  } else if (driverName == QLatin1String("QSQLITE3")) {
94  s_DbConfigInstance = new DbConfigSqlite(DbConfigSqlite::Custom);
95  } else if (driverName == QLatin1String("QPSQL")) {
96  s_DbConfigInstance = new DbConfigPostgresql;
97  } else {
98  qCCritical(AKONADISERVER_LOG) << "Unknown database driver: " << driverName;
99  qCCritical(AKONADISERVER_LOG) << "Available drivers are: " << QSqlDatabase::drivers();
100  return nullptr;
101  }
102 
103  if (!s_DbConfigInstance->init(settings)) {
104  delete s_DbConfigInstance;
105  s_DbConfigInstance = nullptr;
106  }
107  }
108 
109  return s_DbConfigInstance;
110 }
111 
113 {
114  // do nothing
115  return true;
116 }
117 
119 {
120  // do nothing
121 }
122 
124 {
125  // do nothing
126 }
127 
129 {
130  return mSizeThreshold;
131 }
132 
134 {
135  if (!Instance::hasIdentifier()) {
136  return QStringLiteral("akonadi");
137  }
138  // dash is not allowed in PSQL
139  return QLatin1String("akonadi_") % Instance::identifier().replace(QLatin1Char('-'), QLatin1Char('_'));
140 }
141 
142 void DbConfig::initSession(const QSqlDatabase &database)
143 {
144  Q_UNUSED(database)
145 }
146 
147 int DbConfig::execute(const QString &cmd, const QStringList &args) const
148 {
149  qCDebug(AKONADISERVER_LOG) << "Executing: " << cmd << args.join(QLatin1Char(' '));
150  return QProcess::execute(cmd, args);
151 }
virtual bool startInternalServer()
This method is called to start an external server.
Definition: dbconfig.cpp:112
void sync()
T value() const const
static DbConfig * configuredDatabase()
Returns the DbConfig instance for the database the user has configured.
Definition: dbconfig.cpp:73
void setValue(const QString &key, const QVariant &value)
virtual QString driverName() const =0
Returns the name of the used driver.
bool isEmpty() const const
int execute(const QString &program, const QStringList &arguments)
QVariant value(const QString &key, const QVariant &defaultValue) const const
QString join(const QString &separator) const const
virtual bool init(QSettings &settings, bool storeSettings=true)=0
This method is called whenever the Akonadi server is started and before the initial database connecti...
virtual qint64 sizeThreshold() const
Payload data bigger than this value will be stored in separate files, instead of the database.
Definition: dbconfig.cpp:128
bool canConvert(int targetTypeId) const const
static QString defaultDatabaseName()
Returns the suggested default database name, if none is specified in the configuration already.
Definition: dbconfig.cpp:133
virtual void setup()
This method is called to setup initial database settings after a connection is established.
Definition: dbconfig.cpp:123
int execute(const QString &cmd, const QStringList &args) const
Calls QProcess::execute() and also prints the command and arguments via qCDebug()
Definition: dbconfig.cpp:147
virtual bool isAvailable(QSettings &settings)=0
This method checks if the requirements for this database connection are met in the system (i....
static bool isConfigured()
Returns whether database have been configured.
Definition: dbconfig.cpp:49
virtual void initSession(const QSqlDatabase &database)
Do session setup/initialization work on database.
Definition: dbconfig.cpp:142
A base class that provides an unique access layer to configuration and initialization of different da...
Definition: dbconfig.h:20
QStringList drivers()
QString toString() const const
virtual void stopInternalServer()
This method is called to stop the external server.
Definition: dbconfig.cpp:118
Helper integration between Akonadi and Qt.
This file is part of the KDE documentation.
Documentation copyright © 1996-2022 The KDE developers.
Generated on Sat Jul 2 2022 06:41:47 by doxygen 1.8.17 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.