MythTV  master
mythuithemehelper.cpp
Go to the documentation of this file.
1 // Qt
2 #include <QDir>
3 
4 // MythTV
5 #include "mythlogging.h"
6 #include "storagegroup.h"
7 #include "mythdirs.h"
8 #include "mythcorecontext.h"
9 #include "mythuithemehelper.h"
10 
11 #define LOC QString("ThemeHelper: ")
12 
14 {
15  StorageGroup sgroup("Themes", gCoreContext->GetHostName());
16  m_userThemeDir = sgroup.GetFirstDir(true);
17 
18  QString themename = gCoreContext->GetSetting("Theme", DEFAULT_UI_THEME);
19  QString themedir = FindThemeDir(themename);
20 
21  ThemeInfo themeinfo(themedir);
22  m_isWide = themeinfo.IsWide();
23  m_baseSize = themeinfo.GetBaseRes();
24  m_themename = themeinfo.GetName();
25  LOG(VB_GUI, LOG_INFO, LOC + QString("Using theme base resolution of %1x%2")
26  .arg(m_baseSize.width()).arg(m_baseSize.height()));
27 
28  m_themepathname = themedir + '/';
29  m_searchPaths.clear();
30 
31  themename = gCoreContext->GetSetting("MenuTheme", "defaultmenu");
32  if (themename == "default")
33  themename = "defaultmenu";
35 }
36 
47 QString MythUIThemeHelper::FindThemeDir(const QString& ThemeName, bool Fallback)
48 {
49  QString testdir;
50  QDir dir;
51 
52  if (!ThemeName.isEmpty())
53  {
54  testdir = m_userThemeDir + ThemeName;
55  dir.setPath(testdir);
56  if (dir.exists())
57  return testdir;
58 
59  testdir = GetThemesParentDir() + ThemeName;
60  dir.setPath(testdir);
61  if (dir.exists())
62  return testdir;
63 
64  LOG(VB_GENERAL, LOG_WARNING, LOC + QString("No theme dir: '%1'").arg(dir.absolutePath()));
65  }
66 
67  if (!Fallback)
68  return QString();
69 
71  dir.setPath(testdir);
72  if (dir.exists())
73  {
74  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Could not find theme: %1 - Switching to %2")
75  .arg(ThemeName).arg(DEFAULT_UI_THEME));
77  return testdir;
78  }
79 
80  LOG(VB_GENERAL, LOG_WARNING, LOC + QString("No default theme dir: '%1'")
81  .arg(dir.absolutePath()));
82 
84  dir.setPath(testdir);
85  if (dir.exists())
86  {
87  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Could not find theme: %1 - Switching to %2")
88  .arg(ThemeName).arg(FALLBACK_UI_THEME));
90  return testdir;
91  }
92 
93  LOG(VB_GENERAL, LOG_ERR, LOC + QString("No fallback GUI theme dir: '%1'").arg(dir.absolutePath()));
94  return QString();
95 }
96 
105 QString MythUIThemeHelper::FindMenuThemeDir(const QString& MenuName)
106 {
107  QString testdir;
108  QDir dir;
109 
110  testdir = m_userThemeDir + MenuName;
111  dir.setPath(testdir);
112  if (dir.exists())
113  return testdir;
114 
115  testdir = GetThemesParentDir() + MenuName;
116  dir.setPath(testdir);
117  if (dir.exists())
118  return testdir;
119 
120  testdir = GetShareDir();
121  dir.setPath(testdir);
122  if (dir.exists())
123  {
124  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Could not find menu theme: %1 - Switching to default")
125  .arg(MenuName));
126  gCoreContext->SaveSetting("MenuTheme", "default");
127  return testdir;
128  }
129 
130  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Could not find menu theme: %1 - Fallback to default failed.")
131  .arg(MenuName));
132  return QString();
133 }
134 
136 {
137  return m_menuthemepathname;
138 }
139 
141 {
142  return m_themepathname;
143 }
144 
146 {
147  return m_themename;
148 }
149 
151 {
152  if (!m_searchPaths.isEmpty())
153  return m_searchPaths;
154 
155  // traverse up the theme inheritance list adding their location to the search path
156  QList<ThemeInfo> themeList = GetThemes(THEME_UI);
157  bool found = true;
158  QString themeName = m_themename;
159  QString baseName;
160  QString dirName;
161 
162  while (found && !themeName.isEmpty())
163  {
164  // find the ThemeInfo for this theme
165  found = false;
166  baseName = "";
167  dirName = "";
168 
169  for (int x = 0; x < themeList.count(); x++)
170  {
171  if (themeList.at(x).GetName() == themeName)
172  {
173  found = true;
174  baseName = themeList.at(x).GetBaseTheme();
175  dirName = themeList.at(x).GetDirectoryName();
176  break;
177  }
178  }
179 
180  // try to find where the theme is installed
181  if (found)
182  {
183  QString themedir = FindThemeDir(dirName, false);
184  if (!themedir.isEmpty())
185  {
186  LOG(VB_GUI, LOG_INFO, LOC + QString("Adding path '%1' to theme search paths").arg(themedir));
187  m_searchPaths.append(themedir + '/');
188  }
189  else
190  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Could not find ui theme location: %1").arg(themedir));
191  }
192  else
193  {
194  LOG(VB_GENERAL, LOG_ERR, LOC + QString("Could not find inherited theme: %1").arg(themeName));
195  }
196 
198  }
199 
200  if (m_isWide)
201  m_searchPaths.append(GetThemesParentDir() + "default-wide/");
202 
203  m_searchPaths.append(GetThemesParentDir() + "default/");
204  m_searchPaths.append("/tmp/");
205  return m_searchPaths;
206 }
207 
209 {
210  QDir themeDirs(GetThemesParentDir());
211  themeDirs.setFilter(QDir::Dirs | QDir::NoDotAndDotDot);
212  themeDirs.setSorting(QDir::Name | QDir::IgnoreCase);
213  QFileInfoList fileList { themeDirs.entryInfoList() };
214  themeDirs.setPath(m_userThemeDir);
215  fileList.append(themeDirs.entryInfoList());
216 
217  QList<ThemeInfo> themeList;
218  for (const auto & theme : qAsConst(fileList))
219  {
220  if (theme.baseName() == "default" || theme.baseName() == "default-wide" ||
221  theme.baseName() == "Slave")
222  {
223  continue;
224  }
225 
226  ThemeInfo themeInfo(theme.absoluteFilePath());
227 
228  if (themeInfo.GetType() & Type)
229  themeList.append(themeInfo);
230  }
231 
232  return themeList;
233 }
234 
236 {
237  QFileInfo fi(Path);
238 
239  if (fi.isAbsolute() && fi.exists())
240  return true;
241 #ifdef Q_OS_ANDROID
242  if (Path.startsWith("assets:/") && fi.exists())
243  return true;
244 #endif
245 
246  QString file;
247  bool foundit = false;
248  const QStringList searchpath = GetThemeSearchPath();
249 
250  for (const auto & ii : qAsConst(searchpath))
251  {
252  if (fi.isRelative())
253  file = ii + fi.filePath();
254  else if (fi.isAbsolute() && !fi.isRoot())
255  file = ii + fi.fileName();
256 
257  if (QFile::exists(file))
258  {
259  Path = file;
260  foundit = true;
261  break;
262  }
263  }
264 
265  return foundit;
266 }
267 
269 {
270  return m_baseSize;
271 }
272 
273 
GetThemesParentDir
QString GetThemesParentDir(void)
Definition: mythdirs.cpp:225
themedir
static QString themedir
Definition: mythdirs.cpp:21
MythUIThemeHelper::GetBaseSize
QSize GetBaseSize() const
Definition: mythuithemehelper.cpp:268
MythUIThemeHelper::GetThemes
QList< ThemeInfo > GetThemes(ThemeType Type)
Definition: mythuithemehelper.cpp:208
MythUIThemeHelper::GetThemeName
QString GetThemeName()
Definition: mythuithemehelper.cpp:145
LOC
#define LOC
Definition: mythuithemehelper.cpp:11
MythCoreContext::OverrideSettingForSession
void OverrideSettingForSession(const QString &key, const QString &value)
Definition: mythcorecontext.cpp:1346
MythUIThemeHelper::m_themename
QString m_themename
Definition: mythuithemehelper.h:28
arg
arg(title).arg(filename).arg(doDelete))
MythUIThemeHelper::FindMenuThemeDir
QString FindMenuThemeDir(const QString &MenuName)
Returns the full path to the menu theme denoted by menuname.
Definition: mythuithemehelper.cpp:105
LOG
#define LOG(_MASK_, _LEVEL_, _QSTRING_)
Definition: mythlogging.h:23
build_compdb.file
file
Definition: build_compdb.py:55
mythdirs.h
MythUIThemeHelper::m_menuthemepathname
QString m_menuthemepathname
Definition: mythuithemehelper.h:26
ThemeInfo::GetName
QString GetName() const
Definition: themeinfo.h:30
baseName
QString baseName
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:593
MythUIThemeHelper::m_baseSize
QSize m_baseSize
Definition: mythuithemehelper.h:30
mythlogging.h
ThemeInfo::IsWide
bool IsWide() const
Definition: themeinfo.cpp:247
MythUIThemeHelper::GetThemeDir
QString GetThemeDir()
Definition: mythuithemehelper.cpp:140
FALLBACK_UI_THEME
#define FALLBACK_UI_THEME
Definition: mythuithemehelper.h:8
GetShareDir
QString GetShareDir(void)
Definition: mythdirs.cpp:222
MythUIThemeHelper::m_userThemeDir
QString m_userThemeDir
Definition: mythuithemehelper.h:29
MythUIThemeHelper::FindThemeDir
QString FindThemeDir(const QString &ThemeName, bool Fallback=true)
Returns the full path to the theme denoted by themename.
Definition: mythuithemehelper.cpp:47
StorageGroup::GetFirstDir
QString GetFirstDir(bool appendSlash=false) const
Definition: storagegroup.cpp:189
ThemeInfo
Definition: themeinfo.h:22
storagegroup.h
MythUIThemeHelper::GetThemeSearchPath
QStringList GetThemeSearchPath()
Definition: mythuithemehelper.cpp:150
ThemeType
ThemeType
Definition: themeinfo.h:14
ThemeInfo::GetBaseRes
QSize GetBaseRes() const
Definition: themeinfo.h:29
gCoreContext
MythCoreContext * gCoreContext
This global variable contains the MythCoreContext instance for the app.
Definition: mythcorecontext.cpp:60
MythUIThemeHelper::m_searchPaths
QStringList m_searchPaths
Definition: mythuithemehelper.h:25
MythUIThemeHelper::m_themepathname
QString m_themepathname
Definition: mythuithemehelper.h:27
MythUIThemeHelper::m_isWide
bool m_isWide
Definition: mythuithemehelper.h:31
DEFAULT_UI_THEME
#define DEFAULT_UI_THEME
Definition: mythuithemehelper.h:7
MythUIThemeHelper::InitThemeHelper
void InitThemeHelper()
Definition: mythuithemehelper.cpp:13
mythburn.themeName
string themeName
Definition: mythburn.py:218
ThemeInfo::GetType
int GetType() const
Definition: themeinfo.h:35
MythUIThemeHelper::FindThemeFile
bool FindThemeFile(QString &Path)
Definition: mythuithemehelper.cpp:235
mythcorecontext.h
dir
QDir dir
Definition: mythplugins/mytharchive/mytharchivehelper/main.cpp:1174
mythuithemehelper.h
StorageGroup
Definition: storagegroup.h:12
MythCoreContext::GetHostName
QString GetHostName(void)
Definition: mythcorecontext.cpp:859
MythUIThemeHelper::GetMenuThemeDir
QString GetMenuThemeDir()
Definition: mythuithemehelper.cpp:135
MythCoreContext::SaveSetting
void SaveSetting(const QString &key, int newValue)
Definition: mythcorecontext.cpp:902
THEME_UI
@ THEME_UI
Definition: themeinfo.h:16
MythCoreContext::GetSetting
QString GetSetting(const QString &key, const QString &defaultval="")
Definition: mythcorecontext.cpp:919