added possibility to update existing layout window from config
authorVladimir Nadvornik <nadvornik@suse.cz>
Fri, 20 Mar 2009 14:36:59 +0000 (14:36 +0000)
committerVladimir Nadvornik <nadvornik@suse.cz>
Fri, 20 Mar 2009 14:36:59 +0000 (14:36 +0000)
src/layout.c
src/layout.h
src/options.c
src/rcfile.c
src/typedefs.h

index 93fb021..c6725fe 100644 (file)
@@ -114,6 +114,52 @@ LayoutWindow *layout_find_by_image_fd(ImageWindow *imd)
        return NULL;
 }
 
+LayoutWindow *layout_find_by_layout_id(const gchar *id)
+{
+       GList *work;
+
+       if (!id || !id[0]) return NULL;
+       work = layout_window_list;
+       while (work)
+               {
+               LayoutWindow *lw = work->data;
+               work = work->next;
+
+               if (lw->options.id && strcmp(id, lw->options.id) == 0)
+                       return lw;
+               }
+
+       return NULL;
+}
+
+static void layout_set_unique_id(LayoutWindow *lw)
+{
+       char id[10];
+       gint i;
+       if (lw->options.id && lw->options.id[0]) return; /* id is already set */
+       
+       g_free(lw->options.id);
+       lw->options.id = NULL;
+       
+       if (!layout_find_by_layout_id("main"))
+               {
+               lw->options.id = g_strdup("main");
+               return;
+               }
+       
+       i = 1;
+       while (TRUE)
+               {
+               g_snprintf(id, sizeof(id), "lw%d", i);
+               if (!layout_find_by_layout_id(id))
+                       {
+                       lw->options.id = g_strdup(id);
+                       return;
+                       }
+               i++;
+               }
+}
+
 /*
  *-----------------------------------------------------------------------------
  * menu, toolbar, and dir view
@@ -2210,6 +2256,7 @@ LayoutWindow *layout_new_with_geometry(FileData *dir_fd, LayoutOptions *lop,
        lw->sort_method = SORT_NAME;
        lw->sort_ascend = TRUE;
 
+       layout_set_unique_id(lw);
 //     lw->options.tools_float = popped;
 //     lw->options.tools_hidden = hidden;
 //     lw->bar_sort_enabled = options->panels.sort.enabled;
@@ -2328,6 +2375,8 @@ LayoutWindow *layout_new_with_geometry(FileData *dir_fd, LayoutOptions *lop,
 
 void layout_write_attributes(LayoutOptions *layout, GString *outstr, gint indent)
 {
+       WRITE_NL(); WRITE_CHAR(*layout, id);
+
        WRITE_NL(); WRITE_INT(*layout, style);
        WRITE_NL(); WRITE_CHAR(*layout, order);
        WRITE_NL(); WRITE_UINT(*layout, dir_view_type);
@@ -2395,6 +2444,7 @@ void layout_load_attributes(LayoutOptions *layout, const gchar **attribute_names
                const gchar *value = *attribute_values++;
 
                /* layout options */
+               if (READ_CHAR(*layout, id)) continue;
 
                if (READ_INT(*layout, style)) continue;
                if (READ_CHAR(*layout, order)) continue;
@@ -2509,5 +2559,18 @@ LayoutWindow *layout_new_from_config(const gchar **attribute_names, const gchar
        return lw;
 }
 
+void layout_update_from_config(LayoutWindow *lw, const gchar **attribute_names, const gchar **attribute_values)
+{
+       LayoutOptions lop;
+       
+       init_layout_options(&lop);
+
+       if (attribute_names) layout_load_attributes(&lop, attribute_names, attribute_values);
+
+       layout_apply_options(lw, &lop);
+               
+       free_layout_options_content(&lop);
+}
+
 
 /* vim: set shiftwidth=8 softtabstop=0 cindent cinoptions={1s: */
index 08870b9..6b1e1e0 100644 (file)
@@ -21,6 +21,7 @@ LayoutWindow *layout_new(FileData *dir_fd, LayoutOptions *lop);
 LayoutWindow *layout_new_with_geometry(FileData *dir_fd, LayoutOptions *lop,
                                       const gchar *geometry);
 LayoutWindow *layout_new_from_config(const gchar **attribute_names, const gchar **attribute_values, gboolean use_commandline);
+void layout_update_from_config(LayoutWindow *lw, const gchar **attribute_names, const gchar **attribute_values);
 
 void layout_close(LayoutWindow *lw);
 void layout_free(LayoutWindow *lw);
@@ -39,6 +40,8 @@ void layout_write_config(LayoutWindow *lw, GString *outstr, gint indent);
 
 LayoutWindow *layout_find_by_image(ImageWindow *imd);
 LayoutWindow *layout_find_by_image_fd(ImageWindow *imd);
+LayoutWindow *layout_find_by_layout_id(const gchar *id);
+
 
 const gchar *layout_get_path(LayoutWindow *lw);
 gboolean layout_set_path(LayoutWindow *lw, const gchar *path);
index a5e416a..33030f3 100644 (file)
@@ -170,14 +170,16 @@ void copy_layout_options(LayoutOptions *dest, const LayoutOptions *src)
        free_layout_options_content(dest);
        
        *dest = *src;
+       dest->id = g_strdup(src->id);
        dest->order = g_strdup(src->order);
        dest->home_path = g_strdup(src->home_path);
 }
 
 void free_layout_options_content(LayoutOptions *dest)
 {
-       if (dest->order) g_free(dest->order);
-       if (dest->home_path) g_free(dest->home_path);
+       g_free(dest->id);
+       g_free(dest->order);
+       g_free(dest->home_path);
 }
 
 LayoutOptions *init_layout_options(LayoutOptions *options)
index cdc6626..eb9b22d 100644 (file)
@@ -777,6 +777,19 @@ struct _GQParserData
        gboolean startup; /* reading config for the first time - add commandline and defaults */
 };
 
+static const gchar *options_get_id(const gchar **attribute_names, const gchar **attribute_values)
+{
+       while (*attribute_names)
+               {
+               const gchar *option = *attribute_names++;
+               const gchar *value = *attribute_values++;
+               
+               if (strcmp(option, "id") == 0) return value;
+
+               }
+       return NULL;
+}
+
 
 void options_parse_leaf(GQParserData *parser_data, GMarkupParseContext *context, const gchar *element_name, const gchar **attribute_names, const gchar **attribute_values, gpointer data, GError **error)
 {
@@ -1001,7 +1014,15 @@ static void options_parse_toplevel(GQParserData *parser_data, GMarkupParseContex
        if (g_ascii_strcasecmp(element_name, "layout") == 0)
                {
                LayoutWindow *lw;
-               lw = layout_new_from_config(attribute_names, attribute_values, parser_data->startup);
+               lw = layout_find_by_layout_id(options_get_id(attribute_names, attribute_values));
+               if (lw) 
+                       {
+                       layout_update_from_config(lw, attribute_names, attribute_values);
+                       }
+               else
+                       {
+                       lw = layout_new_from_config(attribute_names, attribute_values, parser_data->startup);
+                       }
                options_parse_func_push(parser_data, options_parse_layout, options_parse_layout_end, lw);
                }
        else
index 7d9726e..9d684df 100644 (file)
@@ -481,6 +481,8 @@ struct _FileData {
 
 struct _LayoutOptions
 {
+       gchar *id;
+
        gchar *order;
        gint style;