-
Notifications
You must be signed in to change notification settings - Fork 4
/
servermanager.cpp
190 lines (168 loc) · 4.14 KB
/
servermanager.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
/*
Copyright 2010 Jean Fairlie jmfairlie@gmail.com
This file is part of CacaMap
CacaMap 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.
This program 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.
*/
#include "servermanager.h"
#include <iostream>
using namespace std;
/**
* loads info from xml file
* @return true if succesful false otherwise
*/
bool servermanager::loadConfigFile(QString xmlfile)
{
QDomDocument doc("mydocument");
QFile file(xmlfile);
if (!file.open(QIODevice::ReadOnly))
{
std::cout<<"couldn't open file"<<std::endl;
return false;
}
if (!doc.setContent(&file))
{
file.close();
cout<<"couldn't set content"<<endl;
return false;
}
QDomNodeList servers = doc.elementsByTagName("server");
if(!servers.length())
{
cout<<"no servers defined in xml file"<<endl;
return false;
}
for (quint32 i=0; i< servers.length(); i++)
{
QDomNode server = servers.item(i);
QDomNode namenode = server.namedItem("name");
if (namenode.isNull())
{
cout<<"server has no name tag in xml file"<<endl;
return 0;
}
QDomCharacterData nametext = namenode.firstChild().toCharacterData();
if (nametext.isNull())
{
cout<<"server name is empty in xml"<<endl;
return 0;
}
serverNames.append(nametext.data());
QDomNode urlnode = server.namedItem("url");
QDomCDATASection urltext = urlnode.firstChild().toCDATASection();
if (urltext.isNull())
{
cout<<"url format is empty in xml"<<endl;
return 0;
}
QDomNode foldernode = server.namedItem("folder");
QDomCharacterData foldertext = foldernode.firstChild().toCharacterData();
if (foldertext.isNull())
{
cout<<"folder format is empty in xml"<<endl;
return 0;
}
QDomNode filepathnode = server.namedItem("filepath");
QDomCDATASection filepathtext = filepathnode.firstChild().toCDATASection();
if (filepathtext.isNull())
{
cout<<"filepath is empty in xml"<<endl;
return 0;
}
QDomNode tilenode = server.namedItem("tile");
QDomCDATASection tiletext = tilenode.firstChild().toCDATASection();
if (tiletext.isNull())
{
cout<<"tile format is empty in xml"<<endl;
return 0;
}
tileserver serveritem;
serveritem.name = nametext.data();
serveritem.url = urltext.data();
serveritem.folder = foldertext.data();
serveritem.path = filepathtext.data();
serveritem.tile = tiletext.data();
serverlist.append(serveritem);
}
selectedServer = 0;
file.close();
return true;
}
/**
* Get URL of a specific %tile
* @param zoom zoom level
* @return string containing the url where the %tile image can be found.
*/
QString servermanager::getTileUrl(int zoom, quint32 x, quint32 y)
{
QString sz,sx,sy;
sz.setNum(zoom);
sx.setNum(x);
sy.setNum(y);
QString urltmpl = serverlist.at(selectedServer).url;
urltmpl.replace(QString("%z"),sz);
urltmpl.replace(QString("%x"),sx);
urltmpl.replace(QString("%y"),sy);
return urltmpl;
}
/**
* @return name of the cache folder for the given tile server
*/
QString servermanager::tileCacheFolder()
{
return serverlist.at(selectedServer).folder;
}
/**
* @return tile file name
*/
QString servermanager::fileName(quint32 y)
{
QString filetmpl = serverlist.at(selectedServer).tile;
QString sy;
sy.setNum(y);
filetmpl.replace("%y",sy);
return filetmpl;
}
/**
* @return tile file path
*/
QString servermanager::filePath(int zoom, quint32 x)
{
QString filetmpl = serverlist.at(selectedServer).path;
QString sz, sx;
sz.setNum(zoom);
sx.setNum(x);
filetmpl.replace("%z",sz);
filetmpl.replace("%x",sx);
return filetmpl;
}
/**
* @return server name
*/
QString servermanager::serverName()
{
return serverlist.at(selectedServer).name;
}
/**
* selects server at index
*/
void servermanager::selectServer(int index)
{
if (index >=0 && index < serverlist.size())
{
selectedServer = index;
}
}
/**
* return list of server names
*/
QStringList servermanager::getServerNames()
{
return serverNames;
}