2010-10-31 15:35:07 +03:00
|
|
|
/*
|
|
|
|
* 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
|
|
|
|
*/
|
|
|
|
|
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
|
|
|
|
2010-10-31 15:35:07 +03:00
|
|
|
#include "rssdownloadrulelist.h"
|
2010-11-13 22:36:46 +03:00
|
|
|
#include "rsssettings.h"
|
2010-10-31 15:35:07 +03:00
|
|
|
#include "qinisettings.h"
|
|
|
|
|
|
|
|
RssDownloadRuleList* RssDownloadRuleList::m_instance = 0;
|
|
|
|
|
|
|
|
RssDownloadRuleList::RssDownloadRuleList(){
|
|
|
|
loadRulesFromStorage();
|
|
|
|
}
|
|
|
|
|
|
|
|
RssDownloadRuleList* RssDownloadRuleList::instance()
|
|
|
|
{
|
|
|
|
if(!m_instance)
|
|
|
|
m_instance = new RssDownloadRuleList;
|
|
|
|
return m_instance;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RssDownloadRuleList::drop()
|
|
|
|
{
|
|
|
|
if(m_instance)
|
|
|
|
delete m_instance;
|
|
|
|
}
|
|
|
|
|
2010-11-13 13:49:22 +03:00
|
|
|
RssDownloadRule RssDownloadRuleList::findMatchingRule(const QString &feed_url, const QString &article_title) const
|
2010-10-31 15:35:07 +03:00
|
|
|
{
|
2010-11-17 23:21:03 +03:00
|
|
|
Q_ASSERT(RssSettings().isRssDownloadingEnabled());
|
2011-01-29 17:10:08 +03:00
|
|
|
const QStringList rule_names = m_feedRules.value(feed_url);
|
2010-11-13 13:49:22 +03:00
|
|
|
foreach(const QString &rule_name, rule_names) {
|
2011-01-29 17:10:08 +03:00
|
|
|
const RssDownloadRule &rule = m_rules[rule_name];
|
2010-11-13 13:49:22 +03:00
|
|
|
if(rule.isEnabled() && rule.matches(article_title)) return rule;
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
2010-11-13 13:49:22 +03:00
|
|
|
return RssDownloadRule();
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
|
|
|
|
2010-11-07 16:06:00 +03:00
|
|
|
void RssDownloadRuleList::saveRulesToStorage()
|
|
|
|
{
|
|
|
|
QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss");
|
2010-11-13 13:49:22 +03:00
|
|
|
qBTRSS.setValue("download_rules", toVariantHash());
|
2010-11-07 16:06:00 +03:00
|
|
|
}
|
|
|
|
|
2010-10-31 15:35:07 +03:00
|
|
|
void RssDownloadRuleList::loadRulesFromStorage()
|
|
|
|
{
|
|
|
|
QIniSettings qBTRSS("qBittorrent", "qBittorrent-rss");
|
|
|
|
if(qBTRSS.contains("feed_filters")) {
|
2010-11-13 18:14:50 +03:00
|
|
|
importFeedsInOldFormat(qBTRSS.value("feed_filters").toHash());
|
2010-11-07 16:06:00 +03:00
|
|
|
// Remove outdated rules
|
|
|
|
qBTRSS.remove("feed_filters");
|
|
|
|
// Save to new format
|
|
|
|
saveRulesToStorage();
|
|
|
|
return;
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
2010-11-07 16:06:00 +03:00
|
|
|
// Load from new format
|
2010-11-13 13:49:22 +03:00
|
|
|
loadRulesFromVariantHash(qBTRSS.value("download_rules").toHash());
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
|
|
|
|
2010-11-13 18:14:50 +03:00
|
|
|
void RssDownloadRuleList::importFeedsInOldFormat(const QHash<QString, QVariant> &rules)
|
2010-10-31 15:35:07 +03:00
|
|
|
{
|
|
|
|
foreach(const QString &feed_url, rules.keys()) {
|
2010-11-13 18:14:50 +03:00
|
|
|
importFeedRulesInOldFormat(feed_url, rules.value(feed_url).toHash());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RssDownloadRuleList::importFeedRulesInOldFormat(const QString &feed_url, const QHash<QString, QVariant> &rules)
|
|
|
|
{
|
|
|
|
foreach(const QString &rule_name, rules.keys()) {
|
|
|
|
RssDownloadRule rule = RssDownloadRule::fromOldFormat(rules.value(rule_name).toHash(), feed_url, rule_name);
|
|
|
|
if(!rule.isValid()) continue;
|
|
|
|
// Check for rule name clash
|
|
|
|
while(m_rules.contains(rule.name())) {
|
|
|
|
rule.setName(rule.name()+"_");
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
2010-11-13 18:14:50 +03:00
|
|
|
// Add the rule to the list
|
|
|
|
saveRule(rule);
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-13 13:49:22 +03:00
|
|
|
QVariantHash RssDownloadRuleList::toVariantHash() const
|
2010-10-31 15:35:07 +03:00
|
|
|
{
|
2010-11-13 13:49:22 +03:00
|
|
|
QVariantHash ret;
|
|
|
|
foreach(const RssDownloadRule &rule, m_rules.values()) {
|
|
|
|
ret.insert(rule.name(), rule.toVariantHash());
|
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RssDownloadRuleList::loadRulesFromVariantHash(const QVariantHash &h)
|
|
|
|
{
|
|
|
|
foreach(const QVariant& v, h.values()) {
|
|
|
|
RssDownloadRule rule = RssDownloadRule::fromNewFormat(v.toHash());
|
|
|
|
if(!rule.name().isEmpty()) {
|
|
|
|
saveRule(rule);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void RssDownloadRuleList::saveRule(const RssDownloadRule &rule)
|
|
|
|
{
|
2010-11-13 22:36:46 +03:00
|
|
|
qDebug() << Q_FUNC_INFO << rule.name();
|
2010-11-13 13:49:22 +03:00
|
|
|
Q_ASSERT(rule.isValid());
|
2010-11-13 21:08:09 +03:00
|
|
|
if(m_rules.contains(rule.name())) {
|
2010-11-13 23:08:44 +03:00
|
|
|
qDebug("This is an update, removing old rule first");
|
2010-11-13 21:08:09 +03:00
|
|
|
removeRule(rule.name());
|
|
|
|
}
|
2010-11-13 13:49:22 +03:00
|
|
|
m_rules.insert(rule.name(), rule);
|
2010-11-07 16:06:00 +03:00
|
|
|
// Update feedRules hashtable
|
2010-10-31 15:35:07 +03:00
|
|
|
foreach(const QString &feed_url, rule.rssFeeds()) {
|
2010-11-13 13:49:22 +03:00
|
|
|
m_feedRules[feed_url].append(rule.name());
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
2010-11-13 13:49:22 +03:00
|
|
|
// Save rules
|
|
|
|
saveRulesToStorage();
|
2010-11-13 23:08:44 +03:00
|
|
|
qDebug() << Q_FUNC_INFO << "EXIT";
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
2010-11-07 16:06:00 +03:00
|
|
|
|
2010-11-13 13:49:22 +03:00
|
|
|
void RssDownloadRuleList::removeRule(const QString &name)
|
2010-11-07 16:06:00 +03:00
|
|
|
{
|
2010-11-13 15:33:52 +03:00
|
|
|
qDebug() << Q_FUNC_INFO << name;
|
2010-11-13 13:49:22 +03:00
|
|
|
if(!m_rules.contains(name)) return;
|
|
|
|
const RssDownloadRule rule = m_rules.take(name);
|
|
|
|
// Update feedRules hashtable
|
|
|
|
foreach(const QString &feed_url, rule.rssFeeds()) {
|
2010-11-13 15:08:29 +03:00
|
|
|
m_feedRules[feed_url].removeOne(rule.name());
|
2010-11-07 16:06:00 +03:00
|
|
|
}
|
2010-11-13 13:49:22 +03:00
|
|
|
// Save rules
|
|
|
|
saveRulesToStorage();
|
2010-11-07 16:06:00 +03:00
|
|
|
}
|
|
|
|
|
2010-11-13 13:49:22 +03:00
|
|
|
void RssDownloadRuleList::renameRule(const QString &old_name, const QString &new_name)
|
|
|
|
{
|
|
|
|
if(!m_rules.contains(old_name)) return;
|
|
|
|
RssDownloadRule rule = m_rules.take(old_name);
|
|
|
|
rule.setName(new_name);
|
|
|
|
m_rules.insert(new_name, rule);
|
|
|
|
// Update feedRules hashtable
|
|
|
|
foreach(const QString &feed_url, rule.rssFeeds()) {
|
2010-11-13 15:08:29 +03:00
|
|
|
m_feedRules[feed_url].replace(m_feedRules[feed_url].indexOf(old_name), new_name);
|
2010-11-07 16:06:00 +03:00
|
|
|
}
|
2010-11-13 13:49:22 +03:00
|
|
|
// Save rules
|
|
|
|
saveRulesToStorage();
|
|
|
|
}
|
|
|
|
|
2011-01-29 17:10:08 +03:00
|
|
|
const RssDownloadRule RssDownloadRuleList::getRule(const QString &name) const
|
2010-11-13 13:49:22 +03:00
|
|
|
{
|
2011-04-16 14:20:45 +04:00
|
|
|
return m_rules.value(name);
|
2010-11-07 16:06:00 +03:00
|
|
|
}
|
2010-11-13 15:08:29 +03:00
|
|
|
|
|
|
|
bool RssDownloadRuleList::serialize(const QString& path)
|
|
|
|
{
|
|
|
|
QFile f(path);
|
|
|
|
if(f.open(QIODevice::WriteOnly)) {
|
|
|
|
QDataStream out(&f);
|
|
|
|
out.setVersion(QDataStream::Qt_4_5);
|
|
|
|
out << toVariantHash();
|
|
|
|
f.close();
|
|
|
|
return true;
|
|
|
|
} else {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2010-11-13 15:33:52 +03:00
|
|
|
|
|
|
|
bool RssDownloadRuleList::unserialize(const QString &path)
|
|
|
|
{
|
|
|
|
QFile f(path);
|
|
|
|
if(f.open(QIODevice::ReadOnly)) {
|
|
|
|
QDataStream in(&f);
|
2010-11-13 18:14:50 +03:00
|
|
|
if(path.endsWith(".filters", Qt::CaseInsensitive)) {
|
2010-11-13 15:33:52 +03:00
|
|
|
// Old format (< 2.5.0)
|
2010-11-13 18:14:50 +03:00
|
|
|
qDebug("Old serialization format detected, processing...");
|
2010-11-13 15:33:52 +03:00
|
|
|
in.setVersion(QDataStream::Qt_4_3);
|
|
|
|
QVariantHash tmp;
|
|
|
|
in >> tmp;
|
|
|
|
f.close();
|
|
|
|
if(tmp.isEmpty()) return false;
|
2010-11-13 18:14:50 +03:00
|
|
|
qDebug("Processing was successful!");
|
|
|
|
// Unfortunately the feed_url is lost
|
|
|
|
importFeedRulesInOldFormat("", tmp);
|
2010-11-13 15:33:52 +03:00
|
|
|
} else {
|
2010-11-13 18:14:50 +03:00
|
|
|
qDebug("New serialization format detected, processing...");
|
2010-11-13 15:33:52 +03:00
|
|
|
in.setVersion(QDataStream::Qt_4_5);
|
|
|
|
QVariantHash tmp;
|
|
|
|
in >> tmp;
|
|
|
|
f.close();
|
|
|
|
if(tmp.isEmpty()) return false;
|
2010-11-13 18:14:50 +03:00
|
|
|
qDebug("Processing was successful!");
|
2010-11-13 15:33:52 +03:00
|
|
|
loadRulesFromVariantHash(tmp);
|
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
2010-11-13 18:14:50 +03:00
|
|
|
qDebug("Error: could not open file at %s", qPrintable(path));
|
2010-11-13 15:33:52 +03:00
|
|
|
return false;
|
|
|
|
}
|
2010-11-13 18:14:50 +03:00
|
|
|
|