LeechCraft  0.6.70-6645-gcd10d7e
Modular cross-platform feature rich live environment.
paths.cpp
Go to the documentation of this file.
1 /**********************************************************************
2  * LeechCraft - modular cross-platform feature rich internet client.
3  * Copyright (C) 2006-2014 Georg Rudoy
4  *
5  * Boost Software License - Version 1.0 - August 17th, 2003
6  *
7  * Permission is hereby granted, free of charge, to any person or organization
8  * obtaining a copy of the software and accompanying documentation covered by
9  * this license (the "Software") to use, reproduce, display, distribute,
10  * execute, and transmit the Software, and to prepare derivative works of the
11  * Software, and to permit third-parties to whom the Software is furnished to
12  * do so, all subject to the following:
13  *
14  * The copyright notices in the Software and this entire statement, including
15  * the above license grant, this restriction and the following disclaimer,
16  * must be included in all copies of the Software, in whole or in part, and
17  * all derivative works of the Software, unless such copies or derivative
18  * works are solely in the form of machine-executable object code generated by
19  * a source language processor.
20  *
21  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
22  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
23  * FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
24  * SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
25  * FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
26  * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
27  * DEALINGS IN THE SOFTWARE.
28  **********************************************************************/
29 
30 #include "paths.h"
31 #include <stdexcept>
32 #include <QFile>
33 #include <QTemporaryFile>
34 #if defined (Q_OS_WIN32) || defined (Q_OS_MAC)
35 #include <QApplication>
36 #endif
37 #include <QtDebug>
38 #include <QDir>
39 #include <QUrl>
40 
41 #if QT_VERSION < 0x050000
42 #include <QDesktopServices>
43 #else
44 #include <QStandardPaths>
45 #endif
46 
47 #include <util/util.h>
48 
49 namespace LeechCraft
50 {
51 namespace Util
52 {
53  QStringList GetPathCandidates (SysPath path, QString suffix)
54  {
55  if (!suffix.isEmpty () && suffix.at (suffix.size () - 1) != '/')
56  suffix += '/';
57 
58  QStringList candidates;
59  switch (path)
60  {
61  case SysPath::QML:
62 #if QT_VERSION < 0x050000
63  return GetPathCandidates (SysPath::Share, "qml/" + suffix);
64 #else
65  return GetPathCandidates (SysPath::Share, "qml5/" + suffix);
66 #endif
67  case SysPath::Share:
68 #ifdef Q_OS_WIN32
69  candidates << QApplication::applicationDirPath () + "/share/" + suffix;
70 #elif defined (Q_OS_MAC) && !defined (USE_UNIX_LAYOUT)
71  candidates << QApplication::applicationDirPath () + "/../Resources/share/" + suffix;
72 #else
73  #ifdef INSTALL_PREFIX
74  candidates << INSTALL_PREFIX "/share/leechcraft/" + suffix;
75  #endif
76  candidates << "/usr/local/share/leechcraft/" + suffix
77  << "/usr/share/leechcraft/" + suffix;
78 #endif
79  return candidates;
80  }
81 
82  qWarning () << Q_FUNC_INFO
83  << "unknown system path"
84  << static_cast<int> (path);
85  return QStringList ();
86  }
87 
88  QString GetSysPath (SysPath path, const QString& suffix, const QString& filename)
89  {
90  for (const QString& cand : GetPathCandidates (path, suffix))
91  if (QFile::exists (cand + filename))
92  return cand + filename;
93 
94  qWarning () << Q_FUNC_INFO
95  << "unable to find"
96  << suffix
97  << filename;
98  return QString ();
99  }
100 
101  QUrl GetSysPathUrl (SysPath path, const QString& subfolder, const QString& filename)
102  {
103  return QUrl::fromLocalFile (GetSysPath (path, subfolder, filename));
104  }
105 
106  QStringList GetSystemPaths ()
107  {
108  return QString (qgetenv ("PATH")).split (":", QString::SkipEmptyParts);
109  }
110 
111  QString FindInSystemPath (const QString& name, const QStringList& paths,
112  const std::function<bool (QFileInfo)>& filter)
113  {
114  for (const auto& dir : paths)
115  {
116  const QFileInfo fi (dir + '/' + name);
117  if (!fi.exists ())
118  continue;
119 
120  if (filter && !filter (fi))
121  continue;
122 
123  return fi.absoluteFilePath ();
124  }
125 
126  return {};
127  }
128 
129  QDir GetUserDir (UserDir dir, const QString& subpath)
130  {
131  QString path;
132  switch (dir)
133  {
134  case UserDir::Cache:
135 #if QT_VERSION < 0x050000
136  path = QDesktopServices::storageLocation (QDesktopServices::CacheLocation);
137 #else
138  path = QStandardPaths::writableLocation (QStandardPaths::CacheLocation);
139 #endif
140  break;
141  case UserDir::LC:
142  path = QDir::home ().path () + "/.leechcraft/";
143  break;
144  }
145 
146  if (path.isEmpty ())
147  throw std::runtime_error ("cannot get root path");
148 
149  if (!path.endsWith ('/'))
150  path += '/';
151  if (dir == UserDir::Cache)
152 #if QT_VERSION < 0x050000
153  path += "leechcraft/";
154 #else
155  path += "leechcraft5/";
156 #endif
157  path += subpath;
158 
159  if (!QDir {}.exists (path) &&
160  !QDir {}.mkpath (path))
161  throw std::runtime_error ("cannot create path " + path.toStdString ());
162 
163  return { path };
164  }
165 
166  QDir CreateIfNotExists (QString path)
167  {
168  auto home = QDir::home ();
169  path.prepend (".leechcraft/");
170 
171  if (!home.exists (path) &&
172  !home.mkpath (path))
173  throw std::runtime_error (qPrintable (QObject::tr ("Could not create %1")
174  .arg (QDir::toNativeSeparators (home.filePath (path)))));
175 
176  if (home.cd (path))
177  return home;
178  else
179  throw std::runtime_error (qPrintable (QObject::tr ("Could not cd into %1")
180  .arg (QDir::toNativeSeparators (home.filePath (path)))));
181  }
182 
183  QString GetTemporaryName (const QString& pattern)
184  {
185  QTemporaryFile file (QDir::tempPath () + "/" + pattern);
186  file.open ();
187  QString name = file.fileName ();
188  file.close ();
189  file.remove ();
190  return name;
191  }
192 }
193 }
QStringList GetPathCandidates(SysPath path, QString suffix)
Returns possible full paths for the path and subfolder.
Definition: paths.cpp:53
QDir CreateIfNotExists(QString path)
Creates a path if it doesn&#39;t exist.
Definition: paths.cpp:166
SysPath
Describes various root paths recognized by GetSysPath().
Definition: paths.h:48
QString GetSysPath(SysPath path, const QString &suffix, const QString &filename)
Returns path to the file in the given root path and subfolder.
Definition: paths.cpp:88
QStringList GetSystemPaths()
Returns the components of the system PATH variable.
Definition: paths.cpp:106
QUrl GetSysPathUrl(SysPath path, const QString &subfolder, const QString &filename)
Returns path to the file in the given root path and subfolder.
Definition: paths.cpp:101
Directory with shared data files.
QString FindInSystemPath(const QString &name, const QStringList &paths, const std::function< bool(QFileInfo)> &filter)
Searches for a file in system paths according to a filter.
Definition: paths.cpp:111
QString GetTemporaryName(const QString &pattern)
Returns a temporary filename.
Definition: paths.cpp:183
QDir GetUserDir(UserDir dir, const QString &subpath)
Definition: paths.cpp:129
Root path for QML files.
Root LeechCraft directory (something like ~/.leechcraft).
Cache for volatile data.
UserDir
Describes various user-specific paths.
Definition: paths.h:170