Browse Source

Merge pull request #3165 from Chocobo1/rss_sort

Sort labels in RSS Downloader dialog
adaptive-webui-19844
sledgehammer999 9 years ago
parent
commit
8b547644b0
  1. 79
      src/core/utils/string.cpp
  2. 19
      src/core/utils/string.h
  3. 13
      src/gui/rss/automatedrssdownloader.cpp
  4. 346
      src/gui/rss/automatedrssdownloader.ui

79
src/core/utils/string.cpp

@ -45,7 +45,7 @@ std::string Utils::String::toStdString(const QString &str) @@ -45,7 +45,7 @@ std::string Utils::String::toStdString(const QString &str)
}
// uses lessThan comparison
bool Utils::String::naturalSort(QString left, QString right, bool &result)
bool Utils::String::naturalSort(const QString &left, const QString &right, bool &result)
{
// Return value indicates if functions was successful
// result argument will contain actual comparison result if function was successful
@ -106,6 +106,83 @@ bool Utils::String::naturalSort(QString left, QString right, bool &result) @@ -106,6 +106,83 @@ bool Utils::String::naturalSort(QString left, QString right, bool &result)
return false;
}
Utils::String::NaturalCompare::NaturalCompare()
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
#if defined(Q_OS_WIN)
// Without ICU library, QCollator doesn't support `setNumericMode(true)` on OS older than Win7
if(SysInfo::windowsVersion() < QSysInfo::WV_WINDOWS7)
return;
#endif
m_collator.setNumericMode(true);
m_collator.setCaseSensitivity(Qt::CaseInsensitive);
#endif
}
bool Utils::String::NaturalCompare::operator()(const QString &l, const QString &r)
{
#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
#if defined(Q_OS_WIN)
// Without ICU library, QCollator doesn't support `setNumericMode(true)` on OS older than Win7
if(SysInfo::windowsVersion() < QSysInfo::WV_WINDOWS7)
return lessThan(l, r);
#endif
return (m_collator.compare(l, r) < 0);
#else
return lessThan(l, r);
#endif
}
bool Utils::String::NaturalCompare::lessThan(const QString &left, const QString &right)
{
// Return value `false` indicates `right` should go before `left`, otherwise, after
int posL = 0;
int posR = 0;
while (true) {
while (true) {
if (posL == left.size() || posR == right.size())
return (left.size() < right.size()); // when a shorter string is another string's prefix, shorter string place before longer string
QChar leftChar = left[posL].toLower();
QChar rightChar = right[posR].toLower();
if (leftChar == rightChar)
; // compare next character
else if (leftChar.isDigit() && rightChar.isDigit())
break; // Both are digits, break this loop and compare numbers
else
return leftChar < rightChar;
++posL;
++posR;
}
int startL = posL;
while ((posL < left.size()) && left[posL].isDigit())
++posL;
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
int numL = left.midRef(startL, posL - startL).toInt();
#else
int numL = left.mid(startL, posL - startL).toInt();
#endif
int startR = posR;
while ((posR < right.size()) && right[posR].isDigit())
++posR;
#if QT_VERSION >= QT_VERSION_CHECK(5, 1, 0)
int numR = right.midRef(startR, posR - startR).toInt();
#else
int numR = right.mid(startR, posR - startR).toInt();
#endif
if (numL != numR)
return (numL < numR);
// Strings + digits do match and we haven't hit string end
// Do another round
}
return false;
}
// to send numbers instead of strings with suffixes
QString Utils::String::fromDouble(double n, int precision)
{

19
src/core/utils/string.h

@ -31,6 +31,10 @@ @@ -31,6 +31,10 @@
#define UTILS_STRING_H
#include <string>
#include <QtGlobal>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
#include <QCollator>
#endif
class QString;
class QByteArray;
@ -41,12 +45,25 @@ namespace Utils @@ -41,12 +45,25 @@ namespace Utils
{
QString fromStdString(const std::string &str);
std::string toStdString(const QString &str);
bool naturalSort(QString left, QString right, bool &result);
QString fromDouble(double n, int precision);
// Implements constant-time comparison to protect against timing attacks
// Taken from https://crackstation.net/hashing-security.htm
bool slowEquals(const QByteArray &a, const QByteArray &b);
bool naturalSort(const QString &left, const QString &right, bool &result);
class NaturalCompare
{
public:
NaturalCompare();
bool operator()(const QString &l, const QString &r);
bool lessThan(const QString &left, const QString &right);
#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
private:
QCollator m_collator;
#endif
};
}
}

13
src/gui/rss/automatedrssdownloader.cpp

@ -43,6 +43,7 @@ @@ -43,6 +43,7 @@
#include "guiiconprovider.h"
#include "autoexpandabledialog.h"
#include "core/utils/fs.h"
#include "core/utils/string.h"
AutomatedRssDownloader::AutomatedRssDownloader(const QWeakPointer<RssManager>& manager, QWidget *parent) :
QDialog(parent),
@ -262,9 +263,9 @@ void AutomatedRssDownloader::updateRuleDefinitionBox() @@ -262,9 +263,9 @@ void AutomatedRssDownloader::updateRuleDefinitionBox()
QDateTime dateTime = rule->lastMatch();
QString lMatch;
if (dateTime.isValid())
lMatch = tr("Last match: %1 days ago").arg(dateTime.daysTo(QDateTime::currentDateTime()));
lMatch = tr("Last Match: %1 days ago").arg(dateTime.daysTo(QDateTime::currentDateTime()));
else
lMatch = tr("Last match: Unknown");
lMatch = tr("Last Match: Unknown");
ui->lblLastMatch->setText(lMatch);
updateMustLineValidity();
updateMustNotLineValidity();
@ -310,10 +311,10 @@ RssDownloadRulePtr AutomatedRssDownloader::getCurrentRule() const @@ -310,10 +311,10 @@ RssDownloadRulePtr AutomatedRssDownloader::getCurrentRule() const
void AutomatedRssDownloader::initLabelCombobox()
{
// Load custom labels
const QStringList customLabels = Preferences::instance()->getTorrentLabels();
foreach (const QString& label, customLabels) {
ui->comboLabel->addItem(label);
}
QStringList customLabels = Preferences::instance()->getTorrentLabels();
std::sort(customLabels.begin(), customLabels.end(), Utils::String::NaturalCompare());
foreach (const QString& l, customLabels)
ui->comboLabel->addItem(l);
}
void AutomatedRssDownloader::saveEditedRule()

346
src/gui/rss/automatedrssdownloader.ui

@ -7,11 +7,11 @@ @@ -7,11 +7,11 @@
<x>0</x>
<y>0</y>
<width>816</width>
<height>494</height>
<height>537</height>
</rect>
</property>
<property name="windowTitle">
<string>Automated RSS Downloader</string>
<string>RSS Downloader</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_5">
<item>
@ -23,7 +23,7 @@ @@ -23,7 +23,7 @@
</font>
</property>
<property name="text">
<string>Enable the automated RSS downloader</string>
<string>Enable Automated RSS Downloader</string>
</property>
</widget>
</item>
@ -45,31 +45,12 @@ @@ -45,31 +45,12 @@
</font>
</property>
<property name="text">
<string>Download rules</string>
<string>Download Rules</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeType">
<enum>QSizePolicy::Expanding</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QToolButton" name="removeRuleBtn">
<property name="text">
<string/>
</property>
<property name="iconSize">
<size>
<width>24</width>
@ -80,9 +61,6 @@ @@ -80,9 +61,6 @@
</item>
<item>
<widget class="QToolButton" name="addRuleBtn">
<property name="text">
<string/>
</property>
<property name="iconSize">
<size>
<width>24</width>
@ -91,19 +69,6 @@ @@ -91,19 +69,6 @@
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_2">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
</layout>
</item>
<item>
@ -120,141 +85,78 @@ @@ -120,141 +85,78 @@
<item>
<widget class="QGroupBox" name="ruleDefBox">
<property name="title">
<string>Rule definition</string>
<string>Rule Definition</string>
</property>
<layout class="QVBoxLayout" name="verticalLayout_6">
<item>
<widget class="QCheckBox" name="checkRegex">
<property name="text">
<string>Use regular expressions</string>
<string>Use Regular Expressions</string>
</property>
</widget>
</item>
<item>
<layout class="QFormLayout" name="formLayout">
<layout class="QGridLayout" name="gridLayout">
<item row="0" column="0">
<widget class="QLabel" name="label_4">
<property name="text">
<string>Must contain:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
<string>Must Contain:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLineEdit" name="lineContains"/>
</item>
<item>
<widget class="QLabel" name="lbl_must_stat">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>18</width>
<height>18</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>18</width>
<height>18</height>
</size>
</property>
<property name="text">
<string notr="true"/>
</property>
</widget>
</item>
</layout>
</item>
<item row="1" column="0">
<widget class="QLabel" name="label_5">
<property name="text">
<string>Must not contain:</string>
<string>Must Not Contain:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</widget>
</item>
<item row="2" column="0">
<widget class="QLabel" name="lblEFilter">
<property name="text">
<string>Episode Filter:</string>
</property>
</widget>
</item>
<item row="1" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QLineEdit" name="lineNotContains"/>
</item>
<item>
<widget class="QLabel" name="lbl_mustnot_stat">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>18</width>
<height>18</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>18</width>
<height>18</height>
</size>
</property>
<property name="text">
<string notr="true"/>
</property>
</widget>
</item>
</layout>
<item row="2" column="2">
<widget class="QLabel" name="lblEFilterStat">
<property name="maximumSize">
<size>
<width>18</width>
<height>18</height>
</size>
</property>
</widget>
</item>
<item row="1" column="2">
<widget class="QLabel" name="lbl_mustnot_stat">
<property name="maximumSize">
<size>
<width>18</width>
<height>18</height>
</size>
</property>
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="lineContains"/>
</item>
<item row="2" column="1">
<layout class="QHBoxLayout" name="horizontalLayout_6">
<item>
<widget class="QLineEdit" name="lineEFilter"/>
</item>
<item>
<widget class="QLabel" name="lblEFilterStat">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="minimumSize">
<size>
<width>18</width>
<height>18</height>
</size>
</property>
<property name="maximumSize">
<size>
<width>18</width>
<height>18</height>
</size>
</property>
<property name="text">
<string notr="true"/>
</property>
</widget>
</item>
</layout>
<widget class="QLineEdit" name="lineEFilter"/>
</item>
<item row="2" column="0">
<widget class="QLabel" name="lblEFilter">
<property name="text">
<string>Episode filter:</string>
<item row="0" column="2">
<widget class="QLabel" name="lbl_must_stat">
<property name="maximumSize">
<size>
<width>18</width>
<height>18</height>
</size>
</property>
</widget>
</item>
<item row="1" column="1">
<widget class="QLineEdit" name="lineNotContains"/>
</item>
</layout>
</item>
<item>
@ -265,18 +167,21 @@ @@ -265,18 +167,21 @@
</widget>
</item>
<item>
<layout class="QFormLayout" name="formLayout_2">
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_3">
<item>
<widget class="QLabel" name="label_7">
<property name="text">
<string>Assign label:</string>
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
<property name="text">
<string>Assign Label:</string>
</property>
</widget>
</item>
<item row="0" column="1">
<item>
<widget class="QComboBox" name="comboLabel">
<property name="editable">
<bool>true</bool>
@ -288,13 +193,13 @@ @@ -288,13 +193,13 @@
<item>
<widget class="QCheckBox" name="saveDiffDir_check">
<property name="text">
<string>Save to a different directory</string>
<string>Save to a Different Directory</string>
</property>
</widget>
</item>
<item>
<layout class="QFormLayout" name="formLayout_3">
<item row="0" column="0">
<layout class="QHBoxLayout" name="horizontalLayout_5">
<item>
<widget class="QLabel" name="label_6">
<property name="enabled">
<bool>false</bool>
@ -302,31 +207,24 @@ @@ -302,31 +207,24 @@
<property name="text">
<string>Save to:</string>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</widget>
</item>
<item>
<widget class="QLineEdit" name="lineSavePath">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item row="0" column="1">
<layout class="QHBoxLayout" name="horizontalLayout">
<item>
<widget class="QLineEdit" name="lineSavePath">
<property name="enabled">
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QToolButton" name="browseSP">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string notr="true">...</string>
</property>
</widget>
</item>
</layout>
<item>
<widget class="QToolButton" name="browseSP">
<property name="enabled">
<bool>false</bool>
</property>
<property name="text">
<string notr="true">...</string>
</property>
</widget>
</item>
</layout>
</item>
@ -335,23 +233,10 @@ @@ -335,23 +233,10 @@
<item>
<widget class="QLabel" name="lblIgnoreDays">
<property name="text">
<string comment="... X days">Ignore subsequent matches for (0 to disable)</string>
<string comment="... X days">Ignore Subsequent Matches for (0 to Disable)</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_4">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QSpinBox" name="spinIgnorePeriod">
<property name="enabled">
@ -361,43 +246,32 @@ @@ -361,43 +246,32 @@
<string> days</string>
</property>
<property name="maximum">
<number>360</number>
<number>365</number>
</property>
</widget>
</item>
</layout>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_7">
<item>
<spacer name="horizontalSpacer_5">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QLabel" name="lblLastMatch">
<property name="enabled">
<bool>true</bool>
</property>
<property name="text">
<string notr="true"/>
</property>
</widget>
</item>
</layout>
<widget class="QLabel" name="lblLastMatch">
<property name="enabled">
<bool>true</bool>
</property>
<property name="alignment">
<set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
</property>
</widget>
</item>
<item>
<layout class="QHBoxLayout" name="horizontalLayout_9">
<item>
<widget class="QLabel" name="lblAddPaused">
<property name="sizePolicy">
<sizepolicy hsizetype="Fixed" vsizetype="Preferred">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<property name="text">
<string>Add Paused:</string>
</property>
@ -405,25 +279,19 @@ @@ -405,25 +279,19 @@
</item>
<item>
<widget class="QComboBox" name="comboAddPaused">
<property name="sizePolicy">
<sizepolicy hsizetype="Expanding" vsizetype="Fixed">
<horstretch>0</horstretch>
<verstretch>0</verstretch>
</sizepolicy>
</property>
<item>
<property name="text">
<string>Use global setting</string>
<string>Use global settings</string>
</property>
</item>
<item>
<property name="text">
<string>Always add paused</string>
<string>Always</string>
</property>
</item>
<item>
<property name="text">
<string>Never add paused</string>
<string>Never</string>
</property>
</item>
</widget>
@ -444,7 +312,7 @@ @@ -444,7 +312,7 @@
</font>
</property>
<property name="text">
<string>Apply rule to feeds:</string>
<string>Apply Rule to Feeds:</string>
</property>
</widget>
</item>
@ -466,7 +334,7 @@ @@ -466,7 +334,7 @@
</font>
</property>
<property name="text">
<string>Matching RSS articles</string>
<string>Matching RSS Articles</string>
</property>
</widget>
</item>
@ -491,35 +359,19 @@ @@ -491,35 +359,19 @@
<item>
<widget class="QPushButton" name="importBtn">
<property name="text">
<string>Import...</string>
<string>&amp;Import...</string>
</property>
</widget>
</item>
<item>
<widget class="QPushButton" name="exportBtn">
<property name="text">
<string>Export...</string>
<string>&amp;Export...</string>
</property>
</widget>
</item>
<item>
<spacer name="horizontalSpacer_3">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="sizeHint" stdset="0">
<size>
<width>40</width>
<height>20</height>
</size>
</property>
</spacer>
</item>
<item>
<widget class="QDialogButtonBox" name="buttonBox">
<property name="orientation">
<enum>Qt::Horizontal</enum>
</property>
<property name="standardButtons">
<set>QDialogButtonBox::Close</set>
</property>

Loading…
Cancel
Save