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 #include "mythcorecontext.h"
7 
15 {
16  m_prefixes = tr("^(The |A |An )",
17  "Regular Expression for what to ignore when sorting");
18  m_prefixes = m_prefixes.trimmed();
19  if (not hasPrefixes()) {
20  // This language doesn't ignore any words when sorting
22  return;
23  }
24  if (not m_prefixes.startsWith("^"))
25  m_prefixes = "^" + m_prefixes;
26 
27  if (m_caseSensitive == Qt::CaseInsensitive)
28  {
29  m_prefixes = m_prefixes.toLower();
30  m_exclusions = m_exclusions.toLower();
31  }
32  m_prefixesRegex = QRegularExpression(m_prefixes);
33  m_prefixesRegex2 = QRegularExpression(m_prefixes + "(.*)");
34 #if QT_VERSION < QT_VERSION_CHECK(5,14,0)
35  m_exclList = m_exclusions.split(";", QString::SkipEmptyParts);
36 #else
37  m_exclList = m_exclusions.split(";", Qt::SkipEmptyParts);
38 #endif
39  // NOLINTNEXTLINE(modernize-loop-convert)
40  for (int i = 0; i < m_exclList.size(); i++)
41  m_exclList[i] = m_exclList[i].trimmed();
42 }
43 
54 {
55  if (gCoreContext) {
56 #if 0
57  // Last minute change. QStringRef::localeAwareCompare appears to
58  // always do case insensitive sorting, so there's no point in
59  // presenting this option to a user.
61  gCoreContext->GetBoolSetting("SortCaseSensitive", false)
62  ? Qt::CaseSensitive : Qt::CaseInsensitive;
63 #endif
64  m_prefixMode =
65  gCoreContext->GetBoolSetting("SortStripPrefixes", true)
67  m_exclusions =
68  gCoreContext->GetSetting("SortPrefixExceptions", "");
69  }
71 }
72 
86  Qt::CaseSensitivity case_sensitive,
87  SortPrefixMode prefix_mode,
88  QString exclusions) :
89  m_caseSensitive(case_sensitive),
90  m_prefixMode(prefix_mode),
91  m_exclusions(std::move(exclusions))
92 {
94 }
95 
106 {
107  if (other == nullptr)
108  {
109  LOG(VB_GENERAL, LOG_ERR,
110  QString("MythSortHelper created from null pointer."));
111  return;
112  }
114  m_prefixMode = other->m_prefixMode;
115  m_prefixes = other->m_prefixes;
118  m_exclusions = other->m_exclusions;
119  m_exclList = other->m_exclList;
120  m_exclMode = other->m_exclMode;
121 }
122 
123 static std::shared_ptr<MythSortHelper> singleton = nullptr;
124 
133 std::shared_ptr<MythSortHelper> getMythSortHelper(void)
134 {
135  if (singleton == nullptr)
136  // coverity[resource_leak]
137  singleton = std::make_shared<MythSortHelper>(new MythSortHelper());
138  return singleton;
139 }
140 
151 {
152  singleton = nullptr;
153 }
154 
167 QString 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 : qAsConst(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 
200 QString 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::any_of(m_exclList.cbegin(), m_exclList.cend(),
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 }
MythSortHelper::m_exclMode
SortExclusionMode m_exclMode
Definition: mythsorthelper.h:78
MythSortHelper::m_prefixes
QString m_prefixes
A string containing the regular expression of prefixes to ignore when sorting.
Definition: mythsorthelper.h:62
getMythSortHelper
std::shared_ptr< MythSortHelper > getMythSortHelper(void)
Get a pointer to the MythSortHelper singleton.
Definition: mythsorthelper.cpp:133
SortExclusionMatch
@ SortExclusionMatch
Definition: mythsorthelper.h:21
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
MythSortHelper
A class to consolidate all the soring functions.
Definition: mythsorthelper.h:28
MythSortHelper::m_caseSensitive
Qt::CaseSensitivity m_caseSensitive
Whether sorting two strings should ignore upper/lower case.
Definition: mythsorthelper.h:53
MythSortHelper::m_exclusions
QString m_exclusions
A string containing names that should be ignored when greating the sortable form of a title.
Definition: mythsorthelper.h:75
mythsorthelper.h
MythSortHelper::doPathname
QString doPathname(const QString &pathname) const
Create the sortable form of an item.
Definition: mythsorthelper.cpp:200
SortPrefixRemove
@ SortPrefixRemove
Definition: mythsorthelper.h:15
MythSortHelper::doTitle
QString doTitle(const QString &title) const
Create the sortable form of an title string.
Definition: mythsorthelper.cpp:167
MythSortHelper::MythSortHelperCommon
void MythSortHelperCommon(void)
Common code for creating a MythSortHelper object.
Definition: mythsorthelper.cpp:14
MythSortHelper::m_exclList
QStringList m_exclList
The m_exclusion string converted into a string list.
Definition: mythsorthelper.h:77
MythSortHelper::MythSortHelper
MythSortHelper()
Create a MythSortHelper object.
Definition: mythsorthelper.cpp:53
MythSortHelper::m_prefixMode
SortPrefixMode m_prefixMode
Whether or not to ignore prefix words (like A, An, and The) when sorting two strings.
Definition: mythsorthelper.h:57
MythSortHelper::m_prefixesRegex2
QRegularExpression m_prefixesRegex2
A regular expression used for moving leading prefix to the end of a string.
Definition: mythsorthelper.h:68
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:55
MythSortHelper::hasPrefixes
bool hasPrefixes(void)
Does the language translation specify any prefixes.
Definition: mythsorthelper.h:47
MythCoreContext::GetBoolSetting
bool GetBoolSetting(const QString &key, bool defaultval=false)
Definition: mythcorecontext.cpp:905
mythcorecontext.h
MythSortHelper::m_prefixesRegex
QRegularExpression m_prefixesRegex
A regular expression used for removing a leading prefix.
Definition: mythsorthelper.h:65
SortPrefixMode
SortPrefixMode
Definition: mythsorthelper.h:12
std
Definition: mythchrono.h:23
resetMythSortHelper
void resetMythSortHelper(void)
Delete the MythSortHelper singleton.
Definition: mythsorthelper.cpp:150
SortPrefixKeep
@ SortPrefixKeep
Definition: mythsorthelper.h:14
singleton
static std::shared_ptr< MythSortHelper > singleton
Definition: mythsorthelper.cpp:123
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:897