364 } |
393 } |
365 QString pictureUrl = item->data(Qt::UserRole).toString(); |
394 QString pictureUrl = item->data(Qt::UserRole).toString(); |
366 pictureUrl.append("_m.jpg"); |
395 pictureUrl.append("_m.jpg"); |
367 |
396 |
368 QUrl url(pictureUrl); |
397 QUrl url(pictureUrl); |
369 QFileInfo fileInfo(url.path()); |
398 |
370 QString fileName = fileInfo.fileName(); |
399 QNetworkRequest req(url); |
371 if (fileName.isEmpty()) { |
400 m_pictureReply = m_nam->get(req); |
372 fileName = "test.jpg"; |
401 connect(m_pictureReply, |
373 } |
402 SIGNAL(downloadProgress(qint64, qint64)), |
374 |
403 this, |
375 m_filePath = savePath; |
404 SLOT(pictureDownloadProgress(qint64, qint64))); |
376 m_filePath.append(fileName); |
405 connect(m_pictureReply, |
377 |
406 SIGNAL(finished()), |
378 if (QFile::exists(m_filePath)) { |
407 this, |
379 if (QMessageBox::question(this, |
408 SLOT(pictureFinished())); |
380 tr("Flickr Demo"), |
409 connect(m_pictureReply, |
381 tr("File %1 is already downloaded." |
410 SIGNAL(error(QNetworkReply::NetworkError)), |
382 "Overwrite?").arg(fileName), |
411 this, |
383 QMessageBox::Yes | QMessageBox::No, |
412 SLOT(pictureError(QNetworkReply::NetworkError))); |
384 QMessageBox::No) |
|
385 == QMessageBox::No) { |
|
386 displayImage(); |
|
387 return; |
|
388 } |
|
389 QFile::remove(m_filePath); |
|
390 } |
|
391 |
|
392 m_file = new QFile(m_filePath); |
|
393 if (!m_file->open(QIODevice::WriteOnly)) { |
|
394 QMessageBox::information(this, tr("Flickr Demo"), |
|
395 tr("Unable to save the file %1: %2.").arg(m_filePath).arg(m_file->errorString())); |
|
396 delete m_file; |
|
397 m_file = 0; |
|
398 return; |
|
399 } |
|
400 |
|
401 m_http->setHost(url.host(), QHttp::ConnectionModeHttp, url.port() == -1 ? 0 : url.port()); |
|
402 |
|
403 m_httpRequestAborted = false; |
|
404 QByteArray encodedUrl = QUrl::toPercentEncoding(url.path(), "!$&'()*+,;=:@/"); |
|
405 if (encodedUrl.isEmpty()) { |
|
406 encodedUrl = "/"; |
|
407 } |
|
408 m_httpGetId = m_http->get(encodedUrl, m_file); |
|
409 |
413 |
410 m_progressDialog->setWindowTitle(tr("Flickr Demo")); |
414 m_progressDialog->setWindowTitle(tr("Flickr Demo")); |
411 m_progressDialog->setLabelText(tr("Downloading:\n%1").arg(fileName)); |
415 m_progressDialog->setLabelText(tr("Downloading:\n%1").arg(pictureUrl)); |
412 m_progressDialog->setMaximum(10); |
416 m_progressDialog->setMaximum(10); |
413 m_progressDialog->setValue(0); |
417 m_progressDialog->setValue(0); |
414 m_progressDialog->show(); |
418 m_progressDialog->show(); |
415 |
419 |
416 downloadButton->setEnabled(false); |
420 downloadButton->setEnabled(false); |
417 } |
421 } |
418 |
422 |
419 void FlickrDemo::cancelDownload() |
423 void FlickrDemo::cancelDownload() |
420 { |
424 { |
421 m_httpRequestAborted = true; |
425 if (m_pictureListReply) { |
422 m_downloadingThumbnails = false; |
426 m_pictureListReply->abort(); |
423 m_http->abort(); |
427 delete m_pictureListReply; |
|
428 m_pictureListReply = 0; |
|
429 } |
|
430 |
|
431 if (m_pictureReply) { |
|
432 m_pictureReply->abort(); |
|
433 delete m_pictureReply; |
|
434 m_pictureReply = 0; |
|
435 } |
|
436 |
424 downloadButton->setEnabled(true); |
437 downloadButton->setEnabled(true); |
425 } |
438 } |
426 |
439 |
427 void FlickrDemo::httpRequestFinished(int requestId, bool error) |
440 void FlickrDemo::pictureListDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) |
428 { |
441 { |
429 if (m_downloadingThumbnails && m_httpThumbnailGetId == requestId) { |
442 m_progressDialog->setMaximum(bytesTotal); |
430 if (!error) { |
443 m_progressDialog->setValue(bytesReceived); |
431 QByteArray picture = m_http->readAll(); |
444 } |
432 if (!picture.isNull() && picture.size() > 0) { |
445 |
433 QListWidgetItem* item = listWidget->item(m_nameCounter); |
446 void FlickrDemo::pictureListFinished() |
434 QImage image; |
447 { |
435 if (image.loadFromData(picture, "jpg")) { |
448 m_progressDialog->hide(); |
436 item->setIcon(QPixmap::fromImage(image.scaled(thumbnailSize, |
449 if (parsePictureList(QString::fromUtf8(m_pictureListReply->readAll()))) { |
437 Qt::KeepAspectRatio, Qt::SmoothTransformation))); |
450 m_downloadPictureList = false; |
438 listWidget->update(); |
451 downloadButton->setText(tr("Download Selected Picture")); |
439 } |
452 m_downloadAct->setText(tr("Download Selected Picture")); |
440 } |
453 } |
|
454 |
|
455 downloadButton->setEnabled(true); |
|
456 |
|
457 QTimer::singleShot(0, this, SLOT(clearPictureListRequest())); |
|
458 } |
|
459 |
|
460 void FlickrDemo::pictureListError(QNetworkReply::NetworkError code) |
|
461 { |
|
462 if (m_shuttingDown) |
|
463 return; |
|
464 |
|
465 m_progressDialog->hide(); |
|
466 QMessageBox::information(this, |
|
467 tr("Flickr Demo"), |
|
468 tr("Error downloading picture list: %1.").arg(m_pictureListReply->errorString())); |
|
469 |
|
470 QTimer::singleShot(0, this, SLOT(clearPictureListRequest())); |
|
471 } |
|
472 |
|
473 void FlickrDemo::clearPictureListRequest() |
|
474 { |
|
475 delete m_pictureListReply; |
|
476 m_pictureListReply = 0; |
|
477 } |
|
478 |
|
479 void FlickrDemo::thumbnailFinished() |
|
480 { |
|
481 QByteArray picture = m_thumbnailReply->readAll(); |
|
482 if (!picture.isNull() && picture.size() > 0) { |
|
483 QListWidgetItem* item = listWidget->item(m_nameCounter); |
|
484 QImage image; |
|
485 if (image.loadFromData(picture, "jpg")) { |
|
486 item->setIcon(QPixmap::fromImage( |
|
487 image.scaled(thumbnailSize, |
|
488 Qt::KeepAspectRatio, |
|
489 Qt::SmoothTransformation))); |
|
490 listWidget->update(); |
441 } |
491 } |
442 downloadNextThumbnail(); |
492 } |
|
493 downloadNextThumbnail(); |
|
494 } |
|
495 |
|
496 void FlickrDemo::thumbnailError(QNetworkReply::NetworkError code) |
|
497 { |
|
498 if (m_shuttingDown) |
443 return; |
499 return; |
444 } |
500 |
445 |
501 QMessageBox::information(this, |
446 if (requestId != m_httpGetId) { |
502 tr("Flickr Demo"), |
|
503 tr("Error downloading thumbnails: %1.").arg(m_thumbnailReply->errorString())); |
|
504 |
|
505 QTimer::singleShot(0, this, SLOT(clearThumbnailRequest())); |
|
506 } |
|
507 |
|
508 void FlickrDemo::clearThumbnailRequest() |
|
509 { |
|
510 delete m_thumbnailReply; |
|
511 m_thumbnailReply = 0; |
|
512 } |
|
513 |
|
514 void FlickrDemo::pictureDownloadProgress(qint64 bytesReceived, qint64 bytesTotal) |
|
515 { |
|
516 m_progressDialog->setMaximum(bytesTotal); |
|
517 m_progressDialog->setValue(bytesReceived); |
|
518 } |
|
519 |
|
520 void FlickrDemo::pictureFinished() |
|
521 { |
|
522 m_progressDialog->hide(); |
|
523 downloadButton->setEnabled(true); |
|
524 |
|
525 QByteArray picture = m_pictureReply->readAll(); |
|
526 |
|
527 if (picture.isNull() || picture.size() <= 0) |
447 return; |
528 return; |
448 } |
529 |
|
530 QImage image; |
|
531 if (!image.loadFromData(picture, "jpg")) |
|
532 return; |
|
533 |
|
534 QPixmap pixmap = QPixmap::fromImage( |
|
535 image.scaled(imageSize, |
|
536 Qt::KeepAspectRatio, |
|
537 Qt::SmoothTransformation)); |
|
538 |
|
539 displayImage(pixmap); |
|
540 |
|
541 QTimer::singleShot(0, this, SLOT(clearPictureRequest())); |
|
542 } |
|
543 |
|
544 void FlickrDemo::pictureError(QNetworkReply::NetworkError code) |
|
545 { |
|
546 if (m_shuttingDown) |
|
547 return; |
449 |
548 |
450 m_progressDialog->hide(); |
549 m_progressDialog->hide(); |
451 |
550 QMessageBox::information(this, |
452 if (m_httpRequestAborted) { |
551 tr("Flickr Demo"), |
453 if (m_file) { |
552 tr("Error downloading picture: %1.").arg(m_pictureReply->errorString())); |
454 m_file->close(); |
|
455 m_file->remove(); |
|
456 delete m_file; |
|
457 m_file = 0; |
|
458 } |
|
459 |
|
460 return; |
|
461 } |
|
462 |
|
463 if (!m_downloadPictureList && m_file) { |
|
464 m_file->close(); |
|
465 } |
|
466 |
|
467 if (error) { |
|
468 if (!m_downloadPictureList && m_file) { |
|
469 m_file->remove(); |
|
470 } |
|
471 QMessageBox::information(this, |
|
472 tr("Flickr Demo"), |
|
473 tr("Download failed: %1.").arg(m_http->errorString())); |
|
474 } |
|
475 |
|
476 if (m_downloadPictureList) { |
|
477 if (parsePictureList(QString::fromUtf8(m_http->readAll()))) { |
|
478 m_downloadPictureList = false; |
|
479 downloadButton->setText(tr("Download Selected Picture")); |
|
480 m_downloadAct->setText(tr("Download Selected Picture")); |
|
481 } |
|
482 } else { |
|
483 displayImage(); |
|
484 } |
|
485 |
553 |
486 downloadButton->setEnabled(true); |
554 downloadButton->setEnabled(true); |
487 } |
555 |
488 |
556 QTimer::singleShot(0, this, SLOT(clearPictureRequest())); |
489 void FlickrDemo::displayImage() |
557 } |
490 { |
558 |
491 PictureDialog dialog(m_filePath, listWidget->currentItem()->text(), this); |
559 void FlickrDemo::clearPictureRequest() |
|
560 { |
|
561 delete m_pictureReply; |
|
562 m_pictureReply = 0; |
|
563 } |
|
564 |
|
565 void FlickrDemo::displayImage(const QPixmap &pixmap) |
|
566 { |
|
567 PictureDialog dialog(pixmap, listWidget->currentItem()->text(), this); |
492 #if defined(Q_OS_SYMBIAN) || defined (Q_OS_WINCE) |
568 #if defined(Q_OS_SYMBIAN) || defined (Q_OS_WINCE) |
493 dialog.showMaximized(); |
569 dialog.showMaximized(); |
494 #endif |
570 #endif |
495 if (!dialog.exec()) { |
571 dialog.exec(); |
496 if (m_file && m_file->exists()) { |
|
497 m_file->remove(); |
|
498 } |
|
499 } |
|
500 if(m_file) |
|
501 delete m_file; |
|
502 m_file = 0; |
|
503 } |
|
504 |
|
505 void FlickrDemo::readResponseHeader(const QHttpResponseHeader& responseHeader) |
|
506 { |
|
507 switch (responseHeader.statusCode()) { |
|
508 case 200: // Ok |
|
509 case 301: // Moved Permanently |
|
510 case 302: // Found |
|
511 case 303: // See Other |
|
512 case 307: // Temporary Redirect |
|
513 // these are not error conditions |
|
514 break; |
|
515 default: |
|
516 QMessageBox::information(this, |
|
517 tr("Flickr Demo"), |
|
518 tr("Download failed: %1.").arg(responseHeader.reasonPhrase())); |
|
519 m_downloadingThumbnails = false; |
|
520 m_httpRequestAborted = true; |
|
521 m_progressDialog->hide(); |
|
522 m_http->abort(); |
|
523 } |
|
524 } |
|
525 |
|
526 void FlickrDemo::updateDataReadProgress(int bytesRead, int totalBytes) |
|
527 { |
|
528 if (m_httpRequestAborted) { |
|
529 return; |
|
530 } |
|
531 |
|
532 if (!m_downloadingThumbnails) { |
|
533 m_progressDialog->setMaximum(totalBytes); |
|
534 m_progressDialog->setValue(bytesRead); |
|
535 } |
|
536 } |
572 } |
537 |
573 |
538 void FlickrDemo::downloadNextThumbnail() |
574 void FlickrDemo::downloadNextThumbnail() |
539 { |
575 { |
540 m_nameCounter++; |
576 m_nameCounter++; |
541 if (m_nameCounter < m_names.count()) { |
577 if (m_nameCounter < m_names.count()) { |
542 QString pictureUrl = m_names[m_nameCounter]; |
578 QString pictureUrl = m_names[m_nameCounter]; |
543 pictureUrl.append("_s.jpg"); |
579 pictureUrl.append("_s.jpg"); |
544 QUrl url(pictureUrl); |
580 QUrl url(pictureUrl); |
545 m_http->setHost(url.host(), QHttp::ConnectionModeHttp, url.port() == -1 ? 0 : url.port()); |
581 |
546 m_downloadingThumbnails = true; |
582 QNetworkRequest req(url); |
547 m_httpThumbnailGetId = m_http->get(pictureUrl); |
583 m_thumbnailReply = m_nam->get(req); |
|
584 connect(m_thumbnailReply, |
|
585 SIGNAL(finished()), |
|
586 this, |
|
587 SLOT(thumbnailFinished())); |
|
588 connect(m_thumbnailReply, |
|
589 SIGNAL(error(QNetworkReply::NetworkError)), |
|
590 this, |
|
591 SLOT(thumbnailError(QNetworkReply::NetworkError))); |
548 } else { |
592 } else { |
549 m_downloadingThumbnails = false; |
593 QTimer::singleShot(0, this, SLOT(clearThumbnailRequest())); |
550 } |
594 } |
551 } |
595 } |
552 |
596 |
553 // static constant intialization |
597 PictureDialog::PictureDialog(const QPixmap& pixmap, const QString& pictureName, QWidget* parent) : |
554 |
|
555 const QSize PictureDialog::imageSize = QSize(150, 150); |
|
556 |
|
557 PictureDialog::PictureDialog(const QString& filePath, const QString& pictureName, QWidget* parent) : |
|
558 QDialog(parent) |
598 QDialog(parent) |
559 { |
599 { |
560 resize(252, 361); |
600 resize(252, 361); |
561 QVBoxLayout *verticalLayout = new QVBoxLayout(); |
601 QVBoxLayout *verticalLayout = new QVBoxLayout(); |
562 verticalLayout->setSpacing(6); |
602 verticalLayout->setSpacing(6); |
563 verticalLayout->setContentsMargins(11, 11, 11, 11); |
603 verticalLayout->setContentsMargins(11, 11, 11, 11); |
564 |
604 |
565 label = new QLabel(); |
605 label = new QLabel(); |
566 QString fileName = QFileInfo(filePath).fileName(); |
606 label->setText(tr("Downloaded:\n%1").arg(pictureName)); |
567 label->setText(tr("Downloaded:\n%1\n%2").arg(pictureName).arg(fileName)); |
|
568 |
607 |
569 QSizePolicy sizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred); |
608 QSizePolicy sizePolicy(QSizePolicy::Ignored, QSizePolicy::Preferred); |
570 sizePolicy.setHorizontalStretch(0); |
609 sizePolicy.setHorizontalStretch(0); |
571 sizePolicy.setVerticalStretch(0); |
610 sizePolicy.setVerticalStretch(0); |
572 sizePolicy.setHeightForWidth(label->sizePolicy().hasHeightForWidth()); |
611 sizePolicy.setHeightForWidth(label->sizePolicy().hasHeightForWidth()); |
573 label->setSizePolicy(sizePolicy); |
612 label->setSizePolicy(sizePolicy); |
574 |
613 |
575 verticalLayout->addWidget(label); |
614 verticalLayout->addWidget(label); |
576 |
615 |
577 imageLabel = new QLabel(); |
616 imageLabel = new QLabel(); |
578 QImage image; |
617 imageLabel->setPixmap(pixmap); |
579 image.load(filePath); |
|
580 imageLabel->setPixmap(QPixmap::fromImage(image.scaled(imageSize, Qt::KeepAspectRatio, |
|
581 Qt::SmoothTransformation))); |
|
582 |
618 |
583 verticalLayout->addWidget(imageLabel); |
619 verticalLayout->addWidget(imageLabel); |
584 |
620 |
585 keepButton = new QPushButton(tr("Keep")); |
|
586 keepButton->setDefault(true); |
|
587 discardButton = new QPushButton(tr("Discard")); |
|
588 |
|
589 buttonBox = new QDialogButtonBox(); |
621 buttonBox = new QDialogButtonBox(); |
590 buttonBox->addButton(keepButton, QDialogButtonBox::AcceptRole); |
622 buttonBox->setStandardButtons(QDialogButtonBox::Close); |
591 buttonBox->addButton(discardButton, QDialogButtonBox::DestructiveRole); |
623 connect(buttonBox, SIGNAL(rejected()), this, SLOT(accept())); |
592 connect(buttonBox, SIGNAL(clicked(QAbstractButton *)), this, SLOT(clicked(QAbstractButton *))); |
|
593 |
624 |
594 verticalLayout->addWidget(buttonBox); |
625 verticalLayout->addWidget(buttonBox); |
595 |
626 |
596 setLayout(verticalLayout); |
627 setLayout(verticalLayout); |
597 |
628 |
598 setWindowTitle(tr("Flickr Demo")); |
629 setWindowTitle(tr("Flickr Demo")); |
599 } |
630 } |
600 |
|
601 void PictureDialog::clicked(QAbstractButton* button) |
|
602 { |
|
603 if (button == keepButton) { |
|
604 accept(); |
|
605 } else if (button == discardButton) { |
|
606 reject(); |
|
607 } |
|
608 } |
|