From d395f9d36cb6b3bfec9da6cb6c68c3b2d06f145a Mon Sep 17 00:00:00 2001 From: Martin Rotter Date: Mon, 12 Apr 2021 10:23:31 +0200 Subject: [PATCH] gmail now fills in address when test setup is made, also date/time headers of gmails articles are more correctly parsed --- .../desktop/com.github.rssguard.appdata.xml | 2 +- resources/scripts/7za | 2 +- src/librssguard/core/feedsmodel.cpp | 2 +- src/librssguard/miscellaneous/textfactory.cpp | 2 +- .../services/feedly/feedlyentrypoint.cpp | 4 +-- src/librssguard/services/gmail/definitions.h | 1 + .../services/gmail/gmailnetworkfactory.cpp | 36 +++++++++++++++++++ .../services/gmail/gmailnetworkfactory.h | 1 + .../gmail/gui/formeditgmailaccount.cpp | 3 ++ .../gmail/gui/gmailaccountdetails.cpp | 23 ++++++++++-- .../services/gmail/gui/gmailaccountdetails.h | 7 +++- 11 files changed, 73 insertions(+), 10 deletions(-) diff --git a/resources/desktop/com.github.rssguard.appdata.xml b/resources/desktop/com.github.rssguard.appdata.xml index 313fc1928..72916c17e 100644 --- a/resources/desktop/com.github.rssguard.appdata.xml +++ b/resources/desktop/com.github.rssguard.appdata.xml @@ -30,7 +30,7 @@ https://martinrotter.github.io/donate/ - + none diff --git a/resources/scripts/7za b/resources/scripts/7za index 9c10723bf..47f412575 160000 --- a/resources/scripts/7za +++ b/resources/scripts/7za @@ -1 +1 @@ -Subproject commit 9c10723bfbaf6cb85107d6ee16e0324e9e487749 +Subproject commit 47f4125753452eff8800dbd6600c5a05540b15d9 diff --git a/src/librssguard/core/feedsmodel.cpp b/src/librssguard/core/feedsmodel.cpp index becd66971..b3f1c27a6 100644 --- a/src/librssguard/core/feedsmodel.cpp +++ b/src/librssguard/core/feedsmodel.cpp @@ -538,7 +538,7 @@ void FeedsModel::loadActivatedServiceAccounts() { } if (serviceRoots().isEmpty()) { - QTimer::singleShot(3000, qApp->mainForm(), []() { + QTimer::singleShot(2000, qApp->mainForm(), []() { qApp->mainForm()->showAddAccountDialog(); }); } diff --git a/src/librssguard/miscellaneous/textfactory.cpp b/src/librssguard/miscellaneous/textfactory.cpp index 1a80343f7..86ad79785 100755 --- a/src/librssguard/miscellaneous/textfactory.cpp +++ b/src/librssguard/miscellaneous/textfactory.cpp @@ -79,7 +79,7 @@ QDateTime TextFactory::parseDateTime(const QString& date_time) { QSL("MMM d yyyy hh:mm:ss") << QSL("ddd, dd MMM yyyy HH:mm:ss") << QSL("ddd, d MMM yyyy HH:mm:ss") << QSL("dd MMM yyyy") << QSL("yyyy-MM-dd HH:mm:ss.z") << QSL("yyyy-MM-dd") << QSL("yyyy") << QSL("yyyy-MM") << QSL("yyyy-MM-dd") << QSL("yyyy-MM-ddThh:mm") << - QSL("yyyy-MM-ddThh:mm:ss"); + QSL("yyyy-MM-ddThh:mm:ss") << QSL("d MMM yyyy HH:mm:ss"); QStringList timezone_offset_patterns; timezone_offset_patterns << QSL("+hh:mm") << QSL("-hh:mm") << QSL("+hhmm") diff --git a/src/librssguard/services/feedly/feedlyentrypoint.cpp b/src/librssguard/services/feedly/feedlyentrypoint.cpp index 543c09791..3a672fc1d 100755 --- a/src/librssguard/services/feedly/feedlyentrypoint.cpp +++ b/src/librssguard/services/feedly/feedlyentrypoint.cpp @@ -2,9 +2,9 @@ #include "services/feedly/feedlyentrypoint.h" +#include "database/databasequeries.h" #include "definitions/definitions.h" #include "miscellaneous/application.h" -#include "database/databasequeries.h" #include "miscellaneous/iconfactory.h" #include "services/feedly/definitions.h" #include "services/feedly/feedlyserviceroot.h" @@ -31,7 +31,7 @@ QString FeedlyEntryPoint::code() const { } QString FeedlyEntryPoint::description() const { - return QObject::tr("Keep up with the topics and trends you care about, without the overwhelm. " + return QObject::tr("Keep up with the topics and trends you care about, without the overwhelm.\n\n" "Feedly is a secure space where you can privately organize and research the " "topics and trends that matter to you."); } diff --git a/src/librssguard/services/gmail/definitions.h b/src/librssguard/services/gmail/definitions.h index 49782611a..ca40c267f 100644 --- a/src/librssguard/services/gmail/definitions.h +++ b/src/librssguard/services/gmail/definitions.h @@ -12,6 +12,7 @@ #define GMAIL_API_SEND_MESSAGE "https://www.googleapis.com/upload/gmail/v1/users/me/messages/send?uploadType=media" #define GMAIL_API_BATCH_UPD_LABELS "https://www.googleapis.com/gmail/v1/users/me/messages/batchModify" +#define GMAIL_API_GET_PROFILE "https://gmail.googleapis.com/gmail/v1/users/me/profile" #define GMAIL_API_GET_ATTACHMENT "https://www.googleapis.com/gmail/v1/users/me/messages/%1/attachments/%2" #define GMAIL_API_LABELS_LIST "https://www.googleapis.com/gmail/v1/users/me/labels" #define GMAIL_API_MSGS_LIST "https://www.googleapis.com/gmail/v1/users/me/messages" diff --git a/src/librssguard/services/gmail/gmailnetworkfactory.cpp b/src/librssguard/services/gmail/gmailnetworkfactory.cpp index 411ccfa82..2f8309a19 100755 --- a/src/librssguard/services/gmail/gmailnetworkfactory.cpp +++ b/src/librssguard/services/gmail/gmailnetworkfactory.cpp @@ -5,6 +5,7 @@ #include "database/databasequeries.h" #include "definitions/definitions.h" #include "exceptions/applicationexception.h" +#include "exceptions/networkexception.h" #include "gui/dialogs/formmain.h" #include "gui/tabwidget.h" #include "miscellaneous/application.h" @@ -380,6 +381,41 @@ QNetworkReply::NetworkError GmailNetworkFactory::markMessagesStarred(RootItem::I return QNetworkReply::NetworkError::NoError; } +QVariantHash GmailNetworkFactory::getProfile(const QNetworkProxy& custom_proxy) { + QString bearer = m_oauth2->bearer().toLocal8Bit(); + + if (bearer.isEmpty()) { + throw ApplicationException(tr("you are not logged in")); + } + + QList> headers; + + headers.append(QPair(QString(HTTP_HEADERS_AUTHORIZATION).toLocal8Bit(), + m_oauth2->bearer().toLocal8Bit())); + + int timeout = qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::UpdateTimeout)).toInt(); + QByteArray output; + auto result = NetworkFactory::performNetworkOperation(GMAIL_API_GET_PROFILE, + timeout, + {}, + output, + QNetworkAccessManager::Operation::GetOperation, + headers, + false, + {}, + {}, + custom_proxy).first; + + if (result != QNetworkReply::NetworkError::NoError) { + throw NetworkException(result, output); + } + else { + QJsonDocument doc = QJsonDocument::fromJson(output); + + return doc.object().toVariantHash(); + } +} + void GmailNetworkFactory::onTokensError(const QString& error, const QString& error_description) { Q_UNUSED(error) diff --git a/src/librssguard/services/gmail/gmailnetworkfactory.h b/src/librssguard/services/gmail/gmailnetworkfactory.h index 90d1e9038..94b2cf743 100755 --- a/src/librssguard/services/gmail/gmailnetworkfactory.h +++ b/src/librssguard/services/gmail/gmailnetworkfactory.h @@ -48,6 +48,7 @@ class GmailNetworkFactory : public QObject { QNetworkReply::NetworkError markMessagesStarred(RootItem::Importance importance, const QStringList& custom_ids, const QNetworkProxy& custom_proxy); + QVariantHash getProfile(const QNetworkProxy& custom_proxy); private slots: void onTokensError(const QString& error, const QString& error_description); diff --git a/src/librssguard/services/gmail/gui/formeditgmailaccount.cpp b/src/librssguard/services/gmail/gui/formeditgmailaccount.cpp index db2d55007..f1e80d8cb 100644 --- a/src/librssguard/services/gmail/gui/formeditgmailaccount.cpp +++ b/src/librssguard/services/gmail/gui/formeditgmailaccount.cpp @@ -17,6 +17,9 @@ FormEditGmailAccount::FormEditGmailAccount(QWidget* parent) activateTab(0); m_details->m_ui.m_txtUsername->setFocus(); + connect(m_details->m_ui.m_btnTestSetup, &QPushButton::clicked, this, [this]() { + m_details->testSetup(m_proxyDetails->proxy()); + }); } void FormEditGmailAccount::apply() { diff --git a/src/librssguard/services/gmail/gui/gmailaccountdetails.cpp b/src/librssguard/services/gmail/gui/gmailaccountdetails.cpp index ea262274b..87a3da4af 100644 --- a/src/librssguard/services/gmail/gui/gmailaccountdetails.cpp +++ b/src/librssguard/services/gmail/gui/gmailaccountdetails.cpp @@ -2,14 +2,16 @@ #include "services/gmail/gui/gmailaccountdetails.h" +#include "exceptions/applicationexception.h" #include "gui/guiutilities.h" #include "miscellaneous/application.h" #include "network-web/oauth2service.h" #include "network-web/webfactory.h" #include "services/gmail/definitions.h" +#include "services/gmail/gmailnetworkfactory.h" GmailAccountDetails::GmailAccountDetails(QWidget* parent) - : QWidget(parent), m_oauth(nullptr) { + : QWidget(parent), m_oauth(nullptr), m_lastProxy({}) { m_ui.setupUi(this); GuiUtilities::setLabelAsNotice(*m_ui.m_lblInfo, true); @@ -40,7 +42,6 @@ GmailAccountDetails::GmailAccountDetails(QWidget* parent) connect(m_ui.m_txtAppKey->lineEdit(), &BaseLineEdit::textChanged, this, &GmailAccountDetails::checkOAuthValue); connect(m_ui.m_txtRedirectUrl->lineEdit(), &BaseLineEdit::textChanged, this, &GmailAccountDetails::checkOAuthValue); connect(m_ui.m_txtUsername->lineEdit(), &BaseLineEdit::textChanged, this, &GmailAccountDetails::checkUsername); - connect(m_ui.m_btnTestSetup, &QPushButton::clicked, this, &GmailAccountDetails::testSetup); connect(m_ui.m_btnRegisterApi, &QPushButton::clicked, this, &GmailAccountDetails::registerApi); emit m_ui.m_txtUsername->lineEdit()->textChanged(m_ui.m_txtUsername->lineEdit()->text()); @@ -51,12 +52,13 @@ GmailAccountDetails::GmailAccountDetails(QWidget* parent) hookNetwork(); } -void GmailAccountDetails::testSetup() { +void GmailAccountDetails::testSetup(const QNetworkProxy& custom_proxy) { m_oauth->logout(); m_oauth->setClientId(m_ui.m_txtAppId->lineEdit()->text()); m_oauth->setClientSecret(m_ui.m_txtAppKey->lineEdit()->text()); m_oauth->setRedirectUrl(m_ui.m_txtRedirectUrl->lineEdit()->text()); + m_lastProxy = custom_proxy; m_oauth->login(); } @@ -87,6 +89,21 @@ void GmailAccountDetails::onAuthGranted() { m_ui.m_lblTestResult->setStatus(WidgetWithStatus::StatusType::Ok, tr("Tested successfully. You may be prompted to login once more."), tr("Your access was approved.")); + + GmailNetworkFactory fac; + + fac.setOauth(m_oauth); + + try { + auto resp = fac.getProfile(m_lastProxy); + + m_ui.m_txtUsername->lineEdit()->setText(resp["emailAddress"].toString()); + } + catch (const ApplicationException& ex) { + qCriticalNN << LOGSEC_GMAIL + << "Failed to obtain profile with error:" + << QUOTE_W_SPACE_DOT(ex.message()); + } } void GmailAccountDetails::hookNetwork() { diff --git a/src/librssguard/services/gmail/gui/gmailaccountdetails.h b/src/librssguard/services/gmail/gui/gmailaccountdetails.h index c99979fd9..1df44046e 100644 --- a/src/librssguard/services/gmail/gui/gmailaccountdetails.h +++ b/src/librssguard/services/gmail/gui/gmailaccountdetails.h @@ -7,6 +7,8 @@ #include "ui_gmailaccountdetails.h" +#include + class OAuth2Service; class GmailAccountDetails : public QWidget { @@ -17,9 +19,11 @@ class GmailAccountDetails : public QWidget { public: explicit GmailAccountDetails(QWidget* parent = nullptr); + public slots: + void testSetup(const QNetworkProxy& custom_proxy); + private slots: void registerApi(); - void testSetup(); void checkOAuthValue(const QString& value); void checkUsername(const QString& username); void onAuthFailed(); @@ -34,6 +38,7 @@ class GmailAccountDetails : public QWidget { // Pointer to live OAuth. OAuth2Service* m_oauth; + QNetworkProxy m_lastProxy; }; #endif // GMAILACCOUNTDETAILS_H