File: mapper_resource.cpp

package info (click to toggle)
openorienteering-mapper 0.6.7-1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 42,556 kB
  • ctags: 8,993
  • sloc: cpp: 84,560; sh: 3,530; ansic: 1,917; java: 145; xml: 135; sed: 43; makefile: 30
file content (206 lines) | stat: -rw-r--r-- 5,951 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
/*
 *    Copyright 2012-2015 Kai Pastor
 *
 *    This file is part of OpenOrienteering.
 *
 *    OpenOrienteering is free software: you can redistribute it and/or modify
 *    it under the terms of the GNU General Public License as published by
 *    the Free Software Foundation, either version 3 of the License, or
 *    (at your option) any later version.
 *
 *    OpenOrienteering is distributed in the hope that it will be useful,
 *    but WITHOUT ANY WARRANTY; without even the implied warranty of
 *    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *    GNU General Public License for more details.
 *
 *    You should have received a copy of the GNU General Public License
 *    along with OpenOrienteering.  If not, see <http://www.gnu.org/licenses/>.
 */


#include "mapper_resource.h"

#include <mapper_config.h>

#include <QCoreApplication>
#include <QDir>
#include <QFile>
#include <QStringList>



/**
 * Private MapperResource utilities
 */
namespace MapperResource
{
	/** 
	 * Get a list of paths where to find a particular executable program.
	 * Returns an empty list if no valid path exists
	 * or if the resource type does not identify a program.
	 */
	QStringList getProgramLocations(MapperResource::RESOURCE_TYPE resource_type);
	
	/**
	 * Add a path to a string list only if that path exists.
	 */
	inline void addIfExists(QStringList& list, QString path)
	{
		if (QFile::exists(path))
			list << path;
	}
}


QStringList MapperResource::getLocations(MapperResource::RESOURCE_TYPE resource_type)
{
	QStringList locations;
	QString resource_path;
	QDir app_dir(QCoreApplication::applicationDirPath());
	
	switch (resource_type)
	{
		case ASSISTANT:
			return MapperResource::getProgramLocations(resource_type);
			
		case EXAMPLE:
			resource_path = QString::fromLatin1("/examples");
			break;
			
		case GDAL_DATA:
#if defined(Mapper_BUILD_GDAL) || defined(Q_OS_WIN)
			resource_path = QString::fromLatin1("/gdal");
			break;
#else
			// Don't fiddle with gdal resource path.
			return locations;
#endif
		
		case MANUAL:
			// TODO: Support localized manual
			resource_path = QString::fromLatin1("/doc/manual");
			break;
			
		case PROJ_DATA:
#if defined(Mapper_BUILD_PROJ) || defined(Q_OS_WIN)
			resource_path = QString::fromLatin1("/proj");
			break;
#else
			// Don't fiddle with proj resource path.
			return locations;
#endif
			
		case SYMBOLSET:
			// TODO: Translate directory name "my symbol sets"?
			//       Possible Windows solution: desktop.ini
			addIfExists(locations, QDir::homePath() + QLatin1String("/my symbol sets"));
			resource_path = QString::fromLatin1("/symbol sets");
			break;
			
		case TEST_DATA:
			addIfExists(locations, app_dir.absoluteFilePath(QString::fromLatin1("data")));
			resource_path = QString::fromLatin1("/test/data");
			break;
	
		case TRANSLATION:
#if defined(Mapper_TRANSLATIONS_EMBEDDED)
			// Always load embedded translations first if enabled
			addIfExists(locations, ":/translations");
#endif
			resource_path = QString::fromLatin1("/translations");
			break;
			
		default:
			return locations;
	}
	
#if defined(MAPPER_DEVELOPMENT_BUILD) && defined(MAPPER_DEVELOPMENT_RES_DIR)
	// Use the directory where Mapper is built during development, 
	// even for the unit tests located in other directories.
	QString build_dir = QLatin1String(MAPPER_DEVELOPMENT_RES_DIR) + resource_path;
	addIfExists(locations, build_dir);
#endif
	
#if defined(MAPPER_PACKAGE_NAME)
	// Linux: resources in MAPPER_DATA_DESTINATION
	QString linux_dir(app_dir.absoluteFilePath(QString::fromUtf8(MAPPER_DATA_DESTINATION) + resource_path));
	addIfExists(locations, linux_dir);
#endif
	
#if defined(Q_OS_MAC)
	// Mac OS X: load resources from the Resources directory of the bundle
	QString osx_dir(app_dir.absoluteFilePath(QLatin1String("../Resources") + resource_path));
	addIfExists(locations, osx_dir);
#elif defined(Q_OS_WIN)
	// Windows: load resources from the application directory
	QString win_dir(app_dir.absolutePath() + resource_path);
	addIfExists(locations, win_dir);
#elif defined(Q_OS_ANDROID)
	// Android: load resources from the application directory
	QString assets_dir(QString::fromLatin1("assets:") + resource_path);
	addIfExists(locations, assets_dir);
#endif
	
	// General default path: Qt resource system
	addIfExists(locations, QLatin1Char(':') + resource_path);
	
	return locations;
}


QStringList MapperResource::getProgramLocations(MapperResource::RESOURCE_TYPE resource_type)
{
	QStringList locations;
	QString program_name;
	
	switch (resource_type)
	{
		case ASSISTANT:
#if defined(Q_OS_WIN)
			program_name = QString::fromLatin1("assistant.exe");
#elif defined(Q_OS_MAC)
			program_name = QString::fromLatin1("Assistant");
#else
			program_name = QString::fromLatin1("assistant");
#endif
			break;
		default:
			return locations;
	}
	
#if defined(MAPPER_DEVELOPMENT_BUILD) and defined(QT_QTASSISTANT_EXECUTABLE)
	addIfExists(locations, QString::fromUtf8(QT_QTASSISTANT_EXECUTABLE));
#endif
	
	QDir app_dir(QCoreApplication::applicationDirPath());
	
#if defined(Mapper_PACKAGE_ASSISTANT) and defined(MAPPER_PACKAGE_NAME)
	// Linux: extra binaries in MAPPER_LIBRARY_DESTINATION/bin
	addIfExists(locations, app_dir.absoluteFilePath(QString::fromUtf8(MAPPER_LIBRARY_DESTINATION) + QLatin1String("/bin/") + program_name));

#endif
	
	// Find the program which is in the same directory as Mapper
	addIfExists(locations, app_dir.absoluteFilePath(program_name));
	
	// General: let system use its search path to find the program
	locations << program_name;
	
	return locations;
}


QString MapperResource::locate(MapperResource::RESOURCE_TYPE resource_type, const QString& name)
{
	QStringList locations = getLocations(resource_type);
	if (locations.isEmpty())
		return QString();
	
	if (name.isEmpty())
		return locations.first();
	
	if (!QDir(locations.first()).exists(name))
		return QString();
	
	return QDir(locations.first()).absoluteFilePath(name);
}