src/partDownloader.cc

changeset 865
6d68840fcb26
parent 861
83426c5fa732
child 867
557cb07dbe57
equal deleted inserted replaced
864:34033bc2ffb6 865:6d68840fcb26
274 for (PartDownloadRequest* req : requests()) 274 for (PartDownloadRequest* req : requests())
275 { 275 {
276 if (not req->isFinished()) 276 if (not req->isFinished())
277 return; 277 return;
278 278
279 if (req->state() == DLRQ_Failed) 279 if (req->state() == PartDownloadRequest::State::Failed)
280 failed = true; 280 failed = true;
281 } 281 }
282 282
283 for (PartDownloadRequest* req : requests()) 283 for (PartDownloadRequest* req : requests())
284 delete req; 284 delete req;
328 328
329 // ============================================================================= 329 // =============================================================================
330 // 330 //
331 PartDownloadRequest::PartDownloadRequest (QString url, QString dest, bool primary, PartDownloader* parent) : 331 PartDownloadRequest::PartDownloadRequest (QString url, QString dest, bool primary, PartDownloader* parent) :
332 QObject (parent), 332 QObject (parent),
333 m_state (DLRQ_Requesting), 333 m_state (State::Requesting),
334 m_prompt (parent), 334 m_prompt (parent),
335 m_url (url), 335 m_url (url),
336 m_destinaton (dest), 336 m_destinaton (dest),
337 m_filePath (PartDownloader::getDownloadPath() + DIRSLASH + dest), 337 m_filePath (PartDownloader::getDownloadPath() + DIRSLASH + dest),
338 m_networkManager (new QNetworkAccessManager), 338 m_networkManager (new QNetworkAccessManager),
373 QTableWidget* table = prompt()->form()->progress; 373 QTableWidget* table = prompt()->form()->progress;
374 QProgressBar* prog; 374 QProgressBar* prog;
375 375
376 switch (state()) 376 switch (state())
377 { 377 {
378 case DLRQ_Requesting: 378 case State::Requesting:
379 case DLRQ_Downloading: 379 case State::Downloading:
380 { 380 {
381 prog = qobject_cast<QProgressBar*> (table->cellWidget (tableRow(), progcol)); 381 prog = qobject_cast<QProgressBar*> (table->cellWidget (tableRow(), progcol));
382 382
383 if (not prog) 383 if (not prog)
384 { 384 {
388 388
389 prog->setRange (0, numBytesTotal()); 389 prog->setRange (0, numBytesTotal());
390 prog->setValue (numBytesRead()); 390 prog->setValue (numBytesRead());
391 } break; 391 } break;
392 392
393 case DLRQ_Finished: 393 case State::Finished:
394 case DLRQ_Failed: 394 case State::Failed:
395 { 395 {
396 const QString text = (state() == DLRQ_Finished) 396 const QString text = (state() == State::Finished)
397 ? "<b><span style=\"color: #080\">FINISHED</span></b>" 397 ? "<b><span style=\"color: #080\">FINISHED</span></b>"
398 : "<b><span style=\"color: #800\">FAILED</span></b>"; 398 : "<b><span style=\"color: #800\">FAILED</span></b>";
399 399
400 QLabel* lb = new QLabel (text); 400 QLabel* lb = new QLabel (text);
401 lb->setAlignment (Qt::AlignCenter); 401 lb->setAlignment (Qt::AlignCenter);
425 if (networkReply()->error() != QNetworkReply::NoError) 425 if (networkReply()->error() != QNetworkReply::NoError)
426 { 426 {
427 if (isPrimary() and not prompt()->isAborted()) 427 if (isPrimary() and not prompt()->isAborted())
428 CriticalError (networkReply()->errorString()); 428 CriticalError (networkReply()->errorString());
429 429
430 setState (DLRQ_Failed); 430 setState (State::Failed);
431 } 431 }
432 elif (state() != DLRQ_Failed) 432 elif (state() != State::Failed)
433 setState (DLRQ_Finished); 433 setState (State::Finished);
434 434
435 setNumBytesRead (numBytesTotal()); 435 setNumBytesRead (numBytesTotal());
436 updateToTable(); 436 updateToTable();
437 437
438 if (filePointer()) 438 if (filePointer())
439 { 439 {
440 filePointer()->close(); 440 filePointer()->close();
441 delete filePointer(); 441 delete filePointer();
442 setFilePointer (null); 442 setFilePointer (null);
443 443
444 if (state() == DLRQ_Failed) 444 if (state() == State::Failed)
445 QFile::remove (filePath()); 445 QFile::remove (filePath());
446 } 446 }
447 447
448 if (state() != DLRQ_Finished) 448 if (state() != State::Finished)
449 { 449 {
450 prompt()->checkIfFinished(); 450 prompt()->checkIfFinished();
451 return; 451 return;
452 } 452 }
453 453
486 // 486 //
487 void PartDownloadRequest::downloadProgress (int64 recv, int64 total) 487 void PartDownloadRequest::downloadProgress (int64 recv, int64 total)
488 { 488 {
489 setNumBytesRead (recv); 489 setNumBytesRead (recv);
490 setNumBytesTotal (total); 490 setNumBytesTotal (total);
491 setState (DLRQ_Downloading); 491 setState (State::Downloading);
492 updateToTable(); 492 updateToTable();
493 } 493 }
494 494
495 // ============================================================================= 495 // =============================================================================
496 // 496 //
497 void PartDownloadRequest::readyRead() 497 void PartDownloadRequest::readyRead()
498 { 498 {
499 if (state() == DLRQ_Failed) 499 if (state() == State::Failed)
500 return; 500 return;
501 501
502 if (filePointer() == null) 502 if (filePointer() == null)
503 { 503 {
504 m_filePath.replace ("\\", "/"); 504 m_filePath.replace ("\\", "/");
508 setFilePointer (new QFile (filePath().toLocal8Bit())); 508 setFilePointer (new QFile (filePath().toLocal8Bit()));
509 509
510 if (not filePointer()->open (QIODevice::WriteOnly)) 510 if (not filePointer()->open (QIODevice::WriteOnly))
511 { 511 {
512 CriticalError (format (tr ("Couldn't open %1 for writing: %2"), filePath(), strerror (errno))); 512 CriticalError (format (tr ("Couldn't open %1 for writing: %2"), filePath(), strerror (errno)));
513 setState (DLRQ_Failed); 513 setState (State::Failed);
514 networkReply()->abort(); 514 networkReply()->abort();
515 updateToTable(); 515 updateToTable();
516 prompt()->checkIfFinished(); 516 prompt()->checkIfFinished();
517 return; 517 return;
518 } 518 }
523 523
524 // ============================================================================= 524 // =============================================================================
525 // 525 //
526 bool PartDownloadRequest::isFinished() const 526 bool PartDownloadRequest::isFinished() const
527 { 527 {
528 return Eq (state(), DLRQ_Finished, DLRQ_Failed); 528 return Eq (state(), State::Finished, State::Failed);
529 } 529 }
530 530
531 // ============================================================================= 531 // =============================================================================
532 // 532 //
533 void PartDownloadRequest::abort() 533 void PartDownloadRequest::abort()

mercurial