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