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 #include "mythcorecontext.h"
6 
14 {
15  m_prefixes = tr("^(The |A |An )",
16  "Regular Expression for what to ignore when sorting");
17  m_prefixes = m_prefixes.trimmed();
18  if (not hasPrefixes()) {
19  // This language doesn't ignore any words when sorting
21  return;
22  }
23  if (not m_prefixes.startsWith("^"))
24  m_prefixes = "^" + m_prefixes;
25 
26  if (m_case_sensitive == Qt::CaseInsensitive)
27  {
28  m_prefixes = m_prefixes.toLower();
29  m_exclusions = m_exclusions.toLower();
30  }
31  m_prefixes_regex = QRegularExpression(m_prefixes);
32  m_prefixes_regex2 = QRegularExpression(m_prefixes + "(.*)");
33  m_excl_list = m_exclusions.split(";", QString::SkipEmptyParts);
34  for (int i = 0; i < m_excl_list.size(); i++)
35  m_excl_list[i] = m_excl_list[i].trimmed();
36 }
37 
48 {
49  if (gCoreContext) {
50 #if 0
51  // Last minute change. QStringRef::localeAwareCompare appears to
52  // always do case insensitive sorting, so there's no point in
53  // presenting this option to a user.
55  gCoreContext->GetBoolSetting("SortCaseSensitive", false)
56  ? Qt::CaseSensitive : Qt::CaseInsensitive;
57 #endif
59  gCoreContext->GetBoolSetting("SortStripPrefixes", true)
61  m_exclusions =
62  gCoreContext->GetSetting("SortPrefixExceptions", "");
63  }
65 }
66 
80  Qt::CaseSensitivity case_sensitive,
81  SortPrefixMode prefix_mode,
82  const QString &exclusions) :
83  m_case_sensitive(case_sensitive),
84  m_prefix_mode(prefix_mode),
85  m_exclusions(exclusions)
86 {
88 }
89 
100 {
102  m_prefix_mode = other->m_prefix_mode;
103  m_prefixes = other->m_prefixes;
106  m_exclusions = other->m_exclusions;
107  m_excl_list = other->m_excl_list;
108  m_excl_mode = other->m_excl_mode;
109 }
110 
111 static std::shared_ptr<MythSortHelper> singleton = nullptr;
112 
121 std::shared_ptr<MythSortHelper> getMythSortHelper(void)
122 {
123  if (singleton == nullptr)
124  singleton = std::make_shared<MythSortHelper>(new MythSortHelper());
125  return singleton;
126 }
127 
138 {
139  singleton = nullptr;
140 }
141 
154 QString MythSortHelper::doTitle(const QString& title) const
155 {
156  QString ltitle = title;
157  if (m_case_sensitive == Qt::CaseInsensitive)
158  ltitle = ltitle.toLower();
160  return ltitle;
162  {
163  if (m_excl_list.contains(ltitle))
164  return ltitle;
165  } else {
166  foreach (const QString &str, m_excl_list)
167  if (ltitle.startsWith(str))
168  return ltitle;
169  }
171  return ltitle.remove(m_prefixes_regex);
172  return ltitle.replace(m_prefixes_regex2, "\\2, \\1").trimmed();
173 }
174 
187 QString MythSortHelper::doPathname(const QString& pathname) const
188 {
189  QString lpathname = pathname;
190  if (m_case_sensitive == Qt::CaseInsensitive)
191  lpathname = lpathname.toLower();
193  return lpathname;
194  QStringList parts = lpathname.split("/");
195  for (int i = 0; i < parts.size(); ++i) {
196  bool excluded = false;
197  for (int j = 0; j < m_excl_list.size(); ++j) {
198  if (parts[i].startsWith(m_excl_list[j])) {
199  excluded = true;
200  break;
201  }
202  }
203  if (excluded)
204  continue;
206  parts[i] = parts[i].remove(m_prefixes_regex);
207  else
208  parts[i] = parts[i].replace(m_prefixes_regex2, "\\2, \\1").trimmed();
209  }
210  return parts.join("/");
211 }
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.