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