qBittorrent/src/base/rss/rssdownloadrulelist.cpp

173 lines
5.2 KiB
C++
Raw Normal View History

/*
* Bittorrent Client using Qt4 and libtorrent.
* Copyright (C) 2010 Christophe Dumez
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* as published by the Free Software Foundation; either version 2
* of the License, or (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* In addition, as a special exception, the copyright holders give permission to
* link this program with the OpenSSL project's "OpenSSL" library (or with
* modified versions of it that use the same license as the "OpenSSL" library),
* and distribute the linked executables. You must obey the GNU General Public
* License in all respects for all of the code used other than "OpenSSL". If you
* modify file(s), you may extend this exception to your version of the file(s),
* but you are not obligated to do so. If you do not wish to do so, delete this
* exception statement from your version.
*
* Contact : chris@qbittorrent.org
*/
#include <QFile>
#include <QDataStream>
#include <QDebug>
2015-09-25 11:10:05 +03:00
#include "base/preferences.h"
#include "base/qinisettings.h"
2015-10-14 12:31:44 +03:00
#include "rssdownloadrulelist.h"
2012-02-20 21:49:35 +04:00
RssDownloadRuleList::RssDownloadRuleList()
{
2012-02-20 21:49:35 +04:00
loadRulesFromStorage();
}
2012-02-19 20:53:10 +04:00
RssDownloadRulePtr RssDownloadRuleList::findMatchingRule(const QString &feed_url, const QString &article_title) const
{
Q_ASSERT(Preferences::instance()->isRssDownloadingEnabled());
2012-02-19 20:53:10 +04:00
QStringList rule_names = m_feedRules.value(feed_url);
2012-02-20 21:30:53 +04:00
foreach (const QString &rule_name, rule_names) {
2012-02-19 20:53:10 +04:00
RssDownloadRulePtr rule = m_rules[rule_name];
2012-02-20 21:30:53 +04:00
if (rule->isEnabled() && rule->matches(article_title)) return rule;
}
2012-02-19 20:53:10 +04:00
return RssDownloadRulePtr();
}
void RssDownloadRuleList::replace(RssDownloadRuleList *other) {
m_rules.clear();
m_feedRules.clear();
foreach (const QString& name, other->ruleNames()) {
2013-07-08 19:04:53 +04:00
saveRule(other->getRule(name));
}
}
void RssDownloadRuleList::saveRulesToStorage()
{
QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss");
qBTRSS.setValue("download_rules", toVariantHash());
}
void RssDownloadRuleList::loadRulesFromStorage()
{
QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss");
loadRulesFromVariantHash(qBTRSS.value("download_rules").toHash());
}
QVariantHash RssDownloadRuleList::toVariantHash() const
{
QVariantHash ret;
2012-02-20 21:30:53 +04:00
foreach (const RssDownloadRulePtr &rule, m_rules.values()) {
2012-02-19 20:53:10 +04:00
ret.insert(rule->name(), rule->toVariantHash());
}
return ret;
}
void RssDownloadRuleList::loadRulesFromVariantHash(const QVariantHash &h)
{
2012-07-14 15:45:40 +04:00
QVariantHash::ConstIterator it = h.begin();
QVariantHash::ConstIterator itend = h.end();
for ( ; it != itend; ++it) {
RssDownloadRulePtr rule = RssDownloadRule::fromVariantHash(it.value().toHash());
if (rule && !rule->name().isEmpty())
saveRule(rule);
}
}
2012-02-19 20:53:10 +04:00
void RssDownloadRuleList::saveRule(const RssDownloadRulePtr &rule)
{
2012-02-19 20:53:10 +04:00
qDebug() << Q_FUNC_INFO << rule->name();
Q_ASSERT(rule);
2012-02-20 21:30:53 +04:00
if (m_rules.contains(rule->name())) {
2010-11-13 23:08:44 +03:00
qDebug("This is an update, removing old rule first");
2012-02-19 20:53:10 +04:00
removeRule(rule->name());
2010-11-13 21:08:09 +03:00
}
2012-02-19 20:53:10 +04:00
m_rules.insert(rule->name(), rule);
// Update feedRules hashtable
2012-02-20 21:30:53 +04:00
foreach (const QString &feed_url, rule->rssFeeds()) {
2012-02-19 20:53:10 +04:00
m_feedRules[feed_url].append(rule->name());
}
2010-11-13 23:08:44 +03:00
qDebug() << Q_FUNC_INFO << "EXIT";
}
void RssDownloadRuleList::removeRule(const QString &name)
{
qDebug() << Q_FUNC_INFO << name;
2012-02-20 21:30:53 +04:00
if (!m_rules.contains(name)) return;
2012-02-19 20:53:10 +04:00
RssDownloadRulePtr rule = m_rules.take(name);
// Update feedRules hashtable
2012-02-20 21:30:53 +04:00
foreach (const QString &feed_url, rule->rssFeeds()) {
2012-02-19 20:53:10 +04:00
m_feedRules[feed_url].removeOne(rule->name());
}
}
void RssDownloadRuleList::renameRule(const QString &old_name, const QString &new_name)
{
2012-02-20 21:30:53 +04:00
if (!m_rules.contains(old_name)) return;
2012-02-19 20:53:10 +04:00
RssDownloadRulePtr rule = m_rules.take(old_name);
rule->setName(new_name);
m_rules.insert(new_name, rule);
// Update feedRules hashtable
2012-02-20 21:30:53 +04:00
foreach (const QString &feed_url, rule->rssFeeds()) {
m_feedRules[feed_url].replace(m_feedRules[feed_url].indexOf(old_name), new_name);
}
}
2012-02-19 20:53:10 +04:00
RssDownloadRulePtr RssDownloadRuleList::getRule(const QString &name) const
{
2011-04-16 14:20:45 +04:00
return m_rules.value(name);
}
bool RssDownloadRuleList::serialize(const QString& path)
{
QFile f(path);
2012-02-20 21:30:53 +04:00
if (f.open(QIODevice::WriteOnly)) {
QDataStream out(&f);
out.setVersion(QDataStream::Qt_4_5);
out << toVariantHash();
f.close();
return true;
} else {
return false;
}
}
bool RssDownloadRuleList::unserialize(const QString &path)
{
QFile f(path);
2012-02-20 21:30:53 +04:00
if (f.open(QIODevice::ReadOnly)) {
QDataStream in(&f);
in.setVersion(QDataStream::Qt_4_5);
QVariantHash tmp;
in >> tmp;
f.close();
if (tmp.isEmpty())
return false;
qDebug("Processing was successful!");
loadRulesFromVariantHash(tmp);
return true;
} else {
qDebug("Error: could not open file at %s", qPrintable(path));
return false;
}
}