First implementation of auto-configuration (bug #3140)
[claws.git] / src / common / utils.c
index a48d4e4cf7168a5fdda6d17245571338cf1326fa..52f6bac343111275e539d4018cf4ec514d6c607d 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2012 Hiroyuki Yamamoto & The Claws Mail Team
+ * Copyright (C) 1999-2013 Hiroyuki Yamamoto & The Claws Mail Team
  *
  * This program is free software; you can redistribute it and/or modify
  * it under the terms of the GNU General Public License as published by
@@ -25,6 +25,7 @@
 #include "defs.h"
 
 #include <glib.h>
+#include <gio/gio.h>
 
 #include <glib/gi18n.h>
 
 #  include <w32lib.h>
 #endif
 
-#ifdef MAEMO
-#include <libosso.h>
-#ifdef CHINOOK
-# include <tablet-browser-interface.h>
-#else
-# include <osso-browser-interface.h>
-#endif
-#endif
-
 #include "utils.h"
 #include "socket.h"
 #include "../codeconv.h"
@@ -246,6 +238,21 @@ gint mkstemp(gchar *template)
 }
 #endif /* G_OS_WIN32 */
 
+GSList *slist_copy_deep(GSList *list, GCopyFunc func)
+{
+#if GLIB_CHECK_VERSION(2, 34, 0)
+       return g_slist_copy_deep(list, func, NULL);
+#else
+       GSList *res = g_slist_copy(list);
+       GSList *walk = res;
+       while (walk) {
+               walk->data = func(walk->data, NULL);
+               walk = walk->next;
+       }
+       return res;
+#endif
+}
+
 void list_free_strings(GList *list)
 {
        list = g_list_first(list);
@@ -269,10 +276,7 @@ void slist_free_strings_full(GSList *list)
 #if GLIB_CHECK_VERSION(2,28,0)
        g_slist_free_full(list, (GDestroyNotify)g_free);
 #else
-       while (list != NULL) {
-               g_free(list->data);
-               list = list->next;
-       }
+       g_slist_foreach(list, (GFunc)g_free, NULL);
        g_slist_free(list);
 #endif
 }
@@ -819,6 +823,47 @@ void extract_quote(gchar *str, gchar quote_chr)
        }
 }
 
+/* Returns a newly allocated string with all quote_chr not at the beginning
+   or the end of str escaped with '\' or the given str if not required. */
+gchar *escape_internal_quotes(gchar *str, gchar quote_chr)
+{
+       register gchar *p, *q;
+       gchar *qstr;
+       int k = 0, l = 0;
+
+       if (str == NULL || *str == '\0')
+               return str;
+
+       /* search for unescaped quote_chr */
+       p = str;
+       if (*p == quote_chr)
+               ++p, ++l;
+       while (*p) {
+               if (*p == quote_chr && *(p - 1) != '\\' && *(p + 1) != '\0')
+                       ++k;
+               ++p, ++l;
+       }
+       if (!k) /* nothing to escape */
+               return str;
+
+       /* unescaped quote_chr found */
+       qstr = g_malloc(l + k + 1);
+       p = str;
+       q = qstr;
+       if (*p == quote_chr) {
+               *q = quote_chr;
+               ++p, ++q;
+       }
+       while (*p) {
+               if (*p == quote_chr && *(p - 1) != '\\' && *(p + 1) != '\0')
+                       *q++ = '\\';
+               *q++ = *p++;
+       }
+       *q = '\0';
+
+       return qstr;
+}
+
 void eliminate_address_comment(gchar *str)
 {
        register gchar *srcp, *destp;
@@ -1918,77 +1963,23 @@ const gchar *get_mail_base_dir(void)
        return get_home_dir();
 }
 
-#ifdef MAEMO
-const gchar *prefs_common_get_data_root(void);
-gchar *last_data_root = NULL;
-#endif
-
 const gchar *get_news_cache_dir(void)
 {
        static gchar *news_cache_dir = NULL;
-#ifdef MAEMO
-       const gchar *data_root = prefs_common_get_data_root();
-       if (strcmp2(data_root, last_data_root)) {
-               g_free(news_cache_dir);
-               news_cache_dir = NULL;
-       }
-#endif
        if (!news_cache_dir)
-#ifndef MAEMO
                news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                                             NEWS_CACHE_DIR, NULL);
-#else
-       {
-               if (data_root) {
-                       news_cache_dir = g_strconcat(data_root, G_DIR_SEPARATOR_S,
-                                            "Claws", G_DIR_SEPARATOR_S, 
-                                            g_get_user_name(), G_DIR_SEPARATOR_S,
-                                            NEWS_CACHE_DIR, NULL);
-                       g_free(last_data_root);
-                       last_data_root = g_strdup(last_data_root);
-               } else {
-                       news_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
-                                            NEWS_CACHE_DIR, NULL);
-                       g_free(last_data_root);
-                       last_data_root = NULL;
-               }
-       }
-#endif
+
        return news_cache_dir;
 }
 
 const gchar *get_imap_cache_dir(void)
 {
        static gchar *imap_cache_dir = NULL;
-#ifdef MAEMO
-       const gchar *data_root = prefs_common_get_data_root();
-       if (strcmp2(data_root, last_data_root)) {
-               g_free(imap_cache_dir);
-               imap_cache_dir = NULL;
-       }
-#endif
 
        if (!imap_cache_dir)
-#ifndef MAEMO
                imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
                                             IMAP_CACHE_DIR, NULL);
-#else
-       {
-               if (data_root) {
-                       imap_cache_dir = g_strconcat(data_root, G_DIR_SEPARATOR_S,
-                                            "Claws", G_DIR_SEPARATOR_S, 
-                                            g_get_user_name(), G_DIR_SEPARATOR_S,
-                                            IMAP_CACHE_DIR, NULL);
-                       g_free(last_data_root);
-                       last_data_root = g_strdup(last_data_root);
-               } else {
-                       imap_cache_dir = g_strconcat(get_rc_dir(), G_DIR_SEPARATOR_S,
-                                            IMAP_CACHE_DIR, NULL);
-                       g_free(last_data_root);
-                       last_data_root = NULL;
-               }
-       }
-#endif
 
        return imap_cache_dir;
 }
@@ -2111,7 +2102,7 @@ const gchar *get_domain_name(void)
 
                if (gethostname(hostname, sizeof(hostname)) != 0) {
                        perror("gethostname");
-                       domain_name = "unknown";
+                       domain_name = "localhost";
                } else {
                        hostname[sizeof(hostname) - 1] = '\0';
                        if ((hp = my_gethostbyname(hostname)) == NULL) {
@@ -2126,7 +2117,7 @@ const gchar *get_domain_name(void)
 
        return domain_name;
 #else
-       return "unknown";
+       return "localhost";
 #endif
 }
 
@@ -2240,7 +2231,7 @@ gboolean is_file_entry_exist(const gchar *file)
 
 gboolean dirent_is_regular_file(struct dirent *d)
 {
-#if !defined(G_OS_WIN32) && !defined(MAEMO) && defined(HAVE_DIRENT_D_TYPE)
+#if !defined(G_OS_WIN32) && defined(HAVE_DIRENT_D_TYPE)
        if (d->d_type == DT_REG)
                return TRUE;
        else if (d->d_type != DT_UNKNOWN)
@@ -3421,7 +3412,7 @@ gchar *get_command_output(const gchar *cmdline)
 
        return child_stdout;
 }
-#ifndef MAEMO
+
 static gint is_unchanged_uri_char(char c)
 {
        switch (c) {
@@ -3457,10 +3448,10 @@ static void encode_uri(gchar *encoded_uri, gint bufsize, const gchar *uri)
        }
        encoded_uri[k] = 0;
 }
-#endif
+
 gint open_uri(const gchar *uri, const gchar *cmdline)
 {
-#ifndef MAEMO
+
 #ifndef G_OS_WIN32
        gchar buf[BUFFSIZE];
        gchar *p;
@@ -3485,12 +3476,6 @@ gint open_uri(const gchar *uri, const gchar *cmdline)
        execute_command_line(buf, TRUE);
 #else
        ShellExecute(NULL, "open", uri, NULL, NULL, SW_SHOW);
-#endif
-#else
-       extern osso_context_t *get_osso_context(void);
-       osso_rpc_run_with_defaults(get_osso_context(), "osso_browser",
-                                       OSSO_BROWSER_OPEN_NEW_WINDOW_REQ, NULL, 
-                                       DBUS_TYPE_STRING, uri, DBUS_TYPE_INVALID);
 #endif
        return 0;
 }
@@ -5530,3 +5515,45 @@ int cm_canonicalize_filename(const gchar *filename, gchar **canonical_name) {
        slist_free_strings_full(canonical_parts);
        return 0;
 }
+
+#if (defined USE_GNUTLS && GLIB_CHECK_VERSION(2,22,0))
+gboolean auto_configure_service(const gchar *service, const gchar *domain, gchar **srvhost, guint16 *srvport)
+{
+       GResolver *resolver;
+       GList *answers, *cur;
+       GError *error = NULL;
+       gboolean result = FALSE;
+
+       cm_return_val_if_fail(service != NULL, FALSE);
+       cm_return_val_if_fail(domain != NULL, FALSE);
+
+       resolver = g_resolver_get_default();
+       if (resolver == NULL)
+               return FALSE;
+       
+       answers = g_resolver_lookup_service(resolver, service, "tcp", domain, NULL, &error);
+
+       *srvhost = NULL;
+       *srvport = 0;
+
+       if (answers) {
+               for (cur = g_srv_target_list_sort(answers); cur; cur = cur->next) {
+                       GSrvTarget *target = (GSrvTarget *)cur->data;
+                       const gchar *hostname = g_srv_target_get_hostname(target);
+                       guint16 port = g_srv_target_get_port(target);
+                       if (hostname && strcmp(hostname,"") && port > 0) {
+                               result = TRUE;
+                               *srvhost = g_strdup(hostname);
+                               *srvport = port;
+                               break;
+                       }
+               }
+               g_resolver_free_targets(answers);
+       } else if (error) {
+               g_error_free(error);
+       }
+
+       g_object_unref(resolver);
+       return result;
+}
+#endif
\ No newline at end of file