Browse Source

Do not autoDL items based on rules still being edited.

adaptive-webui-19844
Nick Tiskov 12 years ago
parent
commit
454dbd5cd8
  1. 38
      src/rss/automatedrssdownloader.cpp
  2. 1
      src/rss/automatedrssdownloader.h
  3. 16
      src/rss/rssdownloadrulelist.cpp
  4. 3
      src/rss/rssdownloadrulelist.h

38
src/rss/automatedrssdownloader.cpp

@ -68,6 +68,7 @@ AutomatedRssDownloader::AutomatedRssDownloader(const QWeakPointer<RssManager>& m
ok = connect(ui->listRules, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(displayRulesListMenu(const QPoint&))); ok = connect(ui->listRules, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(displayRulesListMenu(const QPoint&)));
Q_ASSERT(ok); Q_ASSERT(ok);
m_ruleList = manager.toStrongRef()->downloadRules(); m_ruleList = manager.toStrongRef()->downloadRules();
m_editableRuleList = new RssDownloadRuleList; // Read rule list from disk
initLabelCombobox(); initLabelCombobox();
loadFeedList(); loadFeedList();
loadSettings(); loadSettings();
@ -101,8 +102,11 @@ AutomatedRssDownloader::~AutomatedRssDownloader()
qDebug() << Q_FUNC_INFO; qDebug() << Q_FUNC_INFO;
// Save current item on exit // Save current item on exit
saveEditedRule(); saveEditedRule();
m_ruleList->replace(m_editableRuleList);
m_ruleList->saveRulesToStorage();
saveSettings(); saveSettings();
delete ui; delete ui;
delete m_editableRuleList;
} }
void AutomatedRssDownloader::loadSettings() void AutomatedRssDownloader::loadSettings()
@ -132,10 +136,10 @@ void AutomatedRssDownloader::loadRulesList()
saveEditedRule(); saveEditedRule();
} }
ui->listRules->clear(); 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); QListWidgetItem *item = new QListWidgetItem(rule_name, ui->listRules);
item->setFlags(item->flags()|Qt::ItemIsUserCheckable); item->setFlags(item->flags()|Qt::ItemIsUserCheckable);
if (m_ruleList->getRule(rule_name)->isEnabled()) if (m_editableRuleList->getRule(rule_name)->isEnabled())
item->setCheckState(Qt::Checked); item->setCheckState(Qt::Checked);
else else
item->setCheckState(Qt::Unchecked); item->setCheckState(Qt::Unchecked);
@ -170,7 +174,7 @@ void AutomatedRssDownloader::updateFeedList()
const QString feed_url = item->data(Qt::UserRole).toString(); const QString feed_url = item->data(Qt::UserRole).toString();
bool all_enabled = false; bool all_enabled = false;
foreach (const QListWidgetItem *ruleItem, ui->listRules->selectedItems()) { foreach (const QListWidgetItem *ruleItem, ui->listRules->selectedItems()) {
RssDownloadRulePtr rule = m_ruleList->getRule(ruleItem->text()); RssDownloadRulePtr rule = m_editableRuleList->getRule(ruleItem->text());
if (!rule) continue; if (!rule) continue;
qDebug() << "Rule" << rule->name() << "affects" << rule->rssFeeds().size() << "feeds."; qDebug() << "Rule" << rule->name() << "affects" << rule->rssFeeds().size() << "feeds.";
foreach (QString test, rule->rssFeeds()) { foreach (QString test, rule->rssFeeds()) {
@ -257,7 +261,7 @@ RssDownloadRulePtr AutomatedRssDownloader::getCurrentRule() const
{ {
QListWidgetItem * current_item = ui->listRules->currentItem(); QListWidgetItem * current_item = ui->listRules->currentItem();
if (current_item) if (current_item)
return m_ruleList->getRule(current_item->text()); return m_editableRuleList->getRule(current_item->text());
return RssDownloadRulePtr(); return RssDownloadRulePtr();
} }
@ -279,7 +283,7 @@ void AutomatedRssDownloader::saveEditedRule()
qDebug() << "Probably removed the item, no need to save it"; qDebug() << "Probably removed the item, no need to save it";
return; return;
} }
RssDownloadRulePtr rule = m_ruleList->getRule(m_editedRule->text()); RssDownloadRulePtr rule = m_editableRuleList->getRule(m_editedRule->text());
if (!rule) { if (!rule) {
rule = RssDownloadRulePtr(new RssDownloadRule); rule = RssDownloadRulePtr(new RssDownloadRule);
rule->setName(m_editedRule->text()); rule->setName(m_editedRule->text());
@ -301,7 +305,7 @@ void AutomatedRssDownloader::saveEditedRule()
Preferences().addTorrentLabel(rule->label()); Preferences().addTorrentLabel(rule->label());
//rule->setRssFeeds(getSelectedFeeds()); //rule->setRssFeeds(getSelectedFeeds());
// Save it // Save it
m_ruleList->saveRule(rule); m_editableRuleList->saveRule(rule);
} }
@ -311,7 +315,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.")); 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; if (rule_name.isEmpty()) return;
// Check if this rule name already exists // 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.")); QMessageBox::warning(this, tr("Rule name conflict"), tr("A rule with this name already exists, please choose another name."));
return; return;
} }
@ -342,8 +346,8 @@ void AutomatedRssDownloader::on_removeRuleBtn_clicked()
// Clean up memory // Clean up memory
delete item; delete item;
qDebug("Removed item for the UI list"); qDebug("Removed item for the UI list");
// Remove it from the m_ruleList // Remove it from the m_editableRuleList
m_ruleList->removeRule(rule_name); m_editableRuleList->removeRule(rule_name);
} }
} }
@ -356,7 +360,7 @@ void AutomatedRssDownloader::on_browseSP_clicked()
void AutomatedRssDownloader::on_exportBtn_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.")); QMessageBox::warning(this, tr("Invalid action"), tr("The list is empty, there is nothing to export."));
return; return;
} }
@ -365,7 +369,7 @@ void AutomatedRssDownloader::on_exportBtn_clicked()
if (save_path.isEmpty()) return; if (save_path.isEmpty()) return;
if (!save_path.endsWith(".rssrules", Qt::CaseInsensitive)) if (!save_path.endsWith(".rssrules", Qt::CaseInsensitive))
save_path += ".rssrules"; 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")); QMessageBox::warning(this, tr("I/O Error"), tr("Failed to create the destination file"));
return; return;
} }
@ -377,7 +381,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)")); 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; if (load_path.isEmpty() || !QFile::exists(load_path)) return;
// Load it // 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")); QMessageBox::warning(this, tr("Import Error"), tr("Failed to import the selected rules file"));
return; return;
} }
@ -426,11 +430,11 @@ void AutomatedRssDownloader::renameSelectedRule()
QString new_name = QInputDialog::getText(this, tr("Rule renaming"), tr("Please type the new rule name"), QLineEdit::Normal, item->text()); 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(); new_name = new_name.trimmed();
if (new_name.isEmpty()) return; 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.")); QMessageBox::warning(this, tr("Rule name conflict"), tr("A rule with this name already exists, please choose another name."));
} else { } else {
// Rename the rule // Rename the rule
m_ruleList->renameRule(item->text(), new_name); m_editableRuleList->renameRule(item->text(), new_name);
item->setText(new_name); item->setText(new_name);
return; return;
} }
@ -445,7 +449,7 @@ void AutomatedRssDownloader::handleFeedCheckStateChange(QListWidgetItem *feed_it
} }
const QString feed_url = feed_item->data(Qt::UserRole).toString(); const QString feed_url = feed_item->data(Qt::UserRole).toString();
foreach (QListWidgetItem* rule_item, ui->listRules->selectedItems()) { 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); Q_ASSERT(rule);
QStringList affected_feeds = rule->rssFeeds(); QStringList affected_feeds = rule->rssFeeds();
if (feed_item->checkState() == Qt::Checked) { if (feed_item->checkState() == Qt::Checked) {
@ -458,7 +462,7 @@ void AutomatedRssDownloader::handleFeedCheckStateChange(QListWidgetItem *feed_it
// Save the updated rule // Save the updated rule
if (affected_feeds.size() != rule->rssFeeds().size()) { if (affected_feeds.size() != rule->rssFeeds().size()) {
rule->setRssFeeds(affected_feeds); rule->setRssFeeds(affected_feeds);
m_ruleList->saveRule(rule); m_editableRuleList->saveRule(rule);
} }
} }
// Update Matching articles // Update Matching articles
@ -477,7 +481,7 @@ void AutomatedRssDownloader::updateMatchingArticles()
const QHash<QString, RssFeedPtr> all_feeds = manager->getAllFeedsAsHash(); const QHash<QString, RssFeedPtr> all_feeds = manager->getAllFeedsAsHash();
foreach (const QListWidgetItem *rule_item, ui->listRules->selectedItems()) { 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; if (!rule) continue;
foreach (const QString &feed_url, rule->rssFeeds()) { foreach (const QString &feed_url, rule->rssFeeds()) {
qDebug() << Q_FUNC_INFO << feed_url; qDebug() << Q_FUNC_INFO << feed_url;

1
src/rss/automatedrssdownloader.h

@ -91,6 +91,7 @@ private:
QWeakPointer<RssManager> m_manager; QWeakPointer<RssManager> m_manager;
QListWidgetItem* m_editedRule; QListWidgetItem* m_editedRule;
RssDownloadRuleList *m_ruleList; RssDownloadRuleList *m_ruleList;
RssDownloadRuleList *m_editableRuleList;
}; };
#endif // AUTOMATEDRSSDOWNLOADER_H #endif // AUTOMATEDRSSDOWNLOADER_H

16
src/rss/rssdownloadrulelist.cpp

@ -52,6 +52,16 @@ RssDownloadRulePtr RssDownloadRuleList::findMatchingRule(const QString &feed_url
return RssDownloadRulePtr(); 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() void RssDownloadRuleList::saveRulesToStorage()
{ {
QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss"); QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss");
@ -97,8 +107,6 @@ void RssDownloadRuleList::saveRule(const RssDownloadRulePtr &rule)
foreach (const QString &feed_url, rule->rssFeeds()) { foreach (const QString &feed_url, rule->rssFeeds()) {
m_feedRules[feed_url].append(rule->name()); m_feedRules[feed_url].append(rule->name());
} }
// Save rules
saveRulesToStorage();
qDebug() << Q_FUNC_INFO << "EXIT"; qDebug() << Q_FUNC_INFO << "EXIT";
} }
@ -111,8 +119,6 @@ void RssDownloadRuleList::removeRule(const QString &name)
foreach (const QString &feed_url, rule->rssFeeds()) { foreach (const QString &feed_url, rule->rssFeeds()) {
m_feedRules[feed_url].removeOne(rule->name()); m_feedRules[feed_url].removeOne(rule->name());
} }
// Save rules
saveRulesToStorage();
} }
void RssDownloadRuleList::renameRule(const QString &old_name, const QString &new_name) 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()) { foreach (const QString &feed_url, rule->rssFeeds()) {
m_feedRules[feed_url].replace(m_feedRules[feed_url].indexOf(old_name), new_name); m_feedRules[feed_url].replace(m_feedRules[feed_url].indexOf(old_name), new_name);
} }
// Save rules
saveRulesToStorage();
} }
RssDownloadRulePtr RssDownloadRuleList::getRule(const QString &name) const RssDownloadRulePtr RssDownloadRuleList::getRule(const QString &name) const

3
src/rss/rssdownloadrulelist.h

@ -50,14 +50,15 @@ public:
RssDownloadRulePtr getRule(const QString &name) const; RssDownloadRulePtr getRule(const QString &name) const;
inline QStringList ruleNames() const { return m_rules.keys(); } inline QStringList ruleNames() const { return m_rules.keys(); }
inline bool isEmpty() const { return m_rules.isEmpty(); } inline bool isEmpty() const { return m_rules.isEmpty(); }
void saveRulesToStorage();
bool serialize(const QString& path); bool serialize(const QString& path);
bool unserialize(const QString& path); bool unserialize(const QString& path);
void replace(RssDownloadRuleList* other);
private: private:
void loadRulesFromStorage(); void loadRulesFromStorage();
void loadRulesFromVariantHash(const QVariantHash& l); void loadRulesFromVariantHash(const QVariantHash& l);
QVariantHash toVariantHash() const; QVariantHash toVariantHash() const;
void saveRulesToStorage();
private: private:
QHash<QString, RssDownloadRulePtr> m_rules; QHash<QString, RssDownloadRulePtr> m_rules;

Loading…
Cancel
Save