1 /**************************************************************************
2 * Otter Browser: Web browser controlled by the user, not vice-versa.
3 * Copyright (C) 2015 - 2017 Michal Dutkiewicz aka Emdek <michal@emdek.pl>
4 *
5 * This program 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 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
17 *
18 **************************************************************************/
19 
20 #include "CookiesExceptionsDialog.h"
21 
22 #include "ui_CookiesExceptionsDialog.h"
23 
24 namespace Otter
25 {
26 
CookiesExceptionsDialog(const QStringList & acceptedHosts,const QStringList & rejectedHosts,QWidget * parent)27 CookiesExceptionsDialog::CookiesExceptionsDialog(const QStringList &acceptedHosts, const QStringList &rejectedHosts, QWidget *parent) : Dialog(parent),
28 	m_ui(new Ui::CookiesExceptionsDialog)
29 {
30 	m_ui->setupUi(this);
31 
32 	QStandardItemModel *acceptedHostsModel(new QStandardItemModel(this));
33 
34 	for (int i = 0; i < acceptedHosts.count(); ++i)
35 	{
36 		if (!acceptedHosts.at(i).isEmpty())
37 		{
38 			acceptedHostsModel->appendRow(new QStandardItem(acceptedHosts.at(i)));
39 		}
40 	}
41 
42 	m_ui->acceptedHostsItemView->setModel(acceptedHostsModel);
43 
44 	QStandardItemModel *rejectedHostsModel(new QStandardItemModel(this));
45 
46 	for (int i = 0; i < rejectedHosts.count(); ++i)
47 	{
48 		if (!rejectedHosts.at(i).isEmpty())
49 		{
50 			rejectedHostsModel->appendRow(new QStandardItem(rejectedHosts.at(i)));
51 		}
52 	}
53 
54 	m_ui->rejectedHostsItemView->setModel(rejectedHostsModel);
55 
56 	updateAcceptedHostsActions();
57 	updateRejectedHostsActions();
58 
59 	connect(m_ui->acceptedHostsItemView, &ItemViewWidget::needsActionsUpdate, this, &CookiesExceptionsDialog::updateAcceptedHostsActions);
60 	connect(m_ui->addAcceptedHostsButton, &QPushButton::clicked, this, &CookiesExceptionsDialog::addAcceptedHost);
61 	connect(m_ui->editAcceptedHostsButton, &QPushButton::clicked, this, &CookiesExceptionsDialog::editAcceptedHost);
62 	connect(m_ui->removeAcceptedHostsButton, &QPushButton::clicked, this, &CookiesExceptionsDialog::removeAcceptedHost);
63 	connect(m_ui->rejectedHostsItemView, &ItemViewWidget::needsActionsUpdate, this, &CookiesExceptionsDialog::updateRejectedHostsActions);
64 	connect(m_ui->addRejectedHostsButton, &QPushButton::clicked, this, &CookiesExceptionsDialog::addRejectedHost);
65 	connect(m_ui->editRejectedHostsButton, &QPushButton::clicked, this, &CookiesExceptionsDialog::editRejectedHost);
66 	connect(m_ui->removeRejectedHostsButton, &QPushButton::clicked, this, &CookiesExceptionsDialog::removeRejectedHost);
67 }
68 
~CookiesExceptionsDialog()69 CookiesExceptionsDialog::~CookiesExceptionsDialog()
70 {
71 	delete m_ui;
72 }
73 
changeEvent(QEvent * event)74 void CookiesExceptionsDialog::changeEvent(QEvent *event)
75 {
76 	QDialog::changeEvent(event);
77 
78 	if (event->type() == QEvent::LanguageChange)
79 	{
80 		m_ui->retranslateUi(this);
81 	}
82 }
83 
addAcceptedHost()84 void CookiesExceptionsDialog::addAcceptedHost()
85 {
86 	m_ui->acceptedHostsItemView->insertRow();
87 
88 	editAcceptedHost();
89 }
90 
addRejectedHost()91 void CookiesExceptionsDialog::addRejectedHost()
92 {
93 	m_ui->rejectedHostsItemView->insertRow();
94 
95 	editRejectedHost();
96 }
97 
editAcceptedHost()98 void CookiesExceptionsDialog::editAcceptedHost()
99 {
100 	m_ui->acceptedHostsItemView->edit(m_ui->acceptedHostsItemView->getIndex(m_ui->acceptedHostsItemView->getCurrentRow()));
101 }
102 
editRejectedHost()103 void CookiesExceptionsDialog::editRejectedHost()
104 {
105 	m_ui->rejectedHostsItemView->edit(m_ui->rejectedHostsItemView->getIndex(m_ui->rejectedHostsItemView->getCurrentRow()));
106 }
107 
removeAcceptedHost()108 void CookiesExceptionsDialog::removeAcceptedHost()
109 {
110 	m_ui->acceptedHostsItemView->removeRow();
111 	m_ui->acceptedHostsItemView->setFocus();
112 
113 	updateAcceptedHostsActions();
114 }
115 
removeRejectedHost()116 void CookiesExceptionsDialog::removeRejectedHost()
117 {
118 	m_ui->rejectedHostsItemView->removeRow();
119 	m_ui->rejectedHostsItemView->setFocus();
120 
121 	updateRejectedHostsActions();
122 }
123 
updateAcceptedHostsActions()124 void CookiesExceptionsDialog::updateAcceptedHostsActions()
125 {
126 	const bool isEditable(m_ui->acceptedHostsItemView->getCurrentRow() >= 0);
127 
128 	m_ui->editAcceptedHostsButton->setEnabled(isEditable);
129 	m_ui->removeAcceptedHostsButton->setEnabled(isEditable);
130 }
131 
updateRejectedHostsActions()132 void CookiesExceptionsDialog::updateRejectedHostsActions()
133 {
134 	const bool isEditable(m_ui->rejectedHostsItemView->getCurrentRow() >= 0);
135 
136 	m_ui->editRejectedHostsButton->setEnabled(isEditable);
137 	m_ui->removeRejectedHostsButton->setEnabled(isEditable);
138 }
139 
getAcceptedHosts() const140 QStringList CookiesExceptionsDialog::getAcceptedHosts() const
141 {
142 	QStringList entries;
143 
144 	for (int i = 0; i < m_ui->acceptedHostsItemView->model()->rowCount(); ++i)
145 	{
146 		const QString entry(m_ui->acceptedHostsItemView->getIndex(i).data(Qt::DisplayRole).toString());
147 
148 		if (!entry.isEmpty() && !entries.contains(entry))
149 		{
150 			entries.append(entry);
151 		}
152 	}
153 
154 	return entries;
155 }
156 
getRejectedHosts() const157 QStringList CookiesExceptionsDialog::getRejectedHosts() const
158 {
159 	QStringList entries;
160 
161 	for (int i = 0; i < m_ui->rejectedHostsItemView->model()->rowCount(); ++i)
162 	{
163 		const QString entry(m_ui->rejectedHostsItemView->getIndex(i).data(Qt::DisplayRole).toString());
164 
165 		if (!entry.isEmpty() && !entries.contains(entry))
166 		{
167 			entries.append(entry);
168 		}
169 	}
170 
171 	return entries;
172 }
173 
174 }
175