Predefined folders for gmail.

This commit is contained in:
Martin Rotter 2017-10-19 08:53:31 +02:00
parent 1fbdfc796c
commit 5d51b8d28d
8 changed files with 73 additions and 75 deletions

View File

@ -69,6 +69,12 @@ QList<QAction*> ServiceRoot::serviceMenu() {
return QList<QAction*>(); return QList<QAction*>();
} }
void ServiceRoot::start(bool freshly_activated) {
Q_UNUSED(freshly_activated)
}
void ServiceRoot::stop() {}
void ServiceRoot::updateCounts(bool including_total_count) { void ServiceRoot::updateCounts(bool including_total_count) {
QList<Feed*> feeds; QList<Feed*> feeds;
@ -187,6 +193,14 @@ QList<Message> ServiceRoot::undeletedMessages() const {
return DatabaseQueries::getUndeletedMessagesForAccount(database, accountId()); return DatabaseQueries::getUndeletedMessagesForAccount(database, accountId());
} }
bool ServiceRoot::supportsFeedAdding() const {
return false;
}
bool ServiceRoot::supportsCategoryAdding() const {
return false;
}
void ServiceRoot::itemChanged(const QList<RootItem*>& items) { void ServiceRoot::itemChanged(const QList<RootItem*>& items) {
emit dataChanged(items); emit dataChanged(items);
} }
@ -211,6 +225,12 @@ void ServiceRoot::requestItemRemoval(RootItem* item) {
emit itemRemovalRequested(item); emit itemRemovalRequested(item);
} }
void ServiceRoot::addNewFeed(const QString& url) {
Q_UNUSED(url)
}
void ServiceRoot::addNewCategory() {}
QMap<QString, QVariant> ServiceRoot::storeCustomFeedsData() { QMap<QString, QVariant> ServiceRoot::storeCustomFeedsData() {
QMap<QString, QVariant> custom_data; QMap<QString, QVariant> custom_data;

View File

@ -35,8 +35,8 @@ class ServiceRoot : public RootItem {
virtual RecycleBin* recycleBin() const; virtual RecycleBin* recycleBin() const;
QList<Message> undeletedMessages() const; QList<Message> undeletedMessages() const;
virtual bool supportsFeedAdding() const = 0; virtual bool supportsFeedAdding() const;
virtual bool supportsCategoryAdding() const = 0; virtual bool supportsCategoryAdding() const;
// Returns list of specific actions for "Add new item" main window menu. // Returns list of specific actions for "Add new item" main window menu.
// So typical list of returned actions could look like: // So typical list of returned actions could look like:
@ -61,8 +61,8 @@ class ServiceRoot : public RootItem {
// //
// Stop method is called just before application exits OR when // Stop method is called just before application exits OR when
// user explicitly deletes existing service instance. // user explicitly deletes existing service instance.
virtual void start(bool freshly_activated) = 0; virtual void start(bool freshly_activated);
virtual void stop() = 0; virtual void stop();
// Account ID corresponds with DB attribute Accounts (id). // Account ID corresponds with DB attribute Accounts (id).
int accountId() const; int accountId() const;
@ -145,8 +145,8 @@ class ServiceRoot : public RootItem {
void requestItemRemoval(RootItem* item); void requestItemRemoval(RootItem* item);
public slots: public slots:
virtual void addNewFeed(const QString& url = QString()) = 0; virtual void addNewFeed(const QString& url = QString());
virtual void addNewCategory() = 0; virtual void addNewCategory();
virtual void syncIn(); virtual void syncIn();
protected: protected:

View File

@ -9,6 +9,12 @@
GmailFeed::GmailFeed(RootItem* parent) : Feed(parent) {} GmailFeed::GmailFeed(RootItem* parent) : Feed(parent) {}
GmailFeed::GmailFeed(const QString& title, const QString& custom_id, const QIcon& icon, RootItem* parent) : GmailFeed(parent) {
setTitle(title);
setCustomId(custom_id);
setIcon(icon);
}
GmailFeed::GmailFeed(const QSqlRecord& record) : Feed(record) {} GmailFeed::GmailFeed(const QSqlRecord& record) : Feed(record) {}
GmailServiceRoot* GmailFeed::serviceRoot() const { GmailServiceRoot* GmailFeed::serviceRoot() const {

View File

@ -10,6 +10,7 @@ class GmailServiceRoot;
class GmailFeed : public Feed { class GmailFeed : public Feed {
public: public:
explicit GmailFeed(RootItem* parent = nullptr); explicit GmailFeed(RootItem* parent = nullptr);
explicit GmailFeed(const QString& title, const QString& custom_id, const QIcon& icon, RootItem* parent = nullptr);
explicit GmailFeed(const QSqlRecord& record); explicit GmailFeed(const QSqlRecord& record);
GmailServiceRoot* serviceRoot() const; GmailServiceRoot* serviceRoot() const;

View File

@ -25,6 +25,7 @@
#include "network-web/oauth2service.h" #include "network-web/oauth2service.h"
#include "services/abstract/recyclebin.h" #include "services/abstract/recyclebin.h"
#include "services/gmail/gmailentrypoint.h" #include "services/gmail/gmailentrypoint.h"
#include "services/gmail/gmailfeed.h"
#include "services/gmail/network/gmailnetworkfactory.h" #include "services/gmail/network/gmailnetworkfactory.h"
GmailServiceRoot::GmailServiceRoot(GmailNetworkFactory* network, RootItem* parent) : ServiceRoot(parent), GmailServiceRoot::GmailServiceRoot(GmailNetworkFactory* network, RootItem* parent) : ServiceRoot(parent),
@ -47,13 +48,10 @@ void GmailServiceRoot::updateTitle() {
} }
void GmailServiceRoot::loadFromDatabase() { void GmailServiceRoot::loadFromDatabase() {
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings); appendChild(new GmailFeed(tr("Inbox"), QSL("INBOX"), qApp->icons()->fromTheme(QSL("mail-inbox")), this));
Assignment categories = DatabaseQueries::getCategories(database, accountId()); appendChild(new GmailFeed(tr("Sent"), QSL("SENT"), qApp->icons()->fromTheme(QSL("mail-sent")), this));
Assignment feeds = DatabaseQueries::getInoreaderFeeds(database, accountId()); 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));
// All data are now obtained, lets create the hierarchy.
assembleCategories(categories);
assembleFeeds(feeds);
// As the last item, add recycle bin, which is needed. // As the last item, add recycle bin, which is needed.
appendChild(recycleBin()); appendChild(recycleBin());
@ -121,27 +119,12 @@ void GmailServiceRoot::start(bool freshly_activated) {
loadCacheFromFile(accountId()); loadCacheFromFile(accountId());
m_network->oauth()->login(); m_network->oauth()->login();
if (childCount() <= 1) {
syncIn();
}
} }
void GmailServiceRoot::stop() { void GmailServiceRoot::stop() {
saveCacheToFile(accountId()); saveCacheToFile(accountId());
} }
QList<QAction*> GmailServiceRoot::serviceMenu() {
if (m_serviceMenu.isEmpty()) {
QAction* act_sync_in = new QAction(qApp->icons()->fromTheme(QSL("view-refresh")), tr("Sync in"), this);
connect(act_sync_in, &QAction::triggered, this, &GmailServiceRoot::syncIn);
m_serviceMenu.append(act_sync_in);
}
return m_serviceMenu;
}
QString GmailServiceRoot::code() const { QString GmailServiceRoot::code() const {
return GmailEntryPoint().code(); return GmailEntryPoint().code();
} }
@ -153,16 +136,6 @@ QString GmailServiceRoot::additionalTooltip() const {
network()->oauth()->tokensExpireIn().toString() : QSL("-")); network()->oauth()->tokensExpireIn().toString() : QSL("-"));
} }
RootItem* GmailServiceRoot::obtainNewTreeForSyncIn() const {
return m_network->feedsCategories();
}
void GmailServiceRoot::addNewFeed(const QString& url) {
Q_UNUSED(url)
}
void GmailServiceRoot::addNewCategory() {}
void GmailServiceRoot::saveAllCachedData(bool async) { void GmailServiceRoot::saveAllCachedData(bool async) {
QPair<QMap<RootItem::ReadStatus, QStringList>, QMap<RootItem::Importance, QList<Message>>> msgCache = takeMessageCache(); QPair<QMap<RootItem::ReadStatus, QStringList>, QMap<RootItem::Importance, QList<Message>>> msgCache = takeMessageCache();
QMapIterator<RootItem::ReadStatus, QStringList> i(msgCache.first); QMapIterator<RootItem::ReadStatus, QStringList> i(msgCache.first);

View File

@ -49,18 +49,13 @@ class GmailServiceRoot : public ServiceRoot, public CacheForServiceRoot {
QString additionalTooltip() const; QString additionalTooltip() const;
RootItem* obtainNewTreeForSyncIn() const;
void saveAllCachedData(bool async = true); void saveAllCachedData(bool async = true);
public slots: public slots:
void addNewFeed(const QString& url);
void addNewCategory();
void updateTitle(); void updateTitle();
private: private:
void loadFromDatabase(); void loadFromDatabase();
QList<QAction*> serviceMenu();
private: private:
QList<QAction*> m_serviceMenu; QList<QAction*> m_serviceMenu;

View File

@ -70,32 +70,33 @@ void GmailNetworkFactory::setUsername(const QString& username) {
m_username = username; m_username = username;
} }
RootItem* GmailNetworkFactory::feedsCategories() { /*
Downloader downloader; RootItem* GmailNetworkFactory::feedsCategories() {
QEventLoop loop; Downloader downloader;
QString bearer = m_oauth2->bearer().toLocal8Bit(); QEventLoop loop;
QString bearer = m_oauth2->bearer().toLocal8Bit();
if (bearer.isEmpty()) { if (bearer.isEmpty()) {
return nullptr; return nullptr;
} }
downloader.appendRawHeader(QString(HTTP_HEADERS_AUTHORIZATION).toLocal8Bit(), bearer.toLocal8Bit()); downloader.appendRawHeader(QString(HTTP_HEADERS_AUTHORIZATION).toLocal8Bit(), bearer.toLocal8Bit());
// We need to quit event loop when the download finishes. // We need to quit event loop when the download finishes.
connect(&downloader, &Downloader::completed, &loop, &QEventLoop::quit); connect(&downloader, &Downloader::completed, &loop, &QEventLoop::quit);
// TODO: dodělat // TODO: dodělat
downloader.manipulateData(GMAIL_API_LABELS_LIST, QNetworkAccessManager::Operation::GetOperation); downloader.manipulateData(GMAIL_API_LABELS_LIST, QNetworkAccessManager::Operation::GetOperation);
loop.exec(); loop.exec();
if (downloader.lastOutputError() != QNetworkReply::NetworkError::NoError) { if (downloader.lastOutputError() != QNetworkReply::NetworkError::NoError) {
return nullptr; return nullptr;
} }
QString category_data = downloader.lastOutputData(); QString category_data = downloader.lastOutputData();
return decodeFeedCategoriesData(category_data); return decodeFeedCategoriesData(category_data);
} }*/
QList<Message> GmailNetworkFactory::messages(const QString& stream_id, Feed::Status& error) { QList<Message> GmailNetworkFactory::messages(const QString& stream_id, Feed::Status& error) {
Downloader downloader; Downloader downloader;
@ -356,14 +357,15 @@ QList<Message> GmailNetworkFactory::decodeMessages(const QString& messages_json_
return messages; return messages;
} }
RootItem* GmailNetworkFactory::decodeFeedCategoriesData(const QString& categories) { /*
RootItem* parent = new RootItem(); RootItem* GmailNetworkFactory::decodeFeedCategoriesData(const QString& categories) {
QJsonArray json = QJsonDocument::fromJson(categories.toUtf8()).object()["labels"].toArray(); RootItem* parent = new RootItem();
QJsonArray json = QJsonDocument::fromJson(categories.toUtf8()).object()["labels"].toArray();
QMap<QString, RootItem*> cats; QMap<QString, RootItem*> cats;
cats.insert(QString(), parent); cats.insert(QString(), parent);
foreach (const QJsonValue& obj, json) { foreach (const QJsonValue& obj, json) {
auto label = obj.toObject(); auto label = obj.toObject();
QString label_id = label["id"].toString(); QString label_id = label["id"].toString();
QString label_name = label["name"].toString(); QString label_name = label["name"].toString();
@ -381,7 +383,7 @@ RootItem* GmailNetworkFactory::decodeFeedCategoriesData(const QString& categorie
parent->appendChild(feed); parent->appendChild(feed);
} }
/*
if (label_id.contains(QSL("/label/"))) { if (label_id.contains(QSL("/label/"))) {
// We have label (not "state"). // We have label (not "state").
Category* category = new Category(); Category* category = new Category();
@ -394,10 +396,9 @@ RootItem* GmailNetworkFactory::decodeFeedCategoriesData(const QString& categorie
// All categories in ownCloud are top-level. // All categories in ownCloud are top-level.
parent->appendChild(category); parent->appendChild(category);
}*/ }
} }
/*
json = QJsonDocument::fromJson(feeds.toUtf8()).object()["subscriptions"].toArray(); json = QJsonDocument::fromJson(feeds.toUtf8()).object()["subscriptions"].toArray();
foreach (const QJsonValue& obj, json) { foreach (const QJsonValue& obj, json) {
@ -428,7 +429,8 @@ RootItem* GmailNetworkFactory::decodeFeedCategoriesData(const QString& categorie
if (cats.contains(parent_label)) { if (cats.contains(parent_label)) {
cats[parent_label]->appendChild(feed); cats[parent_label]->appendChild(feed);
} }
}*/ }
return parent; return parent;
} }
*/

View File

@ -36,7 +36,7 @@ class GmailNetworkFactory : public QObject {
// Returns tree of feeds/categories. // Returns tree of feeds/categories.
// Top-level root of the tree is not needed here. // Top-level root of the tree is not needed here.
// Returned items do not have primary IDs assigned. // Returned items do not have primary IDs assigned.
RootItem* feedsCategories(); //RootItem* feedsCategories();
QList<Message> messages(const QString& stream_id, Feed::Status& error); QList<Message> messages(const QString& stream_id, Feed::Status& error);
void markMessagesRead(RootItem::ReadStatus status, const QStringList& custom_ids, bool async = true); void markMessagesRead(RootItem::ReadStatus status, const QStringList& custom_ids, bool async = true);
@ -48,7 +48,8 @@ class GmailNetworkFactory : public QObject {
private: private:
QList<Message> decodeMessages(const QString& messages_json_data, const QString& stream_id); QList<Message> decodeMessages(const QString& messages_json_data, const QString& stream_id);
RootItem* decodeFeedCategoriesData(const QString& categories);
//RootItem* decodeFeedCategoriesData(const QString& categories);
void initializeOauth(); void initializeOauth();