void MagnatuneDownloadDialog::DownloadFinished() { QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); reply->deleteLater(); // Close any open file download_file_.reset(); QUrl redirect = reply->attribute(QNetworkRequest::RedirectionTargetAttribute).toUrl(); if (redirect.isValid()) { // Open the output file QString output_filename = GetOutputFilename(); download_file_.reset(new QFile(output_filename)); if (!download_file_->open(QIODevice::WriteOnly)) { ShowError(tr("Couldn't open output file %1").arg(output_filename)); return; } // Round and round we go redirect.setUserName(service_->username()); redirect.setPassword(service_->password()); current_reply_ = network_->get(QNetworkRequest(redirect)); connect(current_reply_, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(Error(QNetworkReply::NetworkError))); connect(current_reply_, SIGNAL(finished()), SLOT(DownloadFinished())); connect(current_reply_, SIGNAL(downloadProgress(qint64,qint64)), SLOT(DownloadProgress(qint64,qint64))); connect(current_reply_, SIGNAL(readyRead()), SLOT(DownloadReadyRead())); return; } next_row_ ++; DownloadNext(); }
void MagnatuneDownloadDialog::MetadataFinished() { QNetworkReply* reply = qobject_cast<QNetworkReply*>(sender()); reply->deleteLater(); // The reply isn't valid XML so we can't use QtXML to parse it :( QString data = QString::fromUtf8(reply->readAll()); // Check for errors if (data.contains("<ERROR>")) { ShowError(tr("There was a problem fetching the metadata from Magnatune")); return; } // Work out what format we want QString type; switch (ui_->format->currentIndex()) { case MagnatuneService::Format_Ogg: type = "URL_OGGZIP"; break; case MagnatuneService::Format_Flac: type = "URL_FLACZIP"; break; case MagnatuneService::Format_Wav: type = "URL_WAVZIP"; break; case MagnatuneService::Format_MP3_VBR: type = "URL_VBRZIP"; break; case MagnatuneService::Format_MP3_128: type = "URL_128KMP3ZIP"; break; } // Parse the XML (lol) to find the URL QRegExp re(QString("<%1>([^<]+)</%2>").arg(type, type)); if (re.indexIn(data) == -1) { ShowError(tr("This album is not available in the requested format")); return; } // Munge the URL a bit QString url_text = Utilities::DecodeHtmlEntities(re.cap(1)); QUrl url = QUrl(url_text); url.setUserName(service_->username()); url.setPassword(service_->password()); qLog(Debug) << "Downloading" << url; // Start the actual download current_reply_ = network_->get(QNetworkRequest(url)); connect(current_reply_, SIGNAL(error(QNetworkReply::NetworkError)), SLOT(Error(QNetworkReply::NetworkError))); connect(current_reply_, SIGNAL(finished()), SLOT(DownloadFinished())); connect(current_reply_, SIGNAL(downloadProgress(qint64,qint64)), SLOT(DownloadProgress(qint64,qint64))); connect(current_reply_, SIGNAL(readyRead()), SLOT(DownloadReadyRead())); // Close any open file download_file_.reset(); // Open the output file QString output_filename = GetOutputFilename(); download_file_.reset(new QFile(output_filename)); if (!download_file_->open(QIODevice::WriteOnly)) { ShowError(tr("Couldn't open output file %1").arg(output_filename)); } }