]> err.no Git - mapper/blob - src/map-tile-repo.c
Remove old map sources
[mapper] / src / map-tile-repo.c
1 /*
2  * This file is part of mapper
3  *
4  * Copyright (C) 2007 Kaj-Michael Lang
5  * Copyright (C) 2006-2007 John Costigan.
6  *
7  * This program is free software; you can redistribute it and/or modify
8  * it under the terms of the GNU General Public License as published by
9  * the Free Software Foundation; either version 2 of the License, or
10  * (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License along
18  * with this program; if not, write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20  */
21
22 #include <unistd.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <glib.h>
26
27 #include "map-tile-repo.h"
28
29 RepoData *
30 map_tile_repo_new(void)
31 {
32 return g_new0(RepoData, 1);
33 }
34
35 RepoData *
36 map_tile_repo_new_from_string(gchar *str)
37 {
38 gchar *token, *error_check;
39 RepoData *rd;
40
41 /* Parse each part of a repo, delimited by newline characters:
42  * 1. name
43  * 2. url
44  * 3. cache_dir
45  * 4. dl_zoom_steps
46  * 5. view_zoom_steps
47  */
48
49 rd=map_tile_repo_new();
50
51 /* Parse name. */
52 token = strsep(&str, "\n\t");
53 if (token)
54         rd->name = g_strdup(token);
55
56 /* Parse URL format. */
57 token = strsep(&str, "\n\t");
58 if (token)
59         rd->url = g_strdup(token);
60
61 /* Parse cache dir. */
62 token = strsep(&str, "\n\t");
63 if (token)
64         rd->cache_dir = gnome_vfs_expand_initial_tilde(token);
65
66 /* Parse download zoom steps. */
67 token = strsep(&str, "\n\t");
68 if (!token || !*token || !(rd->dl_zoom_steps = atoi(token)))
69         rd->dl_zoom_steps = 1;
70
71 /* Parse view zoom steps. */
72 token = strsep(&str, "\n\t");
73 if (!token || !*token || !(rd->view_zoom_steps = atoi(token)))
74         rd->view_zoom_steps = 1;
75
76 /* Parse double-size. */
77 token = strsep(&str, "\n\t");
78 if (token)
79         rd->double_size = atoi(token);  /* Default is zero (FALSE) */
80
81 /* Parse next-able. */
82 token = strsep(&str, "\n\t");
83 if (!token || !*token || (rd->nextable = strtol(token, &error_check, 10), token == str))
84         rd->nextable = TRUE;
85
86 map_tile_repo_set_type(rd);
87 return rd;
88 }
89
90
91 void
92 map_tile_repo_free(RepoData *rd)
93 {
94 if (rd->name)
95         g_free(rd->name);
96 if (rd->url)
97         g_free(rd->url);
98 if (rd->cache_dir)
99         g_free(rd->cache_dir);
100
101 g_free(rd);
102 }
103
104 void 
105 map_tile_repo_set_type(RepoData *rd)
106 {
107 if (rd->url && *rd->url) {
108         gchar *url = g_utf8_strdown(rd->url, -1);
109
110         /* Determine type of repository. */
111         if (strstr(url, "service=wms"))
112                 rd->type = REPOTYPE_WMS;
113         else if (strstr(url, "%s"))
114                 rd->type = REPOTYPE_QUAD_QRST;
115         else if (strstr(url, "%0d"))
116                 rd->type = REPOTYPE_XYZ_INV;
117         else if (strstr(url, "%0s"))
118                 rd->type = REPOTYPE_QUAD_ZERO;
119         else
120                 rd->type = REPOTYPE_XYZ;
121
122         g_free(url);
123 } else 
124         rd->type = REPOTYPE_NONE;
125 }
126
127 gboolean
128 map_tile_repo_make_cache_dir(RepoData *rd)
129 {
130 if (g_mkdir_with_parents(rd->cache_dir, 0755))
131         return FALSE;
132 return g_file_test(rd->cache_dir, G_FILE_TEST_EXISTS);
133 }
134