example/DemoGUI/ImageDownload.cpp
author cgandhi
Tue, 12 Oct 2010 15:23:52 +0530
changeset 27 b3e1347ac96a
parent 26 83d6a149c755
permissions -rw-r--r--
Updating the information for test applications and results.
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
	{
26
83d6a149c755 Submitting following changes -
cgandhi
parents: 23
diff changeset
    45
	Q_UNUSED(parent)
16
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    46
	manager = new QNetworkAccessManager(this);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    47
	
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    48
#ifdef EMULATORTESTING
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    49
	qDebug()<<"Using PROXY SETTINGS!!!, change for device testing in image download";
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    50
	
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    51
	// Reading the keys, CSM Stubbed - START
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    52
	QFile winFile("c:\\data\\DoNotShare.txt");
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    53
	if (!winFile.open(QIODevice::ReadOnly))
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    54
		{
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    55
		qDebug()<<"File to read the windows username and password could not be opened, returning!!!";
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    56
		return;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    57
		}
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    58
	
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    59
	QByteArray winArr = winFile.readAll();
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    60
	QList<QByteArray> winList = winArr.split(' ');
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    61
	winFile.close();
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    62
	
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    63
	QString httpUser(winList[0]);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    64
	QString httpPass(winList[1]);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    65
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    66
    // For proxy settings on emulator only - REMOVE for device
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    67
    QString httpProxy = "10.1.0.214";
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    68
    QString httpPort = "3128";
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    69
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    70
    //==Classes used from Network Module==
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    71
    QNetworkProxy proxy;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    72
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    73
    proxy.setType(QNetworkProxy::HttpProxy);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    74
    proxy.setHostName(httpProxy);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    75
    proxy.setPort(httpPort.toInt());
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    76
    proxy.setUser(httpUser);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    77
    proxy.setPassword(httpPass);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    78
		
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    79
    QNetworkProxy::setApplicationProxy(proxy);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    80
#endif
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    81
	}
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    82
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    83
ImageDownload::~ImageDownload()
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    84
	{
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    85
	if(manager)
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    86
		delete manager;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    87
	}
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    88
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    89
void ImageDownload::downloadImage(HomeView *homeView, QString name, QUrl url, const SmfItemIdentifier &identifier)
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    90
	{
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    91
	m_homeView = homeView;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    92
	m_identifier = identifier;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    93
	qDebug()<<"Inside ImageDownload::downloadImage(name, url)";
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    94
	QNetworkRequest request;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    95
	request.setUrl(url);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    96
	qDebug()<<"URL of image to be downloaded = "<<url;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    97
	connect(manager, SIGNAL(finished(QNetworkReply*)), 
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    98
			this, SLOT(networkReplyFinished(QNetworkReply*)));
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
    99
			
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   100
	QNetworkReply* reply = manager->get(request);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   101
	m_nameReplyHash.insert(name, reply);
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
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   106
void ImageDownload::networkReplyFinished ( QNetworkReply *aNetworkReply )
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   107
	{
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   108
	qDebug()<<"Error code if any = "<<aNetworkReply->error();
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   109
	qDebug()<<"Response for reply = "<<aNetworkReply;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   110
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   111
	QByteArray response = aNetworkReply->readAll();
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   112
	qDebug()<<"Response Size :"<<response.size();
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   113
	if(response.size())
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   114
		{
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   115
		QImage image = QImage::fromData(response);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   116
		
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   117
		QString fileName("c:\\data\\");
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   118
		fileName.append(m_nameReplyHash.key(aNetworkReply));
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   119
		fileName.append(".jpg");
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   120
		
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   121
		qDebug()<<"Image URL :"<<fileName;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   122
		
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   123
		bool saved = image.save(fileName, "JPG", -1);
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   124
		qDebug()<<"Image saved ? "<<saved;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   125
		m_nameReplyHash.remove(m_nameReplyHash.key(aNetworkReply));
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   126
		if(m_nameReplyHash.count() == 0)
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   127
			{
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   128
			downloading = false;
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   129
			if(SmfFriendsFetch == m_identifier)
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   130
				m_homeView->populateFriendsWidget();
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   131
			else if(SmfPostsFetch == m_identifier)
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   132
				m_homeView->populatePostsWidget();
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   133
			else if(SmfPhotosFetch == m_identifier)
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   134
				m_homeView->populatePhotosGridView();
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   135
			// ToDo:- for albums, photos and activity
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   136
			else
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   137
				qDebug()<<"Unknown request for download, so do nothing";
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   138
			}
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   139
		}
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   140
	else
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   141
		{
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   142
		qDebug()<<"Response data is NULL";
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   143
		}
b78fa4cdbf2b pushing the demo application
cgandhi
parents:
diff changeset
   144
	}