ntl-window-msgs.c 28.6 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
/* -*- 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.
 *
23 24 25 26 27 28 29
 *  Authors: Elliot Lee <sopwith@redhat.com>
 *           John Sullivan <sullivan@eazel.com>
 */
/* Main operations needed:
 *   Initiate location change
 *   Initiate content view change
 *   Cancel action
30 31
 */

32 33
#include "nautilus.h"
#include "ntl-window-private.h"
34
#include "ntl-index-panel.h"
35
#include "explorer-location-bar.h"
36
#include <libnautilus/nautilus-gtk-extensions.h>
37
#include <stdarg.h>
38

39 40 41 42
static void nautilus_window_notify_selection_change(NautilusWindow *window,
						    NautilusView *view,
						    Nautilus_SelectionInfo *loc,
						    NautilusView *requesting_view);
43
static void nautilus_window_refresh_title (NautilusWindow *window);
44
static void nautilus_window_load_content_view_menu (NautilusWindow *window, NautilusNavigationInfo *ni);
45

Elliot Lee's avatar
Elliot Lee committed
46 47 48 49 50 51 52 53 54 55
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)
{
  /* Do nothing for now */
}

56
/* Stays alive */
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72
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;
}

73 74 75 76 77
static void
nautilus_window_notify_selection_change(NautilusWindow *window,
					NautilusView *view,
					Nautilus_SelectionInfo *loc,
					NautilusView *requesting_view)
78
{
79 80 81
  loc->self_originated = (view == requesting_view);

  nautilus_view_notify_selection_change(view, loc);
82 83 84 85 86
}

void
nautilus_window_request_selection_change(NautilusWindow *window,
					 Nautilus_SelectionRequestInfo *loc,
Maciej Stachowiak's avatar
Maciej Stachowiak committed
87
					 NautilusView *requesting_view)
88
{
Maciej Stachowiak's avatar
Maciej Stachowiak committed
89 90
  GSList *cur;
  Nautilus_SelectionInfo selinfo;
91

Maciej Stachowiak's avatar
Maciej Stachowiak committed
92 93
  selinfo.selected_uris = loc->selected_uris;
  selinfo.content_view = nautilus_view_get_objref(NAUTILUS_VIEW(window->content_view));
94

Maciej Stachowiak's avatar
Maciej Stachowiak committed
95 96 97 98 99 100 101 102 103
  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);
104 105 106
}

void
107 108 109
nautilus_window_request_status_change(NautilusWindow *window,
                                      Nautilus_StatusRequestInfo *loc,
                                      NautilusView *requesting_view)
110
{
111
  nautilus_window_set_status(window, loc->status_string);
112 113
}

114 115 116 117
/*
 * Since this is can be called while the window is still getting set up, it
 * doesn't do any work directly.
 */
118 119 120
void
nautilus_window_request_progress_change(NautilusWindow *window,
					Nautilus_ProgressRequestInfo *loc,
Maciej Stachowiak's avatar
Maciej Stachowiak committed
121
					NautilusView *requesting_view)
122
{
123
  NautilusWindowStateItem item;
124
  
125 126
  if (requesting_view != window->new_content_view
      && requesting_view != window->content_view)
127
    return; /* Only pay attention to progress information from the upcoming content view, for now */
128 129 130 131
  
  /* If the progress indicates we are done, record that, otherwise
     just record the fact that we have at least begun loading.
  */
132
  switch(loc->type)
133
    {
134 135 136 137 138 139 140 141 142
    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;
Elliot Lee's avatar
Elliot Lee committed
143 144 145 146
    default:
      g_assert_not_reached();
      item = -1;
      break;
147
    }
148

149
  nautilus_window_set_state_info(window, item, (NautilusWindowStateItem)0);
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
}

/* 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;
165 166
}

167
/* Handle the changes for the NautilusWindow itself. */
168
static void
169
nautilus_window_update_internals(NautilusWindow *window, NautilusNavigationInfo *loci)
170
{
171 172
  GnomeVFSURI *new_uri;

173
  if(loci) /* This is a location change */
174
    {
175
      Nautilus_NavigationInfo *newni;
176

177 178 179 180
      /* Maintain history lists. */
      if (window->is_back)
        {
          /* Going back. Remove one item from the prev list and add the current item to the next list. */
181

182 183 184
          g_assert(window->uris_prev);
          g_assert(!strcmp((const char*)window->uris_prev->data, loci->navinfo.requested_uri));
          g_assert(window->ni);
185

186 187 188 189
          window->uris_next = g_slist_prepend(window->uris_next, g_strdup(window->ni->requested_uri));
          g_free(window->uris_prev->data);
          window->uris_prev = g_slist_remove_link(window->uris_prev, window->uris_prev);
        }
190
      else
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208
        {
          /* Going forward. Remove one item from the next if it's the same as the the request.
             Otherwise, clobber the entire next list.
          */

          if (window->uris_next && !strcmp(loci->navinfo.requested_uri, (const char*)window->uris_next->data))
            {
              g_free(window->uris_next->data);
              window->uris_next = g_slist_remove_link(window->uris_next, window->uris_next);
            }
          else
            {
              g_slist_foreach(window->uris_next, (GFunc)g_free, NULL);
              g_slist_free(window->uris_next); window->uris_next = NULL;
            }
          if (window->ni)
            window->uris_prev = g_slist_prepend(window->uris_prev, g_strdup(window->ni->requested_uri));
        }
209

210 211 212 213
      new_uri = gnome_vfs_uri_new (loci->navinfo.requested_uri);
      nautilus_window_allow_up(window, 
                               gnome_vfs_uri_has_parent(new_uri));
      gnome_vfs_uri_unref(new_uri);
214

215
      newni = Nautilus_NavigationInfo__alloc();
216
      Nautilus_NavigationInfo__copy(newni, &loci->navinfo);
217 218 219 220 221
      CORBA_free(window->ni);
      window->ni = newni;

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

223 224 225 226 227 228 229
      nautilus_window_load_content_view_menu (window, loci);

      /* 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 */
      nautilus_index_panel_set_uri(window->index_panel, loci->navinfo.requested_uri);
    }
230

231 232 233
  nautilus_window_allow_back(window, window->uris_prev?TRUE:FALSE);
  nautilus_window_allow_forward(window, window->uris_next?TRUE:FALSE);
  
234
  explorer_location_bar_set_uri_string(EXPLORER_LOCATION_BAR(window->ent_uri),
235
				       window->ni->requested_uri);
236 237

  nautilus_window_refresh_title (window);
238 239 240 241 242
}

static void
nautilus_window_update_view(NautilusWindow *window,
                            NautilusView *view,
243
                            Nautilus_NavigationInfo *loci,
244
                            Nautilus_SelectionInfo *seli,
245 246
                            NautilusView *requesting_view,
			    NautilusView *content_view)
247
{
248 249
  g_return_if_fail(view);

250
  loci->self_originated = (view == requesting_view);
251

252
  nautilus_view_notify_location_change(NAUTILUS_VIEW(view), loci);
253

254
  if(seli)
255
    {
256 257
      seli->content_view = nautilus_view_get_client_objref(content_view);
      nautilus_window_notify_selection_change(window, view, seli, NULL);
258 259 260
    }
}

261 262 263
void
nautilus_window_view_destroyed(NautilusView *view, NautilusWindow *window)
{
264 265 266
  NautilusWindowStateItem item = VIEW_ERROR;
  nautilus_window_set_state_info(window, item, view, (NautilusWindowStateItem)0);
}
267

268 269 270 271 272 273 274 275 276 277 278
static void
nautilus_window_refresh_title (NautilusWindow *window)
{
  GnomeVFSURI *vfs_uri;
	
  g_return_if_fail (NAUTILUS_IS_WINDOW (window));
	
  vfs_uri = gnome_vfs_uri_new (nautilus_window_get_requested_uri (window));
  if (vfs_uri == NULL)
    {
      gtk_window_set_title (GTK_WINDOW (window), _("Nautilus"));
279
    }
280
  else
281
    {
282 283 284 285 286 287 288
      gchar *short_name;
      gchar *new_title;

      short_name = gnome_vfs_uri_extract_short_name (vfs_uri);
      gnome_vfs_uri_unref (vfs_uri);

      g_assert (short_name != NULL);
289

290 291 292 293 294
      new_title = g_strdup_printf (_("Nautilus: %s"), short_name);
      gtk_window_set_title (GTK_WINDOW (window), new_title);
      g_free (new_title);
		
      g_free (short_name);
295
    }
296

297 298
}

299 300 301
/* 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)
302
{
303 304
  GSList *cur, *discard_views;
  GSList *new_meta_views;
305

306 307
  new_meta_views = window->new_meta_views;
  window->new_meta_views = NULL;
Darin Adler's avatar
Darin Adler committed
308

309
  if(window->new_content_view)
310
    {
311
      if(!GTK_WIDGET(window->new_content_view)->parent)
312 313 314 315 316
        {
          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);
        }
317 318 319 320 321 322 323 324
      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)
325
        {
326 327 328
          if(!GTK_WIDGET(cur->data)->parent)
            nautilus_window_add_meta_view(window, cur->data);
          gtk_object_unref(cur->data);
329
        }
330

331 332 333 334 335 336 337 338
      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);
339 340
    }

341
  if(window->pending_ni)
342
    {
343 344
      nautilus_window_update_internals(window, window->pending_ni);
      nautilus_navinfo_free(window->pending_ni); window->pending_ni = NULL;
345
    }
346
}
347

348 349 350 351
/* 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)
{
352 353
  g_message("-> FREE_LOAD_INFO <-");

354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371
  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 = 0;
  window->action_tag = 0;
  window->made_changes = 0;
  window->state = NW_IDLE;
  window->changes_pending =
    window->is_back =
    window->views_shown =
    window->view_bombed_out =
    window->view_activation_complete =
    window->cv_progress_initial =
    window->cv_progress_done =
    window->cv_progress_error = 
372
    window->sent_update_view =
373
    window->reset_to_idle = FALSE;
374 375
}

376 377
/* Meta view handling */
static NautilusView *
378 379 380 381
nautilus_window_load_meta_view(NautilusWindow *window,
                               const char *iid,
                               NautilusView *requesting_view)
{
382
  NautilusView *meta_view = NULL;
383 384 385 386 387 388 389 390 391 392 393
  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)
    {
394
      meta_view = NAUTILUS_VIEW(gtk_widget_new(nautilus_meta_view_get_type(), "main_window", window, NULL));
395
      nautilus_window_connect_view(window, meta_view);
396 397
      if(!nautilus_view_load_client(meta_view, iid))
	{
398
	  gtk_widget_unref(GTK_WIDGET(meta_view));
399 400
	  meta_view = NULL;
	}
401 402
    }

403
  if(meta_view)
404 405 406 407
    {
      gtk_object_ref(GTK_OBJECT(meta_view));

      nautilus_view_set_active_errors(meta_view, TRUE);
408
    }
409

410 411
  return meta_view;
}
412

413 414 415 416 417 418
void
nautilus_window_request_location_change(NautilusWindow *window,
					Nautilus_NavigationRequestInfo *loc,
					NautilusView *requesting_view)
{  
  nautilus_window_change_location(window, loc, requesting_view, FALSE);
419 420
}

421 422 423 424 425
static NautilusView *
nautilus_window_load_content_view(NautilusWindow *window,
                                  const char *iid,
                                  Nautilus_NavigationInfo *navinfo,
                                  NautilusView **requesting_view)
426
{
427 428
  NautilusView *content_view = window->content_view;
  NautilusView *new_view;
429

430 431
  g_return_val_if_fail(iid, NULL);
  g_return_val_if_fail(navinfo, NULL);
432

433 434 435 436 437 438 439 440 441
  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;
        }
442

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

445
      nautilus_window_connect_view(window, new_view);
446

447 448 449 450 451 452 453 454 455 456
      if(!nautilus_view_load_client(new_view, iid))
	{
	  gtk_widget_unref(GTK_WIDGET(new_view));
	  new_view = NULL;
	}
    }
  else
    new_view = window->content_view;

  if(new_view && NAUTILUS_IS_VIEW(new_view))
457
    {
458 459 460 461 462
      gtk_object_ref(GTK_OBJECT(new_view));

      navinfo->content_view = nautilus_view_get_client_objref(new_view);

      nautilus_view_set_active_errors(new_view, TRUE);
463
    }
464 465 466 467
  else
    new_view = NULL;

  return new_view;
468
}
469

470 471
static gboolean
nautilus_window_update_state(gpointer data)
472
{
473
  NautilusWindow *window = data;
474
  GSList *cur;
475
  gboolean retval = FALSE;
476

477
  if(window->making_changes)
478
    {
479
      return FALSE;
480
    }
481 482 483 484

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

485 486 487 488 489 490 491 492
  g_message(">>> nautilus_window_update_state:");
  g_print("made_changes %d, making_changes %d\n", window->made_changes, window->making_changes);
  g_print("changes_pending %d, is_back %d, views_shown %d, view_bombed_out %d, view_activation_complete %d\n",
          window->changes_pending, window->is_back, 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);
493 494 495

  /* Now make any needed state changes based on available information */
  if(window->view_bombed_out && window->error_views)
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
      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;
538
    }
539 540

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

544
      g_message("Reset to idle!");
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
      window->changes_pending = FALSE;
      window->made_changes++;
      window->reset_to_idle = FALSE;

      if(window->cancel_tag)
        g_source_remove(window->cancel_tag);

      if(window->pending_ni)
        {
          /* 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);
576
  
Elliot Lee's avatar
Elliot Lee committed
577 578 579
      nautilus_window_free_load_info(window);

      nautilus_window_allow_stop(window, FALSE);
580 581 582
    }

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

586
      g_message("Changes pending");
587

Elliot Lee's avatar
Elliot Lee committed
588 589
      if(window->pending_ni && !window->new_content_view && !window->cv_progress_error
         && !window->view_activation_complete)
590 591 592 593
        {
          window->new_content_view = nautilus_window_load_content_view(window, window->pending_ni->default_content_iid,
                                                                       &window->pending_ni->navinfo,
                                                                       &window->new_requesting_view);
594

595 596 597
          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);
598

599 600 601
              if(meta_view)
                window->new_meta_views = g_slist_prepend(window->new_meta_views, meta_view);
            }
602

603
          window->view_activation_complete = TRUE;
604

605 606
          window->made_changes++;
        }
607

Elliot Lee's avatar
Elliot Lee committed
608
      if(window->view_activation_complete
609
         && !window->sent_update_view)
610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
        {
          Nautilus_NavigationInfo *ni;
          Nautilus_SelectionInfo *si;

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

625 626
          g_message("!!! Sending update_view");

627 628 629 630 631 632 633 634 635
          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);
Elliot Lee's avatar
Elliot Lee committed
636

637 638
          window->sent_update_view = TRUE;
          window->made_changes++;
639
        }
640

641 642 643 644 645 646 647 648 649 650 651 652 653 654 655
      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;
656
          g_message("cv_progress_(error|done) kicking in");
657
        }
658 659
    }

660
  if(window->made_changes)
661
    {
662 663
      if(!window->action_tag)
        window->action_tag = g_idle_add_full(G_PRIORITY_LOW, nautilus_window_update_state, window, NULL);
664

665 666
      retval = TRUE;
      window->made_changes = 0;
667
    }
668 669
  else
    window->action_tag = 0;
670

671
  g_message("update_state done <<<");
672

673
  window->making_changes--;
674

675 676
  return retval;
}
677

678 679 680 681 682 683
void
nautilus_window_set_state_info(NautilusWindow *window, ...)
{
  va_list args;
  NautilusWindowStateItem item_type;
  NautilusView *new_view;
684

685
  if(window->made_changes) /* Ensure that changes happen in-order */
686
    {
687 688 689 690 691
      if(window->action_tag)
        {
          g_source_remove(window->action_tag);
          window->action_tag = 0;
        }
692

693 694
      nautilus_window_update_state(window);
    }
695

696 697 698 699 700 701 702 703 704 705 706 707 708 709 710
  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 */
          g_message("NAVINFO_RECEIVED"); 
          window->pending_ni = va_arg(args, NautilusNavigationInfo*);
          window->cancel_tag = 0;
          window->changes_pending = TRUE;
          break;
        case VIEW_ERROR:
          window->view_bombed_out = TRUE;
          new_view = va_arg(args, NautilusView*);
711
          g_message("VIEW_ERROR on %p", new_view);
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
          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:
          g_message("NEW_CONTENT_VIEW_ACTIVATED");
          g_return_if_fail(!window->new_content_view);
          new_view = va_arg(args, NautilusView*);
          gtk_object_ref(GTK_OBJECT(new_view));
          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:
          g_message("NEW_META_VIEW_ACTIVATED");
          new_view = va_arg(args, NautilusView*);
          gtk_object_ref(GTK_OBJECT(new_view));
          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 */
          g_message("CV_PROGRESS_INITIAL");
          window->cv_progress_initial = TRUE;
          window->changes_pending = TRUE;
          break;
        case CV_PROGRESS_ERROR: /* We have received a load error from the content view */
          window->cv_progress_error = TRUE;
          g_message("CV_PROGRESS_ERROR");
          break;
        case CV_PROGRESS_DONE: /* The content view is done loading */
          window->cv_progress_done = TRUE;
          g_message("CV_PROGRESS_DONE");
          break;
        case RESET_TO_IDLE: /* Someone pressed the stop button or something */
          window->reset_to_idle = TRUE;
          g_message("RESET_TO_IDLE");
          break;
        }
752 753
    }

754
  va_end(args);
755

756 757 758 759 760 761 762 763 764
  if(!window->action_tag)
    window->action_tag = g_idle_add_full(G_PRIORITY_LOW, nautilus_window_update_state, window, NULL);
  window->made_changes++;
}

static void
nautilus_window_change_location_2(NautilusNavigationInfo *navi, gpointer data)
{
  NautilusWindow *window = data;
Elliot Lee's avatar
Elliot Lee committed
765 766 767 768 769 770 771 772 773 774 775 776 777 778
  char *errmsg;

  /* Do various error checking here */

  window->cancel_tag = 0;

  if(!navi)
    {
      errmsg = _("The chosen page could not be retrieved.");
      goto errout;
    }

  if(!navi->default_content_iid)
    {
779
      errmsg = _("There is no known method of displaying the chosen page.");
Elliot Lee's avatar
Elliot Lee committed
780 781
      goto errout;
    }
782

783
  nautilus_window_set_state_info(window, (NautilusWindowStateItem)NAVINFO_RECEIVED, navi, (NautilusWindowStateItem)0);
Elliot Lee's avatar
Elliot Lee committed
784 785 786 787 788 789 790
  return;

 errout:
  nautilus_window_allow_stop(window, FALSE);
  nautilus_navinfo_free(navi);
  window->is_back = FALSE;
  nautilus_window_progress_indicate(window, PROGRESS_ERROR, 0, errmsg);
791 792
}

793 794 795 796 797 798
void
nautilus_window_change_location(NautilusWindow *window,
				Nautilus_NavigationRequestInfo *loc,
				NautilusView *requesting_view,
				gboolean is_back)
{
799
  nautilus_window_set_state_info(window, (NautilusWindowStateItem)RESET_TO_IDLE, (NautilusWindowStateItem)0);
800

801
  while (gdk_events_pending())
802
    gtk_main_iteration();
803

804
  nautilus_window_allow_stop(window, TRUE);
805

806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825
  nautilus_window_progress_indicate(window, PROGRESS_INITIAL, 0, _("Gathering information"));
  window->is_back = is_back;
  window->new_requesting_view = requesting_view;

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


/******** content view switching **********/
static void
view_menu_switch_views_cb (GtkWidget *widget, gpointer data)
{
  NautilusWindow *window;
  NautilusView *view;
  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);
826

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

830
  iid = (char *)data;
Elliot Lee's avatar
Elliot Lee committed
831 832
  nautilus_window_allow_stop(window, TRUE);

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
  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->default_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);
898
}