Improve coding style

This commit is contained in:
Vladimir Golovnev (Glassez) 2020-11-16 10:02:11 +03:00
parent acad35c5bc
commit c41df9ffbd
No known key found for this signature in database
GPG key ID: 52A2C7DEE2DFA6F7
147 changed files with 4454 additions and 2227 deletions

View file

@ -181,7 +181,8 @@ void AutomatedRssDownloader::loadFeedList()
{
const QSignalBlocker feedListSignalBlocker(m_ui->listFeeds);
for (const auto feed : asConst(RSS::Session::instance()->feeds())) {
for (const auto feed : asConst(RSS::Session::instance()->feeds()))
{
QListWidgetItem *item = new QListWidgetItem(feed->name(), m_ui->listFeeds);
item->setData(Qt::UserRole, feed->url());
item->setFlags(item->flags() | Qt::ItemIsUserCheckable | Qt::ItemIsTristate);
@ -203,7 +204,8 @@ void AutomatedRssDownloader::updateFeedList()
bool enable = !selection.isEmpty();
for (int i = 0; i < m_ui->listFeeds->count(); ++i) {
for (int i = 0; i < m_ui->listFeeds->count(); ++i)
{
QListWidgetItem *item = m_ui->listFeeds->item(i);
const QString feedURL = item->data(Qt::UserRole).toString();
item->setHidden(!enable);
@ -211,7 +213,8 @@ void AutomatedRssDownloader::updateFeedList()
bool allEnabled = true;
bool anyEnabled = false;
for (const QListWidgetItem *ruleItem : asConst(selection)) {
for (const QListWidgetItem *ruleItem : asConst(selection))
{
const auto rule = RSS::AutoDownloader::instance()->ruleByName(ruleItem->text());
if (rule.feedURLs().contains(feedURL))
anyEnabled = true;
@ -236,14 +239,16 @@ void AutomatedRssDownloader::updateRuleDefinitionBox()
{
const QList<QListWidgetItem *> selection = m_ui->listRules->selectedItems();
QListWidgetItem *currentRuleItem = ((selection.count() == 1) ? selection.first() : nullptr);
if (m_currentRuleItem != currentRuleItem) {
if (m_currentRuleItem != currentRuleItem)
{
saveEditedRule(); // Save previous rule first
m_currentRuleItem = currentRuleItem;
//m_ui->listRules->setCurrentItem(m_currentRuleItem);
}
// Update rule definition box
if (m_currentRuleItem) {
if (m_currentRuleItem)
{
m_currentRule = RSS::AutoDownloader::instance()->ruleByName(m_currentRuleItem->text());
m_ui->lineContains->setText(m_currentRule.mustContain());
@ -290,7 +295,8 @@ void AutomatedRssDownloader::updateRuleDefinitionBox()
updateFieldsToolTips(m_ui->checkRegex->isChecked());
m_ui->ruleDefBox->setEnabled(true);
}
else {
else
{
m_currentRule = RSS::AutoDownloadRule();
clearRuleDefinitionBox();
m_ui->ruleDefBox->setEnabled(false);
@ -376,7 +382,8 @@ void AutomatedRssDownloader::on_addRuleBtn_clicked()
if (ruleName.isEmpty()) return;
// Check if this rule name already exists
if (RSS::AutoDownloader::instance()->hasRule(ruleName)) {
if (RSS::AutoDownloader::instance()->hasRule(ruleName))
{
QMessageBox::warning(this, tr("Rule name conflict")
, tr("A rule with this name already exists, please choose another name."));
return;
@ -404,7 +411,8 @@ void AutomatedRssDownloader::on_removeRuleBtn_clicked()
void AutomatedRssDownloader::on_exportBtn_clicked()
{
if (RSS::AutoDownloader::instance()->rules().isEmpty()) {
if (RSS::AutoDownloader::instance()->rules().isEmpty())
{
QMessageBox::warning(this, tr("Invalid action")
, tr("The list is empty, there is nothing to export."));
return;
@ -416,24 +424,28 @@ void AutomatedRssDownloader::on_exportBtn_clicked()
, QString::fromLatin1("%1;;%2").arg(m_formatFilterJSON, m_formatFilterLegacy), &selectedFilter);
if (path.isEmpty()) return;
const RSS::AutoDownloader::RulesFileFormat format {
const RSS::AutoDownloader::RulesFileFormat format
{
(selectedFilter == m_formatFilterJSON)
? RSS::AutoDownloader::RulesFileFormat::JSON
: RSS::AutoDownloader::RulesFileFormat::Legacy
};
if (format == RSS::AutoDownloader::RulesFileFormat::JSON) {
if (format == RSS::AutoDownloader::RulesFileFormat::JSON)
{
if (!path.endsWith(EXT_JSON, Qt::CaseInsensitive))
path += EXT_JSON;
}
else {
else
{
if (!path.endsWith(EXT_LEGACY, Qt::CaseInsensitive))
path += EXT_LEGACY;
}
QFile file {path};
if (!file.open(QFile::WriteOnly)
|| (file.write(RSS::AutoDownloader::instance()->exportRules(format)) == -1)) {
|| (file.write(RSS::AutoDownloader::instance()->exportRules(format)) == -1))
{
QMessageBox::critical(
this, tr("I/O Error")
, tr("Failed to create the destination file. Reason: %1").arg(file.errorString()));
@ -450,23 +462,27 @@ void AutomatedRssDownloader::on_importBtn_clicked()
return;
QFile file {path};
if (!file.open(QIODevice::ReadOnly)) {
if (!file.open(QIODevice::ReadOnly))
{
QMessageBox::critical(
this, tr("I/O Error")
, tr("Failed to open the file. Reason: %1").arg(file.errorString()));
return;
}
const RSS::AutoDownloader::RulesFileFormat format {
const RSS::AutoDownloader::RulesFileFormat format
{
(selectedFilter == m_formatFilterJSON)
? RSS::AutoDownloader::RulesFileFormat::JSON
: RSS::AutoDownloader::RulesFileFormat::Legacy
};
try {
try
{
RSS::AutoDownloader::instance()->importRules(file.readAll(),format);
}
catch (const RSS::ParsingError &error) {
catch (const RSS::ParsingError &error)
{
QMessageBox::critical(
this, tr("Import Error")
, tr("Failed to import the selected rules file. Reason: %1").arg(error.message()));
@ -483,8 +499,10 @@ void AutomatedRssDownloader::displayRulesListMenu()
const QList<QListWidgetItem *> selection = m_ui->listRules->selectedItems();
if (!selection.isEmpty()) {
if (selection.count() == 1) {
if (!selection.isEmpty())
{
if (selection.count() == 1)
{
const QAction *delAct = menu->addAction(UIThemeManager::instance()->getIcon("list-remove"), tr("Delete rule"));
connect(delAct, &QAction::triggered, this, &AutomatedRssDownloader::on_removeRuleBtn_clicked);
@ -493,7 +511,8 @@ void AutomatedRssDownloader::displayRulesListMenu()
const QAction *renameAct = menu->addAction(UIThemeManager::instance()->getIcon("edit-rename"), tr("Rename rule..."));
connect(renameAct, &QAction::triggered, this, &AutomatedRssDownloader::renameSelectedRule);
}
else {
else
{
const QAction *delAct = menu->addAction(UIThemeManager::instance()->getIcon("list-remove"), tr("Delete selected rules"));
connect(delAct, &QAction::triggered, this, &AutomatedRssDownloader::on_removeRuleBtn_clicked);
}
@ -513,18 +532,21 @@ void AutomatedRssDownloader::renameSelectedRule()
if (selection.isEmpty()) return;
QListWidgetItem *item = selection.first();
forever {
forever
{
QString newName = AutoExpandableDialog::getText(
this, tr("Rule renaming"), tr("Please type the new rule name")
, QLineEdit::Normal, item->text());
newName = newName.trimmed();
if (newName.isEmpty()) return;
if (RSS::AutoDownloader::instance()->hasRule(newName)) {
if (RSS::AutoDownloader::instance()->hasRule(newName))
{
QMessageBox::warning(this, tr("Rule name conflict")
, tr("A rule with this name already exists, please choose another name."));
}
else {
else
{
// Rename the rule
RSS::AutoDownloader::instance()->renameRule(item->text(), newName);
return;
@ -545,7 +567,8 @@ void AutomatedRssDownloader::clearSelectedRuleDownloadedEpisodeList()
tr("Are you sure you want to clear the list of downloaded episodes for the selected rule?"),
QMessageBox::Yes | QMessageBox::No);
if (reply == QMessageBox::Yes) {
if (reply == QMessageBox::Yes)
{
m_currentRule.setPreviouslyMatchedEpisodes(QStringList());
handleRuleDefinitionChanged();
}
@ -554,7 +577,8 @@ void AutomatedRssDownloader::clearSelectedRuleDownloadedEpisodeList()
void AutomatedRssDownloader::handleFeedCheckStateChange(QListWidgetItem *feedItem)
{
const QString feedURL = feedItem->data(Qt::UserRole).toString();
for (QListWidgetItem *ruleItem : asConst(m_ui->listRules->selectedItems())) {
for (QListWidgetItem *ruleItem : asConst(m_ui->listRules->selectedItems()))
{
RSS::AutoDownloadRule rule = (ruleItem == m_currentRuleItem
? m_currentRule
: RSS::AutoDownloader::instance()->ruleByName(ruleItem->text()));
@ -578,11 +602,13 @@ void AutomatedRssDownloader::updateMatchingArticles()
{
m_ui->treeMatchingArticles->clear();
for (const QListWidgetItem *ruleItem : asConst(m_ui->listRules->selectedItems())) {
for (const QListWidgetItem *ruleItem : asConst(m_ui->listRules->selectedItems()))
{
RSS::AutoDownloadRule rule = (ruleItem == m_currentRuleItem
? m_currentRule
: RSS::AutoDownloader::instance()->ruleByName(ruleItem->text()));
for (const QString &feedURL : asConst(rule.feedURLs())) {
for (const QString &feedURL : asConst(rule.feedURLs()))
{
auto feed = RSS::Session::instance()->feedByURL(feedURL);
if (!feed) continue; // feed doesn't exist
@ -605,16 +631,19 @@ void AutomatedRssDownloader::addFeedArticlesToTree(RSS::Feed *feed, const QStrin
// Check if this feed is already in the tree
QTreeWidgetItem *treeFeedItem = nullptr;
for (int i = 0; i < m_ui->treeMatchingArticles->topLevelItemCount(); ++i) {
for (int i = 0; i < m_ui->treeMatchingArticles->topLevelItemCount(); ++i)
{
QTreeWidgetItem *item = m_ui->treeMatchingArticles->topLevelItem(i);
if (item->data(0, Qt::UserRole).toString() == feed->url()) {
if (item->data(0, Qt::UserRole).toString() == feed->url())
{
treeFeedItem = item;
break;
}
}
// If there is none, create it
if (!treeFeedItem) {
if (!treeFeedItem)
{
treeFeedItem = new QTreeWidgetItem(QStringList() << feed->name());
treeFeedItem->setToolTip(0, feed->name());
QFont f = treeFeedItem->font(0);
@ -626,10 +655,12 @@ void AutomatedRssDownloader::addFeedArticlesToTree(RSS::Feed *feed, const QStrin
}
// Insert the articles
for (const QString &article : articles) {
for (const QString &article : articles)
{
QPair<QString, QString> key(feed->name(), article);
if (!m_treeListEntries.contains(key)) {
if (!m_treeListEntries.contains(key))
{
m_treeListEntries << key;
QTreeWidgetItem *item = new QTreeWidgetItem(QStringList() << article);
item->setToolTip(0, article);
@ -645,10 +676,12 @@ void AutomatedRssDownloader::addFeedArticlesToTree(RSS::Feed *feed, const QStrin
void AutomatedRssDownloader::updateFieldsToolTips(bool regex)
{
QString tip;
if (regex) {
if (regex)
{
tip = "<p>" + tr("Regex mode: use Perl-compatible regular expressions") + "</p>";
}
else {
else
{
tip = "<p>" + tr("Wildcard mode: you can use") + "<ul>"
+ "<li>" + tr("? to match any single character") + "</li>"
+ "<li>" + tr("* to match zero or more of any characters") + "</li>"
@ -676,7 +709,8 @@ void AutomatedRssDownloader::updateMustLineValidity()
bool valid = true;
QString error;
if (!text.isEmpty()) {
if (!text.isEmpty())
{
QStringList tokens;
if (isRegex)
tokens << text;
@ -684,9 +718,11 @@ void AutomatedRssDownloader::updateMustLineValidity()
for (const QString &token : asConst(text.split('|')))
tokens << Utils::String::wildcardToRegex(token);
for (const QString &token : asConst(tokens)) {
for (const QString &token : asConst(tokens))
{
QRegularExpression reg(token, QRegularExpression::CaseInsensitiveOption);
if (!reg.isValid()) {
if (!reg.isValid())
{
if (isRegex)
error = tr("Position %1: %2").arg(reg.patternErrorOffset()).arg(reg.errorString());
valid = false;
@ -695,12 +731,14 @@ void AutomatedRssDownloader::updateMustLineValidity()
}
}
if (valid) {
if (valid)
{
m_ui->lineContains->setStyleSheet("");
m_ui->labelMustStat->setPixmap(QPixmap());
m_ui->labelMustStat->setToolTip("");
}
else {
else
{
m_ui->lineContains->setStyleSheet("QLineEdit { color: #ff0000; }");
m_ui->labelMustStat->setPixmap(UIThemeManager::instance()->getIcon("task-attention").pixmap(16, 16));
m_ui->labelMustStat->setToolTip(error);
@ -714,7 +752,8 @@ void AutomatedRssDownloader::updateMustNotLineValidity()
bool valid = true;
QString error;
if (!text.isEmpty()) {
if (!text.isEmpty())
{
QStringList tokens;
if (isRegex)
tokens << text;
@ -722,9 +761,11 @@ void AutomatedRssDownloader::updateMustNotLineValidity()
for (const QString &token : asConst(text.split('|')))
tokens << Utils::String::wildcardToRegex(token);
for (const QString &token : asConst(tokens)) {
for (const QString &token : asConst(tokens))
{
QRegularExpression reg(token, QRegularExpression::CaseInsensitiveOption);
if (!reg.isValid()) {
if (!reg.isValid())
{
if (isRegex)
error = tr("Position %1: %2").arg(reg.patternErrorOffset()).arg(reg.errorString());
valid = false;
@ -733,12 +774,14 @@ void AutomatedRssDownloader::updateMustNotLineValidity()
}
}
if (valid) {
if (valid)
{
m_ui->lineNotContains->setStyleSheet("");
m_ui->labelMustNotStat->setPixmap(QPixmap());
m_ui->labelMustNotStat->setToolTip("");
}
else {
else
{
m_ui->lineNotContains->setStyleSheet("QLineEdit { color: #ff0000; }");
m_ui->labelMustNotStat->setPixmap(UIThemeManager::instance()->getIcon("task-attention").pixmap(16, 16));
m_ui->labelMustNotStat->setToolTip(error);
@ -750,11 +793,13 @@ void AutomatedRssDownloader::updateEpisodeFilterValidity()
const QString text = m_ui->lineEFilter->text();
bool valid = text.isEmpty() || m_episodeRegex->match(text).hasMatch();
if (valid) {
if (valid)
{
m_ui->lineEFilter->setStyleSheet("");
m_ui->labelEpFilterStat->setPixmap(QPixmap());
}
else {
else
{
m_ui->lineEFilter->setStyleSheet("QLineEdit { color: #ff0000; }");
m_ui->labelEpFilterStat->setPixmap(UIThemeManager::instance()->getIcon("task-attention").pixmap(16, 16));
}