LP-311 Remove basic/advanced stabilization tab auto-switch (autotune/txpid lock issues)
[librepilot.git] / ground / gcs / src / libs / utils / pathutils.cpp
blob212045401146173cae1672fb1323f1f9b8b93383
1 /**
2 ******************************************************************************
4 * @file pathutils.cpp
5 * @author The OpenPilot Team, http://www.openpilot.org Copyright (C) 2010.
6 * @brief Utilities to find the location of openpilot GCS files:
7 * - Plugins Share directory path
9 * @see The GNU Public License (GPL) Version 3
11 *****************************************************************************/
13 * This program is free software; you can redistribute it and/or modify
14 * it under the terms of the GNU General Public License as published by
15 * the Free Software Foundation; either version 3 of the License, or
16 * (at your option) any later version.
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
20 * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
21 * for more details.
23 * You should have received a copy of the GNU General Public License along
24 * with this program; if not, write to the Free Software Foundation, Inc.,
25 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
28 #include "pathutils.h"
30 #include <QApplication>
31 #include <QDir>
32 #include <QStandardPaths>
34 namespace Utils {
35 /**
36 Returns the base path of the share directory.
38 Path is in Qt/Unix conventions, separated by "/".
40 QString GetDataPath()
42 QString dataPath = QApplication::applicationDirPath();
44 dataPath += QLatin1Char('/');
45 dataPath += QLatin1String(DATA_REL_PATH);
47 return QDir::cleanPath(dataPath) + QLatin1Char('/');
50 /**
51 Cuts the standard data path from the 'path' argument. If path does not start
52 with the standard data path, then do nothing.
54 Always returns a path converted to "/".
56 QString RemoveDataPath(QString path)
58 // Depending on the platform, we might get either "/" or "\"
59 // so we need to go to the standard ("/")
60 QString goodPath = QDir::fromNativeSeparators(path);
62 if (goodPath.startsWith(GetDataPath())) {
63 int i = goodPath.length() - GetDataPath().length();
64 return QString("%%DATAPATH%%") + goodPath.right(i);
65 } else {
66 return goodPath;
70 /**
71 Inserts the data path (only if the path starts with %%DATAPATH%%)
73 Returns a "/" or "\" separated path depending on platform conventions.
75 QString InsertDataPath(QString path)
77 if (path.startsWith(QString("%%DATAPATH%%"))) {
78 QString newPath = GetDataPath();
79 newPath += path.right(path.length() - 12);
80 return QDir::toNativeSeparators(newPath);
82 return QDir::toNativeSeparators(path);
85 /**
86 Gets a standard user-writable location for the system
88 QString GetStoragePath()
90 return QStandardPaths::writableLocation(QStandardPaths::CacheLocation) + QLatin1Char('/');
93 /**
94 Removes the standard storage path and replace with a tag
96 QString RemoveStoragePath(QString path)
98 // Depending on the platform, we might get either "/" or "\"
99 // so we need to go to the standard ("/")
100 QString goodPath = QDir::fromNativeSeparators(path);
102 if (goodPath.startsWith(GetStoragePath())) {
103 int i = goodPath.length() - GetStoragePath().length();
104 return QString("%%STOREPATH%%") + goodPath.right(i);
105 } else {
106 return goodPath;
111 Inserts the standard storage path is there is a storage path tag
113 QString InsertStoragePath(QString path)
115 if (path.startsWith(QString("%%STOREPATH%%"))) {
116 QString newPath = GetStoragePath();
117 newPath += path.right(path.length() - 13);
118 return QDir::toNativeSeparators(newPath);
120 return QDir::toNativeSeparators(path);
124 Returns the base path of the library directory.
126 Path is in Qt/Unix conventions, separated by "/".
128 QString GetLibraryPath()
130 QString libPath = QApplication::applicationDirPath();
132 libPath += QLatin1Char('/');
133 libPath += QLatin1String(LIB_REL_PATH);
135 return QDir::cleanPath(libPath) + QLatin1Char('/');
138 QStringList GetPluginPaths()
140 QStringList rc;
142 QString pluginPath = QApplication::applicationDirPath();
144 pluginPath += QLatin1Char('/');
145 pluginPath += QLatin1String(PLUGIN_REL_PATH);
147 rc.push_back(QDir::cleanPath(pluginPath));
149 return rc;