]> git.street.me.uk Git - andy/viking.git/blob - src/vikmapsource.c
Remove temporary files used asynchronously on program exit.
[andy/viking.git] / src / vikmapsource.c
1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 4; tab-width: 4 -*- */
2 /*
3  * viking
4  * Copyright (C) 2009-2010, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
5  * 
6  * viking is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  * 
11  * viking is distributed in the hope that it will be useful, but
12  * WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
14  * See the GNU General Public License for more details.
15  * 
16  * You should have received a copy of the GNU General Public License along
17  * with this program.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20  /**
21   * SECTION:vikmapsource
22   * @short_description: the base class to describe map source
23   * 
24   * The #VikMapSource class is both the interface and the base class
25   * for the hierarchie of map source.
26   */
27 #ifdef HAVE_CONFIG_H
28 #include "config.h"
29 #endif
30
31 #include "vikviewport.h"
32 #include "vikcoord.h"
33 #include "mapcoord.h"
34 #include "download.h"
35 #include "vikmapsource.h"
36
37 static void vik_map_source_init (VikMapSource *object);
38 static void vik_map_source_finalize (GObject *object);
39 static void vik_map_source_class_init (VikMapSourceClass *klass);
40
41 static gboolean _supports_download_only_new (VikMapSource *object);
42
43 G_DEFINE_ABSTRACT_TYPE (VikMapSource, vik_map_source, G_TYPE_OBJECT);
44
45 static void
46 vik_map_source_init (VikMapSource *object)
47 {
48         /* TODO: Add initialization code here */
49 }
50
51 static void
52 vik_map_source_finalize (GObject *object)
53 {
54         /* TODO: Add deinitalization code here */
55
56         G_OBJECT_CLASS (vik_map_source_parent_class)->finalize (object);
57 }
58
59 static void
60 vik_map_source_class_init (VikMapSourceClass *klass)
61 {
62         GObjectClass* object_class = G_OBJECT_CLASS (klass);
63
64         klass->get_copyright = NULL;
65         klass->get_license = NULL;
66         klass->get_license_url = NULL;
67         klass->get_logo = NULL;
68         klass->get_uniq_id = NULL;
69         klass->get_label = NULL;
70         klass->get_tilesize_x = NULL;
71         klass->get_tilesize_y = NULL;
72         klass->get_drawmode = NULL;
73         klass->is_direct_file_access = NULL;
74         klass->is_mbtiles = NULL;
75         klass->supports_download_only_new = _supports_download_only_new;
76         klass->coord_to_mapcoord = NULL;
77         klass->mapcoord_to_center_coord = NULL;
78         klass->download = NULL;
79         klass->download_handle_init = NULL;
80         klass->download_handle_cleanup = NULL;
81         
82         object_class->finalize = vik_map_source_finalize;
83 }
84
85 gboolean
86 _supports_download_only_new (VikMapSource *self)
87 {
88         // Default feature: does not support
89         return FALSE;
90 }
91
92 /**
93  * vik_map_source_get_copyright:
94  * @self: the VikMapSource of interest.
95  * @bbox: bounding box of interest.
96  * @zoom: the zoom level of interest.
97  * @fct: the callback function to use to return matching copyrights.
98  * @data: the user data to use to call the callbaack function.
99  *
100  * Retrieve copyright(s) for the corresponding bounding box and zoom level.
101  */
102 void
103 vik_map_source_get_copyright (VikMapSource *self, LatLonBBox bbox, gdouble zoom, void (*fct)(VikViewport*,const gchar*), void *data)
104 {
105         VikMapSourceClass *klass;
106         g_return_if_fail (self != NULL);
107         g_return_if_fail (VIK_IS_MAP_SOURCE (self));
108         klass = VIK_MAP_SOURCE_GET_CLASS(self);
109
110         g_return_if_fail (klass->get_copyright != NULL);
111
112         (*klass->get_copyright)(self, bbox, zoom, fct, data);
113 }
114
115 const gchar *
116 vik_map_source_get_license (VikMapSource *self)
117 {
118         VikMapSourceClass *klass;
119         g_return_val_if_fail (self != NULL, NULL);
120         g_return_val_if_fail (VIK_IS_MAP_SOURCE (self), NULL);
121         klass = VIK_MAP_SOURCE_GET_CLASS(self);
122
123         g_return_val_if_fail (klass->get_license != NULL, NULL);
124
125         return (*klass->get_license)(self);
126 }
127
128 const gchar *
129 vik_map_source_get_license_url (VikMapSource *self)
130 {
131         VikMapSourceClass *klass;
132         g_return_val_if_fail (self != NULL, NULL);
133         g_return_val_if_fail (VIK_IS_MAP_SOURCE (self), NULL);
134         klass = VIK_MAP_SOURCE_GET_CLASS(self);
135
136         g_return_val_if_fail (klass->get_license_url != NULL, NULL);
137
138         return (*klass->get_license_url)(self);
139 }
140
141 const GdkPixbuf *
142 vik_map_source_get_logo (VikMapSource *self)
143 {
144         VikMapSourceClass *klass;
145         g_return_val_if_fail (self != NULL, NULL);
146         g_return_val_if_fail (VIK_IS_MAP_SOURCE (self), NULL);
147         klass = VIK_MAP_SOURCE_GET_CLASS(self);
148
149         g_return_val_if_fail (klass->get_logo != NULL, NULL);
150
151         return (*klass->get_logo)(self);
152 }
153
154 guint16
155 vik_map_source_get_uniq_id (VikMapSource *self)
156 {
157         VikMapSourceClass *klass;
158         g_return_val_if_fail (self != NULL, (guint16 )0);
159         g_return_val_if_fail (VIK_IS_MAP_SOURCE (self), (guint16 )0);
160         klass = VIK_MAP_SOURCE_GET_CLASS(self);
161
162         g_return_val_if_fail (klass->get_uniq_id != NULL, (guint16 )0);
163
164         return (*klass->get_uniq_id)(self);
165 }
166
167 const gchar *
168 vik_map_source_get_label (VikMapSource *self)
169 {
170         VikMapSourceClass *klass;
171         g_return_val_if_fail (self != NULL, NULL);
172         g_return_val_if_fail (VIK_IS_MAP_SOURCE (self), NULL);
173         klass = VIK_MAP_SOURCE_GET_CLASS(self);
174
175         g_return_val_if_fail (klass->get_label != NULL, NULL);
176
177         return (*klass->get_label)(self);
178 }
179
180 guint16
181 vik_map_source_get_tilesize_x (VikMapSource *self)
182 {
183         VikMapSourceClass *klass;
184         g_return_val_if_fail (self != NULL, (guint16 )0);
185         g_return_val_if_fail (VIK_IS_MAP_SOURCE (self), (guint16 )0);
186         klass = VIK_MAP_SOURCE_GET_CLASS(self);
187
188         g_return_val_if_fail (klass->get_tilesize_x != NULL, (guint16 )0);
189
190         return (*klass->get_tilesize_x)(self);
191 }
192
193 guint16
194 vik_map_source_get_tilesize_y (VikMapSource *self)
195 {
196         VikMapSourceClass *klass;
197         g_return_val_if_fail (self != NULL, (guint16 )0);
198         g_return_val_if_fail (VIK_IS_MAP_SOURCE (self), (guint16 )0);
199         klass = VIK_MAP_SOURCE_GET_CLASS(self);
200
201         g_return_val_if_fail (klass->get_tilesize_y != NULL, (guint16 )0);
202
203         return (*klass->get_tilesize_y)(self);
204 }
205
206 VikViewportDrawMode
207 vik_map_source_get_drawmode (VikMapSource *self)
208 {
209         VikMapSourceClass *klass;
210         g_return_val_if_fail (self != NULL, (VikViewportDrawMode )0);
211         g_return_val_if_fail (VIK_IS_MAP_SOURCE (self), (VikViewportDrawMode )0);
212         klass = VIK_MAP_SOURCE_GET_CLASS(self);
213
214         g_return_val_if_fail (klass->get_drawmode != NULL, (VikViewportDrawMode )0);
215
216         return (*klass->get_drawmode)(self);
217 }
218
219 /**
220  * vik_map_source_is_direct_file_access:
221  * @self: the VikMapSource of interest.
222  *
223  *   Return true when we can bypass all this download malarky
224  *   Treat the files as a pre generated data set in OSM tile server layout: tiledir/%d/%d/%d.png
225  */
226 gboolean
227 vik_map_source_is_direct_file_access (VikMapSource * self)
228 {
229         VikMapSourceClass *klass;
230         g_return_val_if_fail (self != NULL, 0);
231         g_return_val_if_fail (VIK_IS_MAP_SOURCE (self), 0);
232         klass = VIK_MAP_SOURCE_GET_CLASS(self);
233
234         g_return_val_if_fail (klass->is_direct_file_access != NULL, 0);
235
236         return (*klass->is_direct_file_access)(self);
237 }
238
239 /**
240  * vik_map_source_is_mbtiles:
241  * @self: the VikMapSource of interest.
242  *
243  *   Return true when the map is in an MB Tiles format.
244  *   See http://github.com/mapbox/mbtiles-spec
245  *   (Read Only ATM)
246  */
247 gboolean
248 vik_map_source_is_mbtiles (VikMapSource * self)
249 {
250         VikMapSourceClass *klass;
251         g_return_val_if_fail (self != NULL, 0);
252         g_return_val_if_fail (VIK_IS_MAP_SOURCE (self), 0);
253         klass = VIK_MAP_SOURCE_GET_CLASS(self);
254
255         g_return_val_if_fail (klass->is_mbtiles != NULL, 0);
256
257         return (*klass->is_mbtiles)(self);
258 }
259
260 gboolean
261 vik_map_source_supports_download_only_new (VikMapSource * self)
262 {
263         VikMapSourceClass *klass;
264         g_return_val_if_fail (self != NULL, 0);
265         g_return_val_if_fail (VIK_IS_MAP_SOURCE (self), 0);
266         klass = VIK_MAP_SOURCE_GET_CLASS(self);
267
268         g_return_val_if_fail (klass->supports_download_only_new != NULL, 0);
269
270         return (*klass->supports_download_only_new)(self);
271 }
272
273 gboolean
274 vik_map_source_coord_to_mapcoord (VikMapSource *self, const VikCoord *src, gdouble xzoom, gdouble yzoom, MapCoord *dest )
275 {
276         VikMapSourceClass *klass;
277         g_return_val_if_fail (self != NULL, FALSE);
278         g_return_val_if_fail (VIK_IS_MAP_SOURCE (self), FALSE);
279         klass = VIK_MAP_SOURCE_GET_CLASS(self);
280
281         g_return_val_if_fail (klass->coord_to_mapcoord != NULL, FALSE);
282
283         return (*klass->coord_to_mapcoord)(self, src, xzoom, yzoom, dest);
284 }
285
286 void
287 vik_map_source_mapcoord_to_center_coord (VikMapSource *self, MapCoord *src, VikCoord *dest)
288 {
289         VikMapSourceClass *klass;
290         g_return_if_fail (self != NULL);
291         g_return_if_fail (VIK_IS_MAP_SOURCE (self));
292         klass = VIK_MAP_SOURCE_GET_CLASS(self);
293
294         g_return_if_fail (klass->mapcoord_to_center_coord != NULL);
295
296         (*klass->mapcoord_to_center_coord)(self, src, dest);
297 }
298
299 /**
300  * vik_map_source_download:
301  * @self:    The VikMapSource of interest.
302  * @src:     The map location to download
303  * @dest_fn: The filename to save the result in
304  * @handle:  Potential reusable Curl Handle (may be NULL)
305  *
306  * Returns: How successful the download was as per the type #DownloadResult_t
307  */
308 DownloadResult_t
309 vik_map_source_download (VikMapSource * self, MapCoord * src, const gchar * dest_fn, void *handle)
310 {
311         VikMapSourceClass *klass;
312         g_return_val_if_fail (self != NULL, 0);
313         g_return_val_if_fail (VIK_IS_MAP_SOURCE (self), 0);
314         klass = VIK_MAP_SOURCE_GET_CLASS(self);
315
316         g_return_val_if_fail (klass->download != NULL, 0);
317
318         return (*klass->download)(self, src, dest_fn, handle);
319 }
320
321 void *
322 vik_map_source_download_handle_init (VikMapSource *self)
323 {
324         VikMapSourceClass *klass;
325         g_return_val_if_fail (self != NULL, 0);
326         g_return_val_if_fail (VIK_IS_MAP_SOURCE (self), 0);
327         klass = VIK_MAP_SOURCE_GET_CLASS(self);
328
329         g_return_val_if_fail (klass->download_handle_init != NULL, 0);
330
331         return (*klass->download_handle_init)(self);
332 }
333
334 void
335 vik_map_source_download_handle_cleanup (VikMapSource * self, void * handle)
336 {
337         VikMapSourceClass *klass;
338         g_return_if_fail (self != NULL);
339         g_return_if_fail (VIK_IS_MAP_SOURCE (self));
340         klass = VIK_MAP_SOURCE_GET_CLASS(self);
341
342         g_return_if_fail (klass->download_handle_cleanup != NULL);
343
344         (*klass->download_handle_cleanup)(self, handle);
345 }