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 #include "mythlogging.h"
8 
16 {
17  m_prefixes = tr("^(The |A |An )",
18  "Regular Expression for what to ignore when sorting");
19  m_prefixes = m_prefixes.trimmed();
20  if (not hasPrefixes()) {
21  // This language doesn't ignore any words when sorting
23  return;
24  }
25  if (not m_prefixes.startsWith("^"))
26  m_prefixes = "^" + m_prefixes;
27 
28  if (m_caseSensitive == Qt::CaseInsensitive)
29  {
30  m_prefixes = m_prefixes.toLower();
31  m_exclusions = m_exclusions.toLower();
32  }
33  m_prefixesRegex = QRegularExpression(m_prefixes);
34  m_prefixesRegex2 = QRegularExpression(m_prefixes + "(.*)");
35  m_exclList = m_exclusions.split(";", Qt::SkipEmptyParts);
36  // NOLINTNEXTLINE(modernize-loop-convert)
37  for (int i = 0; i < m_exclList.size(); i++)
38  m_exclList[i] = m_exclList[i].trimmed();
39 }
40 
51 {
52  if (gCoreContext) {
53 #if 0
54  // Last minute change. QStringRef::localeAwareCompare appears to
55  // always do case insensitive sorting, so there's no point in
56  // presenting this option to a user.
58  gCoreContext->GetBoolSetting("SortCaseSensitive", false)
59  ? Qt::CaseSensitive : Qt::CaseInsensitive;
60 #endif
61  m_prefixMode =
62  gCoreContext->GetBoolSetting("SortStripPrefixes", true)
64  m_exclusions =
65  gCoreContext->GetSetting("SortPrefixExceptions", "");
66  }
68 }
69 
83  Qt::CaseSensitivity case_sensitive,
84  SortPrefixMode prefix_mode,
85  QString exclusions) :
86  m_caseSensitive(case_sensitive),
87  m_prefixMode(prefix_mode),
88  m_exclusions(std::move(exclusions))
89 {
91 }
92 
103 {
104  if (other == nullptr)
105  {
106  LOG(VB_GENERAL, LOG_ERR,
107  QString("MythSortHelper created from null pointer."));
108  return;
109  }
111  m_prefixMode = other->m_prefixMode;
112  m_prefixes = other->m_prefixes;
115  m_exclusions = other->m_exclusions;
116  m_exclList = other->m_exclList;
117  m_exclMode = other->m_exclMode;
118 }
119 
120 static std::shared_ptr<MythSortHelper> singleton = nullptr;
121 
130 std::shared_ptr<MythSortHelper> getMythSortHelper(void)
131 {
132  if (singleton == nullptr)
133  // coverity[resource_leak]
134  singleton = std::make_shared<MythSortHelper>(new MythSortHelper());
135  return singleton;
136 }
137 
148 {
149  singleton = nullptr;
150 }
151 
164 QString MythSortHelper::doTitle(const QString& title) const
165 {
166  QString ltitle = title;
167  if (m_caseSensitive == Qt::CaseInsensitive)
168  ltitle = ltitle.toLower();
170  return ltitle;
172  {
173  if (m_exclList.contains(ltitle))
174  return ltitle;
175  } else {
176  for (const auto& str : std::as_const(m_exclList))
177  if (ltitle.startsWith(str))
178  return ltitle;
179  }
181  return ltitle.remove(m_prefixesRegex);
182  return ltitle.replace(m_prefixesRegex2, "\\2, \\1").trimmed();
183 }
184 
197 QString MythSortHelper::doPathname(const QString& pathname) const
198 {
199  QString lpathname = pathname;
200  if (m_caseSensitive == Qt::CaseInsensitive)
201  lpathname = lpathname.toLower();
203  return lpathname;
204  QStringList parts = lpathname.split("/");
205  // NOLINTNEXTLINE(modernize-loop-convert)
206  for (int i = 0; i < parts.size(); ++i) {
207  if (std::any_of(m_exclList.cbegin(), m_exclList.cend(),
208  [&parts,i](const QString& excl)
209  { return parts[i].startsWith(excl); } ))
210  continue;
212  parts[i] = parts[i].remove(m_prefixesRegex);
213  else
214  parts[i] = parts[i].replace(m_prefixesRegex2, "\\2, \\1").trimmed();
215  }
216  return parts.join("/");
217 }
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:130
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:39
SortPrefixRemove
@ SortPrefixRemove
Definition: mythsorthelper.h:15
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:197
SortPrefixKeep
@ SortPrefixKeep
Definition: mythsorthelper.h:14
mythlogging.h
MythSortHelper::doTitle
QString doTitle(const QString &title) const
Create the sortable form of an title string.
Definition: mythsorthelper.cpp:164
MythSortHelper::MythSortHelperCommon
void MythSortHelperCommon(void)
Common code for creating a MythSortHelper object.
Definition: mythsorthelper.cpp:15
SortExclusionMatch
@ SortExclusionMatch
Definition: mythsorthelper.h:21
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:50
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:57
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:912
mythcorecontext.h
MythSortHelper::m_prefixesRegex
QRegularExpression m_prefixesRegex
A regular expression used for removing a leading prefix.
Definition: mythsorthelper.h:65
resetMythSortHelper
void resetMythSortHelper(void)
Delete the MythSortHelper singleton.
Definition: mythsorthelper.cpp:147
SortPrefixMode
SortPrefixMode
Definition: mythsorthelper.h:12
singleton
static std::shared_ptr< MythSortHelper > singleton
Definition: mythsorthelper.cpp:120
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:904