]> git.street.me.uk Git - andy/viking.git/blame - src/util.c
Make mpp_to_zoom() a generic function
[andy/viking.git] / src / util.c
CommitLineData
7d02a0b0
GB
1/*
2 * Viking - GPS data editor
a482007a 3 * Copyright (C) 2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
7d02a0b0 4 * Based on:
a482007a 5 * Copyright (C) 2003-2007, Leandro A. F. Pereira <leandro@linuxmag.com.br>
7d02a0b0
GB
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, version 2.
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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
4c77d5e0
GB
20#ifdef HAVE_CONFIG_H
21#include "config.h"
22#endif
23
24#include <glib/gi18n.h>
c612e922 25#include <glib/gprintf.h>
7d02a0b0 26
8e562a70 27#include "util.h"
7d02a0b0
GB
28#include "dialog.h"
29
7d384035
RN
30/*
31#ifdef WINDOWS
32#include <windows.h>
33#endif
34
02d0c367 35#ifndef WINDOWS
3530c91e
GB
36static gboolean spawn_command_line_async(const gchar * cmd,
37 const gchar * arg)
38{
39 gchar *cmdline = NULL;
40 gboolean status;
41
42 cmdline = g_strdup_printf("%s '%s'", cmd, arg);
43 g_debug("Running: %s", cmdline);
44
45 status = g_spawn_command_line_async(cmdline, NULL);
46
47 g_free(cmdline);
48
a0f4c917 49 return status;
3530c91e 50}
02d0c367 51#endif
7d384035 52*/
3530c91e 53
7d02a0b0
GB
54void open_url(GtkWindow *parent, const gchar * url)
55{
7d384035
RN
56 GError *error = NULL;
57 gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), url, GDK_CURRENT_TIME, &error );
58 if ( error ) {
59 a_dialog_error_msg_extra ( parent, _("Could not launch web browser. %s"), error->message );
60 g_error_free ( error );
7d02a0b0 61 }
7d02a0b0 62}
3530c91e
GB
63
64void new_email(GtkWindow *parent, const gchar * address)
65{
66 gchar *uri = g_strdup_printf("mailto:%s", address);
7d384035
RN
67 GError *error = NULL;
68 gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), uri, GDK_CURRENT_TIME, &error );
69 if ( error ) {
70 a_dialog_error_msg_extra ( parent, _("Could not create new email. %s"), error->message );
71 g_error_free ( error );
72 }
73 /*
3530c91e
GB
74#ifdef WINDOWS
75 ShellExecute(NULL, NULL, (char *) uri, NULL, ".\\", 0);
7d384035 76#else
3530c91e
GB
77 if (!spawn_command_line_async("xdg-email", uri))
78 a_dialog_error_msg ( parent, _("Could not create new email.") );
7d384035
RN
79#endif
80 */
3530c91e
GB
81 g_free(uri);
82 uri = NULL;
83}
ba4a5e11
GB
84gchar *uri_escape(gchar *str)
85{
86 gchar *esc_str = g_malloc(3*strlen(str));
87 gchar *dst = esc_str;
88 gchar *src;
89
90 for (src = str; *src; src++) {
91 if (*src == ' ')
92 *dst++ = '+';
93 else if (g_ascii_isalnum(*src))
94 *dst++ = *src;
95 else {
7705a76f 96 g_sprintf(dst, "%%%02hhX", *src);
ba4a5e11
GB
97 dst += 3;
98 }
99 }
100 *dst = '\0';
101
102 return(esc_str);
103}
104
70434be3
GB
105
106GList * str_array_to_glist(gchar* data[])
107{
108 GList *gl = NULL;
109 gpointer * p;
110 for (p = (gpointer)data; *p; p++)
111 gl = g_list_prepend(gl, *p);
112 return g_list_reverse(gl);
113}
9106934d 114
0da89d90
RN
115/**
116 * split_string_from_file_on_equals:
117 *
118 * @buf: the input string
119 * @key: newly allocated string that is before the '='
120 * @val: newly allocated string after the '='
121 *
122 * Designed for file line processing, so it ignores strings beginning with special
123 * characters, such as '#'; returns false in such situations.
124 * Also returns false if no equals character is found
125 *
126 * e.g. if buf = "GPS.parameter=42"
127 * key = "GPS.parameter"
128 * val = "42"
129 */
130gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
9106934d 131{
9106934d
RN
132 // comments, special characters in viking file format
133 if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
134 return FALSE;
0da89d90
RN
135
136 if ( ! strchr ( buf, '=' ) )
9106934d 137 return FALSE;
0da89d90
RN
138
139 gchar **strv = g_strsplit ( buf, "=", 2 );
140
141 gint gi = 0;
142 gchar *str = strv[gi];
143 while ( str ) {
144 if ( gi == 0 )
145 *key = g_strdup ( str );
146 else
147 *val = g_strdup ( str );
148 gi++;
149 str = strv[gi];
150 }
151
152 g_strfreev ( strv );
153
154 // Remove newline from val and also any other whitespace
155 *key = g_strstrip ( *key );
156 *val = g_strstrip ( *val );
157
9106934d
RN
158 return TRUE;
159}
f05ecca4
RN
160
161/* 1 << (x) is like a 2**(x) */
162#define GZ(x) (1<<(x))
163
164static const gdouble scale_mpps[] = { GZ(0), GZ(1), GZ(2), GZ(3), GZ(4), GZ(5), GZ(6), GZ(7), GZ(8), GZ(9),
165 GZ(10), GZ(11), GZ(12), GZ(13), GZ(14), GZ(15), GZ(16), GZ(17) };
166
167static const gint num_scales = (sizeof(scale_mpps) / sizeof(scale_mpps[0]));
168
169#define ERROR_MARGIN 0.01
170guint8 mpp_to_zoom ( gdouble mpp )
171{
172 gint i;
173 for ( i = 0; i < num_scales; i++ ) {
174 if ( ABS(scale_mpps[i] - mpp) < ERROR_MARGIN ) {
175 g_debug ( "mpp_to_zoom: %f -> %d", mpp, i );
176 return i;
177 }
178 }
179 // Handle mpp smaller than 1
180 // return a useful value such that '17 - this number' gives a natural number.
181 // Ideally should return '-1' or '0.5' but that's tricky with an unsigned int type!
182 // (i.e. should rework to support zoom levels of 18 or 19)
183 return 0;
184}