Falkon Develop
Cross-platform Qt-based web browser
datapaths.cpp
Go to the documentation of this file.
1/* ============================================================
2* Falkon - Qt web browser
3* Copyright (C) 2014-2018 David Rosca <nowrep@gmail.com>
4*
5* This program is free software: you can redistribute it and/or modify
6* it under the terms of the GNU General Public License as published by
7* the Free Software Foundation, either version 3 of the License, or
8* (at your option) any later version.
9*
10* This program is distributed in the hope that it will be useful,
11* but WITHOUT ANY WARRANTY; without even the implied warranty of
12* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13* GNU General Public License for more details.
14*
15* You should have received a copy of the GNU General Public License
16* along with this program. If not, see <http://www.gnu.org/licenses/>.
17* ============================================================ */
18#include "datapaths.h"
19#include "qztools.h"
20#include "../config.h"
21#include "mainapplication.h"
22
23#include <QApplication>
24#include <QDir>
25
26#include <QStandardPaths>
27#include <QTemporaryDir>
28
29Q_GLOBAL_STATIC(DataPaths, qz_data_paths)
30
32{
33 init();
34}
35
37= default;
38
39// static
40void DataPaths::setCurrentProfilePath(const QString &profilePath)
41{
42 qz_data_paths()->initCurrentProfile(profilePath);
43}
44
45// static
47{
48 DataPaths* d = qz_data_paths();
49
50 const QString appDir = QCoreApplication::applicationDirPath();
51
52 d->m_paths[AppData] = QStringList{appDir};
53 d->m_paths[Config] = QStringList{appDir + QSL("/config")};
54 d->m_paths[Cache] = QStringList{appDir + QSL("/cache")};
55 d->m_paths[Profiles] = QStringList{appDir + QSL("/config/profiles")};
56
57 d->m_paths[Themes].clear();
58 d->m_paths[Plugins].clear();
59 d->initAssetsIn(appDir);
60
61 // Make sure Temp path exists
62 QDir().mkpath(d->m_paths[Temp].at(0));
63}
64
65// static
67{
68 Q_ASSERT(!qz_data_paths()->m_paths[path].isEmpty());
69
70 return qz_data_paths()->m_paths[path].at(0);
71}
72
73// static
75{
76 Q_ASSERT(!qz_data_paths()->m_paths[type].isEmpty());
77
78 return qz_data_paths()->m_paths[type];
79}
80
81// static
82QString DataPaths::locate(Path type, const QString &file)
83{
84 const QStringList dirs = allPaths(type);
85 for (const QString &dir : dirs) {
86 const QString fullPath = QDir(dir).absoluteFilePath(file);
87 if (QFileInfo::exists(fullPath)) {
88 return fullPath;
89 }
90 }
91 return {};
92}
93
94// static
96{
97 return path(CurrentProfile);
98}
99
100void DataPaths::init()
101{
102 m_paths[AppData].append(QStandardPaths::standardLocations(QStandardPaths::AppDataLocation));
103
104#ifdef FALKON_PLUGIN_PATH
105 m_paths[Plugins].append(QStringLiteral(FALKON_PLUGIN_PATH));
106#endif
107
108 for (const QString &location : std::as_const(m_paths[AppData])) {
109 initAssetsIn(location);
110 }
111
113 m_paths[Config].append(QDir::tempPath() + QSL("/Falkon-test"));
114 } else {
115 m_paths[Config].append(QStandardPaths::writableLocation(QStandardPaths::AppConfigLocation));
116 }
117
118 m_paths[Profiles].append(m_paths[Config].at(0) + QLatin1String("/profiles"));
119 // We also allow to load data from Config path
120 initAssetsIn(m_paths[Config].at(0));
121
122 // If FALKON_PLUGIN_PATH is set, only load plugins from there
123 const QByteArray pluginPath = qgetenv("FALKON_PLUGIN_PATH");
124 if (!pluginPath.isNull()) {
125 m_paths[Plugins] = QStringList{QString::fromLocal8Bit(pluginPath)};
126 }
127
128 m_tmpdir.reset(new QTemporaryDir());
129 m_paths[Temp].append(m_tmpdir->path());
130 if (!m_tmpdir->isValid()) {
131 qWarning() << "Failed to create temporary directory" << m_tmpdir->path();
132 }
133
134 m_paths[Cache].append(QStandardPaths::writableLocation(QStandardPaths::CacheLocation));
135}
136
137void DataPaths::initCurrentProfile(const QString &profilePath)
138{
139 m_paths[CurrentProfile].append(profilePath);
140
141 if (m_paths[Cache].isEmpty())
142 m_paths[Cache].append(m_paths[CurrentProfile].at(0) + QLatin1String("/cache"));
143
144 if (m_paths[Sessions].isEmpty())
145 m_paths[Sessions].append(m_paths[CurrentProfile].at(0) + QLatin1String("/sessions"));
146
147 QDir dir;
148 dir.mkpath(m_paths[Cache].at(0));
149 dir.mkpath(m_paths[Sessions].at(0));
150}
151
152void DataPaths::initAssetsIn(const QString &path)
153{
154 m_paths[Themes].append(path + QLatin1String("/themes"));
155 m_paths[Plugins].append(path + QLatin1String("/plugins"));
156}
static void setCurrentProfilePath(const QString &profilePath)
Definition: datapaths.cpp:40
static void setPortableVersion()
Definition: datapaths.cpp:46
@ Sessions
Definition: datapaths.h:39
@ CurrentProfile
Definition: datapaths.h:36
@ Profiles
Definition: datapaths.h:35
static QString path(Path type)
Definition: datapaths.cpp:66
static QStringList allPaths(Path type)
Definition: datapaths.cpp:74
static QString currentProfilePath()
Definition: datapaths.cpp:95
static QString locate(Path type, const QString &file)
Definition: datapaths.cpp:82
static bool isTestModeEnabled()
#define QSL(x)
Definition: qzcommon.h:40