Proxy support for gmail.
This commit is contained in:
parent
5131881c3f
commit
b60da1bdb3
@ -38,7 +38,7 @@ GmailServiceRoot* GmailFeed::serviceRoot() const {
|
||||
|
||||
QList<Message> GmailFeed::obtainNewMessages(bool* error_during_obtaining) {
|
||||
Feed::Status error = Feed::Status::Normal;
|
||||
QList<Message> messages = serviceRoot()->network()->messages(customId(), error);
|
||||
QList<Message> messages = serviceRoot()->network()->messages(customId(), error, getParentServiceRoot()->networkProxy());
|
||||
|
||||
setStatus(error);
|
||||
|
||||
|
@ -104,7 +104,7 @@ bool GmailServiceRoot::downloadAttachmentOnMyOwn(const QUrl& url) const {
|
||||
qApp->homeFolder() + QDir::separator() + parts.at(0));
|
||||
|
||||
if (!file.isEmpty() && parts.size() == 3) {
|
||||
Downloader* down = network()->downloadAttachment(parts.at(1), parts.at(2));
|
||||
Downloader* down = network()->downloadAttachment(parts.at(1), parts.at(2), networkProxy());
|
||||
FormDownloadAttachment form(file, down, qApp->mainFormWidget());
|
||||
|
||||
form.exec();
|
||||
@ -203,7 +203,9 @@ void GmailServiceRoot::saveAllCachedData(bool ignore_errors) {
|
||||
QStringList ids = i.value();
|
||||
|
||||
if (!ids.isEmpty()) {
|
||||
if (network()->markMessagesRead(key, ids) != QNetworkReply::NetworkError::NoError && !ignore_errors) {
|
||||
if (network()->markMessagesRead(key, ids, networkProxy()) !=
|
||||
QNetworkReply::NetworkError::NoError &&
|
||||
!ignore_errors) {
|
||||
addMessageStatesToCache(ids, key);
|
||||
}
|
||||
}
|
||||
@ -224,7 +226,9 @@ void GmailServiceRoot::saveAllCachedData(bool ignore_errors) {
|
||||
custom_ids.append(msg.m_customId);
|
||||
}
|
||||
|
||||
if (network()->markMessagesStarred(key, custom_ids) != QNetworkReply::NetworkError::NoError && !ignore_errors) {
|
||||
if (network()->markMessagesStarred(key, custom_ids, networkProxy()) !=
|
||||
QNetworkReply::NetworkError::NoError &&
|
||||
!ignore_errors) {
|
||||
addMessageStatesToCache(messages, key);
|
||||
}
|
||||
}
|
||||
|
@ -126,7 +126,7 @@ void FormAddEditEmail::onOkClicked() {
|
||||
msg.set_header("Content-Type", "text/plain; charset=utf-8");
|
||||
|
||||
try {
|
||||
m_root->network()->sendEmail(msg, m_originalMessage);
|
||||
m_root->network()->sendEmail(msg, m_root->networkProxy(), m_originalMessage);
|
||||
accept();
|
||||
}
|
||||
catch (const ApplicationException& ex) {
|
||||
|
@ -52,7 +52,7 @@ void GmailNetworkFactory::setBatchSize(int batch_size) {
|
||||
m_batchSize = batch_size;
|
||||
}
|
||||
|
||||
QString GmailNetworkFactory::sendEmail(Mimesis::Message msg, Message* reply_to_message) {
|
||||
QString GmailNetworkFactory::sendEmail(Mimesis::Message msg, const QNetworkProxy& custom_proxy, Message* reply_to_message) {
|
||||
QString bearer = m_oauth2->bearer().toLocal8Bit();
|
||||
|
||||
if (bearer.isEmpty()) {
|
||||
@ -65,7 +65,7 @@ QString GmailNetworkFactory::sendEmail(Mimesis::Message msg, Message* reply_to_m
|
||||
auto metadata = getMessageMetadata(reply_to_message->m_customId, {
|
||||
QSL("References"),
|
||||
QSL("Message-ID")
|
||||
});
|
||||
}, custom_proxy);
|
||||
|
||||
/*if (metadata.contains(QSL("References"))) {
|
||||
|
||||
@ -92,7 +92,11 @@ QString GmailNetworkFactory::sendEmail(Mimesis::Message msg, Message* reply_to_m
|
||||
input_data,
|
||||
out,
|
||||
QNetworkAccessManager::Operation::PostOperation,
|
||||
headers);
|
||||
headers,
|
||||
false,
|
||||
{},
|
||||
{},
|
||||
custom_proxy);
|
||||
|
||||
if (result.first != QNetworkReply::NetworkError::NoError) {
|
||||
if (!out.isEmpty()) {
|
||||
@ -135,7 +139,9 @@ void GmailNetworkFactory::setUsername(const QString& username) {
|
||||
m_username = username;
|
||||
}
|
||||
|
||||
Downloader* GmailNetworkFactory::downloadAttachment(const QString& msg_id, const QString& attachment_id) {
|
||||
Downloader* GmailNetworkFactory::downloadAttachment(const QString& msg_id,
|
||||
const QString& attachment_id,
|
||||
const QNetworkProxy& custom_proxy) {
|
||||
QString bearer = m_oauth2->bearer().toLocal8Bit();
|
||||
|
||||
if (bearer.isEmpty()) {
|
||||
@ -145,6 +151,10 @@ Downloader* GmailNetworkFactory::downloadAttachment(const QString& msg_id, const
|
||||
auto* downloader = new Downloader();
|
||||
QString target_url = QString(GMAIL_API_GET_ATTACHMENT).arg(msg_id, attachment_id);
|
||||
|
||||
if (custom_proxy.type() != QNetworkProxy::ProxyType::DefaultProxy) {
|
||||
downloader->setProxy(custom_proxy);
|
||||
}
|
||||
|
||||
downloader->appendRawHeader(QString(HTTP_HEADERS_AUTHORIZATION).toLocal8Bit(), bearer.toLocal8Bit());
|
||||
downloader->downloadFile(target_url);
|
||||
|
||||
@ -152,9 +162,9 @@ Downloader* GmailNetworkFactory::downloadAttachment(const QString& msg_id, const
|
||||
}
|
||||
}
|
||||
|
||||
QList<Message> GmailNetworkFactory::messages(const QString& stream_id, Feed::Status& error) {
|
||||
Downloader downloader;
|
||||
QEventLoop loop;
|
||||
QList<Message> GmailNetworkFactory::messages(const QString& stream_id,
|
||||
Feed::Status& error,
|
||||
const QNetworkProxy& custom_proxy) {
|
||||
QString bearer = m_oauth2->bearer().toLocal8Bit();
|
||||
QString next_page_token;
|
||||
QList<Message> messages;
|
||||
@ -164,11 +174,9 @@ QList<Message> GmailNetworkFactory::messages(const QString& stream_id, Feed::Sta
|
||||
return QList<Message>();
|
||||
}
|
||||
|
||||
downloader.appendRawHeader(QString(HTTP_HEADERS_AUTHORIZATION).toLocal8Bit(), bearer.toLocal8Bit());
|
||||
|
||||
// We need to quit event loop when the download finishes.
|
||||
connect(&downloader, &Downloader::completed, &loop, &QEventLoop::quit);
|
||||
QString target_url;
|
||||
int timeout = qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::UpdateTimeout)).toInt();
|
||||
|
||||
do {
|
||||
target_url = GMAIL_API_MSGS_LIST;
|
||||
@ -182,17 +190,27 @@ QList<Message> GmailNetworkFactory::messages(const QString& stream_id, Feed::Sta
|
||||
target_url += QString("&pageToken=%1").arg(next_page_token);
|
||||
}
|
||||
|
||||
downloader.manipulateData(target_url, QNetworkAccessManager::Operation::GetOperation);
|
||||
loop.exec();
|
||||
QByteArray messages_raw_data;
|
||||
auto netw = NetworkFactory::performNetworkOperation(target_url,
|
||||
timeout,
|
||||
{},
|
||||
messages_raw_data,
|
||||
QNetworkAccessManager::Operation::GetOperation,
|
||||
{ { QString(HTTP_HEADERS_AUTHORIZATION).toLocal8Bit(),
|
||||
bearer.toLocal8Bit() } },
|
||||
false,
|
||||
{},
|
||||
{},
|
||||
custom_proxy);
|
||||
|
||||
if (downloader.lastOutputError() == QNetworkReply::NetworkError::NoError) {
|
||||
if (netw.first == QNetworkReply::NetworkError::NoError) {
|
||||
// We parse this chunk.
|
||||
QString messages_data = downloader.lastOutputData();
|
||||
QString messages_data = QString::fromUtf8(messages_raw_data);
|
||||
QList<Message> more_messages = decodeLiteMessages(messages_data, stream_id, next_page_token);
|
||||
|
||||
if (!more_messages.isEmpty()) {
|
||||
// Now, we via batch HTTP request obtain full data for each message.
|
||||
bool obtained = obtainAndDecodeFullMessages(more_messages, stream_id);
|
||||
bool obtained = obtainAndDecodeFullMessages(more_messages, stream_id, custom_proxy);
|
||||
|
||||
if (obtained) {
|
||||
messages.append(more_messages);
|
||||
@ -220,7 +238,9 @@ QList<Message> GmailNetworkFactory::messages(const QString& stream_id, Feed::Sta
|
||||
return messages;
|
||||
}
|
||||
|
||||
QNetworkReply::NetworkError GmailNetworkFactory::markMessagesRead(RootItem::ReadStatus status, QStringList custom_ids) {
|
||||
QNetworkReply::NetworkError GmailNetworkFactory::markMessagesRead(RootItem::ReadStatus status,
|
||||
QStringList custom_ids,
|
||||
const QNetworkProxy& custom_proxy) {
|
||||
QString bearer = m_oauth2->bearer().toLocal8Bit();
|
||||
|
||||
if (bearer.isEmpty()) {
|
||||
@ -263,7 +283,11 @@ QNetworkReply::NetworkError GmailNetworkFactory::markMessagesRead(RootItem::Read
|
||||
param_doc.toJson(QJsonDocument::JsonFormat::Compact),
|
||||
output,
|
||||
QNetworkAccessManager::Operation::PostOperation,
|
||||
headers).first;
|
||||
headers,
|
||||
false,
|
||||
{},
|
||||
{},
|
||||
custom_proxy).first;
|
||||
|
||||
if (result != QNetworkReply::NetworkError::NoError) {
|
||||
return result;
|
||||
@ -273,7 +297,9 @@ QNetworkReply::NetworkError GmailNetworkFactory::markMessagesRead(RootItem::Read
|
||||
return QNetworkReply::NetworkError::NoError;
|
||||
}
|
||||
|
||||
QNetworkReply::NetworkError GmailNetworkFactory::markMessagesStarred(RootItem::Importance importance, const QStringList& custom_ids) {
|
||||
QNetworkReply::NetworkError GmailNetworkFactory::markMessagesStarred(RootItem::Importance importance,
|
||||
const QStringList& custom_ids,
|
||||
const QNetworkProxy& custom_proxy) {
|
||||
QString bearer = m_oauth2->bearer().toLocal8Bit();
|
||||
|
||||
if (bearer.isEmpty()) {
|
||||
@ -316,7 +342,11 @@ QNetworkReply::NetworkError GmailNetworkFactory::markMessagesStarred(RootItem::I
|
||||
param_doc.toJson(QJsonDocument::JsonFormat::Compact),
|
||||
output,
|
||||
QNetworkAccessManager::Operation::PostOperation,
|
||||
headers).first;
|
||||
headers,
|
||||
false,
|
||||
{},
|
||||
{},
|
||||
custom_proxy).first;
|
||||
|
||||
if (result != QNetworkReply::NetworkError::NoError) {
|
||||
return result;
|
||||
@ -435,7 +465,7 @@ bool GmailNetworkFactory::fillFullMessage(Message& msg, const QJsonObject& json,
|
||||
return true;
|
||||
}
|
||||
|
||||
QMap<QString, QString> GmailNetworkFactory::getMessageMetadata(const QString& msg_id, const QStringList& metadata) {
|
||||
QMap<QString, QString> GmailNetworkFactory::getMessageMetadata(const QString& msg_id, const QStringList& metadata, const QNetworkProxy& custom_proxy) {
|
||||
QString bearer = m_oauth2->bearer();
|
||||
|
||||
if (bearer.isEmpty()) {
|
||||
@ -457,7 +487,11 @@ QMap<QString, QString> GmailNetworkFactory::getMessageMetadata(const QString& ms
|
||||
QByteArray(),
|
||||
output,
|
||||
QNetworkAccessManager::Operation::GetOperation,
|
||||
headers);
|
||||
headers,
|
||||
false,
|
||||
{},
|
||||
{},
|
||||
custom_proxy);
|
||||
|
||||
if (res.first == QNetworkReply::NetworkError::NoError) {
|
||||
QJsonDocument doc = QJsonDocument::fromJson(output);
|
||||
@ -477,7 +511,9 @@ QMap<QString, QString> GmailNetworkFactory::getMessageMetadata(const QString& ms
|
||||
}
|
||||
}
|
||||
|
||||
bool GmailNetworkFactory::obtainAndDecodeFullMessages(QList<Message>& messages, const QString& feed_id) {
|
||||
bool GmailNetworkFactory::obtainAndDecodeFullMessages(QList<Message>& messages,
|
||||
const QString& feed_id,
|
||||
const QNetworkProxy& custom_proxy) {
|
||||
QHash<QString, int> msgs;
|
||||
int next_message = 0;
|
||||
QString bearer = m_oauth2->bearer();
|
||||
@ -515,7 +551,11 @@ bool GmailNetworkFactory::obtainAndDecodeFullMessages(QList<Message>& messages,
|
||||
multi,
|
||||
output,
|
||||
QNetworkAccessManager::Operation::PostOperation,
|
||||
headers);
|
||||
headers,
|
||||
false,
|
||||
{},
|
||||
{},
|
||||
custom_proxy);
|
||||
|
||||
if (res.first == QNetworkReply::NetworkError::NoError) {
|
||||
// We parse each part of HTTP response (it contains HTTP headers and payload with msg full data).
|
||||
|
@ -36,13 +36,17 @@ class GmailNetworkFactory : public QObject {
|
||||
void setBatchSize(int batch_size);
|
||||
|
||||
// Sends e-mail, returns its ID.
|
||||
QString sendEmail(Mimesis::Message msg, Message* reply_to_message = nullptr);
|
||||
QString sendEmail(Mimesis::Message msg, const QNetworkProxy& custom_proxy, Message* reply_to_message = nullptr);
|
||||
|
||||
Downloader* downloadAttachment(const QString& msg_id, const QString& attachment_id);
|
||||
Downloader* downloadAttachment(const QString& msg_id, const QString& attachment_id, const QNetworkProxy& custom_proxy);
|
||||
|
||||
QList<Message> messages(const QString& stream_id, Feed::Status& error);
|
||||
QNetworkReply::NetworkError markMessagesRead(RootItem::ReadStatus status, QStringList custom_ids);
|
||||
QNetworkReply::NetworkError markMessagesStarred(RootItem::Importance importance, const QStringList& custom_ids);
|
||||
QList<Message> messages(const QString& stream_id, Feed::Status& error, const QNetworkProxy& custom_proxy);
|
||||
QNetworkReply::NetworkError markMessagesRead(RootItem::ReadStatus status,
|
||||
QStringList custom_ids,
|
||||
const QNetworkProxy& custom_proxy);
|
||||
QNetworkReply::NetworkError markMessagesStarred(RootItem::Importance importance,
|
||||
const QStringList& custom_ids,
|
||||
const QNetworkProxy& custom_proxy);
|
||||
|
||||
private slots:
|
||||
void onTokensError(const QString& error, const QString& error_description);
|
||||
@ -50,8 +54,12 @@ class GmailNetworkFactory : public QObject {
|
||||
|
||||
private:
|
||||
bool fillFullMessage(Message& msg, const QJsonObject& json, const QString& feed_id);
|
||||
QMap<QString, QString> getMessageMetadata(const QString& msg_id, const QStringList& metadata);
|
||||
bool obtainAndDecodeFullMessages(QList<Message>& lite_messages, const QString& feed_id);
|
||||
QMap<QString, QString> getMessageMetadata(const QString& msg_id,
|
||||
const QStringList& metadata,
|
||||
const QNetworkProxy& custom_proxy);
|
||||
bool obtainAndDecodeFullMessages(QList<Message>& lite_messages,
|
||||
const QString& feed_id,
|
||||
const QNetworkProxy& custom_proxy);
|
||||
QList<Message> decodeLiteMessages(const QString& messages_json_data, const QString& stream_id, QString& next_page_token);
|
||||
|
||||
//RootItem* decodeFeedCategoriesData(const QString& categories);
|
||||
|
Loading…
x
Reference in New Issue
Block a user