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"
|
|
|
|
#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)
|
2017-09-20 08:10:51 +02:00
|
|
|
: ServiceRoot(parent),
|
2017-09-19 10:18:21 +02:00
|
|
|
m_actionExportFeeds(nullptr), m_actionImportFeeds(nullptr), m_serviceMenu(QList<QAction*>()),
|
|
|
|
m_feedContextMenu(QList<QAction*>()), 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."));
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
StandardServiceRoot::~StandardServiceRoot() {
|
2017-09-19 10:18:21 +02:00
|
|
|
qDeleteAll(m_serviceMenu);
|
|
|
|
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();
|
|
|
|
|
|
|
|
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();
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StandardServiceRoot::stop() {
|
2017-09-19 10:18:21 +02:00
|
|
|
qDebug("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
|
|
|
}
|
|
|
|
|
|
|
|
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();
|
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() {
|
2017-09-19 10:18:21 +02:00
|
|
|
QSqlDatabase database = qApp->database()->connection(metaObject()->className(), DatabaseFactory::FromSettings);
|
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);
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void StandardServiceRoot::checkArgumentsForFeedAdding() {
|
2017-09-19 10:18:21 +02:00
|
|
|
foreach (const QString& arg, qApp->arguments().mid(1)) {
|
|
|
|
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))) {
|
|
|
|
addNewFeed(processFeedUrl(argument));
|
|
|
|
}
|
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.
|
|
|
|
m_actionFeedFetchMetadata = new QAction(qApp->icons()->fromTheme(QSL("emblem-downloads")), tr("Fetch metadata"), nullptr);
|
|
|
|
m_feedContextMenu.append(m_actionFeedFetchMetadata);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Make connections.
|
|
|
|
disconnect(m_actionFeedFetchMetadata, &QAction::triggered, 0, 0);
|
|
|
|
connect(m_actionFeedFetchMetadata, &QAction::triggered, feed, &StandardFeed::fetchMetadataForItself);
|
|
|
|
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;
|
|
|
|
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();
|
|
|
|
|
|
|
|
foreach (RootItem* source_item, source_parent->childItems()) {
|
|
|
|
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) {
|
|
|
|
StandardCategory* source_category = static_cast<StandardCategory*>(source_item);
|
|
|
|
StandardCategory* new_category = new StandardCategory(*source_category);
|
|
|
|
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;
|
|
|
|
|
|
|
|
foreach (RootItem* child, target_parent->childItems()) {
|
|
|
|
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) {
|
|
|
|
StandardFeed* source_feed = static_cast<StandardFeed*>(source_item);
|
|
|
|
StandardFeed* new_feed = new StandardFeed(*source_feed);
|
|
|
|
|
|
|
|
// 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()));
|
|
|
|
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()));
|
|
|
|
form.data()->setMode(FeedsImportExportModel::Import);
|
|
|
|
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()));
|
|
|
|
form.data()->setMode(FeedsImportExportModel::Export);
|
|
|
|
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()) {
|
|
|
|
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;
|
2017-07-21 06:53:23 +02:00
|
|
|
}
|