summaryrefslogtreecommitdiff
path: root/src/nautilus-window-manage-views.c
blob: 371022b09426cc1c5877de8e5f4cc7fe44526f7a (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
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*- */

/*
 *  Nautilus
 *
 *  Copyright (C) 1999, 2000 Red Hat, Inc.
 *  Copyright (C) 1999, 2000, 2001 Eazel, Inc.
 *
 *  Nautilus 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.
 *
 *  Nautilus 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., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Authors: Elliot Lee <sopwith@redhat.com>
 *           John Sullivan <sullivan@eazel.com>
 *           Darin Adler <darin@eazel.com>
 */

#include <config.h>
#include "nautilus-window-manage-views.h"

#include "nautilus-applicable-views.h"
#include "nautilus-application.h"
#include "nautilus-main.h"
#include "nautilus-location-bar.h"
#include "nautilus-window-private.h"
#include "nautilus-zoom-control.h"
#include <gtk/gtksignal.h>
#include <gtk/gtkmain.h>
#include <bonobo/bonobo-ui-util.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomeui/gnome-dialog-util.h>
#include <libgnomevfs/gnome-vfs-async-ops.h>
#include <libgnomevfs/gnome-vfs-uri.h>
#include <libgnomevfs/gnome-vfs-utils.h>
#include <libnautilus-extensions/nautilus-bonobo-extensions.h>
#include <libnautilus-extensions/nautilus-debug.h>
#include <libnautilus-extensions/nautilus-file.h>
#include <libnautilus-extensions/nautilus-file-attributes.h>
#include <libnautilus-extensions/nautilus-file-utilities.h>
#include <libnautilus-extensions/nautilus-gdk-extensions.h>
#include <libnautilus-extensions/nautilus-glib-extensions.h>
#include <libnautilus-extensions/nautilus-global-preferences.h>
#include <libnautilus-extensions/nautilus-gtk-extensions.h>
#include <libnautilus-extensions/nautilus-metadata.h>
#include <libnautilus-extensions/nautilus-mime-actions.h>
#include <libnautilus-extensions/nautilus-search-uri.h>
#include <libnautilus-extensions/nautilus-stock-dialogs.h>
#include <libnautilus-extensions/nautilus-string.h>

/* FIXME bugzilla.eazel.com 1243: 
 * We should use inheritance instead of these special cases
 * for the desktop window.
 */
#include "nautilus-desktop-window.h"

/* This number controls a maximum character count for a Nautilus
 * window title. Without limiting the window title, most window
 * managers make the window wide enough to able to display the whole
 * title. When this happens, the Nautilus window in question becomes
 * unusable. This is a very common thing to happen, especially with
 * generated web content, such as bugzilla queries, which generate
 * very long urls. I found the number experimentally. To properly
 * compute it, we would need window manager support to access the
 * character metrics for the window title.
 */
#define MAX_TITLE_LENGTH 180

/* This number controls a maximum character count for a URL that is
 * displayed as part of a dialog. It's fairly arbitrary -- big enough
 * to allow most "normal" URIs to display in full, but small enough to
 * prevent the dialog from getting insanely wide.
 */
#define MAX_URI_IN_DIALOG_LENGTH 60

typedef enum {
        THIS_WINDOW,
        EXISTING_WINDOW,
        NEW_WINDOW
} OpenLocationWindow;

typedef struct {
	gboolean is_sidebar_panel;
	char *label;
} ViewFrameInfo;

static void connect_view           (NautilusWindow             *window,
                                    NautilusViewFrame          *view);
static void disconnect_view        (NautilusWindow             *window,
                                    NautilusViewFrame          *view);
static void begin_location_change  (NautilusWindow             *window,
                                    const char                 *location,
                                    NautilusLocationChangeType  type,
                                    guint                       distance);
static void free_location_change   (NautilusWindow             *window);
static void cancel_location_change (NautilusWindow             *window);

static void
change_selection (NautilusWindow *window,
                  GList *selection,
                  NautilusViewFrame *requesting_view)
{
        GList *sorted, *node;
        NautilusViewFrame *view;

        /* Sort list into canonical order and check if it's the same as
         * the selection we already have.
         */
        sorted = nautilus_g_str_list_alphabetize (nautilus_g_str_list_copy (selection));
        if (nautilus_g_str_list_equal (sorted, window->details->selection)) {
                nautilus_g_list_free_deep (sorted);
                return;
        }

        /* Store the new selection. */
        nautilus_g_list_free_deep (window->details->selection);
        window->details->selection = sorted;

        /* Tell all the view frames about it, except the one that changed it. */
        if (window->content_view != requesting_view) {
                nautilus_view_frame_selection_changed (window->content_view, sorted);
        }
        for (node = window->sidebar_panels; node != NULL; node = node->next) {
                view = node->data;
                if (view != requesting_view) {
                        nautilus_view_frame_selection_changed (view, sorted);
                }
        }
}

static char *
compute_default_title (const char *text_uri)
{
	NautilusFile *file;
	char *title;

	if (text_uri == NULL) {
		title = g_strdup ("");
	} else {
		file = nautilus_file_get (text_uri);
		title = nautilus_file_get_name (file);
		nautilus_file_unref (file);
	}

	return title;
}

/* compute_title:
 * 
 * Get a newly allocated copy of the user-displayable title for the current
 * location. Note that the window title is related to this but might not
 * be exactly this.
 * @window: The NautilusWindow in question.
 * 
 * Return value: A newly allocated string. Use g_free when done with it.
 */
static char *
compute_title (NautilusWindow *window)
{
        char *title;

	title = NULL;
        if (window->new_content_view != NULL) {
                title = nautilus_view_frame_get_title (window->new_content_view);
        } else if (window->content_view != NULL) {
                title = nautilus_view_frame_get_title (window->content_view);
        }
        if (title == NULL) {
                title = compute_default_title (window->details->location);
        }
        return title;
}

/* window_set_title_with_time_stamp
 * 
 * Update the non-NautilusViewFrame objects that use the location's user-displayable
 * title in some way. Called when the location or title has changed.
 * @window: The NautilusWindow in question.
 * @title: The new user-displayable title.
 * 
 */
static void
window_set_title_with_time_stamp (NautilusWindow *window, const char *title)
{
        char *time_stamp;
	char *title_with_time_stamp;
	
        g_return_if_fail (NAUTILUS_IS_WINDOW (window));
        g_return_if_fail (title != NULL);

	time_stamp = nautilus_get_build_time_stamp ();
	
	if (time_stamp != NULL) {
		/* FIXME bugzilla.eazel.com 5037: The text Preview
		 * Release is hardcoded here. Are all builds with
		 * time stamps really best described as "preview
		 * release"?.
                 */
		title_with_time_stamp = g_strdup_printf (_("Preview Release %s: %s"), time_stamp, title);
		gtk_window_set_title (GTK_WINDOW (window), title_with_time_stamp);
		g_free (title_with_time_stamp);
	} else {
		gtk_window_set_title (GTK_WINDOW (window), title);
	}

	g_free (time_stamp);
}

/* update_title:
 * 
 * Update the non-NautilusViewFrame objects that use the location's user-displayable
 * title in some way. Called when the location or title has changed.
 * @window: The NautilusWindow in question.
 * @title: The new user-displayable title.
 * 
 */
static void
update_title (NautilusWindow *window)
{
        char *title;
        char *window_title;
        GList *node;

        title = compute_title (window);

        /* Remember the title and check if it's the same as last time. */
        if (window->details->title != NULL
            && strcmp (title, window->details->title) == 0) {
                g_free (title);
                return;
        }
        g_free (window->details->title);
        window->details->title = g_strdup (title);

        if (title[0] == '\0') {
		window_set_title_with_time_stamp (window, _("Nautilus"));
        } else {
                window_title = nautilus_str_middle_truncate (title, MAX_TITLE_LENGTH);
		window_set_title_with_time_stamp (window, window_title);
                g_free (window_title);
        }

        nautilus_sidebar_set_title (window->sidebar, title);
        nautilus_bookmark_set_name (window->current_location_bookmark, title);

        g_free (title);
        
        /* Name of item in history list may have changed, tell listeners. */
        nautilus_send_history_list_changed ();

        /* warn all views and sidebar panels of the potential title change */
        if (window->content_view != NULL) {
                nautilus_view_frame_title_changed (window->content_view, title);
        }
        for (node = window->sidebar_panels; node != NULL; node = node->next) {
                nautilus_view_frame_title_changed (node->data, title);
        }
}

/* set_displayed_location:
 * 
 * Update the non-NautilusViewFrame objects that use the location's user-displayable
 * title in some way. Called when the location or title has changed.
 * @window: The NautilusWindow in question.
 * @title: The new user-displayable title.
 */
static void
set_displayed_location (NautilusWindow *window, const char *location)
{
        char *bookmark_uri;
        gboolean recreate;
        
        if (window->current_location_bookmark == NULL || location == NULL) {
                recreate = TRUE;
        } else {
                bookmark_uri = nautilus_bookmark_get_uri (window->current_location_bookmark);
                recreate = !nautilus_uris_match (bookmark_uri, location);
                g_free (bookmark_uri);
        }
        
        if (recreate) {
                /* We've changed locations, must recreate bookmark for current location. */
                if (window->last_location_bookmark != NULL)  {
                        gtk_object_unref (GTK_OBJECT (window->last_location_bookmark));
                }
                window->last_location_bookmark = window->current_location_bookmark;
                window->current_location_bookmark = location == NULL ? NULL
                        : nautilus_bookmark_new (location, location);
        }

        update_title (window);
}

static void
check_bookmark_location_matches (NautilusBookmark *bookmark, const char *uri)
{
	char *bookmark_uri;

	bookmark_uri = nautilus_bookmark_get_uri (bookmark);
	if (!nautilus_uris_match (uri, bookmark_uri)) {
		g_warning ("bookmark uri is %s, but expected %s", bookmark_uri, uri);
	}
	g_free (bookmark_uri);
}

/* Debugging function used to verify that the last_location_bookmark
 * is in the state we expect when we're about to use it to update the
 * Back or Forward list.
 */
static void
check_last_bookmark_location_matches_window (NautilusWindow *window)
{
	check_bookmark_location_matches (window->last_location_bookmark,
                                         window->details->location);
}

static void
handle_go_back (NautilusWindow *window, const char *location)
{
        guint i;
        GList *link;
        NautilusBookmark *bookmark;

        /* Going back. Move items from the back list to the forward list. */
        g_assert (g_list_length (window->back_list) > window->details->location_change_distance);
        check_bookmark_location_matches (NAUTILUS_BOOKMARK (g_list_nth_data (window->back_list,
                                                                             window->details->location_change_distance)),
                                         location);
        g_assert (window->details->location != NULL);
        
        /* Move current location to Forward list */

        check_last_bookmark_location_matches_window (window);

        /* Use the first bookmark in the history list rather than creating a new one. */
        window->forward_list = g_list_prepend (window->forward_list,
                                               window->last_location_bookmark);
        gtk_object_ref (GTK_OBJECT (window->forward_list->data));
                                
        /* Move extra links from Back to Forward list */
        for (i = 0; i < window->details->location_change_distance; ++i) {
        	bookmark = NAUTILUS_BOOKMARK (window->back_list->data);
                window->back_list = g_list_remove (window->back_list, bookmark);
                window->forward_list = g_list_prepend (window->forward_list, bookmark);
        }
        
        /* One bookmark falls out of back/forward lists and becomes viewed location */
        link = window->back_list;
        window->back_list = g_list_remove_link (window->back_list, link);
        gtk_object_unref (GTK_OBJECT (link->data));
        g_list_free_1 (link);
}

static void
handle_go_forward (NautilusWindow *window, const char *location)
{
        guint i;
        GList *link;
        NautilusBookmark *bookmark;

        /* Going forward. Move items from the forward list to the back list. */
        g_assert (g_list_length (window->forward_list) > window->details->location_change_distance);
        check_bookmark_location_matches (NAUTILUS_BOOKMARK (g_list_nth_data (window->forward_list,
                                                                             window->details->location_change_distance)),
                                         location);
        g_assert (window->details->location != NULL);
                                
        /* Move current location to Back list */

        check_last_bookmark_location_matches_window (window);
        
        /* Use the first bookmark in the history list rather than creating a new one. */
        window->back_list = g_list_prepend (window->back_list,
                                            window->last_location_bookmark);
        gtk_object_ref (GTK_OBJECT (window->back_list->data));
        
        /* Move extra links from Forward to Back list */
        for (i = 0; i < window->details->location_change_distance; ++i) {
        	bookmark = NAUTILUS_BOOKMARK (window->forward_list->data);
                window->forward_list = g_list_remove (window->forward_list, bookmark);
                window->back_list = g_list_prepend (window->back_list, bookmark);
        }
        
        /* One bookmark falls out of back/forward lists and becomes viewed location */
        link = window->forward_list;
        window->forward_list = g_list_remove_link (window->forward_list, link);
        gtk_object_unref (GTK_OBJECT (link->data));
        g_list_free_1 (link);
}

static void
handle_go_elsewhere (NautilusWindow *window, const char *location)
{
       /* Clobber the entire forward list, and move displayed location to back list */
        nautilus_window_clear_forward_list (window);
                                
        if (window->details->location != NULL) {
                /* If we're returning to the same uri somehow, don't put this uri on back list. 
                 * This also avoids a problem where set_displayed_location
                 * didn't update last_location_bookmark since the uri didn't change.
                 */
                if (!nautilus_uris_match (window->details->location, location)) {
                        /* Store bookmark for current location in back list, unless there is no current location */
	                check_last_bookmark_location_matches_window (window);
                        
                        /* Use the first bookmark in the history list rather than creating a new one. */
                        window->back_list = g_list_prepend (window->back_list,
                                                            window->last_location_bookmark);
                        gtk_object_ref (GTK_OBJECT (window->back_list->data));
                }
        }
}

static void
update_up_button (NautilusWindow *window)
{
        gboolean allowed;
        GnomeVFSURI *new_uri;

        allowed = FALSE;
        if (window->details->location != NULL) {
                new_uri = gnome_vfs_uri_new (window->details->location);
                if (new_uri != NULL) {
                        allowed = gnome_vfs_uri_has_parent (new_uri);
                        gnome_vfs_uri_unref (new_uri);
                }
        }
        nautilus_window_allow_up (window, allowed);
}

static void
viewed_file_changed_callback (NautilusFile *file,
                              NautilusWindow *window)
{
        char *new_location;

        g_assert (NAUTILUS_IS_FILE (file));
	g_assert (NAUTILUS_IS_WINDOW (window));
	g_assert (window->details->viewed_file == file);

	/* Close window if the file it's viewing has been deleted. */
	if (nautilus_file_is_gone (file)) {
                /* detecting a file is gone may happen in the middle
                 * of a pending location change, we need to cancel it
                 * before closing the window or things break.
                 */
                cancel_location_change (window);

                /* FIXME bugzilla.eazel.com 5038: Is closing the window really the right thing to do
                 * for all cases?
                 */
		nautilus_window_close (window);
	} else {
                new_location = nautilus_file_get_uri (file);

                /* if the file was renamed, update location and/or title */
                if (!nautilus_uris_match (new_location, window->details->location)) {
                        g_free (window->details->location);
                        window->details->location = new_location;
                        
                        /* Check if we can go up. */
                        update_up_button (window);
                        
                        /* Change the location bar to match the current location. */
                        nautilus_navigation_bar_set_location
                                (NAUTILUS_NAVIGATION_BAR (window->navigation_bar),
                                 window->details->location);

                        update_title (window);
                } else {
                        g_free (new_location);
                }
        }
}

static void
cancel_viewed_file_changed_callback (NautilusWindow *window)
{
        if (window->details->viewed_file != NULL) {
                gtk_signal_disconnect_by_func (GTK_OBJECT (window->details->viewed_file),
                                               viewed_file_changed_callback,
                                               window);
        }
}

/* Handle the changes for the NautilusWindow itself. */
static void
update_for_new_location (NautilusWindow *window)
{
        char *new_location;
        NautilusFile *file;
        
        new_location = window->details->pending_location;
        window->details->pending_location = NULL;
        
        /* Maintain history lists. */
        if (window->details->location_change_type != NAUTILUS_LOCATION_CHANGE_RELOAD) {
                /* Always add new location to history list. */
                nautilus_window_add_current_location_to_history_list (window);
                
                /* Update back and forward list. */
                if (window->details->location_change_type == NAUTILUS_LOCATION_CHANGE_BACK) {
                        handle_go_back (window, new_location);
                } else if (window->details->location_change_type == NAUTILUS_LOCATION_CHANGE_FORWARD) {
                        handle_go_forward (window, new_location);
                } else {
                        g_assert (window->details->location_change_type == NAUTILUS_LOCATION_CHANGE_STANDARD);
                        handle_go_elsewhere (window, new_location);
                }
        }
        
        /* Set the new location. */
        g_free (window->details->location);
        window->details->location = new_location;
        
        /* Create a NautilusFile for this location, so we can catch it
         * if it goes away.
         */
        cancel_viewed_file_changed_callback (window);
        file = nautilus_file_get (window->details->location);
        nautilus_window_set_viewed_file (window, file);
        gtk_signal_connect (GTK_OBJECT (file),
                            "changed",
                            viewed_file_changed_callback,
                            window);
        nautilus_file_unref (file);
        
        /* Check if we can go up. */
        update_up_button (window);
        
        /* Set up the content view menu for this new location. */
        nautilus_window_load_view_as_menu (window);
        
        /* Check if the back and forward buttons need enabling or disabling. */
        nautilus_window_allow_back (window, window->back_list != NULL);
        nautilus_window_allow_forward (window, window->forward_list != NULL);
        
        /* Change the location bar to match the current location. */
        nautilus_navigation_bar_set_location (NAUTILUS_NAVIGATION_BAR (window->navigation_bar),
                                              window->details->location);
        
        /* Notify the sidebar of the location change. */
        /* FIXME bugzilla.eazel.com 211:
         * Eventually, this will not be necessary when we restructure the 
         * sidebar itself to be a NautilusViewFrame.
         */
        nautilus_sidebar_set_uri (window->sidebar,
                                  window->details->location,
                                  window->details->title);
}

static gboolean
unref_callback (gpointer callback_data)
{
        gtk_object_unref (GTK_OBJECT (callback_data));
        return FALSE;
}

static void
ref_now_unref_at_idle_time (GtkObject *object)
{
        gtk_object_ref (object);
        g_idle_add (unref_callback, object);
}

/* This is called when we have decided we can actually change to the new view/location situation. */
static void
location_has_really_changed (NautilusWindow *window)
{
        /* Switch to the new content view. */
        if (GTK_WIDGET (window->new_content_view)->parent == NULL) {
                /* If we don't unref the old view until idle
                 * time, we avoid certain kinds of problems in
                 * in-process components, since they won't
                 * lose their ViewFrame in the middle of some
                 * operation. This still doesn't necessarily
                 * help for out of process components.
                 */
                if (window->content_view != NULL) {
                        ref_now_unref_at_idle_time (GTK_OBJECT (window->content_view));
                }
                
                disconnect_view (window, window->content_view);
                nautilus_window_set_content_view_widget (window, window->new_content_view);
        }
        gtk_object_unref (GTK_OBJECT (window->new_content_view));
        window->new_content_view = NULL;
        
        /* Update displayed view in menu. Only do this if we're not switching
         * locations though, because if we are switching locations we'll
         * install a whole new set of views in the menu later (the current
         * views in the menu are for the old location).
         */
        if (window->details->pending_location == NULL) {
                nautilus_window_synch_view_as_menu (window);
        }

        /* Tell the window we are finished. */
        if (window->details->pending_location != NULL) {
                update_for_new_location (window);
        }

        free_location_change (window);

        update_title (window);
}


static NautilusWindow *
get_topmost_nautilus_window (void)
{
        GList *window_list, *node;
        NautilusWindow *result;

        window_list = nautilus_get_window_list_ordered_front_to_back ();

        result = NULL;
        for (node = window_list; node != NULL; node = node->next) {
                if (NAUTILUS_IS_WINDOW (node->data)) {
                        result = NAUTILUS_WINDOW (node->data);
                        break;
                }
        }

        g_list_free (window_list);

        return result;
}

static void
open_location (NautilusWindow *window,
               const char *location,
               gboolean force_new_window,
               GList *new_selection)
{
        NautilusWindow *target_window;
        gboolean create_new_window;
        
        target_window = window;
        create_new_window = force_new_window;

	/* FIXME bugzilla.eazel.com 1243: 
	 * We should use inheritance instead of these special cases
	 * for the desktop window.
	 */
        /* When loading a location on the desktop (in "open within
         * same window mode"), always use another window. Either use
         * the topmost window, or create a new window if the desktop
         * is the topmost (and only).
         */
        if (!create_new_window
            && NAUTILUS_IS_DESKTOP_WINDOW (window)
            && window->content_view != NULL) {
                target_window = get_topmost_nautilus_window ();
                if (target_window == window) {
                        create_new_window = TRUE;
                } else {
                        nautilus_gtk_window_present (GTK_WINDOW (target_window));
                }
        }

        if (create_new_window) {
                target_window = nautilus_application_create_window (window->application);
        }

	nautilus_g_list_free_deep (target_window->details->pending_selection);
        target_window->details->pending_selection = nautilus_g_str_list_copy (new_selection);

        begin_location_change (target_window, location,
                               NAUTILUS_LOCATION_CHANGE_STANDARD, 0);
}

void
nautilus_window_open_location (NautilusWindow *window,
                               const char *location)
{
        open_location (window, location, FALSE, NULL);
}


static ViewFrameInfo *
view_frame_info_new (gboolean is_sidebar_panel, const char *label)
{
	ViewFrameInfo *new_info;

	g_return_val_if_fail (label != NULL, NULL);

	new_info = g_new0 (ViewFrameInfo, 1);
	new_info->is_sidebar_panel = is_sidebar_panel;
	new_info->label = g_strdup (label);

	return new_info;
}

static void
view_frame_info_free (ViewFrameInfo *info)
{
	if (info != NULL) {
		g_free (info->label);
		g_free (info);
	}
}

static void
set_view_frame_info (NautilusViewFrame *view_frame, 
		     gboolean is_sidebar_panel, 
		     const char *label)
{
	gtk_object_set_data_full (GTK_OBJECT (view_frame),
				  "info",
				  view_frame_info_new (is_sidebar_panel, label),
				  (GtkDestroyNotify) view_frame_info_free);
}

static gboolean
view_frame_is_sidebar_panel (NautilusViewFrame *view_frame)
{
	ViewFrameInfo *info;

	info = (ViewFrameInfo *)gtk_object_get_data 
		(GTK_OBJECT (view_frame), "info");
	return info->is_sidebar_panel;
}

static char *
view_frame_get_label (NautilusViewFrame *view_frame)
{
	ViewFrameInfo *info;

	info = (ViewFrameInfo *)gtk_object_get_data 
		(GTK_OBJECT (view_frame), "info");
	return g_strdup (info->label);
}

static void
report_content_view_failure_to_user_internal (NautilusWindow *window,
                                     	      NautilusViewFrame *view_frame,
                                     	      const char *message)
{
	char *label;

	label = view_frame_get_label (view_frame);
	message = g_strdup_printf (message, label);
	nautilus_show_error_dialog (message, _("View Failed"), GTK_WINDOW (window));
	g_free (label);
}

static void
report_current_content_view_failure_to_user (NautilusWindow *window,
                                     	     NautilusViewFrame *view_frame)
{
	report_content_view_failure_to_user_internal 
		(window,
		 view_frame,
		 _("The %s view encountered an error and can't continue. "
		   "You can choose another view or go to a different location."));
}

static void
report_nascent_content_view_failure_to_user (NautilusWindow *window,
                                     	     NautilusViewFrame *view_frame)
{
	report_content_view_failure_to_user_internal 
		(window,
		 view_frame,
		 _("The %s view encountered an error while starting up."));
}

static void
load_new_location_in_one_view (NautilusViewFrame *view,
                               const char *new_location,
                               GList *new_selection)
{
        nautilus_view_frame_load_location (view, new_location);
        nautilus_view_frame_selection_changed (view, new_selection);
}

static void
load_new_location_in_sidebar_panels (NautilusWindow *window,
                                     const char *location,
                                     GList *selection,
                                     NautilusViewFrame *view_to_skip)
{
        GList *node;
        NautilusViewFrame *view;

        for (node = window->sidebar_panels; node != NULL; node = node->next) {
                view = node->data;
                if (view != view_to_skip
                    && nautilus_view_frame_get_is_view_loaded (view)) {
                        load_new_location_in_one_view (view, location, selection);
                }
        }
}

static void
load_new_location_in_all_views (NautilusWindow *window,
                                const char *location,
                                GList *selection,
                                NautilusViewFrame *view_to_skip)
{
	g_assert (NAUTILUS_IS_WINDOW (window));
	g_assert (location != NULL);

        set_displayed_location (window, location);
        if (window->new_content_view != view_to_skip
            && window->new_content_view != NULL) {
                load_new_location_in_one_view (window->new_content_view,
                                               location,
                                               selection);
        }
        load_new_location_in_sidebar_panels (window,
                                             location,
                                             selection,
                                             view_to_skip);
}

static void
set_to_pending_location_and_selection (NautilusWindow *window)
{
        g_assert (window->new_content_view != NULL);
        g_assert (window->details->pending_location != NULL);

        load_new_location_in_all_views (window,
                                        window->details->pending_location,
                                        window->details->pending_selection,
                                        NULL);
        
        nautilus_g_list_free_deep (window->details->pending_selection);
        window->details->pending_selection = NULL;
}

static gboolean
nautilus_window_content_view_matches_iid (NautilusWindow *window, 
					  const char *iid)
{
	const char *content_view_iid;

        if (window->content_view == NULL) {
                content_view_iid = "";
        } else {
                content_view_iid = nautilus_view_frame_get_view_iid (window->content_view);
        }

	return strcmp (content_view_iid, iid) == 0;
}

static void
load_content_view (NautilusWindow *window,
                   const NautilusViewIdentifier *id)
{
        const char *iid;
        NautilusViewFrame *view;

 	/* FIXME bugzilla.eazel.com 1243: 
	 * We should use inheritance instead of these special cases
	 * for the desktop window.
	 */
        if (NAUTILUS_IS_DESKTOP_WINDOW (window)) {
        	/* We force the desktop to use a desktop_icon_view. It's simpler
        	 * to fix it here than trying to make it pick the right view in
        	 * the first place.
        	 */
		iid = NAUTILUS_DESKTOP_ICON_VIEW_IID;
	} else {
        	g_return_if_fail (id != NULL);
        	iid = id->iid;
        }
        
        bonobo_ui_component_freeze (window->details->shell_ui, NULL);
	nautilus_bonobo_set_sensitive (window->details->shell_ui,
				       NAUTILUS_COMMAND_ZOOM_IN,
				       FALSE);
	nautilus_bonobo_set_sensitive (window->details->shell_ui,
				       NAUTILUS_COMMAND_ZOOM_OUT,
				       FALSE);
	nautilus_bonobo_set_sensitive (window->details->shell_ui,
				       NAUTILUS_COMMAND_ZOOM_NORMAL,
				       FALSE);
        
        bonobo_ui_component_thaw (window->details->shell_ui, NULL);
        
        nautilus_view_identifier_free (window->content_view_id);
        window->content_view_id = nautilus_view_identifier_copy (id);

        if (nautilus_window_content_view_matches_iid (window, iid)) {
                /* reuse existing content view */
                view = window->content_view;
                window->new_content_view = view;
        	gtk_object_ref (GTK_OBJECT (view));
                set_to_pending_location_and_selection (window);
        } else {
                /* create a new content view */
                view = nautilus_view_frame_new (window->details->ui_container,
                                                window->application->undo_manager);
                window->new_content_view = view;
                gtk_object_ref (GTK_OBJECT (view));
                gtk_object_sink (GTK_OBJECT (view));
		set_view_frame_info (view, FALSE, id->name);
                connect_view (window, view);
                nautilus_view_frame_load_view (view, iid);
        }
}

static void
report_sidebar_panel_failure_to_user (NautilusWindow *window, NautilusViewFrame *view_frame)
{
	char *message;
	char *label;

	label = view_frame_get_label (view_frame);

        if (label == NULL) {
                message = g_strdup
                        (_("One of the sidebar panels encountered an error and can't continue. "
                           "Unfortunately I couldn't tell which one."));
        } else {
                message = g_strdup_printf
                        (_("The %s sidebar panel encountered an error and can't continue. "
                           "If this keeps happening, you might want to turn this panel off."),
                         label);
        }

	nautilus_show_error_dialog (message, _("Sidebar Panel Failed"), GTK_WINDOW (window));

	g_free (label);
	g_free (message);
}

static void
disconnect_and_destroy_sidebar_panel (NautilusWindow *window, NautilusViewFrame *view)
{
        gtk_widget_ref (GTK_WIDGET (view));
	disconnect_view (window, view);
        nautilus_window_remove_sidebar_panel (window, view);
	gtk_widget_destroy (GTK_WIDGET (view));
        gtk_widget_unref (GTK_WIDGET (view));
}

static void
handle_view_failure (NautilusWindow *window,
                     NautilusViewFrame *view)
{
	const char *current_iid;

        g_warning ("A view failed. The UI will handle this with a dialog but this should be debugged.");

	if (view_frame_is_sidebar_panel (view)) {
                report_sidebar_panel_failure_to_user (window, view);
		current_iid = nautilus_view_frame_get_view_iid (view);
		nautilus_sidebar_hide_active_panel_if_matches (window->sidebar, current_iid);
		disconnect_and_destroy_sidebar_panel (window, view);
	} else {
	        if (view == window->content_view) {
	                nautilus_window_set_content_view_widget (window, NULL);

                        /* FIXME bugzilla.eazel.com 5039: We need a
                         * way to report the specific error that
                         * happens in this case - adapter factory not
                         * found, component failed to load, etc.
                         */
	                report_current_content_view_failure_to_user (window, view);
	        } else {
                        /* FIXME bugzilla.eazel.com 5039: We need a
                         * way to report the specific error that
                         * happens in this case - adapter factory not
                         * found, component failed to load, etc.
                         */
	                report_nascent_content_view_failure_to_user (window, view);
	        }

                cancel_location_change (window);
	}
}

static void
free_location_change (NautilusWindow *window)
{
        g_free (window->details->pending_location);
        window->details->pending_location = NULL;

        if (window->details->pending_location_as_directory != NULL) {
                nautilus_directory_file_monitor_remove (window->details->pending_location_as_directory,
                                                        window);
                nautilus_directory_unref (window->details->pending_location_as_directory);
                window->details->pending_location_as_directory = NULL;
        }

        if (window->details->determine_view_handle != NULL) {
                nautilus_determine_initial_view_cancel (window->details->determine_view_handle);
                window->details->determine_view_handle = NULL;
        }
        
        if (window->new_content_view != NULL) {
        	if (window->new_content_view != window->content_view) {
                        disconnect_view (window, window->new_content_view);
                        gtk_widget_destroy (GTK_WIDGET (window->new_content_view));
        	}
        	gtk_object_unref (GTK_OBJECT (window->new_content_view));
                window->new_content_view = NULL;
        }
}

static void
end_location_change (NautilusWindow *window)
{
        nautilus_window_allow_stop (window, FALSE);
        free_location_change (window);
}

static void
cancel_location_change (NautilusWindow *window)
{
        NautilusViewFrame *skip_view;
        if (window->details->pending_location != NULL
            && window->details->location != NULL) {
                
                /* Tell previously-notified views to go back to the old page */
                if (window->new_content_view == NULL
                    || window->new_content_view != window->content_view) {
                        skip_view = window->content_view;
                } else {
                        skip_view = NULL;
                }

                load_new_location_in_all_views (window,
                                                window->details->location,
                                                window->details->selection,
                                                skip_view);
        }

        end_location_change (window);
}
        
static void
position_and_show_window_callback (NautilusFile *file,
                       		   gpointer callback_data)
{
	NautilusWindow *window;
	char *geometry_string;
        
	window = NAUTILUS_WINDOW (callback_data);

	if (nautilus_preferences_get_boolean (NAUTILUS_PREFERENCES_WINDOW_ALWAYS_NEW)) {
		geometry_string = nautilus_file_get_metadata 
			(file, NAUTILUS_METADATA_KEY_WINDOW_GEOMETRY, NULL);
		if (geometry_string != NULL) {
			nautilus_gtk_window_set_initial_geometry_from_string 
				(GTK_WINDOW (window), 
				 geometry_string,
				 NAUTILUS_WINDOW_MIN_WIDTH, 
				 NAUTILUS_WINDOW_MIN_HEIGHT);
		}
		g_free (geometry_string);
	}

	gtk_widget_show (GTK_WIDGET (window));

        /* This object was ref'd when starting the callback. */
        nautilus_file_unref (file);
}                       			     

/* utility routine that returns true if there's one or fewer windows in the window list */
static gboolean
just_one_window (void)
{
	return !nautilus_g_list_more_than_one_item
                (nautilus_application_get_window_list ());
}

static void
determined_initial_view_callback (NautilusDetermineViewHandle *handle,
                                  NautilusDetermineViewResult result_code,
                                  const NautilusViewIdentifier *initial_view,
                                  gpointer data)
{
        NautilusWindow *window;
        NautilusFile *file;
        char *full_uri_for_display;
        char *uri_for_display;
        char *error_message;
        char *scheme_string;
        char *type_string;
        char *dialog_title;
        char *home_uri;
        const char *location;
	GnomeDialog *dialog;
        GList *attributes;
        GnomeVFSURI *vfs_uri;
       
        window = NAUTILUS_WINDOW (data);

        g_assert (window->details->determine_view_handle == handle
                  || window->details->determine_view_handle == NULL);
        window->details->determine_view_handle = NULL;

        location = window->details->pending_location;

        if (result_code == NAUTILUS_DETERMINE_VIEW_OK) {
		/* If the window is not yet showing (as is the case for nascent
		 * windows), position and show it only after we've got the
		 * metadata (since position info is stored there).
		 */
                if (!GTK_WIDGET_VISIBLE (window)) {
	                file = nautilus_file_get (location);

                        attributes = g_list_append (NULL, NAUTILUS_FILE_ATTRIBUTE_METADATA);
			nautilus_file_call_when_ready (file,
                                                       attributes,
                                                       position_and_show_window_callback,
                                                       window);
                        g_list_free (attributes);
                }

                load_content_view (window, initial_view);
                return;
        }
        
        /* Some sort of failure occurred. How 'bout we tell the user? */
        full_uri_for_display = nautilus_format_uri_for_display (location);
	/* Truncate the URI so it doesn't get insanely wide. Note that even
	 * though the dialog uses wrapped text, if the URI doesn't contain
	 * white space then the text-wrapping code is too stupid to wrap it.
	 */
        uri_for_display = nautilus_str_middle_truncate
                (full_uri_for_display, MAX_URI_IN_DIALOG_LENGTH);
	g_free (full_uri_for_display);

	dialog_title = NULL;
        
        switch (result_code) {

        case NAUTILUS_DETERMINE_VIEW_NOT_FOUND:
                error_message = g_strdup_printf
                        (_("Couldn't find \"%s\". Please check the spelling and try again."),
                         uri_for_display);
                break;

        case NAUTILUS_DETERMINE_VIEW_INVALID_URI:
                error_message = g_strdup_printf
                        (_("\"%s\" is not a valid location. Please check the spelling and try again."),
                         uri_for_display);
                break;

        case NAUTILUS_DETERMINE_VIEW_NO_HANDLER_FOR_TYPE:
                /* FIXME bugzilla.eazel.com 866: Can't expect to read the
                 * permissions instantly here. We might need to wait for
                 * a stat first.
                 */
        	file = nautilus_file_get (location);
                type_string = nautilus_file_get_string_attribute (file, "type");
                nautilus_file_unref (file);
                if (type_string == NULL) {
	                error_message = g_strdup_printf
                                (_("Couldn't display \"%s\", because Nautilus cannot determine what type of file it is."),
                                 uri_for_display);
        	} else {
        		/* FIXME bugzilla.eazel.com 4932:
        		 * Should distinguish URIs with no handlers at all from remote URIs
        		 * with local-only handlers.
        		 */
	                error_message = g_strdup_printf
                                (_("Nautilus has no installed viewer capable of displaying \"%s\"."),
                                 uri_for_display);
			g_free (type_string);
        	}
                break;

        case NAUTILUS_DETERMINE_VIEW_UNSUPPORTED_SCHEME:
                /* Can't create a vfs_uri and get the method from that, because 
                 * gnome_vfs_uri_new might return NULL.
                 */
                scheme_string = nautilus_str_get_prefix (location, ":");
                g_assert (scheme_string != NULL);  /* Shouldn't have gotten this error unless there's a : separator. */
                error_message = g_strdup_printf (_("Couldn't display \"%s\", because Nautilus cannot handle %s: locations."),
                                                 uri_for_display, scheme_string);
                g_free (scheme_string);
                break;

	case NAUTILUS_DETERMINE_VIEW_LOGIN_FAILED:
                error_message = g_strdup_printf (_("Couldn't display \"%s\", because the attempt to log in failed."),
                                                 uri_for_display);		
		break;

	case NAUTILUS_DETERMINE_VIEW_ACCESS_DENIED:
                error_message = g_strdup_printf (_("Couldn't display \"%s\", because access was denied."),
                                                 uri_for_display);
		break;

	case NAUTILUS_DETERMINE_VIEW_HOST_NOT_FOUND:
		/* This case can be hit for user-typed strings like "foo" due to
		 * the code that guesses web addresses when there's no initial "/".
		 * But this case is also hit for legitimate web addresses when
		 * the proxy is set up wrong.
		 */
		vfs_uri = gnome_vfs_uri_new (location);
                error_message = g_strdup_printf (_("Couldn't display \"%s\", because no host \"%s\" could be found. "
                				   "Check that the spelling is correct and that your proxy settings are correct."),
                                                 uri_for_display,
                                                 gnome_vfs_uri_get_host_name (vfs_uri));
                gnome_vfs_uri_unref (vfs_uri);
		break;

	case NAUTILUS_DETERMINE_VIEW_HOST_HAS_NO_ADDRESS:
                error_message = g_strdup_printf (_("Couldn't display \"%s\", because the host name was empty. "
                				   "Check that your proxy settings are correct."),
                                                 uri_for_display);
		break;

	case NAUTILUS_DETERMINE_VIEW_SERVICE_NOT_AVAILABLE:
		if (nautilus_is_search_uri (location)) {
			/* FIXME bugzilla.eazel.com 2458: Need to give
                         * the user better advice about what to do
                         * here.
                         */
			error_message = g_strdup_printf
                                (_("Searching is unavailable right now, because you either have no index, "
                                   "or the search service isn't running. "
                                   "Be sure that you have started the Medusa search service, and if you "
                                   "don't have an index, that the Medusa indexer is running."));
			dialog_title = g_strdup (_("Searching Unavailable"));
			break;
		} /* else fall through */
        default:
                error_message = g_strdup_printf (_("Nautilus cannot display \"%s\"."),
                                                 uri_for_display);
        }
        
        if (dialog_title == NULL) {
		dialog_title = g_strdup (_("Can't Display Location"));
        }

        if (!GTK_WIDGET_VISIBLE (GTK_WIDGET (window))) {
                /* Destroy never-had-a-chance-to-be-seen window. This case
                 * happens when a new window cannot display its initial URI. 
                 */

                dialog = nautilus_show_error_dialog (error_message, dialog_title, NULL);
                
		/* if this is the only window, we don't want to quit, so we redirect it to home */
		if (just_one_window ()) {
			/* the user could have typed in a home directory that doesn't exist,
			   in which case going home would cause an infinite loop, so we
			   better test for that */
			
			home_uri = nautilus_preferences_get (NAUTILUS_PREFERENCES_HOME_URI);
			if (!nautilus_uris_match (home_uri, location)) {	
				nautilus_window_go_home (NAUTILUS_WINDOW (window));
			} else {
				/* the last fallback is to go to a known place that can't be deleted! */
				nautilus_window_go_to (NAUTILUS_WINDOW (window), "file:///");
			}
			g_free (home_uri);
		} else {
                /* Since this is a window, destroying it will also unref it. */
                gtk_object_destroy (GTK_OBJECT (window));
       		}
        } else {
                /* Clean up state of already-showing window */
                nautilus_window_allow_stop (window, FALSE);
                nautilus_show_error_dialog (error_message, dialog_title, GTK_WINDOW (window));

                /* Leave the location bar showing the bad location that the user
                 * typed (or maybe achieved by dragging or something). Many times
                 * the mistake will just be an easily-correctable typo. The user
                 * can choose "Refresh" to get the original URI back in the location bar.
                 */
        }

	g_free (dialog_title);
	g_free (uri_for_display);
        g_free (error_message);
}

/*
 * begin_location_change
 * 
 * Change a window's location.
 * @window: The NautilusWindow whose location should be changed.
 * @loc: A Nautilus_NavigationRequestInfo specifying info about this transition.
 * @type: Which type of location change is this? Standard, back, forward, or reload?
 * @distance: If type is back or forward, the index into the back or forward chain. If
 * type is standard or reload, this is ignored, and must be 0.
 */
static void
begin_location_change (NautilusWindow *window,
                       const char *location,
                       NautilusLocationChangeType type,
                       guint distance)
{
        NautilusDirectory *directory;
        NautilusFile *file;

        g_assert (NAUTILUS_IS_WINDOW (window));
        g_assert (location != NULL);
        g_assert (type == NAUTILUS_LOCATION_CHANGE_BACK
                  || type == NAUTILUS_LOCATION_CHANGE_FORWARD
                  || distance == 0);

        cancel_location_change (window);
        
        nautilus_window_allow_stop (window, TRUE);

        window->details->pending_location = g_strdup (location);
        window->details->location_change_type = type;
        window->details->location_change_distance = distance;

        directory = nautilus_directory_get (location);

        /* In all 4 cases, we want fresh information. */
        nautilus_directory_force_reload (directory);
        file = nautilus_directory_get_corresponding_file (directory);
        nautilus_file_invalidate_all_attributes (file);
        nautilus_file_unref (file);

        /* We start monitoring files here so we get a single load of
         * the directory instead of multiple ones. The concept is that
         * our load of the directory is shared both with the possible
         * call_when_ready done by the nautilus_determine_initial_view
         * call and loads done by components (like the icon view).
         */
        window->details->pending_location_as_directory = directory;
        nautilus_directory_file_monitor_add
                (window->details->pending_location_as_directory, window,
                 TRUE, TRUE, NULL);

        window->details->determine_view_handle = nautilus_determine_initial_view
                (location,
                 determined_initial_view_callback,
                 window);
}

static void
stop_loading (NautilusViewFrame *view)
{
        if (view != NULL) {
                nautilus_view_frame_stop (view);
        }
}

static void
stop_loading_cover (gpointer data, gpointer callback_data)
{
        g_assert (callback_data == NULL);
        stop_loading (NAUTILUS_VIEW_FRAME (data));
}

void
nautilus_window_stop_loading (NautilusWindow *window)
{
        stop_loading (window->content_view);
        stop_loading (window->new_content_view);
        g_list_foreach (window->sidebar_panels, stop_loading_cover, NULL);
        cancel_location_change (window);
}

void
nautilus_window_set_content_view (NautilusWindow *window,
                                  NautilusViewIdentifier *id)
{
	NautilusFile *file;

	g_return_if_fail (NAUTILUS_IS_WINDOW (window));
        g_return_if_fail (window->details->location != NULL);
        g_return_if_fail (window->new_content_view == NULL);
	g_return_if_fail (id != NULL);

        if (nautilus_window_content_view_matches_iid (window, id->iid)) {
        	return;
        }

	file = nautilus_file_get (window->details->location);
        nautilus_mime_set_default_component_for_file
		(file, id->iid);
        nautilus_file_unref (file);
        
        nautilus_window_allow_stop (window, TRUE);
        
        load_content_view (window, id);
}

static int
compare_view_identifier_with_iid (gconstpointer passed_view_identifier,
                                  gconstpointer passed_iid)
{
        return strcmp (((NautilusViewIdentifier *) passed_view_identifier)->iid,
                       (char *) passed_iid);
}

void
nautilus_window_set_sidebar_panels (NautilusWindow *window,
                                    GList *passed_identifier_list)
{
	GList *identifier_list;
	GList *node, *next, *found_node;
	NautilusViewFrame *sidebar_panel;
	NautilusViewIdentifier *identifier;
	const char *current_iid;

	g_return_if_fail (NAUTILUS_IS_WINDOW (window));

	/* Make a copy of the list so we can remove items from it. */
	identifier_list = g_list_copy (passed_identifier_list);
	
	/* Remove panels from the window that don't appear in the list. */
	for (node = window->sidebar_panels; node != NULL; node = next) {
		next = node->next;

		sidebar_panel = NAUTILUS_VIEW_FRAME (node->data);
		
		found_node = g_list_find_custom (identifier_list,
						 (char *) nautilus_view_frame_get_view_iid (sidebar_panel),
						 compare_view_identifier_with_iid);
		if (found_node == NULL) {
			current_iid = nautilus_view_frame_get_view_iid (sidebar_panel);
			nautilus_sidebar_hide_active_panel_if_matches (window->sidebar, current_iid);
			disconnect_and_destroy_sidebar_panel (window, sidebar_panel);
		} else {
                        identifier = (NautilusViewIdentifier *) found_node->data;

                        /* Right panel, make sure it has the right name. */
                        /* FIXME: Is this set_label necessary? Shouldn't it already
                         * have the right label here?
                         */
                        nautilus_view_frame_set_label (sidebar_panel, identifier->name);

                        /* Since this was found, there's no need to add it in the loop below. */
			identifier_list = g_list_remove_link (identifier_list, found_node);
			g_list_free_1 (found_node);
		}
        }

	/* Add panels to the window that were in the list, but not the window. */
	for (node = identifier_list; node != NULL; node = node->next) {
		g_assert (node->data != NULL);
		
		identifier = (NautilusViewIdentifier *) node->data;

                /* Create and load the panel. */
		sidebar_panel = nautilus_view_frame_new (window->details->ui_container,
                                                         window->application->undo_manager);
		nautilus_view_frame_set_label (sidebar_panel, identifier->name);
		set_view_frame_info (sidebar_panel, TRUE, identifier->name);
		connect_view (window, sidebar_panel);
		nautilus_window_add_sidebar_panel (window, sidebar_panel);
		nautilus_view_frame_load_view (sidebar_panel, identifier->iid);
                gtk_object_sink (GTK_OBJECT (sidebar_panel));
	}

	g_list_free (identifier_list);
}

static void
zoom_level_changed_callback (NautilusViewFrame *view,
                             NautilusWindow *window)
{
        g_assert (NAUTILUS_IS_WINDOW (window));

        /* This is called each time the component successfully completed
         * a zooming operation.
         */

	nautilus_zoom_control_set_zoom_level (NAUTILUS_ZOOM_CONTROL (window->zoom_control),
                                              nautilus_view_frame_get_zoom_level (view));

	nautilus_bonobo_set_sensitive (window->details->shell_ui,
				       NAUTILUS_COMMAND_ZOOM_IN,
				       nautilus_zoom_control_can_zoom_in (NAUTILUS_ZOOM_CONTROL (window->zoom_control)));
	nautilus_bonobo_set_sensitive (window->details->shell_ui,
				       NAUTILUS_COMMAND_ZOOM_OUT,
				       nautilus_zoom_control_can_zoom_out (NAUTILUS_ZOOM_CONTROL (window->zoom_control)));
	nautilus_bonobo_set_sensitive (window->details->shell_ui,
				       NAUTILUS_COMMAND_ZOOM_NORMAL,
				       TRUE);
	/* FIXME bugzilla.eazel.com 3442: Desensitize "Zoom Normal"? */
}

static void
zoom_parameters_changed_callback (NautilusViewFrame *view,
                                  NautilusWindow *window)
{
        double zoom_level;

        g_assert (NAUTILUS_IS_WINDOW (window));

        /* This callback is invoked via the "zoom_parameters_changed"
         * signal of the BonoboZoomableFrame.
         * 
         * You can rely upon this callback being called in the following
         * situations:
         *
         * - a zoomable component has been set in the NautilusViewFrame;
         *   in this case nautilus_view_frame_set_to_component() emits the
         *   "zoom_parameters_changed" signal after creating the
         *   BonoboZoomableFrame and binding it to the Bonobo::Zoomable.
         *
         *   This means that we can use the following call to
         *   nautilus_zoom_control_set_parameters() to display the zoom
         *   control when a new zoomable component has been loaded.
         *
         * - a new file has been loaded by the zoomable component; this is
         *   not 100% guaranteed since it's up to the component to emit this
         *   signal, but I consider it "good behaviour" of a component to
         *   emit this signal after loading a new file.
         */

        nautilus_zoom_control_set_parameters
                (NAUTILUS_ZOOM_CONTROL (window->zoom_control),
                 nautilus_view_frame_get_min_zoom_level (view),
                 nautilus_view_frame_get_max_zoom_level (view),
                 nautilus_view_frame_get_has_min_zoom_level (view),
                 nautilus_view_frame_get_has_max_zoom_level (view),
                 nautilus_view_frame_get_preferred_zoom_levels (view));

        /* The initial zoom level of a component is allowed to be 0.0 if
         * there is no file loaded yet. In this case we need to set the
         * commands insensitive but display the zoom control nevertheless
         * (the component is just temporarily unable to zoom, but the
         *  zoom control will "do the right thing" here).
         */
        zoom_level = nautilus_view_frame_get_zoom_level (view);
        if (zoom_level == 0.0) {
                nautilus_bonobo_set_sensitive (window->details->shell_ui,
                                               NAUTILUS_COMMAND_ZOOM_IN,
                                               FALSE);
                nautilus_bonobo_set_sensitive (window->details->shell_ui,
                                               NAUTILUS_COMMAND_ZOOM_OUT,
                                               FALSE);
                nautilus_bonobo_set_sensitive (window->details->shell_ui,
                                               NAUTILUS_COMMAND_ZOOM_NORMAL,
                                               FALSE);

                /* Don't attempt to set 0.0 as zoom level. */
                return;
        }

        /* "zoom_parameters_changed" always implies "zoom_level_changed",
         * but you won't get both signals, so we need to pass it down.
         */
        zoom_level_changed_callback (view, window);
}


static Nautilus_History *
get_history_list_callback (NautilusViewFrame *view,
                           NautilusWindow *window)
{
	Nautilus_History *list;
	NautilusBookmark *bookmark;
	int length, i;
	GList *node;
	char *name, *location, *pixbuf_xml;
	GdkPixbuf *pixbuf;
	
	/* Get total number of history items */
	length = g_list_length (nautilus_get_history_list ());

	list = Nautilus_History__alloc ();

	list->_length = length;
	list->_maximum = length;
	list->_buffer = CORBA_sequence_Nautilus_HistoryItem_allocbuf (length);
	CORBA_sequence_set_release (list, CORBA_TRUE);
	
	/* Iterate through list and copy item data */
	for (i = 0, node = nautilus_get_history_list (); i < length; i++, node = node->next) {
		bookmark = node->data;

		name = nautilus_bookmark_get_name (bookmark);
		location = nautilus_bookmark_get_uri (bookmark);
		pixbuf = nautilus_bookmark_get_pixbuf (bookmark, NAUTILUS_ICON_SIZE_FOR_MENUS);
		pixbuf_xml = bonobo_ui_util_pixbuf_to_xml (pixbuf);
		
		list->_buffer[i].title = CORBA_string_dup (name);
		list->_buffer[i].location = CORBA_string_dup (location);
		list->_buffer[i].icon = CORBA_string_dup (pixbuf_xml);

		g_free (name);
		g_free (location);
		g_free (pixbuf_xml);
		gdk_pixbuf_unref (pixbuf);		
	}

	return list;
}

static void
change_selection_callback (NautilusViewFrame *view,
                           GList *selection,
                           NautilusWindow *window)
{
        g_assert (NAUTILUS_IS_WINDOW (window));

        change_selection (window, selection, view);
}

static void
change_status_callback (NautilusViewFrame *view,
                        const char *status,
                        NautilusWindow *window)
{
        g_assert (NAUTILUS_IS_WINDOW (window));

        nautilus_window_set_status (window, status);
}

static void
failed_callback (NautilusViewFrame *view,
                 NautilusWindow *window)
{
        g_assert (NAUTILUS_IS_WINDOW (window));

        handle_view_failure (window, view);
}

static void
load_underway_callback (NautilusViewFrame *view,
                        NautilusWindow *window)
{
        g_assert (NAUTILUS_IS_WINDOW (window));

        /* FIXME bugzilla.eazel.com 2460: We intentionally ignore
         * progress from sidebar panels. Some sidebar panels may get
         * their own progress indicators later.
         */

        if (view == window->new_content_view) {
                location_has_really_changed (window);
        } else if (view == window->content_view) {
                nautilus_window_allow_stop (window, TRUE);
        }
}

static void
load_complete_callback (NautilusViewFrame *view,
                        NautilusWindow *window)
{
        g_assert (NAUTILUS_IS_WINDOW (window));

        /* FIXME bugzilla.eazel.com 2460: We intentionally ignore
         * progress from sidebar panels. Some sidebar panels may get
         * their own progress indicators later.
         */

        if (view == window->content_view) {
                end_location_change (window);
        }
}

static void
open_location_in_this_window_callback (NautilusViewFrame *view,
                                       const char *location,
                                       NautilusWindow *window)
{
        g_assert (NAUTILUS_IS_WINDOW (window));

        nautilus_window_open_location (window, location);
}

static void
open_location_prefer_existing_window_callback (NautilusViewFrame *view,
                                               const char *location,
                                               NautilusWindow *window)
{
        NautilusWindow *existing_window;
	GList *node;

        g_assert (NAUTILUS_IS_WINDOW (window));

        /* First, handle the case where there's already a window for
         * this location.
         */
        for (node = nautilus_application_get_window_list ();
             node != NULL; node = node->next) {
                existing_window = NAUTILUS_WINDOW (node->data);
                if (nautilus_uris_match (existing_window->details->location, location)) {
                        nautilus_gtk_window_present (GTK_WINDOW (existing_window));
                        return;
                }
        }

        /* Otherwise, open a new window. */
        open_location (window, location, TRUE, NULL);
}

static void
open_location_force_new_window_callback (NautilusViewFrame *view,
                                         const char *location,
                                         GList *selection,
                                         NautilusWindow *window)
{
        g_assert (NAUTILUS_IS_WINDOW (window));

        open_location (window, location, TRUE, selection);
}

static void
report_location_change_callback (NautilusViewFrame *view,
                                 const char *location,
                                 GList *selection,
                                 const char *title,
                                 NautilusWindow *window)
{
        g_assert (NAUTILUS_IS_WINDOW (window));

        if (view != window->content_view) {
                /* Do we need to do anything in this case. */
                return;
        }

        cancel_location_change (window);

        load_new_location_in_all_views (window,
                                        location,
                                        selection,
                                        view);
        
        /* Setting the change type to reload here is a hack. */
        window->details->location_change_type = NAUTILUS_LOCATION_CHANGE_RELOAD;
        window->details->pending_location = g_strdup (location);
        update_for_new_location (window);
}

static void
title_changed_callback (NautilusViewFrame *view,
                        NautilusWindow *window)
{
        g_assert (NAUTILUS_IS_WINDOW (window));

        update_title (window);
}

static void
view_loaded_callback (NautilusViewFrame *view,
                      NautilusWindow *window)
{
        g_assert (NAUTILUS_IS_WINDOW (window));

        if (view == window->new_content_view 
            && window->details->pending_location != NULL) {
                set_to_pending_location_and_selection (window);
        } else {
                /* It's a sidebar panel being loaded, or a content view
                 * being switched (with unchanged location and selection).
                 */
                if (window->details->location != NULL) {
                        load_new_location_in_one_view (view,
                                                       window->details->location,
                                                       window->details->selection);
                }
        }
        if (window->details->title != NULL) {
                nautilus_view_frame_title_changed (view, window->details->title);
        }
}

#define FOR_EACH_NAUTILUS_WINDOW_SIGNAL(macro) \
	macro (change_selection)			\
	macro (change_status)				\
	macro (failed)					\
	macro (get_history_list)			\
	macro (load_complete)				\
	macro (load_underway)				\
	macro (open_location_force_new_window)		\
	macro (open_location_in_this_window)		\
	macro (open_location_prefer_existing_window)	\
	macro (report_location_change)			\
	macro (title_changed)				\
	macro (view_loaded)				\
	macro (zoom_level_changed)			\
        macro (zoom_parameters_changed)

static void
connect_view (NautilusWindow *window, NautilusViewFrame *view)
{
	GtkObject *view_object;
	
	view_object = GTK_OBJECT (view);

	#define CONNECT(signal) gtk_signal_connect \
        	(view_object, #signal, \
                 GTK_SIGNAL_FUNC (signal##_callback), window);
        FOR_EACH_NAUTILUS_WINDOW_SIGNAL (CONNECT)
	#undef CONNECT
}

static void
disconnect_view (NautilusWindow *window, NautilusViewFrame *view)
{
	GtkObject *view_object;
	
	g_assert (NAUTILUS_IS_WINDOW (window));

	if (view == NULL) {
		return;
	}

	g_assert (NAUTILUS_IS_VIEW_FRAME (view));

	view_object = GTK_OBJECT (view);

	#define DISCONNECT(signal) gtk_signal_disconnect_by_func \
        	(view_object, \
        	 GTK_SIGNAL_FUNC (signal##_callback), window);
        FOR_EACH_NAUTILUS_WINDOW_SIGNAL (DISCONNECT)
	#undef DISCONNECT
}

static void
disconnect_view_callback (gpointer list_item_data, gpointer callback_data)
{
	disconnect_view (NAUTILUS_WINDOW (callback_data),
                         NAUTILUS_VIEW_FRAME (list_item_data));
}

void
nautilus_window_manage_views_destroy (NautilusWindow *window)
{
        free_location_change (window);

	/* Disconnect view signals here so they don't trigger when
	 * views are destroyed.
         */
	g_list_foreach (window->sidebar_panels, disconnect_view_callback, window);
        disconnect_view (window, window->content_view);

        /* Cancel callbacks. */
        cancel_viewed_file_changed_callback (window);
}

void
nautilus_window_back_or_forward (NautilusWindow *window, gboolean back, guint distance)
{
	GList *list;
	char *uri;
	
	list = back ? window->back_list : window->forward_list;
	g_assert (g_list_length (list) > distance);

	uri = nautilus_bookmark_get_uri (g_list_nth_data (list, distance));
	begin_location_change
		(window,
		 uri,
		 back ? NAUTILUS_LOCATION_CHANGE_BACK : NAUTILUS_LOCATION_CHANGE_FORWARD,
		 distance);

	g_free (uri);
}

/* reload the contents of the window */
void
nautilus_window_reload (NautilusWindow *window)
{
        g_return_if_fail (NAUTILUS_IS_WINDOW (window));

	begin_location_change
		(window, window->details->location,
		 NAUTILUS_LOCATION_CHANGE_RELOAD, 0);
}