23 #include "datapaths.h" 25 #include <QCoreApplication> 26 #include "plugins/engineplugin.h" 28 #include <QDesktopServices> 29 #include <QProcessEnvironment> 33 #if QT_VERSION >= 0x050000 34 #include <QStandardPaths> 40 static const QString PLUGINS_DIR_NAME;
42 bool bIsPortableModeOn;
43 QString programsDirectoryName;
44 QString programsSupportDirectoryName;
45 QString demosDirectoryName;
46 QString workingDirectory;
55 const QString DataPaths::PROGRAMS_APPDATA_DIR_NAME =
"Library/Preferences/Doomseeker";
56 const QString DataPaths::PROGRAMS_APPDATASUPPORT_DIR_NAME =
"Library/Application Support/Doomseeker";
58 const QString DataPaths::PROGRAMS_APPDATA_DIR_NAME =
".doomseeker";
59 const QString DataPaths::PROGRAMS_APPDATASUPPORT_DIR_NAME =
"";
61 const QString DataPaths::DEMOS_DIR_NAME =
"demos";
62 const QString DataPaths::CHATLOGS_DIR_NAME =
"chatlogs";
64 const QString DataPaths::TRANSLATIONS_DIR_NAME =
"translations";
65 const QString DataPaths::UPDATE_PACKAGES_DIR_NAME =
"updates";
66 const QString DataPaths::UPDATE_PACKAGE_FILENAME_PREFIX =
"doomseeker-update-pkg-";
68 DataPaths::DataPaths(
bool bPortableModeOn)
70 d->bIsPortableModeOn = bPortableModeOn;
72 d->programsDirectoryName = PROGRAMS_APPDATA_DIR_NAME;
73 d->programsSupportDirectoryName = PROGRAMS_APPDATASUPPORT_DIR_NAME;
74 d->demosDirectoryName = PROGRAMS_APPDATA_DIR_NAME + QDir::separator() + DEMOS_DIR_NAME;
75 d->workingDirectory =
"./";
78 DataPaths::~DataPaths()
84 QStringList failedList;
89 failedList.append(dataDirectory);
99 bool bAllSuccessful =
true;
104 bAllSuccessful =
false;
109 bAllSuccessful =
false;
115 bAllSuccessful =
false;
118 return bAllSuccessful;
123 return staticDefaultInstance;
126 QString DataPaths::demosDirectoryPath()
const 134 QStringList failedList;
135 QList<QDir> checkList;
138 if (!d->programsSupportDirectoryName.isEmpty())
141 foreach(
const QDir &dataDirectory, checkList)
143 if (!dataDirectory.exists())
145 failedList.append(dataDirectory.absolutePath());
155 if (!isPortableModeOn())
157 #if QT_VERSION >= 0x050000 158 rootPath = QStandardPaths::standardLocations(QStandardPaths::DocumentsLocation).first();
160 rootPath = QDesktopServices::storageLocation(QDesktopServices::DocumentsLocation);
171 QString DataPaths::env(
const QString &key)
173 return QProcessEnvironment::systemEnvironment().value(key);
178 return d->programsDirectoryName;
181 void DataPaths::initDefault(
bool bPortableModeOn)
183 assert(staticDefaultInstance == NULL &&
"DataPaths can have only one default.");
184 if (staticDefaultInstance == NULL)
186 staticDefaultInstance =
new DataPaths(bPortableModeOn);
190 bool DataPaths::isPortableModeOn()
const 192 return d->bIsPortableModeOn;
195 void DataPaths::setPortableModeOn(
bool b)
197 d->bIsPortableModeOn = b;
200 void DataPaths::setProgramDirName(
const QString& name)
202 d->programsDirectoryName = name;
208 if (!isPortableModeOn())
210 #if QT_VERSION >= 0x050000 211 rootPath = QStandardPaths::standardLocations(QStandardPaths::DataLocation).first();
213 rootPath = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
226 PrivData<DataPaths>::PLUGINS_DIR_NAME, plugin.
nameCanonical()));
232 PrivData<DataPaths>::PLUGINS_DIR_NAME, plugin.
nameCanonical()));
238 QString envVarName =
"";
243 envVarName =
"ProgramFiles(x86)";
247 envVarName =
"ProgramW6432";
251 envVarName =
"ProgramFiles";
258 QString path = env(envVarName);
259 if (path.isEmpty() && machineType != Preferred)
284 if (isPortableModeOn() || d->programsSupportDirectoryName.isEmpty())
288 return appSupportDataDir;
299 paths.append(QDir::currentPath());
300 paths.append(QCoreApplication::applicationDirPath());
301 paths.append(
"/usr/share/doomseeker");
302 paths.append(
"/usr/local/share/doomseeker");
303 QString subdirFiltered = subdir.trimmed();
304 if (!subdirFiltered.isEmpty())
306 for (
int i = 0; i < paths.size(); ++i)
316 Strings::triml(append,
"/\\");
318 if (isPortableModeOn())
320 QString path = d->workingDirectory +
"/" + append;
321 return QDir(path).absolutePath();
330 QString envVar = env(
"APPDATA");
340 dir = QDir::homePath();
347 Strings::trimr(dir,
"/\\");
349 dir += QDir::separator() + append;
351 return QDir(dir).absolutePath();
356 if (!rootDir.exists(dirToCreate))
358 return rootDir.mkdir(dirToCreate);
371 QFileInfo fileInfo(path);
373 bool bCondition1 = !path.isEmpty();
374 bool bCondition2 = fileInfo.exists();
375 bool bCondition3 = fileInfo.isDir();
377 return bCondition1 && bCondition2 && bCondition3;
382 return d->workingDirectory;
static QString combinePaths(QString pathFront, QString pathEnd)
bool tryCreateDirectory(const QDir &rootDir, const QString &dirToCreate) const
If directory already exists true is returned.
const QString & programDirName() const
Defaults to PROGRAMS_APPDATA_DIR_NAME.
QString pluginDocumentsLocationPath(const EnginePlugin &plugin) const
Place where EnginePlugin can store its "My Documents" content.
QStringList directoriesExist() const
Checks if all necessary directories exist.
static bool validateDir(const QString &path)
QString programsDataDirectoryPath() const
Path to directory where this concrete application should store its data.
Represents directories used by Doomseeker to store data.
static QString programFilesDirectory(MachineType machineType)
QString localDataLocationPath(const QString &subpath=QString()) const
Path to the directory where large data should be saved.
const QString & workingDirectory() const
Program working directory.
QString documentsLocationPath(const QString &subpath=QString()) const
Path to the "My Documents" directory with Doomseeker's own subpath suffix.
QString nameCanonical() const
Derived from actual plugin name.
QStringList canWrite() const
Checks if all directories can be written to.
QString systemAppDataDirectory(QString append=QString()) const
Gets path to the root directory for data storage.
QString programsDataSupportDirectoryPath() const
Allows switching from Preferences to Application Support on OS X.
bool validateAppDataDirectory()
Checks if the root directory for Doomseeker data storage is accessible.
QString pluginLocalDataLocationPath(const EnginePlugin &plugin) const
Place where EnginePlugin can store its local files.
static QStringList staticDataSearchDirs(const QString &subdir=QString())
Paths to directories where program should search for its static data.
static DataPaths * defaultInstance()
Retrieves default instance that is used throughout the program.
bool createDirectories()
Creates necessary directories for application run.