2 * viking -- GPS Data and Topo Analyzer, Explorer, and Manager
4 * Copyright (C) 2012-2013, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
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.
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.
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
23 * @short_description: the routing framework
25 * This module handles the list of #VikRoutingEngine.
26 * It also handles the "default" functions.
34 #include <glib/gstdio.h>
35 #include <glib/gi18n.h>
37 #include "curl_download.h"
40 #include "preferences.h"
42 #include "vikrouting.h"
43 #include "vikroutingengine.h"
45 /* params will be routing.default */
46 /* we have to make sure these don't collide. */
47 #define VIKING_ROUTING_PARAMS_GROUP_KEY "routing"
48 #define VIKING_ROUTING_PARAMS_NAMESPACE "routing."
50 /* List to register all routing engines */
51 static GList *routing_engine_list = NULL;
53 static VikLayerParam prefs[] = {
54 { VIK_LAYER_NUM_TYPES, VIKING_ROUTING_PARAMS_NAMESPACE "default", VIK_LAYER_PARAM_STRING, VIK_LAYER_GROUP_NONE, N_("Default engine:"), VIK_LAYER_WIDGET_COMBOBOX, NULL, NULL, NULL },
57 gchar **routing_engine_labels = NULL;
58 gchar **routing_engine_ids = NULL;
61 * vik_routing_prefs_init:
63 * Initialize the preferences of the routing feature.
66 vik_routing_prefs_init()
68 a_preferences_register_group ( VIKING_ROUTING_PARAMS_GROUP_KEY, "Routing" );
70 VikLayerParamData tmp;
72 a_preferences_register(prefs, tmp, VIKING_ROUTING_PARAMS_GROUP_KEY);
76 search_by_id (gconstpointer a,
80 VikRoutingEngine *engine = (VikRoutingEngine *)a;
81 gchar *engineId = vik_routing_engine_get_id (engine);
83 return strcmp(id, engineId);
89 * vik_routing_find_engine:
90 * @id: the id of the engine we are looking for
92 * Returns: the found engine or %NULL
95 vik_routing_find_engine ( const gchar *id )
97 VikRoutingEngine *engine = NULL;
98 GList *elem = g_list_find_custom (routing_engine_list, id, search_by_id);
105 * vik_routing_default:
107 * Retrieve the default engine, based on user's preferences.
109 * Returns: the default engine
111 static VikRoutingEngine *
112 vik_routing_default( void )
114 const gchar *id = a_preferences_get ( VIKING_ROUTING_PARAMS_NAMESPACE "default")->s;
115 VikRoutingEngine *engine = vik_routing_find_engine(id);
116 if (engine == NULL && routing_engine_list != NULL && g_list_first (routing_engine_list) != NULL)
117 /* Fallback to first element */
118 engine = g_list_first (routing_engine_list)->data;
124 * vik_routing_default_find:
126 * Route computation with default engine.
129 vik_routing_default_find(VikTrwLayer *vt, struct LatLon start, struct LatLon end)
132 VikRoutingEngine *engine = vik_routing_default ( );
133 /* The route computation */
134 vik_routing_engine_find ( engine, vt, start, end );
138 * vik_routing_register:
139 * @engine: new routing engine to register
141 * Register a new routing engine.
144 vik_routing_register( VikRoutingEngine *engine )
146 gchar *label = vik_routing_engine_get_label ( engine );
147 gchar *id = vik_routing_engine_get_id ( engine );
150 /* check if id already exists in list */
151 GList *elem = g_list_find_custom (routing_engine_list, id, search_by_id);
153 g_debug("%s: %s already exists: update", __FUNCTION__, id);
155 /* Update main list */
156 g_object_unref (elem->data);
157 elem->data = g_object_ref ( engine );
159 /* Update GUI arrays */
160 len = g_strv_length (routing_engine_labels);
161 for (; len > 0 ; len--) {
162 if (strcmp (routing_engine_ids[len-1], id) == 0)
165 /* Update the label (possibly different */
166 g_free (routing_engine_labels[len-1]);
167 routing_engine_labels[len-1] = g_strdup (label);
170 g_debug("%s: %s is new: append", __FUNCTION__, id);
171 routing_engine_list = g_list_append ( routing_engine_list, g_object_ref ( engine ) );
173 if (routing_engine_labels)
174 len = g_strv_length (routing_engine_labels);
177 routing_engine_labels = g_realloc (routing_engine_labels, (len+2)*sizeof(gchar*));
178 routing_engine_labels[len] = g_strdup (label);
179 routing_engine_labels[len+1] = NULL;
182 routing_engine_ids = g_realloc (routing_engine_ids, (len+2)*sizeof(gchar*));
183 routing_engine_ids[len] = g_strdup (id);
184 routing_engine_ids[len+1] = NULL;
187 We have to ensure the mode LayerParam references the up-to-date
191 memcpy(&maps_layer_params[0].widget_data, ¶ms_maptypes, sizeof(gpointer));
192 memcpy(&maps_layer_params[0].extra_widget_data, ¶ms_maptypes_ids, sizeof(gpointer));
194 prefs[0].widget_data = routing_engine_labels;
195 prefs[0].extra_widget_data = routing_engine_ids;
200 * vik_routing_unregister_all:
202 * Unregister all registered routing engines.
205 vik_routing_unregister_all ()
207 g_list_foreach ( routing_engine_list, (GFunc) g_object_unref, NULL );
208 g_strfreev ( routing_engine_labels );
209 g_strfreev ( routing_engine_ids );