MythTV  master
mythdirs.cpp
Go to the documentation of this file.
1 #include <iostream>
2 #include <cstdlib>
3 
4 #include <QDir>
5 #include <QCoreApplication>
6 
7 #include "mythconfig.h" // for CONFIG_DARWIN
8 #include "mythdirs.h"
9 #include "mythlogging.h"
10 
11 #ifdef Q_OS_ANDROID
12 #include <QStandardPaths>
13 #include <sys/statfs.h>
14 #endif
15 
16 static QString installprefix;
17 static QString appbindir;
18 static QString sharedir;
19 static QString libdir;
20 static QString confdir;
21 static QString themedir;
22 static QString pluginsdir;
23 static QString translationsdir;
24 static QString filtersdir;
25 static QString cachedir;
26 static QString remotecachedir;
27 static QString themebasecachedir;
28 static QString thumbnaildir;
29 
31 {
32  installprefix = qgetenv( "MYTHTVDIR" );
33  confdir = qgetenv( "MYTHCONFDIR" );
34 
35  if (confdir.length())
36  {
37  LOG(VB_GENERAL, LOG_NOTICE, QString("Read conf dir = %1").arg(confdir));
38  confdir.replace("$HOME", QDir::homePath());
39  }
40 
41 #ifdef _WIN32
42 
43  if (installprefix.length() == 0)
44  installprefix = QDir( qApp->applicationDirPath() )
45  .absolutePath();
46 
47  appbindir = installprefix + "/";
48  libdir = appbindir;
49 
50  // Turn into Canonical Path for consistent compares
51 
52  QDir sDir(qgetenv("ProgramData") + "\\mythtv\\");
53  sharedir = sDir.canonicalPath() + "/";
54 
55  if (confdir.length() == 0)
56  confdir = qgetenv( "LOCALAPPDATA" ) + "\\mythtv";
57 
58  #if 0
59  // The following code only works for Qt 5.0 and above, but it may
60  // be a generic solution for both windows & linux.
61  // Re-evalute use oce min Qt == 5.0
62 
63  // QStringList sorted by least public to most.
64  // Assume first is private user data and last is shared program data.
65 
66  qApp->setOrganizationName( "mythtv" );
67 
68  QStringList lstPaths = QStandardPaths::standardLocations(
69  QStandardPaths::DataLocation);
70 
71  // Remove AppName from end of path
72 
73  if (lstPaths.length() > 0)
74  {
75  QString sAppName = qApp->applicationName();
76 
77  sharedir = lstPaths.last();
78 
79  if (sharedir.endsWith( sAppName ))
80  sharedir = sharedir.left( sharedir.length() - sAppName.length());
81 
82  // Only use if user didn't override with env variable.
83  if (confdir.length() == 0)
84  {
85  confdir = lstPaths.first();
86 
87  if (confdir.endsWith( sAppName ))
88  confdir = confdir.left( confdir.length() - sAppName.length());
89  }
90  }
91 
92  #endif
93 
94  if (sharedir.length() == 0)
95  sharedir = confdir;
96 
97 #elif defined(Q_OS_ANDROID)
98  if (installprefix.isEmpty())
99  installprefix = QDir( qApp->applicationDirPath() )
100  .absolutePath();
101  QString extdir = QStandardPaths::writableLocation(QStandardPaths::GenericDataLocation) + "/Mythtv";
102  if (!QDir(extdir).exists())
103  QDir(extdir).mkdir(".");
104 
105  if (confdir.isEmpty())
106  confdir = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
107 
108 #if 0
109  // TODO allow choice of base fs or the SD card for data
110  QStringList appLocs = QStandardPaths::standardLocations(QStandardPaths::AppDataLocation);
111  uint64_t maxFreeSpace = 0;
112  for(auto s : appLocs)
113  {
114  struct statfs statFs;
115  memset(&statFs, 0, sizeof(statFs));
116  int ret = statfs(s.toLocal8Bit().data(), &statFs);
117  if (ret == 0 && statFs.f_bavail >= maxFreeSpace)
118  {
119  maxFreeSpace = statFs.f_bavail;
120  confdir = s;
121  }
122  LOG(VB_GENERAL, LOG_NOTICE, QString(" appdatadir = %1 (%2, %3, %4)")
123  .arg(s)
124  .arg(statFs.f_bavail)
125  .arg(statFs.f_bfree)
126  .arg(statFs.f_bsize));
127  }
128  QStringList cacheLocs = QStandardPaths::standardLocations(QStandardPaths::CacheLocation);
129  maxFreeSpace = 0;
130  for(auto s : cacheLocs)
131  {
132  struct statfs statFs;
133  memset(&statFs, 0, sizeof(statFs));
134  int ret = statfs(s.toLocal8Bit().data(), &statFs);
135  if (ret == 0 && statFs.f_bavail >= maxFreeSpace)
136  {
137  maxFreeSpace = statFs.f_bavail;
138  //confdir = s;
139  }
140  LOG(VB_GENERAL, LOG_NOTICE, QString(" cachedir = %1 (%2, %3, %4)")
141  .arg(s)
142  .arg(statFs.f_bavail)
143  .arg(statFs.f_bfree)
144  .arg(statFs.f_bsize));
145  }
146 #endif
147 
148  appbindir = installprefix + "/";
149  sharedir = "assets:/mythtv/";
150  libdir = installprefix + "/";
151 
152 
153 #else
154 
155  if (installprefix.length() == 0)
156  installprefix = QString(RUNPREFIX);
157 
158  QDir prefixDir = qApp->applicationDirPath();
159 
160  if (QDir(installprefix).isRelative())
161  {
162  // If the PREFIX is relative, evaluate it relative to our
163  // executable directory. This can be fragile on Unix, so
164  // use relative PREFIX values with care.
165 
166  LOG(VB_GENERAL, LOG_DEBUG, QString("Relative PREFIX! (%1), appDir=%2")
167  .arg(installprefix) .arg(prefixDir.canonicalPath()));
168 
169  if (!prefixDir.cd(installprefix))
170  {
171  LOG(VB_GENERAL, LOG_ERR,
172  QString("Relative PREFIX does not resolve, using %1")
173  .arg(prefixDir.canonicalPath()));
174  }
175  installprefix = prefixDir.canonicalPath();
176  }
177 
178  appbindir = installprefix + "/bin/";
179  sharedir = installprefix + "/share/mythtv/";
180  libdir = installprefix + '/' + QString(LIBDIRNAME) + "/mythtv/";
181 
182 #endif
183 
184  if (confdir.length() == 0)
185  confdir = QDir::homePath() + "/.mythtv";
186  cachedir = confdir + "/cache";
187  remotecachedir = cachedir + "/remotecache";
188  themebasecachedir = cachedir + "/themecache";
189  thumbnaildir = cachedir + "/thumbnails";
190 
191 #if defined(Q_OS_ANDROID)
192  themedir = sharedir + "themes/";
193  pluginsdir = libdir;
194  translationsdir = sharedir + "i18n/";
195  filtersdir = libdir;
196 #else
197  themedir = sharedir + "themes/";
198  pluginsdir = libdir + "plugins/";
199  translationsdir = sharedir + "i18n/";
200  filtersdir = libdir + "filters/";
201 #endif
202 
203  LOG(VB_GENERAL, LOG_NOTICE, "Using runtime prefix = " + installprefix);
204  LOG(VB_GENERAL, LOG_NOTICE, QString("Using configuration directory = %1")
205  .arg(confdir));
206 
207  LOG(VB_GENERAL, LOG_DEBUG, "appbindir = "+ appbindir );
208  LOG(VB_GENERAL, LOG_DEBUG, "sharedir = "+ sharedir );
209  LOG(VB_GENERAL, LOG_DEBUG, "libdir = "+ libdir );
210  LOG(VB_GENERAL, LOG_DEBUG, "themedir = "+ themedir );
211  LOG(VB_GENERAL, LOG_DEBUG, "pluginsdir = "+ pluginsdir );
212  LOG(VB_GENERAL, LOG_DEBUG, "translationsdir = "+ translationsdir );
213  LOG(VB_GENERAL, LOG_DEBUG, "filtersdir = "+ filtersdir );
214  LOG(VB_GENERAL, LOG_DEBUG, "cachedir = "+ cachedir );
215  LOG(VB_GENERAL, LOG_DEBUG, "remotecachedir = "+ remotecachedir );
216  LOG(VB_GENERAL, LOG_DEBUG, "themebasecachedir = "+ themebasecachedir);
217  LOG(VB_GENERAL, LOG_DEBUG, "thumbnaildir = "+ thumbnaildir);
218 }
219 
220 QString GetInstallPrefix(void) { return installprefix; }
221 QString GetAppBinDir(void) { return appbindir; }
222 QString GetShareDir(void) { return sharedir; }
223 QString GetLibraryDir(void) { return libdir; }
224 QString GetConfDir(void) { return confdir; }
225 QString GetThemesParentDir(void) { return themedir; }
226 QString GetPluginsDir(void) { return pluginsdir; }
227 QString GetTranslationsDir(void) { return translationsdir; }
228 QString GetFiltersDir(void) { return filtersdir; }
229 
234 QString GetCacheDir(void) { return cachedir; }
235 
241 QString GetRemoteCacheDir(void) { return remotecachedir; }
242 
249 QString GetThumbnailDir(void) { return thumbnaildir; }
250 
257 QString GetThemeBaseCacheDir(void) { return themebasecachedir; }
258 
259 // These defines provide portability for different
260 // plugin file names.
261 #if CONFIG_DARWIN
262 static const QString kPluginLibPrefix = "lib";
263 static const QString kPluginLibSuffix = ".dylib";
264 static const QString kFilterLibPrefix = "lib";
265 static const QString kFilterLibSuffix = ".dylib";
266 #elif _WIN32
267 static const QString kPluginLibPrefix = "lib";
268 static const QString kPluginLibSuffix = ".dll";
269 static const QString kFilterLibPrefix = "lib";
270 static const QString kFilterLibSuffix = ".dll";
271 #elif ANDROID
272 static const QString kPluginLibPrefix = "libmythplugin";
273 static const QString kPluginLibSuffix = ".so";
274 static const QString kFilterLibPrefix = "libmythfilter";
275 static const QString kFilterLibSuffix = ".so";
276 #else
277 static const QString kPluginLibPrefix = "lib";
278 static const QString kPluginLibSuffix = ".so";
279 static const QString kFilterLibPrefix = "lib";
280 static const QString kFilterLibSuffix = ".so";
281 #endif
282 
283 QString GetFiltersNameFilter(void)
284 {
285  return kFilterLibPrefix + '*' + kFilterLibSuffix;
286 }
287 
288 QString GetPluginsNameFilter(void)
289 {
290  return kPluginLibPrefix + '*' + kPluginLibSuffix;
291 }
292 
293 QString FindPluginName(const QString &plugname)
294 {
295  return GetPluginsDir() + kPluginLibPrefix + plugname + kPluginLibSuffix;
296 }
297 
299 {
300  return "mythfrontend_*.qm";
301 }
302 
303 QString FindTranslation(const QString &translation)
304 {
305  return GetTranslationsDir()
306  + "mythfrontend_" + translation.toLower() + ".qm";
307 }
308 
309 QString GetFontsDir(void)
310 {
311  return GetShareDir() + "fonts/";
312 }
static QString libdir
Definition: mythdirs.cpp:19
static QString installprefix
Definition: mythdirs.cpp:16
static QString themedir
Definition: mythdirs.cpp:21
static QString sharedir
Definition: mythdirs.cpp:18
QString GetLibraryDir(void)
Definition: mythdirs.cpp:223
QString GetTranslationsNameFilter(void)
Definition: mythdirs.cpp:298
QString GetThumbnailDir(void)
Returns the directory where all non-theme thumbnail files should be cached.
Definition: mythdirs.cpp:249
static QString remotecachedir
Definition: mythdirs.cpp:26
QString GetInstallPrefix(void)
Definition: mythdirs.cpp:220
static QString translationsdir
Definition: mythdirs.cpp:23
static QString pluginsdir
Definition: mythdirs.cpp:22
static QString thumbnaildir
Definition: mythdirs.cpp:28
long f_bavail
Definition: compat.h:170
static const QString kFilterLibSuffix
Definition: mythdirs.cpp:270
static QString filtersdir
Definition: mythdirs.cpp:24
QString GetConfDir(void)
Definition: mythdirs.cpp:224
QString GetFiltersDir(void)
Definition: mythdirs.cpp:228
QString GetPluginsDir(void)
Definition: mythdirs.cpp:226
int statfs(const char *path, struct statfs *buffer)
Definition: compat.h:177
QString GetAppBinDir(void)
Definition: mythdirs.cpp:221
static QString cachedir
Definition: mythdirs.cpp:25
QString GetShareDir(void)
Definition: mythdirs.cpp:222
QString FindPluginName(const QString &plugname)
Definition: mythdirs.cpp:293
Definition: compat.h:165
QString GetThemeBaseCacheDir(void)
Returns the base directory where all theme related files should be cached.
Definition: mythdirs.cpp:257
static const QString kPluginLibSuffix
Definition: mythdirs.cpp:268
QString GetTranslationsDir(void)
Definition: mythdirs.cpp:227
QString GetCacheDir(void)
Returns the base directory for all cached files.
Definition: mythdirs.cpp:234
#define LOG(_MASK_, _LEVEL_, _STRING_)
Definition: mythlogging.h:41
static QString themebasecachedir
Definition: mythdirs.cpp:27
QString GetFiltersNameFilter(void)
Definition: mythdirs.cpp:283
QString GetFontsDir(void)
Definition: mythdirs.cpp:309
static QString appbindir
Definition: mythdirs.cpp:17
long f_bsize
Definition: compat.h:167
static const QString kPluginLibPrefix
Definition: mythdirs.cpp:267
QString GetThemesParentDir(void)
Definition: mythdirs.cpp:225
QString GetPluginsNameFilter(void)
Definition: mythdirs.cpp:288
QString FindTranslation(const QString &translation)
Definition: mythdirs.cpp:303
QString GetRemoteCacheDir(void)
Returns the directory for all files cached from the backend.
Definition: mythdirs.cpp:241
static QString confdir
Definition: mythdirs.cpp:20
static const QString kFilterLibPrefix
Definition: mythdirs.cpp:269
void InitializeMythDirs(void)
Definition: mythdirs.cpp:30