]> git.street.me.uk Git - andy/viking.git/blob - src/util.c
Only allow JPGs in the file selector for Geotagging.
[andy/viking.git] / src / util.c
1 /*
2  *    Viking - GPS data editor
3  *    Copyright (C) 2007, Guilhem Bonnefille <guilhem.bonnefille@gmail.com>
4  *    Based on:
5  *    Copyright (C) 2003-2007, Leandro A. F. Pereira <leandro@linuxmag.com.br>
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  */
20  /*
21   * Ideally dependencies should just be on Glib, Gtk,
22   * see vikutils for things that further depend on other Viking types
23   */
24 #ifdef HAVE_CONFIG_H
25 #include "config.h"
26 #endif
27
28 #include <glib/gstdio.h>
29 #include <glib/gi18n.h>
30 #include <glib/gprintf.h>
31
32 #include "util.h"
33 #include "dialog.h"
34
35 /*
36 #ifdef WINDOWS
37 #include <windows.h>
38 #endif
39
40 #ifndef WINDOWS
41 static 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  
54   return status;
55 }
56 #endif
57 */
58
59 void open_url(GtkWindow *parent, const gchar * url)
60 {
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 );
66   }
67 }
68
69 void new_email(GtkWindow *parent, const gchar * address)
70 {
71   gchar *uri = g_strdup_printf("mailto:%s", address);
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   /*
79 #ifdef WINDOWS
80   ShellExecute(NULL, NULL, (char *) uri, NULL, ".\\", 0);
81 #else
82   if (!spawn_command_line_async("xdg-email", uri))
83     a_dialog_error_msg ( parent, _("Could not create new email.") );
84 #endif
85   */
86   g_free(uri);
87   uri = NULL;
88 }
89 gchar *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 {
101       g_sprintf(dst, "%%%02hhX", *src);
102       dst += 3;
103     }
104   }
105   *dst = '\0';
106
107   return(esc_str);
108 }
109
110
111 GList * 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 }
119
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  */
135 gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
136 {
137   // comments, special characters in viking file format
138   if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
139     return FALSE;
140
141   if ( ! strchr ( buf, '=' ) )
142     return FALSE;
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
163   return TRUE;
164 }