]> gitweb.stoutner.com Git - PrivacyBrowserPC.git/blob - src/views/DomainSettingsView.cpp
ff1131f817494ce84a3e570a6b3b67a3ec376f13
[PrivacyBrowserPC.git] / src / views / DomainSettingsView.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 "DomainSettingsView.h"
22 #include "ui_DomainSettingsView.h"
23 #include "helpers/DomainsDatabaseHelper.h"
24
25 DomainSettingsView::DomainSettingsView(QWidget *parent) : QWidget(parent)
26 {
27     // Instantiate the domain settings view UI.
28     Ui::DomainSettingsView domainSettingsViewUi;
29
30     // Setup the UI.
31     domainSettingsViewUi.setupUi(this);
32
33     // Get handles for the views.
34     domainsListViewPointer = domainSettingsViewUi.domainsListView;
35     domainNameLineEditPointer = domainSettingsViewUi.domainNameLineEdit;
36
37     // Create a table model.
38     domainsTableModelPointer = new QSqlTableModel(0, QSqlDatabase::database(DomainsDatabaseHelper::CONNECTION_NAME));
39
40     // Set the table for the model.
41     domainsTableModelPointer->setTable(DomainsDatabaseHelper::DOMAINS_TABLE);
42
43     // Set the model for the list view.
44     domainsListViewPointer->setModel(domainsTableModelPointer);
45
46     // Set the visible column to be the domain name.
47     domainsListViewPointer->setModelColumn(1);
48
49     // Disable editing of the list view.
50     domainsListViewPointer->setEditTriggers(QAbstractItemView::NoEditTriggers);
51
52     // Handle clicks on the domains.
53     connect(domainsListViewPointer, SIGNAL(activated(QModelIndex)), this, SLOT(domainSelected(QModelIndex)));
54
55     // Read the data from the database and apply it to the table model.
56     domainsTableModelPointer->select();
57
58     // Select the first entry in the list view.
59     domainsListViewPointer->setCurrentIndex(domainsTableModelPointer->index(0, 1));
60
61     // Populate the domain settings.
62     domainSelected(domainsListViewPointer->selectionModel()->currentIndex());
63 }
64
65 void DomainSettingsView::addDomain()
66 {
67     // Insert a row.
68     domainsTableModelPointer->insertRows(domainsTableModelPointer->rowCount(), 1);
69 }
70
71 void DomainSettingsView::deleteDomain()
72 {
73     // Delete the current row.
74     domainsTableModelPointer->removeRow(domainsListViewPointer->selectionModel()->currentIndex().row());
75 }
76
77
78 void DomainSettingsView::domainSelected(QModelIndex modelIndex)
79 {
80     // Populate the domain name line edit pointer.
81     domainNameLineEditPointer->setText(modelIndex.data().toString());
82 }