Modified reviewed code.
[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 #include "mapcommon.h"
33
34 MapFetcher::MapFetcher(QNetworkAccessManager *manager, QObject *parent)
35     : QObject(parent)
36     , m_manager(manager)
37 {
38     QNetworkDiskCache *diskCache = new QNetworkDiskCache(this);
39     diskCache->setCacheDirectory(QDesktopServices::storageLocation(
40             QDesktopServices::CacheLocation));
41     m_manager->setCache(diskCache);
42
43     connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(
44             downloadFinished(QNetworkReply*)));
45 }
46
47 void MapFetcher::fetchMapImage(const QUrl &url)
48 {
49     //qDebug() << __PRETTY_FUNCTION__;
50
51     if (url.isEmpty() || !url.isValid())
52         return;
53
54     if (loadImageFromCache(url))
55         return;
56
57     if (m_downloadQueue.size() >= DOWNLOAD_QUEUE_SIZE)
58         m_downloadQueue.dequeue();
59
60     m_downloadQueue.enqueue(url);
61
62     if (m_currentDownloads.size() < MAX_PARALLEL_DOWNLOADS)
63         startNextDownload();
64 }
65
66 bool MapFetcher::loadImageFromCache(const QUrl &url)
67 {
68     //qDebug() << __PRETTY_FUNCTION__;
69
70     bool imageFound = false;
71
72     if (m_manager->cache()) {
73         QIODevice *cacheImage = m_manager->cache()->data(url);
74
75         if (cacheImage) {
76             QImage image;
77
78             if (image.load(cacheImage, 0)) {
79                 imageFound = true;
80                 emit mapImageReceived(url, QPixmap::fromImage(image));
81             }
82
83             delete cacheImage;
84         }
85     }
86
87     return imageFound;
88 }
89
90 void MapFetcher::startNextDownload()
91 {
92     //qDebug() << __PRETTY_FUNCTION__;
93
94     if (m_downloadQueue.isEmpty())
95         return;
96
97     QUrl url = m_downloadQueue.dequeue();
98
99     QNetworkRequest request(url);
100     /// @todo Jussi: set user agent DONE
101     request.setRawHeader("User-Agent", "Situare");
102     QNetworkReply *reply = m_manager->get(request);
103
104     m_currentDownloads.append(reply);
105 }
106
107 void MapFetcher::downloadFinished(QNetworkReply *reply)
108 {
109     //qDebug() << __PRETTY_FUNCTION__;
110
111     if (reply->error() == QNetworkReply::NoError) {
112         QImage image;
113         QUrl url = reply->url();
114
115         if (!image.load(reply, 0))
116             image = QImage();
117
118         emit mapImageReceived(url, QPixmap::fromImage(image));
119     }
120     else {
121         emit error(reply->errorString());
122     }
123
124     m_currentDownloads.removeAll(reply);
125     reply->deleteLater();
126     startNextDownload();
127 }
128
129 /// @todo Jussi: remove destructor DONE