]> git.street.me.uk Git - andy/viking.git/blob - src/vikgototool.c
Some spelling fixes in a comment
[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
29 #include <string.h>
30
31 #include <glib.h>
32 #include <glib/gi18n.h>
33 #include <glib/gstdio.h>
34
35 static GObjectClass *parent_class;
36
37 static void goto_tool_finalize ( GObject *gob );
38 static gchar *goto_tool_get_label ( VikGotoTool *vw );
39 static DownloadFileOptions *goto_tool_get_download_options ( VikGotoTool *self );
40
41 typedef struct _VikGotoToolPrivate VikGotoToolPrivate;
42
43 struct _VikGotoToolPrivate
44 {
45   gint   id;
46   gchar *label;
47 };
48
49 #define GOTO_TOOL_GET_PRIVATE(o) (G_TYPE_INSTANCE_GET_PRIVATE ((o), \
50                                     VIK_GOTO_TOOL_TYPE,          \
51                                     VikGotoToolPrivate))
52
53 G_DEFINE_ABSTRACT_TYPE (VikGotoTool, vik_goto_tool, G_TYPE_OBJECT)
54
55 enum
56 {
57   PROP_0,
58
59   PROP_ID,
60   PROP_LABEL,
61 };
62
63 static void
64 goto_tool_set_property (GObject      *object,
65                           guint         property_id,
66                           const GValue *value,
67                           GParamSpec   *pspec)
68 {
69   VikGotoTool *self = VIK_GOTO_TOOL (object);
70   VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
71
72   switch (property_id)
73     {
74     case PROP_ID:
75       priv->id = g_value_get_uint (value);
76       g_debug ("VikGotoTool.id: %d", priv->id);
77       break;
78
79     case PROP_LABEL:
80       g_free (priv->label);
81       priv->label = g_value_dup_string (value);
82       g_debug ("VikGotoTool.label: %s", priv->label);
83       break;
84
85     default:
86       /* We don't have any other property... */
87       G_OBJECT_WARN_INVALID_PROPERTY_ID (object, property_id, pspec);
88       break;
89     }
90 }
91
92 static void
93 goto_tool_get_property (GObject    *object,
94                           guint       property_id,
95                           GValue     *value,
96                           GParamSpec *pspec)
97 {
98   VikGotoTool *self = VIK_GOTO_TOOL (object);
99   VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
100
101   switch (property_id)
102     {
103     case PROP_ID:
104       g_value_set_uint (value, priv->id);
105       break;
106
107     case PROP_LABEL:
108       g_value_set_string (value, 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 vik_goto_tool_class_init ( VikGotoToolClass *klass )
119 {
120   GObjectClass *gobject_class;
121   GParamSpec *pspec;
122
123   gobject_class = G_OBJECT_CLASS (klass);
124   gobject_class->finalize = goto_tool_finalize;
125   gobject_class->set_property = goto_tool_set_property;
126   gobject_class->get_property = goto_tool_get_property;
127
128   pspec = g_param_spec_string ("label",
129                                "Label",
130                                "Set the label",
131                                "<no-set>" /* default value */,
132                                G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
133   g_object_class_install_property (gobject_class,
134                                    PROP_LABEL,
135                                    pspec);
136
137   pspec = g_param_spec_uint ("id",
138                              "Id of the tool",
139                              "Set the id",
140                              0  /* minimum value */,
141                              G_MAXUINT16 /* maximum value */,
142                              0  /* default value */,
143                              G_PARAM_CONSTRUCT_ONLY | G_PARAM_READWRITE);
144   g_object_class_install_property (gobject_class,
145                                    PROP_ID,
146                                    pspec);
147
148   klass->get_label = goto_tool_get_label;
149   klass->get_download_options = goto_tool_get_download_options;
150
151   parent_class = g_type_class_peek_parent (klass);
152
153   g_type_class_add_private (klass, sizeof (VikGotoToolPrivate));
154 }
155
156 VikGotoTool *vik_goto_tool_new ()
157 {
158   return VIK_GOTO_TOOL ( g_object_new ( VIK_GOTO_TOOL_TYPE, NULL ) );
159 }
160
161 static void vik_goto_tool_init ( VikGotoTool *self )
162 {
163   VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE (self);
164   priv->label = NULL;
165 }
166
167 static void goto_tool_finalize ( GObject *gob )
168 {
169   VikGotoToolPrivate *priv = GOTO_TOOL_GET_PRIVATE ( gob );
170   g_free ( priv->label ); priv->label = NULL;
171   G_OBJECT_CLASS(parent_class)->finalize(gob);
172 }
173
174 static gchar *goto_tool_get_label ( VikGotoTool *self )
175 {
176   VikGotoToolPrivate *priv = NULL;
177   priv = GOTO_TOOL_GET_PRIVATE (self);
178   return g_strdup ( priv->label );
179 }
180
181 static DownloadFileOptions *goto_tool_get_download_options ( VikGotoTool *self )
182 {
183   // Default: return NULL
184   return NULL;
185 }
186
187 gchar *vik_goto_tool_get_label ( VikGotoTool *self )
188 {
189   return VIK_GOTO_TOOL_GET_CLASS( self )->get_label( self );
190 }
191
192 gchar *vik_goto_tool_get_url_format ( VikGotoTool *self )
193 {
194   return VIK_GOTO_TOOL_GET_CLASS( self )->get_url_format( self );
195 }
196
197 DownloadFileOptions *vik_goto_tool_get_download_options ( VikGotoTool *self )
198 {
199   return VIK_GOTO_TOOL_GET_CLASS( self )->get_download_options( self );
200 }
201
202 gboolean vik_goto_tool_parse_file_for_latlon (VikGotoTool *self, gchar *filename, struct LatLon *ll)
203 {
204   return VIK_GOTO_TOOL_GET_CLASS( self )->parse_file_for_latlon( self, filename, ll );
205 }
206
207 /**
208  * vik_goto_tool_get_coord:
209  *
210  * @self:      The #VikGotoTool
211  * @vvp:       The #VikViewport
212  * @srch_str:  The string to search with
213  * @coord:     Returns the top match position for a successful search
214  *
215  * Returns: An integer value indicating:
216  *  0  = search found something
217  *  -1 = search place not found by the #VikGotoTool
218  *  1  = search unavailable in the #VikGotoTool due to communication issue
219  *
220  */
221 int vik_goto_tool_get_coord ( VikGotoTool *self, VikWindow *vw, VikViewport *vvp, gchar *srch_str, VikCoord *coord )
222 {
223   gchar *tmpname;
224   gchar *uri;
225   gchar *escaped_srch_str;
226   int ret = 0;  /* OK */
227   struct LatLon ll;
228
229   g_debug("%s: raw goto: %s", __FUNCTION__, srch_str);
230
231   escaped_srch_str = g_uri_escape_string(srch_str, NULL, TRUE);
232
233   g_debug("%s: escaped goto: %s", __FUNCTION__, escaped_srch_str);
234
235   uri = g_strdup_printf(vik_goto_tool_get_url_format(self), escaped_srch_str);
236
237   tmpname = a_download_uri_to_tmp_file ( uri, vik_goto_tool_get_download_options(self) );
238
239   if ( !tmpname ) {
240     // Some kind of download error, so no tmp file
241     ret = 1;
242     goto done_no_file;
243   }
244
245   g_debug("%s: %s", __FILE__, tmpname);
246   if (!vik_goto_tool_parse_file_for_latlon(self, tmpname, &ll)) {
247     ret = -1;
248     goto done;
249   }
250   vik_coord_load_from_latlon ( coord, vik_viewport_get_coord_mode(vvp), &ll );
251
252 done:
253   (void)util_remove(tmpname);
254 done_no_file:
255   g_free(tmpname);
256   g_free(escaped_srch_str);
257   g_free(uri);
258   return ret;
259 }