]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/helpers/DomainsDatabaseHelper.cpp
Begin implementing Domain Settings using KXmlGuiWindow part 2.
[PrivacyBrowserPC.git] / src / helpers / DomainsDatabaseHelper.cpp
1 /*
2  * Copyright © 2022 Soren Stoutner <soren@stoutner.com>.
3  *
4  * This file is part of Privacy Browser PC <https://www.stoutner.com/privacy-browser-pc>.
5  *
6  * Privacy Browser PC is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Privacy Browser PC is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Privacy Browser PC.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 // Application headers.
21 #include "DomainsDatabaseHelper.h"
22
23 // Qt framework headers.
24 #include <QtSql>
25
26 // Define the static constants.
27 const QString DomainsDatabaseHelper::CONNECTION_NAME = "domains_database";
28 const QString DomainsDatabaseHelper::DOMAINS_TABLE = "domains";
29
30 // The default constructor.
31 DomainsDatabaseHelper::DomainsDatabaseHelper() {}
32
33 void DomainsDatabaseHelper::addDatabase()
34 {
35     // Add the domain settings database.
36     QSqlDatabase domainsDatabase = QSqlDatabase::addDatabase("QSQLITE", CONNECTION_NAME);
37
38     // Set the database name.
39     domainsDatabase.setDatabaseName(QStandardPaths::writableLocation(QStandardPaths::AppDataLocation) + "/domains.db");
40
41     // Open the database.
42     if (domainsDatabase.open())  // Opening the database succeeded.
43     {
44         // Check to see if the domains table already exists.
45         if (domainsDatabase.tables().contains(DOMAINS_TABLE))
46         {
47             // Run schema update code.
48         }
49         else
50         {
51             // Instantiate a create table query.
52             QSqlQuery createTableQuery(domainsDatabase);
53
54             // Prepare the create table query.
55             createTableQuery.prepare("CREATE TABLE " + DOMAINS_TABLE + "("
56                 "_id INTEGER PRIMARY KEY, "
57                 "domain_name TEXT)"
58             );
59
60             // Execute the query.
61             if (!createTableQuery.exec())
62             {
63                 // Log any errors.
64                 qDebug().noquote().nospace() << "Error creating table:  " << domainsDatabase.lastError();
65             }
66         }
67     }
68     else  // Opening the database failed.
69     {
70         // Write the last database error message to the debug output.
71         qDebug().noquote().nospace() << "Error opening database:  " << domainsDatabase.lastError();
72     }
73 };