From 7f300b757fe4f8606513e95f6d405ff1ef08df36 Mon Sep 17 00:00:00 2001 From: Chocobo1 Date: Wed, 31 Jul 2019 20:00:04 +0800 Subject: [PATCH] Use C++11 uniform initialization --- src/webui/api/logcontroller.cpp | 27 +++--- src/webui/api/serialize/serialize_torrent.cpp | 91 ++++++++++--------- src/webui/api/synccontroller.cpp | 30 +++--- src/webui/api/torrentscontroller.cpp | 17 ++-- 4 files changed, 86 insertions(+), 79 deletions(-) diff --git a/src/webui/api/logcontroller.cpp b/src/webui/api/logcontroller.cpp index 9310821e5..b512941ec 100644 --- a/src/webui/api/logcontroller.cpp +++ b/src/webui/api/logcontroller.cpp @@ -78,12 +78,13 @@ void LogController::mainAction() || (msg.type == Log::WARNING && isWarning) || (msg.type == Log::CRITICAL && isCritical))) continue; - QVariantMap map; - map[KEY_LOG_ID] = msg.id; - map[KEY_LOG_TIMESTAMP] = msg.timestamp; - map[KEY_LOG_MSG_TYPE] = msg.type; - map[KEY_LOG_MSG_MESSAGE] = msg.message; - msgList.append(map); + + msgList.append(QVariantMap { + {KEY_LOG_ID, msg.id}, + {KEY_LOG_TIMESTAMP, msg.timestamp}, + {KEY_LOG_MSG_TYPE, msg.type}, + {KEY_LOG_MSG_MESSAGE, msg.message} + }); } setResult(QJsonArray::fromVariantList(msgList)); @@ -112,13 +113,13 @@ void LogController::peersAction() QVariantList peerList; for (const Log::Peer &peer : asConst(logger->getPeers(lastKnownId))) { - QVariantMap map; - map[KEY_LOG_ID] = peer.id; - map[KEY_LOG_TIMESTAMP] = peer.timestamp; - map[KEY_LOG_PEER_IP] = peer.ip; - map[KEY_LOG_PEER_BLOCKED] = peer.blocked; - map[KEY_LOG_PEER_REASON] = peer.reason; - peerList.append(map); + peerList.append(QVariantMap { + {KEY_LOG_ID, peer.id}, + {KEY_LOG_TIMESTAMP, peer.timestamp}, + {KEY_LOG_PEER_IP, peer.ip}, + {KEY_LOG_PEER_BLOCKED, peer.blocked}, + {KEY_LOG_PEER_REASON, peer.reason} + }); } setResult(QJsonArray::fromVariantList(peerList)); diff --git a/src/webui/api/serialize/serialize_torrent.cpp b/src/webui/api/serialize/serialize_torrent.cpp index 655902607..9f22966d4 100644 --- a/src/webui/api/serialize/serialize_torrent.cpp +++ b/src/webui/api/serialize/serialize_torrent.cpp @@ -82,50 +82,55 @@ namespace QVariantMap serialize(const BitTorrent::TorrentHandle &torrent) { - QVariantMap ret; - ret[KEY_TORRENT_HASH] = QString(torrent.hash()); - ret[KEY_TORRENT_NAME] = torrent.name(); - ret[KEY_TORRENT_MAGNET_URI] = torrent.toMagnetUri(); - ret[KEY_TORRENT_SIZE] = torrent.wantedSize(); - ret[KEY_TORRENT_PROGRESS] = torrent.progress(); - ret[KEY_TORRENT_DLSPEED] = torrent.downloadPayloadRate(); - ret[KEY_TORRENT_UPSPEED] = torrent.uploadPayloadRate(); - ret[KEY_TORRENT_QUEUE_POSITION] = static_cast(torrent.queuePosition()); - ret[KEY_TORRENT_SEEDS] = torrent.seedsCount(); - ret[KEY_TORRENT_NUM_COMPLETE] = torrent.totalSeedsCount(); - ret[KEY_TORRENT_LEECHS] = torrent.leechsCount(); - ret[KEY_TORRENT_NUM_INCOMPLETE] = torrent.totalLeechersCount(); + QVariantMap ret = { + {KEY_TORRENT_HASH, QString(torrent.hash())}, + {KEY_TORRENT_NAME, torrent.name()}, + {KEY_TORRENT_MAGNET_URI, torrent.toMagnetUri()}, + {KEY_TORRENT_SIZE, torrent.wantedSize()}, + {KEY_TORRENT_PROGRESS, torrent.progress()}, + {KEY_TORRENT_DLSPEED, torrent.downloadPayloadRate()}, + {KEY_TORRENT_UPSPEED, torrent.uploadPayloadRate()}, + {KEY_TORRENT_QUEUE_POSITION, torrent.queuePosition()}, + {KEY_TORRENT_SEEDS, torrent.seedsCount()}, + {KEY_TORRENT_NUM_COMPLETE, torrent.totalSeedsCount()}, + {KEY_TORRENT_LEECHS, torrent.leechsCount()}, + {KEY_TORRENT_NUM_INCOMPLETE, torrent.totalLeechersCount()}, + + {KEY_TORRENT_STATE, torrentStateToString(torrent.state())}, + {KEY_TORRENT_ETA, torrent.eta()}, + {KEY_TORRENT_SEQUENTIAL_DOWNLOAD, torrent.isSequentialDownload()}, + {KEY_TORRENT_FIRST_LAST_PIECE_PRIO, torrent.hasFirstLastPiecePriority()}, + + {KEY_TORRENT_CATEGORY, torrent.category()}, + {KEY_TORRENT_TAGS, torrent.tags().toList().join(", ")}, + {KEY_TORRENT_SUPER_SEEDING, torrent.superSeeding()}, + {KEY_TORRENT_FORCE_START, torrent.isForced()}, + {KEY_TORRENT_SAVE_PATH, Utils::Fs::toNativePath(torrent.savePath())}, + {KEY_TORRENT_ADDED_ON, torrent.addedTime().toTime_t()}, + {KEY_TORRENT_COMPLETION_ON, torrent.completedTime().toTime_t()}, + {KEY_TORRENT_TRACKER, torrent.currentTracker()}, + {KEY_TORRENT_DL_LIMIT, torrent.downloadLimit()}, + {KEY_TORRENT_UP_LIMIT, torrent.uploadLimit()}, + {KEY_TORRENT_AMOUNT_DOWNLOADED, torrent.totalDownload()}, + {KEY_TORRENT_AMOUNT_UPLOADED, torrent.totalUpload()}, + {KEY_TORRENT_AMOUNT_DOWNLOADED_SESSION, torrent.totalPayloadDownload()}, + {KEY_TORRENT_AMOUNT_UPLOADED_SESSION, torrent.totalPayloadUpload()}, + {KEY_TORRENT_AMOUNT_LEFT, torrent.incompletedSize()}, + {KEY_TORRENT_AMOUNT_COMPLETED, torrent.completedSize()}, + {KEY_TORRENT_MAX_RATIO, torrent.maxRatio()}, + {KEY_TORRENT_MAX_SEEDING_TIME, torrent.maxSeedingTime()}, + {KEY_TORRENT_RATIO_LIMIT, torrent.ratioLimit()}, + {KEY_TORRENT_SEEDING_TIME_LIMIT, torrent.seedingTimeLimit()}, + {KEY_TORRENT_LAST_SEEN_COMPLETE_TIME, torrent.lastSeenComplete().toTime_t()}, + {KEY_TORRENT_AUTO_TORRENT_MANAGEMENT, torrent.isAutoTMMEnabled()}, + {KEY_TORRENT_TIME_ACTIVE, torrent.activeTime()}, + {KEY_TORRENT_AVAILABILITY, torrent.distributedCopies()}, + + {KEY_TORRENT_TOTAL_SIZE, torrent.totalSize()} + }; + const qreal ratio = torrent.realRatio(); ret[KEY_TORRENT_RATIO] = (ratio > BitTorrent::TorrentHandle::MAX_RATIO) ? -1 : ratio; - ret[KEY_TORRENT_STATE] = torrentStateToString(torrent.state()); - ret[KEY_TORRENT_ETA] = torrent.eta(); - ret[KEY_TORRENT_SEQUENTIAL_DOWNLOAD] = torrent.isSequentialDownload(); - if (torrent.hasMetadata()) - ret[KEY_TORRENT_FIRST_LAST_PIECE_PRIO] = torrent.hasFirstLastPiecePriority(); - ret[KEY_TORRENT_CATEGORY] = torrent.category(); - ret[KEY_TORRENT_TAGS] = torrent.tags().toList().join(", "); - ret[KEY_TORRENT_SUPER_SEEDING] = torrent.superSeeding(); - ret[KEY_TORRENT_FORCE_START] = torrent.isForced(); - ret[KEY_TORRENT_SAVE_PATH] = Utils::Fs::toNativePath(torrent.savePath()); - ret[KEY_TORRENT_ADDED_ON] = torrent.addedTime().toTime_t(); - ret[KEY_TORRENT_COMPLETION_ON] = torrent.completedTime().toTime_t(); - ret[KEY_TORRENT_TRACKER] = torrent.currentTracker(); - ret[KEY_TORRENT_DL_LIMIT] = torrent.downloadLimit(); - ret[KEY_TORRENT_UP_LIMIT] = torrent.uploadLimit(); - ret[KEY_TORRENT_AMOUNT_DOWNLOADED] = torrent.totalDownload(); - ret[KEY_TORRENT_AMOUNT_UPLOADED] = torrent.totalUpload(); - ret[KEY_TORRENT_AMOUNT_DOWNLOADED_SESSION] = torrent.totalPayloadDownload(); - ret[KEY_TORRENT_AMOUNT_UPLOADED_SESSION] = torrent.totalPayloadUpload(); - ret[KEY_TORRENT_AMOUNT_LEFT] = torrent.incompletedSize(); - ret[KEY_TORRENT_AMOUNT_COMPLETED] = torrent.completedSize(); - ret[KEY_TORRENT_MAX_RATIO] = torrent.maxRatio(); - ret[KEY_TORRENT_MAX_SEEDING_TIME] = torrent.maxSeedingTime(); - ret[KEY_TORRENT_RATIO_LIMIT] = torrent.ratioLimit(); - ret[KEY_TORRENT_SEEDING_TIME_LIMIT] = torrent.seedingTimeLimit(); - ret[KEY_TORRENT_LAST_SEEN_COMPLETE_TIME] = torrent.lastSeenComplete().toTime_t(); - ret[KEY_TORRENT_AUTO_TORRENT_MANAGEMENT] = torrent.isAutoTMMEnabled(); - ret[KEY_TORRENT_TIME_ACTIVE] = torrent.activeTime(); - ret[KEY_TORRENT_AVAILABILITY] = torrent.distributedCopies(); if (torrent.isPaused() || torrent.isChecking()) { ret[KEY_TORRENT_LAST_ACTIVITY_TIME] = 0; @@ -136,7 +141,5 @@ QVariantMap serialize(const BitTorrent::TorrentHandle &torrent) ret[KEY_TORRENT_LAST_ACTIVITY_TIME] = dt.toTime_t(); } - ret[KEY_TORRENT_TOTAL_SIZE] = torrent.totalSize(); - return ret; } diff --git a/src/webui/api/synccontroller.cpp b/src/webui/api/synccontroller.cpp index f13669c4d..c3eecb6b8 100644 --- a/src/webui/api/synccontroller.cpp +++ b/src/webui/api/synccontroller.cpp @@ -503,26 +503,28 @@ void SyncController::torrentPeersAction() for (const BitTorrent::PeerInfo &pi : peersList) { if (pi.address().ip.isNull()) continue; - QVariantMap peer; + QVariantMap peer = { + {KEY_PEER_IP, pi.address().ip.toString()}, + {KEY_PEER_PORT, pi.address().port}, + {KEY_PEER_CLIENT, pi.client()}, + {KEY_PEER_PROGRESS, pi.progress()}, + {KEY_PEER_DOWN_SPEED, pi.payloadDownSpeed()}, + {KEY_PEER_UP_SPEED, pi.payloadUpSpeed()}, + {KEY_PEER_TOT_DOWN, pi.totalDownload()}, + {KEY_PEER_TOT_UP, pi.totalUpload()}, + {KEY_PEER_CONNECTION_TYPE, pi.connectionType()}, + {KEY_PEER_FLAGS, pi.flags()}, + {KEY_PEER_FLAGS_DESCRIPTION, pi.flagsDescription()}, + {KEY_PEER_RELEVANCE, pi.relevance()}, + {KEY_PEER_FILES, torrent->info().filesForPiece(pi.downloadingPieceIndex()).join('\n')} + }; + #ifndef DISABLE_COUNTRIES_RESOLUTION if (resolvePeerCountries) { peer[KEY_PEER_COUNTRY_CODE] = pi.country().toLower(); peer[KEY_PEER_COUNTRY] = Net::GeoIPManager::CountryName(pi.country()); } #endif - peer[KEY_PEER_IP] = pi.address().ip.toString(); - peer[KEY_PEER_PORT] = pi.address().port; - peer[KEY_PEER_CLIENT] = pi.client(); - peer[KEY_PEER_PROGRESS] = pi.progress(); - peer[KEY_PEER_DOWN_SPEED] = pi.payloadDownSpeed(); - peer[KEY_PEER_UP_SPEED] = pi.payloadUpSpeed(); - peer[KEY_PEER_TOT_DOWN] = pi.totalDownload(); - peer[KEY_PEER_TOT_UP] = pi.totalUpload(); - peer[KEY_PEER_CONNECTION_TYPE] = pi.connectionType(); - peer[KEY_PEER_FLAGS] = pi.flags(); - peer[KEY_PEER_FLAGS_DESCRIPTION] = pi.flagsDescription(); - peer[KEY_PEER_RELEVANCE] = pi.relevance(); - peer[KEY_PEER_FILES] = torrent->info().filesForPiece(pi.downloadingPieceIndex()).join(QLatin1String("\n")); peers[pi.address().ip.toString() + ':' + QString::number(pi.address().port)] = peer; } diff --git a/src/webui/api/torrentscontroller.cpp b/src/webui/api/torrentscontroller.cpp index 0a965bf28..741687833 100644 --- a/src/webui/api/torrentscontroller.cpp +++ b/src/webui/api/torrentscontroller.cpp @@ -417,9 +417,9 @@ void TorrentsController::webseedsAction() throw APIError(APIErrorType::NotFound); for (const QUrl &webseed : asConst(torrent->urlSeeds())) { - QVariantMap webSeedDict; - webSeedDict[KEY_WEBSEED_URL] = webseed.toString(); - webSeedList.append(webSeedDict); + webSeedList.append(QVariantMap { + {KEY_WEBSEED_URL, webseed.toString()} + }); } setResult(QJsonArray::fromVariantList(webSeedList)); @@ -451,11 +451,12 @@ void TorrentsController::filesAction() const QVector fileAvailability = torrent->availableFileFractions(); const BitTorrent::TorrentInfo info = torrent->info(); for (int i = 0; i < torrent->filesCount(); ++i) { - QVariantMap fileDict; - fileDict[KEY_FILE_PROGRESS] = fp[i]; - fileDict[KEY_FILE_PRIORITY] = static_cast(priorities[i]); - fileDict[KEY_FILE_SIZE] = torrent->fileSize(i); - fileDict[KEY_FILE_AVAILABILITY] = fileAvailability[i]; + QVariantMap fileDict = { + {KEY_FILE_PROGRESS, fp[i]}, + {KEY_FILE_PRIORITY, static_cast(priorities[i])}, + {KEY_FILE_SIZE, torrent->fileSize(i)}, + {KEY_FILE_AVAILABILITY, fileAvailability[i]} + }; QString fileName = torrent->filePath(i); if (fileName.endsWith(QB_EXT, Qt::CaseInsensitive))