Can load messages for gmail acc and display them.

This commit is contained in:
Martin Rotter 2017-10-20 23:12:08 +02:00
parent 56d5b707d0
commit edec9d7582
6 changed files with 74 additions and 10 deletions

View File

@ -8,6 +8,7 @@
#include "network-web/oauth2service.h"
#include "services/abstract/category.h"
#include "services/gmail/definitions.h"
#include "services/gmail/gmailfeed.h"
#include "services/gmail/gmailserviceroot.h"
#include "services/gmail/network/gmailnetworkfactory.h"
#include "services/inoreader/definitions.h"
@ -1475,6 +1476,37 @@ Assignment DatabaseQueries::getCategories(QSqlDatabase db, int account_id, bool*
return categories;
}
Assignment DatabaseQueries::getGmailFeeds(QSqlDatabase db, int account_id, bool* ok) {
Assignment feeds;
QSqlQuery q(db);
q.setForwardOnly(true);
q.prepare(QSL("SELECT * FROM Feeds WHERE account_id = :account_id;"));
q.bindValue(QSL(":account_id"), account_id);
if (!q.exec()) {
qFatal("Gmail: Query for obtaining feeds failed. Error message: '%s'.", qPrintable(q.lastError().text()));
if (ok != nullptr) {
*ok = false;
}
}
while (q.next()) {
AssignmentItem pair;
pair.first = q.value(FDS_DB_CATEGORY_INDEX).toInt();
pair.second = new GmailFeed(q.record());
feeds << pair;
}
if (ok != nullptr) {
*ok = true;
}
return feeds;
}
QList<ServiceRoot*> DatabaseQueries::getGmailAccounts(QSqlDatabase db, bool* ok) {
QSqlQuery query(db);

View File

@ -63,6 +63,7 @@ class DatabaseQueries {
static Assignment getCategories(QSqlDatabase db, int account_id, bool* ok = nullptr);
// Gmail account.
static Assignment getGmailFeeds(QSqlDatabase db, int account_id, bool* ok = nullptr);
static bool deleteGmailAccount(QSqlDatabase db, int account_id);
static QList<ServiceRoot*> getGmailAccounts(QSqlDatabase db, bool* ok = nullptr);
static bool overwriteGmailAccount(QSqlDatabase db, const QString& username, const QString& app_id,

View File

@ -47,15 +47,28 @@ void GmailServiceRoot::updateTitle() {
setTitle(m_network->userName() + QSL(" (Gmail)"));
}
void GmailServiceRoot::loadFromDatabase() {
GmailFeed* inbox = new GmailFeed(tr("Inbox"), QSL("INBOX"), qApp->icons()->fromTheme(QSL("mail-inbox")), this);
RootItem* GmailServiceRoot::obtainNewTreeForSyncIn() const {
RootItem* root = new RootItem();
GmailFeed* inbox = new GmailFeed(tr("Inbox"), QSL("INBOX"), qApp->icons()->fromTheme(QSL("mail-inbox")), root);
inbox->setKeepOnTop(true);
appendChild(inbox);
appendChild(new GmailFeed(tr("Sent"), QSL("SENT"), qApp->icons()->fromTheme(QSL("mail-sent")), this));
appendChild(new GmailFeed(tr("Drafts"), QSL("DRAFT"), qApp->icons()->fromTheme(QSL("gtk-edit")), this));
appendChild(new GmailFeed(tr("Spam"), QSL("SPAM"), qApp->icons()->fromTheme(QSL("mail-mark-junk")), this));
root->appendChild(inbox);
root->appendChild(new GmailFeed(tr("Sent"), QSL("SENT"), qApp->icons()->fromTheme(QSL("mail-sent")), root));
root->appendChild(new GmailFeed(tr("Drafts"), QSL("DRAFT"), qApp->icons()->fromTheme(QSL("gtk-edit")), root));
root->appendChild(new GmailFeed(tr("Spam"), QSL("SPAM"), qApp->icons()->fromTheme(QSL("mail-mark-junk")), root));
return root;
}
void GmailServiceRoot::loadFromDatabase() {
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
Assignment categories = DatabaseQueries::getCategories(database, accountId());
Assignment feeds = DatabaseQueries::getGmailFeeds(database, accountId());
// All data are now obtained, lets create the hierarchy.
assembleCategories(categories);
assembleFeeds(feeds);
// As the last item, add recycle bin, which is needed.
appendChild(recycleBin());
@ -123,6 +136,10 @@ void GmailServiceRoot::start(bool freshly_activated) {
loadCacheFromFile(accountId());
m_network->oauth()->login();
if (childCount() <= 1) {
syncIn();
}
}
void GmailServiceRoot::stop() {

View File

@ -54,12 +54,16 @@ class GmailServiceRoot : public ServiceRoot, public CacheForServiceRoot {
public slots:
void updateTitle();
protected:
RootItem* obtainNewTreeForSyncIn() const;
private:
void loadFromDatabase();
private:
QList<QAction*> m_serviceMenu;
GmailNetworkFactory* m_network;
};
inline void GmailServiceRoot::setNetwork(GmailNetworkFactory* network) {

View File

@ -139,12 +139,13 @@ QList<Message> GmailNetworkFactory::messages(const QString& stream_id, Feed::Sta
QString messages_data = downloader.lastOutputData();
QList<Message> more_messages = decodeLiteMessages(messages_data, stream_id, next_page_token);
QList<Message> full_messages;
// 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, full_messages);
if (obtained) {
messages.append(more_messages);
messages.append(full_messages);
// New batch of messages was obtained, check if we have enough.
if (batchSize() > 0 && batchSize() <= messages.size()) {
@ -343,9 +344,17 @@ void GmailNetworkFactory::fillFullMessage(Message& msg, const QJsonObject& json,
msg.m_created = TextFactory::parseDateTime(headers["Date"]);
// TODO: Pokračovat.
foreach (const QJsonValue& body_part, json["payload"].toObject()["parts"].toArray()) {
QJsonObject body_obj = body_part.toObject();
msg.m_contents = QByteArray::fromBase64(body_obj["body"].toObject()["data"].toString().toLocal8Bit(),
QByteArray::Base64Option::Base64UrlEncoding);
}
}
bool GmailNetworkFactory::obtainAndDecodeFullMessages(QList<Message>& lite_messages, const QString& feed_id) {
bool GmailNetworkFactory::obtainAndDecodeFullMessages(const QList<Message>& lite_messages,
const QString& feed_id,
QList<Message>& full_messages) {
QHttpMultiPart* multi = new QHttpMultiPart();
multi->setContentType(QHttpMultiPart::ContentType::MixedType);
@ -393,6 +402,7 @@ bool GmailNetworkFactory::obtainAndDecodeFullMessages(QList<Message>& lite_messa
Message& msg = msgs[msg_id];
fillFullMessage(msg, msg_doc, feed_id);
full_messages.append(msg);
}
}

View File

@ -48,7 +48,7 @@ class GmailNetworkFactory : public QObject {
private:
void fillFullMessage(Message& msg, const QJsonObject& json, const QString& feed_id);
bool obtainAndDecodeFullMessages(QList<Message>& lite_messages, const QString& feed_id);
bool obtainAndDecodeFullMessages(const QList<Message>& lite_messages, const QString& feed_id, QList<Message>& full_messages);
QList<Message> decodeLiteMessages(const QString& messages_json_data, const QString& stream_id, QString& next_page_token);
//RootItem* decodeFeedCategoriesData(const QString& categories);