fm-directory-view.c 26.7 KB
Newer Older
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2
/* fm-directory-view.c
Ettore Perazzoli's avatar
Ettore Perazzoli committed
3
 *
Elliot Lee's avatar
Elliot Lee committed
4
 * Copyright (C) 1999, 2000  Free Software Foundaton
5
 * Copyright (C) 2000  Eazel, Inc.
Ettore Perazzoli's avatar
Ettore Perazzoli committed
6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 * Author: Ettore Perazzoli
 */

#ifdef HAVE_CONFIG_H
#include "config.h"
#endif

29
#include "fm-directory-view.h"
Ettore Perazzoli's avatar
Ettore Perazzoli committed
30

31 32
#include <gtk/gtksignal.h>
#include <gtk/gtkmain.h>
33 34
#include <gtk/gtkmenu.h>
#include <gtk/gtkmenuitem.h>
35 36 37
#include <libgnome/gnome-i18n.h>
#include <libgnomevfs/gnome-vfs-async-ops.h>
#include <libgnomevfs/gnome-vfs-directory-list.h>
38
#include <libgnomevfs/gnome-vfs-file-info.h>
39
#include <libgnomevfs/gnome-vfs-uri.h>
40
#include <libgnomevfs/gnome-vfs-utils.h>
41
#include <libnautilus/nautilus-gtk-macros.h>
42

43 44 45 46 47
#ifndef g_alloca
#include <alloca.h>
#define g_alloca alloca
#endif

Ettore Perazzoli's avatar
Ettore Perazzoli committed
48
#define DISPLAY_TIMEOUT_INTERVAL 500
Elliot Lee's avatar
Elliot Lee committed
49
#define ENTRIES_PER_CB 1
50

51 52
enum 
{
53 54
	ADD_ENTRY,
	BEGIN_ADDING_ENTRIES,
55
	CLEAR,
56 57
	DONE_ADDING_ENTRIES,
	BEGIN_LOADING,
58 59 60 61
	LAST_SIGNAL
};

static guint fm_directory_view_signals[LAST_SIGNAL] = { 0 };
Ettore Perazzoli's avatar
Ettore Perazzoli committed
62

63
struct _FMDirectoryViewDetails
Ettore Perazzoli's avatar
Ettore Perazzoli committed
64
{
65
	NautilusContentViewFrame *view_frame;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
66

67 68
	GnomeVFSDirectoryList *directory_list;
	GnomeVFSDirectoryListPosition current_position;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
69

70 71
	guint display_selection_idle_id;
	guint display_timeout_id;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
72

73 74
	GnomeVFSAsyncHandle *vfs_async_handle;
	GnomeVFSURI *uri;
75 76

	NautilusDirectory *model;
77 78

	GtkMenu *background_context_menu;
79
};
Ettore Perazzoli's avatar
Ettore Perazzoli committed
80

81 82 83
/* forward declarations */
static gint display_selection_info_idle_cb 	(gpointer data);
static void display_selection_info 		(FMDirectoryView *view);
84 85
static void fm_directory_view_initialize_class	(FMDirectoryViewClass *klass);
static void fm_directory_view_initialize 	(FMDirectoryView *view);
86
static void fm_directory_view_destroy 		(GtkObject *object);
87 88 89
static GtkMenu *create_item_context_menu        (FMDirectoryView *view,
						 NautilusFile *file);
static GtkMenu *create_background_context_menu  (FMDirectoryView *view);
90 91 92 93 94
static void stop_location_change_cb 		(NautilusViewFrame *view_frame, 
						 FMDirectoryView *directory_view);
static void notify_location_change_cb 		(NautilusViewFrame *view_frame, 
						 Nautilus_NavigationInfo *nav_context, 
						 FMDirectoryView *directory_view);
95
static void fm_directory_view_repopulate        (FMDirectoryView *view);
96

97
NAUTILUS_DEFINE_CLASS_BOILERPLATE (FMDirectoryView, fm_directory_view, GTK_TYPE_SCROLLED_WINDOW)
98 99 100
NAUTILUS_IMPLEMENT_MUST_OVERRIDE_SIGNAL (fm_directory_view, add_entry)
NAUTILUS_IMPLEMENT_MUST_OVERRIDE_SIGNAL (fm_directory_view, clear)
NAUTILUS_IMPLEMENT_MUST_OVERRIDE_SIGNAL (fm_directory_view, get_selection)
101

Ettore Perazzoli's avatar
Ettore Perazzoli committed
102
static void
103
fm_directory_view_initialize_class (FMDirectoryViewClass *klass)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
104 105 106
{
	GtkObjectClass *object_class;

107
	object_class = GTK_OBJECT_CLASS (klass);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
108

109
	object_class->destroy = fm_directory_view_destroy;
110 111 112 113 114 115 116 117

	fm_directory_view_signals[CLEAR] =
		gtk_signal_new ("clear",
       				GTK_RUN_LAST,
                    		object_class->type,
                    		GTK_SIGNAL_OFFSET (FMDirectoryViewClass, clear),
		    		gtk_marshal_NONE__NONE,
		    		GTK_TYPE_NONE, 0);
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
	fm_directory_view_signals[BEGIN_ADDING_ENTRIES] =
		gtk_signal_new ("begin_adding_entries",
       				GTK_RUN_LAST,
                    		object_class->type,
                    		GTK_SIGNAL_OFFSET (FMDirectoryViewClass, begin_adding_entries),
		    		gtk_marshal_NONE__NONE,
		    		GTK_TYPE_NONE, 0);
	fm_directory_view_signals[ADD_ENTRY] =
		gtk_signal_new ("add_entry",
       				GTK_RUN_LAST,
                    		object_class->type,
                    		GTK_SIGNAL_OFFSET (FMDirectoryViewClass, add_entry),
		    		gtk_marshal_NONE__POINTER,
		    		GTK_TYPE_NONE, 1, GTK_TYPE_POINTER);
	fm_directory_view_signals[DONE_ADDING_ENTRIES] =
		gtk_signal_new ("done_adding_entries",
       				GTK_RUN_LAST,
                    		object_class->type,
                    		GTK_SIGNAL_OFFSET (FMDirectoryViewClass, done_adding_entries),
		    		gtk_marshal_NONE__NONE,
		    		GTK_TYPE_NONE, 0);
	fm_directory_view_signals[BEGIN_LOADING] =
		gtk_signal_new ("begin_loading",
       				GTK_RUN_LAST,
                    		object_class->type,
                    		GTK_SIGNAL_OFFSET (FMDirectoryViewClass, begin_loading),
		    		gtk_marshal_NONE__NONE,
		    		GTK_TYPE_NONE, 0);
146

147 148 149
	NAUTILUS_ASSIGN_MUST_OVERRIDE_SIGNAL (klass, fm_directory_view, add_entry);
	NAUTILUS_ASSIGN_MUST_OVERRIDE_SIGNAL (klass, fm_directory_view, clear);
	NAUTILUS_ASSIGN_MUST_OVERRIDE_SIGNAL (klass, fm_directory_view, get_selection);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
150 151 152
}

static void
153
fm_directory_view_initialize (FMDirectoryView *directory_view)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
154
{
155
	directory_view->details = g_new0 (FMDirectoryViewDetails, 1);
156

157
#if 0
158
	gtk_scroll_frame_set_policy (GTK_SCROLL_FRAME (directory_view),
Ettore Perazzoli's avatar
Ettore Perazzoli committed
159 160
				     GTK_POLICY_AUTOMATIC,
				     GTK_POLICY_AUTOMATIC);
161
	gtk_scroll_frame_set_shadow_type (GTK_SCROLL_FRAME (directory_view), GTK_SHADOW_IN);
162
#else
163
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (directory_view),
164 165
					GTK_POLICY_AUTOMATIC,
					GTK_POLICY_AUTOMATIC);
166 167
	gtk_scrolled_window_set_hadjustment (GTK_SCROLLED_WINDOW (directory_view), NULL);
	gtk_scrolled_window_set_vadjustment (GTK_SCROLLED_WINDOW (directory_view), NULL);
168 169

#endif
170

171 172
	directory_view->details->background_context_menu = create_background_context_menu(directory_view);

173 174
	directory_view->details->view_frame = NAUTILUS_CONTENT_VIEW_FRAME
		(gtk_widget_new (nautilus_content_view_frame_get_type(), NULL));
175

176
	gtk_signal_connect (GTK_OBJECT (directory_view->details->view_frame), 
177 178 179 180
			    "stop_location_change",
			    GTK_SIGNAL_FUNC (stop_location_change_cb),
			    directory_view);

181
	gtk_signal_connect (GTK_OBJECT (directory_view->details->view_frame), 
182 183 184 185
			    "notify_location_change",
			    GTK_SIGNAL_FUNC (notify_location_change_cb), 
			    directory_view);

186

187
	gtk_widget_show (GTK_WIDGET (directory_view));
188

189 190
	gtk_container_add (GTK_CONTAINER (directory_view->details->view_frame),
			   GTK_WIDGET (directory_view));
Ettore Perazzoli's avatar
Ettore Perazzoli committed
191 192
}

193 194 195 196 197 198 199 200 201 202 203 204 205
static void
fm_directory_view_destroy (GtkObject *object)
{
	FMDirectoryView *view;

	view = FM_DIRECTORY_VIEW (object);

	if (view->details->directory_list != NULL)
		gnome_vfs_directory_list_destroy (view->details->directory_list);

	if (view->details->uri != NULL)
		gnome_vfs_uri_unref (view->details->uri);

206
	if (view->details->vfs_async_handle != NULL)
207 208 209 210 211
		gnome_vfs_async_cancel (view->details->vfs_async_handle);

	if (view->details->display_timeout_id != 0)
		gtk_timeout_remove (view->details->display_timeout_id);

212 213 214
	if (view->details->model != NULL)
		gtk_object_unref (GTK_OBJECT (view->details->model));

215 216 217
	if (view->details->background_context_menu != NULL)
		gtk_object_unref(GTK_OBJECT(view->details->background_context_menu));

218 219 220 221 222 223 224 225
	NAUTILUS_CALL_PARENT_CLASS (GTK_OBJECT_CLASS, destroy, (object));
}



/**
 * display_selection_info:
 *
226
 * Display information about the current selection, and notify the view frame of the changed selection.
227 228 229 230 231 232
 * @view: FMDirectoryView for which to display selection info.
 * 
 **/
static void
display_selection_info (FMDirectoryView *view)
{
233
	NautilusFileList *selection;
234 235
	GnomeVFSFileSize size;
	guint count;
236 237
	NautilusFileList *p;
	char *first_item_name;
238 239 240 241 242 243 244 245
	Nautilus_StatusRequestInfo sri;

	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));

	selection = fm_directory_view_get_selection (view);
	
	count = 0;
	size = 0;
246
	first_item_name = NULL;
247
	for (p = selection; p != NULL; p = p->next) {
248
		NautilusFile *file;
249

250
		file = p->data;
251
		count++;
252
		size += nautilus_file_get_size (file);
253
		if (first_item_name == NULL)
254
			first_item_name = nautilus_file_get_name (file);
255
	}
256
		
257
	g_list_free (selection);
258
	
259 260 261 262
	memset(&sri, 0, sizeof(sri));

	if (count == 0) 
	{
263 264
	        sri.status_string = "";
	}
265
	else
266
	{
267
		char *size_string;
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285

		size_string = gnome_vfs_file_size_to_string (size);
		if (count == 1)
		{
			g_assert (first_item_name != NULL && strlen (first_item_name) > 0);
			
			sri.status_string = g_strdup_printf (_("\"%s\" selected -- %s"), 
							     first_item_name, 
							     size_string);
		}
		else
		{
			sri.status_string = g_strdup_printf (_("%d items selected -- %s"), 
							     count, 
							     size_string);
		}
		g_free (size_string);
	}
286

287 288
	g_free (first_item_name);

289 290
	nautilus_view_frame_request_status_change
		(NAUTILUS_VIEW_FRAME (view->details->view_frame), &sri);
291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
}

static void
fm_directory_view_send_selection_change (FMDirectoryView *view)
{
	Nautilus_SelectionRequestInfo request;
	NautilusFileList *selection;
	NautilusFileList *p;
	int i;

	memset (&request, 0, sizeof (request));

	/* Collect a list of URIs. */
	selection = fm_directory_view_get_selection (view);
	request.selected_uris._buffer = g_alloca (g_list_length (selection) * sizeof (char *));
	for (p = selection; p != NULL; p = p->next)
		request.selected_uris._buffer[request.selected_uris._length++]
			= nautilus_file_get_uri (p->data);
	g_list_free (selection);

	/* Send the selection change. */
312
	nautilus_view_frame_request_selection_change
313 314 315 316 317
		(NAUTILUS_VIEW_FRAME (view->details->view_frame), &request);

	/* Free the URIs. */
	for (i = 0; i < request.selected_uris._length; i++)
		g_free (request.selected_uris._buffer[i]);
318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333
}



static void
notify_location_change_cb (NautilusViewFrame *view_frame, Nautilus_NavigationInfo *nav_context, FMDirectoryView *directory_view)
{
	fm_directory_view_load_uri(directory_view, nav_context->requested_uri);
}

static void
stop_location_change_cb (NautilusViewFrame *view_frame, FMDirectoryView *directory_view)
{
	fm_directory_view_stop(directory_view);
}

Ettore Perazzoli's avatar
Ettore Perazzoli committed
334 335 336


static void
337
stop_load (FMDirectoryView *view, gboolean error)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
338
{
339 340
	Nautilus_ProgressRequestInfo pri;

341 342 343
	if (view->details->vfs_async_handle != NULL) {
		gnome_vfs_async_cancel (view->details->vfs_async_handle);
		view->details->vfs_async_handle = NULL;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
344 345
	}

346 347 348
	if (view->details->display_timeout_id != 0) {
		gtk_timeout_remove (view->details->display_timeout_id);
		view->details->display_timeout_id = 0;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
349 350
	}

351
	memset(&pri, 0, sizeof(pri));
352 353 354
	pri.amount = 100.0;
	pri.type = error ? Nautilus_PROGRESS_DONE_ERROR : Nautilus_PROGRESS_DONE_OK;
	
355 356
	nautilus_view_frame_request_progress_change
		(NAUTILUS_VIEW_FRAME (view->details->view_frame), &pri);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
357 358 359
}


360 361 362


/**
363
 * fm_directory_view_repopulate:
364
 *
365 366
 * Fill view with already-discovered entries for current location, after emptying 
 * any old contents. This is normally called only by FMDirectoryView.
367 368 369
 * @view: FMDirectoryView to fill.
 * 
 **/
370 371
static void
fm_directory_view_repopulate (FMDirectoryView *view)
372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392
{
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));

	/* Always start from scratch */
	fm_directory_view_clear (view);

	if (view->details->directory_list != NULL) 
	{
		GnomeVFSDirectoryListPosition *position;

		position = gnome_vfs_directory_list_get_first_position
			(view->details->directory_list);

		fm_directory_view_begin_adding_entries (view);

		while (position != view->details->current_position) {
			GnomeVFSFileInfo *info;

			info = gnome_vfs_directory_list_get
				(view->details->directory_list, position);

393 394
			fm_directory_view_add_entry
				(view, nautilus_directory_new_file (view->details->model, info));
395 396 397 398 399 400 401 402 403

			position = gnome_vfs_directory_list_position_next
				(position);
		}

		fm_directory_view_done_adding_entries (view);
	}
}

Ettore Perazzoli's avatar
Ettore Perazzoli committed
404
static void
405
display_pending_entries (FMDirectoryView *view)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
406
{
407
	fm_directory_view_begin_adding_entries (view);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
408

409
	while (view->details->current_position != GNOME_VFS_DIRECTORY_LIST_POSITION_NONE)
410
	{
Ettore Perazzoli's avatar
Ettore Perazzoli committed
411 412
		GnomeVFSFileInfo *info;

413 414
		info = gnome_vfs_directory_list_get (view->details->directory_list,
						     view->details->current_position);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
415

416
		fm_directory_view_add_entry (view, nautilus_directory_new_file (view->details->model, info));
Ettore Perazzoli's avatar
Ettore Perazzoli committed
417

418 419
		view->details->current_position = gnome_vfs_directory_list_position_next
						       (view->details->current_position);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
420 421
	}

422
	fm_directory_view_done_adding_entries (view);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
423 424
}

425 426
static gint
display_selection_info_idle_cb (gpointer data)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
427
{
428
	FMDirectoryView *view;
429
	
430
	g_return_val_if_fail (FM_IS_DIRECTORY_VIEW (data), FALSE);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
431

432
	view = FM_DIRECTORY_VIEW (data);
433

434
	display_selection_info (view);
435 436
	fm_directory_view_send_selection_change (view);

437
	view->details->display_selection_idle_id = 0;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
438

439 440
	return FALSE;
}
Ettore Perazzoli's avatar
Ettore Perazzoli committed
441

442 443 444 445 446 447
static gboolean
display_timeout_cb (gpointer data)
{
	g_return_val_if_fail (FM_IS_DIRECTORY_VIEW (data), TRUE);
	
	display_pending_entries (FM_DIRECTORY_VIEW (data));
Ettore Perazzoli's avatar
Ettore Perazzoli committed
448 449 450 451 452 453 454 455 456 457 458 459

	return TRUE;
}


static void
directory_load_cb (GnomeVFSAsyncHandle *handle,
		   GnomeVFSResult result,
		   GnomeVFSDirectoryList *list,
		   guint entries_read,
		   gpointer callback_data)
{
460
	FMDirectoryView *view;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
461

Elliot Lee's avatar
Elliot Lee committed
462 463
	g_assert(entries_read <= ENTRIES_PER_CB);

464
	view = FM_DIRECTORY_VIEW (callback_data);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
465

466
	if (view->details->directory_list == NULL) {
Ettore Perazzoli's avatar
Ettore Perazzoli committed
467
		if (result == GNOME_VFS_OK || result == GNOME_VFS_ERROR_EOF) {
468

469 470
			fm_directory_view_begin_loading (view);

471
			view->details->directory_list = list;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
472

473 474
			g_assert (view->details->current_position
				== GNOME_VFS_DIRECTORY_LIST_POSITION_NONE);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
475 476

			if (result != GNOME_VFS_ERROR_EOF)
477
				view->details->display_timeout_id
Ettore Perazzoli's avatar
Ettore Perazzoli committed
478 479 480 481 482
					= gtk_timeout_add
						(DISPLAY_TIMEOUT_INTERVAL,
						 display_timeout_cb,
						 view);
		} else if (entries_read == 0) {
Elliot Lee's avatar
Elliot Lee committed
483
			/*
Ettore Perazzoli's avatar
Ettore Perazzoli committed
484 485
			gtk_signal_emit (GTK_OBJECT (view),
					 signals[OPEN_FAILED]);
Elliot Lee's avatar
Elliot Lee committed
486
			*/
Ettore Perazzoli's avatar
Ettore Perazzoli committed
487 488 489
		}
	}

490 491
	if(view->details->current_position == GNOME_VFS_DIRECTORY_LIST_POSITION_NONE && list)
		view->details->current_position
492
			= gnome_vfs_directory_list_get_position (list);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
493 494

	if (result == GNOME_VFS_ERROR_EOF) {
495
		display_pending_entries (view);
496
		stop_load (view, FALSE);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
497
	} else if (result != GNOME_VFS_OK) {
498
		stop_load (view, TRUE);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
499 500 501
	}
}

502

503 504 505 506 507 508 509 510 511
/**
 * fm_directory_view_clear:
 *
 * Emit the signal to clear the contents of the view. Subclasses must
 * override the signal handler for this signal. This is normally called
 * only by FMDirectoryView.
 * @view: FMDirectoryView to empty.
 * 
 **/
512 513
void
fm_directory_view_clear (FMDirectoryView *view)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
514
{
515
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));
Ettore Perazzoli's avatar
Ettore Perazzoli committed
516

517
	gtk_signal_emit (GTK_OBJECT (view), fm_directory_view_signals[CLEAR]);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
518 519
}

520 521 522 523 524 525 526 527 528
/**
 * fm_directory_view_begin_adding_entries:
 *
 * Emit the signal to prepare for adding a set of entries to the view. 
 * Subclasses might want to override the signal handler for this signal. 
 * This is normally called only by FMDirectoryView.
 * @view: FMDirectoryView that will soon have new entries added.
 * 
 **/
Ettore Perazzoli's avatar
Ettore Perazzoli committed
529
void
530
fm_directory_view_begin_adding_entries (FMDirectoryView *view)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
531
{
532
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));
Ettore Perazzoli's avatar
Ettore Perazzoli committed
533

534 535 536
	gtk_signal_emit (GTK_OBJECT (view), fm_directory_view_signals[BEGIN_ADDING_ENTRIES]);
}

537 538 539 540 541 542 543
/**
 * fm_directory_view_add_entry:
 *
 * Emit the signal to add one entry to the view. Subclasses must
 * override the signal handler for this signal. This is normally called
 * only by FMDirectoryView.
 * @view: FMDirectoryView to add entry to.
544
 * @file: NautilusFile describing entry to add.
545 546
 * 
 **/
547
void
548
fm_directory_view_add_entry (FMDirectoryView *view, NautilusFile *file)
549 550
{
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));
Ettore Perazzoli's avatar
Ettore Perazzoli committed
551

552
	gtk_signal_emit (GTK_OBJECT (view), fm_directory_view_signals[ADD_ENTRY], file);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
553 554
}

555 556 557 558 559 560 561 562 563
/**
 * fm_directory_view_done_adding_entries:
 *
 * Emit the signal to clean up after adding a set of entries to the view. 
 * Subclasses might want to override the signal handler for this signal. 
 * This is normally called only by FMDirectoryView.
 * @view: FMDirectoryView that has just had new entries added.
 * 
 **/
564
void
565
fm_directory_view_done_adding_entries (FMDirectoryView *view)
566 567 568
{
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));

569 570 571
	gtk_signal_emit (GTK_OBJECT (view), fm_directory_view_signals[DONE_ADDING_ENTRIES]);
}

572 573 574 575 576 577 578 579 580
/**
 * fm_directory_view_begin_loading:
 *
 * Emit the signal to prepare for loading the contents of a new location. 
 * Subclasses might want to override the signal handler for this signal. 
 * This is normally called only by FMDirectoryView.
 * @view: FMDirectoryView that is switching to view a new location.
 * 
 **/
581
void
582
fm_directory_view_begin_loading (FMDirectoryView *view)
583 584 585
{
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));

586
	gtk_signal_emit (GTK_OBJECT (view), fm_directory_view_signals[BEGIN_LOADING]);
587 588
}

589 590 591
/**
 * fm_directory_view_get_selection:
 *
592
 * Get a list of NautilusFile pointers that represents the
593 594 595 596 597
 * currently-selected items in this view. Subclasses must override
 * the signal handler for the 'get_selection' signal. Callers are
 * responsible for g_free-ing the list (but not its data).
 * @view: FMDirectoryView whose selected items are of interest.
 * 
598
 * Return value: GList of NautilusFile pointers representing the selection.
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
 * 
 **/
GList *
fm_directory_view_get_selection (FMDirectoryView *view)
{
	g_return_val_if_fail (FM_IS_DIRECTORY_VIEW (view), NULL);

	return (* FM_DIRECTORY_VIEW_CLASS (GTK_OBJECT (view)->klass)->get_selection) (view);
}

/**
 * fm_directory_view_get_view_frame:
 *
 * Get the NautilusContentViewFrame for this FMDirectoryView.
 * This is normally called only by the embedding framework.
 * @view: FMDirectoryView of interest.
 * 
 * Return value: NautilusContentViewFrame for this view.
 * 
 **/
NautilusContentViewFrame *
fm_directory_view_get_view_frame (FMDirectoryView *view)
{
	g_return_val_if_fail (FM_IS_DIRECTORY_VIEW (view), NULL);

	return view->details->view_frame;
}

/**
628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643
 * fm_directory_view_get_model:
 *
 * Get the model for this FMDirectoryView.
 * @view: FMDirectoryView of interest.
 * 
 * Return value: NautilusDirectory for this view.
 * 
 **/
NautilusDirectory *
fm_directory_view_get_model (FMDirectoryView *view)
{
	g_return_val_if_fail (FM_IS_DIRECTORY_VIEW (view), NULL);

	return view->details->model;
}

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
static void
popup_context_menu (GtkMenu *menu)
{
	gtk_menu_popup (menu, NULL, NULL, NULL,
			NULL, 3, GDK_CURRENT_TIME);
}

static void
popup_temporary_context_menu (GtkMenu *menu)
{
	gtk_object_ref (GTK_OBJECT(menu));
	gtk_object_sink (GTK_OBJECT(menu));

	popup_context_menu (menu);

	gtk_object_unref (GTK_OBJECT(menu));
}

/* FIXME - need better architecture for setting these. Also need to
   merge per-view items. */

static GtkMenu *
create_item_context_menu (FMDirectoryView *view,
			  NautilusFile *file) 
{
	GtkMenu *menu;
	GtkWidget *menu_item;
	
	menu = GTK_MENU (gtk_menu_new ());

	menu_item = gtk_menu_item_new_with_label ("Open");
	gtk_widget_set_sensitive (menu_item, FALSE);
	gtk_widget_show (menu_item);
	gtk_menu_append (menu, menu_item);

	menu_item = gtk_menu_item_new_with_label ("Delete");
	gtk_widget_set_sensitive (menu_item, FALSE);
	gtk_widget_show (menu_item);
	gtk_menu_append (menu, menu_item);

	return menu;
}

/* FIXME - need a way for specific views to add custom commands here. */

static GtkMenu *
create_background_context_menu (FMDirectoryView *view)
{
	GtkMenu *menu;
	GtkWidget *menu_item;

	menu = GTK_MENU (gtk_menu_new ());

	menu_item = gtk_menu_item_new_with_label ("Select all");
	gtk_widget_set_sensitive (menu_item, FALSE);
	gtk_widget_show (menu_item);
	gtk_menu_append (menu, menu_item);

	menu_item = gtk_menu_item_new_with_label ("Zoom in");
	gtk_widget_set_sensitive (menu_item, FALSE);
	gtk_widget_show (menu_item);
	gtk_menu_append (menu, menu_item);

	menu_item = gtk_menu_item_new_with_label ("Zoom out");
	gtk_widget_set_sensitive (menu_item, FALSE);
	gtk_widget_show (menu_item);
	gtk_menu_append (menu, menu_item);

	gtk_object_ref(GTK_OBJECT(menu));
	gtk_object_sink(GTK_OBJECT(menu));
	return menu;
}


/**
 * fm_directory_view_popup_item_context_menu
 *
 * Pop up a context menu appropriate to a specific view item at the last right click location.
 * @view: FMDirectoryView of interest.
 * @file: The model object for which a menu should be popped up.
 * 
 * Return value: NautilusDirectory for this view.
 * 
 **/

void 
fm_directory_view_popup_item_context_menu  (FMDirectoryView *view,
					    NautilusFile *file)
{
	GtkMenu *menu;
	g_assert (FM_IS_DIRECTORY_VIEW (view));
	/* g_assert (NAUTILUS_IS_FILE (file)); */
	
	menu = create_item_context_menu (view, file);

	popup_temporary_context_menu (menu);
}

/**
 * fm_directory_view_popup_background_context_menu
 *
 * Pop up a context menu appropriate to the view globally at the last right click location.
 * @view: FMDirectoryView of interest.
 * 
 * Return value: NautilusDirectory for this view.
 * 
 **/

void 
fm_directory_view_popup_background_context_menu  (FMDirectoryView *view)
{
	g_assert (FM_IS_DIRECTORY_VIEW (view));
	
	popup_context_menu (view->details->background_context_menu);
}


761 762 763 764 765 766 767 768
/**
 * fm_directory_view_notify_selection_changed:
 * 
 * Notify this view that the selection has changed. This is normally
 * called only by subclasses.
 * @view: FMDirectoryView whose selection has changed.
 * 
 **/
769
void
770
fm_directory_view_notify_selection_changed (FMDirectoryView *view)
771 772 773
{
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));

774 775 776 777 778 779 780 781 782 783 784 785 786
	if (view->details->display_selection_idle_id == 0)
		view->details->display_selection_idle_id
			= gtk_idle_add (display_selection_info_idle_cb,
					view);
}

/**
 * fm_directory_view_activate_entry:
 * 
 * Activate an entry in this view. This might involve switching the displayed
 * location for the current window, or launching an application. This is normally
 * called only by subclasses.
 * @view: FMDirectoryView in question.
787
 * @file: A NautilusFile representing the entry in this view to activate.
788 789 790
 * 
 **/
void
791
fm_directory_view_activate_entry (FMDirectoryView *view, NautilusFile *file)
792 793
{
	GnomeVFSURI *new_uri;
794 795
	char *name;
	char *new_uri_text;
796 797 798
	Nautilus_NavigationRequestInfo nri;

	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));
799 800
	g_return_if_fail (file != NULL);

801
	name = nautilus_file_get_name (file);
802 803
	new_uri = gnome_vfs_uri_append_path(view->details->uri, name);
	g_free (name);
804

805 806 807 808
	new_uri_text = gnome_vfs_uri_to_string (new_uri, GNOME_VFS_URI_HIDE_NONE);
	gnome_vfs_uri_unref (new_uri);

	nri.requested_uri = new_uri_text;
809 810 811
	nri.new_window_default = nri.new_window_suggested = Nautilus_V_FALSE;
	nri.new_window_enforced = Nautilus_V_UNKNOWN;
	nautilus_view_frame_request_location_change
812 813 814
		(NAUTILUS_VIEW_FRAME (view->details->view_frame), &nri);

	g_free (new_uri_text);
815 816
}

817 818 819 820 821 822 823 824 825
/**
 * fm_directory_view_load_uri:
 * 
 * Switch the displayed location to a new uri. If the uri is not valid,
 * the location will not be switched; user feedback will be provided instead.
 * @view: FMDirectoryView whose location will be changed.
 * @uri: A string representing the uri to switch to.
 * 
 **/
Ettore Perazzoli's avatar
Ettore Perazzoli committed
826
void
827 828
fm_directory_view_load_uri (FMDirectoryView *view,
			    const char *uri)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
829 830 831 832 833 834 835
{
	static GnomeVFSDirectorySortRule sort_rules[] = {
		GNOME_VFS_DIRECTORY_SORT_DIRECTORYFIRST,
		GNOME_VFS_DIRECTORY_SORT_BYNAME,
		GNOME_VFS_DIRECTORY_SORT_NONE
	};			/* FIXME */
	GnomeVFSResult result;
836
	Nautilus_ProgressRequestInfo pri;
837
	NautilusDirectory *old_model;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
838 839

	g_return_if_fail (view != NULL);
840
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));
Ettore Perazzoli's avatar
Ettore Perazzoli committed
841 842
	g_return_if_fail (uri != NULL);

843
	fm_directory_view_stop (view);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
844

845
	old_model = view->details->model;
846
	view->details->model = nautilus_directory_get (uri);
847

848 849 850
	if (view->details->uri != NULL)
		gnome_vfs_uri_unref (view->details->uri);
	view->details->uri = gnome_vfs_uri_new (uri);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
851

852 853
	if (view->details->directory_list != NULL)
		gnome_vfs_directory_list_destroy (view->details->directory_list);
854
	view->details->directory_list = NULL;
855 856
	view->details->current_position = GNOME_VFS_DIRECTORY_LIST_POSITION_NONE;

857
	fm_directory_view_clear (view);
858

859 860 861
	if (old_model != NULL)
		gtk_object_unref (GTK_OBJECT (old_model));

862 863 864
	memset(&pri, 0, sizeof(pri));
	pri.type = Nautilus_PROGRESS_UNDERWAY;
	pri.amount = 0;
865 866
	nautilus_view_frame_request_progress_change
		(NAUTILUS_VIEW_FRAME (view->details->view_frame), &pri);
867

Ettore Perazzoli's avatar
Ettore Perazzoli committed
868
	result = gnome_vfs_async_load_directory_uri
869 870
		(&view->details->vfs_async_handle, 	/* handle */
		 view->details->uri,			/* uri */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
871 872 873 874 875 876 877 878 879 880
		 (GNOME_VFS_FILE_INFO_GETMIMETYPE	/* options */
		  | GNOME_VFS_FILE_INFO_FASTMIMETYPE
		  | GNOME_VFS_FILE_INFO_FOLLOWLINKS),
		 NULL, 					/* meta_keys */
		 sort_rules, 				/* sort_rules */
		 FALSE, 				/* reverse_order */
		 GNOME_VFS_DIRECTORY_FILTER_NONE, 	/* filter_type */
		 (GNOME_VFS_DIRECTORY_FILTER_NOSELFDIR  /* filter_options */
		  | GNOME_VFS_DIRECTORY_FILTER_NOPARENTDIR),
		 NULL, 					/* filter_pattern */
Elliot Lee's avatar
Elliot Lee committed
881
		 ENTRIES_PER_CB,			 /* items_per_notification */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
882 883 884
		 directory_load_cb,	 		/* callback */
		 view);		 			/* callback_data */

885
	g_return_if_fail(result == GNOME_VFS_OK);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
886 887
}

888 889 890 891 892 893 894
/**
 * fm_directory_view_stop:
 * 
 * Stop the current ongoing process, such as switching to a new uri.
 * @view: FMDirectoryView in question.
 * 
 **/
Ettore Perazzoli's avatar
Ettore Perazzoli committed
895
void
896
fm_directory_view_stop (FMDirectoryView *view)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
897 898
{
	g_return_if_fail (view != NULL);
899
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));
Ettore Perazzoli's avatar
Ettore Perazzoli committed
900

901
	if (view->details->vfs_async_handle == NULL)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
902 903
		return;

904 905
	display_pending_entries (view);
	stop_load (view, FALSE);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
906 907
}

908 909 910 911 912 913
/**
 * fm_directory_view_sort:
 * 
 * Reorder the items in this view.
 * @view: FMDirectoryView whose items will be reordered.
 * @sort_type: FMDirectoryViewSortType specifying what new order to use.
914
 * @reverse_sort: TRUE if items should be sorted in reverse of standard order.
915 916
 * 
 **/
Ettore Perazzoli's avatar
Ettore Perazzoli committed
917
void
918
fm_directory_view_sort (FMDirectoryView *view,
919 920
			FMDirectoryViewSortType sort_type,
			gboolean reverse_sort)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
921 922 923 924 925 926
{
	GnomeVFSDirectorySortRule *rules;

#define ALLOC_RULES(n) alloca ((n) * sizeof (GnomeVFSDirectorySortRule))

	g_return_if_fail (view != NULL);
927
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));
Ettore Perazzoli's avatar
Ettore Perazzoli committed
928

929
	if (view->details->directory_list == NULL)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
930 931 932
		return;

	switch (sort_type) {
933
	case FM_DIRECTORY_VIEW_SORT_BYNAME:
934 935 936 937 938 939 940 941
		rules = ALLOC_RULES (2);
		/* Note: This used to put directories first. I
		 * thought that was counterproductive and removed it,
		 * but I can imagine discussing this further.
		 * John Sullivan <sullivan@eazel.com>
		 */
		rules[0] = GNOME_VFS_DIRECTORY_SORT_BYNAME;
		rules[1] = GNOME_VFS_DIRECTORY_SORT_NONE;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
942
		break;
943
	case FM_DIRECTORY_VIEW_SORT_BYSIZE:
Ettore Perazzoli's avatar
Ettore Perazzoli committed
944 945 946 947 948 949
		rules = ALLOC_RULES (4);
		rules[0] = GNOME_VFS_DIRECTORY_SORT_DIRECTORYFIRST;
		rules[1] = GNOME_VFS_DIRECTORY_SORT_BYSIZE;
		rules[2] = GNOME_VFS_DIRECTORY_SORT_BYNAME;
		rules[3] = GNOME_VFS_DIRECTORY_SORT_NONE;
		break;
950
	case FM_DIRECTORY_VIEW_SORT_BYTYPE:
Ettore Perazzoli's avatar
Ettore Perazzoli committed
951 952 953 954 955 956
		rules = ALLOC_RULES (4);
		rules[0] = GNOME_VFS_DIRECTORY_SORT_DIRECTORYFIRST;
		rules[1] = GNOME_VFS_DIRECTORY_SORT_BYMIMETYPE;
		rules[2] = GNOME_VFS_DIRECTORY_SORT_BYNAME;
		rules[3] = GNOME_VFS_DIRECTORY_SORT_NONE;
		break;
957 958 959 960 961 962
	case FM_DIRECTORY_VIEW_SORT_BYMTIME:
		rules = ALLOC_RULES (3);
		rules[0] = GNOME_VFS_DIRECTORY_SORT_BYMTIME;
		rules[1] = GNOME_VFS_DIRECTORY_SORT_BYNAME;
		rules[2] = GNOME_VFS_DIRECTORY_SORT_NONE;
		break;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
963
	default:
964
		g_warning ("fm_directory_view_sort: Unknown sort mode %d\n",
Ettore Perazzoli's avatar
Ettore Perazzoli committed
965 966 967 968
			   sort_type);
		return;
	}

969
	gnome_vfs_directory_list_sort (view->details->directory_list, reverse_sort, rules);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
970

971
	fm_directory_view_repopulate (view);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
972 973 974

#undef ALLOC_RULES
}