~ubuntu-branches/ubuntu/trusty/virtualbox-lts-xenial/trusty-proposed

« back to all changes in this revision

Viewing changes to src/VBox/Frontends/VirtualBox/src/net/UINetworkManagerIndicator.cpp

  • Committer: Package Import Robot
  • Author(s): Gianfranco Costamagna
  • Date: 2016-02-23 14:28:26 UTC
  • Revision ID: package-import@ubuntu.com-20160223142826-bdu69el2z6wa2a44
Tags: upstream-4.3.36-dfsg
ImportĀ upstreamĀ versionĀ 4.3.36-dfsg

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* $Id: UINetworkManagerIndicator.cpp $ */
 
2
/** @file
 
3
 *
 
4
 * VBox frontends: Qt GUI ("VirtualBox"):
 
5
 * UINetworkManagerIndicator stuff implementation
 
6
 */
 
7
 
 
8
/*
 
9
 * Copyright (C) 2012 Oracle Corporation
 
10
 *
 
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.
 
18
 */
 
19
 
 
20
/* Local includes: */
 
21
#include "UINetworkManagerIndicator.h"
 
22
#include "UINetworkRequest.h"
 
23
#include "VBoxGlobal.h"
 
24
 
 
25
UINetworkManagerIndicator::UINetworkManagerIndicator()
 
26
{
 
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"));
 
31
 
 
32
    /* Translate content: */
 
33
    retranslateUi();
 
34
}
 
35
 
 
36
void UINetworkManagerIndicator::addNetworkRequest(UINetworkRequest *pNetworkRequest)
 
37
{
 
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!"));
 
42
 
 
43
    /* Append network-request data: */
 
44
    m_ids.append(pNetworkRequest->uuid());
 
45
    m_data.append(UINetworkRequestData(pNetworkRequest->description(), 0, 0));
 
46
 
 
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)));
 
58
 
 
59
    /* Update appearance: */
 
60
    recalculateIndicatorState();
 
61
}
 
62
 
 
63
void UINetworkManagerIndicator::removeNetworkRequest(const QUuid &uuid)
 
64
{
 
65
    /* Make sure network-request still registered: */
 
66
    AssertMsg(m_ids.contains(uuid), ("Network-request already unregistered!"));
 
67
 
 
68
    /* Search for network-request index: */
 
69
    int iIndexOfRequiredElement = m_ids.indexOf(uuid);
 
70
 
 
71
    /* Delete corresponding network-request: */
 
72
    m_ids.remove(iIndexOfRequiredElement);
 
73
    m_data.remove(iIndexOfRequiredElement);
 
74
 
 
75
    /* Update appearance: */
 
76
    recalculateIndicatorState();
 
77
}
 
78
 
 
79
void UINetworkManagerIndicator::sltSetProgressToStarted(const QUuid &uuid)
 
80
{
 
81
    /* Make sure that network-request still registered: */
 
82
    AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
 
83
 
 
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;
 
89
    data.bytesTotal = 0;
 
90
    data.failed = false;
 
91
 
 
92
    /* Update appearance: */
 
93
    recalculateIndicatorState();
 
94
}
 
95
 
 
96
void UINetworkManagerIndicator::sltSetProgressToCanceled(const QUuid &uuid)
 
97
{
 
98
    /* Make sure that network-request still registered: */
 
99
    AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
 
100
    Q_UNUSED(uuid);
 
101
 
 
102
    /* Update appearance: */
 
103
    recalculateIndicatorState();
 
104
}
 
105
 
 
106
void UINetworkManagerIndicator::sltSetProgressToFailed(const QUuid &uuid, const QString &)
 
107
{
 
108
    /* Make sure that network-request still registered: */
 
109
    AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
 
110
 
 
111
    /* Search for network-request index: */
 
112
    int iIndexOfNetworkRequest = m_ids.indexOf(uuid);
 
113
    /* Update corresponding data: */
 
114
    UINetworkRequestData &data = m_data[iIndexOfNetworkRequest];
 
115
    data.failed = true;
 
116
 
 
117
    /* Update appearance: */
 
118
    recalculateIndicatorState();
 
119
}
 
120
 
 
121
void UINetworkManagerIndicator::sltSetProgressToFinished(const QUuid &uuid)
 
122
{
 
123
    /* Make sure that network-request still registered: */
 
124
    AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
 
125
    Q_UNUSED(uuid);
 
126
 
 
127
    /* Update appearance: */
 
128
    recalculateIndicatorState();
 
129
}
 
130
 
 
131
void UINetworkManagerIndicator::sltSetProgress(const QUuid &uuid, qint64 iReceived, qint64 iTotal)
 
132
{
 
133
    /* Make sure that network-request still registered: */
 
134
    AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
 
135
 
 
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;
 
142
 
 
143
    /* Update appearance: */
 
144
    updateAppearance();
 
145
}
 
146
 
 
147
void UINetworkManagerIndicator::retranslateUi()
 
148
{
 
149
    /* Update appearance: */
 
150
    updateAppearance();
 
151
}
 
152
 
 
153
void UINetworkManagerIndicator::recalculateIndicatorState()
 
154
{
 
155
    /* Check if there are network-requests at all: */
 
156
    if (m_ids.isEmpty())
 
157
    {
 
158
        /* Set state to 'idle': */
 
159
        setState(UINetworkManagerIndicatorState_Idle);
 
160
    }
 
161
    else
 
162
    {
 
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)
 
166
        {
 
167
            if (m_data[i].failed)
 
168
            {
 
169
                fIsThereAtLeastOneFailedNetworkRequest = true;
 
170
                break;
 
171
            }
 
172
        }
 
173
 
 
174
        /* If there it least one failed network-request: */
 
175
        if (fIsThereAtLeastOneFailedNetworkRequest)
 
176
        {
 
177
            /* Set state to 'error': */
 
178
            setState(UINetworkManagerIndicatorState_Error);
 
179
        }
 
180
        else
 
181
        {
 
182
            /* Set state to 'loading': */
 
183
            setState(UINetworkManagerIndicatorState_Loading);
 
184
        }
 
185
    }
 
186
 
 
187
    /* Update appearance finally: */
 
188
    updateAppearance();
 
189
}
 
190
 
 
191
void UINetworkManagerIndicator::updateAppearance()
 
192
{
 
193
    /* First of all, we are hiding LED in case of 'idle' state: */
 
194
    if (state() == UINetworkManagerIndicatorState_Idle && !isHidden())
 
195
        hide();
 
196
 
 
197
    /* Prepare description: */
 
198
    QString strDecription;
 
199
    /* Check if there are any network-requests: */
 
200
    if (!m_ids.isEmpty())
 
201
    {
 
202
        /* Prepare table: */
 
203
        QString strTable("<table>%1</table>");
 
204
        QString strBodyItem("<tr><td>%1</td><td>&nbsp;</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: */
 
212
        QString strBody;
 
213
        for (int i = 0; i < m_data.size(); ++i)
 
214
        {
 
215
            const UINetworkRequestData &data = m_data[i];
 
216
            const QString &strDescription = data.description;
 
217
            QString strStatus(data.failed ? tr("failed", "network operation") :
 
218
                                            tr("(%1 of %2)")
 
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;
 
223
        }
 
224
        /* Compose description: */
 
225
        strDecription = strParagraph.arg(strHeader + strTable.arg(strBody)) +
 
226
                        strParagraph.arg(strNobreak.arg(strItalic.arg(tr("Double-click for more information."))));
 
227
    }
 
228
    else
 
229
        strDecription = QString();
 
230
    /* Set description: */
 
231
    setToolTip(strDecription);
 
232
 
 
233
    /* Finally, we are showing LED in case of state is not 'idle': */
 
234
    if (state() != UINetworkManagerIndicatorState_Idle && isHidden())
 
235
        show();
 
236
}
 
237