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

452 lines
17 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"
2021-03-09 07:10:36 +01:00
#include "database/databasequeries.h"
#include "definitions/definitions.h"
2017-09-19 10:18:21 +02:00
#include "exceptions/applicationexception.h"
2021-06-25 13:36:23 +02:00
#include "exceptions/feedfetchexception.h"
2021-03-03 12:05:25 +01:00
#include "exceptions/scriptexception.h"
2017-09-19 10:18:21 +02:00
#include "gui/messagebox.h"
#include "miscellaneous/application.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"
2021-03-03 12:05:25 +01:00
#include "network-web/networkfactory.h"
2021-03-30 08:55:15 +02:00
#include "network-web/webfactory.h"
2021-03-02 14:30:13 +01:00
#include "services/abstract/gui/formcategorydetails.h"
#include "services/abstract/importantnode.h"
2020-10-06 19:58:21 +02:00
#include "services/abstract/labelsnode.h"
2017-09-19 10:18:21 +02:00
#include "services/abstract/recyclebin.h"
#include "services/standard/definitions.h"
2021-01-20 14:23:58 +01:00
#include "services/standard/gui/formeditstandardaccount.h"
#include "services/standard/gui/formstandardfeeddetails.h"
#include "services/standard/gui/formstandardimportexport.h"
2022-02-05 09:29:32 +01:00
#include "services/standard/parsers/atomparser.h"
#include "services/standard/parsers/jsonparser.h"
#include "services/standard/parsers/rdfparser.h"
#include "services/standard/parsers/rssparser.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>
2021-03-03 12:05:25 +01:00
#include <QTextCodec>
StandardServiceRoot::StandardServiceRoot(RootItem* parent)
2020-07-27 10:54:09 +02:00
: ServiceRoot(parent) {
2021-02-21 20:53:26 +01:00
setTitle(qApp->system()->loggedInUser() + QSL(" (RSS/ATOM/JSON)"));
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_feedContextMenu);
}
void StandardServiceRoot::start(bool freshly_activated) {
2021-02-26 14:26:53 +01:00
DatabaseQueries::loadFromDatabase<StandardCategory, StandardFeed>(this);
2017-09-19 10:18:21 +02:00
if (freshly_activated && getSubTreeFeeds().isEmpty()) {
2017-09-19 10:18:21 +02:00
// In other words, if there are no feeds or categories added.
if (MsgBox::show(qApp->mainFormWidget(), QMessageBox::Question, QObject::tr("Load initial set of feeds"),
2017-09-19 10:18:21 +02:00
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);
}
2021-09-13 12:45:07 +02:00
else if (QFile::exists(target_opml_file.arg(QSL(DEFAULT_LOCALE)))) {
file_to_load = target_opml_file.arg(QSL(DEFAULT_LOCALE));
2017-09-19 10:18:21 +02:00
}
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) {
MsgBox::show(qApp->mainFormWidget(), QMessageBox::Critical, tr("Error when loading initial feeds"), ex.message());
2017-09-19 10:18:21 +02:00
}
}
2021-03-09 07:10:36 +01:00
else {
requestItemExpand({ this }, true);
}
2017-09-19 10:18:21 +02:00
}
}
void StandardServiceRoot::stop() {
2020-08-21 11:36:55 +02:00
qDebugNN << LOGSEC_CORE << "Stopping StandardServiceRoot instance.";
}
QString StandardServiceRoot::code() const {
2017-09-19 10:18:21 +02:00
return StandardServiceEntryPoint().code();
}
bool StandardServiceRoot::canBeEdited() const {
2021-01-20 14:23:58 +01:00
return true;
}
2021-01-20 14:23:58 +01:00
bool StandardServiceRoot::editViaGui() {
FormEditStandardAccount form_pointer(qApp->mainFormWidget());
form_pointer.addEditAccount(this);
return true;
}
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(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(Notification::Event::GeneralEvent, {
tr("Cannot add item"),
tr("Cannot add feed because another critical operation is ongoing."),
QSystemTrayIcon::MessageIcon::Warning });
2017-09-19 10:18:21 +02:00
return;
}
QScopedPointer<FormStandardFeedDetails> form_pointer(new FormStandardFeedDetails(this,
2021-03-02 12:02:07 +01:00
selected_item,
url,
qApp->mainFormWidget()));
2021-03-02 12:02:07 +01:00
form_pointer->addEditFeed<StandardFeed>();
2017-09-19 10:18:21 +02:00
qApp->feedUpdateLock()->unlock();
}
Qt::ItemFlags StandardServiceRoot::additionalFlags() const {
2021-01-21 14:29:13 +01:00
return Qt::ItemFlag::ItemIsDropEnabled;
}
QList<Message> StandardServiceRoot::obtainNewMessages(Feed* feed,
const QHash<ServiceRoot::BagOfMessages, QStringList>& stated_messages,
2021-07-14 14:46:45 +02:00
const QHash<QString, QStringList>& tagged_messages) {
Q_UNUSED(stated_messages)
Q_UNUSED(tagged_messages)
StandardFeed* f = static_cast<StandardFeed*>(feed);
QString formatted_feed_contents;
int download_timeout = qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::UpdateTimeout)).toInt();
if (f->sourceType() == StandardFeed::SourceType::Url) {
qDebugNN << LOGSEC_CORE
<< "Downloading URL"
<< QUOTE_W_SPACE(feed->source())
<< "to obtain feed data.";
QByteArray feed_contents;
QList<QPair<QByteArray, QByteArray>> headers;
headers << NetworkFactory::generateBasicAuthHeader(f->username(), f->password());
auto network_result = NetworkFactory::performNetworkOperation(feed->source(),
download_timeout,
{},
feed_contents,
QNetworkAccessManager::Operation::GetOperation,
headers,
false,
{},
{},
2022-02-18 12:35:38 +01:00
networkProxy()).m_networkError;
if (network_result != QNetworkReply::NetworkError::NoError) {
qWarningNN << LOGSEC_CORE
<< "Error"
<< QUOTE_W_SPACE(network_result)
<< "during fetching of new messages for feed"
<< QUOTE_W_SPACE_DOT(feed->source());
throw FeedFetchException(Feed::Status::NetworkError, NetworkFactory::networkErrorText(network_result));
}
2021-03-03 12:05:25 +01:00
// Encode downloaded data for further parsing.
QTextCodec* codec = QTextCodec::codecForName(f->encoding().toLocal8Bit());
2021-03-03 12:05:25 +01:00
if (codec == nullptr) {
// No suitable codec for this encoding was found.
// Use non-converted data.
formatted_feed_contents = feed_contents;
2021-03-03 12:05:25 +01:00
}
else {
formatted_feed_contents = codec->toUnicode(feed_contents);
}
}
else {
qDebugNN << LOGSEC_CORE
<< "Running custom script"
<< QUOTE_W_SPACE(feed->source())
<< "to obtain feed data.";
// Use script to generate feed file.
try {
formatted_feed_contents = StandardFeed::generateFeedFileWithScript(feed->source(), download_timeout);
2021-03-03 12:05:25 +01:00
}
catch (const ScriptException& ex) {
qCriticalNN << LOGSEC_CORE
<< "Custom script for generating feed file failed:"
<< QUOTE_W_SPACE_DOT(ex.message());
2021-03-03 12:05:25 +01:00
throw FeedFetchException(Feed::Status::OtherError, ex.message());
}
}
2021-03-03 12:05:25 +01:00
if (!f->postProcessScript().simplified().isEmpty()) {
qDebugNN << LOGSEC_CORE
<< "We will process feed data with post-process script"
<< QUOTE_W_SPACE_DOT(f->postProcessScript());
2021-03-03 12:05:25 +01:00
try {
formatted_feed_contents = StandardFeed::postProcessFeedFileWithScript(f->postProcessScript(),
formatted_feed_contents,
download_timeout);
2021-03-03 12:05:25 +01:00
}
catch (const ScriptException& ex) {
qCriticalNN << LOGSEC_CORE
<< "Post-processing script for feed file failed:"
<< QUOTE_W_SPACE_DOT(ex.message());
2021-03-03 12:05:25 +01:00
throw FeedFetchException(Feed::Status::OtherError, ex.message());
}
}
2021-03-03 12:05:25 +01:00
// Feed data are downloaded and encoded.
// Parse data and obtain messages.
QList<Message> messages;
2021-03-03 12:05:25 +01:00
switch (f->type()) {
case StandardFeed::Type::Rss0X:
case StandardFeed::Type::Rss2X:
messages = RssParser(formatted_feed_contents).messages();
break;
2021-03-03 12:05:25 +01:00
case StandardFeed::Type::Rdf:
messages = RdfParser(formatted_feed_contents).messages();
break;
2021-03-03 12:05:25 +01:00
case StandardFeed::Type::Atom10:
messages = AtomParser(formatted_feed_contents).messages();
break;
2021-03-03 12:05:25 +01:00
case StandardFeed::Type::Json:
messages = JsonParser(formatted_feed_contents).messages();
break;
2021-03-03 12:05:25 +01:00
default:
break;
}
2021-07-25 09:56:45 +02:00
for (Message& mess : messages) {
mess.m_feedId = feed->customId();
2021-03-03 12:05:25 +01:00
}
return messages;
2021-03-03 12:05:25 +01: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;
}
2021-03-10 06:19:48 +01: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;
2017-09-19 10:18:21 +02:00
original_parents.push(target_root_node);
QStack<RootItem*> new_parents;
new_parents.push(model->sourceModel()->rootItem());
2017-09-19 10:18:21 +02:00
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();
2021-03-22 09:45:25 +01:00
auto sour_chi = source_parent->childItems();
2017-09-19 10:18:21 +02:00
2021-03-22 09:45:25 +01:00
for (RootItem* source_item : qAsConst(sour_chi)) {
if (!model->sourceModel()->isItemChecked(source_item)) {
2017-09-19 10:18:21 +02:00
// 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() == RootItem::Kind::Category) {
2021-09-13 12:45:07 +02:00
auto* source_category = qobject_cast<StandardCategory*>(source_item);
2019-05-28 07:19:19 +02:00
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();
2021-03-05 12:46:03 +01:00
QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className());
2017-09-19 10:18:21 +02:00
2021-03-02 14:30:13 +01:00
try {
DatabaseQueries::createOverwriteCategory(database,
new_category,
target_root_node->getParentServiceRoot()->accountId(),
target_parent->id());
requestItemReassignment(new_category, target_parent);
2021-03-03 14:50:31 +01:00
original_parents.push(new_category);
new_parents.push(source_category);
2017-09-19 10:18:21 +02:00
}
2021-03-02 14:30:13 +01:00
catch (ApplicationException& ex) {
2017-09-19 10:18:21 +02:00
// 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;
2021-03-22 09:45:25 +01:00
auto tar_chi = target_parent->childItems();
2017-09-19 10:18:21 +02:00
2021-03-22 09:45:25 +01:00
for (RootItem* child : qAsConst(tar_chi)) {
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;
qCriticalNN << LOGSEC_CORE
<< "Cannot import category:"
<< QUOTE_W_SPACE_DOT(ex.message());
2017-09-19 10:18:21 +02:00
}
}
}
else if (source_item->kind() == RootItem::Kind::Feed) {
2021-09-13 12:45:07 +02:00
auto* source_feed = qobject_cast<StandardFeed*>(source_item);
2022-02-03 12:41:21 +01:00
const auto* feed_with_same_url = target_root_node->getItemFromSubTree([source_feed](const RootItem* it) {
return it->kind() == RootItem::Kind::Feed &&
2022-02-18 12:35:38 +01:00
it->toFeed()->source().toLower() == source_feed->source().toLower();
2022-02-03 12:41:21 +01:00
});
2022-02-03 12:41:21 +01:00
if (feed_with_same_url != nullptr) {
continue;
}
2019-05-28 07:19:19 +02:00
auto* new_feed = new StandardFeed(*source_feed);
2021-03-05 12:46:03 +01:00
QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className());
2017-09-19 10:18:21 +02:00
2021-03-10 06:19:48 +01:00
try {
DatabaseQueries::createOverwriteFeed(database,
new_feed,
target_root_node->getParentServiceRoot()->accountId(),
target_parent->id());
requestItemReassignment(new_feed, target_parent);
}
catch (const ApplicationException& ex) {
qCriticalNN << LOGSEC_CORE
<< "Cannot import feed:"
<< QUOTE_W_SPACE_DOT(ex.message());
some_feed_category_error = true;
}
2017-09-19 10:18:21 +02:00
}
}
}
if (some_feed_category_error) {
2021-03-10 06:19:48 +01:00
output_message = tr("Some feeds/categories were not imported due to error, check debug log for more details.");
2017-09-19 10:18:21 +02:00
}
else {
output_message = tr("Import was completely successful.");
}
return !some_feed_category_error;
}
void StandardServiceRoot::addNewCategory(RootItem* selected_item) {
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(Notification::Event::GeneralEvent, {
tr("Cannot add category"),
tr("Cannot add category because another critical operation is ongoing."),
QSystemTrayIcon::MessageIcon::Warning });
2017-09-19 10:18:21 +02:00
// Thus, cannot delete and quit the method.
return;
}
2021-03-02 14:30:13 +01:00
QScopedPointer<FormCategoryDetails> form_pointer(new FormCategoryDetails(this,
selected_item,
qApp->mainFormWidget()));
2021-03-02 14:30:13 +01:00
form_pointer->addEditCategory<StandardCategory>();
2017-09-19 10:18:21 +02:00
qApp->feedUpdateLock()->unlock();
}
void StandardServiceRoot::importFeeds() {
2017-09-19 10:18:21 +02:00
QScopedPointer<FormStandardImportExport> form(new FormStandardImportExport(this, qApp->mainFormWidget()));
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();
}
void StandardServiceRoot::exportFeeds() {
2017-09-19 10:18:21 +02:00
QScopedPointer<FormStandardImportExport> form(new FormStandardImportExport(this, qApp->mainFormWidget()));
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();
}
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();
2021-09-13 12:45:07 +02:00
auto* action_export_feeds = new QAction(qApp->icons()->fromTheme(QSL("document-export")), tr("Export feeds"), this);
auto* action_import_feeds = new QAction(qApp->icons()->fromTheme(QSL("document-import")), tr("Import feeds"), this);
2020-07-27 10:54:09 +02:00
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;
}