examples/flickrdemo/flickrdemo.cpp
changeset 0 876b1a06bc25
equal deleted inserted replaced
-1:000000000000 0:876b1a06bc25
       
     1 /****************************************************************************
       
     2 **
       
     3 ** Copyright (C) 2010 Nokia Corporation and/or its subsidiary(-ies).
       
     4 ** All rights reserved.
       
     5 ** Contact: Nokia Corporation (qt-info@nokia.com)
       
     6 **
       
     7 ** This file is part of the Qt Mobility Components.
       
     8 **
       
     9 ** $QT_BEGIN_LICENSE:BSD$
       
    10 ** You may use this file under the terms of the BSD license as follows:
       
    11 **
       
    12 ** "Redistribution and use in source and binary forms, with or without
       
    13 ** modification, are permitted provided that the following conditions are
       
    14 ** met:
       
    15 **   * Redistributions of source code must retain the above copyright
       
    16 **     notice, this list of conditions and the following disclaimer.
       
    17 **   * Redistributions in binary form must reproduce the above copyright
       
    18 **     notice, this list of conditions and the following disclaimer in
       
    19 **     the documentation and/or other materials provided with the
       
    20 **     distribution.
       
    21 **   * Neither the name of Nokia Corporation and its Subsidiary(-ies) nor
       
    22 **     the names of its contributors may be used to endorse or promote
       
    23 **     products derived from this software without specific prior written
       
    24 **     permission.
       
    25 **
       
    26 ** THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
       
    27 ** "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
       
    28 ** LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
       
    29 ** A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
       
    30 ** OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
       
    31 ** SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
       
    32 ** LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
       
    33 ** DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
       
    34 ** THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
       
    35 ** (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
       
    36 ** OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE."
       
    37 ** $QT_END_LICENSE$
       
    38 **
       
    39 ****************************************************************************/
       
    40 
       
    41 #include "flickrdemo.h"
       
    42 #include <QDomDocument>
       
    43 
       
    44 #include <qnmeapositioninfosource.h>
       
    45 #include <qgeopositioninfosource.h>
       
    46 #include <qgeosatelliteinfosource.h>
       
    47 #include <qgeopositioninfo.h>
       
    48 #include <qgeosatelliteinfo.h>
       
    49 #include <qnetworkconfigmanager.h>
       
    50 #include <qnetworksession.h>
       
    51 
       
    52 #include <QAction>
       
    53 #include <QApplication>
       
    54 #include <QDialogButtonBox>
       
    55 #include <QDir>
       
    56 #include <QFile>
       
    57 #include <QLabel>
       
    58 #include <QMenuBar>
       
    59 #include <QMessageBox>
       
    60 #include <QNetworkAccessManager>
       
    61 #include <QNetworkRequest>
       
    62 #include <QProgressDialog>
       
    63 #include <QPushButton>
       
    64 #include <QTimer>
       
    65 #include <QVBoxLayout>
       
    66 #include <QWidget>
       
    67 
       
    68 // static constant intialization
       
    69 
       
    70 const QSize FlickrDemo::gridSize = QSize(52, 52);
       
    71 const QSize FlickrDemo::thumbnailSize = QSize(50, 50);
       
    72 const QSize FlickrDemo::imageSize = QSize(150, 150);
       
    73 const QString FlickrDemo::apikey = QString("e36784df8a03fea04c22ed93318b291c");
       
    74 
       
    75 FlickrDemo::FlickrDemo(QWidget* parent) :
       
    76         QMainWindow(parent),
       
    77         m_logfileInUse(false),
       
    78         m_session(0),
       
    79         m_pictureListReply(0),
       
    80         m_thumbnailReply(0),
       
    81         m_pictureReply(0),
       
    82         m_pages(0),
       
    83         m_page(1),
       
    84         m_satellitesInView(0),
       
    85         m_satellitesUsed(0),
       
    86         m_latitude(-1000),
       
    87         m_longitude(-1000),
       
    88         m_downloadPictureList(true),
       
    89         m_shuttingDown(false)
       
    90 {
       
    91     resize(252, 344);
       
    92 
       
    93     locationLabel = new QLabel(tr("Lat: Long:"));
       
    94     satellitesLabel = new QLabel(tr("Using 0 of 0 satellites"));
       
    95     listWidget = new XQListWidget();
       
    96     downloadButton = new QPushButton(tr("Download Picture List"));
       
    97 
       
    98     QVBoxLayout *verticalLayout = new QVBoxLayout();
       
    99     verticalLayout->addWidget(locationLabel);
       
   100     verticalLayout->addWidget(satellitesLabel);
       
   101     verticalLayout->addWidget(listWidget);
       
   102     verticalLayout->addWidget(downloadButton);
       
   103 
       
   104     QWidget *centralWidget = new QWidget;
       
   105     centralWidget->setLayout(verticalLayout);
       
   106     setCentralWidget(centralWidget);
       
   107 
       
   108     createMenus();
       
   109     listWidget->setGridSize(gridSize);
       
   110     listWidget->setIconSize(thumbnailSize);
       
   111     m_progressDialog = new QProgressDialog(this);
       
   112     m_progressDialog->setModal(true);
       
   113     connect(m_progressDialog, SIGNAL(canceled()), this, SLOT(cancelDownload()));
       
   114 
       
   115     setWindowTitle(tr("Flickr Demo"));
       
   116 
       
   117     connect(downloadButton, SIGNAL(clicked()), this, SLOT(downloadButtonClicked()));
       
   118 
       
   119     // QGeoPositionInfoSource
       
   120     m_location = QGeoPositionInfoSource::createDefaultSource(this);
       
   121 
       
   122     if (m_location == 0) {
       
   123         QNmeaPositionInfoSource *nmeaLocation = new QNmeaPositionInfoSource(QNmeaPositionInfoSource::SimulationMode, this);
       
   124         QFile *logFile = new QFile(QApplication::applicationDirPath()
       
   125                                    + QDir::separator() + "nmealog.txt", this);
       
   126         nmeaLocation->setDevice(logFile);
       
   127         m_location = nmeaLocation;
       
   128         m_logfileInUse = true;
       
   129     }
       
   130 
       
   131     // Listen gps position changes
       
   132     connect(m_location, SIGNAL(positionUpdated(QGeoPositionInfo)),
       
   133             this, SLOT(positionUpdated(QGeoPositionInfo)));
       
   134 
       
   135     // QGeoSatelliteInfoSource
       
   136     m_satellite = QGeoSatelliteInfoSource::createDefaultSource(this);
       
   137     // Listen satellite status changes
       
   138     if (m_satellite != 0) {
       
   139         connect(m_satellite, SIGNAL(satellitesInViewUpdated(const QList<QGeoSatelliteInfo>&)),
       
   140                 this, SLOT(satellitesInViewUpdated(const QList<QGeoSatelliteInfo>&)));
       
   141         connect(m_satellite, SIGNAL(satellitesInUseUpdated(const QList<QGeoSatelliteInfo>&)),
       
   142                 this, SLOT(satellitesInUseUpdated(const QList<QGeoSatelliteInfo>&)));
       
   143     }
       
   144 
       
   145     m_nam = new QNetworkAccessManager(this);
       
   146 
       
   147     QTimer::singleShot(0, this, SLOT(delayedInit()));
       
   148 }
       
   149 
       
   150 FlickrDemo::~FlickrDemo()
       
   151 {
       
   152     m_shuttingDown = true;
       
   153 
       
   154     m_location->stopUpdates();
       
   155     if (m_satellite)
       
   156         m_satellite->stopUpdates();
       
   157 
       
   158     if (m_pictureListReply) {
       
   159         m_pictureListReply->abort();
       
   160         delete m_pictureListReply;
       
   161     }
       
   162     if (m_thumbnailReply) {
       
   163         m_thumbnailReply->abort();
       
   164         delete m_thumbnailReply;
       
   165     }
       
   166     if (m_pictureReply) {
       
   167         m_pictureReply->abort();
       
   168         delete m_pictureReply;
       
   169     }
       
   170 
       
   171     if (m_session)
       
   172         m_session->close();
       
   173 }
       
   174 
       
   175 void FlickrDemo::delayedInit()
       
   176 {
       
   177     if (m_logfileInUse) {
       
   178         QMessageBox::information(this, tr("Flickr Demo"),
       
   179                                  tr("No GPS support detected, using GPS data from a sample log file instead."));
       
   180     }
       
   181 
       
   182     QTM_PREPEND_NAMESPACE(QNetworkConfigurationManager) manager;
       
   183     const bool canStartIAP = (manager.capabilities()
       
   184                               & QTM_PREPEND_NAMESPACE(QNetworkConfigurationManager)::CanStartAndStopInterfaces);
       
   185     QTM_PREPEND_NAMESPACE(QNetworkConfiguration) cfg = manager.defaultConfiguration();
       
   186     if (!cfg.isValid() || (!canStartIAP && cfg.state() != QTM_PREPEND_NAMESPACE(QNetworkConfiguration)::Active)) {
       
   187         QMessageBox::information(this, tr("Flickr Demo"), tr("Available Access Points not found."));
       
   188         return;
       
   189     }
       
   190 
       
   191     m_session = new QNetworkSession(cfg, this);
       
   192     m_connectivityHelper = new ConnectivityHelper(m_session, this);
       
   193     connect(m_session, SIGNAL(opened()), this, SLOT(networkSessionOpened()));
       
   194     connect(m_connectivityHelper, SIGNAL(networkingCancelled()), qApp, SLOT(quit()));
       
   195 
       
   196     m_session->open();
       
   197 }
       
   198 
       
   199 void FlickrDemo::networkSessionOpened()
       
   200 {
       
   201     // Start listening GPS position updates
       
   202     m_location->startUpdates();
       
   203 
       
   204     // Start listening GPS satellite status changes
       
   205     if (m_satellite != 0) {
       
   206         m_satellite->startUpdates();
       
   207     } else {
       
   208         satellitesLabel->setText(tr("GPS not detected, replaying coordinates from sample log file."));
       
   209     }
       
   210 }
       
   211 
       
   212 void FlickrDemo::createMenus()
       
   213 {
       
   214     m_downloadAct = new QAction(tr("Download Picture List"), this);
       
   215 #if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
       
   216     menuBar()->addAction(m_downloadAct);
       
   217 #endif
       
   218     connect(m_downloadAct, SIGNAL(triggered()), this, SLOT(downloadButtonClicked()));
       
   219 
       
   220     QAction* exitAct = new QAction(tr("Exit"), this);
       
   221 #if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
       
   222     menuBar()->addAction(exitAct);
       
   223 #endif
       
   224     connect(exitAct, SIGNAL(triggered()), this, SLOT(close()));
       
   225 }
       
   226 
       
   227 void FlickrDemo::positionUpdated(const QGeoPositionInfo &gpsPos)
       
   228 {
       
   229     if (gpsPos.isValid()) {
       
   230         QGeoCoordinate coord = gpsPos.coordinate();
       
   231         m_latitude = coord.latitude();
       
   232         m_longitude = coord.longitude();
       
   233 
       
   234         locationLabel->setText(tr("Lat: %1 Long: %2").arg(QString::number(m_latitude),
       
   235                                QString::number(m_longitude)));
       
   236     }
       
   237 }
       
   238 
       
   239 void FlickrDemo::satellitesInViewUpdated(const QList<QGeoSatelliteInfo> &satellites)
       
   240 {
       
   241     m_satellitesInView = satellites.count();
       
   242     viewSatelliteInfo();
       
   243 }
       
   244 void FlickrDemo::satellitesInUseUpdated(const QList<QGeoSatelliteInfo> &satellites)
       
   245 {
       
   246     m_satellitesUsed = satellites.count();
       
   247     viewSatelliteInfo();
       
   248 }
       
   249 
       
   250 void FlickrDemo::viewSatelliteInfo()
       
   251 {
       
   252     if (m_satellite != 0) {
       
   253         satellitesLabel->setText(tr("Using %1 of %2 satellites").arg(QString::number(m_satellitesUsed), QString::number(m_satellitesInView)));
       
   254     } else {
       
   255         satellitesLabel->setText(tr("GPS not detected, replaying coordinates from sample log file."));
       
   256     }
       
   257 }
       
   258 
       
   259 void FlickrDemo::downloadButtonClicked()
       
   260 {
       
   261     if (m_downloadPictureList) {
       
   262         downloadFlickerPictureList();
       
   263     } else {
       
   264         downloadPictureFromFlickr();
       
   265     }
       
   266 }
       
   267 
       
   268 void FlickrDemo::downloadFlickerPictureList()
       
   269 {
       
   270     if (m_latitude == -1000 || m_longitude == -1000) {
       
   271         if (QMessageBox::question(this,
       
   272                                   tr("Flickr Demo"),
       
   273                                   tr("No satellite connection.\n""Use hard coded coordinates?"),
       
   274                                   QMessageBox::Ok | QMessageBox::Cancel,
       
   275                                   QMessageBox::Cancel)
       
   276                 == QMessageBox::Cancel) {
       
   277             return;
       
   278         }
       
   279         // If GPS signal is not detected, Tampere area is used as default location
       
   280         m_latitude = 61.4500;
       
   281         m_longitude = 23.8502;
       
   282     }
       
   283 
       
   284     // Set IAP name
       
   285     satellitesLabel->setText(tr("Access Point: %1").arg(m_session->configuration().name()));
       
   286     locationLabel->setText(tr("Lat: %1 Long: %2").arg(QString::number(m_latitude),
       
   287                            QString::number(m_longitude)));
       
   288 
       
   289     QString
       
   290     urlstring =
       
   291         "http://api.flickr.com/services/rest/?min_taken_date=2000-01-01+0:00:00&extras=date_taken&method=flickr.photos.search&per_page=30&sort=date-taken-desc";
       
   292     urlstring.append("&api_key=");
       
   293     urlstring.append(apikey);
       
   294     urlstring.append("&lat=");
       
   295     urlstring.append(QString::number(m_latitude));
       
   296     urlstring.append("&lon=");
       
   297     urlstring.append(QString::number(m_longitude));
       
   298     urlstring.append("&page=");
       
   299     urlstring.append(QString::number(m_page));
       
   300 
       
   301     QUrl url(urlstring);
       
   302 
       
   303     QNetworkRequest req(url);
       
   304     m_pictureListReply = m_nam->get(req);
       
   305     connect(m_pictureListReply,
       
   306             SIGNAL(downloadProgress(qint64, qint64)),
       
   307             this,
       
   308             SLOT(pictureListDownloadProgress(qint64, qint64)));
       
   309     connect(m_pictureListReply,
       
   310             SIGNAL(finished()),
       
   311             this,
       
   312             SLOT(pictureListFinished()));
       
   313     connect(m_pictureListReply,
       
   314             SIGNAL(error(QNetworkReply::NetworkError)),
       
   315             this,
       
   316             SLOT(pictureListError(QNetworkReply::NetworkError)));
       
   317 
       
   318     m_progressDialog->setWindowTitle(tr("FlickrDemo"));
       
   319     m_progressDialog->setLabelText(tr("Downloading\nPicture List."));
       
   320     m_progressDialog->setMaximum(10);
       
   321     m_progressDialog->setValue(0);
       
   322     m_progressDialog->show();
       
   323 }
       
   324 
       
   325 bool FlickrDemo::parsePictureList(QString xmlString)
       
   326 {
       
   327     QString errorStr;
       
   328     int errorLine;
       
   329     int errorColumn;
       
   330 
       
   331     QDomDocument domDocument;
       
   332     if (!domDocument.setContent(xmlString, true, &errorStr, &errorLine, &errorColumn)) {
       
   333         QMessageBox::information(window(),
       
   334                                  tr("Flickr Demo"),
       
   335                                  tr("XML Parse error at line %1, column %2:\n%3").arg(errorLine).arg(errorColumn).arg(errorStr));
       
   336         return false;
       
   337     }
       
   338     QDomElement root = domDocument.documentElement();
       
   339 
       
   340     if (root.tagName() != "rsp") {
       
   341         QMessageBox::information(window(), tr("Flickr Demo"), tr("Invalid XML format."));
       
   342         return false;
       
   343     }
       
   344     if (root.attribute("stat") != "ok") {
       
   345         QMessageBox::information(window(), tr("Flickr Demo"), tr("Query failed."));
       
   346         return false;
       
   347     }
       
   348 
       
   349     listWidget->clear();
       
   350     QListWidgetItem* listItem;
       
   351     QDomElement photosElement = root.firstChildElement("photos");
       
   352     if (!photosElement.isNull()) {
       
   353         m_pages = (photosElement.attribute("pages")).toInt();
       
   354         m_page = (photosElement.attribute("page")).toInt();
       
   355 
       
   356         QDomElement child = photosElement.firstChildElement("photo");
       
   357         while (!child.isNull()) {
       
   358             QString url = "http://farm";
       
   359             url.append(child.attribute("farm"));
       
   360             url.append(".static.flickr.com/");
       
   361             url.append(child.attribute("server"));
       
   362             url.append("/");
       
   363             url.append(child.attribute("id"));
       
   364             url.append("_");
       
   365             url.append(child.attribute("secret"));
       
   366 
       
   367             m_names << url;
       
   368 
       
   369             listItem = new QListWidgetItem(child.attribute("title"));
       
   370             QImage image(thumbnailSize, QImage::Format_Mono);
       
   371             image.fill(1);
       
   372             listItem->setIcon(QPixmap::fromImage(image));
       
   373             listItem->setData(Qt::UserRole, url);
       
   374             listWidget->addItem(listItem);
       
   375 
       
   376             child = child.nextSiblingElement("photo");
       
   377         }
       
   378     } else {
       
   379         return false;
       
   380     }
       
   381     m_nameCounter = -1;
       
   382     downloadNextThumbnail();
       
   383 
       
   384     return true;
       
   385 }
       
   386 
       
   387 void FlickrDemo::downloadPictureFromFlickr()
       
   388 {
       
   389     QListWidgetItem* item = listWidget->currentItem();
       
   390     if (!item) {
       
   391         return;
       
   392     }
       
   393     QString pictureUrl = item->data(Qt::UserRole).toString();
       
   394     pictureUrl.append("_m.jpg");
       
   395 
       
   396     QUrl url(pictureUrl);
       
   397 
       
   398     QNetworkRequest req(url);
       
   399     m_pictureReply = m_nam->get(req);
       
   400     connect(m_pictureReply,
       
   401             SIGNAL(downloadProgress(qint64, qint64)),
       
   402             this,
       
   403             SLOT(pictureDownloadProgress(qint64, qint64)));
       
   404     connect(m_pictureReply,
       
   405             SIGNAL(finished()),
       
   406             this,
       
   407             SLOT(pictureFinished()));
       
   408     connect(m_pictureReply,
       
   409             SIGNAL(error(QNetworkReply::NetworkError)),
       
   410             this,
       
   411             SLOT(pictureError(QNetworkReply::NetworkError)));
       
   412 
       
   413     m_progressDialog->setWindowTitle(tr("Flickr Demo"));
       
   414     m_progressDialog->setLabelText(tr("Downloading:\n%1").arg(pictureUrl));
       
   415     m_progressDialog->setMaximum(10);
       
   416     m_progressDialog->setValue(0);
       
   417     m_progressDialog->show();
       
   418 
       
   419     downloadButton->setEnabled(false);
       
   420 }
       
   421 
       
   422 void FlickrDemo::cancelDownload()
       
   423 {
       
   424     if (m_pictureListReply) {
       
   425         m_pictureListReply->abort();
       
   426         delete m_pictureListReply;
       
   427         m_pictureListReply = 0;
       
   428     }
       
   429 
       
   430     if (m_pictureReply) {
       
   431         m_pictureReply->abort();
       
   432         delete m_pictureReply;
       
   433         m_pictureReply = 0;
       
   434     }
       
   435 
       
   436     downloadButton->setEnabled(true);
       
   437 }
       
   438 
       
   439 void FlickrDemo::pictureListDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
       
   440 {
       
   441     m_progressDialog->setMaximum(bytesTotal);
       
   442     m_progressDialog->setValue(bytesReceived);
       
   443 }
       
   444 
       
   445 void FlickrDemo::pictureListFinished()
       
   446 {
       
   447     m_progressDialog->hide();
       
   448     if (parsePictureList(QString::fromUtf8(m_pictureListReply->readAll()))) {
       
   449         m_downloadPictureList = false;
       
   450         downloadButton->setText(tr("Download Selected Picture"));
       
   451         m_downloadAct->setText(tr("Download Selected Picture"));
       
   452     }
       
   453 
       
   454     downloadButton->setEnabled(true);
       
   455 
       
   456     QTimer::singleShot(0, this, SLOT(clearPictureListRequest()));
       
   457 }
       
   458 
       
   459 void FlickrDemo::pictureListError(QNetworkReply::NetworkError /*code*/)
       
   460 {
       
   461     if (m_shuttingDown)
       
   462         return;
       
   463 
       
   464     m_progressDialog->hide();
       
   465     QMessageBox::information(this,
       
   466                              tr("Flickr Demo"),
       
   467                              tr("Error downloading picture list: %1.").arg(m_pictureListReply->errorString()));
       
   468 
       
   469     QTimer::singleShot(0, this, SLOT(clearPictureListRequest()));
       
   470 }
       
   471 
       
   472 void FlickrDemo::clearPictureListRequest()
       
   473 {
       
   474     delete m_pictureListReply;
       
   475     m_pictureListReply = 0;
       
   476 }
       
   477 
       
   478 void FlickrDemo::thumbnailFinished()
       
   479 {
       
   480     QByteArray picture = m_thumbnailReply->readAll();
       
   481     if (!picture.isNull() && picture.size() > 0) {
       
   482         QListWidgetItem* item = listWidget->item(m_nameCounter);
       
   483         QImage image;
       
   484         if (image.loadFromData(picture, "jpg")) {
       
   485             item->setIcon(QPixmap::fromImage(
       
   486                               image.scaled(thumbnailSize,
       
   487                                            Qt::KeepAspectRatio,
       
   488                                            Qt::SmoothTransformation)));
       
   489             listWidget->update();
       
   490         }
       
   491     }
       
   492     downloadNextThumbnail();
       
   493 }
       
   494 
       
   495 void FlickrDemo::thumbnailError(QNetworkReply::NetworkError /*code*/)
       
   496 {
       
   497     if (m_shuttingDown)
       
   498         return;
       
   499 
       
   500     QMessageBox::information(this,
       
   501                              tr("Flickr Demo"),
       
   502                              tr("Error downloading thumbnails: %1.").arg(m_thumbnailReply->errorString()));
       
   503 
       
   504     QTimer::singleShot(0, this, SLOT(clearThumbnailRequest()));
       
   505 }
       
   506 
       
   507 void FlickrDemo::clearThumbnailRequest()
       
   508 {
       
   509     delete m_thumbnailReply;
       
   510     m_thumbnailReply = 0;
       
   511 }
       
   512 
       
   513 void FlickrDemo::pictureDownloadProgress(qint64 bytesReceived, qint64 bytesTotal)
       
   514 {
       
   515     m_progressDialog->setMaximum(bytesTotal);
       
   516     m_progressDialog->setValue(bytesReceived);
       
   517 }
       
   518 
       
   519 void FlickrDemo::pictureFinished()
       
   520 {
       
   521     m_progressDialog->hide();
       
   522     downloadButton->setEnabled(true);
       
   523 
       
   524     QByteArray picture = m_pictureReply->readAll();
       
   525 
       
   526     if (picture.isNull() || picture.size() <= 0)
       
   527         return;
       
   528 
       
   529     QImage image;
       
   530     if (!image.loadFromData(picture, "jpg"))
       
   531         return;
       
   532 
       
   533     QPixmap pixmap = QPixmap::fromImage(
       
   534                          image.scaled(imageSize,
       
   535                                       Qt::KeepAspectRatio,
       
   536                                       Qt::SmoothTransformation));
       
   537 
       
   538     displayImage(pixmap);
       
   539 
       
   540     QTimer::singleShot(0, this, SLOT(clearPictureRequest()));
       
   541 }
       
   542 
       
   543 void FlickrDemo::pictureError(QNetworkReply::NetworkError /*code*/)
       
   544 {
       
   545     if (m_shuttingDown)
       
   546         return;
       
   547 
       
   548     m_progressDialog->hide();
       
   549     QMessageBox::information(this,
       
   550                              tr("Flickr Demo"),
       
   551                              tr("Error downloading picture: %1.").arg(m_pictureReply->errorString()));
       
   552 
       
   553     downloadButton->setEnabled(true);
       
   554 
       
   555     QTimer::singleShot(0, this, SLOT(clearPictureRequest()));
       
   556 }
       
   557 
       
   558 void FlickrDemo::clearPictureRequest()
       
   559 {
       
   560     delete m_pictureReply;
       
   561     m_pictureReply = 0;
       
   562 }
       
   563 
       
   564 void FlickrDemo::displayImage(const QPixmap &pixmap)
       
   565 {
       
   566     PictureDialog dialog(pixmap, listWidget->currentItem()->text(), this);
       
   567 #if defined(Q_OS_SYMBIAN) || defined(Q_OS_WINCE_WM) || defined(Q_WS_MAEMO_5) || defined(Q_WS_MAEMO_6)
       
   568     dialog.showMaximized();
       
   569 #endif
       
   570     dialog.exec();
       
   571 }
       
   572 
       
   573 void FlickrDemo::downloadNextThumbnail()
       
   574 {
       
   575     m_nameCounter++;
       
   576     if (m_nameCounter < m_names.count()) {
       
   577         QString pictureUrl = m_names[m_nameCounter];
       
   578         pictureUrl.append("_s.jpg");
       
   579         QUrl url(pictureUrl);
       
   580 
       
   581         QNetworkRequest req(url);
       
   582         m_thumbnailReply = m_nam->get(req);
       
   583         connect(m_thumbnailReply,
       
   584                 SIGNAL(finished()),
       
   585                 this,
       
   586                 SLOT(thumbnailFinished()));
       
   587         connect(m_thumbnailReply,
       
   588                 SIGNAL(error(QNetworkReply::NetworkError)),
       
   589                 this,
       
   590                 SLOT(thumbnailError(QNetworkReply::NetworkError)));
       
   591     } else {
       
   592         QTimer::singleShot(0, this, SLOT(clearThumbnailRequest()));
       
   593     }
       
   594 }
       
   595 
       
   596 PictureDialog::PictureDialog(const QPixmap& pixmap, const QString& pictureName, QWidget* parent) :
       
   597         QDialog(parent)
       
   598 {
       
   599     resize(252, 361);
       
   600     QVBoxLayout *verticalLayout = new QVBoxLayout();
       
   601     verticalLayout->setSpacing(6);
       
   602     verticalLayout->setContentsMargins(11, 11, 11, 11);
       
   603 
       
   604     label = new QLabel();
       
   605     label->setText(tr("Downloaded:\n%1").arg(pictureName));
       
   606 
       
   607     QSizePolicy sizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred);
       
   608     sizePolicy.setHorizontalStretch(0);
       
   609     sizePolicy.setVerticalStretch(0);
       
   610     sizePolicy.setHeightForWidth(label->sizePolicy().hasHeightForWidth());
       
   611     label->setSizePolicy(sizePolicy);
       
   612 
       
   613     verticalLayout->addWidget(label);
       
   614 
       
   615     imageLabel = new QLabel();
       
   616     imageLabel->setPixmap(pixmap);
       
   617 
       
   618     verticalLayout->addWidget(imageLabel);
       
   619 
       
   620     buttonBox = new QDialogButtonBox();
       
   621     buttonBox->setStandardButtons(QDialogButtonBox::Close);
       
   622     connect(buttonBox, SIGNAL(rejected()), this, SLOT(accept()));
       
   623 
       
   624     verticalLayout->addWidget(buttonBox);
       
   625 
       
   626     setLayout(verticalLayout);
       
   627 
       
   628     setWindowTitle(tr("Flickr Demo"));
       
   629 }