Browse Source

Extract alert handling to separate functions.

adaptive-webui-19844
Ivan Sorokin 11 years ago
parent
commit
1244a46cbb
  1. 152
      src/qtlibtorrent/qbtsession.cpp
  2. 24
      src/qtlibtorrent/qbtsession.h

152
src/qtlibtorrent/qbtsession.cpp

@ -2145,6 +2145,83 @@ void QBtSession::readAlerts() { @@ -2145,6 +2145,83 @@ void QBtSession::readAlerts() {
void QBtSession::handleAlert(libtorrent::alert* a) {
try {
if (torrent_finished_alert* p = dynamic_cast<torrent_finished_alert*>(a)) {
handleTorrentFinishedAlert(p);
}
else if (save_resume_data_alert* p = dynamic_cast<save_resume_data_alert*>(a)) {
handleSaveResumeDataAlert(p);
}
else if (file_renamed_alert* p = dynamic_cast<file_renamed_alert*>(a)) {
handleFileRenamedAlert(p);
}
else if (torrent_deleted_alert* p = dynamic_cast<torrent_deleted_alert*>(a)) {
handleTorrentDeletedAlert(p);
}
else if (storage_moved_alert* p = dynamic_cast<storage_moved_alert*>(a)) {
handleStorageMovedAlert(p);
}
else if (metadata_received_alert* p = dynamic_cast<metadata_received_alert*>(a)) {
handleMetadataReceivedAlert(p);
}
else if (file_error_alert* p = dynamic_cast<file_error_alert*>(a)) {
handleFileErrorAlert(p);
}
else if (file_completed_alert* p = dynamic_cast<file_completed_alert*>(a)) {
handleFileCompletedAlert(p);
}
else if (torrent_paused_alert* p = dynamic_cast<torrent_paused_alert*>(a)) {
handleTorrentPausedAlert(p);
}
else if (tracker_error_alert* p = dynamic_cast<tracker_error_alert*>(a)) {
handleTrackerErrorAlert(p);
}
else if (tracker_reply_alert* p = dynamic_cast<tracker_reply_alert*>(a)) {
handleTrackerReplyAlert(p);
}
else if (tracker_warning_alert* p = dynamic_cast<tracker_warning_alert*>(a)) {
handleTrackerWarningAlert(p);
}
else if (portmap_error_alert* p = dynamic_cast<portmap_error_alert*>(a)) {
handlePortmapWarningAlert(p);
}
else if (portmap_alert* p = dynamic_cast<portmap_alert*>(a)) {
handlePortmapAlert(p);
}
else if (peer_blocked_alert* p = dynamic_cast<peer_blocked_alert*>(a)) {
handlePeerBlockedAlert(p);
}
else if (peer_ban_alert* p = dynamic_cast<peer_ban_alert*>(a)) {
handlePeerBanAlert(p);
}
else if (fastresume_rejected_alert* p = dynamic_cast<fastresume_rejected_alert*>(a)) {
handleFastResumeRejectedAlert(p);
}
else if (url_seed_alert* p = dynamic_cast<url_seed_alert*>(a)) {
handleUrlSeedAlert(p);
}
else if (listen_succeeded_alert *p = dynamic_cast<listen_succeeded_alert*>(a)) {
handleListenSucceededAlert(p);
}
else if (listen_failed_alert *p = dynamic_cast<listen_failed_alert*>(a)) {
handleListenFailedAlert(p);
}
else if (torrent_checked_alert* p = dynamic_cast<torrent_checked_alert*>(a)) {
handleTorrentCheckedAlert(p);
}
else if (external_ip_alert *p = dynamic_cast<external_ip_alert*>(a)) {
handleExternalIPAlert(p);
}
else if (state_update_alert *p = dynamic_cast<state_update_alert *>(a)) {
handleStateUpdateAlert(p);
}
else if (stats_alert *p = dynamic_cast<stats_alert *>(a)) {
handleStatsAlert(p);
}
} 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();
@ -2253,7 +2330,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2253,7 +2330,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
}
}
}
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) {
@ -2271,7 +2349,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2271,7 +2349,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
}
}
}
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) {
@ -2294,7 +2373,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2294,7 +2373,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
}
}
}
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()) {
@ -2316,7 +2396,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2316,7 +2396,8 @@ 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
@ -2336,7 +2417,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2336,7 +2417,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
//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 +2464,10 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2382,10 +2464,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();
@ -2399,7 +2481,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2399,7 +2481,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
}
}
}
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) {
@ -2413,7 +2496,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2413,7 +2496,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
}
}
}
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())) {
@ -2423,7 +2507,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2423,7 +2507,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
}
}
}
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()) {
@ -2441,7 +2526,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2441,7 +2526,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
}
}
}
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);
@ -2455,7 +2541,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2455,7 +2541,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
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
@ -2468,16 +2555,19 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2468,16 +2555,19 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
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) {
@ -2485,7 +2575,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2485,7 +2575,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
//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) {
@ -2493,7 +2584,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2493,7 +2584,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
//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());
@ -2509,11 +2601,13 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2509,11 +2601,13 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
}
}
}
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
@ -2535,7 +2629,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2535,7 +2629,8 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
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 +2647,10 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2552,8 +2647,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();
@ -2579,20 +2676,19 @@ void QBtSession::handleAlert(libtorrent::alert* a) { @@ -2579,20 +2676,19 @@ void QBtSession::handleAlert(libtorrent::alert* a) {
}
}
}
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) {
QTorrentHandle h = getTorrentHandle(hash);

24
src/qtlibtorrent/qbtsession.h

@ -195,6 +195,30 @@ private: @@ -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&);

Loading…
Cancel
Save