LeechCraft  0.6.70-3565-g2d86529
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 <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)
71  if (QApplication::arguments ().contains ("-nobundle"))
72  candidates << "/usr/local/share/leechcraft/" + suffix;
73  else
74  candidates << QApplication::applicationDirPath () + "/../Resources/share/" + suffix;
75 #else
76  #ifdef INSTALL_PREFIX
77  candidates << INSTALL_PREFIX "/share/leechcraft/" + suffix;
78  #endif
79  candidates << "/usr/local/share/leechcraft/" + suffix
80  << "/usr/share/leechcraft/" + suffix;
81 #endif
82  return candidates;
83  }
84 
85  qWarning () << Q_FUNC_INFO
86  << "unknown system path"
87  << static_cast<int> (path);
88  return QStringList ();
89  }
90 
91  QString GetSysPath (SysPath path, const QString& suffix, const QString& filename)
92  {
93  for (const QString& cand : GetPathCandidates (path, suffix))
94  if (QFile::exists (cand + filename))
95  return cand + filename;
96 
97  qWarning () << Q_FUNC_INFO
98  << "unable to find"
99  << suffix
100  << filename;
101  return QString ();
102  }
103 
104  QUrl GetSysPathUrl (SysPath path, const QString& subfolder, const QString& filename)
105  {
106  return QUrl::fromLocalFile (GetSysPath (path, subfolder, filename));
107  }
108 
109  QStringList GetSystemPaths ()
110  {
111  return QString (qgetenv ("PATH")).split (":", QString::SkipEmptyParts);
112  }
113 
114  QString FindInSystemPath (const QString& name, const QStringList& paths,
115  const std::function<bool (QFileInfo)>& filter)
116  {
117  for (const auto& dir : paths)
118  {
119  const QFileInfo fi (dir + '/' + name);
120  if (!fi.exists ())
121  continue;
122 
123  if (filter && !filter (fi))
124  continue;
125 
126  return fi.absoluteFilePath ();
127  }
128 
129  return {};
130  }
131 
132  QDir GetUserDir (UserDir dir, const QString& subpath)
133  {
134  QString path;
135  switch (dir)
136  {
137  case UserDir::Cache:
138 #if QT_VERSION < 0x050000
139  path = QDesktopServices::storageLocation (QDesktopServices::CacheLocation);
140 #else
141  path = QStandardPaths::writableLocation (QStandardPaths::CacheLocation);
142 #endif
143  break;
144  case UserDir::LC:
145  path = QDir::home ().path () + "/.leechcraft/";
146  break;
147  }
148 
149  if (path.isEmpty ())
150  throw std::runtime_error ("cannot get root path");
151 
152  if (!path.endsWith ('/'))
153  path += '/';
154  if (dir == UserDir::Cache)
155 #if QT_VERSION < 0x050000
156  path += "leechcraft/";
157 #else
158  path += "leechcraft5/";
159 #endif
160  path += subpath;
161 
162  if (!QDir {}.exists (path) &&
163  !QDir {}.mkpath (path))
164  throw std::runtime_error ("cannot create path " + path.toStdString ());
165 
166  return { path };
167  }
168 
169  QDir CreateIfNotExists (QString path)
170  {
171  auto home = QDir::home ();
172  path.prepend (".leechcraft/");
173 
174  if (!home.exists (path) &&
175  !home.mkpath (path))
176  throw std::runtime_error (qPrintable (QObject::tr ("Could not create %1")
177  .arg (QDir::toNativeSeparators (home.filePath (path)))));
178 
179  if (home.cd (path))
180  return home;
181  else
182  throw std::runtime_error (qPrintable (QObject::tr ("Could not cd into %1")
183  .arg (QDir::toNativeSeparators (home.filePath (path)))));
184  }
185 
186  QString GetTemporaryName (const QString& pattern)
187  {
188  QTemporaryFile file (QDir::tempPath () + "/" + pattern);
189  file.open ();
190  QString name = file.fileName ();
191  file.close ();
192  file.remove ();
193  return name;
194  }
195 }
196 }
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't exist.
Definition: paths.cpp:169
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:91
QStringList GetSystemPaths()
Returns the components of the system PATH variable.
Definition: paths.cpp:109
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:104
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:114
QString GetTemporaryName(const QString &pattern)
Returns a temporary filename.
Definition: paths.cpp:186
QDir GetUserDir(UserDir dir, const QString &subpath)
Definition: paths.cpp:132
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