2017-10-25 07:20:47 +02:00
|
|
|
// For license of this file, see <project-root-folder>/LICENSE.md.
|
2017-07-21 06:53:23 +02:00
|
|
|
|
|
|
|
#include "services/standard/standardserviceroot.h"
|
|
|
|
|
2017-09-19 10:18:21 +02:00
|
|
|
#include "core/feedsmodel.h"
|
2017-07-21 06:53:23 +02:00
|
|
|
#include "definitions/definitions.h"
|
2017-09-19 10:18:21 +02:00
|
|
|
#include "exceptions/applicationexception.h"
|
|
|
|
#include "gui/messagebox.h"
|
2017-07-21 06:53:23 +02:00
|
|
|
#include "miscellaneous/application.h"
|
|
|
|
#include "miscellaneous/databasequeries.h"
|
2017-09-19 10:18:21 +02:00
|
|
|
#include "miscellaneous/iconfactory.h"
|
2017-07-21 06:53:23 +02:00
|
|
|
#include "miscellaneous/mutex.h"
|
2017-09-19 10:18:21 +02:00
|
|
|
#include "miscellaneous/settings.h"
|
2020-06-09 14:56:32 +02:00
|
|
|
#include "services/abstract/importantnode.h"
|
2017-09-19 10:18:21 +02:00
|
|
|
#include "services/abstract/recyclebin.h"
|
2017-07-21 06:53:23 +02:00
|
|
|
#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"
|
2017-07-21 06:53:23 +02:00
|
|
|
|
|
|
|
#include <QAction>
|
|
|
|
#include <QClipboard>
|
2017-09-19 10:18:21 +02:00
|
|
|
#include <QSqlTableModel>
|
|
|
|
#include <QStack>
|
2017-07-21 06:53:23 +02:00
|
|
|
|
|
|
|
StandardServiceRoot::StandardServiceRoot(RootItem* parent)
|
2020-07-27 10:54:09 +02:00
|
|
|
: ServiceRoot(parent) {
|
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."));
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
StandardServiceRoot::~StandardServiceRoot() {
|
2017-09-19 10:18:21 +02:00
|
|
|
qDeleteAll(m_feedContextMenu);
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StandardServiceRoot::start(bool freshly_activated) {
|
2017-09-19 10:18:21 +02:00
|
|
|
loadFromDatabase();
|
|
|
|
|
2020-07-31 11:24:33 +02:00
|
|
|
if (freshly_activated && getSubTree(RootItem::Kind::Feed).isEmpty()) {
|
2017-09-19 10:18:21 +02:00
|
|
|
// 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();
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StandardServiceRoot::stop() {
|
2020-08-21 11:36:55 +02:00
|
|
|
qDebugNN << LOGSEC_CORE << "Stopping StandardServiceRoot instance.";
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QString StandardServiceRoot::code() const {
|
2017-09-19 10:18:21 +02:00
|
|
|
return StandardServiceEntryPoint().code();
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StandardServiceRoot::canBeEdited() const {
|
2017-09-19 10:18:21 +02:00
|
|
|
return false;
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StandardServiceRoot::canBeDeleted() const {
|
2017-09-19 10:18:21 +02:00
|
|
|
return true;
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StandardServiceRoot::deleteViaGui() {
|
2017-09-19 10:18:21 +02:00
|
|
|
return ServiceRoot::deleteViaGui();
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StandardServiceRoot::supportsFeedAdding() const {
|
2017-09-19 10:18:21 +02:00
|
|
|
return true;
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StandardServiceRoot::supportsCategoryAdding() const {
|
2017-09-19 10:18:21 +02:00
|
|
|
return true;
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
2020-08-28 07:37:49 +02:00
|
|
|
void StandardServiceRoot::addNewFeed(RootItem* selected_item, 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;
|
|
|
|
}
|
|
|
|
|
2020-08-28 07:37:49 +02:00
|
|
|
QScopedPointer<FormStandardFeedDetails> form_pointer(new FormStandardFeedDetails(this,
|
|
|
|
qApp->mainFormWidget()));
|
2020-06-09 14:56:32 +02:00
|
|
|
|
2020-08-28 07:37:49 +02:00
|
|
|
form_pointer->addEditFeed(nullptr, selected_item, url);
|
2017-09-19 10:18:21 +02:00
|
|
|
qApp->feedUpdateLock()->unlock();
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags StandardServiceRoot::additionalFlags() const {
|
2017-09-19 10:18:21 +02:00
|
|
|
return Qt::ItemIsDropEnabled;
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StandardServiceRoot::loadFromDatabase() {
|
2019-04-12 07:12:13 +02:00
|
|
|
QSqlDatabase database = qApp->database()->connection(metaObject()->className());
|
2020-06-23 13:55:00 +02:00
|
|
|
Assignment categories = DatabaseQueries::getCategories<StandardCategory>(database, accountId());
|
2020-06-24 14:12:03 +02:00
|
|
|
Assignment feeds = DatabaseQueries::getFeeds<StandardFeed>(database, qApp->feedReader()->messageFilters(), 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());
|
2020-06-09 14:56:32 +02:00
|
|
|
appendChild(importantNode());
|
2017-09-19 10:18:21 +02:00
|
|
|
updateCounts(true);
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StandardServiceRoot::checkArgumentsForFeedAdding() {
|
2020-06-03 09:54:52 +02:00
|
|
|
for (const QString& arg : qApp->arguments().mid(1)) {
|
2017-09-19 10:18:21 +02:00
|
|
|
checkArgumentForFeedAdding(arg);
|
|
|
|
}
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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;
|
|
|
|
}
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StandardServiceRoot::checkArgumentForFeedAdding(const QString& argument) {
|
2017-09-19 10:18:21 +02:00
|
|
|
if (argument.startsWith(QL1S(URI_SCHEME_FEED_SHORT))) {
|
2020-08-28 07:37:49 +02:00
|
|
|
addNewFeed(nullptr, processFeedUrl(argument));
|
2017-09-19 10:18:21 +02:00
|
|
|
}
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QList<QAction*> StandardServiceRoot::getContextMenuForFeed(StandardFeed* feed) {
|
2017-09-19 10:18:21 +02:00
|
|
|
if (m_feedContextMenu.isEmpty()) {
|
|
|
|
// Initialize.
|
2020-07-27 10:54:09 +02:00
|
|
|
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();
|
|
|
|
});
|
2017-09-19 10:18:21 +02:00
|
|
|
}
|
|
|
|
|
2020-07-27 10:54:09 +02:00
|
|
|
m_feedForMetadata = feed;
|
|
|
|
|
2017-09-19 10:18:21 +02:00
|
|
|
return m_feedContextMenu;
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool StandardServiceRoot::mergeImportExportModel(FeedsImportExportModel* model, RootItem* target_root_node, QString& output_message) {
|
2017-09-19 10:18:21 +02:00
|
|
|
QStack<RootItem*> original_parents;
|
2020-06-09 14:56:32 +02:00
|
|
|
|
2017-09-19 10:18:21 +02:00
|
|
|
original_parents.push(target_root_node);
|
|
|
|
QStack<RootItem*> new_parents;
|
2020-06-09 14:56:32 +02:00
|
|
|
|
2017-09-19 10:18:21 +02:00
|
|
|
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();
|
|
|
|
|
2020-06-03 09:54:52 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-31 11:24:33 +02:00
|
|
|
if (source_item->kind() == RootItem::Kind::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;
|
|
|
|
|
2020-06-03 09:54:52 +02:00
|
|
|
for (RootItem* child : target_parent->childItems()) {
|
2020-07-31 11:24:33 +02:00
|
|
|
if (child->kind() == RootItem::Kind::Category && child->title() == new_category_title) {
|
2017-09-19 10:18:21 +02:00
|
|
|
existing_category = child;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (existing_category != nullptr) {
|
|
|
|
original_parents.push(existing_category);
|
|
|
|
new_parents.push(source_category);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
some_feed_category_error = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-31 11:24:33 +02:00
|
|
|
else if (source_item->kind() == RootItem::Kind::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;
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
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()));
|
2020-06-09 14:56:32 +02:00
|
|
|
|
2017-09-19 10:18:21 +02:00
|
|
|
form_pointer.data()->addEditCategory(nullptr, nullptr);
|
|
|
|
qApp->feedUpdateLock()->unlock();
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StandardServiceRoot::importFeeds() {
|
2017-09-19 10:18:21 +02:00
|
|
|
QScopedPointer<FormStandardImportExport> form(new FormStandardImportExport(this, qApp->mainFormWidget()));
|
2020-06-09 14:56:32 +02:00
|
|
|
|
2020-07-01 10:13:44 +02:00
|
|
|
form.data()->setMode(FeedsImportExportModel::Mode::Import);
|
2017-09-19 10:18:21 +02:00
|
|
|
form.data()->exec();
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StandardServiceRoot::exportFeeds() {
|
2017-09-19 10:18:21 +02:00
|
|
|
QScopedPointer<FormStandardImportExport> form(new FormStandardImportExport(this, qApp->mainFormWidget()));
|
2020-06-09 14:56:32 +02:00
|
|
|
|
2020-07-01 10:13:44 +02:00
|
|
|
form.data()->setMode(FeedsImportExportModel::Mode::Export);
|
2017-09-19 10:18:21 +02:00
|
|
|
form.data()->exec();
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
QList<QAction*> StandardServiceRoot::serviceMenu() {
|
2017-09-19 10:18:21 +02:00
|
|
|
if (m_serviceMenu.isEmpty()) {
|
2020-07-27 10:54:09 +02:00
|
|
|
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);
|
2017-09-19 10:18:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return m_serviceMenu;
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|