[Nautilus-list] [PATCH] Use GConf for background settings



The included patch reads desktop settings from GConf, and updates the
desktop when these settings change. This is (an attempt) at a straight
port, so it doesn't do any more error checking than the original code
did. It will crash on bad settings for colors or file name.
It doesn't remove methods that are left unused, I'll send a new patch
for that if this patch is accepted. 

Please pay special attention to memory management with this patch, as
I'm not sure that I've yet found the pattern on when to free memory.
(Forgive me, I've mostly used Java the last years. Does anyone have a
pointer to a "Memory management in C/GTK+ for complete duckheads"
reference ;)

Also note that this patch uses the GConf keys described in
libgnome/schemas/desktop_gnome_background.schemas, while the current
background capplet doesn't, so to test changing desktop settings you
have to use gconftool.

-- 
   Håvard

mailto:havardw stud ntnu no||http://www.stud.ntnu.no/~havardw||73 52 55
76
All it takes to start an avalanche is a single snowflake||Or a
snowboarder
        Oh! Un Fraggle! Regarde, maman! J'ai attrapé un Fraggle!
? gconf-background.patch
? nautilus-1st.patch
? libnautilus-private/nautilus-directory-background.verbose.c
Index: libnautilus-private/nautilus-directory-background.c
===================================================================
RCS file: /cvs/gnome/nautilus/libnautilus-private/nautilus-directory-background.c,v
retrieving revision 1.69
diff -u -c -r1.69 nautilus-directory-background.c
*** libnautilus-private/nautilus-directory-background.c	2002/01/18 15:10:34	1.69
--- libnautilus-private/nautilus-directory-background.c	2002/01/25 22:59:52
***************
*** 63,68 ****
--- 63,86 ----
  			const char* image, const char* default_image,
  			EelBackgroundImagePlacement placement, EelBackgroundImagePlacement default_placement);
  
+ void nautilus_desktop_color_changed_callback (GConfClient* client,
+                                                guint cnxn_id,
+                                                GConfEntry *entry,
+                                                gpointer user_data);
+ 
+ void nautilus_desktop_picture_options_changed_callback (GConfClient* client,
+                                                         guint cnxn_id,
+                                                         GConfEntry *entry,
+                                                         gpointer user_data);
+ 
+ void nautilus_desktop_picture_filename_changed_callback (GConfClient* client,
+                                                          guint cnxn_id,
+                                                          GConfEntry *entry,
+                                                          gpointer user_data);
+ 
+ gboolean nautilus_desktop_parse_image_placement (const char *spec, 
+                                                  EelBackgroundImagePlacement *placement);
+ 
  static void
  desktop_background_realized (NautilusIconContainer *icon_container, void *disconnect_signal)
  {
***************
*** 747,755 ****
  	color = eel_background_get_color (background);
  	image = eel_background_get_image_uri (background);
  
! 	if (background_is_desktop (background)) {
! 		nautilus_file_background_write_desktop_settings (color, image, eel_background_get_image_placement (background));
! 	} else {
  	        /* Block the other handler while we are writing metadata so it doesn't
  	         * try to change the background.
  	         */
--- 765,771 ----
  	color = eel_background_get_color (background);
  	image = eel_background_get_image_uri (background);
  
! 	if (!background_is_desktop (background)) {
  	        /* Block the other handler while we are writing metadata so it doesn't
  	         * try to change the background.
  	         */
***************
*** 1003,1006 ****
--- 1019,1292 ----
          file = nautilus_file_get (uri);
          nautilus_connect_background_to_file_metadata (widget, file);
          nautilus_file_unref (file);
+ }
+ 
+ 
+ 
+ void
+ nautilus_desktop_color_changed_callback (GConfClient* client,
+                                          guint cnxn_id,
+                                          GConfEntry *entry,
+                                          gpointer user_data) {
+ 
+         EelBackground* background;
+         char *primary_color;
+         char *secondary_color;
+         char *shading_type;
+         char *gradient;
+ 
+ 
+         g_assert (NAUTILUS_IS_ICON_CONTAINER (user_data));
+         background = eel_get_widget_background (GTK_WIDGET (user_data));
+ 
+ 
+         if (strcmp (entry->key, "/desktop/gnome/background/primary_color") == 0) {
+                 primary_color = g_strdup (gconf_value_get_string (entry->value));
+         }
+         else {
+                 primary_color = eel_gconf_get_string ("/desktop/gnome/background/primary_color");
+         }
+         if (strcmp (entry->key, "/desktop/gnome/background/color_shading_type") == 0) {
+                 shading_type = g_strdup (gconf_value_get_string (entry->value));
+         }
+         else {
+                 shading_type = eel_gconf_get_string ("/desktop/gnome/background/color_shading_type");
+         }
+         if (strcmp (entry->key, "/desktop/gnome/background/secondary_color") == 0) {
+                 secondary_color = g_strdup (gconf_value_get_string (entry->value));
+         }
+         else {
+                 secondary_color = eel_gconf_get_string ("/desktop/gnome/background/secondary_color");
+         }
+ 
+ 
+         g_message ("Values: %s, %s, %s\n", primary_color, secondary_color, shading_type);
+         if (strcmp (shading_type, "solid") == 0) {
+                 eel_background_set_color (background, primary_color);                
+         }
+         else if (strcmp (shading_type, "vertical-gradient") == 0) {
+                 gradient = eel_gradient_new (primary_color, secondary_color, FALSE);
+                 eel_background_set_color (background, gradient);
+                 g_free (gradient);
+         }
+         else if (strcmp (shading_type, "horizontal-gradient") == 0) {
+                 gradient = eel_gradient_new (primary_color, secondary_color, TRUE);
+                 eel_background_set_color (background, gradient);
+                 g_free (gradient);
+         }
+         
+         g_free (primary_color);
+         g_free (secondary_color);
+         g_free (shading_type);
+ }
+         
+ 
+ void
+ nautilus_desktop_picture_options_changed_callback (GConfClient* client,
+                                                    guint cnxn_id,
+                                                    GConfEntry *entry,
+                                                    gpointer user_data) {
+         const char *image_type;
+         char *image_name;
+         EelBackground *background;
+         EelBackgroundImagePlacement placement;        
+ 
+         g_assert (NAUTILUS_IS_ICON_CONTAINER(user_data));
+         background = eel_get_widget_background (GTK_WIDGET (user_data));
+         image_type = gconf_value_get_string (entry->value);
+         image_name = eel_background_get_image_uri (background);
+ 
+         if (strcmp (image_type, "none") == 0) {
+                 if (image_name != NULL) {
+                         eel_background_set_image_uri(background, NULL);
+                 }
+         }
+         else if (nautilus_desktop_parse_image_placement (image_type, &placement)) {
+                 if (image_name == NULL) {
+                         image_name = eel_gconf_get_string ("/desktop/gnome/background/picture_filename");
+                         eel_background_set_image_uri_sync (background, image_name);
+                         g_free (image_name);
+                 }
+                 eel_background_set_image_placement (background, placement);
+         }
+ }
+ 
+ 
+ void 
+ nautilus_desktop_picture_filename_changed_callback (GConfClient* client,
+                                                     guint cnxn_id,
+                                                     GConfEntry *entry,
+                                                     gpointer user_data) {
+ 
+         char *image_type;
+         const char *image_name;
+         EelBackground *background;
+ 
+         g_assert (NAUTILUS_IS_ICON_CONTAINER(user_data));
+         background = eel_get_widget_background (GTK_WIDGET (user_data));
+ 
+         image_type = eel_gconf_get_string ("/desktop/gnome/background/picture_options");
+         if (strcmp (image_type, "none") == 0) {
+                 g_free (image_type);
+                 return;
+         }
+         g_free (image_type);
+         
+         image_name = gconf_value_get_string(entry->value);
+         eel_background_set_image_uri_sync (background, image_name);
+ }
+ 
+ void
+ nautilus_desktop_set_background (NautilusIconContainer *icon_container,
+                                  NautilusFile *file)
+ {
+ 	EelBackground *background;
+         EelBackgroundImagePlacement placement;
+         char *primary_color;
+         char *secondary_color;
+         char *color_type;
+         char *gradient;
+         char *image_name;
+         char *image_type;
+ 
+ 
+ 	background = eel_get_widget_background (GTK_WIDGET (icon_container));
+ 
+ 
+         /* Make sure this is recognized as the desktop view */
+         g_object_set_data (G_OBJECT (background), "theme_source", (gpointer) desktop_theme_source);
+ 
+ 
+         /* Set up callbacks for EelBackground, so that the background is 
+            painted when we change settings later. */
+ 
+         g_signal_connect (background,
+                           "settings_changed",
+                           G_CALLBACK (background_changed_callback),
+                           file);
+         g_signal_connect (background,
+                           "destroy",
+                           G_CALLBACK (background_destroyed_callback),
+                           file);
+         /* FIXME: Ability to reset desktop background to Nautilus
+            theme specification should probably be removed. */
+         g_signal_connect (background,
+                           "reset",
+                           G_CALLBACK (background_reset_callback),
+                           file);
+ 
+ 
+ 
+         /* Attach the new directory. */
+         nautilus_file_ref (file);
+         g_object_set_data_full (G_OBJECT (background),
+                                 "eel_background_file",
+                                 file,
+                                 (GtkDestroyNotify) nautilus_file_unref);
+ 
+         /* Block signals for synchronized update */
+         gtk_signal_handler_block_by_func (GTK_OBJECT (background),
+                                           G_CALLBACK (background_changed_callback),
+                                           file);
+ 
+         
+         /* Set image from GConf */
+         image_type = eel_gconf_get_string ("/desktop/gnome/background/picture_options");
+ 
+         if (strcmp (image_type, "none") != 0) {
+                 image_name = eel_gconf_get_string ("/desktop/gnome/background/picture_filename");
+                 
+                 if (nautilus_desktop_parse_image_placement(image_type, &placement)) {
+                         eel_background_set_image_placement (background, placement);
+                         eel_background_set_image_uri_sync (background, image_name);
+                 }
+                 g_free (image_name);
+         }
+ 
+ 
+         /* Set colors from GConf */
+         primary_color = eel_gconf_get_string ("/desktop/gnome/background/primary_color");
+         color_type = eel_gconf_get_string ("/desktop/gnome/background/color_shading_type");
+         if (strcmp (color_type, "solid") == 0) {
+                 eel_background_set_color (background, primary_color);
+         }
+         else {
+                 secondary_color = eel_gconf_get_string ("/desktop/gnome/background/secondary_color");
+                 if (strcmp (color_type, "horizontal-gradient") == 0) {
+                         gradient = eel_gradient_new (primary_color, secondary_color, TRUE);
+                         eel_background_set_color (background, gradient);
+                         g_free (gradient);                                                  
+                 }
+                 else if (strcmp (color_type, "vertical-gradient") == 0) {
+                         gradient = eel_gradient_new (primary_color, secondary_color, FALSE);
+                         eel_background_set_color (background, gradient);                        
+                         g_free (gradient);
+                 }
+                 else {
+                         /* Unrecognized  value for color_type, default to solid */
+                         eel_background_set_color (background, primary_color);
+                 }
+                 g_free (secondary_color);
+         }
+ 
+ 	
+ 	/* Unblock the handler, so updates wil take effect. */
+ 	gtk_signal_handler_unblock_by_func (GTK_OBJECT (background),
+                                             G_CALLBACK (background_changed_callback),
+                                             file);
+ 	
+         /** Set up callbacks for GConf changes. */
+         eel_gconf_monitor_add ("/desktop/gnome/background");
+         eel_gconf_notification_add ("/desktop/gnome/background/primary_color", 
+                                     nautilus_desktop_color_changed_callback,
+                                     icon_container);
+         eel_gconf_notification_add ("/desktop/gnome/background/secondary_color", 
+                                     nautilus_desktop_color_changed_callback,
+                                     icon_container);
+         eel_gconf_notification_add ("/desktop/gnome/background/color_shading_type", 
+                                     nautilus_desktop_color_changed_callback,
+                                     icon_container);
+         eel_gconf_notification_add ("/desktop/gnome/background/picture_options", 
+                                     nautilus_desktop_picture_options_changed_callback,
+                                     icon_container);
+         eel_gconf_notification_add ("/desktop/gnome/background/picture_filename", 
+                                     nautilus_desktop_picture_filename_changed_callback,
+                                     icon_container);
+ 
+ 
+ 	if (GTK_WIDGET_REALIZED (icon_container)) {
+ 		desktop_background_realized (icon_container, GINT_TO_POINTER (FALSE));
+ 	} else {
+ 		g_signal_connect (icon_container, "realize", G_CALLBACK (desktop_background_realized), GINT_TO_POINTER (TRUE));
+ 	}
+ 
+ 	nautilus_file_background_receive_root_window_changes (background); 
+ 
+         g_free (primary_color);
+         g_free (color_type);
+         g_free (image_type);
+ }
+ 
+ gboolean
+ nautilus_desktop_parse_image_placement (const char *spec, EelBackgroundImagePlacement *placement) {
+ 
+         if (strcmp (spec, "wallpaper") == 0) {
+                 *placement = EEL_BACKGROUND_TILED;
+                 return TRUE;
+         }
+         else if (strcmp (spec, "centered") == 0) {
+                 *placement = EEL_BACKGROUND_CENTERED;
+                 return TRUE;
+         }
+         else if (strcmp (spec, "scaled") == 0) {
+                 *placement = EEL_BACKGROUND_SCALED_ASPECT;
+                 return TRUE;
+         }
+         else if (strcmp (spec, "stretched") == 0) {
+                 *placement = EEL_BACKGROUND_SCALED;
+                 return TRUE;
+         }
+         else {
+                 return FALSE;
+         }
  }
Index: libnautilus-private/nautilus-directory-background.h
===================================================================
RCS file: /cvs/gnome/nautilus/libnautilus-private/nautilus-directory-background.h,v
retrieving revision 1.9
diff -u -c -r1.9 nautilus-directory-background.h
*** libnautilus-private/nautilus-directory-background.h	2001/09/15 19:17:45	1.9
--- libnautilus-private/nautilus-directory-background.h	2002/01/25 22:59:52
***************
*** 33,38 ****
--- 33,40 ----
                                                             NautilusFile      *file);
  void nautilus_connect_desktop_background_to_file_metadata (NautilusIconContainer *icon_container,
                                                             NautilusFile		 *file);
+ void nautilus_desktop_set_background (NautilusIconContainer *icon_container,
+                                       NautilusFile *file);
  void nautilus_connect_background_to_file_metadata_by_uri  (GtkWidget         *widget,
                                                             const char        *uri);
  gboolean nautilus_file_background_is_set		  (EelBackground *background);
Index: src/file-manager/fm-icon-view.c
===================================================================
RCS file: /cvs/gnome/nautilus/src/file-manager/fm-icon-view.c,v
retrieving revision 1.236
diff -u -c -r1.236 fm-icon-view.c
*** src/file-manager/fm-icon-view.c	2002/01/17 21:24:12	1.236
--- src/file-manager/fm-icon-view.c	2002/01/25 23:00:10
***************
*** 940,946 ****
  	 * of hardcoding desktop knowledge in here.
  	 */
  	if (FM_IS_DESKTOP_ICON_VIEW (view)) {
! 		nautilus_connect_desktop_background_to_file_metadata (NAUTILUS_ICON_CONTAINER (icon_container), file);
  	} else {
  		nautilus_connect_background_to_file_metadata (icon_container, file);
  	}
--- 940,946 ----
  	 * of hardcoding desktop knowledge in here.
  	 */
  	if (FM_IS_DESKTOP_ICON_VIEW (view)) {
! 		nautilus_desktop_set_background (NAUTILUS_ICON_CONTAINER (icon_container), file);
  	} else {
  		nautilus_connect_background_to_file_metadata (icon_container, file);
  	}


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]