comparison demos/browser/downloadmanager.d @ 74:37caa90ce503

more porting
author mandel
date Fri, 22 May 2009 23:43:58 +0000
parents 7bfd46c330dc
children 0654fc9bac95
comparison
equal deleted inserted replaced
73:7bfd46c330dc 74:37caa90ce503
180 downloadInfoLabel.setText(tr("Network Error: %1").arg(m_reply.errorString())); 180 downloadInfoLabel.setText(tr("Network Error: %1").arg(m_reply.errorString()));
181 tryAgainButton.setEnabled(true); 181 tryAgainButton.setEnabled(true);
182 tryAgainButton.setVisible(true); 182 tryAgainButton.setVisible(true);
183 } 183 }
184 184
185 void downloadProgress(qint64 bytesReceived, qint64 bytesTotal) 185 void downloadProgress(ulong bytesReceived, ulong bytesTotal)
186 { 186 {
187 m_bytesReceived = bytesReceived; 187 m_bytesReceived = bytesReceived;
188 if (bytesTotal == -1) { 188 if (bytesTotal == -1) {
189 progressBar.setValue(0); 189 progressBar.setValue(0);
190 progressBar.setMaximum(0); 190 progressBar.setMaximum(0);
268 void updateInfoLabel() 268 void updateInfoLabel()
269 { 269 {
270 if (m_reply.error() == QNetworkReply.NoError) 270 if (m_reply.error() == QNetworkReply.NoError)
271 return; 271 return;
272 272
273 qint64 bytesTotal = progressBar.maximum(); 273 ulong bytesTotal = progressBar.maximum();
274 bool running = !downloadedSuccessfully(); 274 bool running = !downloadedSuccessfully();
275 275
276 // update info label 276 // update info label
277 double speed = m_bytesReceived * 1000.0 / m_downloadTime.elapsed(); 277 double speed = m_bytesReceived * 1000.0 / m_downloadTime.elapsed();
278 double timeRemaining = ((double)(bytesTotal - m_bytesReceived)) / speed; 278 double timeRemaining = (cast(double)(bytesTotal - m_bytesReceived)) / speed;
279 QString timeRemainingString = tr("seconds"); 279 QString timeRemainingString = tr("seconds");
280 if (timeRemaining > 60) { 280 if (timeRemaining > 60) {
281 timeRemaining = timeRemaining / 60; 281 timeRemaining = timeRemaining / 60;
282 timeRemainingString = tr("minutes"); 282 timeRemainingString = tr("minutes");
283 } 283 }
296 .arg(timeRemainingString); 296 .arg(timeRemainingString);
297 297
298 info = QString(tr("%1 of %2 (%3/sec) %4")) 298 info = QString(tr("%1 of %2 (%3/sec) %4"))
299 .arg(dataString(m_bytesReceived)) 299 .arg(dataString(m_bytesReceived))
300 .arg(bytesTotal == 0 ? tr("?") : dataString(bytesTotal)) 300 .arg(bytesTotal == 0 ? tr("?") : dataString(bytesTotal))
301 .arg(dataString((int)speed)) 301 .arg(dataString(cast(int) speed))
302 .arg(remaining); 302 .arg(remaining);
303 } else { 303 } else {
304 if (m_bytesReceived == bytesTotal) 304 if (m_bytesReceived == bytesTotal)
305 info = dataString(m_output.size()); 305 info = dataString(m_output.size());
306 else 306 else
324 unit = tr("MB"); 324 unit = tr("MB");
325 } 325 }
326 return QString(QLatin1String("%1 %2")).arg(size).arg(unit); 326 return QString(QLatin1String("%1 %2")).arg(size).arg(unit);
327 } 327 }
328 328
329 QString saveFileName(QString &directory); 329 QString saveFileName(QString directory)
330 { 330 {
331 // Move this function into QNetworkReply to also get file name sent from the server 331 // Move this function into QNetworkReply to also get file name sent from the server
332 QString path = m_url.path(); 332 QString path = m_url.path();
333 QFileInfo info(path); 333 QFileInfo info(path);
334 QString baseName = info.completeBaseName(); 334 QString baseName = info.completeBaseName();
348 } 348 }
349 return name; 349 return name;
350 } 350 }
351 351
352 bool m_requestFileName; 352 bool m_requestFileName;
353 qint64 m_bytesReceived; 353 ulong m_bytesReceived;
354 QTime m_downloadTime; 354 QTime m_downloadTime;
355 } 355 }
356 356
357 357
358 class DownloadManager : public QDialog, public Ui_DownloadDialog 358 class DownloadManager : public QDialog, public Ui_DownloadDialog
370 DownloadManager is a Dialog that contains a list of DownloadItems 370 DownloadManager is a Dialog that contains a list of DownloadItems
371 371
372 It is a basic download manager. It only downloads the file, doesn't do BitTorrent, 372 It is a basic download manager. It only downloads the file, doesn't do BitTorrent,
373 extract zipped files or anything fancy. 373 extract zipped files or anything fancy.
374 */ 374 */
375 this(QWidget parent = null) : QDialog(parent) 375 this(QWidget parent = null)
376 //: QDialog(parent)
376 { 377 {
377 m_autoSaver = new AutoSaver(this); 378 m_autoSaver = new AutoSaver(this);
378 m_manager = BrowserApplication.networkAccessManager(); 379 m_manager = BrowserApplication.networkAccessManager();
379 m_iconProvider = 0; 380 m_iconProvider = 0;
380 m_removePolicy = RemovePolicy.Never; 381 m_removePolicy = RemovePolicy.Never;
385 downloadsView.horizontalHeader().hide(); 386 downloadsView.horizontalHeader().hide();
386 downloadsView.setAlternatingRowColors(true); 387 downloadsView.setAlternatingRowColors(true);
387 downloadsView.horizontalHeader().setStretchLastSection(true); 388 downloadsView.horizontalHeader().setStretchLastSection(true);
388 m_model = new DownloadModel(this); 389 m_model = new DownloadModel(this);
389 downloadsView.setModel(m_model); 390 downloadsView.setModel(m_model);
390 connect(cleanupButton, SIGNAL(clicked()), this, SLOT(cleanup())); 391 cleanupButton.clicked.connect(&this.cleanup);
391 load(); 392 load();
392 } 393 }
393 394
394 ~this() 395 ~this()
395 { 396 {
422 m_autoSaver.changeOccurred(); 423 m_autoSaver.changeOccurred();
423 } 424 }
424 425
425 public: 426 public:
426 427
427 void download(QNetworkRequest request, bool requestFileName = false); 428 void download(QNetworkRequest request, bool requestFileName = false)
428 { 429 {
429 if (request.url().isEmpty()) 430 if (request.url().isEmpty())
430 return; 431 return;
431 handleUnsupportedContent(m_manager.get(request), requestFileName); 432 handleUnsupportedContent(m_manager.get(request), requestFileName);
432 } 433 }
433
434 434
435 void download(QUrl url, bool requestFileName = false) 435 void download(QUrl url, bool requestFileName = false)
436 { 436 {
437 download(QNetworkRequest(url), requestFileName); 437 download(QNetworkRequest(url), requestFileName);
438 } 438 }
439 439
440 void handleUnsupportedContent(QNetworkReply reply, bool requestFileName = false); 440 void handleUnsupportedContent(QNetworkReply reply, bool requestFileName = false)
441 { 441 {
442 if (!reply || reply.url().isEmpty()) 442 if (!reply || reply.url().isEmpty())
443 return; 443 return;
444 QVariant header = reply.header(QNetworkRequest.ContentLengthHeader); 444 QVariant header = reply.header(QNetworkRequest.ContentLengthHeader);
445 bool ok; 445 bool ok;
493 } 493 }
494 } 494 }
495 495
496 void updateRow() 496 void updateRow()
497 { 497 {
498 DownloadItem item = qobject_cast<DownloadItem*>(sender()); 498 DownloadItem item = cast(DownloadItem) sender();
499 int row = m_downloads.indexOf(item); 499 int row = m_downloads.indexOf(item);
500 if (-1 == row) 500 if (-1 == row)
501 return; 501 return;
502 if (!m_iconProvider) 502 if (!m_iconProvider)
503 m_iconProvider = new QFileIconProvider(); 503 m_iconProvider = new QFileIconProvider();
554 if (size.isValid()) 554 if (size.isValid())
555 resize(size); 555 resize(size);
556 QByteArray value = settings.value(QLatin1String("removeDownloadsPolicy"), QLatin1String("Never")).toByteArray(); 556 QByteArray value = settings.value(QLatin1String("removeDownloadsPolicy"), QLatin1String("Never")).toByteArray();
557 QMetaEnum removePolicyEnum = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("RemovePolicy")); 557 QMetaEnum removePolicyEnum = staticMetaObject.enumerator(staticMetaObject.indexOfEnumerator("RemovePolicy"));
558 m_removePolicy = removePolicyEnum.keyToValue(value) == -1 ? Never : 558 m_removePolicy = removePolicyEnum.keyToValue(value) == -1 ? Never :
559 static_cast<RemovePolicy>(removePolicyEnum.keyToValue(value)); 559 cast(RemovePolicy) removePolicyEnum.keyToValue(value);
560 560
561 int i = 0; 561 int i = 0;
562 QString key = QString(QLatin1String("download_%1_")).arg(i); 562 QString key = QString(QLatin1String("download_%1_")).arg(i);
563 while (settings.contains(key + QLatin1String("url"))) { 563 while (settings.contains(key + QLatin1String("url"))) {
564 QUrl url = settings.value(key + QLatin1String("url")).toUrl(); 564 QUrl url = settings.value(key + QLatin1String("url")).toUrl();
583 583
584 AutoSaver m_autoSaver; 584 AutoSaver m_autoSaver;
585 DownloadModel m_model; 585 DownloadModel m_model;
586 QNetworkAccessManager m_manager; 586 QNetworkAccessManager m_manager;
587 QFileIconProvider m_iconProvider; 587 QFileIconProvider m_iconProvider;
588 QList<DownloadItem> m_downloads; 588 DownloadItem[] m_downloads;
589 RemovePolicy m_removePolicy; 589 RemovePolicy m_removePolicy;
590 } 590 }
591 591
592 592
593 class DownloadModel : public QAbstractListModel 593 class DownloadModel : public QAbstractListModel