VirtualBox

source: vbox/trunk/src/VBox/Frontends/VirtualBox/src/net/UINetworkManagerIndicator.cpp@ 82781

Last change on this file since 82781 was 80445, checked in by vboxsync, 5 years ago

Shared Clipboard/URI: Committed a bit too much, reverted.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 8.6 KB
Line 
1/* $Id: UINetworkManagerIndicator.cpp 80445 2019-08-27 17:51:09Z vboxsync $ */
2/** @file
3 * VBox Qt GUI - UINetworkManagerIndicator stuff implementation.
4 */
5
6/*
7 * Copyright (C) 2012-2019 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.virtualbox.org. This file is free software;
11 * you can redistribute it and/or modify it under the terms of the GNU
12 * General Public License (GPL) as published by the Free Software
13 * Foundation, in version 2 as it comes in the "COPYING" file of the
14 * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
15 * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
16 */
17
18/* GUI includes: */
19#include "UICommon.h"
20#include "UIIconPool.h"
21#include "UINetworkManagerIndicator.h"
22#include "UINetworkRequest.h"
23
24
25UINetworkManagerIndicator::UINetworkManagerIndicator()
26{
27 /* Assign state icons: */
28 setStateIcon(UINetworkManagerIndicatorState_Idle, UIIconPool::iconSet(":/download_manager_16px.png"));
29 setStateIcon(UINetworkManagerIndicatorState_Loading, UIIconPool::iconSet(":/download_manager_loading_16px.png"));
30 setStateIcon(UINetworkManagerIndicatorState_Error, UIIconPool::iconSet(":/download_manager_error_16px.png"));
31
32 /* Translate content: */
33 retranslateUi();
34}
35
36void UINetworkManagerIndicator::updateAppearance()
37{
38 /* First of all, we are hiding LED in case of 'idle' state: */
39 if (state() == UINetworkManagerIndicatorState_Idle && !isHidden())
40 hide();
41
42 /* Prepare description: */
43 QString strDecription;
44 /* Check if there are any network-requests: */
45 if (!m_ids.isEmpty())
46 {
47 /* Prepare table: */
48 QString strTable("<table>%1</table>");
49 QString strBodyItem("<tr><td>%1</td><td>&nbsp;</td><td>%2</td></tr>");
50 QString strParagraph("<p>%1</p>");
51 QString strBoldNobreak("<nobr><b>%1</b></nobr>");
52 QString strNobreak("<nobr>%1</nobr>");
53 QString strItalic("<i>%1</i>");
54 /* Prepare header: */
55 QString strHeader(strBoldNobreak.arg(tr("Current network operations:")));
56 /* Prepare table body: */
57 QString strBody;
58 for (int i = 0; i < m_data.size(); ++i)
59 {
60 const UINetworkRequestData &data = m_data[i];
61 const QString &strDescription = data.description;
62 QString strStatus(data.failed ? tr("failed", "network operation") :
63 tr("(%1 of %2)")
64 .arg(uiCommon().formatSize(data.bytesReceived))
65 .arg(uiCommon().formatSize(data.bytesTotal)));
66 QString strBodyLine(strBodyItem.arg(strNobreak.arg(strDescription)).arg(strNobreak.arg(strStatus)));
67 strBody += strBodyLine;
68 }
69 /* Compose description: */
70 strDecription = strParagraph.arg(strHeader + strTable.arg(strBody)) +
71 strParagraph.arg(strNobreak.arg(strItalic.arg(tr("Double-click for more information."))));
72 }
73 else
74 strDecription = QString();
75 /* Set description: */
76 setToolTip(strDecription);
77
78 /* Finally, we are showing LED in case of state is not 'idle': */
79 if (state() != UINetworkManagerIndicatorState_Idle && isHidden())
80 show();
81}
82
83void UINetworkManagerIndicator::sltAddNetworkManagerIndicatorDescription(UINetworkRequest *pNetworkRequest)
84{
85 /* Make sure network-request is really exists: */
86 AssertMsg(pNetworkRequest, ("Invalid network-request passed!"));
87 /* Make sure network-request was NOT registered yet: */
88 AssertMsg(!m_ids.contains(pNetworkRequest->uuid()), ("Network-request already registered!"));
89
90 /* Append network-request data: */
91 m_ids.append(pNetworkRequest->uuid());
92 m_data.append(UINetworkRequestData(pNetworkRequest->description(), 0, 0));
93
94 /* Prepare network-request listeners: */
95 connect(pNetworkRequest, static_cast<void(UINetworkRequest::*)(const QUuid&)>(&UINetworkRequest::sigStarted),
96 this, &UINetworkManagerIndicator::sltSetProgressToStarted);
97 connect(pNetworkRequest, &UINetworkRequest::sigCanceled,
98 this, &UINetworkManagerIndicator::sltSetProgressToCanceled);
99 connect(pNetworkRequest, static_cast<void(UINetworkRequest::*)(const QUuid&)>(&UINetworkRequest::sigFinished),
100 this, &UINetworkManagerIndicator::sltSetProgressToFinished);
101 connect(pNetworkRequest, static_cast<void(UINetworkRequest::*)(const QUuid&, const QString &)>(&UINetworkRequest::sigFailed),
102 this, &UINetworkManagerIndicator::sltSetProgressToFailed);
103 connect(pNetworkRequest, static_cast<void(UINetworkRequest::*)(const QUuid&, qint64, qint64)>(&UINetworkRequest::sigProgress),
104 this, &UINetworkManagerIndicator::sltSetProgress);
105
106 /* Update appearance: */
107 recalculateIndicatorState();
108}
109
110void UINetworkManagerIndicator::sldRemoveNetworkManagerIndicatorDescription(const QUuid &uuid)
111{
112 /* Make sure network-request still registered: */
113 AssertMsg(m_ids.contains(uuid), ("Network-request already unregistered!"));
114
115 /* Search for network-request index: */
116 int iIndexOfRequiredElement = m_ids.indexOf(uuid);
117
118 /* Delete corresponding network-request: */
119 m_ids.remove(iIndexOfRequiredElement);
120 m_data.remove(iIndexOfRequiredElement);
121
122 /* Update appearance: */
123 recalculateIndicatorState();
124}
125
126void UINetworkManagerIndicator::retranslateUi()
127{
128 /* Update appearance: */
129 updateAppearance();
130}
131
132void UINetworkManagerIndicator::sltSetProgressToStarted(const QUuid &uuid)
133{
134 /* Make sure that network-request still registered: */
135 AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
136
137 /* Search for network-request index: */
138 int iIndexOfNetworkRequest = m_ids.indexOf(uuid);
139 /* Update corresponding network-request data: */
140 UINetworkRequestData &data = m_data[iIndexOfNetworkRequest];
141 data.bytesReceived = 0;
142 data.bytesTotal = 0;
143 data.failed = false;
144
145 /* Update appearance: */
146 recalculateIndicatorState();
147}
148
149void UINetworkManagerIndicator::sltSetProgressToCanceled(const QUuid &uuid)
150{
151 /* Make sure that network-request still registered: */
152 AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
153 Q_UNUSED(uuid);
154
155 /* Update appearance: */
156 recalculateIndicatorState();
157}
158
159void UINetworkManagerIndicator::sltSetProgressToFailed(const QUuid &uuid, const QString &)
160{
161 /* Make sure that network-request still registered: */
162 AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
163
164 /* Search for network-request index: */
165 int iIndexOfNetworkRequest = m_ids.indexOf(uuid);
166 /* Update corresponding data: */
167 UINetworkRequestData &data = m_data[iIndexOfNetworkRequest];
168 data.failed = true;
169
170 /* Update appearance: */
171 recalculateIndicatorState();
172}
173
174void UINetworkManagerIndicator::sltSetProgressToFinished(const QUuid &uuid)
175{
176 /* Make sure that network-request still registered: */
177 AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
178 Q_UNUSED(uuid);
179
180 /* Update appearance: */
181 recalculateIndicatorState();
182}
183
184void UINetworkManagerIndicator::sltSetProgress(const QUuid &uuid, qint64 iReceived, qint64 iTotal)
185{
186 /* Make sure that network-request still registered: */
187 AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
188
189 /* Search for network-request index: */
190 int iIndexOfNetworkRequest = m_ids.indexOf(uuid);
191 /* Update corresponding network-request data: */
192 UINetworkRequestData &data = m_data[iIndexOfNetworkRequest];
193 data.bytesReceived = iReceived;
194 data.bytesTotal = iTotal;
195
196 /* Update appearance: */
197 updateAppearance();
198}
199
200void UINetworkManagerIndicator::recalculateIndicatorState()
201{
202 /* Check if there are network-requests at all: */
203 if (m_ids.isEmpty())
204 {
205 /* Set state to 'idle': */
206 setState(UINetworkManagerIndicatorState_Idle);
207 }
208 else
209 {
210 /* Check if there is at least one failed network-request: */
211 bool fIsThereAtLeastOneFailedNetworkRequest = false;
212 for (int i = 0; i < m_data.size(); ++i)
213 {
214 if (m_data[i].failed)
215 {
216 fIsThereAtLeastOneFailedNetworkRequest = true;
217 break;
218 }
219 }
220
221 /* If there it least one failed network-request: */
222 if (fIsThereAtLeastOneFailedNetworkRequest)
223 {
224 /* Set state to 'error': */
225 setState(UINetworkManagerIndicatorState_Error);
226 }
227 else
228 {
229 /* Set state to 'loading': */
230 setState(UINetworkManagerIndicatorState_Loading);
231 }
232 }
233
234 /* Update appearance finally: */
235 updateAppearance();
236}
237
Note: See TracBrowser for help on using the repository browser.

© 2023 Oracle
ContactPrivacy policyTerms of Use