コード例 #1
0
void
SpotifyDownloadDialog::tryDownloadResolver()
{
    debug() << "Trying to download: " << Collections::SpotifyCollection::resolverDownloadUrl();

    NetworkAccessManagerProxy* manager = The::networkAccessManager();
    QNetworkRequest request( Collections::SpotifyCollection::resolverDownloadUrl() );
    QNetworkReply* reply = manager->get( request );
    m_downloadReply = reply;

    connect( reply, SIGNAL( error( QNetworkReply::NetworkError ) ),
            this, SLOT( slotDownloadError( QNetworkReply::NetworkError ) ) );
    connect( reply, SIGNAL( downloadProgress( qint64, qint64 ) ),
            this, SLOT( slotDownloadProgress( qint64, qint64 ) ) );
    connect( reply, SIGNAL( finished() ),
            this, SLOT( slotDownloadFinished() ) );

    //set-up progress bar
    m_ui->progDownload->setMinimum( 0 );
    m_ui->progDownload->setMaximum( 1000 );
    m_ui->progDownload->setValue( 0 );
}
コード例 #2
0
ファイル: httpclient.cpp プロジェクト: Exadios/KFLog
bool HttpClient::downloadFile( QString &urlIn, QString &destinationIn )
{
  // qDebug() << "HttpClient::downloadFile: url=" << urlIn << ", dest=" << destinationIn;

  if( downloadRunning == true )
    {
      qWarning( "HttpClient(%d): download is running!", __LINE__ );
      return false;
    }

  _url = urlIn;
  _destination = destinationIn;

  QUrl url( urlIn );
  QFileInfo fileInfo( destinationIn );

  if( urlIn.isEmpty() || ! url.isValid() || fileInfo.fileName().isEmpty() )
    {
      qWarning( "HttpClient(%d): Url or destination file are invalid!", __LINE__ );
      return false;
    }

  tmpFile = new QFile( destinationIn + "." +
                       QDateTime::currentDateTime().toString("yyyyMMdd_hhmmss") );

  if( ! tmpFile->open( QIODevice::WriteOnly ) )
    {
      qWarning( "HttpClient(%d): Unable to open the file %s: %s",
                 __LINE__,
                 tmpFile->fileName ().toLatin1().data(),
                 tmpFile->errorString().toLatin1().data() );

      delete tmpFile;
      tmpFile = static_cast<QFile *> (0);
      return false;
    }

  // Check, if a proxy is defined in the configuration data. If true, we do use it.
  extern QSettings _settings;

  QString proxy = _settings.value( "/Internet/Proxy", "" ).toString();

  if( proxy.isEmpty() )
    {
      // Check the user's environment, if a proxy is defined there.
      char* proxyFromEnv = getenv("http_proxy");

      if( proxyFromEnv == 0 )
        {
          proxyFromEnv = getenv("HTTP_PROXY");
        }

      if( proxyFromEnv )
        {
          QString qProxy( proxyFromEnv );

          // remove an existing http prefix
          proxy = qProxy.remove("http://");
        }
    }

  if( ! proxy.isEmpty() )
    {
      QString hostName;
      quint16 port;

      if( parseProxy( proxy, hostName, port ) == true )
        {
          QNetworkProxy proxy;
          proxy.setType( QNetworkProxy::HttpProxy );
          proxy.setHostName( hostName );
          proxy.setPort( port );
          manager->setProxy( proxy );
        }
    }

  QNetworkRequest request;
  QString appl = QCoreApplication::applicationName() + "/" +
                 QCoreApplication::applicationVersion() +
                 " (Qt" + QT_VERSION_STR + "/X11)";

  request.setUrl( QUrl( _url, QUrl::TolerantMode ));
  request.setRawHeader( "User-Agent", appl.toLatin1() );

  reply = manager->get(request);

  if( ! reply )
    {
      qWarning( "HttpClient(%d): Reply object is invalid!", __LINE__ );
      return false;
    }

  reply->setReadBufferSize(0);

  connect( reply, SIGNAL(readyRead()), this, SLOT(slotReadyRead()) );

  connect( reply, SIGNAL(error(QNetworkReply::NetworkError)),
           this, SLOT(slotError(QNetworkReply::NetworkError)) );

  connect( reply, SIGNAL(finished()),
           this, SLOT(slotFinished()) );

  connect( reply, SIGNAL(downloadProgress(qint64, qint64)),
           this, SLOT(slotDownloadProgress( qint64, qint64 )) );

  downloadRunning = true;

  if ( _progressDialog != static_cast<QProgressDialog *> (0) )
    {
      _progressDialog->setWindowTitle( tr( "HTTP" ) );
      _progressDialog->setLabelText( tr( "Downloading %1" ).arg( fileInfo.fileName() ) );
      _progressDialog->show();
    }

  timer->start();
  return true;
}