1 /***************************************************************************
2 * This file is part of KWorship. *
3 * Copyright 2008 James Hogan <james@albanarts.com> *
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. *
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. *
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 ***************************************************************************/
21 * @file KwDatabaseSetup.cpp
22 * @brief Database setup manager.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwDatabaseSetup.h"
30 * Constructors + destructor
33 /// Default constructor.
34 KwDatabaseSetup::KwDatabaseSetup(bool testing
)
41 KwDatabaseSetup::~KwDatabaseSetup()
49 /// Initialise from configuration.
50 bool KwDatabaseSetup::initialiseFromConfig()
52 int type
= Settings::databaseType();
55 case Settings::EnumDatabaseType::MySQL
:
56 case Settings::EnumDatabaseType::PostgreSQL
:
59 if (type
== Settings::EnumDatabaseType::MySQL
)
67 return initialiseConnection(strType
,
68 Settings::databaseHost(),
69 Settings::databaseName(),
70 Settings::databaseUsername(),
71 Settings::databasePassword());
74 case Settings::EnumDatabaseType::SQLite
:
76 QString strType
= "QSQLITE";
78 if (Settings::databaseLocationCustom())
80 url
= Settings::databaseLocation();
84 /// @todo Use kde apps dir as default location
85 url
= QUrl("file://~/kworship.db");
87 return initialiseFile(strType
,
97 /// Initialise connection to a remote database.
98 bool KwDatabaseSetup::initialiseConnection(QString type
, QString host
, QString dbname
, QString username
, QString password
)
100 if (type
!= "QMYSQL" && type
!= "QPSQL")
108 m_database
= QSqlDatabase::addDatabase(type
, "test");
112 m_database
= QSqlDatabase::addDatabase(type
);
114 m_database
.setHostName(host
);
115 m_database
.setDatabaseName(dbname
);
116 m_database
.setUserName(username
);
117 if (!password
.isNull())
119 m_database
.setPassword(password
);
123 bool connected
= m_database
.open();
125 if (connected
&& !m_testing
)
127 // Ensure the database is setup ok
134 /// Initialise local file-based database.
135 bool KwDatabaseSetup::initialiseFile(QString type
, QUrl url
)
137 if (type
!= "QSQLITE")
142 m_database
= QSqlDatabase::addDatabase(type
);
143 m_database
.setDatabaseName(url
.toLocalFile());
144 bool connected
= m_database
.open();
148 // Ensure database is setup ok
159 /// Get the database object.
160 QSqlDatabase
& KwDatabaseSetup::database()