1 /**************************************************************************
2 * Otter Browser: Web browser controlled by the user, not vice-versa.
3 * Copyright (C) 2013 - 2018 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 "SaveSessionDialog.h"
21 #include "MainWindow.h"
22 #include "../core/SessionsManager.h"
23 #include "../core/Utils.h"
24 
25 #include "ui_SaveSessionDialog.h"
26 
27 #include <QtGui/QRegularExpressionValidator>
28 #include <QtWidgets/QMessageBox>
29 
30 namespace Otter
31 {
32 
SaveSessionDialog(QWidget * parent)33 SaveSessionDialog::SaveSessionDialog(QWidget *parent) : Dialog(parent),
34 	m_ui(new Ui::SaveSessionDialog)
35 {
36 	QString identifier(SessionsManager::getCurrentSession());
37 
38 	if (identifier == QLatin1String("default"))
39 	{
40 		identifier = Utils::createIdentifier(QLatin1String("default"), SessionsManager::getSessions());
41 	}
42 
43 	m_ui->setupUi(this);
44 	m_ui->titleLineEditWidget->setText(SessionsManager::getSession(SessionsManager::getCurrentSession()).title);
45 	m_ui->identifierLineEditWidget->setText(identifier);
46 	m_ui->identifierLineEditWidget->setValidator(new QRegularExpressionValidator(QRegularExpression(QLatin1String("[a-z0-9\\-_]+")), this));
47 
48 	connect(m_ui->buttonBox, &QDialogButtonBox::accepted, this, &SaveSessionDialog::saveSession);
49 	connect(m_ui->buttonBox, &QDialogButtonBox::rejected, this, &SaveSessionDialog::close);
50 }
51 
~SaveSessionDialog()52 SaveSessionDialog::~SaveSessionDialog()
53 {
54 	delete m_ui;
55 }
56 
changeEvent(QEvent * event)57 void SaveSessionDialog::changeEvent(QEvent *event)
58 {
59 	QDialog::changeEvent(event);
60 
61 	if (event->type() == QEvent::LanguageChange)
62 	{
63 		m_ui->retranslateUi(this);
64 	}
65 }
66 
saveSession()67 void SaveSessionDialog::saveSession()
68 {
69 	const QString identifier(m_ui->identifierLineEditWidget->text());
70 
71 	if (identifier.isEmpty() || (SessionsManager::getCurrentSession() != identifier && SessionsManager::getSession(identifier).isValid() && QMessageBox::question(this, tr("Question"), tr("Session with specified indentifier already exists.\nDo you want to overwrite it?"), QMessageBox::Yes, QMessageBox::No) == QMessageBox::No))
72 	{
73 		show();
74 
75 		return;
76 	}
77 
78 	if (SessionsManager::saveSession(identifier, m_ui->titleLineEditWidget->text(), (m_ui->onlyCurrentWindowCheckBox->isChecked() ? qobject_cast<MainWindow*>(parentWidget()) : nullptr)))
79 	{
80 		close();
81 	}
82 	else
83 	{
84 		QMessageBox::critical(this, tr("Error"), tr("Failed to save session."), QMessageBox::Close);
85 	}
86 }
87 
88 }
89