430474e5ec398f769830a548d818d75ff37d5113vboxsync/* $Id$ */
430474e5ec398f769830a548d818d75ff37d5113vboxsync/** @file
d34409ad02ea0d28e08a6c4b089a412fdb3b4c9cvboxsync * VBox Qt GUI - UINetworkManagerIndicator stuff implementation.
430474e5ec398f769830a548d818d75ff37d5113vboxsync */
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync/*
430474e5ec398f769830a548d818d75ff37d5113vboxsync * Copyright (C) 2012 Oracle Corporation
430474e5ec398f769830a548d818d75ff37d5113vboxsync *
430474e5ec398f769830a548d818d75ff37d5113vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
430474e5ec398f769830a548d818d75ff37d5113vboxsync * available from http://www.virtualbox.org. This file is free software;
430474e5ec398f769830a548d818d75ff37d5113vboxsync * you can redistribute it and/or modify it under the terms of the GNU
430474e5ec398f769830a548d818d75ff37d5113vboxsync * General Public License (GPL) as published by the Free Software
430474e5ec398f769830a548d818d75ff37d5113vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
430474e5ec398f769830a548d818d75ff37d5113vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
430474e5ec398f769830a548d818d75ff37d5113vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
430474e5ec398f769830a548d818d75ff37d5113vboxsync */
430474e5ec398f769830a548d818d75ff37d5113vboxsync
a9b62afc62b28da7a1a77f34259f8013892d9664vboxsync#ifdef VBOX_WITH_PRECOMPILED_HEADERS
a9b62afc62b28da7a1a77f34259f8013892d9664vboxsync# include <precomp.h>
a9b62afc62b28da7a1a77f34259f8013892d9664vboxsync#else /* !VBOX_WITH_PRECOMPILED_HEADERS */
a9b62afc62b28da7a1a77f34259f8013892d9664vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync/* Local includes: */
a9b62afc62b28da7a1a77f34259f8013892d9664vboxsync# include "UINetworkManagerIndicator.h"
a9b62afc62b28da7a1a77f34259f8013892d9664vboxsync# include "UINetworkRequest.h"
a9b62afc62b28da7a1a77f34259f8013892d9664vboxsync# include "VBoxGlobal.h"
a9b62afc62b28da7a1a77f34259f8013892d9664vboxsync
a9b62afc62b28da7a1a77f34259f8013892d9664vboxsync#endif /* !VBOX_WITH_PRECOMPILED_HEADERS */
a9b62afc62b28da7a1a77f34259f8013892d9664vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsyncUINetworkManagerIndicator::UINetworkManagerIndicator()
430474e5ec398f769830a548d818d75ff37d5113vboxsync{
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Assign state icons: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync setStateIcon(UINetworkManagerIndicatorState_Idle, QPixmap(":/nw_16px.png"));
430474e5ec398f769830a548d818d75ff37d5113vboxsync setStateIcon(UINetworkManagerIndicatorState_Loading, QPixmap(":/nw_loading_16px.png"));
430474e5ec398f769830a548d818d75ff37d5113vboxsync setStateIcon(UINetworkManagerIndicatorState_Error, QPixmap(":/nw_error_16px.png"));
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Translate content: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync retranslateUi();
430474e5ec398f769830a548d818d75ff37d5113vboxsync}
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsyncvoid UINetworkManagerIndicator::addNetworkRequest(UINetworkRequest *pNetworkRequest)
430474e5ec398f769830a548d818d75ff37d5113vboxsync{
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Make sure network-request is really exists: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync AssertMsg(pNetworkRequest, ("Invalid network-request passed!"));
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Make sure network-request was NOT registered yet: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync AssertMsg(!m_ids.contains(pNetworkRequest->uuid()), ("Network-request already registered!"));
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Append network-request data: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync m_ids.append(pNetworkRequest->uuid());
430474e5ec398f769830a548d818d75ff37d5113vboxsync m_data.append(UINetworkRequestData(pNetworkRequest->description(), 0, 0));
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Prepare network-request listeners: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync connect(pNetworkRequest, SIGNAL(sigStarted(const QUuid &)),
430474e5ec398f769830a548d818d75ff37d5113vboxsync this, SLOT(sltSetProgressToStarted(const QUuid &)));
430474e5ec398f769830a548d818d75ff37d5113vboxsync connect(pNetworkRequest, SIGNAL(sigCanceled(const QUuid &)),
430474e5ec398f769830a548d818d75ff37d5113vboxsync this, SLOT(sltSetProgressToCanceled(const QUuid &)));
430474e5ec398f769830a548d818d75ff37d5113vboxsync connect(pNetworkRequest, SIGNAL(sigFinished(const QUuid &)),
430474e5ec398f769830a548d818d75ff37d5113vboxsync this, SLOT(sltSetProgressToFinished(const QUuid &)));
430474e5ec398f769830a548d818d75ff37d5113vboxsync connect(pNetworkRequest, SIGNAL(sigFailed(const QUuid &, const QString &)),
430474e5ec398f769830a548d818d75ff37d5113vboxsync this, SLOT(sltSetProgressToFailed(const QUuid &, const QString &)));
430474e5ec398f769830a548d818d75ff37d5113vboxsync connect(pNetworkRequest, SIGNAL(sigProgress(const QUuid &, qint64, qint64)),
430474e5ec398f769830a548d818d75ff37d5113vboxsync this, SLOT(sltSetProgress(const QUuid &, qint64, qint64)));
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Update appearance: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync recalculateIndicatorState();
430474e5ec398f769830a548d818d75ff37d5113vboxsync}
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsyncvoid UINetworkManagerIndicator::removeNetworkRequest(const QUuid &uuid)
430474e5ec398f769830a548d818d75ff37d5113vboxsync{
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Make sure network-request still registered: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync AssertMsg(m_ids.contains(uuid), ("Network-request already unregistered!"));
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Search for network-request index: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync int iIndexOfRequiredElement = m_ids.indexOf(uuid);
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Delete corresponding network-request: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync m_ids.remove(iIndexOfRequiredElement);
430474e5ec398f769830a548d818d75ff37d5113vboxsync m_data.remove(iIndexOfRequiredElement);
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Update appearance: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync recalculateIndicatorState();
430474e5ec398f769830a548d818d75ff37d5113vboxsync}
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsyncvoid UINetworkManagerIndicator::sltSetProgressToStarted(const QUuid &uuid)
430474e5ec398f769830a548d818d75ff37d5113vboxsync{
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Make sure that network-request still registered: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Search for network-request index: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync int iIndexOfNetworkRequest = m_ids.indexOf(uuid);
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Update corresponding network-request data: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync UINetworkRequestData &data = m_data[iIndexOfNetworkRequest];
430474e5ec398f769830a548d818d75ff37d5113vboxsync data.bytesReceived = 0;
430474e5ec398f769830a548d818d75ff37d5113vboxsync data.bytesTotal = 0;
430474e5ec398f769830a548d818d75ff37d5113vboxsync data.failed = false;
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Update appearance: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync recalculateIndicatorState();
430474e5ec398f769830a548d818d75ff37d5113vboxsync}
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsyncvoid UINetworkManagerIndicator::sltSetProgressToCanceled(const QUuid &uuid)
430474e5ec398f769830a548d818d75ff37d5113vboxsync{
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Make sure that network-request still registered: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
430474e5ec398f769830a548d818d75ff37d5113vboxsync Q_UNUSED(uuid);
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Update appearance: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync recalculateIndicatorState();
430474e5ec398f769830a548d818d75ff37d5113vboxsync}
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsyncvoid UINetworkManagerIndicator::sltSetProgressToFailed(const QUuid &uuid, const QString &)
430474e5ec398f769830a548d818d75ff37d5113vboxsync{
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Make sure that network-request still registered: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Search for network-request index: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync int iIndexOfNetworkRequest = m_ids.indexOf(uuid);
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Update corresponding data: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync UINetworkRequestData &data = m_data[iIndexOfNetworkRequest];
430474e5ec398f769830a548d818d75ff37d5113vboxsync data.failed = true;
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Update appearance: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync recalculateIndicatorState();
430474e5ec398f769830a548d818d75ff37d5113vboxsync}
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsyncvoid UINetworkManagerIndicator::sltSetProgressToFinished(const QUuid &uuid)
430474e5ec398f769830a548d818d75ff37d5113vboxsync{
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Make sure that network-request still registered: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
430474e5ec398f769830a548d818d75ff37d5113vboxsync Q_UNUSED(uuid);
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Update appearance: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync recalculateIndicatorState();
430474e5ec398f769830a548d818d75ff37d5113vboxsync}
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsyncvoid UINetworkManagerIndicator::sltSetProgress(const QUuid &uuid, qint64 iReceived, qint64 iTotal)
430474e5ec398f769830a548d818d75ff37d5113vboxsync{
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Make sure that network-request still registered: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync AssertMsg(m_ids.contains(uuid), ("That network-request already unregistered!"));
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Search for network-request index: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync int iIndexOfNetworkRequest = m_ids.indexOf(uuid);
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Update corresponding network-request data: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync UINetworkRequestData &data = m_data[iIndexOfNetworkRequest];
430474e5ec398f769830a548d818d75ff37d5113vboxsync data.bytesReceived = iReceived;
430474e5ec398f769830a548d818d75ff37d5113vboxsync data.bytesTotal = iTotal;
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Update appearance: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync updateAppearance();
430474e5ec398f769830a548d818d75ff37d5113vboxsync}
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsyncvoid UINetworkManagerIndicator::retranslateUi()
430474e5ec398f769830a548d818d75ff37d5113vboxsync{
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Update appearance: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync updateAppearance();
430474e5ec398f769830a548d818d75ff37d5113vboxsync}
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsyncvoid UINetworkManagerIndicator::recalculateIndicatorState()
430474e5ec398f769830a548d818d75ff37d5113vboxsync{
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Check if there are network-requests at all: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync if (m_ids.isEmpty())
430474e5ec398f769830a548d818d75ff37d5113vboxsync {
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Set state to 'idle': */
430474e5ec398f769830a548d818d75ff37d5113vboxsync setState(UINetworkManagerIndicatorState_Idle);
430474e5ec398f769830a548d818d75ff37d5113vboxsync }
430474e5ec398f769830a548d818d75ff37d5113vboxsync else
430474e5ec398f769830a548d818d75ff37d5113vboxsync {
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Check if there is at least one failed network-request: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync bool fIsThereAtLeastOneFailedNetworkRequest = false;
430474e5ec398f769830a548d818d75ff37d5113vboxsync for (int i = 0; i < m_data.size(); ++i)
430474e5ec398f769830a548d818d75ff37d5113vboxsync {
430474e5ec398f769830a548d818d75ff37d5113vboxsync if (m_data[i].failed)
430474e5ec398f769830a548d818d75ff37d5113vboxsync {
430474e5ec398f769830a548d818d75ff37d5113vboxsync fIsThereAtLeastOneFailedNetworkRequest = true;
430474e5ec398f769830a548d818d75ff37d5113vboxsync break;
430474e5ec398f769830a548d818d75ff37d5113vboxsync }
430474e5ec398f769830a548d818d75ff37d5113vboxsync }
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* If there it least one failed network-request: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync if (fIsThereAtLeastOneFailedNetworkRequest)
430474e5ec398f769830a548d818d75ff37d5113vboxsync {
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Set state to 'error': */
430474e5ec398f769830a548d818d75ff37d5113vboxsync setState(UINetworkManagerIndicatorState_Error);
430474e5ec398f769830a548d818d75ff37d5113vboxsync }
430474e5ec398f769830a548d818d75ff37d5113vboxsync else
430474e5ec398f769830a548d818d75ff37d5113vboxsync {
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Set state to 'loading': */
430474e5ec398f769830a548d818d75ff37d5113vboxsync setState(UINetworkManagerIndicatorState_Loading);
430474e5ec398f769830a548d818d75ff37d5113vboxsync }
430474e5ec398f769830a548d818d75ff37d5113vboxsync }
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Update appearance finally: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync updateAppearance();
430474e5ec398f769830a548d818d75ff37d5113vboxsync}
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsyncvoid UINetworkManagerIndicator::updateAppearance()
430474e5ec398f769830a548d818d75ff37d5113vboxsync{
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* First of all, we are hiding LED in case of 'idle' state: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync if (state() == UINetworkManagerIndicatorState_Idle && !isHidden())
430474e5ec398f769830a548d818d75ff37d5113vboxsync hide();
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Prepare description: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync QString strDecription;
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Check if there are any network-requests: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync if (!m_ids.isEmpty())
430474e5ec398f769830a548d818d75ff37d5113vboxsync {
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Prepare table: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync QString strTable("<table>%1</table>");
430474e5ec398f769830a548d818d75ff37d5113vboxsync QString strBodyItem("<tr><td>%1</td><td>&nbsp;</td><td>%2</td></tr>");
430474e5ec398f769830a548d818d75ff37d5113vboxsync QString strParagraph("<p>%1</p>");
430474e5ec398f769830a548d818d75ff37d5113vboxsync QString strBoldNobreak("<nobr><b>%1</b></nobr>");
430474e5ec398f769830a548d818d75ff37d5113vboxsync QString strNobreak("<nobr>%1</nobr>");
430474e5ec398f769830a548d818d75ff37d5113vboxsync QString strItalic("<i>%1</i>");
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Prepare header: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync QString strHeader(strBoldNobreak.arg(tr("Current network operations:")));
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Prepare table body: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync QString strBody;
430474e5ec398f769830a548d818d75ff37d5113vboxsync for (int i = 0; i < m_data.size(); ++i)
430474e5ec398f769830a548d818d75ff37d5113vboxsync {
430474e5ec398f769830a548d818d75ff37d5113vboxsync const UINetworkRequestData &data = m_data[i];
430474e5ec398f769830a548d818d75ff37d5113vboxsync const QString &strDescription = data.description;
430474e5ec398f769830a548d818d75ff37d5113vboxsync QString strStatus(data.failed ? tr("failed", "network operation") :
430474e5ec398f769830a548d818d75ff37d5113vboxsync tr("(%1 of %2)")
430474e5ec398f769830a548d818d75ff37d5113vboxsync .arg(vboxGlobal().formatSize(data.bytesReceived))
430474e5ec398f769830a548d818d75ff37d5113vboxsync .arg(vboxGlobal().formatSize(data.bytesTotal)));
430474e5ec398f769830a548d818d75ff37d5113vboxsync QString strBodyLine(strBodyItem.arg(strNobreak.arg(strDescription)).arg(strNobreak.arg(strStatus)));
430474e5ec398f769830a548d818d75ff37d5113vboxsync strBody += strBodyLine;
430474e5ec398f769830a548d818d75ff37d5113vboxsync }
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Compose description: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync strDecription = strParagraph.arg(strHeader + strTable.arg(strBody)) +
430474e5ec398f769830a548d818d75ff37d5113vboxsync strParagraph.arg(strNobreak.arg(strItalic.arg(tr("Double-click for more information."))));
430474e5ec398f769830a548d818d75ff37d5113vboxsync }
430474e5ec398f769830a548d818d75ff37d5113vboxsync else
430474e5ec398f769830a548d818d75ff37d5113vboxsync strDecription = QString();
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Set description: */
430474e5ec398f769830a548d818d75ff37d5113vboxsync setToolTip(strDecription);
430474e5ec398f769830a548d818d75ff37d5113vboxsync
430474e5ec398f769830a548d818d75ff37d5113vboxsync /* Finally, we are showing LED in case of state is not 'idle': */
430474e5ec398f769830a548d818d75ff37d5113vboxsync if (state() != UINetworkManagerIndicatorState_Idle && isHidden())
430474e5ec398f769830a548d818d75ff37d5113vboxsync show();
430474e5ec398f769830a548d818d75ff37d5113vboxsync}
430474e5ec398f769830a548d818d75ff37d5113vboxsync