23 #include "datapaths.h"
25 #include <QCoreApplication>
27 #include <QDesktopServices>
34 bool bIsPortableModeOn;
35 QString programsDirectoryName;
36 QString programsSupportDirectoryName;
37 QString demosDirectoryName;
38 QString workingDirectory;
47 const QString DataPaths::PROGRAMS_APPDATA_DIR_NAME =
"Library/Preferences/Doomseeker";
48 const QString DataPaths::PROGRAMS_APPDATASUPPORT_DIR_NAME =
"Library/Application Support/Doomseeker";
50 const QString DataPaths::PROGRAMS_APPDATA_DIR_NAME =
".doomseeker";
51 const QString DataPaths::PROGRAMS_APPDATASUPPORT_DIR_NAME =
"";
53 const QString DataPaths::DEMOS_DIR_NAME =
"demos";
54 const QString DataPaths::CHATLOGS_DIR_NAME =
"chatlogs";
55 const QString DataPaths::TRANSLATIONS_DIR_NAME =
"translations";
56 const QString DataPaths::UPDATE_PACKAGES_DIR_NAME =
"updates";
57 const QString DataPaths::UPDATE_PACKAGE_FILENAME_PREFIX =
"doomseeker-update-pkg-";
59 DataPaths::DataPaths(
bool bPortableModeOn)
61 d->bIsPortableModeOn = bPortableModeOn;
63 d->programsDirectoryName = PROGRAMS_APPDATA_DIR_NAME;
64 d->programsSupportDirectoryName = PROGRAMS_APPDATASUPPORT_DIR_NAME;
65 d->demosDirectoryName = PROGRAMS_APPDATA_DIR_NAME + QDir::separator() + DEMOS_DIR_NAME;
66 d->workingDirectory =
"./";
69 DataPaths::~DataPaths()
75 QStringList failedList;
80 failedList.append(dataDirectory);
90 bool bAllSuccessful =
true;
95 bAllSuccessful =
false;
100 bAllSuccessful =
false;
106 bAllSuccessful =
false;
109 return bAllSuccessful;
114 return staticDefaultInstance;
117 QString DataPaths::demosDirectoryPath()
const
125 QStringList failedList;
126 QList<QDir> checkList;
129 if (!d->programsSupportDirectoryName.isEmpty())
132 foreach(
const QDir &dataDirectory, checkList)
134 if (!dataDirectory.exists())
136 failedList.append(dataDirectory.absolutePath());
145 return d->programsDirectoryName;
148 void DataPaths::initDefault(
bool bPortableModeOn)
150 assert(staticDefaultInstance == NULL &&
"DataPaths can have only one default.");
151 if (staticDefaultInstance == NULL)
153 staticDefaultInstance =
new DataPaths(bPortableModeOn);
157 bool DataPaths::isPortableModeOn()
const
159 return d->bIsPortableModeOn;
162 void DataPaths::setPortableModeOn(
bool b)
164 d->bIsPortableModeOn = b;
167 void DataPaths::setProgramDirName(
const QString& name)
169 d->programsDirectoryName = name;
182 if (!isPortableModeOn())
184 rootPath = QDesktopServices::storageLocation(QDesktopServices::DataLocation);
197 QString envVarName =
"";
202 envVarName =
"ProgramFiles(x86)";
206 envVarName =
"ProgramW6432";
210 envVarName =
"ProgramFiles";
217 QString path = getenv(envVarName.toAscii().constData());
218 if (path.isEmpty() && machineType != Preferred)
243 if (isPortableModeOn() || d->programsSupportDirectoryName.isEmpty())
247 return appSupportDataDir;
250 void DataPaths::setWorkingDirectory(
const QString &workingDirectory)
258 paths.append(QDir::currentPath());
259 paths.append(QCoreApplication::applicationDirPath());
260 paths.append(
"/usr/share/doomseeker");
261 paths.append(
"/usr/local/share/doomseeker");
262 QString subdirFiltered = subdir.trimmed();
263 if (!subdirFiltered.isEmpty())
265 for (
int i = 0; i < paths.size(); ++i)
275 Strings::triml(append,
"/\\");
277 if (isPortableModeOn())
279 QString path = d->workingDirectory +
"/" + append;
280 return QDir(path).absolutePath();
289 QString envVar = getenv(
"APPDATA");
299 dir = QDir::homePath();
306 Strings::trimr(dir,
"/\\");
308 dir += QDir::separator() + append;
310 return QDir(dir).absolutePath();
315 if (!rootDir.exists(dirToCreate))
317 return rootDir.mkdir(dirToCreate);
330 QFileInfo fileInfo(path);
332 bool bCondition1 = !path.isEmpty();
333 bool bCondition2 = fileInfo.exists();
334 bool bCondition3 = fileInfo.isDir();
336 return bCondition1 && bCondition2 && bCondition3;
341 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.
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 it's 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.
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.
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.