januszmk
26th July 2011, 12:47
Hello.
I have little problem.
I have functions:
size_t WriteData(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written;
written = fwrite(ptr, size, nmemb, stream);
return written;
}
int MainWindow::progress_func(void* ptr, double TotalToDownload, double NowDownloaded, double TotalToUpload, double NowUploaded)
{
ui->progressBar->setMaximum(TotalToDownload);
ui->progressBar->setValue(NowDownloaded);
double speed;
curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &speed);
ui->label_2->setText(QString("Prędkość: %0").arg(speed));
return NowDownloaded;
}
bool MainWindow::downloadFile(QString url, QString filename)
{
FILE *fp;
fp = fopen(filename.toStdString().c_str(), "wb");
ui->label->setText(filename);
ui->label_2->setText("Predkosc: 0 KB/s");
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "User-Agent: Mozilla/5.0 (compatible;)");
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie");
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie");
curl_easy_setopt(curl, CURLOPT_HEADER, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
curl_easy_setopt(curl, CURLOPT_URL, url.toStdString().c_str());
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, &MainWindow::progress_func);
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
status = curl_easy_perform(curl);
if(status !=0)
qDebug() << QString("curl_easy_perform() error: %0").arg(curl_easy_strerror(status));
curl_easy_cleanup(curl);
fclose(fp);
return true;
}
When I try to download a file, window of my aplication is frozen(?) until downloading is not finished.
Could you suggest what could I do to make this work?
Sory for my bad english
Janusz
I have little problem.
I have functions:
size_t WriteData(void *ptr, size_t size, size_t nmemb, FILE *stream) {
size_t written;
written = fwrite(ptr, size, nmemb, stream);
return written;
}
int MainWindow::progress_func(void* ptr, double TotalToDownload, double NowDownloaded, double TotalToUpload, double NowUploaded)
{
ui->progressBar->setMaximum(TotalToDownload);
ui->progressBar->setValue(NowDownloaded);
double speed;
curl_easy_getinfo(curl, CURLINFO_SPEED_DOWNLOAD, &speed);
ui->label_2->setText(QString("Prędkość: %0").arg(speed));
return NowDownloaded;
}
bool MainWindow::downloadFile(QString url, QString filename)
{
FILE *fp;
fp = fopen(filename.toStdString().c_str(), "wb");
ui->label->setText(filename);
ui->label_2->setText("Predkosc: 0 KB/s");
curl = curl_easy_init();
curl_easy_setopt(curl, CURLOPT_AUTOREFERER, 1);
curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1);
curl_easy_setopt(curl, CURLOPT_USERAGENT, "User-Agent: Mozilla/5.0 (compatible;)");
curl_easy_setopt(curl, CURLOPT_COOKIEFILE, "cookie");
curl_easy_setopt(curl, CURLOPT_COOKIEJAR, "cookie");
curl_easy_setopt(curl, CURLOPT_HEADER, 1);
curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, WriteData);
curl_easy_setopt(curl, CURLOPT_WRITEDATA, fp);
curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
curl_easy_setopt(curl, CURLOPT_HTTPGET, 1);
curl_easy_setopt(curl, CURLOPT_URL, url.toStdString().c_str());
curl_easy_setopt(curl, CURLOPT_NOPROGRESS, FALSE);
curl_easy_setopt(curl, CURLOPT_PROGRESSFUNCTION, &MainWindow::progress_func);
curl_easy_setopt(curl, CURLOPT_PROGRESSDATA, this);
status = curl_easy_perform(curl);
if(status !=0)
qDebug() << QString("curl_easy_perform() error: %0").arg(curl_easy_strerror(status));
curl_easy_cleanup(curl);
fclose(fp);
return true;
}
When I try to download a file, window of my aplication is frozen(?) until downloading is not finished.
Could you suggest what could I do to make this work?
Sory for my bad english
Janusz