small refactoring

This commit is contained in:
Martin Rotter 2020-07-27 10:54:09 +02:00
parent 40402f6c7a
commit 0b2e1df83d
15 changed files with 100 additions and 83 deletions

View File

@ -72,8 +72,23 @@ QList<QAction*> ServiceRoot::contextMenu() {
return serviceMenu();
}
QList<QAction*> ServiceRoot::contextMenuForMessages(const QList<Message*>& messages) {
Q_UNUSED(messages)
return {};
}
QList<QAction*> ServiceRoot::serviceMenu() {
return QList<QAction*>();
if (m_serviceMenu.isEmpty() && isSyncable()) {
m_actionSyncIn = new QAction(qApp->icons()->fromTheme(QSL("view-refresh")), tr("Sync in"), this);
connect(m_actionSyncIn, &QAction::triggered, this, &ServiceRoot::syncIn);
m_serviceMenu.append(m_actionSyncIn);
}
return m_serviceMenu;
}
bool ServiceRoot::isSyncable() const {
return false;
}
void ServiceRoot::start(bool freshly_activated) {

View File

@ -54,13 +54,17 @@ class ServiceRoot : public RootItem {
virtual QList<QAction*> addItemMenu();
// Returns actions to display as context menu.
QList<QAction*> contextMenu();
virtual QList<QAction*> contextMenu();
virtual QList<QAction*> contextMenuForMessages(const QList<Message*>& messages);
// Returns list of specific actions to be shown in main window menu
// bar in sections "Services -> 'this service'".
// NOTE: Caller does NOT take ownership of created menu!
virtual QList<QAction*> serviceMenu();
// If plugin uses online synchronization, then returns true.
virtual bool isSyncable() const;
// Start/stop services.
// Start method is called when feed model gets initialized OR after user adds new service.
// Account should synchronously initialize its children (load them from DB is recommended
@ -204,10 +208,12 @@ class ServiceRoot : public RootItem {
virtual QMap<QString, QVariantMap> storeCustomFeedsData();
virtual void restoreCustomFeedsData(const QMap<QString, QVariantMap>& data, const QHash<QString, Feed*>& feeds);
private:
protected:
RecycleBin* m_recycleBin;
ImportantNode* m_importantNode;
int m_accountId;
QAction* m_actionSyncIn;
QList<QAction*> m_serviceMenu;
};
#endif // SERVICEROOT_H

View File

@ -130,6 +130,8 @@ bool GmailServiceRoot::downloadAttachmentOnMyOwn(const QUrl& url) const {
QList<QAction*> GmailServiceRoot::serviceMenu() {
if (m_serviceMenu.isEmpty()) {
ServiceRoot::serviceMenu();
QAction* act_new_email = new QAction(qApp->icons()->fromTheme(QSL("mail-message-new")), tr("Write new e-mail message"), this);
connect(act_new_email, &QAction::triggered, this, &GmailServiceRoot::writeNewEmail);
@ -139,6 +141,10 @@ QList<QAction*> GmailServiceRoot::serviceMenu() {
return m_serviceMenu;
}
bool GmailServiceRoot::isSyncable() const {
return true;
}
bool GmailServiceRoot::canBeEdited() const {
return true;
}

View File

@ -23,6 +23,7 @@ class GmailServiceRoot : public ServiceRoot, public CacheForServiceRoot {
GmailNetworkFactory* network() const;
QList<QAction*> serviceMenu();
bool isSyncable() const;
bool canBeEdited() const;
bool editViaGui();
bool canBeDeleted() const;
@ -48,9 +49,7 @@ class GmailServiceRoot : public ServiceRoot, public CacheForServiceRoot {
void loadFromDatabase();
private:
QList<QAction*> m_serviceMenu;
GmailNetworkFactory* m_network;
};
inline void GmailServiceRoot::setNetwork(GmailNetworkFactory* network) {

View File

@ -12,7 +12,8 @@
#include "services/gmail/gui/emailrecipientcontrol.h"
#include "services/gmail/network/gmailnetworkfactory.h"
FormAddEditEmail::FormAddEditEmail(GmailServiceRoot* root, QWidget* parent) : QDialog(parent), m_root(root) {
FormAddEditEmail::FormAddEditEmail(GmailServiceRoot* root, QWidget* parent)
: QDialog(parent), m_root(root), m_originalMessage(nullptr) {
m_ui.setupUi(this);
GuiUtilities::applyDialogProperties(*this, qApp->icons()->fromTheme(QSL("mail-message-new")));
@ -39,6 +40,14 @@ void FormAddEditEmail::execForAdd() {
exec();
}
void FormAddEditEmail::execForReply(Message* original_message) {
m_originalMessage = original_message;
addRecipientRow(m_originalMessage->m_author);
m_ui.m_txtSubject->setText(QSL("Re:%1").arg(m_originalMessage->m_title));
exec();
}
void FormAddEditEmail::removeRecipientRow() {
auto* sndr = static_cast<EmailRecipientControl*>(sender());

View File

@ -23,6 +23,7 @@ class FormAddEditEmail : public QDialog {
public slots:
void execForAdd();
void execForReply(Message* original_message);
private slots:
void removeRecipientRow();

View File

@ -82,6 +82,10 @@ void InoreaderServiceRoot::saveAccountDataToDatabase() {
}
}
bool InoreaderServiceRoot::isSyncable() const {
return true;
}
bool InoreaderServiceRoot::canBeEdited() const {
return true;
}
@ -119,17 +123,6 @@ void InoreaderServiceRoot::stop() {
saveCacheToFile(accountId());
}
QList<QAction*> InoreaderServiceRoot::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, &InoreaderServiceRoot::syncIn);
m_serviceMenu.append(act_sync_in);
}
return m_serviceMenu;
}
QString InoreaderServiceRoot::code() const {
return InoreaderEntryPoint().code();
}

View File

@ -20,6 +20,7 @@ class InoreaderServiceRoot : public ServiceRoot, public CacheForServiceRoot {
void setNetwork(InoreaderNetworkFactory* network);
InoreaderNetworkFactory* network() const;
bool isSyncable() const;
bool canBeEdited() const;
bool editViaGui();
bool canBeDeleted() const;
@ -43,10 +44,8 @@ class InoreaderServiceRoot : public ServiceRoot, public CacheForServiceRoot {
private:
void loadFromDatabase();
QList<QAction*> serviceMenu();
private:
QList<QAction*> m_serviceMenu;
InoreaderNetworkFactory* m_network;
};

View File

@ -17,7 +17,7 @@
#include "services/owncloud/owncloudserviceentrypoint.h"
OwnCloudServiceRoot::OwnCloudServiceRoot(RootItem* parent)
: ServiceRoot(parent), m_actionSyncIn(nullptr), m_network(new OwnCloudNetworkFactory()) {
: ServiceRoot(parent), m_network(new OwnCloudNetworkFactory()) {
setIcon(OwnCloudServiceEntryPoint().icon());
}
@ -25,6 +25,10 @@ OwnCloudServiceRoot::~OwnCloudServiceRoot() {
delete m_network;
}
bool OwnCloudServiceRoot::isSyncable() const {
return true;
}
bool OwnCloudServiceRoot::canBeEdited() const {
return true;
}
@ -59,16 +63,6 @@ bool OwnCloudServiceRoot::supportsCategoryAdding() const {
return false;
}
QList<QAction*> OwnCloudServiceRoot::serviceMenu() {
if (m_serviceMenu.isEmpty()) {
m_actionSyncIn = new QAction(qApp->icons()->fromTheme(QSL("view-refresh")), tr("Sync in"), this);
connect(m_actionSyncIn, &QAction::triggered, this, &OwnCloudServiceRoot::syncIn);
m_serviceMenu.append(m_actionSyncIn);
}
return m_serviceMenu;
}
void OwnCloudServiceRoot::start(bool freshly_activated) {
Q_UNUSED(freshly_activated)
loadFromDatabase();

View File

@ -18,13 +18,13 @@ class OwnCloudServiceRoot : public ServiceRoot, public CacheForServiceRoot {
explicit OwnCloudServiceRoot(RootItem* parent = nullptr);
virtual ~OwnCloudServiceRoot();
bool isSyncable() const;
bool canBeEdited() const;
bool canBeDeleted() const;
bool editViaGui();
bool deleteViaGui();
bool supportsFeedAdding() const;
bool supportsCategoryAdding() const;
QList<QAction*> serviceMenu();
void start(bool freshly_activated);
void stop();
@ -42,12 +42,8 @@ class OwnCloudServiceRoot : public ServiceRoot, public CacheForServiceRoot {
private:
RootItem* obtainNewTreeForSyncIn() const;
void loadFromDatabase();
QAction* m_actionSyncIn;
QList<QAction*> m_serviceMenu;
OwnCloudNetworkFactory* m_network;
};

View File

@ -77,6 +77,7 @@ StandardServiceRoot* StandardFeed::serviceRoot() const {
bool StandardFeed::editViaGui() {
QScopedPointer<FormStandardFeedDetails> form_pointer(new FormStandardFeedDetails(serviceRoot(), qApp->mainFormWidget()));
form_pointer.data()->addEditFeed(this, nullptr);
return false;
}
@ -111,7 +112,7 @@ QString StandardFeed::typeToString(StandardFeed::Type type) {
void StandardFeed::fetchMetadataForItself() {
QPair<StandardFeed*, QNetworkReply::NetworkError> metadata = guessFeed(url(), username(), password());
if (metadata.first != nullptr && metadata.second == QNetworkReply::NoError) {
if (metadata.first != nullptr && metadata.second == QNetworkReply::NetworkError::NoError) {
// Some properties are not updated when new metadata are fetched.
metadata.first->setParent(parent());
metadata.first->setUrl(url());
@ -138,10 +139,11 @@ QPair<StandardFeed*, QNetworkReply::NetworkError> StandardFeed::guessFeed(const
const QString& username,
const QString& password) {
QPair<StandardFeed*, QNetworkReply::NetworkError> result;
result.first = nullptr;
QByteArray feed_contents;
QList<QPair<QByteArray, QByteArray>> headers;
headers << NetworkFactory::generateBasicAuthHeader(username, password);
NetworkResult network_result = NetworkFactory::performNetworkOperation(url,
@ -208,8 +210,8 @@ QPair<StandardFeed*, QNetworkReply::NetworkError> StandardFeed::guessFeed(const
QDomElement root_element = xml_document.documentElement();
QString root_tag_name = root_element.tagName();
QList<QString> icon_possible_locations;
icon_possible_locations.append(url);
if (root_tag_name == QL1S("rdf:RDF")) {
@ -308,8 +310,8 @@ bool StandardFeed::addItself(RootItem* parent) {
QSqlDatabase database = qApp->database()->connection(metaObject()->className());
bool ok;
int new_id = DatabaseQueries::addStandardFeed(database, parent->id(), parent->getParentServiceRoot()->accountId(), title(),
description(), creationDate(), icon(), encoding(), url(), passwordProtected(),
username(), password(), autoUpdateType(), autoUpdateInitialInterval(), type(), &ok);
description(), creationDate(), icon(), encoding(), url(), passwordProtected(),
username(), password(), autoUpdateType(), autoUpdateInitialInterval(), type(), &ok);
if (!ok) {
// Query failed.
@ -329,12 +331,13 @@ bool StandardFeed::editItself(StandardFeed* new_feed_data) {
RootItem* new_parent = new_feed_data->parent();
if (!DatabaseQueries::editStandardFeed(database, new_parent->id(), original_feed->id(), new_feed_data->title(),
new_feed_data->description(), new_feed_data->icon(),
new_feed_data->encoding(), new_feed_data->url(), new_feed_data->passwordProtected(),
new_feed_data->username(), new_feed_data->password(),
new_feed_data->autoUpdateType(), new_feed_data->autoUpdateInitialInterval(),
new_feed_data->type())) {
new_feed_data->description(), new_feed_data->icon(),
new_feed_data->encoding(), new_feed_data->url(), new_feed_data->passwordProtected(),
new_feed_data->username(), new_feed_data->password(),
new_feed_data->autoUpdateType(), new_feed_data->autoUpdateInitialInterval(),
new_feed_data->type())) {
// Persistent storage update failed, no way to continue now.
qWarning("Self-editing of standard feed failed.");
return false;
}
@ -399,8 +402,8 @@ void StandardFeed::setEncoding(const QString& encoding) {
QList<Message> StandardFeed::obtainNewMessages(bool* error_during_obtaining) {
QByteArray feed_contents;
int download_timeout = qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::UpdateTimeout)).toInt();
QList<QPair<QByteArray, QByteArray>> headers;
headers << NetworkFactory::generateBasicAuthHeader(username(), password());
m_networkError = NetworkFactory::performNetworkOperation(url(),

View File

@ -27,15 +27,13 @@
#include <QStack>
StandardServiceRoot::StandardServiceRoot(RootItem* parent)
: ServiceRoot(parent),
m_actionExportFeeds(nullptr), m_actionImportFeeds(nullptr), m_actionFeedFetchMetadata(nullptr) {
: ServiceRoot(parent) {
setTitle(qApp->system()->loggedInUser() + QSL(" (RSS/RDF/ATOM)"));
setIcon(StandardServiceEntryPoint().icon());
setDescription(tr("This is obligatory service account for standard RSS/RDF/ATOM feeds."));
}
StandardServiceRoot::~StandardServiceRoot() {
qDeleteAll(m_serviceMenu);
qDeleteAll(m_feedContextMenu);
}
@ -176,13 +174,19 @@ void StandardServiceRoot::checkArgumentForFeedAdding(const QString& argument) {
QList<QAction*> StandardServiceRoot::getContextMenuForFeed(StandardFeed* feed) {
if (m_feedContextMenu.isEmpty()) {
// Initialize.
m_actionFeedFetchMetadata = new QAction(qApp->icons()->fromTheme(QSL("emblem-downloads")), tr("Fetch metadata"), nullptr);
m_feedContextMenu.append(m_actionFeedFetchMetadata);
auto* action_metadata = new QAction(qApp->icons()->fromTheme(QSL("emblem-downloads")),
tr("Fetch metadata"),
this);
m_feedContextMenu.append(action_metadata);
connect(action_metadata, &QAction::triggered, this, [this]() {
m_feedForMetadata->fetchMetadataForItself();
});
}
// Make connections.
disconnect(m_actionFeedFetchMetadata, &QAction::triggered, nullptr, nullptr);
connect(m_actionFeedFetchMetadata, &QAction::triggered, feed, &StandardFeed::fetchMetadataForItself);
m_feedForMetadata = feed;
return m_feedContextMenu;
}
@ -306,12 +310,16 @@ void StandardServiceRoot::exportFeeds() {
QList<QAction*> StandardServiceRoot::serviceMenu() {
if (m_serviceMenu.isEmpty()) {
m_actionExportFeeds = new QAction(qApp->icons()->fromTheme("document-export"), tr("Export feeds"), this);
m_actionImportFeeds = new QAction(qApp->icons()->fromTheme("document-import"), tr("Import feeds"), this);
connect(m_actionExportFeeds, &QAction::triggered, this, &StandardServiceRoot::exportFeeds);
connect(m_actionImportFeeds, &QAction::triggered, this, &StandardServiceRoot::importFeeds);
m_serviceMenu.append(m_actionExportFeeds);
m_serviceMenu.append(m_actionImportFeeds);
ServiceRoot::serviceMenu();
auto* action_export_feeds = new QAction(qApp->icons()->fromTheme("document-export"), tr("Export feeds"), this);
auto* action_import_feeds = new QAction(qApp->icons()->fromTheme("document-import"), tr("Import feeds"), this);
connect(action_export_feeds, &QAction::triggered, this, &StandardServiceRoot::exportFeeds);
connect(action_import_feeds, &QAction::triggered, this, &StandardServiceRoot::importFeeds);
m_serviceMenu.append(action_export_feeds);
m_serviceMenu.append(action_import_feeds);
}
return m_serviceMenu;

View File

@ -5,11 +5,12 @@
#include "services/abstract/serviceroot.h"
#include "services/standard/standardfeed.h"
#include <QCoreApplication>
#include <QPair>
class StandardCategory;
class StandardFeed;
class FeedsImportExportModel;
class QMenu;
@ -58,12 +59,8 @@ class StandardServiceRoot : public ServiceRoot {
QString processFeedUrl(const QString& feed_url);
void checkArgumentsForFeedAdding();
QAction* m_actionExportFeeds;
QAction* m_actionImportFeeds;
QList<QAction*> m_serviceMenu;
QList<QAction*> m_feedContextMenu;
QAction* m_actionFeedFetchMetadata;
QPointer<StandardFeed> m_feedForMetadata = {};
QList<QAction*> m_feedContextMenu = {};
};
#endif // STANDARDSERVICEROOT_H

View File

@ -23,7 +23,7 @@
#include <QSqlTableModel>
TtRssServiceRoot::TtRssServiceRoot(RootItem* parent)
: ServiceRoot(parent), m_actionSyncIn(nullptr), m_network(new TtRssNetworkFactory()) {
: ServiceRoot(parent), m_network(new TtRssNetworkFactory()) {
setIcon(TtRssServiceEntryPoint().icon());
}
@ -52,6 +52,10 @@ QString TtRssServiceRoot::code() const {
return TtRssServiceEntryPoint().code();
}
bool TtRssServiceRoot::isSyncable() const {
return true;
}
bool TtRssServiceRoot::editViaGui() {
QScopedPointer<FormEditTtRssAccount> form_pointer(new FormEditTtRssAccount(qApp->mainFormWidget()));
@ -148,16 +152,6 @@ void TtRssServiceRoot::saveAllCachedData(bool async) {
}
}
QList<QAction*> TtRssServiceRoot::serviceMenu() {
if (m_serviceMenu.isEmpty()) {
m_actionSyncIn = new QAction(qApp->icons()->fromTheme(QSL("view-refresh")), tr("Sync in"), this);
connect(m_actionSyncIn, &QAction::triggered, this, &TtRssServiceRoot::syncIn);
m_serviceMenu.append(m_actionSyncIn);
}
return m_serviceMenu;
}
QString TtRssServiceRoot::additionalTooltip() const {
return tr("Username: %1\nServer: %2\n"
"Last error: %3\nLast login on: %4").arg(m_network->username(),

View File

@ -22,13 +22,14 @@ class TtRssServiceRoot : public ServiceRoot, public CacheForServiceRoot {
void start(bool freshly_activated);
void stop();
QString code() const;
bool isSyncable() const;
bool canBeEdited() const;
bool canBeDeleted() const;
bool editViaGui();
bool deleteViaGui();
bool supportsFeedAdding() const;
bool supportsCategoryAdding() const;
QList<QAction*> serviceMenu();
QString additionalTooltip() const;
@ -46,12 +47,8 @@ class TtRssServiceRoot : public ServiceRoot, public CacheForServiceRoot {
private:
RootItem* obtainNewTreeForSyncIn() const;
void loadFromDatabase();
QAction* m_actionSyncIn;
QList<QAction*> m_serviceMenu;
TtRssNetworkFactory* m_network;
};