]> git.street.me.uk Git - andy/viking.git/blame - src/util.c
Remove mistaken need to change directory.
[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 */
5ab2942c
RN
20 /*
21 * Ideally dependencies should just be on Glib, Gtk,
22 * see vikutils for things that further depend on other Viking types
23 */
4c77d5e0
GB
24#ifdef HAVE_CONFIG_H
25#include "config.h"
26#endif
27
91c46f90 28#include <glib/gstdio.h>
4c77d5e0 29#include <glib/gi18n.h>
c612e922 30#include <glib/gprintf.h>
7d02a0b0 31
8e562a70 32#include "util.h"
7d02a0b0
GB
33#include "dialog.h"
34
7d384035
RN
35/*
36#ifdef WINDOWS
37#include <windows.h>
38#endif
39
02d0c367 40#ifndef WINDOWS
3530c91e
GB
41static gboolean spawn_command_line_async(const gchar * cmd,
42 const gchar * arg)
43{
44 gchar *cmdline = NULL;
45 gboolean status;
46
47 cmdline = g_strdup_printf("%s '%s'", cmd, arg);
48 g_debug("Running: %s", cmdline);
49
50 status = g_spawn_command_line_async(cmdline, NULL);
51
52 g_free(cmdline);
53
a0f4c917 54 return status;
3530c91e 55}
02d0c367 56#endif
7d384035 57*/
3530c91e 58
7d02a0b0
GB
59void open_url(GtkWindow *parent, const gchar * url)
60{
7d384035
RN
61 GError *error = NULL;
62 gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), url, GDK_CURRENT_TIME, &error );
63 if ( error ) {
64 a_dialog_error_msg_extra ( parent, _("Could not launch web browser. %s"), error->message );
65 g_error_free ( error );
7d02a0b0 66 }
7d02a0b0 67}
3530c91e
GB
68
69void new_email(GtkWindow *parent, const gchar * address)
70{
71 gchar *uri = g_strdup_printf("mailto:%s", address);
7d384035
RN
72 GError *error = NULL;
73 gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), uri, GDK_CURRENT_TIME, &error );
74 if ( error ) {
75 a_dialog_error_msg_extra ( parent, _("Could not create new email. %s"), error->message );
76 g_error_free ( error );
77 }
78 /*
3530c91e
GB
79#ifdef WINDOWS
80 ShellExecute(NULL, NULL, (char *) uri, NULL, ".\\", 0);
7d384035 81#else
3530c91e
GB
82 if (!spawn_command_line_async("xdg-email", uri))
83 a_dialog_error_msg ( parent, _("Could not create new email.") );
7d384035
RN
84#endif
85 */
3530c91e
GB
86 g_free(uri);
87 uri = NULL;
88}
ba4a5e11
GB
89gchar *uri_escape(gchar *str)
90{
91 gchar *esc_str = g_malloc(3*strlen(str));
92 gchar *dst = esc_str;
93 gchar *src;
94
95 for (src = str; *src; src++) {
96 if (*src == ' ')
97 *dst++ = '+';
98 else if (g_ascii_isalnum(*src))
99 *dst++ = *src;
100 else {
7705a76f 101 g_sprintf(dst, "%%%02hhX", *src);
ba4a5e11
GB
102 dst += 3;
103 }
104 }
105 *dst = '\0';
106
107 return(esc_str);
108}
109
70434be3
GB
110
111GList * str_array_to_glist(gchar* data[])
112{
113 GList *gl = NULL;
114 gpointer * p;
115 for (p = (gpointer)data; *p; p++)
116 gl = g_list_prepend(gl, *p);
117 return g_list_reverse(gl);
118}
9106934d 119
0da89d90
RN
120/**
121 * split_string_from_file_on_equals:
122 *
123 * @buf: the input string
124 * @key: newly allocated string that is before the '='
125 * @val: newly allocated string after the '='
126 *
127 * Designed for file line processing, so it ignores strings beginning with special
128 * characters, such as '#'; returns false in such situations.
129 * Also returns false if no equals character is found
130 *
131 * e.g. if buf = "GPS.parameter=42"
132 * key = "GPS.parameter"
133 * val = "42"
134 */
135gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
9106934d 136{
9106934d
RN
137 // comments, special characters in viking file format
138 if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
139 return FALSE;
0da89d90
RN
140
141 if ( ! strchr ( buf, '=' ) )
9106934d 142 return FALSE;
0da89d90
RN
143
144 gchar **strv = g_strsplit ( buf, "=", 2 );
145
146 gint gi = 0;
147 gchar *str = strv[gi];
148 while ( str ) {
149 if ( gi == 0 )
150 *key = g_strdup ( str );
151 else
152 *val = g_strdup ( str );
153 gi++;
154 str = strv[gi];
155 }
156
157 g_strfreev ( strv );
158
159 // Remove newline from val and also any other whitespace
160 *key = g_strstrip ( *key );
161 *val = g_strstrip ( *val );
162
9106934d
RN
163 return TRUE;
164}