WebAPI: Add a way to download .torrent file using search plugin
[qBittorrent.git] / src / base / profile.cpp
blob912ac2e22832174fcf7a6cdf1b7373883044270f
1 /*
2 * Bittorrent Client using Qt and libtorrent.
3 * Copyright (C) 2016 Eugene Shalygin <eugene.shalygin@gmail.com>
4 * Copyright (C) 2012 Christophe Dumez
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License
8 * as published by the Free Software Foundation; either version 2
9 * of the License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20 * In addition, as a special exception, the copyright holders give permission to
21 * link this program with the OpenSSL project's "OpenSSL" library (or with
22 * modified versions of it that use the same license as the "OpenSSL" library),
23 * and distribute the linked executables. You must obey the GNU General Public
24 * License in all respects for all of the code used other than "OpenSSL". If you
25 * modify file(s), you may extend this exception to your version of the file(s),
26 * but you are not obligated to do so. If you do not wish to do so, delete this
27 * exception statement from your version.
30 #include "profile.h"
32 #include "base/path.h"
33 #include "base/utils/fs.h"
34 #include "profile_p.h"
36 Profile *Profile::m_instance = nullptr;
38 Profile::Profile(const Path &rootProfilePath, const QString &configurationName, const bool convertPathsToProfileRelative)
40 if (rootProfilePath.isEmpty())
41 m_profileImpl = std::make_unique<Private::DefaultProfile>(configurationName);
42 else
43 m_profileImpl = std::make_unique<Private::CustomProfile>(rootProfilePath, configurationName);
45 ensureDirectoryExists(SpecialFolder::Cache);
46 ensureDirectoryExists(SpecialFolder::Config);
47 ensureDirectoryExists(SpecialFolder::Data);
49 if (convertPathsToProfileRelative)
50 m_pathConverterImpl = std::make_unique<Private::Converter>(m_profileImpl->basePath());
51 else
52 m_pathConverterImpl = std::make_unique<Private::NoConvertConverter>();
55 void Profile::initInstance(const Path &rootProfilePath, const QString &configurationName,
56 const bool convertPathsToProfileRelative)
58 if (m_instance)
59 return;
60 m_instance = new Profile(rootProfilePath, configurationName, convertPathsToProfileRelative);
63 void Profile::freeInstance()
65 delete m_instance;
66 m_instance = nullptr;
69 const Profile *Profile::instance()
71 return m_instance;
74 Path Profile::location(const SpecialFolder folder) const
76 switch (folder)
78 case SpecialFolder::Cache:
79 return m_profileImpl->cacheLocation();
81 case SpecialFolder::Config:
82 return m_profileImpl->configLocation();
84 case SpecialFolder::Data:
85 return m_profileImpl->dataLocation();
87 case SpecialFolder::Downloads:
88 return m_profileImpl->downloadLocation();
92 Q_ASSERT_X(false, Q_FUNC_INFO, "Unknown SpecialFolder value.");
93 return {};
96 Path Profile::rootPath() const
98 return m_profileImpl->rootPath();
101 QString Profile::configurationName() const
103 return m_profileImpl->configurationName();
106 QString Profile::profileName() const
108 return m_profileImpl->profileName();
111 std::unique_ptr<QSettings> Profile::applicationSettings(const QString &name) const
113 return m_profileImpl->applicationSettings(name);
116 void Profile::ensureDirectoryExists(const SpecialFolder folder) const
118 const Path locationPath = location(folder);
119 if (!locationPath.isEmpty() && !Utils::Fs::mkpath(locationPath))
120 qFatal("Could not create required directory '%s'", qUtf8Printable(locationPath.toString()));
123 Path Profile::toPortablePath(const Path &absolutePath) const
125 return m_pathConverterImpl->toPortablePath(absolutePath);
128 Path Profile::fromPortablePath(const Path &portablePath) const
130 return m_pathConverterImpl->fromPortablePath(portablePath);
133 Path specialFolderLocation(const SpecialFolder folder)
135 return Profile::instance()->location(folder);