diff --git a/src/app/application.cpp b/src/app/application.cpp index 7908a9540..5a74d27a4 100644 --- a/src/app/application.cpp +++ b/src/app/application.cpp @@ -453,6 +453,7 @@ void Application::runExternalProgram(const QString &programTemplate, const BitTo }; const QString logMsg = tr("Running external program. Torrent: \"%1\". Command: `%2`"); + const QString logMsgError = tr("Failed to run external program. Torrent: \"%1\". Command: `%2`"); // The processing sequenece is different for Windows and other OS, this is intentional #if defined(Q_OS_WIN) @@ -472,8 +473,6 @@ void Application::runExternalProgram(const QString &programTemplate, const BitTo for (int i = 1; i < argCount; ++i) argList += QString::fromWCharArray(args[i]); - LogMsg(logMsg.arg(torrent->name(), program)); - QProcess proc; proc.setProgram(QString::fromWCharArray(args[0])); proc.setArguments(argList); @@ -498,7 +497,11 @@ void Application::runExternalProgram(const QString &programTemplate, const BitTo args->startupInfo->hStdOutput = nullptr; args->startupInfo->hStdError = nullptr; }); - proc.startDetached(); + + if (proc.startDetached()) + LogMsg(logMsg.arg(torrent->name(), program)); + else + LogMsg(logMsgError.arg(torrent->name(), program)); #else // Q_OS_WIN QStringList args = Utils::String::splitCommand(programTemplate); @@ -514,11 +517,21 @@ void Application::runExternalProgram(const QString &programTemplate, const BitTo arg = replaceVariables(arg); } - // show intended command in log - LogMsg(logMsg.arg(torrent->name(), replaceVariables(programTemplate))); - const QString command = args.takeFirst(); - QProcess::startDetached(command, args); + QProcess proc; + proc.setProgram(command); + proc.setArguments(args); + + if (proc.startDetached()) + { + // show intended command in log + LogMsg(logMsg.arg(torrent->name(), replaceVariables(programTemplate))); + } + else + { + // show intended command in log + LogMsg(logMsgError.arg(torrent->name(), replaceVariables(programTemplate))); + } #endif } @@ -732,7 +745,7 @@ try actionExit->setIcon(UIThemeManager::instance()->getIcon(u"application-exit"_qs)); actionExit->setMenuRole(QAction::QuitRole); actionExit->setShortcut(Qt::CTRL | Qt::Key_Q); - connect(actionExit, &QAction::triggered, this, [this]() + connect(actionExit, &QAction::triggered, this, [] { QApplication::exit(); }); diff --git a/src/base/bittorrent/nativesessionextension.cpp b/src/base/bittorrent/nativesessionextension.cpp index 248397cf9..7db339f3a 100644 --- a/src/base/bittorrent/nativesessionextension.cpp +++ b/src/base/bittorrent/nativesessionextension.cpp @@ -35,26 +35,6 @@ namespace { - void handleAddTorrentAlert([[maybe_unused]] const lt::add_torrent_alert *alert) - { -#ifndef QBT_USES_LIBTORRENT2 - if (alert->error) - return; - - // libtorrent < 2.0.7 has a bug that add_torrent_alert is posted too early - // (before torrent is fully initialized and torrent extensions are created) - // so we have to fill "extension data" in add_torrent_alert handler - - // NOTE: `data` may not exist if a torrent is added behind the scenes to download metadata - auto *data = static_cast(alert->params.userdata); - if (data) - { - data->status = alert->handle.status({}); - data->trackers = alert->handle.trackers(); - } -#endif - } - void handleFastresumeRejectedAlert(const lt::fastresume_rejected_alert *alert) { alert->handle.unset_flags(lt::torrent_flags::auto_managed); @@ -76,9 +56,6 @@ void NativeSessionExtension::on_alert(const lt::alert *alert) { switch (alert->type()) { - case lt::add_torrent_alert::alert_type: - handleAddTorrentAlert(static_cast(alert)); - break; case lt::fastresume_rejected_alert::alert_type: handleFastresumeRejectedAlert(static_cast(alert)); break; diff --git a/src/base/bittorrent/nativetorrentextension.cpp b/src/base/bittorrent/nativetorrentextension.cpp index 1f8ab1a1e..0fe5838b1 100644 --- a/src/base/bittorrent/nativetorrentextension.cpp +++ b/src/base/bittorrent/nativetorrentextension.cpp @@ -36,18 +36,11 @@ NativeTorrentExtension::NativeTorrentExtension(const lt::torrent_handle &torrent { // NOTE: `data` may not exist if a torrent is added behind the scenes to download metadata -#ifdef QBT_USES_LIBTORRENT2 - // libtorrent < 2.0.7 has a bug that add_torrent_alert is posted too early - // (before torrent is fully initialized and torrent extensions are created) - // so we have to fill "extension data" in add_torrent_alert handler and - // we have it already filled at this point - if (m_data) { - m_data->status = m_torrentHandle.status({}); + m_data->status = m_torrentHandle.status(); m_data->trackers = m_torrentHandle.trackers(); } -#endif on_state(m_data ? m_data->status.state : m_torrentHandle.status({}).state); } diff --git a/src/base/bittorrent/sessionimpl.cpp b/src/base/bittorrent/sessionimpl.cpp index 832d39368..944322d90 100644 --- a/src/base/bittorrent/sessionimpl.cpp +++ b/src/base/bittorrent/sessionimpl.cpp @@ -1386,27 +1386,29 @@ void SessionImpl::endStartup(ResumeSessionContext *context) } context->deleteLater(); - - m_nativeSession->resume(); - if (m_refreshEnqueued) - m_refreshEnqueued = false; - else - enqueueRefresh(); - - m_statisticsLastUpdateTimer.start(); - - // Regular saving of fastresume data - connect(m_resumeDataTimer, &QTimer::timeout, this, &SessionImpl::generateResumeData); - const int saveInterval = saveResumeDataInterval(); - if (saveInterval > 0) + connect(context, &QObject::destroyed, this, [this] { - m_resumeDataTimer->setInterval(std::chrono::minutes(saveInterval)); - m_resumeDataTimer->start(); - } + m_nativeSession->resume(); + if (m_refreshEnqueued) + m_refreshEnqueued = false; + else + enqueueRefresh(); - m_isRestored = true; - emit startupProgressUpdated(100); - emit restored(); + m_statisticsLastUpdateTimer.start(); + + // Regular saving of fastresume data + connect(m_resumeDataTimer, &QTimer::timeout, this, &SessionImpl::generateResumeData); + const int saveInterval = saveResumeDataInterval(); + if (saveInterval > 0) + { + m_resumeDataTimer->setInterval(std::chrono::minutes(saveInterval)); + m_resumeDataTimer->start(); + } + + m_isRestored = true; + emit startupProgressUpdated(100); + emit restored(); + }); } void SessionImpl::initializeNativeSession() diff --git a/src/base/bittorrent/torrentimpl.cpp b/src/base/bittorrent/torrentimpl.cpp index 1f3dd2ef4..01e7f528d 100644 --- a/src/base/bittorrent/torrentimpl.cpp +++ b/src/base/bittorrent/torrentimpl.cpp @@ -734,7 +734,13 @@ qreal TorrentImpl::progress() const return 1.; const qreal progress = static_cast(m_nativeStatus.total_wanted_done) / m_nativeStatus.total_wanted; - Q_ASSERT((progress >= 0.f) && (progress <= 1.f)); + if ((progress < 0.f) || (progress > 1.f)) + { + LogMsg(tr("Unexpected data detected. Torrent: %1. Data: total_wanted=%2 total_wanted_done=%3.") + .arg(name(), QString::number(m_nativeStatus.total_wanted), QString::number(m_nativeStatus.total_wanted_done)) + , Log::WARNING); + } + return progress; }