]> git.street.me.uk Git - andy/viking.git/blobdiff - src/util.c
SF Support Requests#21: Make default print size full page.
[andy/viking.git] / src / util.c
index cddb13d7d5f2e31a393ee32908d5973063925d9a..ee84287041970b7d5367843eec9f1d2cfb68e5e7 100644 (file)
  *    along with this program; if not, write to the Free Software
  *    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
  */
+ /*
+  * Ideally dependencies should just be on Glib, Gtk,
+  * see vikutils for things that further depend on other Viking types
+  */
 #ifdef HAVE_CONFIG_H
 #include "config.h"
 #endif
 
-#ifdef WINDOWS
-#include <windows.h>
-#endif
-
+#include <glib/gstdio.h>
 #include <glib/gi18n.h>
 #include <glib/gprintf.h>
 
 #include "util.h"
 #include "dialog.h"
 
+/*
+#ifdef WINDOWS
+#include <windows.h>
+#endif
+
+#ifndef WINDOWS
 static gboolean spawn_command_line_async(const gchar * cmd,
                                          const gchar * arg)
 {
@@ -46,53 +53,39 @@ static gboolean spawn_command_line_async(const gchar * cmd,
  
   return status;
 }
+#endif
+*/
 
 void open_url(GtkWindow *parent, const gchar * url)
 {
-#ifdef WINDOWS
-  ShellExecute(NULL, NULL, (char *) url, NULL, ".\\", 0);
-#else /* WINDOWS */
-  const gchar *browsers[] = {
-    "xdg-open", "gnome-open", "kfmclient openURL",
-    "sensible-browser", "firefox", "epiphany",
-    "iceweasel", "seamonkey", "galeon", "mozilla",
-    "opera", "konqueror", "netscape", "links -g",
-    "chromium-browser", "chromium", "chrome",
-    "safari", "camino", "omniweb", "icab",
-    NULL
-  };
-  gint i=0;
-  
-  const gchar *browser = g_getenv("BROWSER");
-  if (browser == NULL || browser[0] == '\0') {
-    /* $BROWSER not set -> use first entry */
-    browser = browsers[i++];
+  GError *error = NULL;
+  gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), url, GDK_CURRENT_TIME, &error );
+  if ( error ) {
+    a_dialog_error_msg_extra ( parent, _("Could not launch web browser. %s"), error->message );
+    g_error_free ( error );
   }
-  do {
-    if (spawn_command_line_async(browser, url)) {
-      return;
-    }
-
-    browser = browsers[i++];
-  } while(browser);
-  
-  a_dialog_error_msg ( parent, _("Could not launch web browser.") );
-#endif /* WINDOWS */
 }
 
 void new_email(GtkWindow *parent, const gchar * address)
 {
   gchar *uri = g_strdup_printf("mailto:%s", address);
+  GError *error = NULL;
+  gtk_show_uri ( gtk_widget_get_screen (GTK_WIDGET(parent)), uri, GDK_CURRENT_TIME, &error );
+  if ( error ) {
+    a_dialog_error_msg_extra ( parent, _("Could not create new email. %s"), error->message );
+    g_error_free ( error );
+  }
+  /*
 #ifdef WINDOWS
   ShellExecute(NULL, NULL, (char *) uri, NULL, ".\\", 0);
-#else /* WINDOWS */
+#else
   if (!spawn_command_line_async("xdg-email", uri))
     a_dialog_error_msg ( parent, _("Could not create new email.") );
-#endif /* WINDOWS */
+#endif
+  */
   g_free(uri);
   uri = NULL;
 }
-
 gchar *uri_escape(gchar *str)
 {
   gchar *esc_str = g_malloc(3*strlen(str));
@@ -114,3 +107,58 @@ gchar *uri_escape(gchar *str)
   return(esc_str);
 }
 
+
+GList * str_array_to_glist(gchar* data[])
+{
+  GList *gl = NULL;
+  gpointer * p;
+  for (p = (gpointer)data; *p; p++)
+    gl = g_list_prepend(gl, *p);
+  return g_list_reverse(gl);
+}
+
+/**
+ * split_string_from_file_on_equals:
+ *
+ * @buf: the input string
+ * @key: newly allocated string that is before the '='
+ * @val: newly allocated string after the '='
+ *
+ * Designed for file line processing, so it ignores strings beginning with special
+ *  characters, such as '#'; returns false in such situations.
+ * Also returns false if no equals character is found
+ *
+ * e.g. if buf = "GPS.parameter=42"
+ *   key = "GPS.parameter"
+ *   val = "42"
+ */
+gboolean split_string_from_file_on_equals ( const gchar *buf, gchar **key, gchar **val )
+{
+  // comments, special characters in viking file format
+  if ( buf == NULL || buf[0] == '\0' || buf[0] == '~' || buf[0] == '=' || buf[0] == '#' )
+    return FALSE;
+
+  if ( ! strchr ( buf, '=' ) )
+    return FALSE;
+
+  gchar **strv = g_strsplit ( buf, "=", 2 );
+
+  gint gi = 0;
+  gchar *str = strv[gi];
+  while ( str ) {
+       if ( gi == 0 )
+         *key = g_strdup ( str );
+       else
+         *val = g_strdup ( str );
+    gi++;
+    str = strv[gi];
+  }
+
+  g_strfreev ( strv );
+
+  // Remove newline from val and also any other whitespace
+  *key = g_strstrip ( *key );
+  *val = g_strstrip ( *val );
+
+  return TRUE;
+}