From 3023a44a216afe79b9fd3274b4f82392ca166e6b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pawe=C5=82?= Date: Sat, 6 Feb 2021 15:47:09 +0100 Subject: [PATCH] Some fixes related to clipboard (#2405) Also: - Fixed capitalization of one method in `LoginDialog.cpp` - Visually improved some strings in `LoginDialog.cpp` - Added utility function getting clipboard content to `Clipboard.cpp` - Made sure to use `crossPlatformCopy` in `UserInfoPopup.cpp` --- src/util/Clipboard.cpp | 7 +++++ src/util/Clipboard.hpp | 2 ++ src/widgets/dialogs/LoginDialog.cpp | 43 ++++++++++++--------------- src/widgets/dialogs/UserInfoPopup.cpp | 5 ++-- 4 files changed, 31 insertions(+), 26 deletions(-) diff --git a/src/util/Clipboard.cpp b/src/util/Clipboard.cpp index c71014476..255ae14f0 100644 --- a/src/util/Clipboard.cpp +++ b/src/util/Clipboard.cpp @@ -6,11 +6,18 @@ namespace chatterino { void crossPlatformCopy(const QString &text) { auto clipboard = QApplication::clipboard(); + clipboard->setText(text); + if (clipboard->supportsSelection()) { clipboard->setText(text, QClipboard::Selection); } } +QString getClipboardText() +{ + return QApplication::clipboard()->text(); +} + } // namespace chatterino diff --git a/src/util/Clipboard.hpp b/src/util/Clipboard.hpp index ff23a1f2d..52c083aa6 100644 --- a/src/util/Clipboard.hpp +++ b/src/util/Clipboard.hpp @@ -6,4 +6,6 @@ namespace chatterino { void crossPlatformCopy(const QString &text); +QString getClipboardText(); + } // namespace chatterino diff --git a/src/widgets/dialogs/LoginDialog.cpp b/src/widgets/dialogs/LoginDialog.cpp index cab2c966d..121e79560 100644 --- a/src/widgets/dialogs/LoginDialog.cpp +++ b/src/widgets/dialogs/LoginDialog.cpp @@ -5,6 +5,7 @@ #include "common/NetworkRequest.hpp" #include "common/QLogging.hpp" #include "controllers/accounts/AccountController.hpp" +#include "util/Clipboard.hpp" #include "util/Helpers.hpp" #ifdef USEWINSDK @@ -22,7 +23,7 @@ namespace chatterino { namespace { - void LogInWithCredentials(const QString &userID, const QString &username, + void logInWithCredentials(const QString &userID, const QString &username, const QString &clientID, const QString &oauthToken) { @@ -57,16 +58,12 @@ namespace { messageBox.setWindowTitle( "Chatterino - invalid account credentials"); messageBox.setIcon(QMessageBox::Critical); - messageBox.setText(errors.join("
")); + messageBox.setText(errors.join("
")); messageBox.setStandardButtons(QMessageBox::Ok); messageBox.exec(); return; } - // QMessageBox messageBox; - // messageBox.setIcon(QMessageBox::Information); - // messageBox.setText("Successfully logged in with user " + - // qS(username) + "!"); std::string basePath = "/accounts/uid" + userID.toStdString(); pajlada::Settings::Setting::set(basePath + "/username", username); @@ -77,9 +74,6 @@ namespace { oauthToken); getApp()->accounts->twitch.reloadUsers(); - - // messageBox.exec(); - getApp()->accounts->twitch.currentUsername = username; } @@ -97,9 +91,10 @@ BasicLoginWidget::BasicLoginWidget() this->ui_.unableToOpenBrowserHelper.setWordWrap(true); this->ui_.unableToOpenBrowserHelper.hide(); this->ui_.unableToOpenBrowserHelper.setText( - "An error occurred while attempting to open the log in link (" + logInLink + ") " + - " - open it manually in your browser and proceed from there."); + QString("An error occurred while attempting to open the " + "log in link (%1) - open it manually in your browser and " + "proceed from there.") + .arg(logInLink)); this->ui_.unableToOpenBrowserHelper.setOpenExternalLinks(true); this->ui_.horizontalLayout.addWidget(&this->ui_.loginButton); @@ -110,8 +105,7 @@ BasicLoginWidget::BasicLoginWidget() connect(&this->ui_.loginButton, &QPushButton::clicked, [this, logInLink]() { qCDebug(chatterinoWidget) << "open login in browser"; - auto res = QDesktopServices::openUrl(QUrl(logInLink)); - if (!res) + if (!QDesktopServices::openUrl(QUrl(logInLink))) { qCWarning(chatterinoWidget) << "open login in browser failed"; this->ui_.unableToOpenBrowserHelper.show(); @@ -119,10 +113,7 @@ BasicLoginWidget::BasicLoginWidget() }); connect(&this->ui_.pasteCodeButton, &QPushButton::clicked, [this]() { - QClipboard *clipboard = QGuiApplication::clipboard(); - QString clipboardString = clipboard->text(); - QStringList parameters = clipboardString.split(';'); - + QStringList parameters = getClipboardText().split(";"); QString oauthToken, clientID, username, userID; for (const auto ¶m : parameters) @@ -157,9 +148,10 @@ BasicLoginWidget::BasicLoginWidget() } } - LogInWithCredentials(userID, username, clientID, oauthToken); + logInWithCredentials(userID, username, clientID, oauthToken); - clipboard->clear(); + // Removing clipboard content to prevent accidental paste of credentials into somewhere + crossPlatformCopy(""); this->window()->close(); }); } @@ -169,9 +161,12 @@ AdvancedLoginWidget::AdvancedLoginWidget() this->setLayout(&this->ui_.layout); this->ui_.instructionsLabel.setText( - "1. Fill in your username\n2. Fill in your user ID or press " - "the 'Get user ID from username' button\n3. Fill in your " - "Client ID\n4. Fill in your OAuth Token\n5. Press Add User"); + "1. Fill in your username" + "\n2. Fill in your user ID or press the 'Get user ID from username' " + "button" + "\n3. Fill in your Client ID" + "\n4. Fill in your OAuth Token" + "\n5. Press Add User"); this->ui_.instructionsLabel.setWordWrap(true); this->ui_.layout.addWidget(&this->ui_.instructionsLabel); @@ -226,7 +221,7 @@ AdvancedLoginWidget::AdvancedLoginWidget() QString clientID = this->ui_.clientIDInput.text(); QString oauthToken = this->ui_.oauthTokenInput.text(); - LogInWithCredentials(userID, username, clientID, oauthToken); + logInWithCredentials(userID, username, clientID, oauthToken); }); } diff --git a/src/widgets/dialogs/UserInfoPopup.cpp b/src/widgets/dialogs/UserInfoPopup.cpp index 92cf7cf5e..f8785b313 100644 --- a/src/widgets/dialogs/UserInfoPopup.cpp +++ b/src/widgets/dialogs/UserInfoPopup.cpp @@ -12,6 +12,7 @@ #include "providers/twitch/api/Kraken.hpp" #include "singletons/Resources.hpp" #include "singletons/Settings.hpp" +#include "util/Clipboard.hpp" #include "util/LayoutCreator.hpp" #include "util/PostToThread.hpp" #include "util/Shortcut.hpp" @@ -47,8 +48,8 @@ namespace { [label = label.getElement()] { auto copyText = label->property("copy-text").toString(); - qApp->clipboard()->setText(copyText.isEmpty() ? label->getText() - : copyText); + crossPlatformCopy(copyText.isEmpty() ? label->getText() + : copyText); }); return label.getElement();