Database: sqlite default to appdata kworship.db
[kworship.git] / kworship / KwDatabaseSetup.cpp
blob7ecf9a04f03195d388d4b63d749eb694cbb2fa0d
1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
4 * *
5 * KWorship is free software: you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation, either version 2 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * KWorship is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with KWorship. If not, write to the Free Software Foundation, *
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. *
18 ***************************************************************************/
20 /**
21 * @file KwDatabaseSetup.cpp
22 * @brief Database setup manager.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwDatabaseSetup.h"
27 #include "settings.h"
29 #include <kstandarddirs.h>
32 * Constructors + destructor
35 /// Default constructor.
36 KwDatabaseSetup::KwDatabaseSetup(bool testing)
37 : m_testing(testing)
38 , m_database()
42 /// Destructor.
43 KwDatabaseSetup::~KwDatabaseSetup()
48 * Initialisation
51 /// Initialise from configuration.
52 bool KwDatabaseSetup::initialiseFromConfig()
54 int type = Settings::databaseType();
55 switch (type)
57 case Settings::EnumDatabaseType::MySQL:
58 case Settings::EnumDatabaseType::PostgreSQL:
60 QString strType;
61 if (type == Settings::EnumDatabaseType::MySQL)
63 strType = "QMYSQL";
65 else
67 strType = "QPSQL";
69 return initialiseConnection(strType,
70 Settings::databaseHost(),
71 Settings::databaseName(),
72 Settings::databaseUsername(),
73 Settings::databasePassword());
75 break;
76 case Settings::EnumDatabaseType::SQLite:
78 QString strType = "QSQLITE";
79 QUrl url;
80 if (Settings::databaseLocationCustom())
82 url = Settings::databaseLocation();
84 else
86 url = KStandardDirs::locateLocal("appdata", "kworship.db");
88 return initialiseFile(strType,
89 url);
91 break;
92 default:
93 break;
95 return false;
98 /// Initialise connection to a remote database.
99 bool KwDatabaseSetup::initialiseConnection(QString type, QString host, QString dbname, QString username, QString password)
101 if (type != "QMYSQL" && type != "QPSQL")
103 return false;
106 // Setup connection
107 if (m_testing)
109 m_database = QSqlDatabase::addDatabase(type, "test");
111 else
113 m_database = QSqlDatabase::addDatabase(type);
115 m_database.setHostName(host);
116 m_database.setDatabaseName(dbname);
117 m_database.setUserName(username);
118 if (!password.isNull())
120 m_database.setPassword(password);
123 // Connect
124 bool connected = m_database.open();
126 if (connected && !m_testing)
128 // Ensure the database is setup ok
129 /// @todo implement
132 return connected;
135 /// Initialise local file-based database.
136 bool KwDatabaseSetup::initialiseFile(QString type, QUrl url)
138 if (type != "QSQLITE")
140 return false;
143 m_database = QSqlDatabase::addDatabase(type);
144 m_database.setDatabaseName(url.toLocalFile());
145 bool connected = m_database.open();
147 if (connected)
149 // Ensure database is setup ok
150 /// @todo implement
153 return connected;
157 * Accessors
160 /// Get the database object.
161 QSqlDatabase& KwDatabaseSetup::database()
163 return m_database;