- Optimized foreach loop

This commit is contained in:
Christophe Dumez 2009-01-24 19:20:09 +00:00
parent fc79b7dc56
commit 4c6d6a35f3

View file

@ -267,8 +267,7 @@ void bittorrent::deleteTorrent(QString hash, bool permanent) {
QStringList filters; QStringList filters;
filters << hash+".*"; filters << hash+".*";
QStringList files = torrentBackup.entryList(filters, QDir::Files, QDir::Unsorted); QStringList files = torrentBackup.entryList(filters, QDir::Files, QDir::Unsorted);
QString file; foreach(const QString &file, files) {
foreach(file, files) {
torrentBackup.remove(file); torrentBackup.remove(file);
} }
// Remove tracker errors // Remove tracker errors
@ -328,23 +327,21 @@ void bittorrent::loadWebSeeds(QString hash) {
QByteArray urlseeds_lines = urlseeds_file.readAll(); QByteArray urlseeds_lines = urlseeds_file.readAll();
urlseeds_file.close(); urlseeds_file.close();
QList<QByteArray> url_seeds = urlseeds_lines.split('\n'); QList<QByteArray> url_seeds = urlseeds_lines.split('\n');
QByteArray url_seed;
QTorrentHandle h = getTorrentHandle(hash); QTorrentHandle h = getTorrentHandle(hash);
// First remove from the torrent the url seeds that were deleted // First remove from the torrent the url seeds that were deleted
// in a previous session // in a previous session
QStringList seeds_to_delete; QStringList seeds_to_delete;
QStringList existing_seeds = h.url_seeds(); QStringList existing_seeds = h.url_seeds();
QString existing_seed; foreach(const QString &existing_seed, existing_seeds) {
foreach(existing_seed, existing_seeds) {
if(!url_seeds.contains(existing_seed.toUtf8())) { if(!url_seeds.contains(existing_seed.toUtf8())) {
seeds_to_delete << existing_seed; seeds_to_delete << existing_seed;
} }
} }
foreach(existing_seed, seeds_to_delete) { foreach(const QString &existing_seed, seeds_to_delete) {
h.remove_url_seed(existing_seed); h.remove_url_seed(existing_seed);
} }
// Add the ones that were added in a previous session // Add the ones that were added in a previous session
foreach(url_seed, url_seeds) { foreach(const QByteArray &url_seed, url_seeds) {
if(!url_seed.isEmpty()) { if(!url_seed.isEmpty()) {
// XXX: Should we check if it is already in the list before adding it // XXX: Should we check if it is already in the list before adding it
// or is libtorrent clever enough to know // or is libtorrent clever enough to know
@ -877,12 +874,11 @@ bool bittorrent::isFilePreviewPossible(QString hash) const{
void bittorrent::scanDirectory(QString scan_dir) { void bittorrent::scanDirectory(QString scan_dir) {
FSMutex->lock(); FSMutex->lock();
qDebug("Scanning directory: %s", scan_dir.toUtf8().data()); qDebug("Scanning directory: %s", scan_dir.toUtf8().data());
QString file;
QDir dir(scan_dir); QDir dir(scan_dir);
QStringList filters; QStringList filters;
filters << "*.torrent"; filters << "*.torrent";
QStringList files = dir.entryList(filters, QDir::Files, QDir::Unsorted); QStringList files = dir.entryList(filters, QDir::Files, QDir::Unsorted);
foreach(file, files) { foreach(const QString &file, files) {
QString fullPath = dir.path()+QDir::separator()+file; QString fullPath = dir.path()+QDir::separator()+file;
QFile torrent(fullPath); QFile torrent(fullPath);
if(torrent.size() != 0) { if(torrent.size() != 0) {
@ -1000,8 +996,7 @@ bool bittorrent::loadTrackerFile(QString hash) {
tracker_file.open(QIODevice::ReadOnly | QIODevice::Text); tracker_file.open(QIODevice::ReadOnly | QIODevice::Text);
QStringList lines = QString::fromUtf8(tracker_file.readAll().data()).split("\n"); QStringList lines = QString::fromUtf8(tracker_file.readAll().data()).split("\n");
std::vector<announce_entry> trackers; std::vector<announce_entry> trackers;
QString line; foreach(const QString &line, lines) {
foreach(line, lines) {
QStringList parts = line.split("|"); QStringList parts = line.split("|");
if(parts.size() != 2) continue; if(parts.size() != 2) continue;
announce_entry t(parts[0].toStdString()); announce_entry t(parts[0].toStdString());
@ -1284,9 +1279,8 @@ void bittorrent::processDownloadedFile(QString url, QString file_path) {
} }
void bittorrent::downloadFromURLList(const QStringList& url_list) { void bittorrent::downloadFromURLList(const QStringList& url_list) {
QString url;
qDebug("DownloadFromUrlList"); qDebug("DownloadFromUrlList");
foreach(url, url_list) { foreach(const QString url, url_list) {
downloadFromUrl(url); downloadFromUrl(url);
} }
} }
@ -1348,7 +1342,7 @@ void bittorrent::resumeUnfinishedTorrents() {
fileNames = torrentBackup.entryList(filters, QDir::Files, QDir::Unsorted); fileNames = torrentBackup.entryList(filters, QDir::Files, QDir::Unsorted);
if(isQueueingEnabled()) { if(isQueueingEnabled()) {
QList<QPair<int, QString> > filePaths; QList<QPair<int, QString> > filePaths;
foreach(QString fileName, fileNames) { foreach(const QString &fileName, fileNames) {
QString filePath = torrentBackup.path()+QDir::separator()+fileName; QString filePath = torrentBackup.path()+QDir::separator()+fileName;
int prio = 99999; int prio = 99999;
// Get priority // Get priority
@ -1373,11 +1367,11 @@ void bittorrent::resumeUnfinishedTorrents() {
} }
} else { } else {
QStringList filePaths; QStringList filePaths;
foreach(QString fileName, fileNames) { foreach(const QString &fileName, fileNames) {
filePaths.append(torrentBackup.path()+QDir::separator()+fileName); filePaths.append(torrentBackup.path()+QDir::separator()+fileName);
} }
// Resume downloads // Resume downloads
foreach(QString fileName, filePaths) { foreach(const QString &fileName, filePaths) {
addTorrent(fileName, false, QString(), true); addTorrent(fileName, false, QString(), true);
} }
} }