Remove use of fnmatch(), in favour of GPatternSpec.
[claws.git] / src / mimeview.c
index 038abf6fa2fb3dcd4694658cd2e11d5f49b32b01..eb767b92ee0fbe5d09a8efccd9d99c79fe1cf5cd 100644 (file)
 
 #include "defs.h"
 
+#ifdef G_OS_WIN32
+#define UNICODE
+#define _UNICODE
+#endif
+
 #include <glib.h>
 #include <glib/gi18n.h>
 #include <gdk/gdkkeysyms.h>
 #include <sys/types.h>
 #include <sys/stat.h>
 
-#ifndef HAVE_APACHE_FNMATCH
-/* kludge: apache's fnmatch clashes with <regex.h>, don't include
- * fnmatch.h */
-#include <fnmatch.h>
-#endif
-
 #include "main.h"
 #include "mimeview.h"
 #include "textview.h"
@@ -821,28 +820,23 @@ static MimeViewer *get_viewer_for_content_type(MimeView *mimeview, const gchar *
        GSList *cur;
        MimeViewerFactory *factory = NULL;
        MimeViewer *viewer = NULL;
-       gchar *real_contenttype = NULL;
+       gchar *real_contenttype = NULL, *tmp;
 
-/*
- * FNM_CASEFOLD is a GNU extension
- */
-#ifndef FNM_CASEFOLD
-#define FNM_CASEFOLD 0
        real_contenttype = g_utf8_strdown((gchar *)content_type, -1);
-#else
-       real_contenttype = g_strdup(content_type);
-#endif
        
        for (cur = mimeviewer_factories; cur != NULL; cur = g_slist_next(cur)) {
                MimeViewerFactory *curfactory = cur->data;
                gint i = 0;
 
                while (curfactory->content_types[i] != NULL) {
-                       if(!fnmatch(curfactory->content_types[i], real_contenttype, FNM_CASEFOLD)) {
+                       tmp = g_utf8_strdown(curfactory->content_types[i], -1);
+                       if (g_pattern_match_simple(tmp, real_contenttype)) {
                                debug_print("%s\n", curfactory->content_types[i]);
                                factory = curfactory;
+                               g_free(tmp);
                                break;
                        }
+                       g_free(tmp);
                        i++;
                }
                if (factory != NULL)
@@ -1558,6 +1552,8 @@ void mimeview_select_next_part(MimeView *mimeview)
        MimeInfo *partinfo = NULL;
        gboolean has_next;
        
+       if (!mimeview->opened) return;
+
        gtk_tree_model_get_iter(model, &iter, mimeview->opened);
        path = gtk_tree_model_get_path(model, &iter);
 skip:
@@ -1591,6 +1587,8 @@ void mimeview_select_prev_part(MimeView *mimeview)
        MimeInfo *partinfo = NULL;
        gboolean has_prev;
        
+       if (!mimeview->opened) return;
+
        gtk_tree_model_get_iter(model, &iter, mimeview->opened);
        path = gtk_tree_model_get_path(model, &iter);
 skip:
@@ -2200,6 +2198,7 @@ static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
 #ifndef G_OS_WIN32
        gchar *p;
        gchar buf[BUFFSIZE];
+
        if (cmd == NULL)
                mimeview_open_part_with(mimeview, partinfo, TRUE);
        else {
@@ -2222,7 +2221,18 @@ static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
        }
 #else
        SHFILEINFO file_info;
-       if ((SHGetFileInfo(filename, 0, &file_info, sizeof(SHFILEINFO), SHGFI_EXETYPE)) != 0) {
+       GError *error = NULL;
+       gunichar2 *fn16 = g_utf8_to_utf16(filename, -1, NULL, NULL, &error);
+
+       if (error != NULL) {
+               alertpanel_error(_("Could not convert attachment name to UTF-16:\n\n%s"),
+                                       error->message);
+               debug_print("filename '%s' conversion to UTF-16 failed\n", filename);
+               g_error_free(error);
+               return;
+       }
+
+       if ((SHGetFileInfo((LPCWSTR)fn16, 0, &file_info, sizeof(SHFILEINFO), SHGFI_EXETYPE)) != 0) {
                AlertValue val = alertpanel_full(_("Execute untrusted binary?"), 
                                      _("This attachment is an executable file. Executing "
                                        "untrusted binaries is dangerous and could probably "
@@ -2232,10 +2242,13 @@ static void mimeview_view_file(const gchar *filename, MimeInfo *partinfo,
                                      NULL, FALSE, NULL, ALERT_WARNING, G_ALERTDEFAULT);
                if (val == G_ALERTALTERNATE) {
                        debug_print("executing binary\n");
-                       ShellExecute(NULL, "open", filename, NULL, NULL, SW_SHOW);
+                       ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
                }
-       } else
-               ShellExecute(NULL, "open", filename, NULL, NULL, SW_SHOW);
+       } else {
+               ShellExecute(NULL, L"open", (LPCWSTR)fn16, NULL, NULL, SW_SHOW);
+       }
+
+       g_free(fn16);
        
 #endif
 }