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