fix gmail behavior when adding it as account a bit

This commit is contained in:
Martin Rotter 2023-12-08 10:42:02 +01:00
parent c1ada2fb21
commit 1d49b038a1
2 changed files with 29 additions and 38 deletions

View File

@ -41,9 +41,7 @@ void OAuthHttpHandler::setListenAddressPort(const QString& full_uri, bool start_
listen_address = QHostAddress(url.host()); listen_address = QHostAddress(url.host());
} }
if (listen_address == m_listenAddress && if (listen_address == m_listenAddress && listen_port == m_listenPort && start_handler == m_httpServer.isListening()) {
listen_port == m_listenPort &&
start_handler == m_httpServer.isListening()) {
// NOTE: We do not need to change listener's settings or re-start it. // NOTE: We do not need to change listener's settings or re-start it.
return; return;
} }
@ -58,26 +56,18 @@ void OAuthHttpHandler::setListenAddressPort(const QString& full_uri, bool start_
m_listenAddressPort = full_uri; m_listenAddressPort = full_uri;
if (!start_handler) { if (!start_handler) {
qDebugNN << LOGSEC_OAUTH qDebugNN << LOGSEC_OAUTH << "User does not want handler to be running.";
<< "User does not want handler to be running.";
return; return;
} }
if (!m_httpServer.listen(listen_address, listen_port)) { if (!m_httpServer.listen(listen_address, listen_port)) {
qCriticalNN << LOGSEC_OAUTH qCriticalNN << LOGSEC_OAUTH << "OAuth redirect handler FAILED TO START TO LISTEN on address"
<< "OAuth redirect handler FAILED TO START TO LISTEN on address" << QUOTE_W_SPACE(listen_address.toString()) << "and port" << QUOTE_W_SPACE(listen_port) << "with error"
<< QUOTE_W_SPACE(listen_address.toString())
<< "and port"
<< QUOTE_W_SPACE(listen_port)
<< "with error"
<< QUOTE_W_SPACE_DOT(m_httpServer.errorString()); << QUOTE_W_SPACE_DOT(m_httpServer.errorString());
} }
else { else {
qDebugNN << LOGSEC_OAUTH qDebugNN << LOGSEC_OAUTH << "OAuth redirect handler IS LISTENING on address"
<< "OAuth redirect handler IS LISTENING on address" << QUOTE_W_SPACE(m_listenAddress.toString()) << "and port" << QUOTE_W_SPACE_DOT(m_listenPort);
<< QUOTE_W_SPACE(m_listenAddress.toString())
<< "and port"
<< QUOTE_W_SPACE_DOT(m_listenPort);
} }
} }
@ -103,18 +93,15 @@ void OAuthHttpHandler::handleRedirection(const QVariantMap& data) {
const QString uri = data.value(QSL("error_uri")).toString(); const QString uri = data.value(QSL("error_uri")).toString();
const QString description = data.value(QSL("error_description")).toString(); const QString description = data.value(QSL("error_description")).toString();
qCriticalNN << LOGSEC_OAUTH qCriticalNN << LOGSEC_OAUTH << "AuthenticationError: " << error << "(" << uri << "): " << description;
<< "AuthenticationError: " << error << "(" << uri << "): " << description;
emit authRejected(description, received_state); emit authRejected(description, received_state);
} }
else if (code.isEmpty()) { else if (code.isEmpty()) {
qCriticalNN << LOGSEC_OAUTH qCriticalNN << LOGSEC_OAUTH << "We did not receive authentication code.";
<< "We did not receive authentication code.";
emit authRejected(QSL("Code not received"), received_state); emit authRejected(QSL("Code not received"), received_state);
} }
else if (received_state.isEmpty()) { else if (received_state.isEmpty()) {
qCriticalNN << LOGSEC_OAUTH qCriticalNN << LOGSEC_OAUTH << "State not received.";
<< "State not received.";
emit authRejected(QSL("State not received"), received_state); emit authRejected(QSL("State not received"), received_state);
} }
else { else {
@ -135,19 +122,19 @@ void OAuthHttpHandler::answerClient(QTcpSocket* socket, const QUrl& url) {
received_data.insert(item.first, item.second); received_data.insert(item.first, item.second);
} }
QByteArray res = socket->readAll();
QString res_utf = QString::fromUtf8(res);
handleRedirection(received_data); handleRedirection(received_data);
const QString html = QSL("<html><head><title>") + const QString html = QSL("<html><head><title>") + qApp->applicationName() + QSL("</title></head><body>") +
qApp->applicationName() + m_successText + QSL("</body></html>");
QSL("</title></head><body>") +
m_successText +
QSL("</body></html>");
const QByteArray html_utf = html.toUtf8(); const QByteArray html_utf = html.toUtf8();
const QByteArray html_size = QString::number(html_utf.size()).toLocal8Bit(); const QByteArray html_size = QString::number(html_utf.size()).toLocal8Bit();
const QByteArray reply_message = QByteArrayLiteral("HTTP/1.0 200 OK \r\n" const QByteArray reply_message = QByteArrayLiteral("HTTP/1.0 200 OK \r\n"
"Content-Type: text/html; charset=\"utf-8\"\r\n" "Content-Type: text/html; charset=\"utf-8\"\r\n"
"Content-Length: ") + html_size + "Content-Length: ") +
QByteArrayLiteral("\r\n\r\n") + html_utf; html_size + QByteArrayLiteral("\r\n\r\n") + html_utf;
socket->write(reply_message); socket->write(reply_message);
} }
@ -303,7 +290,8 @@ bool OAuthHttpHandler::QHttpRequest::readStatus(QTcpSocket* socket) {
} }
if (finished) { if (finished) {
if ((std::isdigit(m_fragment.at(m_fragment.size() - 3)) == 0) || (std::isdigit(m_fragment.at(m_fragment.size() - 1)) == 0)) { if ((std::isdigit(m_fragment.at(m_fragment.size() - 3)) == 0) ||
(std::isdigit(m_fragment.at(m_fragment.size() - 1)) == 0)) {
qWarningNN << LOGSEC_OAUTH << "Invalid version"; qWarningNN << LOGSEC_OAUTH << "Invalid version";
return false; return false;
} }

View File

@ -196,18 +196,21 @@ void GmailServiceRoot::start(bool freshly_activated) {
updateTitle(); updateTitle();
if (getSubTreeFeeds().isEmpty()) { if (getSubTreeFeeds().isEmpty()) {
syncIn(); m_network->oauth()->login([this]() {
syncIn();
});
} }
else {
auto chi = childItems();
auto chi = childItems(); for (RootItem* feed : std::as_const(chi)) {
if (feed->customId() == QL1S(GMAIL_SYSTEM_LABEL_INBOX)) {
for (RootItem* feed : std::as_const(chi)) { feed->setKeepOnTop(true);
if (feed->customId() == QL1S(GMAIL_SYSTEM_LABEL_INBOX)) { }
feed->setKeepOnTop(true);
} }
}
m_network->oauth()->login(); m_network->oauth()->login();
}
} }
QString GmailServiceRoot::code() const { QString GmailServiceRoot::code() const {