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)
} }
// uses lessThan comparison // 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 // Return value indicates if functions was successful
// result argument will contain actual comparison result if function 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)
return false; 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 // to send numbers instead of strings with suffixes
QString Utils::String::fromDouble(double n, int precision) QString Utils::String::fromDouble(double n, int precision)
{ {

19
src/core/utils/string.h

@ -31,6 +31,10 @@
#define UTILS_STRING_H #define UTILS_STRING_H
#include <string> #include <string>
#include <QtGlobal>
#if (QT_VERSION >= QT_VERSION_CHECK(5, 2, 0))
#include <QCollator>
#endif
class QString; class QString;
class QByteArray; class QByteArray;
@ -41,12 +45,25 @@ namespace Utils
{ {
QString fromStdString(const std::string &str); QString fromStdString(const std::string &str);
std::string toStdString(const QString &str); std::string toStdString(const QString &str);
bool naturalSort(QString left, QString right, bool &result);
QString fromDouble(double n, int precision); QString fromDouble(double n, int precision);
// Implements constant-time comparison to protect against timing attacks // Implements constant-time comparison to protect against timing attacks
// Taken from https://crackstation.net/hashing-security.htm // Taken from https://crackstation.net/hashing-security.htm
bool slowEquals(const QByteArray &a, const QByteArray &b); 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 @@
#include "guiiconprovider.h" #include "guiiconprovider.h"
#include "autoexpandabledialog.h" #include "autoexpandabledialog.h"
#include "core/utils/fs.h" #include "core/utils/fs.h"
#include "core/utils/string.h"
AutomatedRssDownloader::AutomatedRssDownloader(const QWeakPointer<RssManager>& manager, QWidget *parent) : AutomatedRssDownloader::AutomatedRssDownloader(const QWeakPointer<RssManager>& manager, QWidget *parent) :
QDialog(parent), QDialog(parent),
@ -262,9 +263,9 @@ void AutomatedRssDownloader::updateRuleDefinitionBox()
QDateTime dateTime = rule->lastMatch(); QDateTime dateTime = rule->lastMatch();
QString lMatch; QString lMatch;
if (dateTime.isValid()) 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 else
lMatch = tr("Last match: Unknown"); lMatch = tr("Last Match: Unknown");
ui->lblLastMatch->setText(lMatch); ui->lblLastMatch->setText(lMatch);
updateMustLineValidity(); updateMustLineValidity();
updateMustNotLineValidity(); updateMustNotLineValidity();
@ -310,10 +311,10 @@ RssDownloadRulePtr AutomatedRssDownloader::getCurrentRule() const
void AutomatedRssDownloader::initLabelCombobox() void AutomatedRssDownloader::initLabelCombobox()
{ {
// Load custom labels // Load custom labels
const QStringList customLabels = Preferences::instance()->getTorrentLabels(); QStringList customLabels = Preferences::instance()->getTorrentLabels();
foreach (const QString& label, customLabels) { std::sort(customLabels.begin(), customLabels.end(), Utils::String::NaturalCompare());
ui->comboLabel->addItem(label); foreach (const QString& l, customLabels)
} ui->comboLabel->addItem(l);
} }
void AutomatedRssDownloader::saveEditedRule() void AutomatedRssDownloader::saveEditedRule()

346
src/gui/rss/automatedrssdownloader.ui

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

Loading…
Cancel
Save