rssguard/src/librssguard/services/standard/standardserviceroot.cpp

311 lines
11 KiB
C++
Raw Normal View History

// For license of this file, see <project-root-folder>/LICENSE.md.
#include "services/standard/standardserviceroot.h"
2017-09-19 10:18:21 +02:00
#include "core/feedsmodel.h"
#include "definitions/definitions.h"
2017-09-19 10:18:21 +02:00
#include "exceptions/applicationexception.h"
#include "gui/messagebox.h"
#include "miscellaneous/application.h"
#include "miscellaneous/databasequeries.h"
2017-09-19 10:18:21 +02:00
#include "miscellaneous/iconfactory.h"
#include "miscellaneous/mutex.h"
2017-09-19 10:18:21 +02:00
#include "miscellaneous/settings.h"
#include "services/abstract/recyclebin.h"
#include "services/standard/gui/formstandardcategorydetails.h"
#include "services/standard/gui/formstandardfeeddetails.h"
#include "services/standard/gui/formstandardimportexport.h"
2017-09-19 10:18:21 +02:00
#include "services/standard/standardcategory.h"
#include "services/standard/standardfeed.h"
#include "services/standard/standardfeedsimportexportmodel.h"
#include "services/standard/standardserviceentrypoint.h"
#include <QAction>
#include <QClipboard>
2017-09-19 10:18:21 +02:00
#include <QSqlTableModel>
#include <QStack>
StandardServiceRoot::StandardServiceRoot(RootItem* parent)
2017-09-20 08:10:51 +02:00
: ServiceRoot(parent),
2019-05-28 07:19:19 +02:00
m_actionExportFeeds(nullptr), m_actionImportFeeds(nullptr), m_actionFeedFetchMetadata(nullptr) {
2017-09-23 22:15:56 +02:00
setTitle(qApp->system()->loggedInUser() + QSL(" (RSS/RDF/ATOM)"));
2017-09-19 10:18:21 +02:00
setIcon(StandardServiceEntryPoint().icon());
setDescription(tr("This is obligatory service account for standard RSS/RDF/ATOM feeds."));
}
StandardServiceRoot::~StandardServiceRoot() {
2017-09-19 10:18:21 +02:00
qDeleteAll(m_serviceMenu);
qDeleteAll(m_feedContextMenu);
}
void StandardServiceRoot::start(bool freshly_activated) {
2017-09-19 10:18:21 +02:00
loadFromDatabase();
if (freshly_activated && getSubTree(RootItemKind::Feed).isEmpty()) {
// In other words, if there are no feeds or categories added.
if (MessageBox::show(qApp->mainFormWidget(), QMessageBox::Question, QObject::tr("Load initial set of feeds"),
tr("This new account does not include any feeds. You can now add default set of feeds."),
tr("Do you want to load initial set of feeds?"),
QString(), QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
QString target_opml_file = APP_INITIAL_FEEDS_PATH + QDir::separator() + FEED_INITIAL_OPML_PATTERN;
QString current_locale = qApp->localization()->loadedLanguage();
QString file_to_load;
if (QFile::exists(target_opml_file.arg(current_locale))) {
file_to_load = target_opml_file.arg(current_locale);
}
else if (QFile::exists(target_opml_file.arg(DEFAULT_LOCALE))) {
file_to_load = target_opml_file.arg(DEFAULT_LOCALE);
}
FeedsImportExportModel model;
QString output_msg;
try {
2017-10-23 13:04:57 +02:00
model.importAsOPML20(IOFactory::readFile(file_to_load), false);
2017-09-19 10:18:21 +02:00
model.checkAllItems();
if (mergeImportExportModel(&model, this, output_msg)) {
requestItemExpand(getSubTree(), true);
}
}
catch (ApplicationException& ex) {
MessageBox::show(qApp->mainFormWidget(), QMessageBox::Critical, tr("Error when loading initial feeds"), ex.message());
}
}
}
checkArgumentsForFeedAdding();
}
void StandardServiceRoot::stop() {
2017-09-19 10:18:21 +02:00
qDebug("Stopping StandardServiceRoot instance.");
}
QString StandardServiceRoot::code() const {
2017-09-19 10:18:21 +02:00
return StandardServiceEntryPoint().code();
}
bool StandardServiceRoot::canBeEdited() const {
2017-09-19 10:18:21 +02:00
return false;
}
bool StandardServiceRoot::canBeDeleted() const {
2017-09-19 10:18:21 +02:00
return true;
}
bool StandardServiceRoot::deleteViaGui() {
2017-09-19 10:18:21 +02:00
return ServiceRoot::deleteViaGui();
}
bool StandardServiceRoot::supportsFeedAdding() const {
2017-09-19 10:18:21 +02:00
return true;
}
bool StandardServiceRoot::supportsCategoryAdding() const {
2017-09-19 10:18:21 +02:00
return true;
}
void StandardServiceRoot::addNewFeed(const QString& url) {
2017-09-19 10:18:21 +02:00
if (!qApp->feedUpdateLock()->tryLock()) {
// Lock was not obtained because
// it is used probably by feed updater or application
// is quitting.
qApp->showGuiMessage(tr("Cannot add item"),
tr("Cannot add feed because another critical operation is ongoing."),
QSystemTrayIcon::Warning, qApp->mainFormWidget(), true);
// Thus, cannot delete and quit the method.
return;
}
QScopedPointer<FormStandardFeedDetails> form_pointer(new FormStandardFeedDetails(this, qApp->mainFormWidget()));
form_pointer.data()->addEditFeed(nullptr, nullptr, url);
qApp->feedUpdateLock()->unlock();
}
Qt::ItemFlags StandardServiceRoot::additionalFlags() const {
2017-09-19 10:18:21 +02:00
return Qt::ItemIsDropEnabled;
}
void StandardServiceRoot::loadFromDatabase() {
2019-04-12 07:12:13 +02:00
QSqlDatabase database = qApp->database()->connection(metaObject()->className());
2017-09-20 15:11:17 +02:00
Assignment categories = DatabaseQueries::getStandardCategories(database, accountId());
Assignment feeds = DatabaseQueries::getStandardFeeds(database, accountId());
2017-09-19 10:18:21 +02:00
// 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());
updateCounts(true);
}
void StandardServiceRoot::checkArgumentsForFeedAdding() {
for (const QString& arg : qApp->arguments().mid(1)) {
2017-09-19 10:18:21 +02:00
checkArgumentForFeedAdding(arg);
}
}
QString StandardServiceRoot::processFeedUrl(const QString& feed_url) {
2017-09-19 10:18:21 +02:00
if (feed_url.startsWith(QL1S(URI_SCHEME_FEED_SHORT))) {
QString without_feed_prefix = feed_url.mid(5);
if (without_feed_prefix.startsWith(QL1S("https:")) || without_feed_prefix.startsWith(QL1S("http:"))) {
return without_feed_prefix;
}
else {
return feed_url;
}
}
else {
return feed_url;
}
}
void StandardServiceRoot::checkArgumentForFeedAdding(const QString& argument) {
2017-09-19 10:18:21 +02:00
if (argument.startsWith(QL1S(URI_SCHEME_FEED_SHORT))) {
addNewFeed(processFeedUrl(argument));
}
}
QList<QAction*> StandardServiceRoot::getContextMenuForFeed(StandardFeed* feed) {
2017-09-19 10:18:21 +02:00
if (m_feedContextMenu.isEmpty()) {
// Initialize.
m_actionFeedFetchMetadata = new QAction(qApp->icons()->fromTheme(QSL("emblem-downloads")), tr("Fetch metadata"), nullptr);
m_feedContextMenu.append(m_actionFeedFetchMetadata);
}
// Make connections.
2019-04-12 07:12:13 +02:00
disconnect(m_actionFeedFetchMetadata, &QAction::triggered, nullptr, nullptr);
2017-09-19 10:18:21 +02:00
connect(m_actionFeedFetchMetadata, &QAction::triggered, feed, &StandardFeed::fetchMetadataForItself);
return m_feedContextMenu;
}
bool StandardServiceRoot::mergeImportExportModel(FeedsImportExportModel* model, RootItem* target_root_node, QString& output_message) {
2017-09-19 10:18:21 +02:00
QStack<RootItem*> original_parents;
original_parents.push(target_root_node);
QStack<RootItem*> new_parents;
new_parents.push(model->rootItem());
bool some_feed_category_error = false;
// Iterate all new items we would like to merge into current model.
while (!new_parents.isEmpty()) {
RootItem* target_parent = original_parents.pop();
RootItem* source_parent = new_parents.pop();
for (RootItem* source_item : source_parent->childItems()) {
2017-09-19 10:18:21 +02:00
if (!model->isItemChecked(source_item)) {
// We can skip this item, because it is not checked and should not be imported.
// NOTE: All descendants are thus skipped too.
continue;
}
if (source_item->kind() == RootItemKind::Category) {
2019-05-28 07:19:19 +02:00
auto* source_category = dynamic_cast<StandardCategory*>(source_item);
auto* new_category = new StandardCategory(*source_category);
2017-09-19 10:18:21 +02:00
QString new_category_title = new_category->title();
// Add category to model.
new_category->clearChildren();
if (new_category->addItself(target_parent)) {
requestItemReassignment(new_category, target_parent);
// Process all children of this category.
original_parents.push(new_category);
new_parents.push(source_category);
}
else {
delete new_category;
// Add category failed, but this can mean that the same category (with same title)
// already exists. If such a category exists in current parent, then find it and
// add descendants to it.
RootItem* existing_category = nullptr;
for (RootItem* child : target_parent->childItems()) {
2017-09-19 10:18:21 +02:00
if (child->kind() == RootItemKind::Category && child->title() == new_category_title) {
existing_category = child;
}
}
if (existing_category != nullptr) {
original_parents.push(existing_category);
new_parents.push(source_category);
}
else {
some_feed_category_error = true;
}
}
}
else if (source_item->kind() == RootItemKind::Feed) {
2019-05-28 07:19:19 +02:00
auto* source_feed = dynamic_cast<StandardFeed*>(source_item);
auto* new_feed = new StandardFeed(*source_feed);
2017-09-19 10:18:21 +02:00
// Append this feed and end this iteration.
if (new_feed->addItself(target_parent)) {
requestItemReassignment(new_feed, target_parent);
}
else {
delete new_feed;
some_feed_category_error = true;
}
}
}
}
if (some_feed_category_error) {
output_message = tr("Import successful, but some feeds/categories were not imported due to error.");
}
else {
output_message = tr("Import was completely successful.");
}
return !some_feed_category_error;
}
void StandardServiceRoot::addNewCategory() {
2017-09-19 10:18:21 +02:00
if (!qApp->feedUpdateLock()->tryLock()) {
// Lock was not obtained because
// it is used probably by feed updater or application
// is quitting.
qApp->showGuiMessage(tr("Cannot add category"),
tr("Cannot add category because another critical operation is ongoing."),
QSystemTrayIcon::Warning, qApp->mainFormWidget(), true);
// Thus, cannot delete and quit the method.
return;
}
QScopedPointer<FormStandardCategoryDetails> form_pointer(new FormStandardCategoryDetails(this, qApp->mainFormWidget()));
form_pointer.data()->addEditCategory(nullptr, nullptr);
qApp->feedUpdateLock()->unlock();
}
void StandardServiceRoot::importFeeds() {
2017-09-19 10:18:21 +02:00
QScopedPointer<FormStandardImportExport> form(new FormStandardImportExport(this, qApp->mainFormWidget()));
form.data()->setMode(FeedsImportExportModel::Import);
form.data()->exec();
}
void StandardServiceRoot::exportFeeds() {
2017-09-19 10:18:21 +02:00
QScopedPointer<FormStandardImportExport> form(new FormStandardImportExport(this, qApp->mainFormWidget()));
form.data()->setMode(FeedsImportExportModel::Export);
form.data()->exec();
}
QList<QAction*> StandardServiceRoot::serviceMenu() {
2017-09-19 10:18:21 +02:00
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);
}
return m_serviceMenu;
}