Akonadi

dbconfig.cpp
1 /*
2  Copyright (c) 2010 Tobias Koenig <[email protected]>
3 
4  This library is free software; you can redistribute it and/or modify it
5  under the terms of the GNU Library General Public License as published by
6  the Free Software Foundation; either version 2 of the License, or (at your
7  option) any later version.
8 
9  This library is distributed in the hope that it will be useful, but WITHOUT
10  ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11  FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public
12  License for more details.
13 
14  You should have received a copy of the GNU Library General Public License
15  along with this library; see the file COPYING.LIB. If not, write to the
16  Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
17  02110-1301, USA.
18 */
19 
20 #include "dbconfig.h"
21 
22 #include "dbconfigmysql.h"
23 #include "dbconfigpostgresql.h"
24 #include "dbconfigsqlite.h"
25 #include "akonadiserver_debug.h"
26 
27 #include <private/standarddirs_p.h>
28 #include <private/instance_p.h>
29 
30 #include <QProcess>
31 
32 using namespace Akonadi;
33 using namespace Akonadi::Server;
34 
35 //TODO: make me Q_GLOBAL_STATIC
36 static DbConfig *s_DbConfigInstance = nullptr;
37 
38 DbConfig::DbConfig()
39 {
40  const QString serverConfigFile = StandardDirs::serverConfigFile(StandardDirs::ReadWrite);
41  QSettings settings(serverConfigFile, QSettings::IniFormat);
42 
43  mSizeThreshold = 4096;
44  const QVariant value = settings.value(QStringLiteral("General/SizeThreshold"), mSizeThreshold);
45  if (value.canConvert<qint64>()) {
46  mSizeThreshold = value.value<qint64>();
47  if (mSizeThreshold < 0) {
48  mSizeThreshold = 0;
49  }
50  } else {
51  mSizeThreshold = 0;
52  }
53 
54 }
55 
56 DbConfig::~DbConfig()
57 {
58 }
59 
61 {
62  return s_DbConfigInstance;
63 }
64 
66 {
67  if (!s_DbConfigInstance) {
68  const QString serverConfigFile = StandardDirs::serverConfigFile(StandardDirs::ReadWrite);
69  QSettings settings(serverConfigFile, QSettings::IniFormat);
70 
71  // determine driver to use
72  QString driverName = settings.value(QStringLiteral("General/Driver")).toString();
73  if (driverName.isEmpty()) {
74  driverName = QStringLiteral(AKONADI_DATABASE_BACKEND);
75  // when using the default, write it explicitly, in case the default changes later
76  settings.setValue(QStringLiteral("General/Driver"), driverName);
77  settings.sync();
78  }
79 
80  if (driverName == QLatin1String("QMYSQL")) {
81  s_DbConfigInstance = new DbConfigMysql;
82  } else if (driverName == QLatin1String("QSQLITE")) {
83  s_DbConfigInstance = new DbConfigSqlite(DbConfigSqlite::Default);
84  } else if (driverName == QLatin1String("QSQLITE3")) {
85  s_DbConfigInstance = new DbConfigSqlite(DbConfigSqlite::Custom);
86  } else if (driverName == QLatin1String("QPSQL")) {
87  s_DbConfigInstance = new DbConfigPostgresql;
88  } else {
89  qCCritical(AKONADISERVER_LOG) << "Unknown database driver: " << driverName;
90  qCCritical(AKONADISERVER_LOG) << "Available drivers are: " << QSqlDatabase::drivers();
91  return nullptr;
92  }
93 
94  if (!s_DbConfigInstance->init(settings)) {
95  delete s_DbConfigInstance;
96  s_DbConfigInstance = nullptr;
97  }
98  }
99 
100  return s_DbConfigInstance;
101 }
102 
104 {
105  // do nothing
106  return true;
107 }
108 
110 {
111  // do nothing
112 }
113 
115 {
116  // do nothing
117 }
118 
120 {
121  return mSizeThreshold;
122 }
123 
125 {
126  if (!Instance::hasIdentifier()) {
127  return QStringLiteral("akonadi");
128  }
129  // dash is not allowed in PSQL
130  return QLatin1String("akonadi_") % Instance::identifier().replace(QLatin1Char('-'), QLatin1Char('_'));
131 }
132 
133 void DbConfig::initSession(const QSqlDatabase &database)
134 {
135  Q_UNUSED(database);
136 }
137 
138 int DbConfig::execute(const QString &cmd, const QStringList &args) const
139 {
140  qCDebug(AKONADISERVER_LOG) << "Executing: " << cmd << args.join(QLatin1Char(' '));
141  return QProcess::execute(cmd, args);
142 }
int execute(const QString &program, const QStringList &arguments)
bool canConvert(int targetTypeId) const const
static bool isConfigured()
Returns whether database have been configured.
Definition: dbconfig.cpp:60
void sync()
static DbConfig * configuredDatabase()
Returns the DbConfig instance for the database the user has configured.
Definition: dbconfig.cpp:65
A base class that provides an unique access layer to configuration and initialization of different da...
Definition: dbconfig.h:35
QStringList drivers()
virtual qint64 sizeThreshold() const
Payload data bigger than this value will be stored in separate files, instead of the database...
Definition: dbconfig.cpp:119
T value() const const
QString join(const QString &separator) const const
virtual bool startInternalServer()
This method is called to start an external server.
Definition: dbconfig.cpp:103
virtual void stopInternalServer()
This method is called to stop the external server.
Definition: dbconfig.cpp:109
virtual void setup()
This method is called to setup initial database settings after a connection is established.
Definition: dbconfig.cpp:114
void setValue(const QString &key, const QVariant &value)
virtual QString driverName() const =0
Returns the name of the used driver.
int execute(const QString &cmd, const QStringList &args) const
Calls QProcess::execute() and also prints the command and arguments via qCDebug() ...
Definition: dbconfig.cpp:138
virtual bool init(QSettings &settings)=0
This method is called whenever the Akonadi server is started and before the initial database connecti...
bool isEmpty() const const
static QString defaultDatabaseName()
Returns the suggested default database name, if none is specified in the configuration already...
Definition: dbconfig.cpp:124
QVariant value(const QString &key, const QVariant &defaultValue) const const
Helper integration between Akonadi and Qt.
virtual void initSession(const QSqlDatabase &database)
Do session setup/initialization work on database.
Definition: dbconfig.cpp:133
QString toString() const const
This file is part of the KDE documentation.
Documentation copyright © 1996-2020 The KDE developers.
Generated on Sat May 30 2020 22:46:07 by doxygen 1.8.11 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.