diff --git a/src/rss/automatedrssdownloader.cpp b/src/rss/automatedrssdownloader.cpp index 838e0d438..f34c1d7a6 100644 --- a/src/rss/automatedrssdownloader.cpp +++ b/src/rss/automatedrssdownloader.cpp @@ -68,6 +68,7 @@ AutomatedRssDownloader::AutomatedRssDownloader(const QWeakPointer& m ok = connect(ui->listRules, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(displayRulesListMenu(const QPoint&))); Q_ASSERT(ok); m_ruleList = manager.toStrongRef()->downloadRules(); + m_editableRuleList = new RssDownloadRuleList; // Read rule list from disk initLabelCombobox(); loadFeedList(); loadSettings(); @@ -92,6 +93,8 @@ AutomatedRssDownloader::AutomatedRssDownloader(const QWeakPointer& m Q_ASSERT(ok); ok = connect(ui->checkRegex, SIGNAL(stateChanged(int)), SLOT(updateMustNotLineValidity())); Q_ASSERT(ok); + ok = connect(this, SIGNAL(finished(int)), SLOT(on_finished(int))); + Q_ASSERT(ok); updateRuleDefinitionBox(); updateFeedList(); } @@ -99,10 +102,8 @@ AutomatedRssDownloader::AutomatedRssDownloader(const QWeakPointer& m AutomatedRssDownloader::~AutomatedRssDownloader() { qDebug() << Q_FUNC_INFO; - // Save current item on exit - saveEditedRule(); - saveSettings(); delete ui; + delete m_editableRuleList; } void AutomatedRssDownloader::loadSettings() @@ -132,10 +133,10 @@ void AutomatedRssDownloader::loadRulesList() saveEditedRule(); } ui->listRules->clear(); - foreach (const QString &rule_name, m_ruleList->ruleNames()) { + foreach (const QString &rule_name, m_editableRuleList->ruleNames()) { QListWidgetItem *item = new QListWidgetItem(rule_name, ui->listRules); item->setFlags(item->flags()|Qt::ItemIsUserCheckable); - if (m_ruleList->getRule(rule_name)->isEnabled()) + if (m_editableRuleList->getRule(rule_name)->isEnabled()) item->setCheckState(Qt::Checked); else item->setCheckState(Qt::Unchecked); @@ -170,7 +171,7 @@ void AutomatedRssDownloader::updateFeedList() const QString feed_url = item->data(Qt::UserRole).toString(); bool all_enabled = false; foreach (const QListWidgetItem *ruleItem, ui->listRules->selectedItems()) { - RssDownloadRulePtr rule = m_ruleList->getRule(ruleItem->text()); + RssDownloadRulePtr rule = m_editableRuleList->getRule(ruleItem->text()); if (!rule) continue; qDebug() << "Rule" << rule->name() << "affects" << rule->rssFeeds().size() << "feeds."; foreach (QString test, rule->rssFeeds()) { @@ -257,7 +258,7 @@ RssDownloadRulePtr AutomatedRssDownloader::getCurrentRule() const { QListWidgetItem * current_item = ui->listRules->currentItem(); if (current_item) - return m_ruleList->getRule(current_item->text()); + return m_editableRuleList->getRule(current_item->text()); return RssDownloadRulePtr(); } @@ -279,7 +280,7 @@ void AutomatedRssDownloader::saveEditedRule() qDebug() << "Probably removed the item, no need to save it"; return; } - RssDownloadRulePtr rule = m_ruleList->getRule(m_editedRule->text()); + RssDownloadRulePtr rule = m_editableRuleList->getRule(m_editedRule->text()); if (!rule) { rule = RssDownloadRulePtr(new RssDownloadRule); rule->setName(m_editedRule->text()); @@ -301,7 +302,7 @@ void AutomatedRssDownloader::saveEditedRule() Preferences().addTorrentLabel(rule->label()); //rule->setRssFeeds(getSelectedFeeds()); // Save it - m_ruleList->saveRule(rule); + m_editableRuleList->saveRule(rule); } @@ -311,7 +312,7 @@ void AutomatedRssDownloader::on_addRuleBtn_clicked() const QString rule_name = QInputDialog::getText(this, tr("New rule name"), tr("Please type the name of the new download rule.")); if (rule_name.isEmpty()) return; // Check if this rule name already exists - if (m_ruleList->getRule(rule_name)) { + if (m_editableRuleList->getRule(rule_name)) { QMessageBox::warning(this, tr("Rule name conflict"), tr("A rule with this name already exists, please choose another name.")); return; } @@ -342,8 +343,8 @@ void AutomatedRssDownloader::on_removeRuleBtn_clicked() // Clean up memory delete item; qDebug("Removed item for the UI list"); - // Remove it from the m_ruleList - m_ruleList->removeRule(rule_name); + // Remove it from the m_editableRuleList + m_editableRuleList->removeRule(rule_name); } } @@ -356,7 +357,7 @@ void AutomatedRssDownloader::on_browseSP_clicked() void AutomatedRssDownloader::on_exportBtn_clicked() { - if (m_ruleList->isEmpty()) { + if (m_editableRuleList->isEmpty()) { QMessageBox::warning(this, tr("Invalid action"), tr("The list is empty, there is nothing to export.")); return; } @@ -365,7 +366,7 @@ void AutomatedRssDownloader::on_exportBtn_clicked() if (save_path.isEmpty()) return; if (!save_path.endsWith(".rssrules", Qt::CaseInsensitive)) save_path += ".rssrules"; - if (!m_ruleList->serialize(save_path)) { + if (!m_editableRuleList->serialize(save_path)) { QMessageBox::warning(this, tr("I/O Error"), tr("Failed to create the destination file")); return; } @@ -377,7 +378,7 @@ void AutomatedRssDownloader::on_importBtn_clicked() QString load_path = QFileDialog::getOpenFileName(this, tr("Please point to the RSS download rules file"), QDir::homePath(), tr("Rules list (*.rssrules *.filters)")); if (load_path.isEmpty() || !QFile::exists(load_path)) return; // Load it - if (!m_ruleList->unserialize(load_path)) { + if (!m_editableRuleList->unserialize(load_path)) { QMessageBox::warning(this, tr("Import Error"), tr("Failed to import the selected rules file")); return; } @@ -426,11 +427,11 @@ void AutomatedRssDownloader::renameSelectedRule() QString new_name = QInputDialog::getText(this, tr("Rule renaming"), tr("Please type the new rule name"), QLineEdit::Normal, item->text()); new_name = new_name.trimmed(); if (new_name.isEmpty()) return; - if (m_ruleList->ruleNames().contains(new_name, Qt::CaseInsensitive)) { + if (m_editableRuleList->ruleNames().contains(new_name, Qt::CaseInsensitive)) { QMessageBox::warning(this, tr("Rule name conflict"), tr("A rule with this name already exists, please choose another name.")); } else { // Rename the rule - m_ruleList->renameRule(item->text(), new_name); + m_editableRuleList->renameRule(item->text(), new_name); item->setText(new_name); return; } @@ -445,7 +446,7 @@ void AutomatedRssDownloader::handleFeedCheckStateChange(QListWidgetItem *feed_it } const QString feed_url = feed_item->data(Qt::UserRole).toString(); foreach (QListWidgetItem* rule_item, ui->listRules->selectedItems()) { - RssDownloadRulePtr rule = m_ruleList->getRule(rule_item->text()); + RssDownloadRulePtr rule = m_editableRuleList->getRule(rule_item->text()); Q_ASSERT(rule); QStringList affected_feeds = rule->rssFeeds(); if (feed_item->checkState() == Qt::Checked) { @@ -458,7 +459,7 @@ void AutomatedRssDownloader::handleFeedCheckStateChange(QListWidgetItem *feed_it // Save the updated rule if (affected_feeds.size() != rule->rssFeeds().size()) { rule->setRssFeeds(affected_feeds); - m_ruleList->saveRule(rule); + m_editableRuleList->saveRule(rule); } } // Update Matching articles @@ -477,7 +478,7 @@ void AutomatedRssDownloader::updateMatchingArticles() const QHash all_feeds = manager->getAllFeedsAsHash(); foreach (const QListWidgetItem *rule_item, ui->listRules->selectedItems()) { - RssDownloadRulePtr rule = m_ruleList->getRule(rule_item->text()); + RssDownloadRulePtr rule = m_editableRuleList->getRule(rule_item->text()); if (!rule) continue; foreach (const QString &feed_url, rule->rssFeeds()) { qDebug() << Q_FUNC_INFO << feed_url; @@ -588,4 +589,11 @@ void AutomatedRssDownloader::updateMustNotLineValidity() } } - +void AutomatedRssDownloader::on_finished(int result) { + Q_UNUSED(result); + // Save current item on exit + saveEditedRule(); + m_ruleList->replace(m_editableRuleList); + m_ruleList->saveRulesToStorage(); + saveSettings(); +} diff --git a/src/rss/automatedrssdownloader.h b/src/rss/automatedrssdownloader.h index 02047e7b3..630052112 100644 --- a/src/rss/automatedrssdownloader.h +++ b/src/rss/automatedrssdownloader.h @@ -80,6 +80,7 @@ private slots: void updateFieldsToolTips(bool regex); void updateMustLineValidity(); void updateMustNotLineValidity(); + void on_finished(int result); private: RssDownloadRulePtr getCurrentRule() const; @@ -91,6 +92,7 @@ private: QWeakPointer m_manager; QListWidgetItem* m_editedRule; RssDownloadRuleList *m_ruleList; + RssDownloadRuleList *m_editableRuleList; }; #endif // AUTOMATEDRSSDOWNLOADER_H diff --git a/src/rss/rssdownloadrulelist.cpp b/src/rss/rssdownloadrulelist.cpp index be0cb6656..b838eba20 100644 --- a/src/rss/rssdownloadrulelist.cpp +++ b/src/rss/rssdownloadrulelist.cpp @@ -52,6 +52,16 @@ RssDownloadRulePtr RssDownloadRuleList::findMatchingRule(const QString &feed_url return RssDownloadRulePtr(); } +void RssDownloadRuleList::replace(RssDownloadRuleList *other) { + m_rules.clear(); + m_feedRules.clear(); + foreach (const QString& name, other->ruleNames()) { + RssDownloadRule* r = new RssDownloadRule; + *r = *(other->getRule(name).value); // Implicit copy + saveRule(RssDownloadRulePtr(r)); + } +} + void RssDownloadRuleList::saveRulesToStorage() { QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss"); @@ -97,8 +107,6 @@ void RssDownloadRuleList::saveRule(const RssDownloadRulePtr &rule) foreach (const QString &feed_url, rule->rssFeeds()) { m_feedRules[feed_url].append(rule->name()); } - // Save rules - saveRulesToStorage(); qDebug() << Q_FUNC_INFO << "EXIT"; } @@ -111,8 +119,6 @@ void RssDownloadRuleList::removeRule(const QString &name) foreach (const QString &feed_url, rule->rssFeeds()) { m_feedRules[feed_url].removeOne(rule->name()); } - // Save rules - saveRulesToStorage(); } void RssDownloadRuleList::renameRule(const QString &old_name, const QString &new_name) @@ -125,8 +131,6 @@ void RssDownloadRuleList::renameRule(const QString &old_name, const QString &new foreach (const QString &feed_url, rule->rssFeeds()) { m_feedRules[feed_url].replace(m_feedRules[feed_url].indexOf(old_name), new_name); } - // Save rules - saveRulesToStorage(); } RssDownloadRulePtr RssDownloadRuleList::getRule(const QString &name) const diff --git a/src/rss/rssdownloadrulelist.h b/src/rss/rssdownloadrulelist.h index c3c151df0..4a20c4744 100644 --- a/src/rss/rssdownloadrulelist.h +++ b/src/rss/rssdownloadrulelist.h @@ -50,14 +50,15 @@ public: RssDownloadRulePtr getRule(const QString &name) const; inline QStringList ruleNames() const { return m_rules.keys(); } inline bool isEmpty() const { return m_rules.isEmpty(); } + void saveRulesToStorage(); bool serialize(const QString& path); bool unserialize(const QString& path); + void replace(RssDownloadRuleList* other); private: void loadRulesFromStorage(); void loadRulesFromVariantHash(const QVariantHash& l); QVariantHash toVariantHash() const; - void saveRulesToStorage(); private: QHash m_rules;