Ref #820: Problem with window in the current build
[geeqie.git] / src / remote.c
index 73b8ae7..e4249d8 100644 (file)
@@ -74,6 +74,12 @@ struct _RemoteData {
        CollectionData *command_collection;
 };
 
+/* To enable file names containing newlines to be processed correctly,
+ * the --print0 remote option sets returned data to be terminated with a null
+ * character rather a newline
+ */
+static gboolean print0 = FALSE;
+
 /* Remote commands from main.c are prepended with the current dir the remote
  * command was made from. Some remote commands require this. The
  * value is stored here
@@ -121,7 +127,8 @@ static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition conditi
                gchar *buffer = NULL;
                GError *error = NULL;
                gsize termpos;
-
+               /* FIXME: it should be possible to terminate the command with a null character */
+               g_io_channel_set_line_term(source, "<gq_end_of_command>", -1);
                while ((status = g_io_channel_read_line(source, &buffer, NULL, &termpos, &error)) == G_IO_STATUS_NORMAL)
                        {
                        if (buffer)
@@ -131,7 +138,7 @@ static gboolean remote_server_client_cb(GIOChannel *source, GIOCondition conditi
                                if (strlen(buffer) > 0)
                                        {
                                        if (rc->read_func) rc->read_func(rc, buffer, source, rc->read_data);
-                                       g_io_channel_write_chars(source, "\n", -1, NULL, NULL); /* empty line finishes the command */
+                                       g_io_channel_write_chars(source, "<gq_end_of_command>", -1, NULL, NULL); /* empty line finishes the command */
                                        g_io_channel_flush(source, NULL);
                                        }
                                g_free(buffer);
@@ -347,7 +354,7 @@ static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
        channel = g_io_channel_unix_new(rc->fd);
 
        g_io_channel_write_chars(channel, text, -1, NULL, &error);
-       g_io_channel_write_chars(channel, "\n", -1, NULL, &error);
+       g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, &error);
        g_io_channel_flush(channel, &error);
 
        if (error)
@@ -365,18 +372,33 @@ static gboolean remote_client_send(RemoteConnection *rc, const gchar *text)
                {
                gchar *buffer = NULL;
                gsize termpos;
+               g_io_channel_set_line_term(channel, "<gq_end_of_command>", -1);
                while (g_io_channel_read_line(channel, &buffer, NULL, &termpos, &error) == G_IO_STATUS_NORMAL)
                        {
                        if (buffer)
                                {
-                               if (buffer[0] == '\n') /* empty line finishes the command */
+                               if (g_strstr_len(buffer, -1, "<gq_end_of_command>") == buffer) /* empty line finishes the command */
                                        {
                                        g_free(buffer);
                                        fflush(stdout);
                                        break;
                                        }
                                buffer[termpos] = '\0';
-                               printf("%s\n", buffer);
+                               if (g_strstr_len(buffer, -1, "print0") != 0)
+                                       {
+                                       print0 = TRUE;
+                                       }
+                               else
+                                       {
+                                       if (print0)
+                                               {
+                                               printf("%s%c", buffer, 0);
+                                               }
+                                       else
+                                               {
+                                               printf("%s\n", buffer);
+                                               }
+                                       }
                                g_free(buffer);
                                buffer = NULL;
                                }
@@ -436,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()
@@ -652,7 +675,7 @@ static void gr_file_load_no_raise(const gchar *text, GIOChannel *channel, gpoint
        gchar *filename;
        gchar *tilde_filename;
 
-       if (!download_web_file(text, NULL))
+       if (!download_web_file(text, TRUE, NULL))
                {
                tilde_filename = expand_tilde(text);
                filename = set_pwd(tilde_filename);
@@ -697,7 +720,6 @@ static void gr_pixel_info(const gchar *text, GIOChannel *channel, gpointer data)
        gint width, height;
        gint r_mouse, g_mouse, b_mouse;
        PixbufRenderer *pr;
-       LayoutWindow *lw = NULL;
 
        if (!layout_valid(&lw_id)) return;
 
@@ -720,7 +742,7 @@ static void gr_pixel_info(const gchar *text, GIOChannel *channel, gpointer data)
                                                 r_mouse, g_mouse, b_mouse);
 
                        g_io_channel_write_chars(channel, pixel_info, -1, NULL, NULL);
-                       g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
+                       g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
 
                        g_free(pixel_info);
                        }
@@ -739,7 +761,6 @@ static void gr_rectangle(const gchar *text, GIOChannel *channel, gpointer data)
 {
        gchar *rectangle_info;
        PixbufRenderer *pr;
-       LayoutWindow *lw = NULL;
        gint x1, y1, x2, y2;
 
        if (!options->draw_rectangle) return;
@@ -757,7 +778,7 @@ static void gr_rectangle(const gchar *text, GIOChannel *channel, gpointer data)
                                        (y2 > y1) ? y1 : y2);
 
                g_io_channel_write_chars(channel, rectangle_info, -1, NULL, NULL);
-               g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
+               g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
 
                g_free(rectangle_info);
                }
@@ -787,7 +808,7 @@ static void gr_render_intent(const gchar *text, GIOChannel *channel, gpointer da
                }
 
        g_io_channel_write_chars(channel, render_intent, -1, NULL, NULL);
-       g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
+       g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
 
        g_free(render_intent);
 }
@@ -800,6 +821,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)
                {
@@ -814,14 +836,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)
@@ -872,7 +897,7 @@ static void get_filelist(const gchar *text, GIOChannel *channel, gboolean recurs
                }
 
        g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
-       g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
+       g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
 
        g_string_free(out_string, TRUE);
        filelist_free(list);
@@ -893,7 +918,7 @@ static void gr_collection(const gchar *text, GIOChannel *channel, gpointer data)
                }
 
        g_io_channel_write_chars(channel, contents->str, -1, NULL, NULL);
-       g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
+       g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
 
        g_string_free(contents, TRUE);
 }
@@ -918,12 +943,55 @@ static void gr_collection_list(const gchar *text, GIOChannel *channel, gpointer
                }
 
        g_io_channel_write_chars(channel, out_string->str, -1, NULL, NULL);
-       g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
+       g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
 
        string_list_free(collection_list);
        g_string_free(out_string, TRUE);
 }
 
+static gboolean wait_cb(const gpointer data)
+{
+       gint position = GPOINTER_TO_INT(data);
+       gint x = position >> 16;
+       gint y = position - (x << 16);
+
+       gtk_window_move(GTK_WINDOW(lw_id->window), x, y);
+
+       return FALSE;
+}
+
+static void gr_geometry(const gchar *text, GIOChannel *channel, gpointer data)
+{
+       gchar **geometry;
+
+       if (!layout_valid(&lw_id) || !text)
+               {
+               return;
+               }
+
+       if (text[0] == '+')
+               {
+               geometry = g_strsplit_set(text, "+", 3);
+               if (geometry[1] != NULL && geometry[2] != NULL )
+                       {
+                       gtk_window_move(GTK_WINDOW(lw_id->window), atoi(geometry[1]), atoi(geometry[2]));
+                       }
+               }
+       else
+               {
+               geometry = g_strsplit_set(text, "+x", 4);
+               if (geometry[0] != NULL && geometry[1] != NULL)
+                       {
+                       gtk_window_resize(GTK_WINDOW(lw_id->window), atoi(geometry[0]), atoi(geometry[1]));
+                       }
+               if (geometry[2] != NULL && geometry[3] != NULL)
+                       {
+                       /* There is an occasional problem with a window_move immediately after a window_resize */
+                       g_idle_add(wait_cb, GINT_TO_POINTER((atoi(geometry[2]) << 16) + atoi(geometry[3])));
+                       }
+               }
+       g_strfreev(geometry);
+}
 
 static void gr_filelist(const gchar *text, GIOChannel *channel, gpointer data)
 {
@@ -953,13 +1021,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, "\n", -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)
@@ -992,47 +1064,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, "\n", -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);
@@ -1055,7 +1178,7 @@ static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer dat
        if (fd->parent) fd = fd->parent;
 
        g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
-       g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
+       g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
 
        work = fd->sidecar_files;
 
@@ -1064,7 +1187,7 @@ static void gr_get_sidecars(const gchar *text, GIOChannel *channel, gpointer dat
                fd = work->data;
                work = work->next;
                g_io_channel_write_chars(channel, fd->path, -1, NULL, NULL);
-               g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
+               g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
                }
        g_free(filename);
 }
@@ -1077,7 +1200,7 @@ static void gr_get_destination(const gchar *text, GIOChannel *channel, gpointer
        if (fd->change && fd->change->dest)
                {
                g_io_channel_write_chars(channel, fd->change->dest, -1, NULL, NULL);
-               g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
+               g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
                }
        g_free(filename);
 }
@@ -1130,15 +1253,12 @@ 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));
                }
 }
 
 static void gr_raise(const gchar *text, GIOChannel *channel, gpointer data)
 {
-       LayoutWindow *lw = NULL;
-
        if (layout_valid(&lw_id))
                {
                gtk_window_present(GTK_WINDOW(lw_id->window));
@@ -1149,8 +1269,21 @@ 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)
+{
+       g_io_channel_write_chars(channel, "print0", -1, NULL, NULL);
+       g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
 }
 
 #ifdef HAVE_LUA
@@ -1179,7 +1312,7 @@ static void gr_lua(const gchar *text, GIOChannel *channel, gpointer data)
                g_io_channel_write_chars(channel, N_("lua error: no data"), -1, NULL, NULL);
                }
 
-       g_io_channel_write_chars(channel, "\n", -1, NULL, NULL);
+       g_io_channel_write_chars(channel, "<gq_end_of_command>", -1, NULL, NULL);
 
        g_strfreev(lua_command);
        g_free(result);
@@ -1214,13 +1347,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") },
@@ -1239,6 +1372,7 @@ static RemoteCommandEntry remote_commands[] = {
        { NULL, "--id:",                gr_lw_id,               TRUE, FALSE, N_("<ID>"), N_("window id for following commands") },
        { NULL, "--new-window",         gr_new_window,          FALSE, FALSE, NULL, N_("new window") },
        { NULL, "--close-window",       gr_close_window,        FALSE, FALSE, NULL, N_("close window") },
+       { NULL, "--geometry=",          gr_geometry,            TRUE, FALSE, N_("<GEOMETRY>"), N_("set window geometry") },
        { "-ct:", "--cache-thumbs:",    gr_cache_thumb,         TRUE, FALSE, N_("clear|clean"), N_("clear or clean thumbnail cache") },
        { "-cs:", "--cache-shared:",    gr_cache_shared,        TRUE, FALSE, N_("clear|clean"), N_("clear or clean shared thumbnail cache") },
        { "-cm","--cache-metadata",      gr_cache_metadata,               FALSE, FALSE, NULL, N_("    clean the metadata cache") },
@@ -1250,6 +1384,7 @@ static RemoteCommandEntry remote_commands[] = {
        { NULL, "--lua:",               gr_lua,                 TRUE, FALSE, N_("<FILE>,<lua script>"), N_("run lua script on FILE") },
 #endif
        { NULL, "--PWD:",               gr_pwd,                 TRUE, FALSE, N_("<PWD>"), N_("use PWD as working directory for following commands") },
+       { NULL, "--print0",             gr_print0,              TRUE, FALSE, NULL, N_("terminate returned data with null character instead of newline") },
        { NULL, NULL, NULL, FALSE, FALSE, NULL, NULL }
 };
 
@@ -1350,7 +1485,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]);
                        }
@@ -1399,6 +1534,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);