]> git.street.me.uk Git - andy/viking.git/blob - src/vikgototool.c
Bump up default map cache size to 128MB and allow setting to 1GB.
[andy/viking.git] / src / vikgototool.c
1 /*
2  * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
3  *
4  * Copyright (C) 2009, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; either version 2 of the License, or
9  * (at your option) any later version.
10  *
11  * This program is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with this program; if not, write to the Free Software
18  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19  *
20  */
21
22 #ifdef HAVE_CONFIG_H
23 #include "config.h"
24 #endif
25
26 #include "vikgototool.h"
27 #include "util.h"
28 #include "curl_download.h"
29
30 #include <string.h>
31
32 #include <glib.h>
33 #include <glib/gi18n.h>
34 #include <glib/gstdio.h>
35
36 static void goto_tool_class_init ( VikGotoToolClass *klass );
37 static void goto_tool_init ( VikGotoTool *vlp );
38
39 static GObjectClass *parent_class;
40
41 static void goto_tool_finalize ( GObject *gob );
42 static gchar *goto_tool_get_label ( VikGotoTool *vw );
43 static DownloadMapOptions *goto_tool_get_download_options ( VikGotoTool *self );
44
45 typedef struct _VikGotoToolPrivate VikGotoToolPrivate;
46
47 struct _VikGotoToolPrivate
48 {
49   gint   id;
50   gchar *label;
51 };
52
53 #define GOTO_TOOL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
54                                     VIK_GOTO_TOOL_TYPE,          \
55                                     VikGotoToolPrivate))
56
57 GType vik_goto_tool_get_type()
58 {
59   static GType w_type = 0;
60
61   if (!w_type)
62   {
63     static const GTypeInfo w_info = 
64     {
65       sizeof (VikGotoToolClass),
66       NULL, /* base_init */
67       NULL, /* base_finalize */
68       (GClassInitFunc) goto_tool_class_init,
69       NULL, /* class_finalize */
70       NULL, /* class_data */
71       sizeof (VikGotoTool),
72       0,
73       (GInstanceInitFunc) goto_tool_init,
74     };
75     w_type = g_type_register_static ( G_TYPE_OBJECT, "VikGotoTool", &w_info, G_TYPE_FLAG_ABSTRACT );
76   }
77
78   return w_type;
79 }
80
81 enum
82 {
83   PROP_0,
84
85   PROP_ID,
86   PROP_LABEL,
87 };
88
89 static void
90 goto_tool_set_property (GObject      *object,
91                           guint         property_id,
92                           const GValue *value,
93                           GParamSpec   *pspec)
94 {
95   VikGotoTool *self = VIK_GOTO_TOOL (object);
96   VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
97
98   switch (property_id)
99     {
100     case PROP_ID:
101       priv->id = g_value_get_uint (value);
102       g_debug ("VikGotoTool.id: %d", priv->id);
103       break;
104
105     case PROP_LABEL:
106       g_free (priv->label);
107       priv->label = g_value_dup_string (value);
108       g_debug ("VikGotoTool.label: %s", priv->label);
109       break;
110
111     default:
112       /* We don't have any other property... */
113       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
114       break;
115     }
116 }
117
118 static void
119 goto_tool_get_property (GObject    *object,
120                           guint       property_id,
121                           GValue     *value,
122                           GParamSpec *pspec)
123 {
124   VikGotoTool *self = VIK_GOTO_TOOL (object);
125   VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
126
127   switch (property_id)
128     {
129     case PROP_ID:
130       g_value_set_uint (value, priv->id);
131       break;
132
133     case PROP_LABEL:
134       g_value_set_string (value, priv->label);
135       break;
136
137     default:
138       /* We don't have any other property... */
139       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
140       break;
141     }
142 }
143
144 static void goto_tool_class_init ( VikGotoToolClass *klass )
145 {
146   GObjectClass *gobject_class;
147   GParamSpec *pspec;
148
149   gobject_class = G_OBJECT_CLASS (klass);
150   gobject_class->finalize = goto_tool_finalize;
151   gobject_class->set_property = goto_tool_set_property;
152   gobject_class->get_property = goto_tool_get_property;
153
154   pspec = g_param_spec_string ("label",
155                                "Label",
156                                "Set the label",
157                                "<no-set>" /* default value */,
158                                G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
159   g_object_class_install_property (gobject_class,
160                                    PROP_LABEL,
161                                    pspec);
162
163   pspec = g_param_spec_uint ("id",
164                              "Id of the tool",
165                              "Set the id",
166                              0  /* minimum value */,
167                              G_MAXUINT16 /* maximum value */,
168                              0  /* default value */,
169                              G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
170   g_object_class_install_property (gobject_class,
171                                    PROP_ID,
172                                    pspec);
173
174   klass->get_label = goto_tool_get_label;
175   klass->get_download_options = goto_tool_get_download_options;
176
177   parent_class = g_type_class_peek_parent (klass);
178
179   g_type_class_add_private (klass, sizeof (VikGotoToolPrivate));
180 }
181
182 VikGotoTool *vik_goto_tool_new ()
183 {
184   return VIK_GOTO_TOOL ( g_object_new ( VIK_GOTO_TOOL_TYPE, NULL ) );
185 }
186
187 static void goto_tool_init ( VikGotoTool *self )
188 {
189   VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
190   priv->label = NULL;
191 }
192
193 static void goto_tool_finalize ( GObject *gob )
194 {
195   VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE ( gob );
196   g_free ( priv->label ); priv->label = NULL;
197   G_OBJECT_CLASS(parent_class)->finalize(gob);
198 }
199
200 static gchar *goto_tool_get_label ( VikGotoTool *self )
201 {
202   VikGotoToolPrivate *priv = NULL;
203   priv = GOTO_TOOL_GET_PRIVATE (self);
204   return g_strdup ( priv->label );
205 }
206
207 static DownloadMapOptions *goto_tool_get_download_options ( VikGotoTool *self )
208 {
209   // Default: return NULL
210   return NULL;
211 }
212
213 gchar *vik_goto_tool_get_label ( VikGotoTool *self )
214 {
215   return VIK_GOTO_TOOL_GET_CLASS( self )->get_label( self );
216 }
217
218 gchar *vik_goto_tool_get_url_format ( VikGotoTool *self )
219 {
220   return VIK_GOTO_TOOL_GET_CLASS( self )->get_url_format( self );
221 }
222
223 DownloadMapOptions *vik_goto_tool_get_download_options ( VikGotoTool *self )
224 {
225   return VIK_GOTO_TOOL_GET_CLASS( self )->get_download_options( self );
226 }
227
228 gboolean vik_goto_tool_parse_file_for_latlon (VikGotoTool *self, gchar *filename, struct LatLon *ll)
229 {
230   return VIK_GOTO_TOOL_GET_CLASS( self )->parse_file_for_latlon( self, filename, ll );
231 }
232
233 int vik_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord )
234 {
235   FILE *tmp_file;
236   int tmp_fd;
237   gchar *tmpname;
238   gchar *uri;
239   gchar *escaped_srch_str;
240   int ret = 0;  /* OK */
241   struct LatLon ll;
242
243   g_debug("%s: raw goto: %s", __FUNCTION__, srch_str);
244
245   escaped_srch_str = uri_escape(srch_str);
246
247   g_debug("%s: escaped goto: %s", __FUNCTION__, escaped_srch_str);
248
249   if ((tmp_fd = g_file_open_tmp ("vikgoto.XXXXXX", &tmpname, NULL)) == -1) {
250     g_critical(_("couldn't open temp file"));
251     return -1;
252   }
253   
254   tmp_file = fdopen(tmp_fd, "r+");
255   uri = g_strdup_printf(vik_goto_tool_get_url_format(self), escaped_srch_str);
256
257   /* TODO: curl may not be available */
258   if (curl_download_uri(uri, tmp_file, vik_goto_tool_get_download_options(self), 0, NULL)) {  /* error */
259     fclose(tmp_file);
260     tmp_file = NULL;
261     ret = -1;
262     goto done;
263   }
264
265   fclose(tmp_file);
266   tmp_file = NULL;
267   g_debug("%s: %s", __FILE__, tmpname);
268   if (!vik_goto_tool_parse_file_for_latlon(self, tmpname, &ll)) {
269     ret = -1;
270     goto done;
271   }
272   vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll );
273
274 done:
275   g_free(escaped_srch_str);
276   g_free(uri);
277   g_remove(tmpname);
278   g_free(tmpname);
279   return ret;
280 }