1 /*
2  * %kadu copyright begin%
3  * Copyright 2014 Rafał Przemysław Malinowski (rafal.przemyslaw.malinowski@gmail.com)
4  * %kadu copyright end%
5  *
6  * This program is free software; you can redistribute it and/or
7  * modify it under the terms of the GNU General Public License as
8  * published by the Free Software Foundation; either version 2 of
9  * the License, or (at your option) any later version.
10  *
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.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #pragma once
21 
22 #include <QtCore/QObject>
23 
24 #include "exports.h"
25 
26 /**
27  * @class PathsProvider
28  * @short Singleton which holds information about paths used by Kadu.
29  *
30  * This singleton class holds information about paths used by Kadu. It constructs and caches them
31  * upon class instance creation, and path to the current user profile is created if it does not
32  * exist yet. Details are described in the individual getter methods.
33  */
34 class KADUAPI PathsProvider : public QObject
35 {
36 	Q_OBJECT
37 	Q_DISABLE_COPY(PathsProvider)
38 
39 #if defined(Q_OS_UNIX)
40 	QString DesktopFilePath;
41 #endif
42 	QString ProfilePath;
43 	QString PluginsLibPath;
44 	QString DataPath;
45 
46 	void initBasicPaths();
47 	void initProfilePath(const QString &customProfileDir);
48 
49 public:
50 	explicit PathsProvider(const QString &customProfileDir, QObject *parent = nullptr);
51 	virtual ~PathsProvider();
52 
53 	/**
54 	 * @short Returns roaming persistent data storage path on Windows and QDir::homePath() on other platforms.
55 	 * @return absolute path to roaming persistent data storage path on Windows and QDir::homePath() on other platforms
56 	 *
57 	 * This functions returns roaming persistent data storage path (%AppData%) on Windows and QDir::homePath() on other platforms.
58 	 */
59 	static QString homePath();
60 
61 	/**
62 	 * @short Returns fixed path ready to use with WebKit.
63 	 * @param path path to be fixed
64 	 * @return fixed path @p path ready to use with WebKit
65 	 * @todo Check whether this is really needed. Maybe all paths should start with "file://" and it could work everywhere?
66 	 *
67 	 * This function tries to fix path passed in @p path argument to be ready
68 	 * to use with WebKit. Basically on Windows it tries to remove any "file://" and "file:///"
69 	 * prefixes while on other platforms it ensures that the path actually begins with "file:///".
70 	 */
71 	static QString webKitPath(const QString &path);
72 
73 #if defined(Q_OS_UNIX)
74 	/**
75 	 * @short Returns absolute path to the .desktop file owned by Kadu.
76 	 * @return absolute path to the .desktop file owned by Kadu
77 	 *
78 	 * This function returns absolute path to the .desktop file owned by Kadu.
79 	 *
80 	 * Path construction at init:
81 	 * First desktop file path relative to the binary is taken from the configuration
82 	 * written at compilation time, and then absolute canonical path is constructed from it.
83 	 *
84 	 * Default desktop file path is $CMAKE_INSTALL_FULL_DATADIR/applications/kadu.desktop.
85 	 * It can be overwritten by KADU_DESKTOP_FILE_DIR and KADU_DESKTOP_FILE_NAME CMake
86 	 * arguments.
87 	 */
desktopFilePath()88 	const QString & desktopFilePath() const { return DesktopFilePath; }
89 #endif
90 
91 	/**
92 	 * @short Returns absolute path to the data path used by Kadu.
93 	 * @return absolute path to the data path used by Kadu, ended with a trailing slash
94 	 *
95 	 * This function returns absolute path to the data path used by Kadu, ended with a trailing slash.
96 	 *
97 	 * Path construction at init:
98 	 * First Kadu data path relative to the binary is taken from the configuration
99 	 * written at compilation time, and then absolute canonical path is constructed from it.
100 	 *
101 	 * Default data path is $CMAKE_INSTALL_FULL_DATADIR/kadu on X11 and
102 	 * $CMAKE_INSTALL_PREFIX on Windows. It can be overwritten by
103 	 * KADU_INSTALL_DATA_DIR CMake argument.
104 	 */
dataPath()105 	const QString & dataPath() const { return DataPath; }
106 
107 	/**
108 	 * @short Returns absolute path to the library path used by Kadu plugins.
109 	 * @return absolute path to the library path used by Kadu plugins, ended with a trailing slash
110 	 *
111 	 * This function returns absolute path to the library path used by Kadu plugins, ended with a trailing slash.
112 	 *
113 	 * Path construction at init:
114 	 * First plugins library path relative to the binary is taken from the configuration
115 	 * written at compilation time, and then absolute canonical path is constructed from it.
116 	 *
117 	 * Default library path for plugins is $CMAKE_INSTALL_FULL_LIBDIR/kadu/plugins
118 	 * on X11 and $CMAKE_INSTALL_PREFIX/plugins on Windows. It can be overwritten by
119 	 * KADU_INSTALL_PLUGINS_LIB_DIR CMake argument.
120 	 */
pluginsLibPath()121 	const QString & pluginsLibPath() const { return PluginsLibPath; }
122 
123 	/**
124 	 * @short Creates and returns absolute path to the current user profile.
125 	 * @return absolute path to the current user profile, ended with a trailing slash
126 	 *
127 	 * This function returns absolute path to the current user profile, ended with a trailing slash.
128 	 *
129 	 * Path construction at init:
130 	 * Absolute path to the current user profile is constructed at init. By default it is
131 	 * $HOME/.kadu on X11, $HOME/Library/Kadu on Mac OS X, and %AppData%/Kadu on Windows.
132 	 * If this path does not exist yet, it is craeted and 0700 (or equivalent on Windwow)
133 	 * permissions are set.
134 	 *
135 	 * The default path to the current user profile may overwritten by existence
136 	 * of file named "portable" in directory returned by the dataPath() method. Then
137 	 * current user profile is located in the same location as dataPath() + QStringLiteral("config")
138 	 * would give. It may be also overwritten by CONFIG_DIR environment variable
139 	 * and --config-dir command-line option (the latter takes precedence). We will
140 	 * call it CONFIG_DIR in this description, not matter which way it was set.
141 	 * If CONFIG_DIR begins with a "./" (".\" is also supported on Windows), it is
142 	 * treated as relative to the application's current directory. Else, if it is
143 	 * a absolute path, it is treated literally. Else it is treated as relative to
144 	 * the path returned by the homePath() method or, if a file named "portable"
145 	 * in directory returned by the dataPath() method exists, it is treated as relative to
146 	 * the path returned by the dataPath() method.
147 	 *
148 	 * If CONFIG_DIR is used and a middle directory named "kadu" on X11 or "Kadu" on Windows
149 	 * and Mac OS X exists, this directory is used as the current user profile for compatibility
150 	 * with Kadu 0.6.5 and older.
151 	 */
profilePath()152 	const QString & profilePath() const { return ProfilePath; }
153 
154 };
155