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
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <QRegExp>
|
2010-11-13 18:14:50 +03:00
|
|
|
#include <QDebug>
|
2010-10-31 15:35:07 +03:00
|
|
|
|
|
|
|
#include "rssdownloadrule.h"
|
2010-11-13 13:49:22 +03:00
|
|
|
#include "preferences.h"
|
2010-10-31 15:35:07 +03:00
|
|
|
#include "qinisettings.h"
|
2010-11-13 22:36:46 +03:00
|
|
|
#include "rssfeed.h"
|
|
|
|
#include "rssarticle.h"
|
2010-10-31 15:35:07 +03:00
|
|
|
|
2011-04-18 14:36:19 +04:00
|
|
|
RssDownloadRule::RssDownloadRule(): m_enabled(false), m_useRegex(false)
|
2010-10-31 15:35:07 +03:00
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
bool RssDownloadRule::matches(const QString &article_title) const
|
|
|
|
{
|
2012-02-20 21:30:53 +04:00
|
|
|
foreach (const QString& token, m_mustContain) {
|
2012-02-20 22:49:31 +04:00
|
|
|
if (!token.isEmpty()) {
|
|
|
|
QRegExp reg(token, Qt::CaseInsensitive, m_useRegex ? QRegExp::RegExp : QRegExp::Wildcard);
|
|
|
|
if (reg.indexIn(article_title) < 0)
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
|
|
|
qDebug("Checking not matching tokens");
|
|
|
|
// Checking not matching
|
2012-02-20 21:30:53 +04:00
|
|
|
foreach (const QString& token, m_mustNotContain) {
|
2012-02-20 22:49:31 +04:00
|
|
|
if (!token.isEmpty()) {
|
|
|
|
QRegExp reg(token, Qt::CaseInsensitive, m_useRegex ? QRegExp::RegExp : QRegExp::Wildcard);
|
|
|
|
if (reg.indexIn(article_title) > -1)
|
|
|
|
return false;
|
|
|
|
}
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void RssDownloadRule::setMustContain(const QString &tokens)
|
|
|
|
{
|
2012-02-20 21:30:53 +04:00
|
|
|
if (m_useRegex)
|
2011-04-18 14:36:19 +04:00
|
|
|
m_mustContain = QStringList() << tokens;
|
|
|
|
else
|
|
|
|
m_mustContain = tokens.split(" ");
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void RssDownloadRule::setMustNotContain(const QString &tokens)
|
|
|
|
{
|
2012-02-20 21:30:53 +04:00
|
|
|
if (m_useRegex)
|
2011-04-18 14:36:19 +04:00
|
|
|
m_mustNotContain = QStringList() << tokens;
|
|
|
|
else
|
|
|
|
m_mustNotContain = tokens.split(QRegExp("[\\s|]"));
|
2010-10-31 15:35:07 +03:00
|
|
|
}
|
|
|
|
|
2012-02-20 22:49:31 +04:00
|
|
|
RssDownloadRulePtr RssDownloadRule::fromVariantHash(const QVariantHash &rule_hash)
|
2010-11-07 16:06:00 +03:00
|
|
|
{
|
2012-02-19 20:53:10 +04:00
|
|
|
RssDownloadRulePtr rule(new RssDownloadRule);
|
|
|
|
rule->setName(rule_hash.value("name").toString());
|
|
|
|
rule->setMustContain(rule_hash.value("must_contain").toString());
|
|
|
|
rule->setMustNotContain(rule_hash.value("must_not_contain").toString());
|
|
|
|
rule->setRssFeeds(rule_hash.value("affected_feeds").toStringList());
|
|
|
|
rule->setEnabled(rule_hash.value("enabled", false).toBool());
|
|
|
|
rule->setSavePath(rule_hash.value("save_path").toString());
|
|
|
|
rule->setLabel(rule_hash.value("label_assigned").toString());
|
|
|
|
rule->setUseRegex(rule_hash.value("use_regex", false).toBool());
|
2010-11-07 16:06:00 +03:00
|
|
|
return rule;
|
|
|
|
}
|
|
|
|
|
2010-11-13 13:49:22 +03:00
|
|
|
QVariantHash RssDownloadRule::toVariantHash() const
|
2010-11-07 16:06:00 +03:00
|
|
|
{
|
2010-11-13 13:49:22 +03:00
|
|
|
QVariantHash hash;
|
2010-11-07 16:06:00 +03:00
|
|
|
hash["name"] = m_name;
|
|
|
|
hash["must_contain"] = m_mustContain.join(" ");
|
|
|
|
hash["must_not_contain"] = m_mustNotContain.join(" ");
|
2010-11-13 13:49:22 +03:00
|
|
|
hash["save_path"] = m_savePath;
|
2010-11-07 16:06:00 +03:00
|
|
|
hash["affected_feeds"] = m_rssFeeds;
|
|
|
|
hash["enabled"] = m_enabled;
|
|
|
|
hash["label_assigned"] = m_label;
|
2011-04-18 14:36:19 +04:00
|
|
|
hash["use_regex"] = m_useRegex;
|
2010-11-07 16:06:00 +03:00
|
|
|
return hash;
|
|
|
|
}
|
|
|
|
|
2010-10-31 15:35:07 +03:00
|
|
|
bool RssDownloadRule::operator==(const RssDownloadRule &other) {
|
|
|
|
return m_name == other.name();
|
|
|
|
}
|
2010-11-13 13:49:22 +03:00
|
|
|
|
|
|
|
void RssDownloadRule::setSavePath(const QString &save_path)
|
|
|
|
{
|
2012-02-20 21:30:53 +04:00
|
|
|
if (!save_path.isEmpty() && QDir(save_path) != QDir(Preferences().getSavePath()))
|
2010-11-13 13:49:22 +03:00
|
|
|
m_savePath = save_path;
|
|
|
|
else
|
|
|
|
m_savePath = QString();
|
|
|
|
}
|
2010-11-13 22:36:46 +03:00
|
|
|
|
2012-02-19 20:53:10 +04:00
|
|
|
QStringList RssDownloadRule::findMatchingArticles(const RssFeedPtr& feed) const
|
2010-11-13 22:36:46 +03:00
|
|
|
{
|
|
|
|
QStringList ret;
|
2012-02-19 18:38:41 +04:00
|
|
|
const RssArticleHash& feed_articles = feed->articleHash();
|
2012-02-20 21:30:53 +04:00
|
|
|
for (RssArticleHash::ConstIterator artIt = feed_articles.begin(); artIt != feed_articles.end(); artIt++) {
|
2012-02-19 18:38:41 +04:00
|
|
|
const QString title = artIt.value()->title();
|
|
|
|
if (matches(title))
|
2011-01-29 17:10:08 +03:00
|
|
|
ret << title;
|
2010-11-13 22:36:46 +03:00
|
|
|
}
|
|
|
|
return ret;
|
|
|
|
}
|