Some work about adaptive color scheme for Web UI (PR #19901)
http://[316:c51a:62a3:8b9::4]/d4708/qBittorrent/src/branch/adaptive-webui
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
646 lines
24 KiB
646 lines
24 KiB
15 years ago
|
/*
|
||
|
* 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
|
||
|
*/
|
||
|
|
||
14 years ago
|
#include <QMessageBox>
|
||
|
#include <QFileDialog>
|
||
|
#include <QDebug>
|
||
14 years ago
|
#include <QMenu>
|
||
|
#include <QCursor>
|
||
14 years ago
|
|
||
14 years ago
|
#include "automatedrssdownloader.h"
|
||
|
#include "ui_automatedrssdownloader.h"
|
||
14 years ago
|
#include "rssdownloadrulelist.h"
|
||
|
#include "preferences.h"
|
||
14 years ago
|
#include "rssmanager.h"
|
||
|
#include "rssfeed.h"
|
||
14 years ago
|
#include "iconprovider.h"
|
||
11 years ago
|
#include "autoexpandabledialog.h"
|
||
11 years ago
|
#include "fs_utils.h"
|
||
15 years ago
|
|
||
13 years ago
|
AutomatedRssDownloader::AutomatedRssDownloader(const QWeakPointer<RssManager>& manager, QWidget *parent) :
|
||
14 years ago
|
QDialog(parent),
|
||
14 years ago
|
ui(new Ui::AutomatedRssDownloader),
|
||
13 years ago
|
m_manager(manager), m_editedRule(0)
|
||
15 years ago
|
{
|
||
14 years ago
|
ui->setupUi(this);
|
||
14 years ago
|
// Icons
|
||
14 years ago
|
ui->removeRuleBtn->setIcon(IconProvider::instance()->getIcon("list-remove"));
|
||
|
ui->addRuleBtn->setIcon(IconProvider::instance()->getIcon("list-add"));
|
||
14 years ago
|
|
||
14 years ago
|
// Ui Settings
|
||
14 years ago
|
ui->listRules->setSortingEnabled(true);
|
||
14 years ago
|
ui->listRules->setSelectionMode(QAbstractItemView::ExtendedSelection);
|
||
14 years ago
|
ui->treeMatchingArticles->setSortingEnabled(true);
|
||
14 years ago
|
ui->hsplitter->setCollapsible(0, false);
|
||
|
ui->hsplitter->setCollapsible(1, false);
|
||
|
ui->hsplitter->setCollapsible(2, true); // Only the preview list is collapsible
|
||
14 years ago
|
bool ok; Q_UNUSED(ok);
|
||
|
ok = connect(ui->checkRegex, SIGNAL(toggled(bool)), SLOT(updateFieldsToolTips(bool)));
|
||
|
Q_ASSERT(ok);
|
||
|
ok = connect(ui->listRules, SIGNAL(customContextMenuRequested(const QPoint&)), this, SLOT(displayRulesListMenu(const QPoint&)));
|
||
|
Q_ASSERT(ok);
|
||
13 years ago
|
m_ruleList = manager.toStrongRef()->downloadRules();
|
||
12 years ago
|
m_editableRuleList = new RssDownloadRuleList; // Read rule list from disk
|
||
11 years ago
|
m_episodeValidator = new QRegExpValidator(
|
||
|
QRegExp("^(^[1-9]{1,1}\\d{0,3}x([1-9]{1,1}\\d{0,3}(-([1-9]{1,1}\\d{0,3})?)?;){1,}){1,1}",
|
||
|
Qt::CaseInsensitive),
|
||
|
ui->lineEFilter);
|
||
|
ui->lineEFilter->setValidator(m_episodeValidator);
|
||
11 years ago
|
QString tip = "<p>" + tr("Matches articles based on episode filter.") + "</p><p><b>" + tr("Example: ") +
|
||
|
"1x2;8-15;5;30-;</b>" + tr(" will match 2, 5, 8 through 15, 30 and onward episodes of season one", "example X will match") + "</p>";
|
||
|
tip += "<p>" + tr("Episode filter rules: ") + "</p><ul><li>" + tr("Season number is a mandatory non-zero value") + "</li>" +
|
||
|
"<li>" + tr("Episode number is a mandatory non-zero value") + "</li>" +
|
||
|
"<li>" + tr("Filter must end with semicolon") + "</li>" +
|
||
|
"<li>" + tr("Three range types for episodes are supported: ") + "</li>" + "<li><ul>"
|
||
|
"<li>" + tr("Single number: <b>1x25;</b> matches episode 25 of season one") + "</li>" +
|
||
|
"<li>" + tr("Normal range: <b>1x25-40;</b> matches episodes 25 through 40 of season one") + "</li>" +
|
||
10 years ago
|
"<li>" + tr("Infinite range: <b>1x25-;</b> matches episodes 25 and upward of season one") + "</li>" + "</ul></li></ul>";
|
||
11 years ago
|
ui->lineEFilter->setToolTip(tip);
|
||
14 years ago
|
initLabelCombobox();
|
||
|
loadFeedList();
|
||
|
loadSettings();
|
||
14 years ago
|
ok = connect(ui->listRules, SIGNAL(itemSelectionChanged()), SLOT(updateRuleDefinitionBox()));
|
||
|
Q_ASSERT(ok);
|
||
|
ok = connect(ui->listRules, SIGNAL(itemSelectionChanged()), SLOT(updateFeedList()));
|
||
|
Q_ASSERT(ok);
|
||
|
ok = connect(ui->listFeeds, SIGNAL(itemChanged(QListWidgetItem*)), SLOT(handleFeedCheckStateChange(QListWidgetItem*)));
|
||
|
Q_ASSERT(ok);
|
||
14 years ago
|
// Update matching articles when necessary
|
||
14 years ago
|
ok = connect(ui->lineContains, SIGNAL(textEdited(QString)), SLOT(updateMatchingArticles()));
|
||
|
Q_ASSERT(ok);
|
||
14 years ago
|
ok = connect(ui->lineContains, SIGNAL(textEdited(QString)), SLOT(updateMustLineValidity()));
|
||
|
Q_ASSERT(ok);
|
||
14 years ago
|
ok = connect(ui->lineNotContains, SIGNAL(textEdited(QString)), SLOT(updateMatchingArticles()));
|
||
|
Q_ASSERT(ok);
|
||
14 years ago
|
ok = connect(ui->lineNotContains, SIGNAL(textEdited(QString)), SLOT(updateMustNotLineValidity()));
|
||
|
Q_ASSERT(ok);
|
||
12 years ago
|
ok = connect(ui->checkRegex, SIGNAL(stateChanged(int)), SLOT(updateMatchingArticles()));
|
||
|
Q_ASSERT(ok);
|
||
|
ok = connect(ui->checkRegex, SIGNAL(stateChanged(int)), SLOT(updateMustLineValidity()));
|
||
|
Q_ASSERT(ok);
|
||
|
ok = connect(ui->checkRegex, SIGNAL(stateChanged(int)), SLOT(updateMustNotLineValidity()));
|
||
|
Q_ASSERT(ok);
|
||
12 years ago
|
ok = connect(this, SIGNAL(finished(int)), SLOT(on_finished(int)));
|
||
|
Q_ASSERT(ok);
|
||
11 years ago
|
ok = connect(ui->lineEFilter, SIGNAL(textEdited(QString)), SLOT(updateMatchingArticles()));
|
||
|
Q_ASSERT(ok);
|
||
11 years ago
|
editHotkey = new QShortcut(QKeySequence("F2"), ui->listRules, 0, 0, Qt::WidgetShortcut);
|
||
|
ok = connect(editHotkey, SIGNAL(activated()), SLOT(renameSelectedRule()));
|
||
|
Q_ASSERT(ok);
|
||
|
ok = connect(ui->listRules, SIGNAL(doubleClicked(QModelIndex)), SLOT(renameSelectedRule()));
|
||
|
Q_ASSERT(ok);
|
||
|
deleteHotkey = new QShortcut(QKeySequence(QKeySequence::Delete), ui->listRules, 0, 0, Qt::WidgetShortcut);
|
||
|
ok = connect(deleteHotkey, SIGNAL(activated()), SLOT(on_removeRuleBtn_clicked()));
|
||
|
Q_ASSERT(ok);
|
||
14 years ago
|
updateRuleDefinitionBox();
|
||
14 years ago
|
updateFeedList();
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
AutomatedRssDownloader::~AutomatedRssDownloader()
|
||
15 years ago
|
{
|
||
14 years ago
|
qDebug() << Q_FUNC_INFO;
|
||
11 years ago
|
delete editHotkey;
|
||
|
delete deleteHotkey;
|
||
14 years ago
|
delete ui;
|
||
12 years ago
|
delete m_editableRuleList;
|
||
11 years ago
|
delete m_episodeValidator;
|
||
15 years ago
|
}
|
||
|
|
||
14 years ago
|
void AutomatedRssDownloader::loadSettings()
|
||
|
{
|
||
14 years ago
|
// load dialog geometry
|
||
11 years ago
|
const Preferences* const pref = Preferences::instance();
|
||
|
restoreGeometry(pref->getRssGeometry());
|
||
|
ui->checkEnableDownloader->setChecked(pref->isRssDownloadingEnabled());
|
||
|
ui->hsplitter->restoreState(pref->getRssHSplitterSizes());
|
||
14 years ago
|
// Display download rules
|
||
|
loadRulesList();
|
||
14 years ago
|
}
|
||
|
|
||
|
void AutomatedRssDownloader::saveSettings()
|
||
|
{
|
||
11 years ago
|
Preferences::instance()->setRssDownloadingEnabled(ui->checkEnableDownloader->isChecked());
|
||
14 years ago
|
// Save dialog geometry
|
||
11 years ago
|
Preferences* const pref = Preferences::instance();
|
||
|
pref->setRssGeometry(saveGeometry());
|
||
|
pref->setRssHSplitterSizes(ui->hsplitter->saveState());
|
||
14 years ago
|
}
|
||
|
|
||
|
void AutomatedRssDownloader::loadRulesList()
|
||
|
{
|
||
14 years ago
|
// Make sure we save the current item before clearing
|
||
13 years ago
|
if (m_editedRule) {
|
||
14 years ago
|
saveEditedRule();
|
||
14 years ago
|
}
|
||
|
ui->listRules->clear();
|
||
12 years ago
|
foreach (const QString &rule_name, m_editableRuleList->ruleNames()) {
|
||
14 years ago
|
QListWidgetItem *item = new QListWidgetItem(rule_name, ui->listRules);
|
||
|
item->setFlags(item->flags()|Qt::ItemIsUserCheckable);
|
||
12 years ago
|
if (m_editableRuleList->getRule(rule_name)->isEnabled())
|
||
14 years ago
|
item->setCheckState(Qt::Checked);
|
||
|
else
|
||
|
item->setCheckState(Qt::Unchecked);
|
||
|
}
|
||
13 years ago
|
if (ui->listRules->count() > 0 && !ui->listRules->currentItem())
|
||
14 years ago
|
ui->listRules->setCurrentRow(0);
|
||
14 years ago
|
}
|
||
|
|
||
|
void AutomatedRssDownloader::loadFeedList()
|
||
|
{
|
||
11 years ago
|
const Preferences* const pref = Preferences::instance();
|
||
|
const QStringList feed_aliases = pref->getRssFeedsAliases();
|
||
|
const QStringList feed_urls = pref->getRssFeedsUrls();
|
||
14 years ago
|
QStringList existing_urls;
|
||
13 years ago
|
for (int i=0; i<feed_aliases.size(); ++i) {
|
||
14 years ago
|
QString feed_url = feed_urls.at(i);
|
||
|
feed_url = feed_url.split("\\").last();
|
||
|
qDebug() << Q_FUNC_INFO << feed_url;
|
||
13 years ago
|
if (existing_urls.contains(feed_url)) continue;
|
||
14 years ago
|
QListWidgetItem *item = new QListWidgetItem(feed_aliases.at(i), ui->listFeeds);
|
||
14 years ago
|
item->setData(Qt::UserRole, feed_url);
|
||
14 years ago
|
item->setFlags(item->flags()|Qt::ItemIsUserCheckable);
|
||
14 years ago
|
existing_urls << feed_url;
|
||
14 years ago
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
void AutomatedRssDownloader::updateFeedList()
|
||
14 years ago
|
{
|
||
14 years ago
|
disconnect(ui->listFeeds, SIGNAL(itemChanged(QListWidgetItem*)), this, SLOT(handleFeedCheckStateChange(QListWidgetItem*)));
|
||
13 years ago
|
for (int i=0; i<ui->listFeeds->count(); ++i) {
|
||
14 years ago
|
QListWidgetItem *item = ui->listFeeds->item(i);
|
||
|
const QString feed_url = item->data(Qt::UserRole).toString();
|
||
14 years ago
|
bool all_enabled = false;
|
||
13 years ago
|
foreach (const QListWidgetItem *ruleItem, ui->listRules->selectedItems()) {
|
||
12 years ago
|
RssDownloadRulePtr rule = m_editableRuleList->getRule(ruleItem->text());
|
||
13 years ago
|
if (!rule) continue;
|
||
13 years ago
|
qDebug() << "Rule" << rule->name() << "affects" << rule->rssFeeds().size() << "feeds.";
|
||
13 years ago
|
foreach (QString test, rule->rssFeeds()) {
|
||
14 years ago
|
qDebug() << "Feed is " << test;
|
||
|
}
|
||
13 years ago
|
if (rule->rssFeeds().contains(feed_url)) {
|
||
13 years ago
|
qDebug() << "Rule " << rule->name() << " affects feed " << feed_url;
|
||
14 years ago
|
all_enabled = true;
|
||
|
} else {
|
||
13 years ago
|
qDebug() << "Rule " << rule->name() << " does NOT affect feed " << feed_url;
|
||
14 years ago
|
all_enabled = false;
|
||
|
break;
|
||
|
}
|
||
14 years ago
|
}
|
||
13 years ago
|
if (all_enabled)
|
||
14 years ago
|
item->setCheckState(Qt::Checked);
|
||
|
else
|
||
|
item->setCheckState(Qt::Unchecked);
|
||
|
}
|
||
14 years ago
|
ui->listFeeds->setEnabled(!ui->listRules->selectedItems().isEmpty());
|
||
14 years ago
|
connect(ui->listFeeds, SIGNAL(itemChanged(QListWidgetItem*)), SLOT(handleFeedCheckStateChange(QListWidgetItem*)));
|
||
14 years ago
|
updateMatchingArticles();
|
||
14 years ago
|
}
|
||
|
|
||
|
bool AutomatedRssDownloader::isRssDownloaderEnabled() const
|
||
|
{
|
||
|
return ui->checkEnableDownloader->isChecked();
|
||
|
}
|
||
|
|
||
14 years ago
|
void AutomatedRssDownloader::updateRuleDefinitionBox()
|
||
14 years ago
|
{
|
||
14 years ago
|
qDebug() << Q_FUNC_INFO;
|
||
14 years ago
|
// Save previous rule first
|
||
|
saveEditedRule();
|
||
14 years ago
|
// Update rule definition box
|
||
14 years ago
|
const QList<QListWidgetItem*> selection = ui->listRules->selectedItems();
|
||
13 years ago
|
if (selection.count() == 1) {
|
||
14 years ago
|
m_editedRule = selection.first();
|
||
13 years ago
|
RssDownloadRulePtr rule = getCurrentRule();
|
||
|
if (rule) {
|
||
|
ui->lineContains->setText(rule->mustContain());
|
||
|
ui->lineNotContains->setText(rule->mustNotContain());
|
||
11 years ago
|
QString ep = rule->episodeFilter();
|
||
|
if (!ep.isEmpty())
|
||
|
ui->lineEFilter->setText(ep);
|
||
|
else
|
||
|
ui->lineEFilter->clear();
|
||
13 years ago
|
ui->saveDiffDir_check->setChecked(!rule->savePath().isEmpty());
|
||
11 years ago
|
ui->lineSavePath->setText(fsutils::toNativePath(rule->savePath()));
|
||
13 years ago
|
ui->checkRegex->setChecked(rule->useRegex());
|
||
|
if (rule->label().isEmpty()) {
|
||
14 years ago
|
ui->comboLabel->setCurrentIndex(-1);
|
||
|
ui->comboLabel->clearEditText();
|
||
|
} else {
|
||
13 years ago
|
ui->comboLabel->setCurrentIndex(ui->comboLabel->findText(rule->label()));
|
||
14 years ago
|
}
|
||
10 years ago
|
ui->comboAddPaused->setCurrentIndex(rule->addPaused());
|
||
11 years ago
|
ui->spinIgnorePeriod->setValue(rule->ignoreDays());
|
||
|
QDateTime dateTime = rule->lastMatch();
|
||
|
QString lMatch = tr("Last match: ");
|
||
|
if (dateTime.isValid())
|
||
|
lMatch += QString::number(dateTime.daysTo(QDateTime::currentDateTime())) + tr(" days ago.");
|
||
|
else
|
||
|
lMatch += tr("Unknown");
|
||
|
ui->lblLastMatch->setText(lMatch);
|
||
14 years ago
|
updateMustLineValidity();
|
||
|
updateMustNotLineValidity();
|
||
14 years ago
|
} else {
|
||
14 years ago
|
// New rule
|
||
|
clearRuleDefinitionBox();
|
||
|
ui->lineContains->setText(selection.first()->text());
|
||
10 years ago
|
ui->comboAddPaused->setCurrentIndex(0);
|
||
14 years ago
|
}
|
||
14 years ago
|
updateFieldsToolTips(ui->checkRegex->isChecked());
|
||
14 years ago
|
// Enable
|
||
|
ui->ruleDefBox->setEnabled(true);
|
||
|
} else {
|
||
14 years ago
|
m_editedRule = 0;
|
||
14 years ago
|
// Clear
|
||
14 years ago
|
clearRuleDefinitionBox();
|
||
|
ui->ruleDefBox->setEnabled(false);
|
||
14 years ago
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
void AutomatedRssDownloader::clearRuleDefinitionBox()
|
||
|
{
|
||
|
ui->lineContains->clear();
|
||
|
ui->lineNotContains->clear();
|
||
|
ui->saveDiffDir_check->setChecked(false);
|
||
|
ui->lineSavePath->clear();
|
||
|
ui->comboLabel->clearEditText();
|
||
14 years ago
|
ui->checkRegex->setChecked(false);
|
||
11 years ago
|
ui->spinIgnorePeriod->setValue(0);
|
||
14 years ago
|
updateFieldsToolTips(ui->checkRegex->isChecked());
|
||
14 years ago
|
updateMustLineValidity();
|
||
|
updateMustNotLineValidity();
|
||
14 years ago
|
}
|
||
|
|
||
13 years ago
|
RssDownloadRulePtr AutomatedRssDownloader::getCurrentRule() const
|
||
14 years ago
|
{
|
||
|
QListWidgetItem * current_item = ui->listRules->currentItem();
|
||
13 years ago
|
if (current_item)
|
||
12 years ago
|
return m_editableRuleList->getRule(current_item->text());
|
||
13 years ago
|
return RssDownloadRulePtr();
|
||
14 years ago
|
}
|
||
|
|
||
|
void AutomatedRssDownloader::initLabelCombobox()
|
||
|
{
|
||
|
// Load custom labels
|
||
11 years ago
|
const QStringList customLabels = Preferences::instance()->getTorrentLabels();
|
||
13 years ago
|
foreach (const QString& label, customLabels) {
|
||
14 years ago
|
ui->comboLabel->addItem(label);
|
||
|
}
|
||
|
}
|
||
|
|
||
14 years ago
|
void AutomatedRssDownloader::saveEditedRule()
|
||
14 years ago
|
{
|
||
13 years ago
|
if (!m_editedRule) return;
|
||
14 years ago
|
qDebug() << Q_FUNC_INFO << m_editedRule;
|
||
13 years ago
|
if (ui->listRules->findItems(m_editedRule->text(), Qt::MatchExactly).isEmpty()) {
|
||
14 years ago
|
qDebug() << "Could not find rule" << m_editedRule->text() << "in the UI list";
|
||
14 years ago
|
qDebug() << "Probably removed the item, no need to save it";
|
||
|
return;
|
||
|
}
|
||
12 years ago
|
RssDownloadRulePtr rule = m_editableRuleList->getRule(m_editedRule->text());
|
||
13 years ago
|
if (!rule) {
|
||
13 years ago
|
rule = RssDownloadRulePtr(new RssDownloadRule);
|
||
13 years ago
|
rule->setName(m_editedRule->text());
|
||
14 years ago
|
}
|
||
13 years ago
|
if (m_editedRule->checkState() == Qt::Unchecked)
|
||
|
rule->setEnabled(false);
|
||
14 years ago
|
else
|
||
13 years ago
|
rule->setEnabled(true);
|
||
|
rule->setUseRegex(ui->checkRegex->isChecked());
|
||
|
rule->setMustContain(ui->lineContains->text());
|
||
|
rule->setMustNotContain(ui->lineNotContains->text());
|
||
11 years ago
|
rule->setEpisodeFilter(ui->lineEFilter->text());
|
||
13 years ago
|
if (ui->saveDiffDir_check->isChecked())
|
||
13 years ago
|
rule->setSavePath(ui->lineSavePath->text());
|
||
14 years ago
|
else
|
||
13 years ago
|
rule->setSavePath("");
|
||
|
rule->setLabel(ui->comboLabel->currentText());
|
||
10 years ago
|
rule->setAddPaused(RssDownloadRule::AddPausedState(ui->comboAddPaused->currentIndex()));
|
||
14 years ago
|
// Save new label
|
||
13 years ago
|
if (!rule->label().isEmpty())
|
||
11 years ago
|
Preferences::instance()->addTorrentLabel(rule->label());
|
||
11 years ago
|
rule->setIgnoreDays(ui->spinIgnorePeriod->value());
|
||
13 years ago
|
//rule->setRssFeeds(getSelectedFeeds());
|
||
14 years ago
|
// Save it
|
||
12 years ago
|
m_editableRuleList->saveRule(rule);
|
||
14 years ago
|
}
|
||
|
|
||
|
|
||
|
void AutomatedRssDownloader::on_addRuleBtn_clicked()
|
||
|
{
|
||
|
// Ask for a rule name
|
||
11 years ago
|
const QString rule_name = AutoExpandableDialog::getText(this, tr("New rule name"), tr("Please type the name of the new download rule."));
|
||
13 years ago
|
if (rule_name.isEmpty()) return;
|
||
14 years ago
|
// Check if this rule name already exists
|
||
12 years ago
|
if (m_editableRuleList->getRule(rule_name)) {
|
||
14 years ago
|
QMessageBox::warning(this, tr("Rule name conflict"), tr("A rule with this name already exists, please choose another name."));
|
||
|
return;
|
||
|
}
|
||
|
// Add the new rule to the list
|
||
13 years ago
|
QListWidgetItem * item = new QListWidgetItem(rule_name, ui->listRules);
|
||
14 years ago
|
item->setFlags(item->flags()|Qt::ItemIsUserCheckable);
|
||
|
item->setCheckState(Qt::Checked); // Enable as a default
|
||
14 years ago
|
ui->listRules->clearSelection();
|
||
14 years ago
|
ui->listRules->setCurrentItem(item);
|
||
|
}
|
||
|
|
||
|
void AutomatedRssDownloader::on_removeRuleBtn_clicked()
|
||
|
{
|
||
14 years ago
|
const QList<QListWidgetItem*> selection = ui->listRules->selectedItems();
|
||
13 years ago
|
if (selection.isEmpty()) return;
|
||
14 years ago
|
// Ask for confirmation
|
||
14 years ago
|
QString confirm_text;
|
||
13 years ago
|
if (selection.count() == 1)
|
||
14 years ago
|
confirm_text = tr("Are you sure you want to remove the download rule named %1?").arg(selection.first()->text());
|
||
|
else
|
||
|
confirm_text = tr("Are you sure you want to remove the selected download rules?");
|
||
13 years ago
|
if (QMessageBox::question(this, tr("Rule deletion confirmation"), confirm_text, QMessageBox::Yes, QMessageBox::No) != QMessageBox::Yes)
|
||
14 years ago
|
return;
|
||
13 years ago
|
foreach (QListWidgetItem *item, selection) {
|
||
14 years ago
|
// Actually remove the item
|
||
|
ui->listRules->removeItemWidget(item);
|
||
14 years ago
|
const QString rule_name = item->text();
|
||
14 years ago
|
// Clean up memory
|
||
|
delete item;
|
||
14 years ago
|
qDebug("Removed item for the UI list");
|
||
12 years ago
|
// Remove it from the m_editableRuleList
|
||
|
m_editableRuleList->removeRule(rule_name);
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
|
|
||
|
void AutomatedRssDownloader::on_browseSP_clicked()
|
||
|
{
|
||
|
QString save_path = QFileDialog::getExistingDirectory(this, tr("Destination directory"), QDir::homePath());
|
||
13 years ago
|
if (!save_path.isEmpty())
|
||
11 years ago
|
ui->lineSavePath->setText(fsutils::toNativePath(save_path));
|
||
15 years ago
|
}
|
||
14 years ago
|
|
||
|
void AutomatedRssDownloader::on_exportBtn_clicked()
|
||
|
{
|
||
12 years ago
|
if (m_editableRuleList->isEmpty()) {
|
||
14 years ago
|
QMessageBox::warning(this, tr("Invalid action"), tr("The list is empty, there is nothing to export."));
|
||
|
return;
|
||
|
}
|
||
|
// Ask for a save path
|
||
14 years ago
|
QString save_path = QFileDialog::getSaveFileName(this, tr("Where would you like to save the list?"), QDir::homePath(), tr("Rules list (*.rssrules)"));
|
||
13 years ago
|
if (save_path.isEmpty()) return;
|
||
|
if (!save_path.endsWith(".rssrules", Qt::CaseInsensitive))
|
||
14 years ago
|
save_path += ".rssrules";
|
||
12 years ago
|
if (!m_editableRuleList->serialize(save_path)) {
|
||
14 years ago
|
QMessageBox::warning(this, tr("I/O Error"), tr("Failed to create the destination file"));
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void AutomatedRssDownloader::on_importBtn_clicked()
|
||
|
{
|
||
14 years ago
|
// Ask for filter path
|
||
10 years ago
|
QString load_path = QFileDialog::getOpenFileName(this, tr("Please point to the RSS download rules file"), QDir::homePath(), tr("Rules list")+QString(" (*.rssrules *.filters)"));
|
||
13 years ago
|
if (load_path.isEmpty() || !QFile::exists(load_path)) return;
|
||
14 years ago
|
// Load it
|
||
12 years ago
|
if (!m_editableRuleList->unserialize(load_path)) {
|
||
14 years ago
|
QMessageBox::warning(this, tr("Import Error"), tr("Failed to import the selected rules file"));
|
||
|
return;
|
||
|
}
|
||
14 years ago
|
// Reload the rule list
|
||
|
loadRulesList();
|
||
14 years ago
|
}
|
||
14 years ago
|
|
||
|
void AutomatedRssDownloader::displayRulesListMenu(const QPoint &pos)
|
||
|
{
|
||
|
Q_UNUSED(pos);
|
||
|
QMenu menu;
|
||
13 years ago
|
QAction *addAct = menu.addAction(IconProvider::instance()->getIcon("list-add"), tr("Add new rule..."));
|
||
14 years ago
|
QAction *delAct = 0;
|
||
|
QAction *renameAct = 0;
|
||
14 years ago
|
const QList<QListWidgetItem*> selection = ui->listRules->selectedItems();
|
||
13 years ago
|
if (!selection.isEmpty()) {
|
||
|
if (selection.count() == 1) {
|
||
14 years ago
|
delAct = menu.addAction(IconProvider::instance()->getIcon("list-remove"), tr("Delete rule"));
|
||
14 years ago
|
menu.addSeparator();
|
||
13 years ago
|
renameAct = menu.addAction(IconProvider::instance()->getIcon("edit-rename"), tr("Rename rule..."));
|
||
14 years ago
|
} else {
|
||
14 years ago
|
delAct = menu.addAction(IconProvider::instance()->getIcon("list-remove"), tr("Delete selected rules"));
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
|
QAction *act = menu.exec(QCursor::pos());
|
||
13 years ago
|
if (!act) return;
|
||
|
if (act == addAct) {
|
||
14 years ago
|
on_addRuleBtn_clicked();
|
||
|
return;
|
||
|
}
|
||
13 years ago
|
if (act == delAct) {
|
||
14 years ago
|
on_removeRuleBtn_clicked();
|
||
|
return;
|
||
|
}
|
||
13 years ago
|
if (act == renameAct) {
|
||
14 years ago
|
renameSelectedRule();
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void AutomatedRssDownloader::renameSelectedRule()
|
||
|
{
|
||
11 years ago
|
const QList<QListWidgetItem*> selection = ui->listRules->selectedItems();
|
||
|
if (selection.isEmpty())
|
||
|
return;
|
||
|
|
||
|
QListWidgetItem *item = selection.first();
|
||
14 years ago
|
forever {
|
||
11 years ago
|
QString new_name = AutoExpandableDialog::getText(this, tr("Rule renaming"), tr("Please type the new rule name"), QLineEdit::Normal, item->text());
|
||
14 years ago
|
new_name = new_name.trimmed();
|
||
13 years ago
|
if (new_name.isEmpty()) return;
|
||
12 years ago
|
if (m_editableRuleList->ruleNames().contains(new_name, Qt::CaseInsensitive)) {
|
||
14 years ago
|
QMessageBox::warning(this, tr("Rule name conflict"), tr("A rule with this name already exists, please choose another name."));
|
||
|
} else {
|
||
|
// Rename the rule
|
||
12 years ago
|
m_editableRuleList->renameRule(item->text(), new_name);
|
||
14 years ago
|
item->setText(new_name);
|
||
|
return;
|
||
|
}
|
||
|
}
|
||
|
}
|
||
14 years ago
|
|
||
|
void AutomatedRssDownloader::handleFeedCheckStateChange(QListWidgetItem *feed_item)
|
||
|
{
|
||
13 years ago
|
if (ui->ruleDefBox->isEnabled()) {
|
||
14 years ago
|
// Make sure the current rule is saved
|
||
14 years ago
|
saveEditedRule();
|
||
14 years ago
|
}
|
||
14 years ago
|
const QString feed_url = feed_item->data(Qt::UserRole).toString();
|
||
14 years ago
|
foreach (QListWidgetItem* rule_item, ui->listRules->selectedItems()) {
|
||
12 years ago
|
RssDownloadRulePtr rule = m_editableRuleList->getRule(rule_item->text());
|
||
13 years ago
|
Q_ASSERT(rule);
|
||
|
QStringList affected_feeds = rule->rssFeeds();
|
||
13 years ago
|
if (feed_item->checkState() == Qt::Checked) {
|
||
|
if (!affected_feeds.contains(feed_url))
|
||
14 years ago
|
affected_feeds << feed_url;
|
||
|
} else {
|
||
13 years ago
|
if (affected_feeds.contains(feed_url))
|
||
14 years ago
|
affected_feeds.removeOne(feed_url);
|
||
|
}
|
||
|
// Save the updated rule
|
||
13 years ago
|
if (affected_feeds.size() != rule->rssFeeds().size()) {
|
||
13 years ago
|
rule->setRssFeeds(affected_feeds);
|
||
12 years ago
|
m_editableRuleList->saveRule(rule);
|
||
14 years ago
|
}
|
||
14 years ago
|
}
|
||
14 years ago
|
// Update Matching articles
|
||
|
updateMatchingArticles();
|
||
|
}
|
||
|
|
||
|
void AutomatedRssDownloader::updateMatchingArticles()
|
||
|
{
|
||
|
ui->treeMatchingArticles->clear();
|
||
13 years ago
|
RssManagerPtr manager = m_manager.toStrongRef();
|
||
|
if (!manager)
|
||
|
return;
|
||
|
const QHash<QString, RssFeedPtr> all_feeds = manager->getAllFeedsAsHash();
|
||
14 years ago
|
|
||
13 years ago
|
foreach (const QListWidgetItem *rule_item, ui->listRules->selectedItems()) {
|
||
12 years ago
|
RssDownloadRulePtr rule = m_editableRuleList->getRule(rule_item->text());
|
||
13 years ago
|
if (!rule) continue;
|
||
|
foreach (const QString &feed_url, rule->rssFeeds()) {
|
||
14 years ago
|
qDebug() << Q_FUNC_INFO << feed_url;
|
||
13 years ago
|
if (!all_feeds.contains(feed_url)) continue; // Feed was removed
|
||
13 years ago
|
RssFeedPtr feed = all_feeds.value(feed_url);
|
||
14 years ago
|
Q_ASSERT(feed);
|
||
13 years ago
|
if (!feed) continue;
|
||
13 years ago
|
const QStringList matching_articles = rule->findMatchingArticles(feed);
|
||
13 years ago
|
if (!matching_articles.isEmpty())
|
||
14 years ago
|
addFeedArticlesToTree(feed, matching_articles);
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
13 years ago
|
void AutomatedRssDownloader::addFeedArticlesToTree(const RssFeedPtr& feed, const QStringList &articles)
|
||
14 years ago
|
{
|
||
|
// Check if this feed is already in the tree
|
||
|
QTreeWidgetItem *treeFeedItem = 0;
|
||
13 years ago
|
for (int i=0; i<ui->treeMatchingArticles->topLevelItemCount(); ++i) {
|
||
14 years ago
|
QTreeWidgetItem *item = ui->treeMatchingArticles->topLevelItem(i);
|
||
13 years ago
|
if (item->data(0, Qt::UserRole).toString() == feed->url()) {
|
||
14 years ago
|
treeFeedItem = item;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
|
// If there is none, create it
|
||
13 years ago
|
if (!treeFeedItem) {
|
||
14 years ago
|
treeFeedItem = new QTreeWidgetItem(QStringList() << feed->displayName());
|
||
|
treeFeedItem->setToolTip(0, feed->displayName());
|
||
14 years ago
|
QFont f = treeFeedItem->font(0);
|
||
|
f.setBold(true);
|
||
|
treeFeedItem->setFont(0, f);
|
||
14 years ago
|
treeFeedItem->setData(0, Qt::DecorationRole, IconProvider::instance()->getIcon("inode-directory"));
|
||
14 years ago
|
treeFeedItem->setData(0, Qt::UserRole, feed->url());
|
||
14 years ago
|
ui->treeMatchingArticles->addTopLevelItem(treeFeedItem);
|
||
|
}
|
||
|
// Insert the articles
|
||
13 years ago
|
foreach (const QString &art, articles) {
|
||
14 years ago
|
QTreeWidgetItem *item = new QTreeWidgetItem(QStringList() << art);
|
||
14 years ago
|
item->setToolTip(0, art);
|
||
14 years ago
|
treeFeedItem->addChild(item);
|
||
|
}
|
||
|
ui->treeMatchingArticles->expandItem(treeFeedItem);
|
||
14 years ago
|
}
|
||
|
|
||
14 years ago
|
void AutomatedRssDownloader::updateFieldsToolTips(bool regex)
|
||
|
{
|
||
|
QString tip;
|
||
13 years ago
|
if (regex) {
|
||
14 years ago
|
tip = tr("Regex mode: use Perl-like regular expressions");
|
||
14 years ago
|
ui->lineContains->setToolTip(tip);
|
||
|
ui->lineNotContains->setToolTip(tip);
|
||
|
} else {
|
||
|
tip = tr("Wildcard mode: you can use<ul><li>? to match any single character</li><li>* to match zero or more of any characters</li><li>Whitespaces count as AND operators</li></ul>");
|
||
|
ui->lineContains->setToolTip(tip);
|
||
|
tip = tr("Wildcard mode: you can use<ul><li>? to match any single character</li><li>* to match zero or more of any characters</li><li>| is used as OR operator</li></ul>");
|
||
|
ui->lineNotContains->setToolTip(tip);
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void AutomatedRssDownloader::updateMustLineValidity()
|
||
|
{
|
||
|
const QString text = ui->lineContains->text();
|
||
|
bool valid = true;
|
||
|
QStringList tokens;
|
||
13 years ago
|
if (ui->checkRegex->isChecked())
|
||
14 years ago
|
tokens << text;
|
||
|
else
|
||
|
tokens << text.split(" ");
|
||
13 years ago
|
foreach (const QString &token, tokens) {
|
||
14 years ago
|
QRegExp reg(token, Qt::CaseInsensitive, ui->checkRegex->isChecked() ? QRegExp::RegExp : QRegExp::Wildcard);
|
||
13 years ago
|
if (!reg.isValid()) {
|
||
14 years ago
|
valid = false;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
13 years ago
|
if (valid) {
|
||
14 years ago
|
ui->lineContains->setStyleSheet("");
|
||
|
ui->lbl_must_stat->setPixmap(QPixmap());
|
||
|
} else {
|
||
|
ui->lineContains->setStyleSheet("QLineEdit { color: #ff0000; }");
|
||
|
ui->lbl_must_stat->setPixmap(IconProvider::instance()->getIcon("task-attention").pixmap(16, 16));
|
||
|
}
|
||
|
}
|
||
|
|
||
|
void AutomatedRssDownloader::updateMustNotLineValidity()
|
||
|
{
|
||
|
const QString text = ui->lineNotContains->text();
|
||
|
bool valid = true;
|
||
|
QStringList tokens;
|
||
13 years ago
|
if (ui->checkRegex->isChecked())
|
||
14 years ago
|
tokens << text;
|
||
|
else
|
||
10 years ago
|
tokens << text.split("|");
|
||
13 years ago
|
foreach (const QString &token, tokens) {
|
||
14 years ago
|
QRegExp reg(token, Qt::CaseInsensitive, ui->checkRegex->isChecked() ? QRegExp::RegExp : QRegExp::Wildcard);
|
||
13 years ago
|
if (!reg.isValid()) {
|
||
14 years ago
|
valid = false;
|
||
|
break;
|
||
|
}
|
||
|
}
|
||
13 years ago
|
if (valid) {
|
||
14 years ago
|
ui->lineNotContains->setStyleSheet("");
|
||
|
ui->lbl_mustnot_stat->setPixmap(QPixmap());
|
||
14 years ago
|
} else {
|
||
14 years ago
|
ui->lineNotContains->setStyleSheet("QLineEdit { color: #ff0000; }");
|
||
|
ui->lbl_mustnot_stat->setPixmap(IconProvider::instance()->getIcon("task-attention").pixmap(16, 16));
|
||
14 years ago
|
}
|
||
|
}
|
||
|
|
||
12 years ago
|
void AutomatedRssDownloader::on_finished(int result) {
|
||
|
Q_UNUSED(result);
|
||
|
// Save current item on exit
|
||
|
saveEditedRule();
|
||
|
m_ruleList->replace(m_editableRuleList);
|
||
|
m_ruleList->saveRulesToStorage();
|
||
|
saveSettings();
|
||
|
}
|