Пример #1
0
void TorcNetwork::GetSafe(TorcNetworkRequest* Request)
{
    if (Request && m_online)
    {
        Request->UpRef();

        // some servers require a recognised user agent...
        Request->m_request.setRawHeader("User-Agent", DEFAULT_USER_AGENT);

        QNetworkReply* reply = NULL;

        if (Request->m_type == QNetworkAccessManager::GetOperation)
            reply = get(Request->m_request);
        else if (Request->m_type == QNetworkAccessManager::HeadOperation)
            reply = head(Request->m_request);

        if (!reply)
        {
            Request->DownRef();
            LOG(VB_GENERAL, LOG_ERR, "Unknown request type");
            return;
        }

        // join the dots
        connect(reply, SIGNAL(readyRead()), this, SLOT(ReadyRead()));
        connect(reply, SIGNAL(finished()),  this, SLOT(Finished()));
        connect(reply, SIGNAL(downloadProgress(qint64, qint64)), this, SLOT(DownloadProgress(qint64,qint64)));
        connect(reply, SIGNAL(error(QNetworkReply::NetworkError)), this, SLOT(Error(QNetworkReply::NetworkError)));
        connect(reply, SIGNAL(sslErrors(const QList<QSslError> & )), this, SLOT(SSLErrors(const QList<QSslError> & )));

        m_requests.insert(reply, Request);
        m_reverseRequests.insert(Request, reply);
    }
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));
  }
}
Пример #4
0
void VkMusicCache::DownloadNext() {
  if (is_downloading || queue_.isEmpty()) {
    return;
  } else {
    current_download = queue_.first();
    queue_.pop_front();
    current_cashing_index--;

    // Check file path and file existance first
    if (QFile::exists(current_download.filename)) {
      qLog(Warning) << "Tried to overwrite already cached file" << current_download.filename;
      return;
    }

    // Create temporarry file we download to.
    if (file_) {
      qLog(Warning) << "QFile" << file_->fileName() << "is not null";
      delete file_;
      file_ = NULL;
    }

    file_ = new QTemporaryFile;
    if (!file_->open(QFile::WriteOnly)) {
      qLog(Warning) << "Can not create temporary file" << file_->fileName()
                    << "Download right away to" << current_download.filename;
    }

    // Start downloading
    is_aborted = false;
    is_downloading = true;
    task_id = app_->task_manager()->
              StartTask(tr("Caching %1")
                        .arg(QFileInfo(current_download.filename).baseName()));
    reply_ = network_manager_->get(QNetworkRequest(current_download.url));
    connect(reply_, SIGNAL(finished()), SLOT(Downloaded()));
    connect(reply_, SIGNAL(readyRead()), SLOT(DownloadReadyToRead()));
    connect(reply_, SIGNAL(downloadProgress(qint64, qint64)), SLOT(DownloadProgress(qint64, qint64)));
    qLog(Info)<< "Start cashing" << current_download.filename  << "from" << current_download.url;
  }
}
Пример #5
0
void Installer::OnDownloadProgress(qint64 c, qint64 t) {
    if (t > 0) {
        int nPercent = c * 100 / t;
        emit DownloadProgress(nPercent);
    }
}