LeechCraft  0.6.70-9312-g4cc613a2df
Modular cross-platform feature rich live environment.
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Properties Friends Macros Groups Pages
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 <boost/filesystem.hpp>
33 #include <QFile>
34 #include <QTemporaryFile>
35 #if defined (Q_OS_WIN32) || defined (Q_OS_MAC)
36 #include <QApplication>
37 #endif
38 #include <QtDebug>
39 #include <QDir>
40 #include <QUrl>
41 
42 #if QT_VERSION < 0x050000
43 #include <QDesktopServices>
44 #else
45 #include <QStandardPaths>
46 #endif
47 
48 #include <util/util.h>
49 
50 namespace LeechCraft
51 {
52 namespace Util
53 {
54  QStringList GetPathCandidates (SysPath path, QString suffix)
55  {
56  if (!suffix.isEmpty () && suffix.at (suffix.size () - 1) != '/')
57  suffix += '/';
58 
59  QStringList candidates;
60  switch (path)
61  {
62  case SysPath::QML:
63 #if QT_VERSION < 0x050000
64  return GetPathCandidates (SysPath::Share, "qml/" + suffix);
65 #else
66  return GetPathCandidates (SysPath::Share, "qml5/" + suffix);
67 #endif
68  case SysPath::Share:
69 #ifdef Q_OS_WIN32
70  candidates << QApplication::applicationDirPath () + "/share/" + suffix;
71 #elif defined (Q_OS_MAC) && !defined (USE_UNIX_LAYOUT)
72  candidates << QApplication::applicationDirPath () + "/../Resources/share/" + suffix;
73 #else
74  #ifdef INSTALL_PREFIX
75  candidates << INSTALL_PREFIX "/share/leechcraft/" + suffix;
76  #endif
77  candidates << "/usr/local/share/leechcraft/" + suffix
78  << "/usr/share/leechcraft/" + suffix;
79 #endif
80  return candidates;
81  }
82 
83  qWarning () << Q_FUNC_INFO
84  << "unknown system path"
85  << static_cast<int> (path);
86  return QStringList ();
87  }
88 
89  QString GetSysPath (SysPath path, const QString& suffix, const QString& filename)
90  {
91  for (const QString& cand : GetPathCandidates (path, suffix))
92  if (QFile::exists (cand + filename))
93  return cand + filename;
94 
95  qWarning () << Q_FUNC_INFO
96  << "unable to find"
97  << suffix
98  << filename;
99  return QString ();
100  }
101 
102  QUrl GetSysPathUrl (SysPath path, const QString& subfolder, const QString& filename)
103  {
104  return QUrl::fromLocalFile (GetSysPath (path, subfolder, filename));
105  }
106 
107  QStringList GetSystemPaths ()
108  {
109  return QString (qgetenv ("PATH")).split (":", QString::SkipEmptyParts);
110  }
111 
112  QString FindInSystemPath (const QString& name, const QStringList& paths,
113  const std::function<bool (QFileInfo)>& filter)
114  {
115  for (const auto& dir : paths)
116  {
117  const QFileInfo fi (dir + '/' + name);
118  if (!fi.exists ())
119  continue;
120 
121  if (filter && !filter (fi))
122  continue;
123 
124  return fi.absoluteFilePath ();
125  }
126 
127  return {};
128  }
129 
130  QDir GetUserDir (UserDir dir, const QString& subpath)
131  {
132  QString path;
133  switch (dir)
134  {
135  case UserDir::Cache:
136 #if QT_VERSION < 0x050000
137  path = QDesktopServices::storageLocation (QDesktopServices::CacheLocation);
138 #else
139  path = QStandardPaths::writableLocation (QStandardPaths::CacheLocation);
140 #endif
141  break;
142  case UserDir::LC:
143  path = QDir::home ().path () + "/.leechcraft/";
144  break;
145  }
146 
147  if (path.isEmpty ())
148  throw std::runtime_error ("cannot get root path");
149 
150  if (!path.endsWith ('/'))
151  path += '/';
152  if (dir == UserDir::Cache)
153 #if QT_VERSION < 0x050000
154  path += "leechcraft/";
155 #else
156  path += "leechcraft5/";
157 #endif
158  path += subpath;
159 
160  if (!QDir {}.exists (path) &&
161  !QDir {}.mkpath (path))
162  throw std::runtime_error ("cannot create path " + path.toStdString ());
163 
164  return { path };
165  }
166 
167  QDir CreateIfNotExists (QString path)
168  {
169  auto home = QDir::home ();
170  path.prepend (".leechcraft/");
171 
172  if (!home.exists (path) &&
173  !home.mkpath (path))
174  throw std::runtime_error (qPrintable (QObject::tr ("Could not create %1")
175  .arg (QDir::toNativeSeparators (home.filePath (path)))));
176 
177  if (home.cd (path))
178  return home;
179  else
180  throw std::runtime_error (qPrintable (QObject::tr ("Could not cd into %1")
181  .arg (QDir::toNativeSeparators (home.filePath (path)))));
182  }
183 
184  QString GetTemporaryName (const QString& pattern)
185  {
186  QTemporaryFile file (QDir::tempPath () + "/" + pattern);
187  file.open ();
188  QString name = file.fileName ();
189  file.close ();
190  file.remove ();
191  return name;
192  }
193 
194  SpaceInfo GetSpaceInfo (const QString& path)
195  {
196  const auto& info = boost::filesystem::space (path.toStdString ());
197  return
198  {
199  info.capacity,
200  info.free,
201  info.available
202  };
203  }
204 }
205 }
QStringList GetPathCandidates(SysPath path, QString suffix)
Returns possible full paths for the path and subfolder.
Definition: paths.cpp:54
QDir CreateIfNotExists(QString path)
Creates a path if it doesn't exist.
Definition: paths.cpp:167
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:89
QStringList GetSystemPaths()
Returns the components of the system PATH variable.
Definition: paths.cpp:107
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:102
Directory with shared data files.
Contains information about a partition's disk space.
Definition: paths.h:208
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:112
QString GetTemporaryName(const QString &pattern)
Returns a temporary filename.
Definition: paths.cpp:184
QDir GetUserDir(UserDir dir, const QString &subpath)
Definition: paths.cpp:130
Root path for QML files.
Root LeechCraft directory (something like ~/.leechcraft).
Cache for volatile data.
SpaceInfo GetSpaceInfo(const QString &path)
Returns the disk space info of the partition containing path.
Definition: paths.cpp:194
UserDir
Describes various user-specific paths.
Definition: paths.h:170