Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/ui/screens/addtorrent/addtorrentdialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -420,7 +420,7 @@ namespace tremotesf {
info().log("Parsed, result = {}", magnetLink);
auto* const torrent = mRpc->torrentByHash(magnetLink.infoHashV1);
if (torrent) {
askForMergingTrackers(torrent, std::move(magnetLink.trackers), parentWidget());
createAskForMergingTrackersDialog(torrent, std::move(magnetLink.trackers), parentWidget())->show();
return true;
}
} catch (const std::runtime_error& e) {
Expand All @@ -438,7 +438,7 @@ namespace tremotesf {
auto& [infoHashV1, trackers] = *mTorrentFileInfoHashAndTrackers;
auto* const torrent = mRpc->torrentByHash(infoHashV1);
if (torrent) {
askForMergingTrackers(torrent, std::move(trackers), parentWidget());
createAskForMergingTrackersDialog(torrent, std::move(trackers), parentWidget())->show();
return true;
}
return false;
Expand Down
4 changes: 2 additions & 2 deletions src/ui/screens/addtorrent/addtorrenthelpers.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,8 @@ namespace tremotesf {
};
}

QDialog* askForMergingTrackers(Torrent* torrent, std::vector<std::set<QString>> trackers, QWidget* parent) {
QDialog*
createAskForMergingTrackersDialog(Torrent* torrent, std::vector<std::set<QString>> trackers, QWidget* parent) {
auto* const settings = Settings::instance();
QMessageBox* messageBox{};
if (settings->get_askForMergingTrackersWhenAddingExistingTorrent()) {
Expand Down Expand Up @@ -95,7 +96,6 @@ namespace tremotesf {
}
messageBox->setAttribute(Qt::WA_DeleteOnClose);
messageBox->setModal(false);
messageBox->show();
return messageBox;
}

Expand Down
3 changes: 2 additions & 1 deletion src/ui/screens/addtorrent/addtorrenthelpers.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ namespace tremotesf {
AddTorrentParameters getAddTorrentParameters(Rpc* rpc);
AddTorrentParameters getInitialAddTorrentParameters(Rpc* rpc);

QDialog* askForMergingTrackers(Torrent* torrent, std::vector<std::set<QString>> trackers, QWidget* parent);
QDialog*
createAskForMergingTrackersDialog(Torrent* torrent, std::vector<std::set<QString>> trackers, QWidget* parent);

QString bencodeErrorString(bencode::Error::Type type);
}
Expand Down
111 changes: 35 additions & 76 deletions src/ui/screens/mainwindow/mainwindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -171,50 +171,32 @@ namespace tremotesf {
window->raise();
}

#ifdef TREMOTESF_UNIX_FREEDESKTOP
void activeWindowOnX11(QWidget* window, const std::optional<QByteArray>& startupNotificationId) {
if (startupNotificationId.has_value()) {
info().log("Removing startup notification with id {}", *startupNotificationId);
KStartupInfo::setNewStartupId(window->windowHandle(), *startupNotificationId);
KStartupInfo::appStarted(*startupNotificationId);
}
window->activateWindow();
}

void activeWindowOnWayland(
[[maybe_unused]] QWidget* window, [[maybe_unused]] const std::optional<QByteArray>& xdgActivationToken
) {
if (xdgActivationToken.has_value()) {
info().log("Activating window with token {}", *xdgActivationToken);
// Qt gets new token from XDG_ACTIVATION_TOKEN environment variable
// It we be read and unset in QWidget::activateWindow() call below
qputenv(xdgActivationTokenEnvVariable, *xdgActivationToken);
}
window->activateWindow();
}
#endif

void activateWindowCompat(
QWidget* window, [[maybe_unused]] const std::optional<QByteArray>& windowActivationToken = {}
void consumeActivationToken(
[[maybe_unused]] QWidget* window, [[maybe_unused]] std::optional<QByteArray>& windowActivationToken
) {
info().log("Activating window {}", *window);
#ifdef TREMOTESF_UNIX_FREEDESKTOP
if (!windowActivationToken.has_value()) {
return;
}
switch (KWindowSystem::platform()) {
case KWindowSystem::Platform::X11:
debug().log("Windowing system is X11");
activeWindowOnX11(window, windowActivationToken);
info().log("Removing startup notification with id {}", *windowActivationToken);
KStartupInfo::setNewStartupId(window->windowHandle(), *windowActivationToken);
KStartupInfo::appStarted(*windowActivationToken);
break;
case KWindowSystem::Platform::Wayland:
debug().log("Windowing system is Wayland");
activeWindowOnWayland(window, windowActivationToken);
info().log("Activating window with token {}", *windowActivationToken);
// Qt gets new token from XDG_ACTIVATION_TOKEN environment variable
// It wll be read and unset in QWidget::activateWindow() or QWidget::show()
qputenv(xdgActivationTokenEnvVariable, *windowActivationToken);
break;
default:
warning().log("Unknown windowing system");
window->activateWindow();
break;
}
#else
window->activateWindow();
windowActivationToken.reset();
#endif
}
}
Expand Down Expand Up @@ -1024,7 +1006,7 @@ namespace tremotesf {
const auto existingDialog = mTorrentPropertiesDialogs.find(hashString);
if (existingDialog != mTorrentPropertiesDialogs.end()) {
unminimizeAndRaiseWindow(existingDialog->second);
activateWindowCompat(existingDialog->second);
existingDialog->second->activateWindow();
} else {
auto dialog = new TorrentPropertiesDialog(torrent, mViewModel.rpc(), mWindow);
dialog->setAttribute(Qt::WA_DeleteOnClose);
Expand Down Expand Up @@ -1176,7 +1158,7 @@ namespace tremotesf {
auto existingDialog = mWindow->findChild<Dialog*>({}, Qt::FindDirectChildrenOnly);
if (existingDialog) {
unminimizeAndRaiseWindow(existingDialog);
activateWindowCompat(existingDialog);
existingDialog->activateWindow();
} else {
auto dialog = createDialog();
dialog->setAttribute(Qt::WA_DeleteOnClose);
Expand Down Expand Up @@ -1493,33 +1475,21 @@ namespace tremotesf {
} else {
info().log("NSApp is not hidden, activating main window");
unminimizeAndRaiseWindow(mWindow);
activateWindowCompat(mWindow);
mWindow->activateWindow();
}
#else
if (!mWindow->isHidden()) {
info().log("Main window is not hidden, activating it");
unminimizeAndRaiseWindow(mWindow);
activateWindowCompat(mWindow, windowActivationToken);
consumeActivationToken(mWindow, windowActivationToken);
mWindow->activateWindow();
return;
}

# ifdef TREMOTESF_UNIX_FREEDESKTOP
// With Wayland we need to set XDG_ACTIVATION_TOKEN environment variable before show()
// so that Qt handles activation automatically
if (windowActivationToken.has_value() && KWindowSystem::isPlatformWayland()) {
info().log("Showing window with token {}", *windowActivationToken);
// Qt gets new token from XDG_ACTIVATION_TOKEN environment variable
// It we be read and unset in QWidget::show() call below
qputenv(xdgActivationTokenEnvVariable, *windowActivationToken);
windowActivationToken.reset();
}
# endif
consumeActivationToken(mWindow, windowActivationToken);
info().log("Showing window {}", *mWindow);
mWindow->show();
unminimizeAndRaiseWindow(mWindow);
if (windowActivationToken.has_value()) {
activateWindowCompat(mWindow, windowActivationToken);
}
for (const auto& window : mOtherWindowsHiddenByUs) {
if (window) {
info().log("Showing window {}", *window);
Expand Down Expand Up @@ -1602,38 +1572,32 @@ namespace tremotesf {
void showAddTorrentFileDialogs(const QStringList& files, std::optional<QByteArray> windowActivationToken) {
const bool setParent = Settings::instance()->get_showMainWindowWhenAddingTorrent();
for (const QString& filePath : files) {
auto* const dialog = showAddTorrentFileDialog(filePath, setParent);
if (windowActivationToken.has_value()) {
activateWindowCompat(dialog, windowActivationToken);
// Can use token only once
windowActivationToken.reset();
}
showAddTorrentFileDialog(filePath, setParent, windowActivationToken);
}
}

QDialog* showAddTorrentFileDialog(const QString& filePath, bool setParent) {
void showAddTorrentFileDialog(
const QString& filePath, bool setParent, std::optional<QByteArray>& windowActivationToken
) {
auto* const dialog = new AddTorrentDialog(
mViewModel.rpc(),
AddTorrentDialog::FileParams{filePath},
setParent ? mWindow : nullptr
);
dialog->setAttribute(Qt::WA_DeleteOnClose);
consumeActivationToken(dialog, windowActivationToken);
dialog->show();
return dialog;
}

void
showAddTorrentLinksDialog(const QStringList& urls, const std::optional<QByteArray>& windowActivationToken) {
void showAddTorrentLinksDialog(const QStringList& urls, std::optional<QByteArray>& windowActivationToken) {
auto* const dialog = new AddTorrentDialog(
mViewModel.rpc(),
AddTorrentDialog::UrlParams{urls},
Settings::instance()->get_showMainWindowWhenAddingTorrent() ? mWindow : nullptr
);
dialog->setAttribute(Qt::WA_DeleteOnClose);
consumeActivationToken(dialog, windowActivationToken);
dialog->show();
if (windowActivationToken.has_value()) {
activateWindowCompat(dialog, windowActivationToken);
}
}

void askForMergingTrackers(
Expand All @@ -1642,13 +1606,13 @@ namespace tremotesf {
) {
const bool setParent = Settings::instance()->get_showMainWindowWhenAddingTorrent();
for (auto& [torrent, trackers] : existingTorrents) {
auto* const dialog =
tremotesf::askForMergingTrackers(torrent, std::move(trackers), setParent ? mWindow : nullptr);
if (windowActivationToken.has_value()) {
activateWindowCompat(dialog, windowActivationToken);
// Can use token only once
windowActivationToken.reset();
}
auto* const dialog = tremotesf::createAskForMergingTrackersDialog(
torrent,
std::move(trackers),
setParent ? mWindow : nullptr
);
consumeActivationToken(dialog, windowActivationToken);
dialog->show();
}
}

Expand All @@ -1663,7 +1627,6 @@ namespace tremotesf {
dialog->setAttribute(Qt::WA_DeleteOnClose, true);
dialog->setModal(false);
dialog->show();
activateWindowCompat(dialog);
};

QObject::connect(mViewModel.rpc(), &Rpc::torrentAddDuplicate, this, [=] {
Expand All @@ -1681,9 +1644,7 @@ namespace tremotesf {
});
}

void showDelayedTorrentAddDialog(
const QStringList& torrents, const std::optional<QByteArray>& windowActivationToken
) {
void showDelayedTorrentAddDialog(const QStringList& torrents, std::optional<QByteArray> windowActivationToken) {
debug().log("MainWindow: showing delayed torrent add dialog");
const auto dialog = new QMessageBox(
QMessageBox::Information,
Expand All @@ -1702,10 +1663,8 @@ namespace tremotesf {
detailedText += '\n';
}
dialog->setDetailedText(detailedText);
consumeActivationToken(dialog, windowActivationToken);
dialog->show();
if (windowActivationToken.has_value()) {
activateWindowCompat(dialog, windowActivationToken);
}
QObject::connect(mViewModel.rpc(), &Rpc::connectedChanged, dialog, [=, this] {
if (mViewModel.rpc()->isConnected()) dialog->close();
});
Expand Down
Loading