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