1 /**************************************************************************
2 * Otter Browser: Web browser controlled by the user, not vice-versa.
3 * Copyright (C) 2015 - 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 "Dialog.h"
21 #include "../core/JsonSettings.h"
22 #include "../core/SessionsManager.h"
23 
24 #include <QtCore/QFile>
25 #include <QtCore/QJsonArray>
26 #include <QtCore/QJsonObject>
27 
28 namespace Otter
29 {
30 
Dialog(QWidget * parent)31 Dialog::Dialog(QWidget *parent) : QDialog(parent),
32 	m_wasRestored(false)
33 {
34 }
35 
showEvent(QShowEvent * event)36 void Dialog::showEvent(QShowEvent *event)
37 {
38 	if (!m_wasRestored)
39 	{
40 		QFile file(SessionsManager::getWritableDataPath(QLatin1String("dialogs.json")));
41 
42 		if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
43 		{
44 			adjustSize();
45 		}
46 		else
47 		{
48 			const QString name(Utils::normalizeObjectName(objectName(), QLatin1String("Dialog")));
49 			const QJsonObject settingsObject(QJsonDocument::fromJson(file.readAll()).object());
50 
51 			file.close();
52 
53 			if (settingsObject.contains(name))
54 			{
55 				const QJsonObject sizeObject(settingsObject.value(name).toObject().value(QLatin1String("size")).toObject());
56 
57 				if (!sizeObject.isEmpty() && sizeObject.value(QLatin1String("width")).toInt() > 0 && sizeObject.value(QLatin1String("height")).toInt() > 0)
58 				{
59 					resize(sizeObject.value(QLatin1String("width")).toInt(), sizeObject.value(QLatin1String("height")).toInt());
60 				}
61 			}
62 		}
63 
64 		m_wasRestored = true;
65 	}
66 
67 	QDialog::showEvent(event);
68 }
69 
resizeEvent(QResizeEvent * event)70 void Dialog::resizeEvent(QResizeEvent *event)
71 {
72 	QDialog::resizeEvent(event);
73 
74 	if (!m_wasRestored)
75 	{
76 		return;
77 	}
78 
79 	JsonSettings settings(SessionsManager::getWritableDataPath(QLatin1String("dialogs.json")));
80 	QJsonObject settingsObject(settings.object());
81 	const QString name(Utils::normalizeObjectName(objectName(), QLatin1String("Dialog")));
82 	QJsonObject dialogObject(settingsObject.value(name).toObject());
83 	dialogObject.insert(QLatin1String("size"), QJsonObject({{QLatin1String("width"), width()}, {QLatin1String("height"), height()}}));
84 
85 	settingsObject.insert(name, dialogObject);
86 
87 	settings.setObject(settingsObject);
88 	settings.save();
89 }
90 
91 }
92