1 /**************************************************************************
2 * Otter Browser: Web browser controlled by the user, not vice-versa.
3 * Copyright (C) 2016 - 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 "ProgressInformationWidget.h"
21 #include "../../../core/Utils.h"
22 #include "../../../ui/ContentsWidget.h"
23 #include "../../../ui/ProgressBarWidget.h"
24 #include "../../../ui/ToolBarWidget.h"
25 #include "../../../ui/Window.h"
26 
27 #include <QtWidgets/QHBoxLayout>
28 
29 namespace Otter
30 {
31 
ProgressInformationWidget(Window * window,const ToolBarsManager::ToolBarDefinition::Entry & definition,QWidget * parent)32 ProgressInformationWidget::ProgressInformationWidget(Window *window, const ToolBarsManager::ToolBarDefinition::Entry &definition, QWidget *parent) : QWidget(parent),
33 	m_window(nullptr),
34 	m_label(nullptr),
35 	m_progressBar(nullptr),
36 	m_type(UnknownType)
37 {
38 	QHBoxLayout *layout(new QHBoxLayout(this));
39 	layout->setContentsMargins(0, 0, 0, 0);
40 	layout->setSpacing(0);
41 
42 	if (definition.action == QLatin1String("ProgressInformationDocumentProgressWidget"))
43 	{
44 		m_type = DocumentProgressType;
45 	}
46 	else if (definition.action == QLatin1String("ProgressInformationTotalProgressWidget"))
47 	{
48 		m_type = TotalProgressType;
49 	}
50 	else if (definition.action == QLatin1String("ProgressInformationTotalSizeWidget"))
51 	{
52 		m_type = TotalSizeType;
53 	}
54 	else if (definition.action == QLatin1String("ProgressInformationElementsWidget"))
55 	{
56 		m_type = ElementsType;
57 	}
58 	else if (definition.action == QLatin1String("ProgressInformationSpeedWidget"))
59 	{
60 		m_type = SpeedType;
61 	}
62 	else if (definition.action == QLatin1String("ProgressInformationElapsedTimeWidget"))
63 	{
64 		m_type = ElapsedTimeType;
65 	}
66 	else if (definition.action == QLatin1String("ProgressInformationMessageWidget"))
67 	{
68 		m_type = MessageType;
69 	}
70 
71 	if (m_type == DocumentProgressType || m_type == TotalProgressType)
72 	{
73 		m_progressBar = new ProgressBarWidget(this);
74 		m_progressBar->setRange(0, 100);
75 		m_progressBar->setAlignment(Qt::AlignCenter);
76 
77 		layout->addWidget(m_progressBar);
78 	}
79 	else
80 	{
81 		m_label = new QLabel(this);
82 
83 		if (m_type != MessageType)
84 		{
85 			m_label->setAlignment(Qt::AlignCenter);
86 		}
87 
88 		layout->addWidget(m_label);
89 	}
90 
91 	setSizePolicy(((m_type == MessageType) ? QSizePolicy::Expanding : QSizePolicy::Fixed), QSizePolicy::Preferred);
92 	setWindow(window);
93 
94 	const ToolBarWidget *toolBar(qobject_cast<ToolBarWidget*>(parent));
95 
96 	if (toolBar && toolBar->getDefinition().isGlobal())
97 	{
98 		connect(toolBar, &ToolBarWidget::windowChanged, this, &ProgressInformationWidget::setWindow);
99 	}
100 }
101 
updateStatus(WebWidget::PageInformation key,const QVariant & value)102 void ProgressInformationWidget::updateStatus(WebWidget::PageInformation key, const QVariant &value)
103 {
104 	switch (m_type)
105 	{
106 		case DocumentProgressType:
107 			if (key == WebWidget::DocumentLoadingProgressInformation)
108 			{
109 				const bool isValid(value.toInt() >= 0);
110 
111 				m_progressBar->setFormat(isValid ? tr("Document: %p%") : tr("Document: ?"));
112 				m_progressBar->setValue(isValid ? value.toInt() : 0);
113 			}
114 
115 			break;
116 		case TotalProgressType:
117 			if (key == WebWidget::TotalLoadingProgressInformation)
118 			{
119 				m_progressBar->setFormat((value.toInt() < 0) ? tr("Total: ?") : tr("Total: %p%"));
120 				m_progressBar->setValue(value.toInt());
121 			}
122 
123 			break;
124 		case TotalSizeType:
125 			if (key == WebWidget::TotalBytesReceivedInformation)
126 			{
127 				m_label->setText(tr("Total: %1").arg(Utils::formatUnit(value.toLongLong(), false, 1)));
128 			}
129 
130 			break;
131 		case ElementsType:
132 			if (key == WebWidget::RequestsFinishedInformation)
133 			{
134 				m_label->setText(tr("Elements: %1/%2").arg(value.toInt()).arg((m_window && !m_window->isAboutToClose() && m_window->getWebWidget()) ? m_window->getWebWidget()->getPageInformation(WebWidget::RequestsStartedInformation).toInt() : 0));
135 			}
136 
137 			break;
138 		case SpeedType:
139 			if (key == WebWidget::LoadingSpeedInformation)
140 			{
141 				m_label->setText(tr("Speed: %1").arg(Utils::formatUnit(value.toLongLong(), true, 1)));
142 			}
143 
144 			break;
145 		case ElapsedTimeType:
146 			if (key == WebWidget::LoadingTimeInformation)
147 			{
148 				const int minutes(value.toInt() / 60);
149 
150 				m_label->setText(tr("Time: %1").arg(QStringLiteral("%1:%2").arg(minutes).arg((value.toInt() - (minutes * 60)), 2, 10, QLatin1Char('0'))));
151 			}
152 
153 			break;
154 		case MessageType:
155 			if (key == WebWidget::LoadingMessageInformation)
156 			{
157 				m_label->setText(value.toString());
158 			}
159 
160 			break;
161 		default:
162 			break;
163 	}
164 }
165 
setWindow(Window * window)166 void ProgressInformationWidget::setWindow(Window *window)
167 {
168 	WebWidget::PageInformation type(WebWidget::UnknownInformation);
169 
170 	switch (m_type)
171 	{
172 		case DocumentProgressType:
173 			type = WebWidget::DocumentLoadingProgressInformation;
174 
175 			break;
176 		case TotalProgressType:
177 			type = WebWidget::TotalLoadingProgressInformation;
178 
179 			break;
180 		case TotalSizeType:
181 			type = WebWidget::TotalBytesReceivedInformation;
182 
183 			break;
184 		case ElementsType:
185 			type = WebWidget::RequestsFinishedInformation;
186 
187 			break;
188 		case SpeedType:
189 			type = WebWidget::LoadingSpeedInformation;
190 
191 			break;
192 		case ElapsedTimeType:
193 			type = WebWidget::LoadingTimeInformation;
194 
195 			break;
196 		case MessageType:
197 			type = WebWidget::LoadingMessageInformation;
198 
199 			break;
200 		default:
201 			break;
202 	}
203 
204 	if (m_window && !m_window->isAboutToClose())
205 	{
206 		disconnect(m_window, &Window::pageInformationChanged, this, &ProgressInformationWidget::updateStatus);
207 
208 		updateStatus(type);
209 	}
210 
211 	m_window = window;
212 
213 	if (window && window->getWebWidget())
214 	{
215 		updateStatus(type, window->getWebWidget()->getPageInformation(type));
216 
217 		connect(m_window, &Window::pageInformationChanged, this, &ProgressInformationWidget::updateStatus);
218 	}
219 	else
220 	{
221 		updateStatus(type, {});
222 	}
223 }
224 
sizeHint() const225 QSize ProgressInformationWidget::sizeHint() const
226 {
227 	QSize size(QWidget::sizeHint());
228 
229 	if (m_type == MessageType)
230 	{
231 		return QSize(250, size.height());
232 	}
233 
234 	return QSize(150, size.height());
235 }
236 
237 }
238