datapaths.h
1 //------------------------------------------------------------------------------
2 // datapaths.h
3 //------------------------------------------------------------------------------
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18 // 02110-1301 USA
19 //
20 //------------------------------------------------------------------------------
21 // Copyright (C) 2010 "Zalewa" <zalewapl@gmail.com>
22 //------------------------------------------------------------------------------
23 #ifndef __DATAPATHS_H__
24 #define __DATAPATHS_H__
25 
26 #include "dptr.h"
27 #include "global.h"
28 #include <QDir>
29 #include <QString>
30 #include <QStringList>
31 #include <utility>
32 
33 #define gDefaultDataPaths (DataPaths::defaultInstance())
34 
35 class EnginePlugin;
36 
83 class MAIN_EXPORT DataPaths
84 {
85 public:
86  enum MachineType
87  {
88  x86,
89  x64,
90  Preferred
91  };
92 
93  static const QString CHATLOGS_DIR_NAME;
94  static const QString TRANSLATIONS_DIR_NAME;
95  static const QString UPDATE_PACKAGES_DIR_NAME;
96  static const QString UPDATE_PACKAGE_FILENAME_PREFIX;
97 
102  struct DirErrno
103  {
104  static const int CUSTOM_ERROR = -1;
105 
106  QDir directory;
107  int errnoNum;
108  QString errnoString;
109 
110  DirErrno() : errnoNum(0) {}
111 
112  DirErrno(QDir directory, int errnoNum, QString errnoString)
113  : directory(directory), errnoNum(errnoNum), errnoString(std::move(errnoString))
114  {
115  }
116 
117  bool isError() const
118  {
119  return errnoNum != 0;
120  }
121  };
122 
133  static QString programFilesDirectory(MachineType machineType);
134 
146  static QStringList staticDataSearchDirs(const QString &subdir = QString());
147 
148  static void initDefault(bool bPortableModeOn);
155  static DataPaths *defaultInstance();
156 
157  DataPaths(bool bPortableModeOn = false);
158  virtual ~DataPaths();
159 
167  QString cacheLocationPath() const;
168 
174  QStringList canWrite() const;
175 
186  QList<DirErrno> createDirectories();
187 
193  QStringList defaultWadPaths() const;
194 
195  QString demosDirectoryPath() const;
196 
207  QString documentsLocationPath(const QString &subpath = QString()) const;
208 
222  QString localDataLocationPath(const QString &subpath = QString()) const;
223 
236  QString pluginDocumentsLocationPath(const EnginePlugin &plugin) const;
237 
253  QString pluginLocalDataLocationPath(const EnginePlugin &plugin) const;
254 
261  QStringList pluginSearchLocationPaths() const;
262 
272  QString programsDataDirectoryPath() const;
273 
274  bool isPortableModeOn() const;
275 
283  void setWorkingDirectory(const QString &workingDirectory);
284 
303  QString systemAppDataDirectory(QString append = QString()) const;
304 
309  bool validateAppDataDirectory();
310 
318  const QString &workingDirectory() const;
319 
320 protected:
325  static bool validateDir(const QString &path);
326 
330  DirErrno tryCreateDirectory(const QDir &rootDir, const QString &dirToCreate) const;
331 
332 private:
333  DPtr<DataPaths> d;
334 
335  static QString env(const QString &key);
336  static DataPaths *staticDefaultInstance;
337 };
338 
339 #endif