Expand ~ for --remote --get-filelist command
[geeqie.git] / src / remote.c
index 17a64d5..2389a17 100644 (file)
@@ -458,6 +458,7 @@ static void gr_new_window(const gchar *text, GIOChannel *channel, gpointer data)
        if (!layout_valid(&lw)) return;
 
        lw_id = layout_menu_new_window(NULL, lw);
+       layout_set_path(lw_id, pwd);
 }
 
 static gboolean gr_close_window_cb()
@@ -822,6 +823,7 @@ static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurs
        FileData *fd;
        GString *out_string = g_string_new(NULL);
        GList *work;
+       gchar *tilde_filename;
 
        if (strcmp(text, "") == 0)
                {
@@ -836,14 +838,17 @@ static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurs
                }
        else
                {
-               if (isdir(text))
+               tilde_filename = expand_tilde(text);
+               if (isdir(tilde_filename))
                        {
-                       dir_fd = file_data_new_dir(text);
+                       dir_fd = file_data_new_dir(tilde_filename);
                        }
                else
                        {
+                       g_free(tilde_filename);
                        return;
                        }
+               g_free(tilde_filename);
                }
 
        if (recurse)
@@ -975,13 +980,17 @@ static void gr_file_tell(const gchar *text, GIOChannel *channel, gpointer data)
                        {
                        out_string = g_strconcat(image_get_path(lw_id->image), NULL);
                        }
+               }
+       else
+               {
+               out_string = g_strconcat(lw_id->dir_fd->path, G_DIR_SEPARATOR_S, NULL);
+               }
 
-               g_io_channel_write_chars(channel, out_string, -1, NULL, NULL);
-               g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
+       g_io_channel_write_chars(channel, out_string, -1, NULL, NULL);
+       g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
 
-               g_free(collection_name);
-               g_free(out_string);
-               }
+       g_free(collection_name);
+       g_free(out_string);
 }
 
 static void gr_file_info(const gchar *text, GIOChannel *channel, gpointer data)
@@ -1014,47 +1023,98 @@ static void gr_file_info(const gchar *text, GIOChannel *channel, gpointer data)
                        g_string_append_printf(out_string, _("Page no: %d/%d\n"), fd->page_num + 1, fd->page_total);
                        }
 
-               country_name = exif_get_data_as_text(fd->exif, "formatted.countryname");
-               if (country_name)
+               if (fd->exif)
                        {
-                       g_string_append_printf(out_string, _("Country name: %s\n"), country_name);
-                       }
+                       country_name = exif_get_data_as_text(fd->exif, "formatted.countryname");
+                       if (country_name)
+                               {
+                               g_string_append_printf(out_string, _("Country name: %s\n"), country_name);
+                               g_free(country_name);
+                               }
 
-               country_code = exif_get_data_as_text(fd->exif, "formatted.countrycode");
-               if (country_name)
-                       {
-                       g_string_append_printf(out_string, _("Country code: %s\n"), country_code);
-                       }
+                       country_code = exif_get_data_as_text(fd->exif, "formatted.countrycode");
+                       if (country_name)
+                               {
+                               g_string_append_printf(out_string, _("Country code: %s\n"), country_code);
+                               g_free(country_code);
+                               }
 
-               timezone = exif_get_data_as_text(fd->exif, "formatted.timezone");
-               if (timezone)
-                       {
-                       g_string_append_printf(out_string, _("Timezone: %s\n"), timezone);
-                       }
+                       timezone = exif_get_data_as_text(fd->exif, "formatted.timezone");
+                       if (timezone)
+                               {
+                               g_string_append_printf(out_string, _("Timezone: %s\n"), timezone);
+                               g_free(timezone);
+                               }
 
-               local_time = exif_get_data_as_text(fd->exif, "formatted.localtime");
-               if (local_time)
-                       {
-                       g_string_append_printf(out_string, ("Local time: %s\n"), local_time);
+                       local_time = exif_get_data_as_text(fd->exif, "formatted.localtime");
+                       if (local_time)
+                               {
+                               g_string_append_printf(out_string, ("Local time: %s\n"), local_time);
+                               g_free(local_time);
+                               }
                        }
 
                g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
                g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
 
-               g_free(country_name);
-               g_free(country_code);
-               g_free(timezone);
-               g_free(local_time);
                g_string_free(out_string, TRUE);
                file_data_unref(fd);
                g_free(filename);
                }
 }
 
+static gchar *config_file_path(const gchar *param)
+{
+       gchar *path = NULL;
+       gchar *full_name = NULL;
+
+       if (file_extension_match(param, ".xml"))
+               {
+               path = g_build_filename(get_window_layouts_dir(), param, NULL);
+               }
+       else if (file_extension_match(param, NULL))
+               {
+               full_name = g_strconcat(param, ".xml", NULL);
+               path = g_build_filename(get_window_layouts_dir(), full_name, NULL);
+               }
+
+       if (!isfile(path))
+               {
+               g_free(path);
+               path = NULL;
+               }
+
+       g_free(full_name);
+       return path;
+}
+
+static gboolean is_config_file(const gchar *param)
+{
+       gchar *name = NULL;
+
+       name = config_file_path(param);
+       if (name)
+               {
+               g_free(name);
+               return TRUE;
+               }
+       return FALSE;
+}
+
 static void gr_config_load(const gchar *text, GIOChannel *channel, gpointer data)
 {
        gchar *filename = expand_tilde(text);
 
+       if (!g_strstr_len(filename, -1, G_DIR_SEPARATOR_S))
+               {
+               if (is_config_file(filename))
+                       {
+                       gchar *tmp = config_file_path(filename);
+                       g_free(filename);
+                       filename = tmp;
+                       }
+               }
+
        if (isfile(filename))
                {
                load_config_from_file(filename, FALSE);
@@ -1152,8 +1212,7 @@ static void gr_list_add(const gchar *text, GIOChannel *channel, gpointer data)
 
        if (collection_add(remote_data->command_collection, file_data_new_group(text), FALSE) && new)
                {
-               layout_image_set_collection(NULL, remote_data->command_collection,
-                                           collection_get_first(remote_data->command_collection));
+               layout_image_set_collection(lw_id, remote_data->command_collection, collection_get_first(remote_data->command_collection));
                }
 }
 
@@ -1171,8 +1230,15 @@ static void gr_pwd(const gchar *text, GIOChannel *channel, gpointer data)
 {
        LayoutWindow *lw = NULL;
 
+       lw = layout_find_by_layout_id("main");
+       if (!lw)
+               {
+               lw = g_list_first(layout_window_list)->data;
+               }
+
        g_free(pwd);
        pwd = g_strdup(text);
+       lw_id = lw;
 }
 
 static void gr_print0(const gchar *text, GIOChannel *channel, gpointer data)
@@ -1244,13 +1310,13 @@ static RemoteCommandEntry remote_commands[] = {
        { "+t", "--tools-show",         gr_tools_show,          FALSE, TRUE,  NULL, N_("show tools") },
        { "-t", "--tools-hide",         gr_tools_hide,          FALSE, TRUE,  NULL, N_("hide tools") },
        { "-q", "--quit",               gr_quit,                FALSE, FALSE, NULL, N_("quit") },
-       { NULL, "--config-load:",       gr_config_load,         TRUE,  FALSE, N_("<FILE>"), N_("load configuration from FILE") },
+       { NULL, "--config-load:",       gr_config_load,         TRUE,  FALSE, N_("<FILE>|layout ID"), N_("load configuration from FILE") },
        { NULL, "--get-sidecars:",      gr_get_sidecars,        TRUE,  FALSE, N_("<FILE>"), N_("get list of sidecars of FILE") },
        { NULL, "--get-destination:",   gr_get_destination,     TRUE,  FALSE, N_("<FILE>"), N_("get destination path of FILE") },
-       { NULL, "file:",                gr_file_load,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE, bring Geeqie window to the top") },
-       { NULL, "--file:",              gr_file_load,           TRUE,  FALSE, N_("<FILE>"), N_("open FILE, bring Geeqie window to the top") },
-       { NULL, "File:",                gr_file_load_no_raise,  TRUE,  FALSE, N_("<FILE>"), N_("open FILE, do not bring Geeqie window to the top") },
-       { NULL, "--File:",              gr_file_load_no_raise,  TRUE,  FALSE, N_("<FILE>"), N_("open FILE, do not bring Geeqie window to the top") },
+       { NULL, "file:",                gr_file_load,           TRUE,  FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
+       { NULL, "--file:",              gr_file_load,           TRUE,  FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, bring Geeqie window to the top") },
+       { NULL, "File:",                gr_file_load_no_raise,  TRUE,  FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, do not bring Geeqie window to the top") },
+       { NULL, "--File:",              gr_file_load_no_raise,  TRUE,  FALSE, N_("<FILE>|<URL>"), N_("open FILE or URL, do not bring Geeqie window to the top") },
        { NULL, "--tell",               gr_file_tell,           FALSE, FALSE, NULL, N_("print filename [and Collection] of current image") },
        { NULL, "--pixel-info",         gr_pixel_info,          FALSE, FALSE, NULL, N_("print pixel info of mouse pointer on current image") },
        { NULL, "--get-rectangle",      gr_rectangle,           FALSE, FALSE, NULL, N_("get rectangle co-ordinates") },
@@ -1381,7 +1447,7 @@ GList *remote_build_list(GList *list, gint argc, gchar *argv[], GList **errors)
                        {
                        list = g_list_append(list, argv[i]);
                        }
-               else if (errors && !isfile(argv[i]))
+               else if (errors && !isname(argv[i]))
                        {
                        *errors = g_list_append(*errors, argv[i]);
                        }
@@ -1430,6 +1496,11 @@ void remote_control(const gchar *arg_exec, GList *remote_list, const gchar *path
                        entry = remote_command_find(text, NULL);
                        if (entry)
                                {
+                               /* If Geeqie is not running, stop the --new-window command opening a second window */
+                               if (g_strcmp0(text, "--new-window") == 0)
+                                       {
+                                       remote_list = g_list_remove(remote_list, text);
+                                       }
                                if (entry->prefer_command_line)
                                        {
                                        remote_list = g_list_remove(remote_list, text);