bool Downloader::downloadFiles() { if(files.empty()) return true; setMarquee(true); if(getFileSizes() == OPERATION_STOPPED) { setMarquee(false); return false; } if(!openInternet()) { storeError(); setMarquee(false); return false; } sizeTimeTimer.start(500); updateStatus(msg("Starting download...")); if(!(filesSize == FILE_SIZE_UNKNOWN)) setMarquee(false); for(map<tstring, NetFile *>::iterator i = files.begin(); i != files.end(); i++) { NetFile *file = i->second; if(downloadCancelled) break; if(!file->downloaded) if(!downloadFile(file)) { closeInternet(); return false; } else downloadedFilesSize += file->bytesDownloaded; } closeInternet(); return true; }
bool Downloader::downloadFile(NetFile *netFile) { BYTE *buffer = new BYTE[readBufferSize]; DWORD bytesRead; File file; updateFileName(netFile); updateStatus(msg("Connecting...")); setMarquee(true, false); try { netFile->open(internet); } catch(exception &e) { setMarquee(false, stopOnError ? (netFile->size == FILE_SIZE_UNKNOWN) : false); updateStatus(msg(e.what())); storeError(msg(e.what())); delete[] buffer; return false; } if(!netFile->handle) { setMarquee(false, stopOnError ? (netFile->size == FILE_SIZE_UNKNOWN) : false); updateStatus(msg("Cannot connect")); storeError(); delete[] buffer; return false; } if(!file.open(netFile->name)) { setMarquee(false, stopOnError ? (netFile->size == FILE_SIZE_UNKNOWN) : false); tstring errstr = msg("Cannot create file") + _T(" ") + netFile->name; updateStatus(errstr); storeError(errstr); delete[] buffer; return false; } Timer progressTimer(100); Timer speedTimer(1000); updateStatus(msg("Downloading...")); if(!(netFile->size == FILE_SIZE_UNKNOWN)) setMarquee(false, false); processMessages(); while(true) { if(downloadCancelled) { file.close(); netFile->close(); delete[] buffer; return true; } if(!netFile->read(buffer, readBufferSize, &bytesRead)) { setMarquee(false, netFile->size == FILE_SIZE_UNKNOWN); updateStatus(msg("Download failed")); storeError(); file.close(); netFile->close(); delete[] buffer; return false; } if(bytesRead == 0) break; file.write(buffer, bytesRead); if(progressTimer.elapsed()) updateProgress(netFile); if(speedTimer.elapsed()) updateSpeed(netFile, &speedTimer); if(sizeTimeTimer.elapsed()) updateSizeTime(netFile, &sizeTimeTimer); processMessages(); } updateProgress(netFile); updateSpeed(netFile, &speedTimer); updateSizeTime(netFile, &sizeTimeTimer); updateStatus(msg("Download complete")); processMessages(); file.close(); netFile->close(); netFile->downloaded = true; delete[] buffer; return true; }
bool Downloader::downloadFiles(bool useComponents) { if(ownMsgLoop) downloadCancelled = false; if(files.empty() && ftpDirs.empty()) return true; setMarquee(true); processFtpDirs(); if(getFileSizes() == OPERATION_STOPPED) { TRACE(_T("OPERATION_STOPPED")); setMarquee(false); return false; } TRACE(_T("filesSize: %d"), (DWORD)filesSize); if(!openInternet()) { storeError(); setMarquee(false); return false; } sizeTimeTimer.start(500); updateStatus(msg("Starting download...")); TRACE(_T("Starting file download cycle...")); if(!(filesSize == FILE_SIZE_UNKNOWN)) setMarquee(false); processMessages(); for(map<tstring, NetFile *>::iterator i = files.begin(); i != files.end(); i++) { NetFile *file = i->second; if(downloadCancelled) break; if(useComponents) if(!file->selected(components)) continue; if(!file->downloaded) { // If mirror was used in getFileSizes() function, check mirror first: if(file->mirrorUsed.length()) { NetFile newFile(file->mirrorUsed, file->name, file->size); if(downloadFile(&newFile)) { file->downloaded = newFile.downloaded; file->bytesDownloaded = newFile.bytesDownloaded; downloadedFilesSize += file->bytesDownloaded; continue; } } if(!downloadFile(file)) { TRACE(_T("File was not downloaded.")); if(checkMirrors(i->first, true)) downloadedFilesSize += file->bytesDownloaded; else { if(stopOnError) { closeInternet(); return false; } else { TRACE(_T("Ignoring file %s"), file->name.c_str()); } } } else downloadedFilesSize += file->bytesDownloaded; } processMessages(); } closeInternet(); return filesDownloaded(); }
bool Downloader::downloadFile(NetFile *netFile) { BYTE buffer[READ_BUFFER_SIZE]; DWORD bytesRead; File file; updateFileName(netFile); updateStatus(msg("Connecting...")); setMarquee(true, false); try { netFile->open(internet); } catch(exception &e) { setMarquee(false, netFile->size == FILE_SIZE_UNKNOWN); updateStatus(msg(e.what())); storeError(msg(e.what())); return false; } if(!netFile->handle) { setMarquee(false, netFile->size == FILE_SIZE_UNKNOWN); updateStatus(msg("Cannot connect")); storeError(); return false; } file.open(netFile->name); Timer progressTimer(100); Timer speedTimer(1000); updateStatus(msg("Downloading...")); if(!(netFile->size == FILE_SIZE_UNKNOWN)) setMarquee(false, false); while(true) { if(downloadCancelled) { file.close(); netFile->close(); return true; } if(!netFile->read(buffer, READ_BUFFER_SIZE, &bytesRead)) { setMarquee(false, netFile->size == FILE_SIZE_UNKNOWN); updateStatus(msg("Download failed")); storeError(); file.close(); netFile->close(); return false; } if(bytesRead == 0) break; file.write(buffer, bytesRead); if(progressTimer.elapsed()) updateProgress(netFile); if(speedTimer.elapsed()) updateSpeed(netFile, &speedTimer); if(sizeTimeTimer.elapsed()) updateSizeTime(netFile, &sizeTimeTimer); } updateProgress(netFile); updateSpeed(netFile, &speedTimer); updateSizeTime(netFile, &sizeTimeTimer); updateStatus(msg("Download complete")); file.close(); netFile->close(); netFile->downloaded = true; return true; }