da5f31020077d1176f3ea568455e58b50b42f519
[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     //Limit parallel downloads
56     if (downloadQueue.length() < MAX_PARALLEL_DOWNLOADS)
57         QTimer::singleShot(0, this, SLOT(startNextDownload()));
58
59     downloadQueue.enqueue(url);
60 }
61
62 void MapFetcher::startNextDownload()
63 {
64     qDebug() << __PRETTY_FUNCTION__;
65
66     if (downloadQueue.isEmpty())
67         return;
68
69     QUrl url = downloadQueue.dequeue();
70     QNetworkRequest request(url);
71     request.setRawHeader("User-Agent", "Map Test");
72     request.setAttribute(QNetworkRequest::CacheLoadControlAttribute,
73                                               QNetworkRequest::PreferCache);
74
75     QNetworkReply *reply = m_manager->get(request);
76     currentDownloads.append(reply);
77 }
78
79 void MapFetcher::downloadFinished(QNetworkReply *reply)
80 {
81     qDebug() << __PRETTY_FUNCTION__;
82
83     if (reply->error() == QNetworkReply::NoError) {
84         QImage image;
85         QUrl url = reply->url();
86
87         if (!image.load(reply, 0)) {
88             image = QImage();
89         }
90
91         emit mapImageReceived(url, QPixmap::fromImage(image));
92     }
93     else {
94         emit error(reply->errorString());
95     }
96
97     currentDownloads.removeAll(reply);
98     reply->deleteLater();
99     startNextDownload();
100 }
101
102 MapFetcher::~MapFetcher()
103 {
104
105 }