First version of imagefetcher. Added imagefetcher.cpp/.h
authorlampehe-local <henri.lampela@ixonos.com>
Tue, 27 Apr 2010 08:58:58 +0000 (11:58 +0300)
committerlampehe-local <henri.lampela@ixonos.com>
Tue, 27 Apr 2010 08:58:58 +0000 (11:58 +0300)
src/situareservice/imagefetcher.cpp [new file with mode: 0644]
src/situareservice/imagefetcher.h [new file with mode: 0644]
src/situareservice/situarecommon.h
src/situareservice/situareservice.cpp
src/situareservice/situareservice.h
src/src.pro

diff --git a/src/situareservice/imagefetcher.cpp b/src/situareservice/imagefetcher.cpp
new file mode 100644 (file)
index 0000000..825e9dc
--- /dev/null
@@ -0,0 +1,89 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+      Henri Lampela - henri.lampela@ixonos.com
+
+   Situare is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License
+   version 2 as published by the Free Software Foundation.
+
+   Situare is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Situare; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+   USA.
+*/
+
+#include <QDebug>
+#include <QNetworkRequest>
+#include <QNetworkReply>
+#include <QImage>
+#include "imagefetcher.h"
+
+ImageFetcher::ImageFetcher(QNetworkAccessManager *manager, QObject *parent)
+    : QObject(parent)
+    , m_manager(manager)
+{
+    connect(m_manager, SIGNAL(finished(QNetworkReply*)), this, SLOT(
+            downloadFinished(QNetworkReply*)));
+}
+
+void ImageFetcher::fetchImage(const QUrl &url)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (url.isEmpty() || !url.isValid())
+        return;
+
+    if (m_downloadQueue.size() >= DOWNLOAD_QUEUE_SIZE)
+        m_downloadQueue.dequeue();
+
+    m_downloadQueue.enqueue(url);
+
+    if (m_currentDownloads.size() < MAX_PARALLEL_DOWNLOADS)
+        startNextDownload();
+}
+
+void ImageFetcher::startNextDownload()
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (m_downloadQueue.isEmpty())
+        return;
+
+    QUrl url = m_downloadQueue.dequeue();
+
+    QNetworkRequest request(url);
+    request.setRawHeader("User-Agent", "Situare");
+    QNetworkReply *reply = m_manager->get(request);
+
+    m_currentDownloads.append(reply);
+}
+
+void ImageFetcher::downloadFinished(QNetworkReply *reply)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+
+    if (reply->error() == QNetworkReply::NoError) {
+        QImage image;
+        QUrl url = reply->url();
+
+        if (!image.load(reply, 0))
+            image = QImage();
+
+        //emit imageReceived(url, QPixmap::fromImage(image));
+        emit imageReceived(url, image);
+    }
+    else {
+        emit error(reply->errorString());
+    }
+
+    m_currentDownloads.removeAll(reply);
+    reply->deleteLater();
+    startNextDownload();
+}
diff --git a/src/situareservice/imagefetcher.h b/src/situareservice/imagefetcher.h
new file mode 100644 (file)
index 0000000..1404a65
--- /dev/null
@@ -0,0 +1,114 @@
+/*
+   Situare - A location system for Facebook
+   Copyright (C) 2010  Ixonos Plc. Authors:
+
+      Henri Lampela - henri.lampela@ixonos.com
+
+   Situare is free software; you can redistribute it and/or
+   modify it under the terms of the GNU General Public License
+   version 2 as published by the Free Software Foundation.
+
+   Situare is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with Situare; if not, write to the Free Software
+   Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301,
+   USA.
+*/
+
+#ifndef IMAGEFETCHER_H
+#define IMAGEFETCHER_H
+
+#include <QtCore>
+#include <QNetworkAccessManager>
+
+class QNetworkReply;
+class QUrl;
+
+/**
+* @brief ImageFetcher handles requests to get images.
+*
+* @author Henri Lampela
+*/
+class ImageFetcher : public QObject
+{
+    Q_OBJECT
+
+public:
+
+    /**
+    * @brief Default constructor
+    *
+    * @param manager QNetworkAccessManager
+    * @param parent Parent object
+    */
+    ImageFetcher(QNetworkAccessManager *manager, QObject *parent = 0);
+
+/*******************************************************************************
+ * CLASS SPECIFIC MEMBER FUNCTIONS AND SLOTS
+ ******************************************************************************/
+public slots:
+
+    /**
+    * @brief Fecth image from given url
+    *
+    * @param url Image url
+    */
+    void fetchImage(const QUrl &url);
+
+private slots:
+
+    /**
+    * @brief This slot is called when network manager has finished
+    * the download. Loads image and emits imageReceived signal with
+    * url and image. If there was a error in reply emits error-signal.
+    *
+    * @param reply
+    */
+    void downloadFinished(QNetworkReply *reply);
+
+    /**
+    * @brief This slot is called when next download is started. Takes url
+    * from queue, sends request and puts request to download queue.
+    */
+    void startNextDownload();
+
+/*******************************************************************************
+ * SIGNALS
+ ******************************************************************************/
+signals:
+    /**
+    * @brief Signal which is emitted when image
+    * is received from the server and loaded to pixmap.
+    *
+    * @param url URL to image
+    * @param image image QImage
+    */
+    //void imageReceived(const QUrl &url, const QPixmap &image);
+    void imageReceived(const QUrl &url, const QImage &image);
+
+    /**
+    * @brief Signal which is emitted when there is error
+    * in network reply.
+    *
+    * @param message error message
+    */
+    void error(const QString &message);
+
+/*******************************************************************************
+ * DATA MEMBERS
+ ******************************************************************************/
+private:
+    static const int MAX_PARALLEL_DOWNLOADS = 2; ///< Max simultaneous parallel downloads
+    static const int DOWNLOAD_QUEUE_SIZE = 50; ///< Max downloads waiting in queue
+
+    QList<QNetworkReply*> m_currentDownloads; ///< List of current downloads
+    QQueue<QUrl> m_downloadQueue;             ///< Queue of pending requests
+    QNetworkAccessManager *m_manager;       ///< Network access manager
+
+};
+
+#endif // IMAGEFETCHER_H
index 19824ec..09c2204 100644 (file)
@@ -44,9 +44,6 @@ const QString EXPIRES = "expires";
 const QString SESSION_KEY = "session_key";
 const QString SESSION_SECRET = "ss";
 
-const QString USER_AGENT = "User-Agent";
-const QString AGENT = "Agent";
-
 // Locales
 const QString LOCALE = "locale";
 const QString EN_LOCALE = "en_EN";
index f801cd2..1723292 100644 (file)
@@ -36,6 +36,11 @@ SituareService::SituareService(QObject *parent, QNetworkAccessManager *manager)
 
     connect(m_networkManager, SIGNAL(finished(QNetworkReply*)), SLOT(requestFinished(QNetworkReply*)));
 
+    m_imageFetcher = new ImageFetcher(new QNetworkAccessManager(this), this);
+    connect(this, SIGNAL(fetchImage(QUrl)), m_imageFetcher, SLOT(fetchImage(QUrl)));
+    connect(m_imageFetcher, SIGNAL(imageReceived(QUrl,QImage)), this,
+            SLOT(imageReceived(QUrl, QImage)));
+
     m_credentials = m_facebookAuthentication.loginCredentials();
 }
 
@@ -270,3 +275,10 @@ void SituareService::parseUserData(const QByteArray &jsonReply)
     }
     emit userDataChanged(user, friendsList);
 }
+
+void SituareService::imageReceived(const QUrl &url, const QImage &image)
+{
+    qDebug() << __PRETTY_FUNCTION__;
+    qDebug() << "Image URL: " << url << " size :" << image.size();
+    //ToDo: where now?
+}
index e574b71..afaa628 100644 (file)
@@ -27,6 +27,7 @@
 #include "../facebookservice/facebookauthentication.h"
 #include "../facebookservice/facebookcredentials.h"
 #include "../user/user.h"
+#include "imagefetcher.h"
 
 class QNetworkAccessManager;
 class QNetworkReply;
@@ -139,6 +140,16 @@ private:
     */
     void sendRequest(const QUrl &url, const QString &cookieType, const QString &cookie);
 
+private slots:
+
+    /**
+    * @brief Slot for received images
+    *
+    * @param url Image url
+    * @param image Received image
+    */
+    void imageReceived(const QUrl &url, const QImage &image);
+
 /*******************************************************************************
  * SIGNALS
  ******************************************************************************/
@@ -153,6 +164,13 @@ signals:
     void error(const QString &error);
 
     /**
+    * @brief Signal for image fetching
+    *
+    * @param url Image url
+    */
+    void fetchImage(const QUrl &url);
+
+    /**
     * @brief Signals when address data is retrieved
     *
     * @param address Street address
@@ -177,6 +195,7 @@ private:
     QList<QNetworkReply *> m_currentRequests; ///< List of current http requests
     FacebookAuthentication m_facebookAuthentication; ///< Pointer to FacebookAuthentication
     QNetworkAccessManager *m_networkManager; ///< Pointer to QNetworkAccessManager
+    ImageFetcher *m_imageFetcher;
 };
 
 #endif // SITUARESERVICE_H
index a97d212..7d33f8b 100644 (file)
@@ -11,6 +11,7 @@ SOURCES += main.cpp \
     ui/mapviewscreen.cpp \
     ui/listviewscreen.cpp \
     situareservice/situareservice.cpp \
+    situareservice/imagefetcher.cpp \
     cookiehandler/cookiehandler.cpp \
     facebookservice/facebookcredentials.cpp \
     facebookservice/facebookauthentication.cpp \
@@ -40,6 +41,7 @@ HEADERS += ui/mainwindow.h \
     ui/updatelocation/texteditautoresizer.h \
     situareservice/situareservice.h \
     situareservice/situarecommon.h \
+    situareservice/imagefetcher.h \
     cookiehandler/cookiehandler.h \
     facebookservice/facebookcredentials.h \
     facebookservice/facebookauthentication.h \