From 781285403a8ec9495bcb3dcadc31ad3ce1bec4a2 Mon Sep 17 00:00:00 2001 From: kh1 Date: Mon, 10 Oct 2011 14:28:19 +0200 Subject: Test app for download speed implementation. Change-Id: I97d3048adc8d79bfc27fa2201579f881664b0c8a Reviewed-on: http://codereview.qt-project.org/6325 Sanity-Review: Qt Sanity Bot Reviewed-by: Tim Jenssen --- examples/downloadspeed/downloadspeed.pro | 14 +++ examples/downloadspeed/main.cpp | 156 +++++++++++++++++++++++++++++++ examples/examples.pro | 2 +- 3 files changed, 171 insertions(+), 1 deletion(-) create mode 100644 examples/downloadspeed/downloadspeed.pro create mode 100644 examples/downloadspeed/main.cpp (limited to 'examples') diff --git a/examples/downloadspeed/downloadspeed.pro b/examples/downloadspeed/downloadspeed.pro new file mode 100644 index 000000000..e082c1f65 --- /dev/null +++ b/examples/downloadspeed/downloadspeed.pro @@ -0,0 +1,14 @@ +include(../../installerbuilder/libinstaller/libinstaller.pri) +DEPENDPATH += ../../installerbuilder/libinstaller ../../installerbuilder/common +INCLUDEPATH += ../../installerbuilder/libinstaller ../../installerbuilder/common + +TEMPLATE = app +CONFIG += console +CONFIG -= app_bundle +TARGET = downloadspeed + +QT -= gui +QT += core network +LIBS = -L$$OUT_PWD/../../installerbuilder/lib -linstaller $$LIBS + +SOURCES += main.cpp diff --git a/examples/downloadspeed/main.cpp b/examples/downloadspeed/main.cpp new file mode 100644 index 000000000..789d30f66 --- /dev/null +++ b/examples/downloadspeed/main.cpp @@ -0,0 +1,156 @@ +/**************************************************************************** +** Copyright (C) 2001-2010 Klaralvdalens Datakonsult AB. All rights reserved. +** +** This file is part of the KD Tools library. +** +** Licensees holding valid commercial KD Tools licenses may use this file in +** accordance with the KD Tools Commercial License Agreement provided with +** the Software. +** +** +** This file may be distributed and/or modified under the terms of the +** GNU Lesser General Public License version 2 and version 3 as published by the +** Free Software Foundation and appearing in the file LICENSE.LGPL included. +** +** This file is provided AS IS with NO WARRANTY OF ANY KIND, INCLUDING THE +** WARRANTY OF DESIGN, MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. +** +** Contact info@kdab.com if any conditions of this licensing are not +** clear to you. +** +**********************************************************************/ +#include +#include + +#include +#include +#include +#include + +static QString format(double data) +{ + if (data < 1024.0) + return QString::fromLatin1("%L1 B").arg(data); + data /= 1024.0; + if (data < 1024.0) + return QString::fromLatin1("%L1 KB").arg(data, 0, 'f', 2); + data /= 1024.0; + if (data < 1024.0) + return QString::fromLatin1("%L1 MB").arg(data, 0, 'f', 2); + data /= 1024.0; + return QString::fromLatin1("%L1 GB").arg(data, 0, 'f', 2); +} + + +// -- Receiver + +class Receiver : public QObject +{ + Q_OBJECT + +public: + Receiver() : QObject(), m_downloadFinished(false) {} + ~Receiver() {} + + inline bool downloaded() { return m_downloadFinished; } + +public slots: + void downloadStarted() { m_downloadFinished = false; } + void downloadFinished() { m_downloadFinished = true; } + void downloadAborted(const QString &error) { m_downloadFinished = true; qDebug() << "Error:" << error; } + + void downloadSpeed(qint64 speed) + { + qDebug() << "Download speed: " << format(speed) + "/sec"; + } + + void downloadProgress(double progress) + { + qDebug() << "Progress: " << progress; + } + + void estimatedDownloadTime(int time) + { + if (time <= 0) { + qDebug() << "Unknown time remaining."; + return; + } + + const int d = time / 86400; + const int h = (time / 3600) - (d * 24); + const int m = (time / 60) - (d * 1440) - (h * 60); + const int s = time % 60; + + QString days; + if (d > 0) + days = QString::number(d) + (d < 2 ? " day" : " days") + QLatin1String(", "); + + QString hours; + if (h > 0) + hours = QString::number(h) + (h < 2 ? " hour" : " hours") + QLatin1String(", "); + + QString minutes; + if (m > 0) + minutes = QString::number(m) + (m < 2 ? " minute" : " minutes"); + + QString seconds; + if (s >= 0 && minutes.isEmpty()) + seconds = QString::number(s) + (s < 2 ? " second" : " seconds"); + + qDebug() << days + hours + minutes + seconds + tr(" remaining."); + } + + void downloadStatus(const QString &status) + { + qDebug() << status; + } + + void downloadProgress(qint64 bytesReceived, qint64 bytesToReceive) + { + qDebug() << "Bytes received: " << bytesReceived << ", Bytes to receive: " << bytesToReceive; + } + +private: + volatile bool m_downloadFinished; +}; + + +// http://get.qt.nokia.com/qt/source/qt-mac-opensource-4.7.4.dmg +// ftp://ftp.trolltech.com/qt/source/qt-mac-opensource-4.7.4.dmg + +int main(int argc, char *argv[]) +{ + QCoreApplication a(argc, argv); + + if (a.arguments().count() < 2) + return EXIT_FAILURE; + + const QUrl url(a.arguments().value(1)); + qDebug() << url.toString(); + KDUpdater::FileDownloader *loader = KDUpdater::FileDownloaderFactory::instance().create(url.scheme()); + if (loader) { + loader->setUrl(url); + + Receiver r; + r.connect(loader, SIGNAL(downloadStarted()), &r, SLOT(downloadStarted())); + r.connect(loader, SIGNAL(downloadCanceled()), &r, SLOT(downloadFinished())); + r.connect(loader, SIGNAL(downloadCompleted()), &r, SLOT(downloadFinished())); + r.connect(loader, SIGNAL(downloadAborted(QString)), &r, SLOT(downloadAborted(QString))); + + r.connect(loader, SIGNAL(downloadSpeed(qint64)), &r, SLOT(downloadSpeed(qint64))); + r.connect(loader, SIGNAL(downloadStatus(QString)), &r, SLOT(downloadStatus(QString))); + r.connect(loader, SIGNAL(downloadProgress(double)), &r, SLOT(downloadProgress(double))); + r.connect(loader, SIGNAL(estimatedDownloadTime(int)), &r, SLOT(destimatedDownloadTime(int))); + r.connect(loader, SIGNAL(downloadProgress(qint64, qint64)), &r, SLOT(downloadProgress(qint64, qint64))); + + loader->download(); + while (!r.downloaded()) + QCoreApplication::processEvents(); + + delete loader; + } + + return EXIT_SUCCESS; +} + +#include "main.moc" diff --git a/examples/examples.pro b/examples/examples.pro index 7efd15957..7a2731edb 100644 --- a/examples/examples.pro +++ b/examples/examples.pro @@ -1,3 +1,3 @@ TEMPLATE = subdirs CONFIG += ordered -SUBDIRS += testapp +SUBDIRS += testapp downloadspeed -- cgit v1.2.3