| 1 | /* |
| 2 | * Copyright (c) 2009 Nokia Corporation |
| 3 | */ |
| 4 | |
| 5 | #include "qnamredirect.h" |
| 6 | |
| 7 | QNAMRedirect::QNAMRedirect(QWidget *parent) : QMainWindow(parent) { |
| 8 | _qnam = createQNAM(); |
| 9 | _originalUrl = "http://mitbbs.com/article2/PhotoForum/31264659_224.jpg"; |
| 10 | |
| 11 | QVBoxLayout* layout = new QVBoxLayout; |
| 12 | |
| 13 | #ifdef Q_OS_SYMBIAN |
| 14 | setStyleSheet("QLabel, QPushButton{ font: 5pt; }"); |
| 15 | #endif |
| 16 | |
| 17 | _textContainer = new QLabel("Click the button to test URL redirect!"); |
| 18 | _textContainer->setSizePolicy(QSizePolicy::Minimum, QSizePolicy::Minimum); |
| 19 | _textContainer->setAlignment(Qt::AlignCenter); |
| 20 | _textContainer->setWordWrap(true); |
| 21 | layout->addWidget(_textContainer); |
| 22 | |
| 23 | QPushButton* testButton = new QPushButton("Click here!"); |
| 24 | /* If the button is clicked, we'll do a request. */ |
| 25 | connect(testButton, SIGNAL(clicked()), |
| 26 | this, SLOT(doRequest())); |
| 27 | layout->addWidget(testButton); |
| 28 | |
| 29 | QFrame* frame = new QFrame; |
| 30 | frame->setLayout(layout); |
| 31 | setCentralWidget(frame); |
| 32 | } |
| 33 | |
| 34 | QNAMRedirect::~QNAMRedirect() { |
| 35 | if(_qnam) { |
| 36 | _qnam->deleteLater(); |
| 37 | } |
| 38 | if(_textContainer) { |
| 39 | _textContainer->deleteLater(); |
| 40 | } |
| 41 | } |
| 42 | |
| 43 | QNetworkAccessManager* QNAMRedirect::createQNAM() { |
| 44 | QNetworkAccessManager* qnam = new QNetworkAccessManager(this); |
| 45 | /* We'll handle the finished reply in replyFinished */ |
| 46 | connect(qnam, SIGNAL(finished(QNetworkReply*)), |
| 47 | this, SLOT(replyFinished(QNetworkReply*))); |
| 48 | return qnam; |
| 49 | } |
| 50 | |
| 51 | void QNAMRedirect::doRequest() { |
| 52 | QString text = "QNAMRedirect::doRequest doing request to "; |
| 53 | text.append(this->_originalUrl.toString()); |
| 54 | this->_textContainer->setText(text); |
| 55 | /* Let's just create network request for this predifned URL... */ |
| 56 | QNetworkRequest request(this->_originalUrl); |
| 57 | /* ...and ask the manager to do the request. */ |
| 58 | QNetWorkReply reply = this->_qnam->get(request); |
| 59 | connect(reply, SIGNAL(finished()), this, SLOT(httpFinished())); |
| 60 | connect(reply, SIGNAL(readyRead()),this, SLOT(httpReadyRead())); |
| 61 | connect(reply, SIGNAL(downloadProgress(qint64,qint64)), |
| 62 | this, SLOT(updateDataReadProgress(qint64,qint64))); |
| 63 | } |
| 64 | |
| 65 | void QNAMRedirect::replyFinished(QNetworkReply* reply) { |
| 66 | /* |
| 67 | * Reply is finished! |
| 68 | * We'll ask for the reply about the Redirection attribute |
| 69 | * http://doc.trolltech.com/qnetworkrequest.html#Attribute-enum |
| 70 | */ |
| 71 | QVariant possibleRedirectUrl = |
| 72 | reply->attribute(QNetworkRequest::RedirectionTargetAttribute); |
| 73 | |
| 74 | /* We'll deduct if the redirection is valid in the redirectUrl function */ |
| 75 | _urlRedirectedTo = this->redirectUrl(possibleRedirectUrl.toUrl(), |
| 76 | _urlRedirectedTo); |
| 77 | |
| 78 | /* If the URL is not empty, we're being redirected. */ |
| 79 | if(!_urlRedirectedTo.isEmpty()) { |
| 80 | QString text = QString("QNAMRedirect::replyFinished: Redirected to ") |
| 81 | .append(_urlRedirectedTo.toString()); |
| 82 | this->_textContainer->setText(text); |
| 83 | |
| 84 | /* We'll do another request to the redirection url. */ |
| 85 | this->_qnam->get(QNetworkRequest(_urlRedirectedTo)); |
| 86 | } |
| 87 | else { |
| 88 | /* |
| 89 | * We weren't redirected anymore |
| 90 | * so we arrived to the final destination... |
| 91 | */ |
| 92 | QString text = QString("QNAMRedirect::replyFinished: Arrived to ") |
| 93 | .append(reply->url().toString()); |
| 94 | this->_textContainer->setText(text); |
| 95 | /* ...so this can be cleared. */ |
| 96 | _urlRedirectedTo.clear(); |
| 97 | } |
| 98 | /* Clean up. */ |
| 99 | reply->deleteLater(); |
| 100 | } |
| 101 | |
| 102 | QUrl QNAMRedirect::redirectUrl(const QUrl& possibleRedirectUrl, |
| 103 | const QUrl& oldRedirectUrl) const { |
| 104 | QUrl redirectUrl; |
| 105 | /* |
| 106 | * Check if the URL is empty and |
| 107 | * that we aren't being fooled into a infinite redirect loop. |
| 108 | * We could also keep track of how many redirects we have been to |
| 109 | * and set a limit to it, but we'll leave that to you. |
| 110 | */ |
| 111 | if(!possibleRedirectUrl.isEmpty() && |
| 112 | possibleRedirectUrl != oldRedirectUrl) { |
| 113 | redirectUrl = possibleRedirectUrl; |
| 114 | } |
| 115 | return redirectUrl; |
| 116 | } |