summaryrefslogtreecommitdiff
path: root/src/ntl-window-msgs.c
blob: 776d252558f34e20f3a6e1dc79707e399dc0e66a (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */

/*
 *  Nautilus
 *
 *  Copyright (C) 1999, 2000 Red Hat, Inc.
 *  Copyright (C) 1999, 2000 Eazel, Inc.
 *
 *  This library 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 library 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 library; 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>
 */
/* Main operations needed:
 *   Initiate location change
 *   Initiate content view change
 *   Cancel action
 */

/* #define EXTREME_DEBUGGING */

#include "nautilus.h"
#include "ntl-window-private.h"
#include "ntl-index-panel.h"
#include "nautilus-location-bar.h"
#include <libnautilus/nautilus-bookmark.h>
#include <libnautilus/nautilus-gtk-extensions.h>
#include <libnautilus/nautilus-metadata.h>
#include <libnautilus/nautilus-string.h>
#include <stdarg.h>

static void nautilus_window_notify_selection_change(NautilusWindow *window,
						    NautilusView *view,
						    Nautilus_SelectionInfo *loc,
						    NautilusView *requesting_view);
static void nautilus_window_load_content_view_menu (NautilusWindow *window, NautilusNavigationInfo *ni);

typedef enum { PROGRESS_INITIAL, PROGRESS_VIEWS, PROGRESS_DONE, PROGRESS_ERROR } ProgressType;

/* Indicate progress to user interface */
static void
nautilus_window_progress_indicate(NautilusWindow *window, ProgressType type, double percent,
                                  const char *msg)
{
  if(type == PROGRESS_ERROR)
    {
      char *the_uri;

      gtk_widget_show(gnome_error_dialog_parented(msg, GTK_WINDOW(window)));

      /* If it was an error loading a URI that had been dragged to the location bar, we might
         need to reset the URI */
      the_uri = window->ni?window->ni->requested_uri:"";
      nautilus_location_bar_set_location(NAUTILUS_LOCATION_BAR(window->ent_uri),
                                           the_uri);
    }
}

/* Stays alive */
static void
Nautilus_SelectionInfo__copy(Nautilus_SelectionInfo *dest_si, Nautilus_SelectionInfo *src_si)
{
  int i, n;

  dest_si->selected_uris = src_si->selected_uris;

  n = dest_si->selected_uris._length;
  dest_si->selected_uris._buffer = CORBA_sequence_CORBA_string_allocbuf(n);
  for(i = 0; i < n; i++)
    dest_si->selected_uris._buffer[i] = CORBA_string_dup(src_si->selected_uris._buffer[i]);

  dest_si->content_view = CORBA_OBJECT_NIL;
  dest_si->self_originated = CORBA_FALSE;
}

static void
nautilus_window_notify_selection_change(NautilusWindow *window,
					NautilusView *view,
					Nautilus_SelectionInfo *loc,
					NautilusView *requesting_view)
{
  loc->self_originated = (view == requesting_view);

  nautilus_view_notify_selection_change(view, loc);
}

void
nautilus_window_request_selection_change(NautilusWindow *window,
					 Nautilus_SelectionRequestInfo *loc,
					 NautilusView *requesting_view)
{
  GSList *cur;
  Nautilus_SelectionInfo selinfo;
  CORBA_Environment environment;
  
  CORBA_exception_init(&environment);
  selinfo.selected_uris = loc->selected_uris;
  selinfo.content_view = CORBA_Object_duplicate
    (nautilus_view_get_objref(NAUTILUS_VIEW(window->content_view)),
     &environment);
  CORBA_exception_free(&environment);

  CORBA_free(window->si);

  window->si = Nautilus_SelectionInfo__alloc();
  Nautilus_SelectionInfo__copy(window->si, &selinfo);

  nautilus_window_notify_selection_change(window, window->content_view, &selinfo, requesting_view);

  for(cur = window->meta_views; cur; cur = cur->next)
    nautilus_window_notify_selection_change(window, cur->data, &selinfo, requesting_view);
}

void
nautilus_window_request_status_change(NautilusWindow *window,
                                      Nautilus_StatusRequestInfo *loc,
                                      NautilusView *requesting_view)
{
  nautilus_window_set_status(window, loc->status_string);
}

/*
 * Since this is can be called while the window is still getting set up, it
 * doesn't do any work directly.
 */
void
nautilus_window_request_progress_change(NautilusWindow *window,
					Nautilus_ProgressRequestInfo *loc,
					NautilusView *requesting_view)
{
  NautilusWindowStateItem item;
  
  if (requesting_view != window->new_content_view
      && requesting_view != window->content_view)
    return; /* Only pay attention to progress information from the upcoming content view, for now */
  
  /* If the progress indicates we are done, record that, otherwise
     just record the fact that we have at least begun loading.
  */
  switch(loc->type)
    {
    case Nautilus_PROGRESS_DONE_OK:
      item = CV_PROGRESS_DONE;
      break;
    case Nautilus_PROGRESS_DONE_ERROR:
      item = CV_PROGRESS_ERROR;
      break;
    case Nautilus_PROGRESS_UNDERWAY:
      item = CV_PROGRESS_INITIAL;
      break;
    default:
      g_assert_not_reached();
      item = -1;
      break;
    }

  nautilus_window_set_state_info(window, item, (NautilusWindowStateItem)0);
}

static char *
compute_default_title (const char *text_uri)
{
  GnomeVFSURI *vfs_uri;
  char *short_name;
	
  if (text_uri != NULL)
    {
      vfs_uri = gnome_vfs_uri_new (text_uri);
      if (vfs_uri != NULL)
        {
	  short_name = gnome_vfs_uri_extract_short_name (vfs_uri);
	  gnome_vfs_uri_unref (vfs_uri);

	  g_assert (short_name != NULL);
	  return short_name;
        }
    }

  return g_strdup(_("Nautilus"));
}

/*
 * nautilus_window_get_current_location_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 *
nautilus_window_get_current_location_title (NautilusWindow *window)
{
  return window->requested_title != NULL ? g_strdup (window->requested_title) : g_strdup (window->default_title);
   
}

/*
 * nautilus_window_update_title_internal:
 * 
 * Update the non-NautilusView 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
nautilus_window_update_title_internal (NautilusWindow *window, const char *title)
{
  char *window_title;

  if (strcmp (title, _("Nautilus")) == 0)
    {
      gtk_window_set_title (GTK_WINDOW (window), _("Nautilus"));
    }
  else
    {
      window_title = g_strdup_printf (_("Nautilus: %s"), title);
      gtk_window_set_title (GTK_WINDOW (window), window_title);
      g_free (window_title);
    }
  
  nautilus_index_panel_set_title (window->index_panel, title);
  nautilus_bookmark_set_name (window->current_location_bookmark, title);
  /* Name of item in history list may have changed, tell listeners. */
  nautilus_send_history_list_changed ();
}

/*
 * nautilus_window_reset_title_internal:
 * 
 * Update the non-NautilusView 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
nautilus_window_reset_title_internal (NautilusWindow *window, const char *uri)
{
  g_free (window->requested_title);
  window->requested_title = NULL;
  g_free (window->default_title);
  window->default_title = compute_default_title (uri);

  if (window->current_location_bookmark == NULL || 
  	strcmp (uri, nautilus_bookmark_get_uri (window->current_location_bookmark)) != 0)
    {
      /* 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 = nautilus_bookmark_new (uri);
    }

  nautilus_window_update_title_internal (window, window->default_title);
}

void
nautilus_window_request_title_change(NautilusWindow *window,
                                     const char *new_title,
                                     NautilusContentView *requesting_view)
{
  GSList *cur;

  g_return_if_fail (new_title != NULL);

  g_free (window->requested_title);
  window->requested_title = g_strdup (new_title);

  /* 
   * Tell the requesting content view also, to keep the code parallel. 
   * Don't tell the view in window->content_view because it might be 
   * the old one that's about to be removed.
   */
  nautilus_view_notify_title_change (NAUTILUS_VIEW (requesting_view), new_title);

  for(cur = window->meta_views; cur; cur = cur->next)
    {
#if 0
      /*
       * This should tell metaviews of the new title, but
       * something about it isn't right. If the metaview connects
       * to the notify_title_change signal, its callback doesn't
       * get called. And ORBit warns for each metaview about some
       * unhandled message of type 1 (REPLY). Right now no metaviews
       * use this anyway, but we need to fix it.
       */
      nautilus_view_notify_title_change (NAUTILUS_VIEW (cur->data), new_title);
#endif
    }

  /* Now change the stuff that doesn't use the NautilusView API */
  nautilus_window_update_title_internal (window, new_title);
}

/* The bulk of this file - location changing */

static void
Nautilus_NavigationInfo__copy(Nautilus_NavigationInfo *dest_ni, Nautilus_NavigationInfo *src_ni)
{
  dest_ni->requested_uri = CORBA_string_dup(src_ni->requested_uri);
  dest_ni->actual_uri = CORBA_string_dup(src_ni->actual_uri);
  dest_ni->content_type = CORBA_string_dup(src_ni->content_type);
  dest_ni->referring_uri = CORBA_string_dup(src_ni->referring_uri);
  dest_ni->actual_referring_uri = CORBA_string_dup(src_ni->actual_referring_uri);
  dest_ni->referring_content_type = CORBA_string_dup(src_ni->referring_content_type);
  dest_ni->content_view = CORBA_OBJECT_NIL;
  dest_ni->self_originated = CORBA_FALSE;
}

/* Handle the changes for the NautilusWindow itself. */
static void
nautilus_window_update_internals(NautilusWindow *window, NautilusNavigationInfo *loci)
{
  GnomeVFSURI *new_uri;
  char *current_title;

  if(loci) /* This is a location change */
    {
      Nautilus_NavigationInfo *newni;

      /* Maintain history lists. */
      if (window->location_change_type != NAUTILUS_LOCATION_CHANGE_RELOAD)
        {        
	  /* Always add new location to history list. */
	  nautilus_add_to_history_list (window->current_location_bookmark);

          if (window->location_change_type == NAUTILUS_LOCATION_CHANGE_BACK)
            {
              guint index;
              
              /* Going back. Move items from the back list to the forward list. */
              g_assert(g_slist_length (window->back_list) > window->location_change_distance);
              g_assert(!strcmp(nautilus_bookmark_get_uri (NAUTILUS_BOOKMARK (g_slist_nth_data (window->back_list, window->location_change_distance))), loci->navinfo.requested_uri));
              g_assert(window->ni);

	      /* Move current location to Forward list */
	      g_assert (strcmp (nautilus_bookmark_get_uri (window->last_location_bookmark), window->ni->requested_uri) == 0);
	      /* Use the first bookmark in the history list rather than creating a new one. */
              window->forward_list = g_slist_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 (index = 0; index < window->location_change_distance; ++index) 
                {
		  NautilusBookmark *bookmark;

                  bookmark = window->back_list->data;
             	  window->back_list = g_slist_remove_link (window->back_list, window->back_list);
             	  window->forward_list = g_slist_prepend (window->forward_list, bookmark);
                }

	      /* One bookmark falls out of back/forward lists and becomes viewed location */
              gtk_object_unref (window->back_list->data);
              window->back_list = g_slist_remove_link (window->back_list, window->back_list);
            }
          else if (window->location_change_type == NAUTILUS_LOCATION_CHANGE_FORWARD)
            {
              guint index;
              
              /* Going back. Move items from the forward list to the back list. */
              g_assert(g_slist_length (window->forward_list) > window->location_change_distance);
              g_assert(!strcmp(nautilus_bookmark_get_uri (NAUTILUS_BOOKMARK (g_slist_nth_data (window->forward_list, window->location_change_distance))), loci->navinfo.requested_uri));
              g_assert(window->ni);

              /* Move current location to Back list */
	      g_assert (strcmp (nautilus_bookmark_get_uri (window->last_location_bookmark), window->ni->requested_uri) == 0);
	      /* Use the first bookmark in the history list rather than creating a new one. */
              window->back_list = g_slist_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 (index = 0; index < window->location_change_distance; ++index) 
                {
                  NautilusBookmark *bookmark;

                  bookmark = window->forward_list->data;
             	  window->forward_list = g_slist_remove_link (window->forward_list, window->forward_list);
             	  window->back_list = g_slist_prepend (window->back_list, bookmark);
                }

	      /* One bookmark falls out of back/forward lists and becomes viewed location */
              gtk_object_unref (window->forward_list->data);
              window->forward_list = g_slist_remove_link (window->forward_list, window->forward_list);
            }
          else
            {
              g_assert (window->location_change_type == NAUTILUS_LOCATION_CHANGE_STANDARD);
              /* Clobber the entire forward list, and move displayed location to back list */
              if (window->forward_list)
                {
                  g_slist_foreach(window->forward_list, (GFunc)gtk_object_unref, NULL);
                  g_slist_free(window->forward_list); window->forward_list = NULL;
                }

              if (window->ni)
                {
                  /* Store bookmark for current location in back list, unless there is no current location */
		  g_assert (strcmp (nautilus_bookmark_get_uri (window->last_location_bookmark), window->ni->requested_uri) == 0);
  	          /* Use the first bookmark in the history list rather than creating a new one. */
                  window->back_list = g_slist_prepend (window->back_list, window->last_location_bookmark);
                  gtk_object_ref (GTK_OBJECT (window->back_list->data));
		}
            }
        }

      new_uri = gnome_vfs_uri_new (loci->navinfo.requested_uri);
      if(!new_uri)
        new_uri = gnome_vfs_uri_new (loci->navinfo.actual_uri);
      if(new_uri)
        {
          nautilus_window_allow_up(window, 
                                   gnome_vfs_uri_has_parent(new_uri));
          gnome_vfs_uri_unref(new_uri);
        }
      else
        nautilus_window_allow_up(window, FALSE);

      newni = Nautilus_NavigationInfo__alloc();
      Nautilus_NavigationInfo__copy(newni, &loci->navinfo);

      CORBA_free(window->ni);
      
      window->ni = newni;

      CORBA_free(window->si);
      window->si = NULL;

      nautilus_window_load_content_view_menu (window, loci);
    }

  nautilus_window_allow_back(window, window->back_list != NULL);
  nautilus_window_allow_forward(window, window->forward_list != NULL);

  nautilus_location_bar_set_location(NAUTILUS_LOCATION_BAR(window->ent_uri),
                                     window->ni->requested_uri);

  /*
   * Notify the index panel of the location change. FIXME: Eventually,
   * this will not be necessary when we restructure the index panel to
   * be a NautilusView.
   */
  current_title = nautilus_window_get_current_location_title (window);
  nautilus_index_panel_set_uri (window->index_panel, window->ni->requested_uri, current_title);
  g_free (current_title);
}

static void
nautilus_window_update_view(NautilusWindow *window,
                            NautilusView *view,
                            Nautilus_NavigationInfo *loci,
                            Nautilus_SelectionInfo *seli,
                            NautilusView *requesting_view,
			    NautilusView *content_view)
{
  CORBA_Environment environment;
  char *current_title;
  
  g_return_if_fail(view);
  
  loci->self_originated = (view == requesting_view);

  current_title = nautilus_window_get_current_location_title (window);
  g_assert (current_title != NULL);
  nautilus_view_notify_location_change (view, loci, current_title);
  g_free (current_title);

  if(seli)
    {
      CORBA_exception_init(&environment);
      CORBA_Object_release(seli->content_view, &environment);
      seli->content_view = CORBA_Object_duplicate
        (nautilus_view_get_client_objref(content_view),
         &environment);
      CORBA_exception_free(&environment);

      nautilus_window_notify_selection_change(window, view, seli, NULL);
    }
}

void
nautilus_window_view_destroyed(NautilusView *view, NautilusWindow *window)
{
  NautilusWindowStateItem item = VIEW_ERROR;
  nautilus_window_set_state_info(window, item, view, (NautilusWindowStateItem)0);
}

/* This is called when we have decided we can actually change to the new view/location situation. */
static void
nautilus_window_has_really_changed(NautilusWindow *window)
{
  GSList *cur, *discard_views;
  GSList *new_meta_views;

  new_meta_views = window->new_meta_views;
  window->new_meta_views = NULL;

  if(window->new_content_view)
    {
      if(!GTK_WIDGET(window->new_content_view)->parent)
        {
          if(window->content_view)
            gtk_signal_disconnect_by_func(GTK_OBJECT(window->content_view), nautilus_window_view_destroyed, window);
          nautilus_window_set_content_view(window, window->new_content_view);
        }
      gtk_object_unref(GTK_OBJECT(window->new_content_view));
      window->new_content_view = NULL;
    }

  if(new_meta_views)
    {
      /* Do lots of shuffling to make sure we don't remove views that were already there, but add new views */
      for(cur = new_meta_views; cur; cur = cur->next)
        {
          if(!GTK_OBJECT_DESTROYED(cur->data) && !GTK_WIDGET(cur->data)->parent)
            nautilus_window_add_meta_view(window, cur->data);
          gtk_object_unref(cur->data);
        }

      for(discard_views = NULL, cur = window->meta_views; cur; cur = cur->next)
        if(!g_slist_find(new_meta_views, cur->data))
          discard_views = g_slist_prepend(discard_views, cur->data);
      g_slist_free(new_meta_views);
  
      for(cur = discard_views; cur; cur = cur->next)
        nautilus_window_remove_meta_view(window, cur->data);
      g_slist_free(discard_views);
    }

  if(window->pending_ni)
    {
      nautilus_window_update_internals(window, window->pending_ni);
      nautilus_navinfo_free(window->pending_ni); window->pending_ni = NULL;
    }
}

/* This is called when we are done loading to get rid of the load_info structure. */
static void
nautilus_window_free_load_info(NautilusWindow *window)
{
#if defined(EXTREME_DEBUGGING)
  g_message("-> FREE_LOAD_INFO <-");
#endif

  if (window->pending_ni)
    nautilus_navinfo_free(window->pending_ni);
  window->pending_ni = NULL;
  window->error_views = NULL;
  window->new_meta_views = NULL;
  window->new_content_view = NULL;
  window->cancel_tag = NULL;
  window->action_tag = 0;
  window->made_changes = 0;
  window->state = NW_IDLE;
  window->changes_pending =
    window->views_shown =
    window->view_bombed_out =
    window->view_activation_complete =
    window->cv_progress_initial =
    window->cv_progress_done =
    window->cv_progress_error = 
    window->sent_update_view =
    window->reset_to_idle = FALSE;
}

/* Meta view handling */
static NautilusView *
nautilus_window_load_meta_view(NautilusWindow *window,
                               const char *iid,
                               NautilusView *requesting_view)
{
  NautilusView *meta_view = NULL;
  GSList *curview;

  for(curview = window->meta_views; curview; curview = curview->next)
    {
      meta_view = NAUTILUS_VIEW(curview->data);
      if(!strcmp(nautilus_view_get_iid(meta_view), iid))
        break;
    }

  if(!curview)
    {
      meta_view = NAUTILUS_VIEW(gtk_widget_new(nautilus_meta_view_get_type(), "main_window", window, NULL));
      nautilus_window_connect_view(window, meta_view);
      if(!nautilus_view_load_client(meta_view, iid))
	{
	  gtk_widget_unref(GTK_WIDGET(meta_view));
	  meta_view = NULL;
	}
    }

  if(meta_view)
    {
      gtk_object_ref(GTK_OBJECT(meta_view));

      nautilus_view_set_active_errors(meta_view, TRUE);
    }

  return meta_view;
}

void
nautilus_window_request_location_change(NautilusWindow *window,
					Nautilus_NavigationRequestInfo *loc,
					NautilusView *requesting_view)
{  
  if (loc->new_window_requested) 
    {
      NautilusWindow *new_window;

      new_window = nautilus_app_create_window (NAUTILUS_APP(window->app));
      nautilus_window_set_initial_state (new_window, loc->requested_uri);
    }
  else
    {
      nautilus_window_begin_location_change(window, loc, requesting_view, NAUTILUS_LOCATION_CHANGE_STANDARD, 0);
    }
}

static NautilusView *
nautilus_window_load_content_view(NautilusWindow *window,
                                  const char *iid,
                                  Nautilus_NavigationInfo *navinfo,
                                  NautilusView **requesting_view)
{
  NautilusView *content_view = window->content_view;
  NautilusView *new_view;
  CORBA_Environment environment;
  
  g_return_val_if_fail(iid, NULL);
  g_return_val_if_fail(navinfo, NULL);

  if((!content_view || !NAUTILUS_IS_VIEW(content_view)) || strcmp(nautilus_view_get_iid(content_view), iid))
    {
      if(requesting_view != NULL && *requesting_view == window->content_view)
        {
          /* If we are going to be zapping the old view,
             we definitely don't want any of the new views
             thinking they made the request */
          *requesting_view = NULL;
        }

      new_view = NAUTILUS_VIEW(gtk_widget_new(nautilus_content_view_get_type(), "main_window", window, NULL));

      nautilus_window_connect_content_view (window, NAUTILUS_CONTENT_VIEW (new_view));

      if(!nautilus_view_load_client(new_view, iid))
	{
	  gtk_widget_unref(GTK_WIDGET(new_view));
	  new_view = NULL;
	}
      /* Avoid being fooled by extra done notifications from the last view. 
         This is a HACK because the state machine SUCKS. */
      window->cv_progress_done = FALSE;
      window->cv_progress_error = FALSE;
    }
  else
    new_view = window->content_view;

  if(new_view && NAUTILUS_IS_VIEW (new_view))
    {
      gtk_object_ref (GTK_OBJECT (new_view));
      
      CORBA_exception_init(&environment);
      CORBA_Object_release(navinfo->content_view, &environment);
      navinfo->content_view = CORBA_Object_duplicate
        (nautilus_view_get_client_objref (new_view),
         &environment);
      CORBA_exception_free(&environment);

      nautilus_view_set_active_errors (new_view, TRUE);
    }
  else
    new_view = NULL;

  return new_view;
}

static gboolean
nautilus_window_update_state(gpointer data)
{
  NautilusWindow *window = data;
  GSList *cur;
  gboolean retval = FALSE;

  if(window->making_changes)
    {
#ifdef EXTREME_DEBUGGING
      g_message("In the middle of making changes %d (action_tag %d) - RETURNING",
                window->making_changes, window->action_tag);
#endif
      return FALSE;
    }

  window->made_changes = 0;
  window->making_changes++;

#ifdef EXTREME_DEBUGGING
  g_message(">>> nautilus_window_update_state (action tag is %d):", window->action_tag);
  g_print("made_changes %d, making_changes %d\n", window->made_changes, window->making_changes);
  g_print("changes_pending %d, location_change_type %d, views_shown %d, view_bombed_out %d, view_activation_complete %d\n",
          window->changes_pending, window->location_change_type, window->views_shown,
          window->view_bombed_out, window->view_activation_complete);
  g_print("sent_update_view %d, cv_progress_initial %d, cv_progress_done %d, cv_progress_error %d, reset_to_idle %d\n",
          window->sent_update_view, window->cv_progress_initial, window->cv_progress_done, window->cv_progress_error,
          window->reset_to_idle);
#endif

  /* Now make any needed state changes based on available information */
  if(window->view_bombed_out && window->error_views)
    {
      for(cur = window->error_views; cur; cur = cur->next)
        {
          NautilusView *error_view = cur->data;

          if(NAUTILUS_IS_CONTENT_VIEW(error_view))
            {
              if(error_view == window->new_content_view)
                {
                  window->made_changes++;
                  window->reset_to_idle = TRUE;
                }

              if(error_view == window->content_view)
                {
                  if(GTK_WIDGET(window->content_view)->parent)
                    gtk_container_remove(GTK_CONTAINER(GTK_WIDGET(window->content_view)->parent),
                                         GTK_WIDGET(window->content_view));

                  window->content_view = NULL;
                  window->made_changes++;
                }
              window->cv_progress_error = TRUE;
            }
          else if(NAUTILUS_IS_META_VIEW(error_view))
            {
              if(g_slist_find(window->new_meta_views, error_view))
                {
                  window->new_meta_views = g_slist_remove(window->new_meta_views, error_view);
                  gtk_widget_unref(GTK_WIDGET(error_view));
                }
              if(g_slist_find(window->meta_views, error_view))
                {
                  nautilus_window_remove_meta_view(window, error_view);
                }
            }
          gtk_widget_unref(GTK_WIDGET(error_view));
        }
      g_slist_free(window->error_views);
      window->error_views = NULL;

      window->view_bombed_out = FALSE;
    }

  if(window->reset_to_idle)
    {
      GSList *cur;

#ifdef EXTREME_DEBUGGING
      g_message("Reset to idle!");
#endif

      window->changes_pending = FALSE;
      window->made_changes++;
      window->reset_to_idle = FALSE;

      if(window->cancel_tag)
        {
          gnome_vfs_async_cancel(window->cancel_tag);
          if(window->pending_ni)
            window->pending_ni->ah = NULL;
          window->cancel_tag = NULL;
        }

      if(window->pending_ni)
        {
          nautilus_window_reset_title_internal (window, window->ni->requested_uri);
        
          /* Tell previously-notified views to go back to the old page */
          for(cur = window->meta_views; cur; cur = cur->next)
            {
              if(g_slist_find(window->new_meta_views, cur->data))
                nautilus_window_update_view(window, cur->data, window->ni, window->si,
                                            NULL, window->content_view);
            }


          if(window->new_content_view
             && window->new_content_view == window->content_view)
            nautilus_window_update_view(window, window->content_view, window->ni, window->si,
                                        NULL, window->content_view);
        }

      if(window->new_content_view)
        gtk_widget_unref(GTK_WIDGET(window->new_content_view));

      for(cur = window->new_meta_views; cur; cur = cur->next)
        gtk_widget_unref(GTK_WIDGET(cur->data));
      g_slist_free(window->new_meta_views);
  
      nautilus_window_free_load_info(window);

      nautilus_window_allow_stop(window, FALSE);
    }

  if(window->changes_pending)
    {
      window->state = NW_LOADING_VIEWS;

#ifdef EXTREME_DEBUGGING
      g_message("Changes pending");
#endif

      if(window->pending_ni && !window->new_content_view && !window->cv_progress_error
         && !window->view_activation_complete)
        {
          window->new_content_view = nautilus_window_load_content_view(window, window->pending_ni->initial_content_iid,
                                                                       &window->pending_ni->navinfo,
                                                                       &window->new_requesting_view);

          for(cur = window->pending_ni->meta_iids; cur; cur = cur->next)
            {
              NautilusView *meta_view = nautilus_window_load_meta_view(window, cur->data, window->new_requesting_view);

              if(meta_view)
                window->new_meta_views = g_slist_prepend(window->new_meta_views, meta_view);
            }

          window->view_activation_complete = TRUE;

          window->made_changes++;
        }

      if(window->view_activation_complete
         && !window->sent_update_view)
        {
          Nautilus_NavigationInfo *ni;
          Nautilus_SelectionInfo *si;

          if(window->pending_ni)
            {
              ni = &window->pending_ni->navinfo;
              si = NULL;
            }
          else
            {
              ni = window->ni;
              si = window->si;
            }

	  nautilus_window_reset_title_internal (window, ni->requested_uri);

#ifdef EXTREME_DEBUGGING
          g_message("!!! Sending update_view");
#endif

          if(window->new_content_view)
            nautilus_window_update_view(window, window->new_content_view, ni, si,
                                        window->new_requesting_view, window->new_content_view);
          else
            window->cv_progress_error = TRUE;

          for(cur = window->new_meta_views; cur; cur = cur->next)
            nautilus_window_update_view(window, cur->data, ni, si,
                                        window->new_requesting_view, window->new_content_view);

          window->sent_update_view = TRUE;
          window->made_changes++;
        }

      if(!window->cv_progress_error
         && window->view_activation_complete
         && window->cv_progress_initial
         && !window->views_shown)
        {
          nautilus_window_has_really_changed(window);
          window->views_shown = TRUE;
          window->made_changes++;
        }

      if(window->cv_progress_error
         || window->cv_progress_done)
        {
          window->made_changes++;
          window->reset_to_idle = TRUE;
#ifdef EXTREME_DEBUGGING
          g_message("cv_progress_(error|done) kicking in");
#endif
        }
    }

  if(window->made_changes)
    {
      if(!window->action_tag)
        window->action_tag = g_idle_add_full(G_PRIORITY_LOW, nautilus_window_update_state, window, NULL);

      retval = TRUE;
      window->made_changes = 0;
    }
  else
    window->action_tag = 0;

  window->making_changes--;

#ifdef EXTREME_DEBUGGING
  g_message("update_state done (new action tag is %d, making_changes is %d) <<<", window->action_tag,
            window->making_changes);
#endif

  return retval;
}

void
nautilus_window_set_state_info(NautilusWindow *window, ...)
{
  va_list args;
  NautilusWindowStateItem item_type;
  NautilusView *new_view;
  gboolean do_sync = FALSE;

  if(window->made_changes) /* Ensure that changes happen in-order */
    {
      if(window->action_tag)
        {
          g_source_remove(window->action_tag);
          window->action_tag = 0;
        }

      nautilus_window_update_state(window);
    }

  va_start(args, window);

  while((item_type = va_arg(args, NautilusWindowStateItem)))
    {
      switch(item_type)
        {
        case NAVINFO_RECEIVED: /* The information needed for a location change to continue has been received */
#ifdef EXTREME_DEBUGGING
          g_message("NAVINFO_RECEIVED"); 
#endif
          window->pending_ni = va_arg(args, NautilusNavigationInfo*);
          window->cancel_tag = NULL;
          window->changes_pending = TRUE;
          break;
        case VIEW_ERROR:
          window->view_bombed_out = TRUE;
          new_view = va_arg(args, NautilusView*);
#ifdef EXTREME_DEBUGGING
          g_message("VIEW_ERROR on %p", new_view);
#endif
          gtk_object_ref(GTK_OBJECT(new_view)); /* Ya right */
          window->error_views = g_slist_prepend(window->error_views, new_view);
          break;
        case NEW_CONTENT_VIEW_ACTIVATED:
#ifdef EXTREME_DEBUGGING
          g_message("NEW_CONTENT_VIEW_ACTIVATED");
#endif
          g_return_if_fail(!window->new_content_view);
          new_view = va_arg(args, NautilusView*);
          /* Don't ref here, reference is held by widget hierarchy. */
          window->new_content_view = new_view;
          if(!window->pending_ni) {
            window->view_activation_complete = TRUE;
          }
          window->changes_pending = TRUE;
          window->views_shown = FALSE;
          break;
        case NEW_META_VIEW_ACTIVATED:
#ifdef EXTREME_DEBUGGING
          g_message("NEW_META_VIEW_ACTIVATED");
#endif
          new_view = va_arg(args, NautilusView*);
          /* Don't ref here, reference is held by widget hierarchy. */
          window->new_meta_views = g_slist_prepend(window->new_meta_views, new_view);
          window->changes_pending = TRUE;
          window->views_shown = FALSE;
          break;
        case CV_PROGRESS_INITIAL: /* We have received an "I am loading" indication from the content view */
#ifdef EXTREME_DEBUGGING
          g_message("CV_PROGRESS_INITIAL");
#endif
          window->cv_progress_initial = TRUE;
          window->cv_progress_done = window->cv_progress_error = FALSE;
          window->changes_pending = TRUE;
          break;
        case CV_PROGRESS_ERROR: /* We have received a load error from the content view */
          window->cv_progress_error = TRUE;
#ifdef EXTREME_DEBUGGING
          g_message("CV_PROGRESS_ERROR");
#endif
          break;
        case CV_PROGRESS_DONE: /* The content view is done loading */
          window->cv_progress_done = TRUE;
#ifdef EXTREME_DEBUGGING
          g_message("CV_PROGRESS_DONE");
#endif
          break;
        case RESET_TO_IDLE: /* Someone pressed the stop button or something */
          window->reset_to_idle = TRUE;
#ifdef EXTREME_DEBUGGING
          g_message("RESET_TO_IDLE");
#endif
          break;
        case SYNC_STATE:
          do_sync = TRUE;
#ifdef EXTREME_DEBUGGING
          g_message("SYNC_STATE");
#endif
          break;
        default:
          break;
        }
    }

  va_end(args);

  window->made_changes++;
  if(!window->making_changes)
    {
      if(do_sync)
        {
          if(window->action_tag)
            {
#ifdef EXTREME_DEBUGGING
              g_message("Doing sync - action_tag was %d", window->action_tag);
#endif
              g_source_remove(window->action_tag);
              window->action_tag = 0;
            }
          if(nautilus_window_update_state(window))
            do_sync = FALSE;
        }

      if(!window->action_tag && !do_sync)
        {
          window->action_tag = g_idle_add_full(G_PRIORITY_LOW, nautilus_window_update_state, window, NULL);
#ifdef EXTREME_DEBUGGING
          g_message("Added callback to update_state - tag is %d", window->action_tag);
#endif
        }
    }
}

static void
nautilus_window_end_location_change_callback (NautilusNavigationInfo *navi, gpointer data)
{
  NautilusWindow *window = data;
  char *requested_uri;
  char *error_message;

  g_assert (navi != NULL);

  window->cancel_tag = NULL;

  if (navi->result_code == NAUTILUS_NAVIGATION_RESULT_OK)
    {
      /* Navigation successful. Show the window to handle the
       * new-window case. (Doesn't hurt if window is already showing.)
       * Maybe this should go sometime later so the blank window isn't
       * on screen so long.
       */
       gtk_widget_show (GTK_WIDGET (window));
       nautilus_window_set_state_info (window, 
       				       (NautilusWindowStateItem)NAVINFO_RECEIVED, 
       				       navi, 
       				       (NautilusWindowStateItem)0);
       return;
    }

  /* Some sort of failure occurred. How 'bout we tell the user? */
  requested_uri = navi->navinfo.requested_uri;

  switch (navi->result_code)
    {
      case NAUTILUS_NAVIGATION_RESULT_NOT_FOUND:
        error_message = g_strdup_printf (_("Couldn't find \"%s\".\nPlease check the spelling and try again."), requested_uri);
        break;
      case NAUTILUS_NAVIGATION_RESULT_INVALID_URI:
        error_message = g_strdup_printf (_("\"%s\" is not a valid location.\nPlease check the spelling and try again."), requested_uri);
        break;
      case NAUTILUS_NAVIGATION_RESULT_NO_HANDLER_FOR_TYPE:
      	error_message = g_strdup_printf ("Couldn't display \"%s\",\nbecause Nautilus cannot handle items of this type.", requested_uri);
        break;
      case NAUTILUS_NAVIGATION_RESULT_UNSUPPORTED_SCHEME:
      {
        /* Can't create a vfs_uri and get the method from that, because 
         * gnome_vfs_uri_new might return NULL.
         */
        char * scheme_string;

        scheme_string = nautilus_str_get_prefix (requested_uri, ":");
        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\",\nbecause Nautilus cannot handle %s: locations.", requested_uri, scheme_string);
	g_free (scheme_string);
        break;
      }
      default:
        /* It is so sad that we can't say anything more useful than this.
         * When this comes up, we should figure out what's really happening
         * and add another specific case.
         */
      	error_message = g_strdup_printf ("Nautilus cannot display \"%s\".", requested_uri);
    }

  if (navi != NULL)
    nautilus_navinfo_free(navi);

  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. 
       */
      gtk_object_destroy (GTK_OBJECT (window));
      gtk_widget_show (gnome_error_dialog (error_message));
    }
  else
    {
      /* Clean up state of already-showing window */
      nautilus_window_allow_stop(window, FALSE);
      nautilus_window_progress_indicate(window, PROGRESS_ERROR, 0, error_message);
    }

    g_free (error_message);
}

/*
 * nautilus_window_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.
 * @requesting_view: The view from which this location change originated, can be NULL.
 * @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.
 */
void
nautilus_window_begin_location_change(NautilusWindow *window,
				      Nautilus_NavigationRequestInfo *loc,
				      NautilusView *requesting_view,
				      NautilusLocationChangeType type,
				      guint distance)
{
  const char *current_iid;

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

  nautilus_window_set_state_info(window, (NautilusWindowStateItem)RESET_TO_IDLE, (NautilusWindowStateItem)SYNC_STATE, (NautilusWindowStateItem)0);

  while (gdk_events_pending())
    gtk_main_iteration();

  nautilus_window_progress_indicate(window, PROGRESS_INITIAL, 0, _("Gathering information"));

  window->location_change_type = type;
  window->location_change_distance = distance;
  window->new_requesting_view = requesting_view;

  nautilus_window_allow_stop(window, TRUE);

  current_iid = NULL;
  if (window->content_view != NULL) 
    {
      current_iid = nautilus_view_get_iid (window->content_view);
    }

  window->cancel_tag =
    nautilus_navinfo_new(loc, window->ni, nautilus_window_end_location_change_callback, window, current_iid);
}


/******** content view switching **********/
static void
view_menu_switch_views_cb (GtkWidget *widget, gpointer data)
{
  NautilusWindow *window;
  NautilusView *view;
  NautilusDirectory *directory;
  char *iid;

  g_return_if_fail (GTK_IS_MENU_ITEM (widget));
  g_return_if_fail (NAUTILUS_IS_WINDOW (gtk_object_get_user_data(GTK_OBJECT(widget))));
  g_return_if_fail (data != NULL);

  window = NAUTILUS_WINDOW (gtk_object_get_user_data (GTK_OBJECT(widget)));
  g_assert (window->ni != NULL);

  iid = (char *)data;

  directory = nautilus_directory_get (window->ni->requested_uri);
  g_assert (directory != NULL);
  nautilus_directory_set_metadata (directory,
  				   NAUTILUS_INITIAL_VIEW_METADATA_KEY,
  				   NULL,
  				   iid);
  
  nautilus_window_allow_stop(window, TRUE);

  view = nautilus_window_load_content_view (window, iid, window->ni, NULL);
  nautilus_window_set_state_info (window, (NautilusWindowStateItem)NEW_CONTENT_VIEW_ACTIVATED, view, (NautilusWindowStateItem)0);
}

/*
 * FIXME: Probably this should be moved to ntl-window.c with the rest of the UI.
 * I was waiting until we had the framework settled before doing that.
 */
static void
nautilus_window_load_content_view_menu (NautilusWindow *window, NautilusNavigationInfo *ni)
{
  GSList *iter_new;
  GtkWidget *new_menu;
  gint index, default_view_index;

  g_return_if_fail (NAUTILUS_IS_WINDOW (window));
  g_return_if_fail (GTK_IS_OPTION_MENU (window->option_cvtype));
  g_return_if_fail (ni != NULL);

  new_menu = gtk_menu_new ();

  /* Add a menu item for each available content view type */
  iter_new = ni->content_identifiers;
  index = 0;
  default_view_index = -1;
  while (iter_new != NULL)
    {
      GtkWidget *menu_item;
      NautilusViewIdentifier *identifier;
      gchar *menu_label;

      identifier = (NautilusViewIdentifier *)iter_new->data;
      menu_label = g_strdup_printf (_("View as %s"), identifier->name);
      menu_item = gtk_menu_item_new_with_label (menu_label);
      g_free (menu_label);
    
      if (strcmp (identifier->iid, ni->initial_content_iid) == 0)
        {
          default_view_index = index;
        }

      /* Free copy of iid string when signal disconnected. */
      nautilus_gtk_signal_connect_free_data (
                                             GTK_OBJECT (menu_item), 
                                             "activate", 
                                             GTK_SIGNAL_FUNC (view_menu_switch_views_cb), 
                                             g_strdup (identifier->iid));
      /* Store reference to window in item; no need to free this. */
      gtk_object_set_user_data (GTK_OBJECT (menu_item), window);
      gtk_menu_append (GTK_MENU (new_menu), menu_item);
      gtk_widget_show (menu_item);
      iter_new = g_slist_next (iter_new);
      ++index;
    }

  /*
   * We create and attach a new menu here because adding/removing
   * items from existing menu screws up the size of the option menu.
   */

  gtk_option_menu_set_menu (GTK_OPTION_MENU (window->option_cvtype), new_menu);

  g_assert (default_view_index >= 0);
  gtk_option_menu_set_history (GTK_OPTION_MENU (window->option_cvtype), 
  			       default_view_index);
}