example/DemoGUI/ImageDownload.cpp
author cgandhi
Thu, 23 Sep 2010 17:15:03 +0530
changeset 23 574948b60dab
parent 16 b78fa4cdbf2b
child 26 83d6a149c755
permissions -rw-r--r--
updating the lastfm music plugins and adding the smfhelp.chm
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
16
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
     1
/****************************************************************************
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
     2
**
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
     3
** Trolltech hereby grants a license to use the Qt/Eclipse Integration
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
     4
** plug-in (the software contained herein), in binary form, solely for the
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
     5
** purpose of creating code to be used with Trolltech's Qt software.
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
     6
**
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
     7
** Qt Designer is licensed under the terms of the GNU General Public
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
     8
** License versions 2.0 and 3.0 ("GPL License"). Trolltech offers users the
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
     9
** right to use certain no GPL licensed software under the terms of its GPL
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    10
** Exception version 1.2 (http://trolltech.com/products/qt/gplexception).
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    11
**
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    12
** THIS SOFTWARE IS PROVIDED BY TROLLTECH AND ITS CONTRIBUTORS (IF ANY) "AS
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    13
** IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    14
** TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    15
** PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    16
** OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    17
** EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    18
** PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    19
** PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    20
** LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    21
** NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    22
** SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    23
**
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    24
** Since we now have the GPL exception I think that the "special exception
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    25
** is no longer needed. The license text proposed above (other than the
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    26
** special exception portion of it) is the BSD license and we have added
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    27
** the BSD license as a permissible license under the exception.
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    28
**
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    29
****************************************************************************/
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    30
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    31
#include <qdebug.h>
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    32
#include <QImage>
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    33
#include <QByteArray>
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    34
#include <qnetworkproxy.h>
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    35
#include <qfile.h>
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    36
#include "ImageDownload.h"
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    37
#include "HomeView.h"
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    38
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    39
bool downloading = false;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    40
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    41
#define EMULATORTESTING
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    42
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    43
ImageDownload::ImageDownload(QObject *parent)
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    44
	{
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    45
	manager = new QNetworkAccessManager(this);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    46
	
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    47
#ifdef EMULATORTESTING
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    48
	qDebug()<<"Using PROXY SETTINGS!!!, change for device testing in image download";
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    49
	
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    50
	// Reading the keys, CSM Stubbed - START
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    51
	QFile winFile("c:\\data\\DoNotShare.txt");
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    52
	if (!winFile.open(QIODevice::ReadOnly))
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    53
		{
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    54
		qDebug()<<"File to read the windows username and password could not be opened, returning!!!";
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    55
		return;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    56
		}
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    57
	
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    58
	QByteArray winArr = winFile.readAll();
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    59
	QList<QByteArray> winList = winArr.split(' ');
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    60
	winFile.close();
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    61
	
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    62
	QString httpUser(winList[0]);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    63
	QString httpPass(winList[1]);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    64
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    65
    // For proxy settings on emulator only - REMOVE for device
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    66
    QString httpProxy = "10.1.0.214";
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    67
    QString httpPort = "3128";
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    68
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    69
    //==Classes used from Network Module==
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    70
    QNetworkProxy proxy;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    71
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    72
    proxy.setType(QNetworkProxy::HttpProxy);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    73
    proxy.setHostName(httpProxy);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    74
    proxy.setPort(httpPort.toInt());
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    75
    proxy.setUser(httpUser);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    76
    proxy.setPassword(httpPass);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    77
		
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    78
    QNetworkProxy::setApplicationProxy(proxy);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    79
#endif
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    80
	}
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    81
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    82
ImageDownload::~ImageDownload()
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    83
	{
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    84
	if(manager)
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    85
		delete manager;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    86
	}
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    87
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    88
void ImageDownload::downloadImage(HomeView *homeView, QString name, QUrl url, const SmfItemIdentifier &identifier)
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    89
	{
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    90
	m_homeView = homeView;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    91
	m_identifier = identifier;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    92
	qDebug()<<"Inside ImageDownload::downloadImage(name, url)";
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    93
	QNetworkRequest request;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    94
	request.setUrl(url);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    95
	qDebug()<<"URL of image to be downloaded = "<<url;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    96
	connect(manager, SIGNAL(finished(QNetworkReply*)), 
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    97
			this, SLOT(networkReplyFinished(QNetworkReply*)));
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    98
			
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    99
	QNetworkReply* reply = manager->get(request);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   100
	m_nameReplyHash.insert(name, reply);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   101
	}
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   102
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   103
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   104
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   105
void ImageDownload::networkReplyFinished ( QNetworkReply *aNetworkReply )
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   106
	{
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   107
	qDebug()<<"Error code if any = "<<aNetworkReply->error();
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   108
	qDebug()<<"Response for reply = "<<aNetworkReply;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   109
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   110
	QByteArray response = aNetworkReply->readAll();
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   111
	qDebug()<<"Response Size :"<<response.size();
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   112
	if(response.size())
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   113
		{
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   114
		QImage image = QImage::fromData(response);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   115
		
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   116
		QString fileName("c:\\data\\");
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   117
		fileName.append(m_nameReplyHash.key(aNetworkReply));
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   118
		fileName.append(".jpg");
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   119
		
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   120
		qDebug()<<"Image URL :"<<fileName;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   121
		
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   122
		bool saved = image.save(fileName, "JPG", -1);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   123
		qDebug()<<"Image saved ? "<<saved;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   124
		m_nameReplyHash.remove(m_nameReplyHash.key(aNetworkReply));
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   125
		if(m_nameReplyHash.count() == 0)
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   126
			{
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   127
			downloading = false;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   128
			if(SmfFriendsFetch == m_identifier)
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   129
				m_homeView->populateFriendsWidget();
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   130
			else if(SmfPostsFetch == m_identifier)
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   131
				m_homeView->populatePostsWidget();
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   132
			else if(SmfPhotosFetch == m_identifier)
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   133
				m_homeView->populatePhotosGridView();
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   134
			// ToDo:- for albums, photos and activity
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   135
			else
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   136
				qDebug()<<"Unknown request for download, so do nothing";
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   137
			}
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   138
		}
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   139
	else
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   140
		{
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   141
		qDebug()<<"Response data is NULL";
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   142
		}
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   143
	}