void HttpGetFiles::run() { QRegExp rx("file=(.*)"); m_status = Running; QStringList::iterator iter; for (iter = m_fileList.begin(); iter != m_fileList.end(); ++iter) { QString source(*iter); if (rx.indexIn(source, 0) != -1) { QString destination(m_destinationPath); destination += "/" + rx.cap(1); HttpGet* reply(new HttpGet(m_connection, source, destination)); m_replies.append(reply); connect(this, SIGNAL(interrupted()), this, SLOT(interrupt())); connect(reply, SIGNAL(finished()), this, SLOT(replyFinished())); connect(reply, SIGNAL(copyProgress()), this, SLOT(copyProgress())); } } QList<HttpGet*> replies(m_replies); QList<HttpGet*>::iterator reply; for (reply = replies.begin(); reply != replies.end(); ++reply) { (*reply)->run(); } }
std::pair<int, int> Network::checkOnlineMags(const QUrl & url) { int lowestAvailableMag = NUM_MAG_DATASETS; int highestAvailableMag = 0; int maxMagCount = int_log(NUM_MAG_DATASETS) + 1; QProgressDialog progress("Network Operation…", "Abort", 0, 100, state->mainWindow); progress.setModal(true); std::vector<qint64> bytesReceivedAll(maxMagCount); std::vector<qint64> bytesTotalAll(maxMagCount); std::vector<std::unique_ptr<QNetworkReply>> replies(maxMagCount); QTimer::singleShot(400, &progress, &QProgressDialog::show); QEventLoop pause; for (int currMag = 1; currMag <= NUM_MAG_DATASETS; currMag *= 2) { QUrl magUrl = url; magUrl.setPath(QString("%1/mag%2/knossos.conf").arg(url.path()).arg(currMag)); auto * replyPtr = manager.get(QNetworkRequest{magUrl}); replies[int_log(currMag)] = decltype(replies)::value_type{replyPtr}; QObject::connect(replyPtr, &QNetworkReply::finished, [magUrl, &pause, &maxMagCount, currMag, replyPtr, &lowestAvailableMag, &highestAvailableMag]() { auto & reply = *replyPtr; if (reply.error() == QNetworkReply::NoError) { lowestAvailableMag = std::min(lowestAvailableMag, currMag); highestAvailableMag = std::max(highestAvailableMag, currMag); } if (--maxMagCount == 0) { pause.exit(); } }); auto processProgress = [&progress, currMag, &bytesReceivedAll, &bytesTotalAll](qint64 bytesReceived, qint64 bytesTotal){ bytesReceivedAll[int_log(currMag)] = bytesReceived; bytesTotalAll[int_log(currMag)] = bytesTotal; const auto received = std::accumulate(std::begin(bytesReceivedAll), std::end(bytesReceivedAll), qint64{0}); const auto total = std::accumulate(std::begin(bytesTotalAll), std::end(bytesTotalAll), qint64{0}); progress.setRange(0, total); progress.setValue(received); }; QObject::connect(replyPtr, &QNetworkReply::downloadProgress, processProgress); QObject::connect(replyPtr, &QNetworkReply::uploadProgress, processProgress); QObject::connect(&progress, &QProgressDialog::canceled, replyPtr, &QNetworkReply::abort); } pause.exec(); if (lowestAvailableMag > highestAvailableMag) { throw std::runtime_error{"no mags detected"}; } return {lowestAvailableMag, highestAvailableMag}; }