forked from madtreat/cpd-map
-
Notifications
You must be signed in to change notification settings - Fork 1
/
mapsettings.cpp
243 lines (209 loc) · 6.82 KB
/
mapsettings.cpp
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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*
* File: mapsettings.cpp
* Author: Madison Treat <madison.treat@tamu.edu>
*
* Created on June 12, 2015, 8:21 PM
*/
#include "mapsettings.h"
#include <QCoreApplication>
#include <QFile>
#include <QDir>
#include <QDirIterator>
#include <QTextStream>
#include <QDebug>
MapSettings::MapSettings(QString _filename, QObject* _parent)
: QObject(_parent),
settings(NULL) {
m_userHomeDir = QDir::home().absolutePath();
// Get config file from [application root directory]/config
m_appRootDir = QCoreApplication::applicationDirPath();
QDir appRoot(m_appRootDir);
appRoot.cdUp();
m_configDir = appRoot.absolutePath() + "/config";
// If _filename starts with a "/", assume it is a full path
if (_filename.startsWith("/")) {
m_settingsFile = _filename;
m_configDir = QFileInfo(m_settingsFile).absolutePath();
}
// Otherwise, add it to the default m_configDir
else {
m_settingsFile = m_configDir + "/" + _filename;
}
// Enumerate resources for debugging missing map HTML file
if (DEBUG_MAP) {
qDebug() << "Debugging Map HTML Resources...";
QDirIterator it1(":/html", QDirIterator::Subdirectories);
while (it1.hasNext()) {
qDebug() << it1.next();
}
qDebug() << "Debugging Map Aircraft Resources...";
QDirIterator it2(":/ac", QDirIterator::Subdirectories);
while (it2.hasNext()) {
qDebug() << it2.next();
}
}
if (!QFile::exists(m_settingsFile)) {
qWarning() << "Warning: Map settings file" << m_settingsFile << "does not exist. Exiting.";
exit(1);
}
qDebug() << "Loading settings file:\n " << m_settingsFile;
loadSettingsFile(m_settingsFile);
qDebug() << " Done loading maps settings.";
}
MapSettings::MapSettings(const MapSettings& orig) {
}
MapSettings::~MapSettings() {
}
/*
* Load the settings in file _filename.
*/
void MapSettings::loadSettingsFile(QString _filename) {
// If the settings pointer already exists, delete it to start fresh on a new
// file, since this can potentially be called multiple times in an application.
if (settings) {
qDebug() << "Settings object already exists, re-creating it" << settings;
delete settings;
}
settings = new QSettings(_filename, QSettings::IniFormat);
// Load Map Proxy settings
m_useProxy = settings->value("use_proxy").toBool();
if (m_useProxy) {
settings->beginGroup("proxy");
m_proxyHost = settings->value("proxy_host").toString();
m_proxyPort = settings->value("proxy_port").toInt();
settings->endGroup(); // "proxy"
}
// Load the Map Provider
m_mapProvider = settings->value("map_provider").toString();
// Load the API_KEY
m_apiKey = settings->value("api_key", "failed").toString();
if ( m_mapProvider == "google" && ( m_apiKey == "failed" || m_apiKey.isEmpty() ) ) {
qWarning() << "Warning: No Google Maps API Key provided. Exiting.";
m_apiKeyValid = false;
}
else if ( m_mapProvider == "openlayers" && ( m_apiKey == "failed" || m_apiKey.isEmpty() ) ) {
qWarning() << "Warning: No API Key provided for OpenLayers. Exiting.";
m_apiKeyValid = false;
}
else {
// TODO: test API Key to ensure valid
m_apiKeyValid = true;
}
// Load the starting coordinates
m_lat = settings->value("lat", "0.0").toDouble();
m_lon = settings->value("lon", "0.0").toDouble();
// Load zoom level
m_zoom = settings->value("zoom", "12").toInt();
// Load map type
m_mapType = settings->value("map_type", "ROADMAP").toString();
// Load UI disable
m_mapDisableUI = settings->value("disable_map_ui", false).toBool();
// Load map JS file
m_mapJSPath = settings->value("js_file").toString();
bool validJS = loadMapJS();
if (validJS) {
// Load map HTML file from compiled in resource
if (m_mapProvider == "google") {
m_mapHtmlInPath = ":/html/google-maps.html";
}
else {
m_mapHtmlInPath = ":/html/openlayers.html";
}
// must be called AFTER loadExtraJS() for valid JS data to be loaded
m_mapValid = loadMapHtml();
}
else {
m_mapValid = false;
}
// Load Map Orientation
QString os = settings->value("map_orientation").toString();
if (os == "north_up") {
m_mapOrientation = NORTH_UP;
}
else if (os == "track_up") {
m_mapOrientation = TRACK_UP;
}
else {
qWarning() << "Warning: invalid map orientation, defaulting to NORTH_UP";
m_mapOrientation = NORTH_UP;
}
}
/*
* Create a new settings file with the name _filename.
*/
void MapSettings::saveSettingsFile(QString _filename) {
// TODO: make this work
settings->sync();
}
/*
* Returns valid JS: true or false
*/
bool MapSettings::loadMapJS() {
// Load the settings
if (m_mapJSPath == "") {
return false;
}
qDebug() << " Map JS File:";
// TODO: ensure properly works with "/path" and "~/path" and "path"
// read example settings.ini file
if (!m_mapJSPath.startsWith("/") && !m_mapJSPath.startsWith("~")) {
m_mapJSPath.prepend(m_configDir + "/");
}
qDebug() << " " << m_mapJSPath;
if (QFile::exists(m_mapJSPath)) {
QFile file(m_mapJSPath);
if ( !file.open(QFile::ReadOnly | QFile::Text) ) {
qWarning() << "Warning: Unable to open Map JS file, disabling maps.";
return false;
}
else {
QTextStream in(&file);
m_mapJSData = in.readAll();
return true;
}
}
else {
qWarning() << "Warning: No Map JS file provided, disabling maps.";
return false;
}
}
/*
* Returns valid HTML: true or false
*/
bool MapSettings::loadMapHtml() {
// Load the settings
if (m_mapJSData == "") {
qWarning() << "Warning: Map JS file was empty or invalid, disabling maps.";
return false;
}
QFile file(m_mapHtmlInPath);
if ( !file.open(QFile::ReadOnly | QFile::Text) ) {
qWarning() << "Warning: Unable to open Map HTML file, disabling maps.";
return false;
}
else {
QTextStream in(&file);
QString inText = in.readAll();
m_mapHtmlPath = m_configDir + "/map-gen.html";
QFile htmlFile(m_mapHtmlPath);
if (!htmlFile.open(QFile::WriteOnly | QFile::Text)) {
qWarning() << "Warning: Could not open Generated Map HTML file for writing";
return false;
}
else {
// Replace all the tags with their actual values
// __API_KEY__ and __JS_CODE__ are directly in the HTML file,
// The other tags are in the mapJSData string.
m_mapHtmlData = inText.replace("__JS_CODE__", m_mapJSData);
m_mapHtmlData = inText.replace("__API_KEY__", m_apiKey);
m_mapHtmlData = inText.replace("__LAT__", QString::number(m_lat));
m_mapHtmlData = inText.replace("__LON__", QString::number(m_lon));
m_mapHtmlData = inText.replace("__ZOOM__", QString::number(m_zoom));
m_mapHtmlData = inText.replace("__MAP_TYPE__", m_mapType);
m_mapHtmlData = inText.replace("__DISABLE_MAP_UI__", m_mapDisableUI ? "true" : "false");
QTextStream out(&htmlFile);
out << m_mapHtmlData;
return true;
}
}
}