2006-02-05 [colin] 2.0.0cvs12
[claws.git] / src / common / utils.c
index f0bbc45c59743167c8321523b489b3943ef15652..0d0b0d487cffe24fcafdfc84644257674f55f474 100644 (file)
@@ -1,6 +1,6 @@
 /*
  * Sylpheed -- a GTK+ based, lightweight, and fast e-mail client
- * Copyright (C) 1999-2005 Hiroyuki Yamamoto & The Sylpheed-Claws Team
+ * Copyright (C) 1999-2006 Hiroyuki Yamamoto & The Sylpheed-Claws 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
@@ -1199,7 +1199,7 @@ GList *add_history(GList *list, const gchar *str)
                last = g_list_last(list);
                if (last) {
                        g_free(last->data);
-                       g_list_remove(list, last->data);
+                       list = g_list_remove(list, last->data);
                }
        }
 
@@ -3593,6 +3593,30 @@ gint open_uri(const gchar *uri, const gchar *cmdline)
        return 0;
 }
 
+gint open_txt_editor(const gchar *filepath, const gchar *cmdline)
+{
+       gchar buf[BUFFSIZE];
+       gchar *p;
+
+       g_return_val_if_fail(filepath != NULL, -1);
+
+       if (cmdline &&
+           (p = strchr(cmdline, '%')) && *(p + 1) == 's' &&
+           !strchr(p + 2, '%'))
+               g_snprintf(buf, sizeof(buf), cmdline, filepath);
+       else {
+               if (cmdline)
+                       g_warning("Open Text Editor command line is invalid "
+                                 "(there must be only one '%%s'): %s",
+                                 cmdline);
+               g_snprintf(buf, sizeof(buf), DEFAULT_EDITOR_CMD, filepath);
+       }
+
+       execute_command_line(buf, TRUE);
+
+       return 0;
+}
+
 time_t remote_tzoffset_sec(const gchar *zone)
 {
        static gchar ustzstr[] = "PSTPDTMSTMDTCSTCDTESTEDT";
@@ -4297,7 +4321,7 @@ gboolean get_uri_part(const gchar *start, const gchar *scanpos,
 
        /* find end point of URI */
        for (ep_ = scanpos; *ep_ != '\0'; ep_++) {
-               if (!isgraph(*(const guchar *)ep_) ||
+               if (!g_ascii_isgraph(*(const guchar *)ep_) ||
                    !IS_ASCII(*(const guchar *)ep_) ||
                    strchr("[]{}()<>\"", *ep_))
                        break;
@@ -4309,10 +4333,10 @@ gboolean get_uri_part(const gchar *start, const gchar *scanpos,
         * should pass some URI type to this function and decide on that whether
         * to perform punctuation stripping */
 
-#define IS_REAL_PUNCT(ch)      (ispunct(ch) && ((ch) != '/')) 
+#define IS_REAL_PUNCT(ch)      (g_ascii_ispunct(ch) && !strchr("/?=", ch))
 
        for (; ep_ - 1 > scanpos + 1 &&
-              IS_REAL_PUNCT(*(const guchar *)(ep_ - 1));
+              IS_REAL_PUNCT(*(ep_ - 1));
             ep_--)
                ;
 
@@ -4722,3 +4746,30 @@ gint copy_dir(const gchar *src, const gchar *dst)
        }
        return 0;
 }
+
+/* crude test to see if a file is an email. */
+gboolean file_is_email (const gchar *filename)
+{
+       FILE *fp = NULL;
+       gchar buffer[2048];
+       gint i = 0;
+       gint score = 0;
+       if (filename == NULL)
+               return FALSE;
+       if ((fp = g_fopen(filename, "rb")) == NULL)
+               return FALSE;
+       while (i < 60 && score < 4
+              && fgets(buffer, sizeof (buffer), fp) > 0) {
+               if (!strncmp(buffer, "Return-Path:", strlen("Return-Path:")))
+                       score++;
+               if (!strncmp(buffer, "From:", strlen("From:")))
+                       score++;
+               if (!strncmp(buffer, "To:", strlen("To:")))
+                       score++;
+               if (!strncmp(buffer, "Subject:", strlen("Subject:")))
+                       score++;
+               i++;
+       }
+       fclose(fp);
+       return (score >= 4);
+}