Modified test cases.
[situare] / src / map / mapfetcher.cpp
1 /*
2    Situare - A location system for Facebook
3    Copyright (C) 2010  Ixonos Plc. Authors:
4
5        Jussi Laitinen - jussi.laitinen@ixonos.com
6
7    Situare is free software; you can redistribute it and/or
8    modify it under the terms of the GNU General Public License
9    version 2 as published by the Free Software Foundation.
10
11    Situare is distributed in the hope that it will be useful,
12    but WITHOUT ANY WARRANTY; without even the implied warranty of
13    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14    GNU General Public License for more details.
15
16    You should have received a copy of the GNU General Public License
17    along with Situare; if not, write to the Free Software
18    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
19    USA.
20 */
21
22 #include <QNetworkAccessManager>
23 #include <QNetworkRequest>
24 #include <QNetworkReply>
25 #include <QUrl>
26 #include <QDebug>
27 #include <QPixmap>
28 #include <QNetworkDiskCache>
29 #include <QDesktopServices>
30
31 #include "mapfetcher.h"
32
33 static int MAX_PARALLEL_DOWNLOADS = 2;
34
35 MapFetcher::MapFetcher(QObject *parent, QNetworkAccessManager *manager)
36     : QObject(parent), m_manager(manager)
37 {
38     QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
39
40     diskCache->setCacheDirectory(QDesktopServices::storageLocation(
41             QDesktopServices::CacheLocation));
42
43     m_manager->setCache(diskCache);
44
45     connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(downloadFinished(QNetworkReply*)));
46 }
47
48 void MapFetcher::fetchMapImage(const QUrl &url)
49 {
50     qDebug() << __PRETTY_FUNCTION__;
51
52     if (url.isEmpty() || !url.isValid())
53         return;
54
55     if (downloadQueue.length() < MAX_PARALLEL_DOWNLOADS)
56         QTimer::singleShot(0, this, SLOT(startNextDownload()));
57
58     downloadQueue.enqueue(url);
59 }
60
61 void MapFetcher::startNextDownload()
62 {
63     qDebug() << __PRETTY_FUNCTION__;
64
65     if (downloadQueue.isEmpty())
66         return;
67
68     QUrl url = downloadQueue.dequeue();
69     QNetworkRequest request(url);
70     request.setRawHeader("User-Agent", "Map Test");
71     request.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
72                                               QNetworkRequest::PreferCache);
73
74     QNetworkReply *reply = m_manager->get(request);
75     currentDownloads.append(reply);
76 }
77
78 void MapFetcher::downloadFinished(QNetworkReply *reply)
79 {
80     qDebug() << __PRETTY_FUNCTION__;
81
82     if (reply->error() == QNetworkReply::NoError) {
83         QImage image;
84         QUrl url = reply->url();
85
86         if (!image.load(reply, 0))
87             image = QImage();
88
89         emit mapImageReceived(url, QPixmap::fromImage(image));
90     }
91     else {
92         emit error(reply->errorString());
93     }
94
95     currentDownloads.removeAll(reply);
96     reply->deleteLater();
97     startNextDownload();
98 }
99
100 MapFetcher::~MapFetcher()
101 {
102
103 }