| 1 | // For license of this file, see <project-root-folder>/LICENSE.md. |
| 2 | |
| 3 | #include "src/gui/gmailaccountdetails.h" |
| 4 | |
| 5 | #include "src/definitions.h" |
| 6 | #include "src/gmailnetworkfactory.h" |
| 7 | |
| 8 | #include <librssguard/exceptions/applicationexception.h> |
| 9 | #include <librssguard/miscellaneous/application.h> |
| 10 | #include <librssguard/network-web/oauth2service.h> |
| 11 | #include <librssguard/network-web/webfactory.h> |
| 12 | |
| 13 | GmailAccountDetails::GmailAccountDetails(QWidget* parent) : QWidget(parent), m_oauth(nullptr), m_lastProxy({}) { |
| 14 | m_ui.setupUi(this); |
| 15 | |
| 16 | #if defined(GMAIL_OFFICIAL_SUPPORT) |
| 17 | m_ui.m_lblInfo->setHelpText(tr("There are some preconfigured OAuth tokens so you do not have to fill in your " |
| 18 | "client ID/secret, but it is strongly recommended to obtain your " |
| 19 | "own as it preconfigured tokens have limited global usage quota. If you wish " |
| 20 | "to use preconfigured tokens, simply leave those fields empty and make sure " |
| 21 | "to leave default value of redirect URL."), |
| 22 | false); |
| 23 | #else |
| 24 | m_ui.m_lblInfo->setHelpText(tr("You have to fill in your client ID/secret and also fill in correct redirect URL."), |
| 25 | true); |
| 26 | #endif |
| 27 | |
| 28 | m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Information, |
| 29 | tr("Not tested yet."), |
| 30 | tr("Not tested yet.")); |
| 31 | m_ui.m_lblTestResult->label()->setWordWrap(true); |
| 32 | m_ui.m_txtUsername->lineEdit()->setPlaceholderText(tr("User-visible username")); |
| 33 | |
| 34 | setTabOrder(m_ui.m_txtUsername->lineEdit(), m_ui.m_txtAppId); |
| 35 | setTabOrder(m_ui.m_txtAppId, m_ui.m_txtAppKey); |
| 36 | setTabOrder(m_ui.m_txtAppKey, m_ui.m_txtRedirectUrl); |
| 37 | setTabOrder(m_ui.m_txtRedirectUrl, m_ui.m_spinLimitMessages); |
| 38 | setTabOrder(m_ui.m_spinLimitMessages, m_ui.m_btnTestSetup); |
| 39 | |
| 40 | connect(m_ui.m_txtAppId->lineEdit(), &BaseLineEdit::textChanged, this, &GmailAccountDetails::checkOAuthValue); |
| 41 | connect(m_ui.m_txtAppKey->lineEdit(), &BaseLineEdit::textChanged, this, &GmailAccountDetails::checkOAuthValue); |
| 42 | connect(m_ui.m_txtRedirectUrl->lineEdit(), &BaseLineEdit::textChanged, this, &GmailAccountDetails::checkOAuthValue); |
| 43 | connect(m_ui.m_txtUsername->lineEdit(), &BaseLineEdit::textChanged, this, &GmailAccountDetails::checkUsername); |
| 44 | connect(m_ui.m_btnRegisterApi, &QPushButton::clicked, this, &GmailAccountDetails::registerApi); |
| 45 | |
| 46 | emit m_ui.m_txtUsername->lineEdit()->textChanged(m_ui.m_txtUsername->lineEdit()->text()); |
| 47 | emit m_ui.m_txtAppId->lineEdit()->textChanged(m_ui.m_txtAppId->lineEdit()->text()); |
| 48 | emit m_ui.m_txtAppKey->lineEdit()->textChanged(m_ui.m_txtAppKey->lineEdit()->text()); |
| 49 | emit m_ui.m_txtRedirectUrl->lineEdit()->textChanged(m_ui.m_txtAppKey->lineEdit()->text()); |
| 50 | |
| 51 | hookNetwork(); |
| 52 | } |
| 53 | |
| 54 | void GmailAccountDetails::testSetup(const QNetworkProxy& custom_proxy) { |
| 55 | m_oauth->logout(true); |
| 56 | m_oauth->setClientId(m_ui.m_txtAppId->lineEdit()->text()); |
| 57 | m_oauth->setClientSecret(m_ui.m_txtAppKey->lineEdit()->text()); |
| 58 | m_oauth->setRedirectUrl(m_ui.m_txtRedirectUrl->lineEdit()->text(), true); |
| 59 | |
| 60 | m_lastProxy = custom_proxy; |
| 61 | m_oauth->login(); |
| 62 | } |
| 63 | |
| 64 | void GmailAccountDetails::checkUsername(const QString& username) { |
| 65 | if (username.isEmpty()) { |
| 66 | m_ui.m_txtUsername->setStatus(WidgetWithStatus::StatusType::Error, tr("No username entered.")); |
| 67 | } |
| 68 | else { |
| 69 | m_ui.m_txtUsername->setStatus(WidgetWithStatus::StatusType::Ok, tr("Some username entered.")); |
| 70 | } |
| 71 | } |
| 72 | |
| 73 | void GmailAccountDetails::onAuthFailed() { |
| 74 | m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Error, |
| 75 | tr("You did not grant access."), |
| 76 | tr("There was error during testing.")); |
| 77 | } |
| 78 | |
| 79 | void GmailAccountDetails::onAuthError(const QString& error, const QString& detailed_description) { |
| 80 | Q_UNUSED(error) |
| 81 | |
| 82 | m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Error, |
| 83 | tr("There is error: %1").arg(detailed_description), |
| 84 | tr("There was error during testing.")); |
| 85 | } |
| 86 | |
| 87 | void GmailAccountDetails::onAuthGranted() { |
| 88 | m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Ok, |
| 89 | tr("Tested successfully. You may be prompted to login once more."), |
| 90 | tr("Your access was approved.")); |
| 91 | |
| 92 | try { |
| 93 | GmailNetworkFactory fac; |
| 94 | |
| 95 | fac.setOauth(m_oauth); |
| 96 | |
| 97 | auto resp = fac.getProfile(m_lastProxy); |
| 98 | |
| 99 | m_ui.m_txtUsername->lineEdit()->setText(resp[QSL("emailAddress")].toString()); |
| 100 | } |
| 101 | catch (const ApplicationException& ex) { |
| 102 | qCriticalNN << LOGSEC_GMAIL << "Failed to obtain profile with error:" << QUOTE_W_SPACE_DOT(ex.message()); |
| 103 | } |
| 104 | } |
| 105 | |
| 106 | void GmailAccountDetails::hookNetwork() { |
| 107 | connect(m_oauth, &OAuth2Service::tokensRetrieved, this, &GmailAccountDetails::onAuthGranted); |
| 108 | connect(m_oauth, &OAuth2Service::tokensRetrieveError, this, &GmailAccountDetails::onAuthError); |
| 109 | connect(m_oauth, &OAuth2Service::authFailed, this, &GmailAccountDetails::onAuthFailed); |
| 110 | } |
| 111 | |
| 112 | void GmailAccountDetails::registerApi() { |
| 113 | qApp->web()->openUrlInExternalBrowser(QSL(GMAIL_REG_API_URL)); |
| 114 | } |
| 115 | |
| 116 | void GmailAccountDetails::checkOAuthValue(const QString& value) { |
| 117 | auto* line_edit = qobject_cast<LineEditWithStatus*>(sender()->parent()); |
| 118 | |
| 119 | if (line_edit != nullptr) { |
| 120 | if (value.isEmpty()) { |
| 121 | #if defined(GMAIL_OFFICIAL_SUPPORT) |
| 122 | line_edit->setStatus(WidgetWithStatus::StatusType::Ok, tr("Preconfigured client ID/secret will be used.")); |
| 123 | #else |
| 124 | line_edit->setStatus(WidgetWithStatus::StatusType::Error, tr("Empty value is entered.")); |
| 125 | #endif |
| 126 | } |
| 127 | else { |
| 128 | line_edit->setStatus(WidgetWithStatus::StatusType::Ok, tr("Some value is entered.")); |
| 129 | } |
| 130 | } |
| 131 | } |