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 "AcceptCookieDialog.h"
21 
22 #include "ui_AcceptCookieDialog.h"
23 
24 #include <QtCore/QDateTime>
25 #include <QtWidgets/QPushButton>
26 
27 namespace Otter
28 {
29 
AcceptCookieDialog(const QNetworkCookie & cookie,CookieJar::CookieOperation operation,CookieJar * cookieJar,QWidget * parent)30 AcceptCookieDialog::AcceptCookieDialog(const QNetworkCookie &cookie, CookieJar::CookieOperation operation, CookieJar *cookieJar, QWidget *parent) : Dialog(parent),
31 	m_cookieJar(cookieJar),
32 	m_cookie(cookie),
33 	m_operation(operation),
34 	m_ui(new Ui::AcceptCookieDialog)
35 {
36 	QString domain(cookie.domain());
37 
38 	if (domain.startsWith(QLatin1Char('.')))
39 	{
40 		domain = domain.mid(1);
41 	}
42 
43 	m_ui->setupUi(this);
44 
45 	if (operation == CookieJar::InsertCookie)
46 	{
47 		m_ui->messageLabel->setText(tr("Website %1 requested to add new cookie.").arg(domain));
48 	}
49 	else if (operation == CookieJar::UpdateCookie)
50 	{
51 		m_ui->messageLabel->setText(tr("Website %1 requested to update existing cookie.").arg(domain));
52 	}
53 	else
54 	{
55 		m_ui->messageLabel->setText(tr("Website %1 requested to remove existing cookie.").arg(domain));
56 	}
57 
58 	m_ui->domainValueLabelWidget->setText(cookie.domain());
59 	m_ui->nameValueLabelWidget->setText(QString(cookie.name()));
60 	m_ui->valueValueLabelWidget->setText(QString(cookie.value()));
61 	m_ui->expiresValueLabel->setText(cookie.expirationDate().isValid() ? cookie.expirationDate().toString(Qt::ISODate) : tr("This session only"));
62 	m_ui->isSecureValueLabel->setText(cookie.isSecure() ? tr("Secure connections only") : tr("Any type of connection"));
63 	m_ui->isHttpOnlyValueLabel->setText(cookie.isHttpOnly() ? tr("Yes") : tr("No"));
64 	m_ui->buttonBox->addButton(tr("Accept"), QDialogButtonBox::AcceptRole);
65 
66 	if (operation != CookieJar::RemoveCookie && !cookie.isSessionCookie())
67 	{
68 		m_ui->buttonBox->addButton(tr("Accept For This Session Only"), QDialogButtonBox::AcceptRole)->setObjectName(QLatin1String("sessionOnly"));
69 	}
70 
71 	m_ui->buttonBox->addButton(tr("Discard"), QDialogButtonBox::RejectRole);
72 
73 	connect(m_ui->buttonBox, &QDialogButtonBox::clicked, this, &AcceptCookieDialog::handleButtonClicked);
74 }
75 
~AcceptCookieDialog()76 AcceptCookieDialog::~AcceptCookieDialog()
77 {
78 	delete m_ui;
79 }
80 
changeEvent(QEvent * event)81 void AcceptCookieDialog::changeEvent(QEvent *event)
82 {
83 	QDialog::changeEvent(event);
84 
85 	if (event->type() == QEvent::LanguageChange)
86 	{
87 		m_ui->retranslateUi(this);
88 	}
89 }
90 
handleButtonClicked(QAbstractButton * button)91 void AcceptCookieDialog::handleButtonClicked(QAbstractButton *button)
92 {
93 	const QDialogButtonBox::ButtonRole role(m_ui->buttonBox->buttonRole(button));
94 	const AcceptCookieResult result((role == QDialogButtonBox::AcceptRole) ? ((button->objectName() == QLatin1String("sessionOnly")) ? AcceptAsSessionCookie : AcceptCookie) : IgnoreCookie);
95 
96 	if (m_operation == CookieJar::InsertCookie)
97 	{
98 		if (result == AcceptCookieDialog::AcceptAsSessionCookie)
99 		{
100 			m_cookie.setExpirationDate({});
101 
102 			m_cookieJar->forceInsertCookie(m_cookie);
103 		}
104 		else if (result == AcceptCookieDialog::AcceptCookie)
105 		{
106 			m_cookieJar->forceInsertCookie(m_cookie);
107 		}
108 	}
109 	else if (m_operation == CookieJar::UpdateCookie)
110 	{
111 		if (result == AcceptCookieDialog::AcceptAsSessionCookie)
112 		{
113 			m_cookie.setExpirationDate({});
114 
115 			m_cookieJar->forceUpdateCookie(m_cookie);
116 		}
117 		else if (result == AcceptCookieDialog::AcceptCookie)
118 		{
119 			m_cookieJar->forceUpdateCookie(m_cookie);
120 		}
121 	}
122 	else if (m_operation == CookieJar::InsertCookie && result != AcceptCookieDialog::IgnoreCookie)
123 	{
124 		m_cookieJar->forceDeleteCookie(m_cookie);
125 	}
126 
127 	accept();
128 }
129 
130 }
131