fm-directory-view.c 27.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
static void zoom_in_cb                          (GtkMenuItem *item, FMDirectoryView *directory_view);
static void zoom_out_cb                         (GtkMenuItem *item, FMDirectoryView *directory_view);
98

99
NAUTILUS_DEFINE_CLASS_BOILERPLATE (FMDirectoryView, fm_directory_view, GTK_TYPE_SCROLLED_WINDOW)
100
101
102
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)
103
NAUTILUS_IMPLEMENT_MUST_OVERRIDE_SIGNAL (fm_directory_view, bump_zoom_level)
104

Ettore Perazzoli's avatar
Ettore Perazzoli committed
105
static void
106
fm_directory_view_initialize_class (FMDirectoryViewClass *klass)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
107
108
109
{
	GtkObjectClass *object_class;

110
	object_class = GTK_OBJECT_CLASS (klass);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
111

112
	object_class->destroy = fm_directory_view_destroy;
113
114
115
116
117
118
119
120

	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);
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
	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);
149

150
151
152
	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);
153
	NAUTILUS_ASSIGN_MUST_OVERRIDE_SIGNAL (klass, fm_directory_view, bump_zoom_level);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
154
155
156
}

static void
157
fm_directory_view_initialize (FMDirectoryView *directory_view)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
158
{
159
	directory_view->details = g_new0 (FMDirectoryViewDetails, 1);
160

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

#endif
174

175
176
	directory_view->details->background_context_menu = create_background_context_menu(directory_view);

177
178
	directory_view->details->view_frame = NAUTILUS_CONTENT_VIEW_FRAME
		(gtk_widget_new (nautilus_content_view_frame_get_type(), NULL));
179

180
	gtk_signal_connect (GTK_OBJECT (directory_view->details->view_frame), 
181
182
183
184
			    "stop_location_change",
			    GTK_SIGNAL_FUNC (stop_location_change_cb),
			    directory_view);

185
	gtk_signal_connect (GTK_OBJECT (directory_view->details->view_frame), 
186
187
188
189
			    "notify_location_change",
			    GTK_SIGNAL_FUNC (notify_location_change_cb), 
			    directory_view);

190

191
	gtk_widget_show (GTK_WIDGET (directory_view));
192

193
194
	gtk_container_add (GTK_CONTAINER (directory_view->details->view_frame),
			   GTK_WIDGET (directory_view));
Ettore Perazzoli's avatar
Ettore Perazzoli committed
195
196
}

197
198
199
200
201
202
203
204
205
206
207
208
209
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);

210
	if (view->details->vfs_async_handle != NULL)
211
212
213
214
215
		gnome_vfs_async_cancel (view->details->vfs_async_handle);

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

216
217
218
	if (view->details->model != NULL)
		gtk_object_unref (GTK_OBJECT (view->details->model));

219
220
221
	if (view->details->background_context_menu != NULL)
		gtk_object_unref(GTK_OBJECT(view->details->background_context_menu));

222
223
224
225
226
227
228
229
	NAUTILUS_CALL_PARENT_CLASS (GTK_OBJECT_CLASS, destroy, (object));
}



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

	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));

	selection = fm_directory_view_get_selection (view);
	
	count = 0;
	size = 0;
250
	first_item_name = NULL;
251
	for (p = selection; p != NULL; p = p->next) {
252
		NautilusFile *file;
253

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

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

		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);
	}
290

291
292
	g_free (first_item_name);

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

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. */
316
	nautilus_view_frame_request_selection_change
317
318
319
320
321
		(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]);
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
}



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
338
339
340


static void
341
stop_load (FMDirectoryView *view, gboolean error)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
342
{
343
344
	Nautilus_ProgressRequestInfo pri;

345
346
347
	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
348
349
	}

350
351
352
	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
353
354
	}

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


364

365
366
367
368
369
370
371
372
373
374
375
376
377
/* handle the zoom in/out menu items */

static void
zoom_in_cb(GtkMenuItem *item, FMDirectoryView *directory_view)
{
    fm_directory_view_bump_zoom_level (directory_view, 1);
}

static void
zoom_out_cb(GtkMenuItem *item, FMDirectoryView *directory_view)
{
    fm_directory_view_bump_zoom_level (directory_view, -1);
}
378
379

/**
380
 * fm_directory_view_repopulate:
381
 *
382
383
 * Fill view with already-discovered entries for current location, after emptying 
 * any old contents. This is normally called only by FMDirectoryView.
384
385
386
 * @view: FMDirectoryView to fill.
 * 
 **/
387
388
static void
fm_directory_view_repopulate (FMDirectoryView *view)
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
{
	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);

410
411
			fm_directory_view_add_entry
				(view, nautilus_directory_new_file (view->details->model, info));
412
413
414
415
416
417
418
419
420

			position = gnome_vfs_directory_list_position_next
				(position);
		}

		fm_directory_view_done_adding_entries (view);
	}
}

Ettore Perazzoli's avatar
Ettore Perazzoli committed
421
static void
422
display_pending_entries (FMDirectoryView *view)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
423
{
424
	fm_directory_view_begin_adding_entries (view);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
425

426
	while (view->details->current_position != GNOME_VFS_DIRECTORY_LIST_POSITION_NONE)
427
	{
Ettore Perazzoli's avatar
Ettore Perazzoli committed
428
429
		GnomeVFSFileInfo *info;

430
431
		info = gnome_vfs_directory_list_get (view->details->directory_list,
						     view->details->current_position);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
432

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

435
436
		view->details->current_position = gnome_vfs_directory_list_position_next
						       (view->details->current_position);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
437
438
	}

439
	fm_directory_view_done_adding_entries (view);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
440
441
}

442
443
static gint
display_selection_info_idle_cb (gpointer data)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
444
{
445
	FMDirectoryView *view;
446
	
447
	g_return_val_if_fail (FM_IS_DIRECTORY_VIEW (data), FALSE);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
448

449
	view = FM_DIRECTORY_VIEW (data);
450

451
	display_selection_info (view);
452
453
	fm_directory_view_send_selection_change (view);

454
	view->details->display_selection_idle_id = 0;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
455

456
457
	return FALSE;
}
Ettore Perazzoli's avatar
Ettore Perazzoli committed
458

459
460
461
462
463
464
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
465
466
467
468
469
470
471
472
473
474
475
476

	return TRUE;
}

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

Elliot Lee's avatar
Elliot Lee committed
479
480
	g_assert(entries_read <= ENTRIES_PER_CB);

481
	view = FM_DIRECTORY_VIEW (callback_data);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
482

483
	if (view->details->directory_list == NULL) {
Ettore Perazzoli's avatar
Ettore Perazzoli committed
484
		if (result == GNOME_VFS_OK || result == GNOME_VFS_ERROR_EOF) {
485

486
487
			fm_directory_view_begin_loading (view);

488
			view->details->directory_list = list;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
489

490
491
			g_assert (view->details->current_position
				== GNOME_VFS_DIRECTORY_LIST_POSITION_NONE);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
492
493

			if (result != GNOME_VFS_ERROR_EOF)
494
				view->details->display_timeout_id
Ettore Perazzoli's avatar
Ettore Perazzoli committed
495
496
497
498
499
					= gtk_timeout_add
						(DISPLAY_TIMEOUT_INTERVAL,
						 display_timeout_cb,
						 view);
		} else if (entries_read == 0) {
Elliot Lee's avatar
Elliot Lee committed
500
			/*
Ettore Perazzoli's avatar
Ettore Perazzoli committed
501
502
			gtk_signal_emit (GTK_OBJECT (view),
					 signals[OPEN_FAILED]);
Elliot Lee's avatar
Elliot Lee committed
503
			*/
Ettore Perazzoli's avatar
Ettore Perazzoli committed
504
505
506
		}
	}

507
508
	if(view->details->current_position == GNOME_VFS_DIRECTORY_LIST_POSITION_NONE && list)
		view->details->current_position
509
			= gnome_vfs_directory_list_get_position (list);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
510
511

	if (result == GNOME_VFS_ERROR_EOF) {
512
		display_pending_entries (view);
513
		stop_load (view, FALSE);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
514
	} else if (result != GNOME_VFS_OK) {
515
		stop_load (view, TRUE);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
516
517
518
	}
}

519

520
521
522
523
524
525
526
527
528
/**
 * 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.
 * 
 **/
529
530
void
fm_directory_view_clear (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
	gtk_signal_emit (GTK_OBJECT (view), fm_directory_view_signals[CLEAR]);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
535
536
}

537
538
539
540
541
542
543
544
545
/**
 * 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
546
void
547
fm_directory_view_begin_adding_entries (FMDirectoryView *view)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
548
{
549
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));
Ettore Perazzoli's avatar
Ettore Perazzoli committed
550

551
552
553
	gtk_signal_emit (GTK_OBJECT (view), fm_directory_view_signals[BEGIN_ADDING_ENTRIES]);
}

554
555
556
557
558
559
560
/**
 * 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.
561
 * @file: NautilusFile describing entry to add.
562
563
 * 
 **/
564
void
565
fm_directory_view_add_entry (FMDirectoryView *view, NautilusFile *file)
566
567
{
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));
Ettore Perazzoli's avatar
Ettore Perazzoli committed
568

569
	gtk_signal_emit (GTK_OBJECT (view), fm_directory_view_signals[ADD_ENTRY], file);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
570
571
}

572
573
574
575
576
577
578
579
580
/**
 * 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.
 * 
 **/
581
void
582
fm_directory_view_done_adding_entries (FMDirectoryView *view)
583
584
585
{
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));

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

589
590
591
592
593
594
595
596
597
/**
 * 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.
 * 
 **/
598
void
599
fm_directory_view_begin_loading (FMDirectoryView *view)
600
601
602
{
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));

603
	gtk_signal_emit (GTK_OBJECT (view), fm_directory_view_signals[BEGIN_LOADING]);
604
605
}

606
607
608
609
610
611
612
613
614
615
616
617
618
619
/**
 * fm_directory_view_bump_zoom_level:
 *
 * bump the current zoom level by invoking the relevant subclass through the slot
 * 
 **/
void
fm_directory_view_bump_zoom_level (FMDirectoryView *view, gint zoom_increment)
{
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));

	(* FM_DIRECTORY_VIEW_CLASS (GTK_OBJECT (view)->klass)->bump_zoom_level) (view, zoom_increment);
}

620
621
622
/**
 * fm_directory_view_get_selection:
 *
623
 * Get a list of NautilusFile pointers that represents the
624
625
626
627
628
 * 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.
 * 
629
 * Return value: GList of NautilusFile pointers representing the selection.
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
 * 
 **/
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;
}

/**
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
 * 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;
}

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
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");
734
735
736
	gtk_signal_connect(GTK_OBJECT (menu_item), "activate",
		           GTK_SIGNAL_FUNC (zoom_in_cb), view);

737
738
739
740
	gtk_widget_show (menu_item);
	gtk_menu_append (menu, menu_item);

	menu_item = gtk_menu_item_new_with_label ("Zoom out");
741
742
743
744
	gtk_signal_connect(GTK_OBJECT (menu_item), "activate",
		           GTK_SIGNAL_FUNC (zoom_out_cb), view);
	
        gtk_widget_show (menu_item);
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
	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);
}


796
797
798
799
800
801
802
803
/**
 * 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.
 * 
 **/
804
void
805
fm_directory_view_notify_selection_changed (FMDirectoryView *view)
806
807
808
{
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));

809
810
811
812
813
814
815
816
817
818
819
820
821
	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.
822
 * @file: A NautilusFile representing the entry in this view to activate.
823
824
825
 * 
 **/
void
826
fm_directory_view_activate_entry (FMDirectoryView *view, NautilusFile *file)
827
828
{
	GnomeVFSURI *new_uri;
829
830
	char *name;
	char *new_uri_text;
831
832
833
	Nautilus_NavigationRequestInfo nri;

	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));
834
835
	g_return_if_fail (file != NULL);

836
	name = nautilus_file_get_name (file);
837
838
	new_uri = gnome_vfs_uri_append_path(view->details->uri, name);
	g_free (name);
839

840
841
842
843
	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;
844
845
846
	nri.new_window_default = nri.new_window_suggested = Nautilus_V_FALSE;
	nri.new_window_enforced = Nautilus_V_UNKNOWN;
	nautilus_view_frame_request_location_change
847
848
849
		(NAUTILUS_VIEW_FRAME (view->details->view_frame), &nri);

	g_free (new_uri_text);
850
851
}

852
853
854
855
856
857
858
859
860
/**
 * 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
861
void
862
863
fm_directory_view_load_uri (FMDirectoryView *view,
			    const char *uri)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
864
865
{
	GnomeVFSResult result;
866
	Nautilus_ProgressRequestInfo pri;
867
	NautilusDirectory *old_model;
Ettore Perazzoli's avatar
Ettore Perazzoli committed
868
869

	g_return_if_fail (view != NULL);
870
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));
Ettore Perazzoli's avatar
Ettore Perazzoli committed
871
872
	g_return_if_fail (uri != NULL);

873
	fm_directory_view_stop (view);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
874

875
	old_model = view->details->model;
876
	view->details->model = nautilus_directory_get (uri);
877

878
879
880
	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
881

882
883
	if (view->details->directory_list != NULL)
		gnome_vfs_directory_list_destroy (view->details->directory_list);
884
	view->details->directory_list = NULL;
885
886
	view->details->current_position = GNOME_VFS_DIRECTORY_LIST_POSITION_NONE;

887
	fm_directory_view_clear (view);
888

889
890
891
	if (old_model != NULL)
		gtk_object_unref (GTK_OBJECT (old_model));

892
893
894
	memset(&pri, 0, sizeof(pri));
	pri.type = Nautilus_PROGRESS_UNDERWAY;
	pri.amount = 0;
895
896
	nautilus_view_frame_request_progress_change
		(NAUTILUS_VIEW_FRAME (view->details->view_frame), &pri);
897

Ettore Perazzoli's avatar
Ettore Perazzoli committed
898
	result = gnome_vfs_async_load_directory_uri
899
900
		(&view->details->vfs_async_handle, 	/* handle */
		 view->details->uri,			/* uri */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
901
902
903
904
		 (GNOME_VFS_FILE_INFO_GETMIMETYPE	/* options */
		  | GNOME_VFS_FILE_INFO_FASTMIMETYPE
		  | GNOME_VFS_FILE_INFO_FOLLOWLINKS),
		 NULL, 					/* meta_keys */
905
		 NULL, 					/* sort_rules */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
906
907
908
909
910
		 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
911
		 ENTRIES_PER_CB,			 /* items_per_notification */
Ettore Perazzoli's avatar
Ettore Perazzoli committed
912
913
914
		 directory_load_cb,	 		/* callback */
		 view);		 			/* callback_data */

915
	g_return_if_fail(result == GNOME_VFS_OK);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
916
917
}

918
919
920
921
922
923
924
/**
 * 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
925
void
926
fm_directory_view_stop (FMDirectoryView *view)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
927
928
{
	g_return_if_fail (view != NULL);
929
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));
Ettore Perazzoli's avatar
Ettore Perazzoli committed
930

931
	if (view->details->vfs_async_handle == NULL)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
932
933
		return;

934
935
	display_pending_entries (view);
	stop_load (view, FALSE);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
936
937
}

938
939
940
941
942
943
/**
 * 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.
944
 * @reverse_sort: TRUE if items should be sorted in reverse of standard order.
945
946
 * 
 **/
Ettore Perazzoli's avatar
Ettore Perazzoli committed
947
void
948
fm_directory_view_sort (FMDirectoryView *view,
949
950
			FMDirectoryViewSortType sort_type,
			gboolean reverse_sort)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
951
952
953
954
955
956
{
	GnomeVFSDirectorySortRule *rules;

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

	g_return_if_fail (view != NULL);
957
	g_return_if_fail (FM_IS_DIRECTORY_VIEW (view));
Ettore Perazzoli's avatar
Ettore Perazzoli committed
958

959
	if (view->details->directory_list == NULL)
Ettore Perazzoli's avatar
Ettore Perazzoli committed
960
961
962
		return;

	switch (sort_type) {
963
	case FM_DIRECTORY_VIEW_SORT_BYNAME:
964
965
		rules = ALLOC_RULES (2);
		/* Note: This used to put directories first. I
966
		 * thought that was counterintuitive and removed it,
967
968
969
970
971
		 * 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
972
		break;
973
	case FM_DIRECTORY_VIEW_SORT_BYSIZE:
Ettore Perazzoli's avatar
Ettore Perazzoli committed
974
975
976
977
978
979
		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;
980
	case FM_DIRECTORY_VIEW_SORT_BYTYPE:
Ettore Perazzoli's avatar
Ettore Perazzoli committed
981
982
983
984
985
986
		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;
987
988
989
990
991
992
	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
993
	default:
994
		g_warning ("fm_directory_view_sort: Unknown sort mode %d\n",
Ettore Perazzoli's avatar
Ettore Perazzoli committed
995
996
997
998
			   sort_type);
		return;
	}

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

1001
	fm_directory_view_repopulate (view);
Ettore Perazzoli's avatar
Ettore Perazzoli committed
1002
1003
1004

#undef ALLOC_RULES
}