summaryrefslogtreecommitdiff
path: root/libnautilus-private/nautilus-directory-background.c
blob: 439bb347e301e9c84cade31a11b028c0cc0afc40 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */

/*
   nautilus-directory-background.c: Helper for the background of a widget
                                    that is viewing a particular location.
 
   Copyright (C) 2000 Eazel, Inc.
  
   This program is free software; you can redistribute it and/or
   modify it under the terms of the GNU General Public License as
   published by the Free Software Foundation; either version 2 of the
   License, or (at your option) any later version.
  
   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   General Public License for more details.
  
   You should have received a copy of the GNU General Public
   License along with this program; if not, write to the
   Free Software Foundation, Inc., 59 Temple Place - Suite 330,
   Boston, MA 02111-1307, USA.
  
   Author: Darin Adler <darin@bentspoon.com>
*/

#include <config.h>
#include "nautilus-directory-background.h"

#include <eel/eel-gdk-extensions.h>
#include <eel/eel-background.h>
#include "nautilus-global-preferences.h"
#include "nautilus-metadata.h"
#include "nautilus-file-attributes.h"
#include <eel/eel-string.h>
#include "nautilus-theme.h"
#include <X11/Xatom.h>
#include <gdk/gdkx.h>
#include <gtk/gtkmain.h>
#include <gtk/gtksignal.h>
#include <libgnome/gnome-config.h>
#include <libgnome/gnome-util.h>
#include <libgnomevfs/gnome-vfs-utils.h>

/* FIXME: Is there a better way to do this? */
extern char *_gdk_display_name;

static void background_changed_callback     (EelBackground *background, 
                                             NautilusFile       *file);
static void background_reset_callback       (EelBackground *background, 
                                             NautilusFile       *file);

static void saved_settings_changed_callback (NautilusFile       *file, 
                                             EelBackground *background);
                                         
static void nautilus_file_background_receive_root_window_changes (EelBackground *background);

static void nautilus_file_update_desktop_pixmaps (EelBackground *background);

static void nautilus_file_background_write_desktop_settings (char *color,
							     char *image,
							     EelBackgroundImagePlacement placement);

static gboolean nautilus_file_background_matches_default_settings (
			const char* color, const char* default_color,
			const char* image, const char* default_image,
			EelBackgroundImagePlacement placement, EelBackgroundImagePlacement default_placement);

static void
desktop_background_realized (NautilusIconContainer *icon_container, void *disconnect_signal)
{
	EelBackground *background;

        if ((gboolean) GPOINTER_TO_INT (disconnect_signal)) {
		gtk_signal_disconnect_by_func (GTK_OBJECT (icon_container),
					       G_CALLBACK (desktop_background_realized),
					       disconnect_signal);
	}

	background = eel_get_widget_background (GTK_WIDGET (icon_container));

	g_object_set_data (G_OBJECT (background), "icon_container", (gpointer) icon_container);

	nautilus_file_update_desktop_pixmaps (background);
}

static const char *default_theme_source = "directory";
static const char *desktop_theme_source = "desktop";

void
nautilus_connect_desktop_background_to_file_metadata (NautilusIconContainer *icon_container,
                                                      NautilusFile *file)
{
	EelBackground *background;

	background = eel_get_widget_background (GTK_WIDGET (icon_container));

	g_object_set_data (G_OBJECT (background), "theme_source", (gpointer) desktop_theme_source);

	/* Strictly speaking, we don't need to know about metadata changes, since
	 * the desktop setting aren't stored there. But, hooking up to metadata
	 * changes is actually a small part of what this fn does, and we do need
	 * the other stuff (hooked up to background & theme changes, initialize
	 * the background). Being notified of metadata changes on the file is a
	 * waste, but won't hurt, so I don't think it's worth refactoring the fn
	 * at this point.
	 */
	nautilus_connect_background_to_file_metadata (GTK_WIDGET (icon_container), file);

	if (GTK_WIDGET_REALIZED (icon_container)) {
		desktop_background_realized (icon_container, GINT_TO_POINTER (FALSE));
	} else {
		g_signal_connect (G_OBJECT (icon_container), "realize", G_CALLBACK (desktop_background_realized), GINT_TO_POINTER (TRUE));
	}

	nautilus_file_background_receive_root_window_changes (background); 
}

static gboolean
background_is_desktop (EelBackground *background)
{
	/* == works because we're carful to always use the same string.
	 */
	return g_object_get_data (G_OBJECT (background), "theme_source") == desktop_theme_source;
}

static const char *nautilus_file_background_peek_theme_source (EelBackground *background)
{
	char *theme_source;

	theme_source = g_object_get_data (G_OBJECT (background), "theme_source");

	return theme_source != NULL ? theme_source : default_theme_source;
}

static GdkWindow *
background_get_desktop_background_window (EelBackground *background)
{
	gpointer layout;

	layout = g_object_get_data (G_OBJECT (background), "icon_container");
	return layout != NULL ? GTK_LAYOUT (layout)->bin_window : NULL;
}

/* utility routine to handle mapping local image files in themes to a uri */
static char*
theme_image_path_to_uri (char *image_file, const char *theme_name)
{
	char *image_path;
	char *image_uri;

	if (image_file != NULL && !eel_istr_has_prefix (image_file, "file://")) {
		
		if (eel_str_has_prefix (image_file, "./")) {
			image_path = nautilus_theme_get_image_path_from_theme (image_file + 2, theme_name);
		} else {
			image_path = g_strdup_printf ("%s/%s", NAUTILUS_DATADIR, image_file);
		}
		
		if (image_path && g_file_exists (image_path)) {
			image_uri = gnome_vfs_get_uri_from_local_path (image_path);
		} else {
			image_uri = NULL;
		}
		
		g_free (image_path);
	} else {
		image_uri = g_strdup (image_file);
	}

	return image_uri;
}
 
static void
nautilus_file_background_get_default_settings_for_theme (const char* theme_name,
							  const char* theme_source,
							  char **color,
							  char **image,
							  EelBackgroundImagePlacement *placement)
{
	char *image_local_path;

	if (placement != NULL) {
		*placement = EEL_BACKGROUND_TILED;
	}

	if (color != NULL) {
		*color = nautilus_theme_get_theme_data_from_theme (theme_source, NAUTILUS_METADATA_KEY_LOCATION_BACKGROUND_COLOR, theme_name);
	}

	if (image != NULL) {
		image_local_path = nautilus_theme_get_theme_data_from_theme (theme_source, NAUTILUS_METADATA_KEY_LOCATION_BACKGROUND_IMAGE, theme_name);
		*image = theme_image_path_to_uri (image_local_path, theme_name);
		g_free (image_local_path);
	}
}

static void
nautilus_file_background_get_default_settings (const char* theme_source,
                                               char **color,
                                               char **image,
                                               EelBackgroundImagePlacement *placement)
{
	char *theme_name;
	theme_name = nautilus_theme_get_theme ();
	nautilus_file_background_get_default_settings_for_theme
		(theme_name, theme_source, color, image, placement);
	g_free (theme_name);
}

static gboolean
eel_gnome_config_string_match_no_case_with_default (const char *path, const char *test_value, gboolean *was_default)
{
	char *value;
	gboolean result;
	value = gnome_config_get_string_with_default (path, was_default);
	result = !eel_strcasecmp (value, test_value);
	g_free (value);
	return result;
}

/* This enum is from gnome-source/control-center/capplets/background-properties/render-background.h */
enum {
	WALLPAPER_TILED,
	WALLPAPER_CENTERED,
	WALLPAPER_SCALED,
	WALLPAPER_SCALED_KEEP,
	WALLPAPER_EMBOSSED
};

static void
nautilus_file_background_read_desktop_settings (char **color,
                                                char **image,
                                                EelBackgroundImagePlacement *placement)
{
	int	 image_alignment;
	char*	 image_local_path;
	char*	 default_image_uri;
	gboolean no_alignment_set;
	gboolean no_image_set;
	EelBackgroundImagePlacement default_placement;
	
	char	*end_color;
	char	*start_color;
	char	*default_color;
	gboolean use_gradient;
	gboolean is_horizontal;
	gboolean no_start_color_set;
	gboolean no_end_color_set;
	gboolean no_gradient_set;
	gboolean no_gradient_orientation_set;

	char *theme_name;
	char *cur_theme_name;
	gboolean no_theme_name_set;
	gboolean switch_to_cur_theme_default;

	theme_name = gnome_config_get_string_with_default ("/Background/Default/nautilus_theme", &no_theme_name_set);

	if (no_theme_name_set) {
		nautilus_file_background_get_default_settings
			(desktop_theme_source, &default_color, &default_image_uri, &default_placement);
	} else {
		nautilus_file_background_get_default_settings_for_theme
			(theme_name, desktop_theme_source, &default_color, &default_image_uri, &default_placement);
	}

	image_local_path = gnome_config_get_string_with_default ("/Background/Default/wallpaper", &no_image_set);

	if (no_image_set) {
		*image = g_strdup (default_image_uri);
	} else if (eel_strcasecmp (image_local_path, "none") != 0) {
		*image = gnome_vfs_get_uri_from_local_path (image_local_path);
	} else {
		*image = NULL;
	}
	
	g_free(image_local_path);

	image_alignment  = gnome_config_get_int_with_default ("/Background/Default/wallpaperAlign", &no_alignment_set);

	if (no_alignment_set) {
		*placement = default_placement;
	} else {
		 switch (image_alignment) {
		 	default:
		 		g_assert_not_reached ();
			case WALLPAPER_EMBOSSED:
				/* FIXME bugzilla.gnome.org 42193: we don't support embossing.
				 * Just treat it as centered - ugh.
				 */
			case WALLPAPER_CENTERED:
				*placement = EEL_BACKGROUND_CENTERED;
				break;
			case WALLPAPER_TILED:
				*placement = EEL_BACKGROUND_TILED;
				break;
			case WALLPAPER_SCALED:
				*placement = EEL_BACKGROUND_SCALED;
				break;
			case WALLPAPER_SCALED_KEEP:
				*placement = EEL_BACKGROUND_SCALED_ASPECT;
				break;
		 }
	}

	end_color     = gnome_config_get_string_with_default ("/Background/Default/color2", &no_end_color_set);
	start_color   = gnome_config_get_string_with_default ("/Background/Default/color1", &no_start_color_set);
	use_gradient  = !eel_gnome_config_string_match_no_case_with_default ("/Background/Default/simple", "solid", &no_gradient_set);
	is_horizontal = !eel_gnome_config_string_match_no_case_with_default ("/Background/Default/gradient", "vertical", &no_gradient_orientation_set);

	if (no_gradient_set || no_gradient_orientation_set || no_start_color_set) {
		*color = g_strdup (default_color);
	} else if (use_gradient) {
		if (no_end_color_set) {
			*color = g_strdup (default_color);
		} else {
			*color = eel_gradient_new (start_color, end_color , is_horizontal);
		}
	} else {
		*color = g_strdup (start_color);
	}

	g_free(start_color);
	g_free(end_color);

	/* Since we share our settings with the background-capplet, we can't
	 * write the default values specially (e.g. by removing them entirely).
	 * 
	 * The best we can do is check to see if the settings match the default values
	 * for the theme name that's stored with them. If so, we assume it means use
	 * the default - i.e. the default from the current theme.
	 * 
	 *  - there must be a theme name stored with the settings
	 *  - if the stored theme name matches the current theme, then
	 *    we don't need to do anything since we're already using
	 *    the current theme's default values. 
	 * 
	 */
	cur_theme_name = nautilus_theme_get_theme ();

	switch_to_cur_theme_default = !no_theme_name_set &&
				      (eel_strcmp (theme_name, cur_theme_name) != 0) &&
				      nautilus_file_background_matches_default_settings
					(*color, default_color,
					 *image, default_image_uri,
					 *placement, default_placement);

	if (switch_to_cur_theme_default) {
		g_free (*color);
		g_free (*image);
		nautilus_file_background_get_default_settings (desktop_theme_source, color, image, placement);
	}

	if (switch_to_cur_theme_default || no_theme_name_set) {
		/* Writing out the actual settings for the current theme so that the
		 * background capplet will show the right settings.
		 */
		nautilus_file_background_write_desktop_settings (*color, *image, *placement);
	}

	g_free (theme_name);	
	g_free (cur_theme_name);	
	g_free(default_color);
	g_free(default_image_uri);
}

static void
nautilus_file_background_write_desktop_settings (char *color, char *image, EelBackgroundImagePlacement placement)
{
	char *end_color;
	char *start_color;
	char *image_local_path;
	char *theme_name;

	int wallpaper_align;

	int i;
	int wallpaper_count;
	char *wallpaper_path_i;
	char *wallpaper_config_path_i;
	gboolean found_wallpaper;

	if (color != NULL) {
		start_color = eel_gradient_get_start_color_spec (color);
		gnome_config_set_string ("/Background/Default/color1", start_color);		
		g_free (start_color);

		/* if color is not a gradient, this ends up writing same as start_color */
		end_color = eel_gradient_get_end_color_spec (color);
		gnome_config_set_string ("/Background/Default/color2", end_color);		
		g_free (end_color);

		gnome_config_set_string ("/Background/Default/simple", eel_gradient_is_gradient (color) ? "gradient" : "solid");
		gnome_config_set_string ("/Background/Default/gradient", eel_gradient_is_horizontal (color) ? "horizontal" : "vertical");
	} else {
		/* We set it to white here because that's how backgrounds with a NULL color
		 * are drawn by Nautilus - due to usage of eel_gdk_color_parse_with_white_default.
		 */
		gnome_config_set_string ("/Background/Default/color1", "rgb:FFFF/FFFF/FFFF");		
		gnome_config_set_string ("/Background/Default/color2", "rgb:FFFF/FFFF/FFFF");		
		gnome_config_set_string ("/Background/Default/simple", "solid");
		gnome_config_set_string ("/Background/Default/gradient", "vertical");
	}

	if (image != NULL) {
		image_local_path = gnome_vfs_get_local_path_from_uri (image);
		gnome_config_set_string ("/Background/Default/wallpaper", image_local_path);
		switch (placement) {
			case EEL_BACKGROUND_TILED:
				wallpaper_align = WALLPAPER_TILED;
				break;	
			case EEL_BACKGROUND_CENTERED:
				wallpaper_align = WALLPAPER_CENTERED;
				break;	
			case EEL_BACKGROUND_SCALED:
				wallpaper_align = WALLPAPER_SCALED;
				break;	
			case EEL_BACKGROUND_SCALED_ASPECT:
				wallpaper_align = WALLPAPER_SCALED_KEEP;
				break;
			default:
				g_assert_not_reached ();
				wallpaper_align = WALLPAPER_TILED;
				break;	
		}
		
		gnome_config_set_int ("/Background/Default/wallpaperAlign", wallpaper_align);

		wallpaper_count = gnome_config_get_int ("/Background/Default/wallpapers=0");
		found_wallpaper = FALSE;
		for (i = 1; i <= wallpaper_count && !found_wallpaper; ++i) {
			wallpaper_config_path_i = g_strdup_printf ("/Background/Default/wallpaper%d", i);
			wallpaper_path_i = gnome_config_get_string (wallpaper_config_path_i);
			g_free (wallpaper_config_path_i);
			if (eel_strcmp (wallpaper_path_i, image_local_path) == 0) {
				found_wallpaper = TRUE;
			}
			
			g_free (wallpaper_path_i);			
		}

		if (!found_wallpaper) {
			gnome_config_set_int ("/Background/Default/wallpapers", wallpaper_count + 1);
			gnome_config_set_string ("/Background/Default/wallpapers_dir", image_local_path);
			wallpaper_config_path_i = g_strdup_printf ("/Background/Default/wallpaper%d", wallpaper_count + 1);
			gnome_config_set_string (wallpaper_config_path_i, image_local_path);
			g_free (wallpaper_config_path_i);
		}
		
		g_free (image_local_path);		
	} else {
		gnome_config_set_string ("/Background/Default/wallpaper", "none");
	}

	theme_name = nautilus_theme_get_theme ();
	gnome_config_set_string ("/Background/Default/nautilus_theme", theme_name);
	g_free (theme_name);

	gnome_config_sync ();
}

static void
nautilus_file_background_write_desktop_default_settings ()
{
	char *color;
	char *image;
	EelBackgroundImagePlacement placement;
	nautilus_file_background_get_default_settings (desktop_theme_source, &color, &image, &placement);
	nautilus_file_background_write_desktop_settings (color, image, placement);
}

static int
call_settings_changed (EelBackground *background)
{
	NautilusFile *file;
	file = g_object_get_data (G_OBJECT (background), "eel_background_file");
	if (file) {
		saved_settings_changed_callback (file, background);
	}
	return FALSE;
}

/* We don't want to respond to our own changes to the root pixmap.
 * Since there's no way to determine the origin of the x-event (or mark it)
 * we use this variable determine if we think the next PropertyNotify is
 * due to us.
 */
static int set_root_pixmap_count = 0;

static GdkFilterReturn
nautilus_file_background_event_filter (GdkXEvent *gdk_xevent, GdkEvent *event, gpointer data)
{
	XEvent *xevent;
	EelBackground *background;

	xevent = (XEvent *) gdk_xevent;

	if (xevent->type == PropertyNotify && xevent->xproperty.atom == gdk_x11_get_xatom_by_name ("ESETROOT_PMAP_ID")) {

		/* If we caused it, ignore it.
		 */
		if (set_root_pixmap_count > 0) {
			--set_root_pixmap_count;
			return GDK_FILTER_CONTINUE;
		}
		
	    	background = EEL_BACKGROUND (data);
	    	/* FIXME bugzilla.gnome.org 42220:
	    	 * We'd like to call saved_settings_changed_callback right here, directly.
	    	 * However, the current version of the property-background capplet saves
	    	 * the new setting in gnome_config AFTER setting the root window's property -
	    	 * i.e. after we get this event. How long afterwards is not knowable - we
	    	 * guess half a second. Fixing this requires changing the capplet.
	    	 */
	    	gtk_timeout_add (500, (GtkFunction) (call_settings_changed), background);
	}

	return GDK_FILTER_CONTINUE;
}

static void
desktop_background_destroyed_callback (EelBackground *background, void *georgeWBush)
{
	gdk_window_remove_filter (GDK_ROOT_PARENT(), nautilus_file_background_event_filter, background);
}

static void
nautilus_file_background_receive_root_window_changes (EelBackground *background)
{
	XWindowAttributes attribs = { 0 };

	/* set up a filter on the root window to get notified about property changes */
	gdk_window_add_filter (GDK_ROOT_PARENT(), nautilus_file_background_event_filter, background);

	gdk_error_trap_push ();

	/* select events, we need to trap the kde status thingies anyway */
	XGetWindowAttributes (GDK_DISPLAY (), GDK_ROOT_WINDOW (), &attribs);
	XSelectInput (GDK_DISPLAY (), GDK_ROOT_WINDOW (), attribs.your_event_mask | PropertyChangeMask);
	
	gdk_flush ();
	gdk_error_trap_pop ();

	g_signal_connect (G_OBJECT (background),
 			    "destroy",
			    G_CALLBACK (desktop_background_destroyed_callback),
			    NULL);
}

/* Create a persistent pixmap. We create a separate display
 * and set the closedown mode on it to RetainPermanent
 * (copied from gnome-source/control-panels/capplets/background-properties/render-background.c)
 */
static GdkPixmap *
make_root_pixmap (gint width, gint height)
{
	Display *display;
	Pixmap result;

	gdk_flush ();

	display = XOpenDisplay (_gdk_display_name);

	XSetCloseDownMode (display, RetainPermanent);

	result = XCreatePixmap (display,
				DefaultRootWindow (display),
				width, height,
				DefaultDepthOfScreen (DefaultScreenOfDisplay (GDK_DISPLAY())));

	XCloseDisplay (display);

	return gdk_pixmap_foreign_new (result);
}

/* Set the root pixmap, and properties pointing to it. We
 * do this atomically with XGrabServer to make sure that
 * we won't leak the pixmap if somebody else it setting
 * it at the same time. (This assumes that they follow the
 * same conventions we do
 * (copied from gnome-source/control-panels/capplets/background-properties/render-background.c)
 */
static void 
set_root_pixmap (GdkPixmap *pixmap)
{
	int     result;
	gint    format;
	gulong  nitems;
	gulong  bytes_after;
	guchar *data_esetroot;
	Pixmap  pixmap_id;
	Atom type;

	data_esetroot = NULL;

	XGrabServer (GDK_DISPLAY());

	result = XGetWindowProperty (GDK_DISPLAY(), GDK_ROOT_WINDOW(),
				     gdk_x11_get_xatom_by_name ("ESETROOT_PMAP_ID"),
				     0L, 1L, False, XA_PIXMAP,
				     &type, &format, &nitems, &bytes_after,
				     &data_esetroot);

	if (data_esetroot != NULL) {
		if (result == Success && type == XA_PIXMAP && format == 32 && nitems == 1) {
			gdk_error_trap_push ();
			XKillClient(GDK_DISPLAY(), *(Pixmap *)data_esetroot);
			gdk_flush ();
			gdk_error_trap_pop ();
		}
		XFree (data_esetroot);
	}

	++set_root_pixmap_count;
	
	pixmap_id = GDK_WINDOW_XWINDOW (pixmap);

	XChangeProperty (GDK_DISPLAY(), GDK_ROOT_WINDOW(),
			 gdk_x11_get_xatom_by_name ("ESETROOT_PMAP_ID"), XA_PIXMAP,
			 32, PropModeReplace,
			 (guchar *) &pixmap_id, 1);
	XChangeProperty (GDK_DISPLAY(), GDK_ROOT_WINDOW(),
			 gdk_x11_get_xatom_by_name ("_XROOTPMAP_ID"), XA_PIXMAP,
			 32, PropModeReplace,
			 (guchar *) &pixmap_id, 1);

	XSetWindowBackgroundPixmap (GDK_DISPLAY (), GDK_ROOT_WINDOW (), pixmap_id);
	XClearWindow (GDK_DISPLAY (), GDK_ROOT_WINDOW ());

	XUngrabServer (GDK_DISPLAY());
	
	XFlush(GDK_DISPLAY());
}
	
static void
image_loading_done_callback (EelBackground *background, gboolean successful_load, void *disconnect_signal)
{
	int	      width;
	int	      height;
	GdkGC        *gc;
	GdkPixmap    *pixmap;
	GdkWindow    *background_window;

        if ((gboolean) GPOINTER_TO_INT (disconnect_signal)) {
		gtk_signal_disconnect_by_func (GTK_OBJECT (background),
					       G_CALLBACK (image_loading_done_callback),
					       disconnect_signal);
	}

	width  = gdk_screen_width ();
	height = gdk_screen_height ();

	pixmap = make_root_pixmap (width, height);
	gc = gdk_gc_new (pixmap);
	eel_background_draw_to_drawable (background, pixmap, gc, 0, 0, width, height, width, height);
	gdk_gc_unref (gc);

	set_root_pixmap (pixmap);

	background_window = background_get_desktop_background_window (background);
	if (background_window != NULL) {
		gdk_window_set_back_pixmap (background_window, pixmap, FALSE);
	}

        gdk_pixmap_unref (pixmap);
}

static void
nautilus_file_update_desktop_pixmaps (EelBackground *background)
{	
	if (eel_background_is_loaded (background)) {
		image_loading_done_callback (background, TRUE, GINT_TO_POINTER (FALSE));
	} else {
		g_signal_connect (G_OBJECT (background),
				    "image_loading_done",
				    G_CALLBACK (image_loading_done_callback),
				    GINT_TO_POINTER (TRUE));
	}
}

static gboolean
nautilus_file_background_matches_default_settings (
			const char* color, const char* default_color,
			const char* image, const char* default_image,
			EelBackgroundImagePlacement placement, EelBackgroundImagePlacement default_placement)
{
	gboolean match_color;
	gboolean match_image;

	/* A NULL default color or image is not considered when determining a match.
	 */
	
	match_color = (default_color == NULL) || eel_strcmp (color, default_color) == 0;
	
	match_image = (default_image == NULL) ||
		      ((eel_strcmp (image, default_image) == 0) && (placement == default_placement));
	return match_color && match_image;
}

/* return true if the background is not in the default state */
gboolean
nautilus_file_background_is_set (EelBackground *background)
{
	char *color;
	char *image;
	char *default_color;
	char *default_image;
	
	gboolean matches;
	
	EelBackgroundImagePlacement placement;
	EelBackgroundImagePlacement default_placement;

	color = eel_background_get_color (background);
	image = eel_background_get_image_uri (background);
	placement = eel_background_get_image_placement (background);
	
	nautilus_file_background_get_default_settings (
		nautilus_file_background_peek_theme_source (background),
		&default_color, &default_image, &default_placement);
		 			    
	matches = nautilus_file_background_matches_default_settings (color, default_color,
                                                                     image, default_image,
                                                                     placement, default_placement);
	
	g_free (color);
	g_free (image);
	g_free (default_color);
	g_free (default_image);
	
	return !matches;
}

/* handle the background changed signal */
static void
background_changed_callback (EelBackground *background,
                             NautilusFile       *file)
{
  	char *color;
  	char *image;
        
        g_assert (EEL_IS_BACKGROUND (background));
        g_assert (NAUTILUS_IS_FILE (file));
        g_assert (g_object_get_data (G_OBJECT (background), "eel_background_file") == file);
        

	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.
	         */
	        gtk_signal_handler_block_by_func (GTK_OBJECT (file),
	                                          G_CALLBACK (saved_settings_changed_callback),
	                                          background);

		nautilus_file_set_metadata (file,
                                            NAUTILUS_METADATA_KEY_LOCATION_BACKGROUND_COLOR,
                                            NULL,
                                            color);

		nautilus_file_set_metadata (file,
                                            NAUTILUS_METADATA_KEY_LOCATION_BACKGROUND_IMAGE,
                                            NULL,
                                            image);
						 
	        /* Unblock the handler. */
	        gtk_signal_handler_unblock_by_func (GTK_OBJECT (file),
	                                            G_CALLBACK (saved_settings_changed_callback),
	                                            background);
	}

	g_free (color);
	g_free (image);
	
	if (background_is_desktop (background)) {
		nautilus_file_update_desktop_pixmaps (background);
	}
}

static void
initialize_background_from_settings (NautilusFile *file,
				     EelBackground *background)
{
        char *color;
        char *image;
	EelBackgroundImagePlacement placement;
	
        g_assert (NAUTILUS_IS_FILE (file));
        g_assert (EEL_IS_BACKGROUND (background));
        g_assert (g_object_get_data (G_OBJECT (background), "eel_background_file")
                  == file);

	if (background_is_desktop (background)) {
		nautilus_file_background_read_desktop_settings (&color, &image, &placement);
	} else {
		color = nautilus_file_get_metadata (file,
                                                    NAUTILUS_METADATA_KEY_LOCATION_BACKGROUND_COLOR,
                                                    NULL);
		image = nautilus_file_get_metadata (file,
                                                    NAUTILUS_METADATA_KEY_LOCATION_BACKGROUND_IMAGE,
                                                    NULL);
	        placement = EEL_BACKGROUND_TILED; /* non-tiled only avail for desktop, at least for now */

		/* if there's none, read the default from the theme */
		if (color == NULL && image == NULL) {
			nautilus_file_background_get_default_settings 
                                (nautilus_file_background_peek_theme_source (background),
                                 &color, &image, &placement);	
		}
	}

        /* Block the other handler while we are responding to changes
         * in the metadata so it doesn't try to change the metadata.
         */
        gtk_signal_handler_block_by_func (GTK_OBJECT (background),
                                          G_CALLBACK (background_changed_callback),
                                          file);

	eel_background_set_color (background, color);     
	eel_background_set_image_uri (background, image);
        eel_background_set_image_placement (background, placement);
	
	/* Unblock the handler. */
	gtk_signal_handler_unblock_by_func (GTK_OBJECT (background),
                                            G_CALLBACK (background_changed_callback),
                                            file);
	
	g_free (color);
	g_free (image);
}

/* handle the file changed signal */
static void
saved_settings_changed_callback (NautilusFile *file,
                                 EelBackground *background)
{
	initialize_background_from_settings (file, background);
	
	if (background_is_desktop (background)) {
		nautilus_file_update_desktop_pixmaps (background);
	}
}

/* handle the theme changing */
static void
nautilus_file_background_theme_changed (gpointer user_data)
{
	NautilusFile *file;
	EelBackground *background;

	background = EEL_BACKGROUND (user_data);
	file = g_object_get_data (G_OBJECT (background), "eel_background_file");
	if (file) {
		saved_settings_changed_callback (file, background);
	}
}

/* handle the background reset signal by setting values from the current theme */
static void
background_reset_callback (EelBackground *background,
                           NautilusFile       *file)
{
	if (background_is_desktop (background)) {
		nautilus_file_background_write_desktop_default_settings ();
	} else {
	        /* Block the other handler while we are writing metadata so it doesn't
	         * try to change the background.
	         */
	        gtk_signal_handler_block_by_func (GTK_OBJECT (file),
	                                          G_CALLBACK (saved_settings_changed_callback),
	                                          background);

		/* reset the metadata */
		nautilus_file_set_metadata (file,
                                            NAUTILUS_METADATA_KEY_LOCATION_BACKGROUND_COLOR,
                                            NULL,
                                            NULL);

		nautilus_file_set_metadata (file,
                                            NAUTILUS_METADATA_KEY_LOCATION_BACKGROUND_IMAGE,
                                            NULL,
                                            NULL);
	        /* Unblock the handler. */
	        gtk_signal_handler_unblock_by_func (GTK_OBJECT (file),
	                                            G_CALLBACK (saved_settings_changed_callback),
	                                            background);
	}

	saved_settings_changed_callback (file, background);
}

/* handle the background destroyed signal */
static void
background_destroyed_callback (EelBackground *background,
                               NautilusFile       *file)
{
        gtk_signal_disconnect_by_func (GTK_OBJECT (file),
                                       G_CALLBACK (saved_settings_changed_callback),
                                       background);
        nautilus_file_monitor_remove (file, background);
	eel_preferences_remove_callback (NAUTILUS_PREFERENCES_THEME,
                                         nautilus_file_background_theme_changed,
                                         background);
}

/* key routine that hooks up a background and location */
void
nautilus_connect_background_to_file_metadata (GtkWidget    *widget,
                                              NautilusFile *file)
{
	EelBackground *background;
	gpointer old_file;
        GList *attributes;

	/* Get at the background object we'll be connecting. */
	background = eel_get_widget_background (widget);

	/* Check if it is already connected. */
	old_file = g_object_get_data (G_OBJECT (background), "eel_background_file");
	if (old_file == file) {
		return;
	}

	/* Disconnect old signal handlers. */
	if (old_file != NULL) {
		g_assert (NAUTILUS_IS_FILE (old_file));
		gtk_signal_disconnect_by_func (GTK_OBJECT (background),
                                               G_CALLBACK (background_changed_callback),
                                               old_file);
		gtk_signal_disconnect_by_func (GTK_OBJECT (background),
                                               G_CALLBACK (background_destroyed_callback),
                                               old_file);
		gtk_signal_disconnect_by_func (GTK_OBJECT (background),
                                               G_CALLBACK (background_reset_callback),
                                               old_file);
		gtk_signal_disconnect_by_func (GTK_OBJECT (old_file),
                                               G_CALLBACK (saved_settings_changed_callback),
                                               background);
		nautilus_file_monitor_remove (old_file, background);
		eel_preferences_remove_callback (NAUTILUS_PREFERENCES_THEME,
                                                 nautilus_file_background_theme_changed,
                                                 background);

	}

        /* Attach the new directory. */
        nautilus_file_ref (file);
        g_object_set_data_full (G_OBJECT (background),
                                "eel_background_file",
                                file,
                                (GtkDestroyNotify) nautilus_file_unref);

        /* Connect new signal handlers. */
        if (file != NULL) {
                g_signal_connect (G_OBJECT (background),
                                    "settings_changed",
                                    G_CALLBACK (background_changed_callback),
                                    file);
                g_signal_connect (G_OBJECT (background),
                                    "destroy",
                                    G_CALLBACK (background_destroyed_callback),
                                    file);
                g_signal_connect (G_OBJECT (background),
				    "reset",
				    G_CALLBACK (background_reset_callback),
				    file);
		g_signal_connect (G_OBJECT (file),
                                    "changed",
                                    G_CALLBACK (saved_settings_changed_callback),
                                    background);
        	
		/* arrange to receive file metadata */
                attributes = g_list_prepend (NULL, NAUTILUS_FILE_ATTRIBUTE_METADATA);
		nautilus_file_monitor_add (file,
                                           background,
                                           attributes);					     
		g_list_free (attributes);

		/* arrange for notification when the theme changes */
		eel_preferences_add_callback (NAUTILUS_PREFERENCES_THEME,
                                              nautilus_file_background_theme_changed, background);	

	}

        /* Update the background based on the file metadata. */
        initialize_background_from_settings (file, background);
}

void
nautilus_connect_background_to_file_metadata_by_uri (GtkWidget *widget,
                                                     const char *uri)
{
        NautilusFile *file;
        file = nautilus_file_get (uri);
        nautilus_connect_background_to_file_metadata (widget, file);
        nautilus_file_unref (file);
}