Merge pull request #1730 from sorokin/no-dyn-cast

split handling of alerts into several functions and use switch instead of dynamic_casts
This commit is contained in:
sledgehammer999 2014-06-04 14:57:56 +03:00
commit b198c074d6
2 changed files with 566 additions and 444 deletions

View file

@ -2144,7 +2144,86 @@ void QBtSession::readAlerts() {
void QBtSession::handleAlert(libtorrent::alert* a) {
try {
if (torrent_finished_alert* p = dynamic_cast<torrent_finished_alert*>(a)) {
switch (a->type()) {
case torrent_finished_alert::alert_type:
handleTorrentFinishedAlert(static_cast<torrent_finished_alert*>(a));
break;
case save_resume_data_alert::alert_type:
handleSaveResumeDataAlert(static_cast<save_resume_data_alert*>(a));
break;
case file_renamed_alert::alert_type:
handleFileRenamedAlert(static_cast<file_renamed_alert*>(a));
break;
case torrent_deleted_alert::alert_type:
handleTorrentDeletedAlert(static_cast<torrent_deleted_alert*>(a));
break;
case storage_moved_alert::alert_type:
handleStorageMovedAlert(static_cast<storage_moved_alert*>(a));
break;
case metadata_received_alert::alert_type:
handleMetadataReceivedAlert(static_cast<metadata_received_alert*>(a));
break;
case file_error_alert::alert_type:
handleFileErrorAlert(static_cast<file_error_alert*>(a));
break;
case file_completed_alert::alert_type:
handleFileCompletedAlert(static_cast<file_completed_alert*>(a));
break;
case torrent_paused_alert::alert_type:
handleTorrentPausedAlert(static_cast<torrent_paused_alert*>(a));
break;
case tracker_error_alert::alert_type:
handleTrackerErrorAlert(static_cast<tracker_error_alert*>(a));
break;
case tracker_reply_alert::alert_type:
handleTrackerReplyAlert(static_cast<tracker_reply_alert*>(a));
break;
case tracker_warning_alert::alert_type:
handleTrackerWarningAlert(static_cast<tracker_warning_alert*>(a));
break;
case portmap_error_alert::alert_type:
handlePortmapWarningAlert(static_cast<portmap_error_alert*>(a));
break;
case portmap_alert::alert_type:
handlePortmapAlert(static_cast<portmap_alert*>(a));
break;
case peer_blocked_alert::alert_type:
handlePeerBlockedAlert(static_cast<peer_blocked_alert*>(a));
break;
case peer_ban_alert::alert_type:
handlePeerBanAlert(static_cast<peer_ban_alert*>(a));
break;
case fastresume_rejected_alert::alert_type:
handleFastResumeRejectedAlert(static_cast<fastresume_rejected_alert*>(a));
break;
case url_seed_alert::alert_type:
handleUrlSeedAlert(static_cast<url_seed_alert*>(a));
break;
case listen_succeeded_alert::alert_type:
handleListenSucceededAlert(static_cast<listen_succeeded_alert*>(a));
break;
case listen_failed_alert::alert_type:
handleListenFailedAlert(static_cast<listen_failed_alert*>(a));
break;
case torrent_checked_alert::alert_type:
handleTorrentCheckedAlert(static_cast<torrent_checked_alert*>(a));
break;
case external_ip_alert::alert_type:
handleExternalIPAlert(static_cast<external_ip_alert*>(a));
break;
case state_update_alert::alert_type:
handleStateUpdateAlert(static_cast<state_update_alert*>(a));
break;
case stats_alert::alert_type:
handleStatsAlert(static_cast<stats_alert*>(a));
break;
}
} catch (const std::exception& e) {
qWarning() << "Caught exception in readAlerts(): " << e.what();
}
}
void QBtSession::handleTorrentFinishedAlert(libtorrent::torrent_finished_alert* p) {
QTorrentHandle h(p->handle);
if (h.is_valid()) {
const QString hash = h.hash();
@ -2252,8 +2331,9 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
#endif // DISABLE_GUI
}
}
}
else if (save_resume_data_alert* p = dynamic_cast<save_resume_data_alert*>(a)) {
}
void QBtSession::handleSaveResumeDataAlert(libtorrent::save_resume_data_alert* p) {
const QDir torrentBackup(fsutils::BTBackupLocation());
const QTorrentHandle h(p->handle);
if (h.is_valid() && p->resume_data) {
@ -2270,8 +2350,9 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
resume_file.close();
}
}
}
else if (file_renamed_alert* p = dynamic_cast<file_renamed_alert*>(a)) {
}
void QBtSession::handleFileRenamedAlert(libtorrent::file_renamed_alert* p) {
QTorrentHandle h(p->handle);
if (h.is_valid()) {
if (h.num_files() > 1) {
@ -2293,8 +2374,9 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
emit savePathChanged(h);
}
}
}
else if (torrent_deleted_alert* p = dynamic_cast<torrent_deleted_alert*>(a)) {
}
void QBtSession::handleTorrentDeletedAlert(libtorrent::torrent_deleted_alert* p) {
qDebug("A torrent was deleted from the hard disk, attempting to remove the root folder too...");
QString hash = misc::toQString(p->info_hash);
if (!hash.isEmpty()) {
@ -2315,8 +2397,9 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
}
}
}
}
else if (storage_moved_alert* p = dynamic_cast<storage_moved_alert*>(a)) {
}
void QBtSession::handleStorageMovedAlert(libtorrent::storage_moved_alert* p) {
QTorrentHandle h(p->handle);
if (h.is_valid()) {
// Attempt to remove old folder if empty
@ -2335,8 +2418,9 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
emit savePathChanged(h);
//h.force_recheck();
}
}
else if (metadata_received_alert* p = dynamic_cast<metadata_received_alert*>(a)) {
}
void QBtSession::handleMetadataReceivedAlert(libtorrent::metadata_received_alert* p) {
QTorrentHandle h(p->handle);
Preferences pref;
if (h.is_valid()) {
@ -2382,10 +2466,10 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
// and the torrent can be paused when metadata is received
emit pausedTorrent(h);
}
}
}
}
}
else if (file_error_alert* p = dynamic_cast<file_error_alert*>(a)) {
void QBtSession::handleFileErrorAlert(libtorrent::file_error_alert* p) {
QTorrentHandle h(p->handle);
if (h.is_valid()) {
h.pause();
@ -2398,8 +2482,9 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
emit pausedTorrent(h);
}
}
}
else if (file_completed_alert* p = dynamic_cast<file_completed_alert*>(a)) {
}
void QBtSession::handleFileCompletedAlert(libtorrent::file_completed_alert* p) {
QTorrentHandle h(p->handle);
qDebug("A file completed download in torrent %s", qPrintable(h.name()));
if (appendqBExtension) {
@ -2412,8 +2497,9 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
h.rename_file(p->index, name);
}
}
}
else if (torrent_paused_alert* p = dynamic_cast<torrent_paused_alert*>(a)) {
}
void QBtSession::handleTorrentPausedAlert(libtorrent::torrent_paused_alert* p) {
if (p->handle.is_valid()) {
QTorrentHandle h(p->handle);
if (!HiddenData::hasData(h.hash())) {
@ -2422,8 +2508,9 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
emit pausedTorrent(h);
}
}
}
else if (tracker_error_alert* p = dynamic_cast<tracker_error_alert*>(a)) {
}
void QBtSession::handleTrackerErrorAlert(libtorrent::tracker_error_alert* p) {
// Level: fatal
QTorrentHandle h(p->handle);
if (h.is_valid()) {
@ -2440,8 +2527,9 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
emit trackerAuthenticationRequired(h);
}
}
}
else if (tracker_reply_alert* p = dynamic_cast<tracker_reply_alert*>(a)) {
}
void QBtSession::handleTrackerReplyAlert(libtorrent::tracker_reply_alert* p) {
const QTorrentHandle h(p->handle);
if (h.is_valid()) {
qDebug("Received a tracker reply from %s (Num_peers=%d)", p->url.c_str(), p->num_peers);
@ -2454,8 +2542,9 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
trackers_data.insert(tracker_url, data);
trackersInfos[h.hash()] = trackers_data;
}
}
else if (tracker_warning_alert* p = dynamic_cast<tracker_warning_alert*>(a)) {
}
void QBtSession::handleTrackerWarningAlert(libtorrent::tracker_warning_alert* p) {
const QTorrentHandle h(p->handle);
if (h.is_valid()) {
// Connection was successful now but there is a warning message
@ -2467,33 +2556,38 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
trackersInfos[h.hash()] = trackers_data;
qDebug("Received a tracker warning from %s: %s", p->url.c_str(), p->msg.c_str());
}
}
else if (portmap_error_alert* p = dynamic_cast<portmap_error_alert*>(a)) {
}
void QBtSession::handlePortmapWarningAlert(libtorrent::portmap_error_alert* p) {
addConsoleMessage(tr("UPnP/NAT-PMP: Port mapping failure, message: %1").arg(misc::toQStringU(p->message())), "red");
//emit UPnPError(QString(p->msg().c_str()));
}
else if (portmap_alert* p = dynamic_cast<portmap_alert*>(a)) {
}
void QBtSession::handlePortmapAlert(libtorrent::portmap_alert* p) {
qDebug("UPnP Success, msg: %s", p->message().c_str());
addConsoleMessage(tr("UPnP/NAT-PMP: Port mapping successful, message: %1").arg(misc::toQStringU(p->message())), "blue");
//emit UPnPSuccess(QString(p->msg().c_str()));
}
else if (peer_blocked_alert* p = dynamic_cast<peer_blocked_alert*>(a)) {
}
void QBtSession::handlePeerBlockedAlert(libtorrent::peer_blocked_alert* p) {
boost::system::error_code ec;
string ip = p->ip.to_string(ec);
if (!ec) {
addPeerBanMessage(QString::fromLatin1(ip.c_str()), true);
//emit peerBlocked(QString::fromLatin1(ip.c_str()));
}
}
else if (peer_ban_alert* p = dynamic_cast<peer_ban_alert*>(a)) {
}
void QBtSession::handlePeerBanAlert(libtorrent::peer_ban_alert* p) {
boost::system::error_code ec;
string ip = p->ip.address().to_string(ec);
if (!ec) {
addPeerBanMessage(QString::fromLatin1(ip.c_str()), false);
//emit peerBlocked(QString::fromLatin1(ip.c_str()));
}
}
else if (fastresume_rejected_alert* p = dynamic_cast<fastresume_rejected_alert*>(a)) {
}
void QBtSession::handleFastResumeRejectedAlert(libtorrent::fastresume_rejected_alert* p) {
QTorrentHandle h(p->handle);
if (h.is_valid()) {
qDebug("/!\\ Fast resume failed for %s, reason: %s", qPrintable(h.name()), p->message().c_str());
@ -2508,12 +2602,14 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
addConsoleMessage(tr("Reason: %1").arg(misc::toQStringU(p->message())));
}
}
}
else if (url_seed_alert* p = dynamic_cast<url_seed_alert*>(a)) {
}
void QBtSession::handleUrlSeedAlert(libtorrent::url_seed_alert* p) {
addConsoleMessage(tr("Url seed lookup failed for url: %1, message: %2").arg(misc::toQString(p->url)).arg(misc::toQStringU(p->message())), QString::fromUtf8("red"));
//emit urlSeedProblem(QString::fromUtf8(p->url.c_str()), QString::fromUtf8(p->msg().c_str()));
}
else if (listen_succeeded_alert *p = dynamic_cast<listen_succeeded_alert*>(a)) {
}
void QBtSession::handleListenSucceededAlert(libtorrent::listen_succeeded_alert *p) {
boost::system::error_code ec;
QString proto = "TCP";
#if LIBTORRENT_VERSION_NUM >= 10000
@ -2534,8 +2630,9 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
for ( ; it != itend; ++it) {
it->force_reannounce();
}
}
else if (listen_failed_alert *p = dynamic_cast<listen_failed_alert*>(a)) {
}
void QBtSession::handleListenFailedAlert(libtorrent::listen_failed_alert *p) {
boost::system::error_code ec;
QString proto = "TCP";
#if LIBTORRENT_VERSION_NUM >= 10000
@ -2552,8 +2649,10 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
#endif
qDebug() << "Failed listening on " << proto << p->endpoint.address().to_string(ec).c_str() << "/" << p->endpoint.port();
addConsoleMessage(tr("qBittorrent failed listening on interface %1 port: %2/%3. Reason: %4", "e.g: qBittorrent failed listening on interface 192.168.0.1 port: TCP/6881. Reason: already in use").arg(p->endpoint.address().to_string(ec).c_str()).arg(proto).arg(QString::number(p->endpoint.port())).arg(misc::toQStringU(p->error.message())), "red");
}
else if (torrent_checked_alert* p = dynamic_cast<torrent_checked_alert*>(a)) {
}
void QBtSession::handleTorrentCheckedAlert(libtorrent::torrent_checked_alert* p) {
QTorrentHandle h(p->handle);
if (h.is_valid()) {
const QString hash = h.hash();
@ -2578,20 +2677,19 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
emit pausedTorrent(h);
}
}
}
else if (external_ip_alert *p = dynamic_cast<external_ip_alert*>(a)) {
}
void QBtSession::handleExternalIPAlert(libtorrent::external_ip_alert *p) {
boost::system::error_code ec;
addConsoleMessage(tr("External IP: %1", "e.g. External IP: 192.168.0.1").arg(p->external_address.to_string(ec).c_str()), "blue");
}
else if (state_update_alert *p = dynamic_cast<state_update_alert *>(a)) {
}
void QBtSession::handleStateUpdateAlert(libtorrent::state_update_alert *p) {
emit stateUpdate(p->status);
}
else if (stats_alert *p = dynamic_cast<stats_alert *>(a)) {
}
void QBtSession::handleStatsAlert(libtorrent::stats_alert *p) {
emit statsReceived(*p);
}
} catch (const std::exception& e) {
qWarning() << "Caught exception in readAlerts(): " << e.what();
}
}
void QBtSession::recheckTorrent(const QString &hash) {

View file

@ -195,6 +195,30 @@ private:
void recoverPersistentData(const QString &hash, const std::vector<char> &buf);
void backupPersistentData(const QString &hash, boost::shared_ptr<libtorrent::entry> data);
void handleAlert(libtorrent::alert* a);
void handleTorrentFinishedAlert(libtorrent::torrent_finished_alert* p);
void handleSaveResumeDataAlert(libtorrent::save_resume_data_alert* p);
void handleFileRenamedAlert(libtorrent::file_renamed_alert* p);
void handleTorrentDeletedAlert(libtorrent::torrent_deleted_alert* p);
void handleStorageMovedAlert(libtorrent::storage_moved_alert* p);
void handleMetadataReceivedAlert(libtorrent::metadata_received_alert* p);
void handleFileErrorAlert(libtorrent::file_error_alert* p);
void handleFileCompletedAlert(libtorrent::file_completed_alert* p);
void handleTorrentPausedAlert(libtorrent::torrent_paused_alert* p);
void handleTrackerErrorAlert(libtorrent::tracker_error_alert* p);
void handleTrackerReplyAlert(libtorrent::tracker_reply_alert* p);
void handleTrackerWarningAlert(libtorrent::tracker_warning_alert* p);
void handlePortmapWarningAlert(libtorrent::portmap_error_alert* p);
void handlePortmapAlert(libtorrent::portmap_alert* p);
void handlePeerBlockedAlert(libtorrent::peer_blocked_alert* p);
void handlePeerBanAlert(libtorrent::peer_ban_alert* p);
void handleFastResumeRejectedAlert(libtorrent::fastresume_rejected_alert* p);
void handleUrlSeedAlert(libtorrent::url_seed_alert* p);
void handleListenSucceededAlert(libtorrent::listen_succeeded_alert *p);
void handleListenFailedAlert(libtorrent::listen_failed_alert *p);
void handleTorrentCheckedAlert(libtorrent::torrent_checked_alert* p);
void handleExternalIPAlert(libtorrent::external_ip_alert *p);
void handleStateUpdateAlert(libtorrent::state_update_alert *p);
void handleStatsAlert(libtorrent::stats_alert *p);
private slots:
void addTorrentsFromScanFolder(QStringList&);