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_case_sensitive == Qt::CaseInsensitive)
28  {
29  m_prefixes = m_prefixes.toLower();
30  m_exclusions = m_exclusions.toLower();
31  }
32  m_prefixes_regex = QRegularExpression(m_prefixes);
33  m_prefixes_regex2 = QRegularExpression(m_prefixes + "(.*)");
34  m_excl_list = m_exclusions.split(";", QString::SkipEmptyParts);
35  for (int i = 0; i < m_excl_list.size(); i++)
36  m_excl_list[i] = m_excl_list[i].trimmed();
37 }
38 
49 {
50  if (gCoreContext) {
51 #if 0
52  // Last minute change. QStringRef::localeAwareCompare appears to
53  // always do case insensitive sorting, so there's no point in
54  // presenting this option to a user.
56  gCoreContext->GetBoolSetting("SortCaseSensitive", false)
57  ? Qt::CaseSensitive : Qt::CaseInsensitive;
58 #endif
60  gCoreContext->GetBoolSetting("SortStripPrefixes", true)
62  m_exclusions =
63  gCoreContext->GetSetting("SortPrefixExceptions", "");
64  }
66 }
67 
81  Qt::CaseSensitivity case_sensitive,
82  SortPrefixMode prefix_mode,
83  QString exclusions) :
84  m_case_sensitive(case_sensitive),
85  m_prefix_mode(prefix_mode),
86  m_exclusions(std::move(exclusions))
87 {
89 }
90 
101 {
103  m_prefix_mode = other->m_prefix_mode;
104  m_prefixes = other->m_prefixes;
107  m_exclusions = other->m_exclusions;
108  m_excl_list = other->m_excl_list;
109  m_excl_mode = other->m_excl_mode;
110 }
111 
112 static std::shared_ptr<MythSortHelper> singleton = nullptr;
113 
122 std::shared_ptr<MythSortHelper> getMythSortHelper(void)
123 {
124  if (singleton == nullptr)
125  singleton = std::make_shared<MythSortHelper>(new MythSortHelper());
126  return singleton;
127 }
128 
139 {
140  singleton = nullptr;
141 }
142 
155 QString MythSortHelper::doTitle(const QString& title) const
156 {
157  QString ltitle = title;
158  if (m_case_sensitive == Qt::CaseInsensitive)
159  ltitle = ltitle.toLower();
161  return ltitle;
163  {
164  if (m_excl_list.contains(ltitle))
165  return ltitle;
166  } else {
167  foreach (const QString &str, m_excl_list)
168  if (ltitle.startsWith(str))
169  return ltitle;
170  }
172  return ltitle.remove(m_prefixes_regex);
173  return ltitle.replace(m_prefixes_regex2, "\\2, \\1").trimmed();
174 }
175 
188 QString MythSortHelper::doPathname(const QString& pathname) const
189 {
190  QString lpathname = pathname;
191  if (m_case_sensitive == Qt::CaseInsensitive)
192  lpathname = lpathname.toLower();
194  return lpathname;
195  QStringList parts = lpathname.split("/");
196  for (int i = 0; i < parts.size(); ++i) {
197  bool excluded = false;
198  for (int j = 0; j < m_excl_list.size(); ++j) {
199  if (parts[i].startsWith(m_excl_list[j])) {
200  excluded = true;
201  break;
202  }
203  }
204  if (excluded)
205  continue;
207  parts[i] = parts[i].remove(m_prefixes_regex);
208  else
209  parts[i] = parts[i].replace(m_prefixes_regex2, "\\2, \\1").trimmed();
210  }
211  return parts.join("/");
212 }
A class to consolidate all the soring functions.
SortExclusionMode m_excl_mode
QStringList m_excl_list
The m_exclusion string converted into a string list.
void MythSortHelperCommon(void)
Common code for creating a MythSortHelper object.
QRegularExpression m_prefixes_regex2
A regular expression used for moving leading prefix to the end of a string.
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
QString m_exclusions
A string containing names that should be ignored when greating the sortable form of a title.
Qt::CaseSensitivity m_case_sensitive
Whether sorting two strings should ignore upper/lower case.
QRegularExpression m_prefixes_regex
A regular expression used for removing a leading prefix.
MythSortHelper()
Create a MythSortHelper object.
QString GetSetting(const QString &key, const QString &defaultval="")
QString doPathname(const QString &pathname) const
Create the sortable form of an item.
static std::shared_ptr< MythSortHelper > singleton
QString m_prefixes
A string containing the regular expression of prefixes to ignore when sorting.
SortPrefixMode m_prefix_mode
Whether or not to ignore prefix words (like A, An, and The) when sorting two strings.
void resetMythSortHelper(void)
Delete the MythSortHelper singleton.
SortPrefixMode
bool GetBoolSetting(const QString &key, bool defaultval=false)
bool hasPrefixes(void)
Does the language translation specify any prefixes.
std::shared_ptr< MythSortHelper > getMythSortHelper(void)
Get a pointer to the MythSortHelper singleton.
QString doTitle(const QString &title) const
Create the sortable form of an title string.