2010-10-31 15:35:07 +03:00
|
|
|
/*
|
2015-10-14 12:49:29 +03:00
|
|
|
* Bittorrent Client using Qt and libtorrent.
|
2010-10-31 15:35:07 +03:00
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
2010-11-13 15:08:29 +03:00
|
|
|
#include <QFile>
|
2010-11-13 18:14:50 +03:00
|
|
|
#include <QDataStream>
|
2010-11-13 15:33:52 +03:00
|
|
|
#include <QDebug>
|
2010-11-13 15:08:29 +03:00
|
|
|
|
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"
|
2010-10-31 15:35:07 +03:00
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
using namespace Rss;
|
|
|
|
|
|
|
|
DownloadRuleList::DownloadRuleList()
|
2010-10-31 15:35:07 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
loadRulesFromStorage();
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
DownloadRulePtr DownloadRuleList::findMatchingRule(const QString &feedUrl, const QString &articleTitle) const
|
2010-10-31 15:35:07 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
Q_ASSERT(Preferences::instance()->isRssDownloadingEnabled());
|
|
|
|
QStringList ruleNames = m_feedRules.value(feedUrl);
|
|
|
|
foreach (const QString &rule_name, ruleNames) {
|
2015-10-15 19:33:27 +03:00
|
|
|
DownloadRulePtr rule = m_rules[rule_name];
|
2015-10-14 12:49:29 +03:00
|
|
|
if (rule->isEnabled() && rule->matches(articleTitle)) return rule;
|
|
|
|
}
|
2015-10-15 19:33:27 +03:00
|
|
|
return DownloadRulePtr();
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRuleList::replace(DownloadRuleList *other)
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
m_rules.clear();
|
|
|
|
m_feedRules.clear();
|
|
|
|
foreach (const QString &name, other->ruleNames()) {
|
|
|
|
saveRule(other->getRule(name));
|
|
|
|
}
|
2013-07-01 12:43:40 +04:00
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRuleList::saveRulesToStorage()
|
2010-11-07 16:06:00 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss");
|
|
|
|
qBTRSS.setValue("download_rules", toVariantHash());
|
2010-11-07 16:06:00 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRuleList::loadRulesFromStorage()
|
2010-10-31 15:35:07 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss");
|
|
|
|
loadRulesFromVariantHash(qBTRSS.value("download_rules").toHash());
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
QVariantHash DownloadRuleList::toVariantHash() const
|
2010-10-31 15:35:07 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
QVariantHash ret;
|
2015-10-15 19:33:27 +03:00
|
|
|
foreach (const DownloadRulePtr &rule, m_rules.values()) {
|
2015-10-14 12:49:29 +03:00
|
|
|
ret.insert(rule->name(), rule->toVariantHash());
|
|
|
|
}
|
|
|
|
return ret;
|
2010-11-13 13:49:22 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRuleList::loadRulesFromVariantHash(const QVariantHash &h)
|
2010-11-13 13:49:22 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
QVariantHash::ConstIterator it = h.begin();
|
|
|
|
QVariantHash::ConstIterator itend = h.end();
|
|
|
|
for ( ; it != itend; ++it) {
|
2015-10-15 19:33:27 +03:00
|
|
|
DownloadRulePtr rule = DownloadRule::fromVariantHash(it.value().toHash());
|
2015-10-14 12:49:29 +03:00
|
|
|
if (rule && !rule->name().isEmpty())
|
|
|
|
saveRule(rule);
|
|
|
|
}
|
2010-11-13 13:49:22 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRuleList::saveRule(const DownloadRulePtr &rule)
|
2010-11-13 13:49:22 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
qDebug() << Q_FUNC_INFO << rule->name();
|
|
|
|
Q_ASSERT(rule);
|
|
|
|
if (m_rules.contains(rule->name())) {
|
|
|
|
qDebug("This is an update, removing old rule first");
|
|
|
|
removeRule(rule->name());
|
|
|
|
}
|
|
|
|
m_rules.insert(rule->name(), rule);
|
|
|
|
// Update feedRules hashtable
|
|
|
|
foreach (const QString &feedUrl, rule->rssFeeds()) {
|
|
|
|
m_feedRules[feedUrl].append(rule->name());
|
|
|
|
}
|
|
|
|
qDebug() << Q_FUNC_INFO << "EXIT";
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
2010-11-07 16:06:00 +03:00
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRuleList::removeRule(const QString &name)
|
2010-11-07 16:06:00 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
qDebug() << Q_FUNC_INFO << name;
|
|
|
|
if (!m_rules.contains(name)) return;
|
2015-10-15 19:33:27 +03:00
|
|
|
DownloadRulePtr rule = m_rules.take(name);
|
2015-10-14 12:49:29 +03:00
|
|
|
// Update feedRules hashtable
|
|
|
|
foreach (const QString &feedUrl, rule->rssFeeds()) {
|
|
|
|
m_feedRules[feedUrl].removeOne(rule->name());
|
|
|
|
}
|
2010-11-07 16:06:00 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
void DownloadRuleList::renameRule(const QString &oldName, const QString &newName)
|
2010-11-13 13:49:22 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
if (!m_rules.contains(oldName)) return;
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
DownloadRulePtr rule = m_rules.take(oldName);
|
2015-10-14 12:49:29 +03:00
|
|
|
rule->setName(newName);
|
|
|
|
m_rules.insert(newName, rule);
|
|
|
|
// Update feedRules hashtable
|
|
|
|
foreach (const QString &feedUrl, rule->rssFeeds()) {
|
|
|
|
m_feedRules[feedUrl].replace(m_feedRules[feedUrl].indexOf(oldName), newName);
|
|
|
|
}
|
2010-11-13 13:49:22 +03:00
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
DownloadRulePtr DownloadRuleList::getRule(const QString &name) const
|
2010-11-13 13:49:22 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
return m_rules.value(name);
|
2010-11-07 16:06:00 +03:00
|
|
|
}
|
2010-11-13 15:08:29 +03:00
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
QStringList DownloadRuleList::ruleNames() const
|
2010-11-13 15:08:29 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
return m_rules.keys();
|
2010-11-13 15:08:29 +03:00
|
|
|
}
|
2010-11-13 15:33:52 +03:00
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
bool DownloadRuleList::isEmpty() const
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
return m_rules.isEmpty();
|
|
|
|
}
|
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
bool DownloadRuleList::serialize(const QString &path)
|
2010-11-13 15:33:52 +03:00
|
|
|
{
|
2015-10-14 12:49:29 +03:00
|
|
|
QFile f(path);
|
|
|
|
if (f.open(QIODevice::WriteOnly)) {
|
|
|
|
QDataStream out(&f);
|
|
|
|
out.setVersion(QDataStream::Qt_4_5);
|
|
|
|
out << toVariantHash();
|
|
|
|
f.close();
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2012-02-20 22:49:31 +04:00
|
|
|
return false;
|
2010-11-13 15:33:52 +03:00
|
|
|
}
|
2010-11-13 18:14:50 +03:00
|
|
|
|
2015-10-15 19:33:27 +03:00
|
|
|
bool DownloadRuleList::unserialize(const QString &path)
|
2015-10-14 12:49:29 +03:00
|
|
|
{
|
|
|
|
QFile f(path);
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|