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"
|
|
|
|
|
2021-03-09 07:10:36 +01:00
|
|
|
#include "database/databasequeries.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"
|
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"
|
2017-07-21 06:53:23 +02:00
|
|
|
#include "miscellaneous/application.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"
|
2021-03-03 12:05:25 +01:00
|
|
|
#include "network-web/networkfactory.h"
|
2021-03-02 14:30:13 +01:00
|
|
|
#include "services/abstract/gui/formcategorydetails.h"
|
2021-02-25 19:59:08 +01:00
|
|
|
#include "services/standard/definitions.h"
|
2023-10-17 15:23:36 +02:00
|
|
|
#include "services/standard/gui/formdiscoverfeeds.h"
|
2021-01-20 14:23:58 +01:00
|
|
|
#include "services/standard/gui/formeditstandardaccount.h"
|
2017-07-21 06:53:23 +02:00
|
|
|
#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"
|
2023-10-16 15:05:42 +02:00
|
|
|
#include "services/standard/parsers/sitemapparser.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
|
|
|
|
2023-10-16 15:05:42 +02:00
|
|
|
#if defined(ENABLE_COMPRESSED_SITEMAP)
|
|
|
|
#include "3rd-party/qcompressor/qcompressor.h"
|
|
|
|
#endif
|
|
|
|
|
2017-07-21 06:53:23 +02:00
|
|
|
#include <QAction>
|
2017-09-19 10:18:21 +02:00
|
|
|
#include <QSqlTableModel>
|
|
|
|
#include <QStack>
|
2021-03-03 12:05:25 +01:00
|
|
|
#include <QTextCodec>
|
2017-07-21 06:53:23 +02:00
|
|
|
|
2023-03-16 07:37:49 +01:00
|
|
|
StandardServiceRoot::StandardServiceRoot(RootItem* parent) : ServiceRoot(parent) {
|
2017-09-19 10:18:21 +02:00
|
|
|
setIcon(StandardServiceEntryPoint().icon());
|
2023-08-11 12:22:51 +02:00
|
|
|
setDescription(tr("This is the 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
|
|
|
}
|
|
|
|
|
2023-10-24 08:35:37 +02:00
|
|
|
void StandardServiceRoot::onDatabaseCleanup() {
|
|
|
|
for (Feed* fd : getSubTreeFeeds()) {
|
|
|
|
qobject_cast<StandardFeed*>(fd)->setLastEtag({});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-21 06:53:23 +02:00
|
|
|
void StandardServiceRoot::start(bool freshly_activated) {
|
2022-03-01 14:45:20 +01:00
|
|
|
DatabaseQueries::loadRootFromDatabase<StandardCategory, StandardFeed>(this);
|
2017-09-19 10:18:21 +02:00
|
|
|
|
2021-02-26 13:50:47 +01: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.
|
2023-03-16 07:37:49 +01:00
|
|
|
if (MsgBox::show(qApp->mainFormWidget(),
|
|
|
|
QMessageBox::Question,
|
|
|
|
QObject::tr("Load initial set of feeds"),
|
2022-03-15 09:34:40 +01: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?"),
|
2023-03-16 07:37:49 +01:00
|
|
|
QString(),
|
|
|
|
QMessageBox::Yes | QMessageBox::No) == QMessageBox::Yes) {
|
2017-09-19 10:18:21 +02:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-04-05 11:24:31 +02:00
|
|
|
FeedsImportExportModel model(this);
|
2017-09-19 10:18:21 +02:00
|
|
|
QString output_msg;
|
|
|
|
|
|
|
|
try {
|
2023-07-24 07:50:51 +02:00
|
|
|
model.importAsOPML20(IOFactory::readFile(file_to_load), false, false, false);
|
2017-09-19 10:18:21 +02:00
|
|
|
model.checkAllItems();
|
|
|
|
|
|
|
|
if (mergeImportExportModel(&model, this, output_msg)) {
|
|
|
|
requestItemExpand(getSubTree(), true);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch (ApplicationException& ex) {
|
2023-03-16 07:37:49 +01:00
|
|
|
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 {
|
2023-03-16 07:37:49 +01:00
|
|
|
requestItemExpand({this}, true);
|
2021-03-09 07:10:36 +01:00
|
|
|
}
|
2017-09-19 10:18:21 +02:00
|
|
|
}
|
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 {
|
2021-01-20 14:23:58 +01:00
|
|
|
return true;
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
2023-10-30 10:54:44 +01:00
|
|
|
FormAccountDetails* StandardServiceRoot::accountSetupDialog() const {
|
|
|
|
return new FormEditStandardAccount(qApp->mainFormWidget());
|
|
|
|
}
|
2021-01-20 14:23:58 +01:00
|
|
|
|
2023-11-08 07:09:44 +01:00
|
|
|
void StandardServiceRoot::editItems(const QList<RootItem*>& items) {
|
2023-10-30 10:54:44 +01:00
|
|
|
auto std_feeds = boolinq::from(items)
|
|
|
|
.select([](RootItem* it) {
|
|
|
|
return qobject_cast<Feed*>(it);
|
|
|
|
})
|
|
|
|
.where([](Feed* fd) {
|
|
|
|
return fd != nullptr;
|
|
|
|
})
|
|
|
|
.toStdList();
|
|
|
|
|
|
|
|
if (!std_feeds.empty()) {
|
|
|
|
QScopedPointer<FormStandardFeedDetails> form_pointer(new FormStandardFeedDetails(this,
|
|
|
|
nullptr,
|
|
|
|
{},
|
|
|
|
qApp->mainFormWidget()));
|
|
|
|
|
|
|
|
form_pointer->addEditFeed<StandardFeed>(FROM_STD_LIST(QList<Feed*>, std_feeds));
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (items.first()->kind() == RootItem::Kind::ServiceRoot) {
|
|
|
|
QScopedPointer<FormEditStandardAccount> p(qobject_cast<FormEditStandardAccount*>(accountSetupDialog()));
|
|
|
|
|
|
|
|
p->addEditAccount(this);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-11-08 07:09:44 +01:00
|
|
|
ServiceRoot::editItems(items);
|
2021-01-20 14:23:58 +01:00
|
|
|
}
|
|
|
|
|
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.
|
2023-03-16 07:37:49 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2023-10-17 15:23:36 +02:00
|
|
|
QScopedPointer<FormDiscoverFeeds> form_discover(new FormDiscoverFeeds(this,
|
|
|
|
selected_item,
|
|
|
|
url,
|
|
|
|
qApp->mainFormWidget()));
|
|
|
|
|
2023-10-18 14:57:36 +02:00
|
|
|
if (form_discover->exec() == ADVANCED_FEED_ADD_DIALOG_CODE) {
|
|
|
|
QScopedPointer<FormStandardFeedDetails> form_pointer(new FormStandardFeedDetails(this,
|
|
|
|
selected_item,
|
|
|
|
url,
|
|
|
|
qApp->mainFormWidget()));
|
2023-10-17 15:23:36 +02:00
|
|
|
|
2023-10-18 14:57:36 +02:00
|
|
|
form_pointer->addEditFeed<StandardFeed>();
|
|
|
|
}
|
2023-10-17 15:23:36 +02:00
|
|
|
|
2017-09-19 10:18:21 +02:00
|
|
|
qApp->feedUpdateLock()->unlock();
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
Qt::ItemFlags StandardServiceRoot::additionalFlags() const {
|
2021-01-21 14:29:13 +01:00
|
|
|
return Qt::ItemFlag::ItemIsDropEnabled;
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
2021-07-30 08:34:07 +02:00
|
|
|
QList<Message> StandardServiceRoot::obtainNewMessages(Feed* feed,
|
2023-03-16 07:37:49 +01:00
|
|
|
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)
|
|
|
|
|
2021-07-30 08:34:07 +02:00
|
|
|
StandardFeed* f = static_cast<StandardFeed*>(feed);
|
2023-10-16 15:18:02 +02:00
|
|
|
QByteArray feed_contents;
|
2021-07-30 08:34:07 +02:00
|
|
|
QString formatted_feed_contents;
|
|
|
|
int download_timeout = qApp->settings()->value(GROUP(Feeds), SETTING(Feeds::UpdateTimeout)).toInt();
|
|
|
|
|
|
|
|
if (f->sourceType() == StandardFeed::SourceType::Url) {
|
2023-03-16 07:37:49 +01:00
|
|
|
qDebugNN << LOGSEC_CORE << "Downloading URL" << QUOTE_W_SPACE(feed->source()) << "to obtain feed data.";
|
2021-07-30 08:34:07 +02:00
|
|
|
|
|
|
|
QList<QPair<QByteArray, QByteArray>> headers;
|
|
|
|
|
2023-03-16 15:02:06 +01:00
|
|
|
headers << NetworkFactory::generateBasicAuthHeader(f->protection(), f->username(), f->password());
|
2021-07-30 08:34:07 +02:00
|
|
|
|
2023-10-24 08:35:37 +02:00
|
|
|
if (!f->lastEtag().isEmpty()) {
|
|
|
|
headers.append({QSL("If-None-Match").toLocal8Bit(), f->lastEtag().toLocal8Bit()});
|
|
|
|
|
|
|
|
qDebugNN << "Using ETag value:" << QUOTE_W_SPACE_DOT(f->lastEtag());
|
|
|
|
}
|
|
|
|
|
2021-07-30 08:34:07 +02:00
|
|
|
auto network_result = NetworkFactory::performNetworkOperation(feed->source(),
|
|
|
|
download_timeout,
|
|
|
|
{},
|
|
|
|
feed_contents,
|
|
|
|
QNetworkAccessManager::Operation::GetOperation,
|
|
|
|
headers,
|
|
|
|
false,
|
|
|
|
{},
|
|
|
|
{},
|
2023-10-23 15:01:07 +02:00
|
|
|
networkProxy());
|
2021-07-30 08:34:07 +02:00
|
|
|
|
2023-10-23 15:01:07 +02:00
|
|
|
if (network_result.m_networkError != QNetworkReply::NetworkError::NoError) {
|
|
|
|
qWarningNN << LOGSEC_CORE << "Error" << QUOTE_W_SPACE(network_result.m_networkError)
|
2023-03-16 07:37:49 +01:00
|
|
|
<< "during fetching of new messages for feed" << QUOTE_W_SPACE_DOT(feed->source());
|
2023-10-23 15:01:07 +02:00
|
|
|
throw FeedFetchException(Feed::Status::NetworkError,
|
|
|
|
NetworkFactory::networkErrorText(network_result.m_networkError));
|
2021-07-30 08:34:07 +02:00
|
|
|
}
|
2023-10-24 08:35:37 +02:00
|
|
|
else {
|
|
|
|
f->setLastEtag(network_result.m_headers.value(QSL("ETag")));
|
|
|
|
}
|
2021-07-30 08:34:07 +02:00
|
|
|
}
|
2023-10-16 15:21:02 +02:00
|
|
|
else if (f->sourceType() == StandardFeed::SourceType::LocalFile) {
|
|
|
|
feed_contents = IOFactory::readFile(feed->source());
|
|
|
|
}
|
2021-07-30 08:34:07 +02:00
|
|
|
else {
|
2023-03-16 07:37:49 +01:00
|
|
|
qDebugNN << LOGSEC_CORE << "Running custom script" << QUOTE_W_SPACE(feed->source()) << "to obtain feed data.";
|
2021-07-30 08:34:07 +02:00
|
|
|
|
|
|
|
// Use script to generate feed file.
|
|
|
|
try {
|
2023-10-16 15:18:02 +02:00
|
|
|
feed_contents = StandardFeed::generateFeedFileWithScript(feed->source(), download_timeout);
|
2021-03-03 12:05:25 +01:00
|
|
|
}
|
2021-07-30 08:34:07 +02:00
|
|
|
catch (const ScriptException& ex) {
|
2023-03-16 07:37:49 +01:00
|
|
|
qCriticalNN << LOGSEC_CORE << "Custom script for generating feed file failed:" << QUOTE_W_SPACE_DOT(ex.message());
|
2021-03-03 12:05:25 +01:00
|
|
|
|
2021-07-30 08:34:07 +02:00
|
|
|
throw FeedFetchException(Feed::Status::OtherError, ex.message());
|
|
|
|
}
|
|
|
|
}
|
2021-03-03 12:05:25 +01:00
|
|
|
|
2023-10-16 15:34:45 +02:00
|
|
|
// Sitemap parser supports gzip-encoded data too.
|
|
|
|
// We need to decode it here before encoding
|
|
|
|
// stuff kicks in.
|
|
|
|
if (SitemapParser::isGzip(feed_contents)) {
|
|
|
|
#if defined(ENABLE_COMPRESSED_SITEMAP)
|
|
|
|
qWarningNN << LOGSEC_CORE << "Decompressing gzipped feed data.";
|
|
|
|
|
|
|
|
QByteArray uncompressed_feed_contents;
|
|
|
|
|
|
|
|
if (!QCompressor::gzipDecompress(feed_contents, uncompressed_feed_contents)) {
|
|
|
|
throw ApplicationException("gzip decompression failed");
|
|
|
|
}
|
|
|
|
|
|
|
|
feed_contents = uncompressed_feed_contents;
|
|
|
|
#else
|
|
|
|
qWarningNN << LOGSEC_CORE << "This feed is gzipped.";
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2021-07-30 08:34:07 +02:00
|
|
|
if (!f->postProcessScript().simplified().isEmpty()) {
|
2023-03-16 07:37:49 +01:00
|
|
|
qDebugNN << LOGSEC_CORE << "We will process feed data with post-process script"
|
2021-07-30 08:34:07 +02:00
|
|
|
<< QUOTE_W_SPACE_DOT(f->postProcessScript());
|
2021-03-03 12:05:25 +01:00
|
|
|
|
2021-07-30 08:34:07 +02:00
|
|
|
try {
|
2023-10-16 15:18:02 +02:00
|
|
|
feed_contents =
|
|
|
|
StandardFeed::postProcessFeedFileWithScript(f->postProcessScript(), feed_contents, download_timeout);
|
2021-03-03 12:05:25 +01:00
|
|
|
}
|
2021-07-30 08:34:07 +02:00
|
|
|
catch (const ScriptException& ex) {
|
2023-03-16 07:37:49 +01:00
|
|
|
qCriticalNN << LOGSEC_CORE << "Post-processing script for feed file failed:" << QUOTE_W_SPACE_DOT(ex.message());
|
2021-03-03 12:05:25 +01:00
|
|
|
|
2021-07-30 08:34:07 +02:00
|
|
|
throw FeedFetchException(Feed::Status::OtherError, ex.message());
|
|
|
|
}
|
|
|
|
}
|
2021-03-03 12:05:25 +01:00
|
|
|
|
2023-10-16 15:18:02 +02:00
|
|
|
// Encode obtained data for further parsing.
|
|
|
|
QTextCodec* codec = QTextCodec::codecForName(f->encoding().toLocal8Bit());
|
|
|
|
|
|
|
|
if (codec == nullptr) {
|
|
|
|
// No suitable codec for this encoding was found.
|
|
|
|
// Use UTF-8.
|
|
|
|
formatted_feed_contents = QString::fromUtf8(feed_contents);
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
formatted_feed_contents = codec->toUnicode(feed_contents);
|
|
|
|
}
|
|
|
|
|
2021-07-30 08:34:07 +02:00
|
|
|
// Feed data are downloaded and encoded.
|
|
|
|
// Parse data and obtain messages.
|
|
|
|
QList<Message> messages;
|
2021-03-03 12:05:25 +01:00
|
|
|
|
2021-07-30 08:34:07 +02: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
|
|
|
|
2021-07-30 08:34:07 +02:00
|
|
|
case StandardFeed::Type::Rdf:
|
|
|
|
messages = RdfParser(formatted_feed_contents).messages();
|
|
|
|
break;
|
2021-03-03 12:05:25 +01:00
|
|
|
|
2021-07-30 08:34:07 +02:00
|
|
|
case StandardFeed::Type::Atom10:
|
|
|
|
messages = AtomParser(formatted_feed_contents).messages();
|
|
|
|
break;
|
2021-03-03 12:05:25 +01:00
|
|
|
|
2021-07-30 08:34:07 +02:00
|
|
|
case StandardFeed::Type::Json:
|
|
|
|
messages = JsonParser(formatted_feed_contents).messages();
|
|
|
|
break;
|
2021-03-03 12:05:25 +01:00
|
|
|
|
2023-10-16 15:05:42 +02:00
|
|
|
case StandardFeed::Type::Sitemap:
|
|
|
|
messages = SitemapParser(formatted_feed_contents).messages();
|
|
|
|
|
2021-07-30 08:34:07 +02:00
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
2021-07-25 09:56:45 +02:00
|
|
|
|
2021-07-30 08:34:07 +02:00
|
|
|
for (Message& mess : messages) {
|
|
|
|
mess.m_feedId = feed->customId();
|
2021-03-03 12:05:25 +01:00
|
|
|
}
|
|
|
|
|
2021-07-30 08:34:07 +02:00
|
|
|
return messages;
|
2021-03-03 12:05:25 +01: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.
|
2023-03-16 07:37:49 +01:00
|
|
|
auto* action_metadata =
|
|
|
|
new QAction(qApp->icons()->fromTheme(QSL("download"), QSL("emblem-downloads")), tr("Fetch metadata"), this);
|
2020-07-27 10:54:09 +02:00
|
|
|
|
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-11-10 13:03:43 +01:00
|
|
|
QVariantHash StandardServiceRoot::customDatabaseData() const {
|
|
|
|
QVariantHash data = ServiceRoot::customDatabaseData();
|
|
|
|
|
|
|
|
data[QSL("title")] = title();
|
|
|
|
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
void StandardServiceRoot::setCustomDatabaseData(const QVariantHash& data) {
|
|
|
|
ServiceRoot::setCustomDatabaseData(data);
|
|
|
|
|
|
|
|
setTitle(data.value(QSL("title"), defaultTitle()).toString());
|
|
|
|
}
|
|
|
|
|
|
|
|
QString StandardServiceRoot::defaultTitle() {
|
|
|
|
return qApp->system()->loggedInUser() + QSL(" (RSS/ATOM/JSON)");
|
|
|
|
}
|
|
|
|
|
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;
|
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
|
|
|
|
2020-09-25 09:29:50 +02:00
|
|
|
new_parents.push(model->sourceModel()->rootItem());
|
2017-09-19 10:18:21 +02:00
|
|
|
bool some_feed_category_error = false;
|
|
|
|
|
2023-03-16 07:37:49 +01:00
|
|
|
QSqlDatabase database = qApp->database()->driver()->connection(metaObject()->className());
|
|
|
|
|
2017-09-19 10:18:21 +02:00
|
|
|
// 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)) {
|
2020-09-25 09:29:50 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
2020-07-31 11:24:33 +02:00
|
|
|
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-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)) {
|
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;
|
2021-03-12 07:13:49 +01:00
|
|
|
|
2023-03-16 07:37:49 +01:00
|
|
|
qCriticalNN << LOGSEC_CORE << "Cannot import category:" << QUOTE_W_SPACE_DOT(ex.message());
|
2017-09-19 10:18:21 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-07-31 11:24:33 +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 &&
|
2023-03-16 07:37:49 +01:00
|
|
|
it->toFeed()->source().toLower() == source_feed->source().toLower();
|
2022-02-03 12:41:21 +01:00
|
|
|
});
|
2022-02-03 12:29:36 +01:00
|
|
|
|
2022-02-03 12:41:21 +01:00
|
|
|
if (feed_with_same_url != nullptr) {
|
|
|
|
continue;
|
2022-02-03 12:29:36 +01:00
|
|
|
}
|
|
|
|
|
2019-05-28 07:19:19 +02:00
|
|
|
auto* new_feed = new StandardFeed(*source_feed);
|
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) {
|
2023-03-16 07:37:49 +01:00
|
|
|
qCriticalNN << LOGSEC_CORE << "Cannot import feed:" << QUOTE_W_SPACE_DOT(ex.message());
|
2021-03-10 06:19:48 +01:00
|
|
|
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;
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
2020-08-31 08:44:03 +02:00
|
|
|
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.
|
2023-03-16 07:37:49 +01:00
|
|
|
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()));
|
2020-06-09 14:56:32 +02:00
|
|
|
|
2021-03-02 14:30:13 +01:00
|
|
|
form_pointer->addEditCategory<StandardCategory>();
|
2017-09-19 10:18:21 +02:00
|
|
|
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();
|
|
|
|
|
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;
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|