Basic sword integration, verse at time keys, very basic\!
[kworship.git] / kworship / KwDatabaseSetup.cpp
blob59e9dcf85dbf2e58149235b6ade07ad27f7a7383
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 A song database manager class.
23 * @author James Hogan <james@albanarts.com>
26 #include "KwDatabaseSetup.h"
27 #include "settings.h"
30 * Constructors + destructor
33 /// Default constructor.
34 KwDatabaseSetup::KwDatabaseSetup(bool testing)
35 : m_testing(testing)
36 , m_database()
40 /// Destructor.
41 KwDatabaseSetup::~KwDatabaseSetup()
46 * Initialisation
49 /// Initialise from configuration.
50 bool KwDatabaseSetup::initialiseFromConfig()
52 int type = Settings::songdbType();
53 switch (type)
55 case Settings::EnumSongdbType::MySQL:
56 case Settings::EnumSongdbType::PostgreSQL:
58 QString strType;
59 if (type == Settings::EnumSongdbType::MySQL)
61 strType = "QMYSQL";
63 else
65 strType = "QPSQL";
67 return initialiseConnection(strType,
68 Settings::songdbHost(),
69 Settings::songdbName(),
70 Settings::songdbUsername(),
71 Settings::songdbPassword());
73 break;
74 case Settings::EnumSongdbType::SQLite:
76 QString strType = "QSQLITE";
77 QUrl url;
78 if (Settings::songdbLocationCustom())
80 url = Settings::songdbLocation();
82 else
84 /// @todo Use kde apps dir as default location
85 url = QUrl("file://~/kworship.db");
87 return initialiseFile(strType,
88 url);
90 break;
91 default:
92 break;
94 return false;
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")
102 return false;
105 // Setup connection
106 if (m_testing)
108 m_database = QSqlDatabase::addDatabase(type, "test");
110 else
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);
122 // Connect
123 bool connected = m_database.open();
125 if (connected && !m_testing)
127 // Ensure the database is setup ok
128 /// @todo implement
131 return connected;
134 /// Initialise local file-based database.
135 bool KwDatabaseSetup::initialiseFile(QString type, QUrl url)
137 if (type != "QSQLITE")
139 return false;
142 /// @todo Implement
143 Q_UNUSED(url)
145 return false;
149 * Accessors
152 /// Get the database object.
153 QSqlDatabase& KwDatabaseSetup::database()
155 return m_database;