summaryrefslogtreecommitdiffstats
path: root/examples
diff options
context:
space:
mode:
authorkh1 <karsten.heimrich@nokia.com>2011-10-10 14:28:19 +0200
committerKarsten Heimrich <karsten.heimrich@nokia.com>2011-10-10 14:42:33 +0200
commit781285403a8ec9495bcb3dcadc31ad3ce1bec4a2 (patch)
tree232d0c455e567929b14e7fb86844fa66c1ef47a8 /examples
parent73bf7e045a6ed228dae4f2a9d3e83e5151a72720 (diff)
Test app for download speed implementation.
Change-Id: I97d3048adc8d79bfc27fa2201579f881664b0c8a Reviewed-on: http://codereview.qt-project.org/6325 Sanity-Review: Qt Sanity Bot <qt_sanity_bot@ovi.com> Reviewed-by: Tim Jenssen <tim.jenssen@nokia.com>
Diffstat (limited to 'examples')
-rw-r--r--examples/downloadspeed/downloadspeed.pro14
-rw-r--r--examples/downloadspeed/main.cpp156
-rw-r--r--examples/examples.pro2
3 files changed, 171 insertions, 1 deletions
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 <KDUpdater/FileDownloader>
+#include <KDUpdater/FileDownloaderFactory>
+
+#include <QtCore/QCoreApplication>
+#include <QtCore/QDebug>
+#include <QtCore/QObject>
+#include <QtCore/QUrl>
+
+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