1
/* $Id: UINetworkManagerIndicator.cpp $ */
4
* VBox frontends: Qt GUI ("VirtualBox"):
5
* UINetworkManagerIndicator stuff implementation
9
* Copyright (C) 2012 Oracle Corporation
11
* This file is part of VirtualBox Open Source Edition (OSE), as
12
* available from http://www.virtualbox.org. This file is free software;
13
* you can redistribute it and/or modify it under the terms of the GNU
14
* General Public License (GPL) as published by the Free Software
15
* Foundation, in version 2 as it comes in the "COPYING" file of the
16
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
17
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
21
#include "UINetworkManagerIndicator.h"
22
#include "UINetworkRequest.h"
23
#include "VBoxGlobal.h"
25
UINetworkManagerIndicator::UINetworkManagerIndicator()
27
/* Assign state icons: */
28
setStateIcon(UINetworkManagerIndicatorState_Idle, QPixmap(":/nw_16px.png"));
29
setStateIcon(UINetworkManagerIndicatorState_Loading, QPixmap(":/nw_loading_16px.png"));
30
setStateIcon(UINetworkManagerIndicatorState_Error, QPixmap(":/nw_error_16px.png"));
32
/* Translate content: */
36
void UINetworkManagerIndicator::addNetworkRequest(UINetworkRequest *pNetworkRequest)
38
/* Make sure network-request is really exists: */
39
AssertMsg(pNetworkRequest, ("Invalid network-request passed!"));
40
/* Make sure network-request was NOT registered yet: */
41
AssertMsg(!m_ids.contains(pNetworkRequest->uuid()), ("Network-request already registered!"));
43
/* Append network-request data: */
44
m_ids.append(pNetworkRequest->uuid());
45
m_data.append(UINetworkRequestData(pNetworkRequest->description(), 0, 0));
47
/* Prepare network-request listeners: */
48
connect(pNetworkRequest, SIGNAL(sigStarted(const QUuid &)),
49
this, SLOT(sltSetProgressToStarted(const QUuid &)));
50
connect(pNetworkRequest, SIGNAL(sigCanceled(const QUuid &)),
51
this, SLOT(sltSetProgressToCanceled(const QUuid &)));
52
connect(pNetworkRequest, SIGNAL(sigFinished(const QUuid &)),
53
this, SLOT(sltSetProgressToFinished(const QUuid &)));
54
connect(pNetworkRequest, SIGNAL(sigFailed(const QUuid &, const QString &)),
55
this, SLOT(sltSetProgressToFailed(const QUuid &, const QString &)));
56
connect(pNetworkRequest, SIGNAL(sigProgress(const QUuid &, qint64, qint64)),
57
this, SLOT(sltSetProgress(const QUuid &, qint64, qint64)));
59
/* Update appearance: */
60
recalculateIndicatorState();
63
void UINetworkManagerIndicator::removeNetworkRequest(const QUuid &uuid)
65
/* Make sure network-request still registered: */
66
AssertMsg(m_ids.contains(uuid), ("Network-request already unregistered!"));
68
/* Search for network-request index: */
69
int iIndexOfRequiredElement = m_ids.indexOf(uuid);
71
/* Delete corresponding network-request: */
72
m_ids.remove(iIndexOfRequiredElement);
73
m_data.remove(iIndexOfRequiredElement);
75
/* Update appearance: */
76
recalculateIndicatorState();
79
void UINetworkManagerIndicator::sltSetProgressToStarted(const QUuid &uuid)
81
/* Make sure that network-request still registered: */
82
AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
84
/* Search for network-request index: */
85
int iIndexOfNetworkRequest = m_ids.indexOf(uuid);
86
/* Update corresponding network-request data: */
87
UINetworkRequestData &data = m_data[iIndexOfNetworkRequest];
88
data.bytesReceived = 0;
92
/* Update appearance: */
93
recalculateIndicatorState();
96
void UINetworkManagerIndicator::sltSetProgressToCanceled(const QUuid &uuid)
98
/* Make sure that network-request still registered: */
99
AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
102
/* Update appearance: */
103
recalculateIndicatorState();
106
void UINetworkManagerIndicator::sltSetProgressToFailed(const QUuid &uuid, const QString &)
108
/* Make sure that network-request still registered: */
109
AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
111
/* Search for network-request index: */
112
int iIndexOfNetworkRequest = m_ids.indexOf(uuid);
113
/* Update corresponding data: */
114
UINetworkRequestData &data = m_data[iIndexOfNetworkRequest];
117
/* Update appearance: */
118
recalculateIndicatorState();
121
void UINetworkManagerIndicator::sltSetProgressToFinished(const QUuid &uuid)
123
/* Make sure that network-request still registered: */
124
AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
127
/* Update appearance: */
128
recalculateIndicatorState();
131
void UINetworkManagerIndicator::sltSetProgress(const QUuid &uuid, qint64 iReceived, qint64 iTotal)
133
/* Make sure that network-request still registered: */
134
AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
136
/* Search for network-request index: */
137
int iIndexOfNetworkRequest = m_ids.indexOf(uuid);
138
/* Update corresponding network-request data: */
139
UINetworkRequestData &data = m_data[iIndexOfNetworkRequest];
140
data.bytesReceived = iReceived;
141
data.bytesTotal = iTotal;
143
/* Update appearance: */
147
void UINetworkManagerIndicator::retranslateUi()
149
/* Update appearance: */
153
void UINetworkManagerIndicator::recalculateIndicatorState()
155
/* Check if there are network-requests at all: */
158
/* Set state to 'idle': */
159
setState(UINetworkManagerIndicatorState_Idle);
163
/* Check if there is at least one failed network-request: */
164
bool fIsThereAtLeastOneFailedNetworkRequest = false;
165
for (int i = 0; i < m_data.size(); ++i)
167
if (m_data[i].failed)
169
fIsThereAtLeastOneFailedNetworkRequest = true;
174
/* If there it least one failed network-request: */
175
if (fIsThereAtLeastOneFailedNetworkRequest)
177
/* Set state to 'error': */
178
setState(UINetworkManagerIndicatorState_Error);
182
/* Set state to 'loading': */
183
setState(UINetworkManagerIndicatorState_Loading);
187
/* Update appearance finally: */
191
void UINetworkManagerIndicator::updateAppearance()
193
/* First of all, we are hiding LED in case of 'idle' state: */
194
if (state() == UINetworkManagerIndicatorState_Idle && !isHidden())
197
/* Prepare description: */
198
QString strDecription;
199
/* Check if there are any network-requests: */
200
if (!m_ids.isEmpty())
203
QString strTable("<table>%1</table>");
204
QString strBodyItem("<tr><td>%1</td><td> </td><td>%2</td></tr>");
205
QString strParagraph("<p>%1</p>");
206
QString strBoldNobreak("<nobr><b>%1</b></nobr>");
207
QString strNobreak("<nobr>%1</nobr>");
208
QString strItalic("<i>%1</i>");
209
/* Prepare header: */
210
QString strHeader(strBoldNobreak.arg(tr("Current network operations:")));
211
/* Prepare table body: */
213
for (int i = 0; i < m_data.size(); ++i)
215
const UINetworkRequestData &data = m_data[i];
216
const QString &strDescription = data.description;
217
QString strStatus(data.failed ? tr("failed", "network operation") :
219
.arg(vboxGlobal().formatSize(data.bytesReceived))
220
.arg(vboxGlobal().formatSize(data.bytesTotal)));
221
QString strBodyLine(strBodyItem.arg(strNobreak.arg(strDescription)).arg(strNobreak.arg(strStatus)));
222
strBody += strBodyLine;
224
/* Compose description: */
225
strDecription = strParagraph.arg(strHeader + strTable.arg(strBody)) +
226
strParagraph.arg(strNobreak.arg(strItalic.arg(tr("Double-click for more information."))));
229
strDecription = QString();
230
/* Set description: */
231
setToolTip(strDecription);
233
/* Finally, we are showing LED in case of state is not 'idle': */
234
if (state() != UINetworkManagerIndicatorState_Idle && isHidden())