MythTV master
mythsorthelper.cpp
Go to the documentation of this file.
1// -*- Mode: c++ -*-
2// vim: set expandtab tabstop=4 shiftwidth=4
3
4#include "mythsorthelper.h"
5
6// C++ headers
7#include <algorithm>
8
9// MythTV
10#include "mythcorecontext.h"
11#include "mythlogging.h"
12
20{
21 m_prefixes = tr("^(The |A |An )",
22 "Regular Expression for what to ignore when sorting");
23 m_prefixes = m_prefixes.trimmed();
24 if (not hasPrefixes()) {
25 // This language doesn't ignore any words when sorting
27 return;
28 }
29 if (not m_prefixes.startsWith("^"))
30 m_prefixes = "^" + m_prefixes;
31
32 if (m_caseSensitive == Qt::CaseInsensitive)
33 {
34 m_prefixes = m_prefixes.toLower();
35 m_exclusions = m_exclusions.toLower();
36 }
37 m_prefixesRegex = QRegularExpression(m_prefixes);
38 m_prefixesRegex2 = QRegularExpression(m_prefixes + "(.*)");
39 m_exclList = m_exclusions.split(";", Qt::SkipEmptyParts);
40 // NOLINTNEXTLINE(modernize-loop-convert)
41 for (int i = 0; i < m_exclList.size(); i++)
42 m_exclList[i] = m_exclList[i].trimmed();
43}
44
55{
56 if (gCoreContext) {
57#if 0
58 // Last minute change. QStringRef::localeAwareCompare appears to
59 // always do case insensitive sorting, so there's no point in
60 // presenting this option to a user.
62 gCoreContext->GetBoolSetting("SortCaseSensitive", false)
63 ? Qt::CaseSensitive : Qt::CaseInsensitive;
64#endif
66 gCoreContext->GetBoolSetting("SortStripPrefixes", true)
69 gCoreContext->GetSetting("SortPrefixExceptions", "");
70 }
72}
73
87 Qt::CaseSensitivity case_sensitive,
88 SortPrefixMode prefix_mode,
89 QString exclusions) :
90 m_caseSensitive(case_sensitive),
91 m_prefixMode(prefix_mode),
92 m_exclusions(std::move(exclusions))
93{
95}
96
107{
108 if (other == nullptr)
109 {
110 LOG(VB_GENERAL, LOG_ERR,
111 QString("MythSortHelper created from null pointer."));
112 return;
113 }
115 m_prefixMode = other->m_prefixMode;
116 m_prefixes = other->m_prefixes;
119 m_exclusions = other->m_exclusions;
120 m_exclList = other->m_exclList;
121 m_exclMode = other->m_exclMode;
122}
123
124static std::shared_ptr<MythSortHelper> singleton = nullptr;
125
134std::shared_ptr<MythSortHelper> getMythSortHelper(void)
135{
136 if (singleton == nullptr)
137 singleton = std::make_shared<MythSortHelper>();
138 return singleton;
139}
140
151{
152 singleton = nullptr;
153}
154
167QString MythSortHelper::doTitle(const QString& title) const
168{
169 QString ltitle = title;
170 if (m_caseSensitive == Qt::CaseInsensitive)
171 ltitle = ltitle.toLower();
173 return ltitle;
175 {
176 if (m_exclList.contains(ltitle))
177 return ltitle;
178 } else {
179 for (const auto& str : std::as_const(m_exclList))
180 if (ltitle.startsWith(str))
181 return ltitle;
182 }
184 return ltitle.remove(m_prefixesRegex);
185 return ltitle.replace(m_prefixesRegex2, "\\2, \\1").trimmed();
186}
187
200QString MythSortHelper::doPathname(const QString& pathname) const
201{
202 QString lpathname = pathname;
203 if (m_caseSensitive == Qt::CaseInsensitive)
204 lpathname = lpathname.toLower();
206 return lpathname;
207 QStringList parts = lpathname.split("/");
208 // NOLINTNEXTLINE(modernize-loop-convert)
209 for (int i = 0; i < parts.size(); ++i) {
210 if (std::ranges::any_of(std::as_const(m_exclList),
211 [&parts,i](const QString& excl)
212 { return parts[i].startsWith(excl); } ))
213 continue;
215 parts[i] = parts[i].remove(m_prefixesRegex);
216 else
217 parts[i] = parts[i].replace(m_prefixesRegex2, "\\2, \\1").trimmed();
218 }
219 return parts.join("/");
220}
QString GetSetting(const QString &key, const QString &defaultval="")
bool GetBoolSetting(const QString &key, bool defaultval=false)
A class to consolidate all the soring functions.
Qt::CaseSensitivity m_caseSensitive
Whether sorting two strings should ignore upper/lower case.
SortPrefixMode m_prefixMode
Whether or not to ignore prefix words (like A, An, and The) when sorting two strings.
bool hasPrefixes(void)
Does the language translation specify any prefixes.
QStringList m_exclList
The m_exclusion string converted into a string list.
QRegularExpression m_prefixesRegex2
A regular expression used for moving leading prefix to the end of a string.
QString doPathname(const QString &pathname) const
Create the sortable form of an item.
QRegularExpression m_prefixesRegex
A regular expression used for removing a leading prefix.
QString m_exclusions
A string containing names that should be ignored when greating the sortable form of a title.
QString doTitle(const QString &title) const
Create the sortable form of an title string.
QString m_prefixes
A string containing the regular expression of prefixes to ignore when sorting.
void MythSortHelperCommon(void)
Common code for creating a MythSortHelper object.
MythSortHelper()
Create a MythSortHelper object.
SortExclusionMode m_exclMode
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
static std::shared_ptr< MythSortHelper > singleton
std::shared_ptr< MythSortHelper > getMythSortHelper(void)
Get a pointer to the MythSortHelper singleton.
void resetMythSortHelper(void)
Delete the MythSortHelper singleton.
@ SortExclusionMatch
SortPrefixMode
@ SortPrefixKeep
@ SortPrefixRemove