Trim trailing white spaces.
[geeqie.git] / src / editors.c
index f4dc2ac..270c5d3 100644 (file)
@@ -1,7 +1,7 @@
 /*
  * Geeqie
  * (C) 2006 John Ellis
- * Copyright (C) 2008 - 2009 The Geeqie Team
+ * Copyright (C) 2008 - 2012 The Geeqie Team
  *
  * Author: John Ellis
  *
@@ -109,7 +109,7 @@ static GList *editor_mime_types_to_extensions(gchar **mime_types)
                {"image/png",           ".png"},
                {"image/svg",           ".svg"},
                {"image/svg+xml",       ".svg"},
-               {"image/svg+xml-compressed",    ".svg"},        
+               {"image/svg+xml-compressed",    ".svg"},
                {"image/tiff",          ".tiff;.tif"},
                {"image/x-bmp",         ".bmp"},
                {"image/x-canon-crw",   ".crw"},
@@ -123,7 +123,7 @@ static GList *editor_mime_types_to_extensions(gchar **mime_types)
                {"image/x-pcx",         ".pcx"},
                {"image/xpm",           ".xpm"},
                {"image/x-png",         ".png"},
-               {"image/x-portable-anymap",     ".pam"},        
+               {"image/x-portable-anymap",     ".pam"},
                {"image/x-portable-bitmap",     ".pbm"},
                {"image/x-portable-graymap",    ".pgm"},
                {"image/x-portable-pixmap",     ".ppm"},
@@ -135,12 +135,13 @@ static GList *editor_mime_types_to_extensions(gchar **mime_types)
                {"image/x-xcf",         ".xcf"},
                {"image/x-xpixmap",     ".xpm"},
                {"image/x-x3f",         ".x3f"},
+               {"application/x-ptoptimizer-script",    ".pto"},
                {NULL, NULL}};
        
        gint i, j;
        GList *list = NULL;
        
-       for (i = 0; mime_types[i]; i++) 
+       for (i = 0; mime_types[i]; i++)
                for (j = 0; conv_table[j][0]; j++)
                        if (strcmp(mime_types[i], conv_table[j][0]) == 0)
                                list = g_list_concat(list, filter_to_list(conv_table[j][1]));
@@ -197,14 +198,14 @@ gboolean editor_read_desktop_file(const gchar *path)
                {
                gboolean found = FALSE;
                gint i;
-               for (i = 0; categories[i]; i++) 
+               for (i = 0; categories[i]; i++)
                        {
                        /* IMHO "Graphics" is exactly the category that we are interested in, so this does not have to be configurable */
                        if (strcmp(categories[i], "Graphics") == 0)
                                {
                                found = TRUE;
                                }
-                       if (strcmp(categories[i], "X-Geeqie") == 0) 
+                       if (strcmp(categories[i], "X-Geeqie") == 0)
                                {
                                found = TRUE;
                                category_geeqie = TRUE;
@@ -224,7 +225,7 @@ gboolean editor_read_desktop_file(const gchar *path)
                {
                gboolean found = FALSE;
                gint i;
-               for (i = 0; only_show_in[i]; i++) 
+               for (i = 0; only_show_in[i]; i++)
                        if (strcmp(only_show_in[i], "X-Geeqie") == 0)
                                {
                                found = TRUE;
@@ -239,7 +240,7 @@ gboolean editor_read_desktop_file(const gchar *path)
                {
                gboolean found = FALSE;
                gint i;
-               for (i = 0; not_show_in[i]; i++) 
+               for (i = 0; not_show_in[i]; i++)
                        if (strcmp(not_show_in[i], "X-Geeqie") == 0)
                                {
                                found = TRUE;
@@ -259,7 +260,7 @@ gboolean editor_read_desktop_file(const gchar *path)
                g_free(try_exec);
                }
 
-       if (editor->ignored) 
+       if (editor->ignored)
                {
                /* ignored editors will be deleted, no need to parse the rest */
                g_key_file_free(key_file);
@@ -274,7 +275,7 @@ gboolean editor_read_desktop_file(const gchar *path)
                {
                gchar *ext = strrchr(editor->icon, '.');
                
-               if (ext && strlen(ext) == 4 && 
+               if (ext && strlen(ext) == 4 &&
                    (!strcmp(ext, ".png") || !strcmp(ext, ".xpm") || !strcmp(ext, ".svg")))
                        {
                        log_printf(_("Desktop file '%s' should not include extension in Icon key: '%s'\n"),
@@ -309,7 +310,7 @@ gboolean editor_read_desktop_file(const gchar *path)
                        {
                        editor->ext_list = editor_mime_types_to_extensions(mime_types);
                        g_strfreev(mime_types);
-                       if (!editor->ext_list) editor->hidden = TRUE; 
+                       if (!editor->ext_list) editor->hidden = TRUE;
                        }
                }
                
@@ -319,7 +320,7 @@ gboolean editor_read_desktop_file(const gchar *path)
        if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "X-Geeqie-Filter", NULL)) editor->flags |= EDITOR_DEST;
        if (g_key_file_get_boolean(key_file, DESKTOP_GROUP, "Terminal", NULL)) editor->flags |= EDITOR_TERMINAL;
        
-       editor->flags |= editor_command_parse(editor, NULL, NULL);
+       editor->flags |= editor_command_parse(editor, NULL, FALSE, NULL);
 
        if ((editor->flags & EDITOR_NO_PARAM) && !category_geeqie) editor->hidden = TRUE;
 
@@ -328,14 +329,14 @@ gboolean editor_read_desktop_file(const gchar *path)
        if (editor->ignored) return TRUE;
        
        gtk_list_store_append(desktop_file_list, &iter);
-       gtk_list_store_set(desktop_file_list, &iter, 
+       gtk_list_store_set(desktop_file_list, &iter,
                           DESKTOP_FILE_COLUMN_KEY, key,
                           DESKTOP_FILE_COLUMN_NAME, editor->name,
-                          DESKTOP_FILE_COLUMN_HIDDEN, editor->hidden,
+                          DESKTOP_FILE_COLUMN_HIDDEN, editor->hidden ? _("yes") : _("no"),
                           DESKTOP_FILE_COLUMN_WRITABLE, access_file(path, W_OK),
                           DESKTOP_FILE_COLUMN_PATH, path, -1);
        
-       return TRUE;    
+       return TRUE;
 }
 
 static gboolean editor_remove_desktop_file_cb(gpointer key, gpointer value, gpointer user_data)
@@ -356,9 +357,9 @@ void editor_table_clear(void)
                {
                gtk_list_store_clear(desktop_file_list);
                }
-       else 
+       else
                {
-               desktop_file_list = gtk_list_store_new(DESKTOP_FILE_COLUMN_COUNT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_BOOLEAN, G_TYPE_STRING);
+               desktop_file_list = gtk_list_store_new(DESKTOP_FILE_COLUMN_COUNT, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_STRING, G_TYPE_BOOLEAN, G_TYPE_STRING);
                }
        if (editors)
                {
@@ -392,7 +393,7 @@ static GList *editor_add_desktop_dir(GList *list, const gchar *path)
                        gchar *dpath = g_build_filename(path, name, NULL);
                        list = g_list_prepend(list, dpath);
                        g_free(name);
-                       }       
+                       }
                }
        closedir(dp);
        return list;
@@ -430,7 +431,7 @@ GList *editor_get_desktop_files(void)
                }
                
        g_strfreev(split_dirs);
-       return g_list_reverse(list);
+       return list;
 }
 
 static void editor_list_add_cb(gpointer key, gpointer value, gpointer data)
@@ -438,9 +439,9 @@ static void editor_list_add_cb(gpointer key, gpointer value, gpointer data)
        GList **listp = data;
        EditorDescription *editor = value;
        
-       /* do not show the special commands in any list, they are called explicitly */ 
+       /* do not show the special commands in any list, they are called explicitly */
        if (strcmp(editor->key, CMD_COPY) == 0 ||
-           strcmp(editor->key, CMD_MOVE) == 0 ||  
+           strcmp(editor->key, CMD_MOVE) == 0 ||
            strcmp(editor->key, CMD_RENAME) == 0 ||
            strcmp(editor->key, CMD_DELETE) == 0 ||
            strcmp(editor->key, CMD_FOLDER) == 0) return;
@@ -639,12 +640,14 @@ typedef enum {
 } PathType;
 
 
-static gchar *editor_command_path_parse(const FileData *fd, PathType type, const EditorDescription *editor)
+static gchar *editor_command_path_parse(const FileData *fd, gboolean consider_sidecars, PathType type, const EditorDescription *editor)
 {
        GString *string;
        gchar *pathl;
        const gchar *p = NULL;
 
+       DEBUG_2("editor_command_path_parse: %s %d %d %s", fd->path, consider_sidecars, type, editor->key);
+
        string = g_string_new("");
 
        if (type == PATH_FILE || type == PATH_FILE_URL)
@@ -668,7 +671,7 @@ static gchar *editor_command_path_parse(const FileData *fd, PathType type, const
                                        break;
                                        }
 
-                               work2 = fd->sidecar_files;
+                               work2 = consider_sidecars ? fd->sidecar_files : NULL;
                                while (work2)
                                        {
                                        FileData *sfd = work2->data;
@@ -705,7 +708,8 @@ static gchar *editor_command_path_parse(const FileData *fd, PathType type, const
                g_free(pathl);
                pathl = NULL;
                }
-
+       
+       DEBUG_2("editor_command_path_parse: return %s", pathl);
        return pathl;
 }
 
@@ -741,7 +745,7 @@ static GString *append_quoted(GString *str, const char *s, gboolean single_quote
 }
 
 
-EditorFlags editor_command_parse(const EditorDescription *editor, GList *list, gchar **output)
+EditorFlags editor_command_parse(const EditorDescription *editor, GList *list, gboolean consider_sidecars, gchar **output)
 {
        EditorFlags flags = 0;
        const gchar *p;
@@ -750,6 +754,8 @@ EditorFlags editor_command_parse(const EditorDescription *editor, GList *list, g
        gboolean single_quotes = FALSE;
        gboolean double_quotes = FALSE;
 
+       DEBUG_2("editor_command_parse: %s %d %d", editor->key, consider_sidecars, !!output);
+
        if (output)
                result = g_string_new("");
 
@@ -818,8 +824,26 @@ EditorFlags editor_command_parse(const EditorDescription *editor, GList *list, g
                                                        goto err;
                                                        }
                                                pathl = editor_command_path_parse((FileData *)list->data,
+                                                                                 consider_sidecars,
                                                                                  (*p == 'f') ? PATH_FILE : PATH_FILE_URL,
                                                                                  editor);
+                                               if (!output)
+                                                       {
+                                                       /* just testing, check also the rest of the list (like with F and U)
+                                                          any matching file is OK */
+                                                       GList *work = list->next;
+                                                       
+                                                       while (!pathl && work)
+                                                               {
+                                                               FileData *fd = work->data;
+                                                               pathl = editor_command_path_parse(fd,
+                                                                                                 consider_sidecars,
+                                                                                                 (*p == 'f') ? PATH_FILE : PATH_FILE_URL,
+                                                                                                 editor);
+                                                               work = work->next;
+                                                               }
+                                                       }
+                                                       
                                                if (!pathl)
                                                        {
                                                        flags |= EDITOR_ERROR_NO_FILE;
@@ -851,7 +875,7 @@ EditorFlags editor_command_parse(const EditorDescription *editor, GList *list, g
                                                while (work)
                                                        {
                                                        FileData *fd = work->data;
-                                                       pathl = editor_command_path_parse(fd, (*p == 'F') ? PATH_FILE : PATH_FILE_URL, editor);
+                                                       pathl = editor_command_path_parse(fd, consider_sidecars, (*p == 'F') ? PATH_FILE : PATH_FILE_URL, editor);
                                                        if (pathl)
                                                                {
                                                                ok = TRUE;
@@ -961,7 +985,7 @@ static EditorFlags editor_command_one(const EditorDescription *editor, GList *li
 
        ed->pid = -1;
        ed->flags = editor->flags;
-       ed->flags |= editor_command_parse(editor, list, &command);
+       ed->flags |= editor_command_parse(editor, list, TRUE, &command);
 
        ok = !EDITOR_ERRORS(ed->flags);
 
@@ -1224,14 +1248,18 @@ EditorFlags start_editor_from_filelist_full(const gchar *key, GList *list, const
 {
        EditorFlags error;
        EditorDescription *editor;
-       if (!key) return FALSE;
+       if (!key) return EDITOR_ERROR_EMPTY;
        
        editor = g_hash_table_lookup(editors, key);
 
-       if (!editor) return FALSE;
-       if (!list && !(editor->flags & EDITOR_NO_PARAM)) return FALSE;
+       if (!editor) return EDITOR_ERROR_EMPTY;
+       if (!list && !(editor->flags & EDITOR_NO_PARAM)) return EDITOR_ERROR_NO_FILE;
+
+       error = editor_command_parse(editor, list, TRUE, NULL);
 
-       error = editor_command_start(editor, editor->name, list, working_directory, cb, data);
+       if (EDITOR_ERRORS(error)) return error;
+
+       error |= editor_command_start(editor, editor->name, list, working_directory, cb, data);
 
        if (EDITOR_ERRORS(error))
                {
@@ -1241,7 +1269,7 @@ EditorFlags start_editor_from_filelist_full(const gchar *key, GList *list, const
                g_free(text);
                }
 
-       return error;
+       return EDITOR_ERRORS(error);
 }
 
 EditorFlags start_editor_from_filelist(const gchar *key, GList *list)