summaryrefslogtreecommitdiff
path: root/components/history/nautilus-history-view.c
blob: 3e1855dc1cad88ce613ae213ed2889bae9130e1e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */

/*
 *  Nautilus
 *
 *  Copyright (C) 1999, 2000 Red Hat, Inc.
 *  Copyright (C) 2000, 2001 Eazel, Inc.
 *
 *  This library is free software; you can redistribute it and/or
 *  modify it under the terms of the GNU General Public License as
 *  published by the Free Software Foundation; either version 2 of the
 *  License, or (at your option) any later version.
 *
 *  This library is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 *  General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this library; if not, write to the Free Software
 *  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *  Authors: Elliot Lee <sopwith@redhat.com>
 *           Darin Adler <darin@eazel.com>
 *
 */
 
#include <config.h>

#include <bonobo/bonobo-ui-util.h>
#include <gtk/gtkclist.h>
#include <gtk/gtkscrolledwindow.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>
#include <libnautilus-extensions/nautilus-bookmark.h>
#include <eel/eel-gdk-pixbuf-extensions.h>
#include <eel/eel-gtk-macros.h>
#include <libnautilus/nautilus-view-standard-main.h>

#define NAUTILUS_TYPE_HISTORY_VIEW            (nautilus_history_view_get_type ())
#define NAUTILUS_HISTORY_VIEW(obj)            (GTK_CHECK_CAST ((obj), NAUTILUS_TYPE_HISTORY_VIEW, NautilusHistoryView))
#define NAUTILUS_HISTORY_VIEW_CLASS(klass)    (GTK_CHECK_CLASS_CAST ((klass), NAUTILUS_TYPE_HISTORY_VIEW, NautilusHistoryViewClass))
#define NAUTILUS_IS_HISTORY_VIEW(obj)         (GTK_CHECK_TYPE ((obj), NAUTILUS_TYPE_HISTORY_VIEW))
#define NAUTILUS_IS_HISTORY_VIEW_CLASS(klass) (GTK_CHECK_CLASS_TYPE ((klass), NAUTILUS_TYPE_HISTORY_VIEW))

typedef struct {
	NautilusView parent;
	GtkCList *list;
	gboolean updating_history;
	int press_row;
	gboolean *external_destroyed_flag;
} NautilusHistoryView;

typedef struct {
	NautilusViewClass parent;
} NautilusHistoryViewClass;

#define HISTORY_VIEW_COLUMN_ICON	0
#define HISTORY_VIEW_COLUMN_NAME	1
#define HISTORY_VIEW_COLUMN_COUNT	2

static GtkType nautilus_history_view_get_type         (void);
static void    nautilus_history_view_initialize_class (NautilusHistoryViewClass *klass);
static void    nautilus_history_view_initialize       (NautilusHistoryView      *view);
static void    nautilus_history_view_destroy          (GtkObject                *object);

EEL_DEFINE_CLASS_BOILERPLATE (NautilusHistoryView,
				   nautilus_history_view,
				   NAUTILUS_TYPE_VIEW)

static NautilusBookmark *
get_bookmark_from_row (GtkCList *list, int row)
{
	return NAUTILUS_BOOKMARK (gtk_clist_get_row_data (list, row));
}

static char *
get_uri_from_row (GtkCList *list, int row)
{
	return nautilus_bookmark_get_uri (get_bookmark_from_row (list, row));
}

static void
install_icon (GtkCList *list, int row, GdkPixbuf *pixbuf)
{
	GdkPixmap *pixmap;
	GdkBitmap *mask;
	NautilusBookmark *bookmark;

	if (pixbuf != NULL) {
		gdk_pixbuf_render_pixmap_and_mask (pixbuf, &pixmap, &mask, 
						   EEL_STANDARD_ALPHA_THRESHHOLD);
	} else {
		bookmark = get_bookmark_from_row (list, row);
		if (!nautilus_bookmark_get_pixmap_and_mask (bookmark, NAUTILUS_ICON_SIZE_SMALLER,
							    &pixmap, &mask)) {
			return;
		}
	}
	
	gtk_clist_set_pixmap (list, row, HISTORY_VIEW_COLUMN_ICON, pixmap, mask);

	gdk_pixmap_unref (pixmap);
	if (mask != NULL) {
		gdk_pixmap_unref (mask);
	}
}

static void
update_history (NautilusHistoryView *view,
		Nautilus_History *history)
{
	char *cols[HISTORY_VIEW_COLUMN_COUNT];
	int new_row;
	GtkCList *list;
	NautilusBookmark *bookmark;
	Nautilus_HistoryItem *item;
	GdkPixbuf *pixbuf;
	guint i;
	gboolean destroyed_flag;

	/* FIXME: We'll end up with old history if this happens. */
	if (view->updating_history) {
		return;
	}

	list = view->list;

	if (GTK_OBJECT_DESTROYED (list)) {
		return;
	}

	/* Set up a local boolean so we can detect that the view has
	 * been destroyed. We can't ask the view itself because once
	 * it's destroyed it's pointer is a pointer to freed storage.
	 */
	/* FIXME: We can't just keep an extra ref to the view as we
	 * normally would because of a bug in Bonobo that means a
	 * BonoboControl must not outlast its BonoboFrame
	 * (NautilusHistoryView is a BonoboControl).
	 */
	destroyed_flag = FALSE;
	view->external_destroyed_flag = &destroyed_flag;

	view->updating_history = TRUE;

	gtk_clist_freeze (list);

	gtk_clist_clear (list);

	for (i = 0; i < history->_length; i++) {
		item = &history->_buffer[i];
		bookmark = nautilus_bookmark_new (item->location, item->title);

		/* Through a long line of calls, nautilus_bookmark_new
		 * can end up calling through to CORBA, so a remote
		 * unref can come in at this point. In theory, other
		 * calls could result in a similar problem, so in
		 * theory we need this check after any call out, but
		 * in practice, none of the other calls used here have
		 * that problem.
		 */
		if (destroyed_flag) {
			return;
		}
		
		cols[HISTORY_VIEW_COLUMN_ICON] = NULL;
		cols[HISTORY_VIEW_COLUMN_NAME] = item->title;

		new_row = gtk_clist_append (list, cols);
		
		gtk_clist_set_row_data_full (list, new_row, bookmark,
					     (GtkDestroyNotify) gtk_object_unref);

		pixbuf = bonobo_ui_util_xml_to_pixbuf (item->icon);
		install_icon (list, new_row, pixbuf);
		if (pixbuf != NULL) {
			gdk_pixbuf_unref (pixbuf);
		}
		
		gtk_clist_columns_autosize (list);
		
		if (gtk_clist_row_is_visible (list, new_row) != GTK_VISIBILITY_FULL) {
			gtk_clist_moveto (list, new_row, -1, 0.5, 0.0);
		}
	}

	gtk_clist_select_row (list, 0, 0);
	
	gtk_clist_thaw (list);
	
  	view->updating_history = FALSE;

	view->external_destroyed_flag = NULL;
}

static void
button_press_callback (GtkCList *list,
		       GdkEventButton *event,
		       NautilusHistoryView *view)
{
	int row, column;

	if (event->button != 1) {
		return;
	}

	gtk_clist_get_selection_info (list, event->x, event->y, &row, &column);

	view->press_row = row;
}

static void
button_release_callback (GtkCList *list,
			 GdkEventButton *event,
			 NautilusHistoryView *view)
{
	char *uri;
	int row, column;
	
	/* FIXME: Is it really a good idea to just ignore button presses when we are updating? */
	if (view->updating_history) {
		return;
	}
	
	if (event->button != 1) {
		return;
	}
	
	gtk_clist_get_selection_info (list, event->x, event->y, &row, &column);
	
	/* Do nothing if row is zero. A click either in the top list
	 * item or in the history content view is ignored.
	 */
	if (row <= 0) {
		return;
	}
	
 	/* Do nothing if the row does not match the row we stashed on
	 * the mouse down. This means that dragging will not cause
	 * navigation.
	 */
	if (row != view->press_row) {
		return;
	}
	
	/* Navigate to the clicked location. */
	uri = get_uri_from_row (list, row);
	nautilus_view_open_location_in_this_window
		(NAUTILUS_VIEW (view), uri);
	g_free (uri);
}

static void
history_changed_callback (NautilusHistoryView *view,
			  Nautilus_History *list,
			  gpointer callback_data)
{
	g_assert (view == callback_data);

	update_history (view, list);
}

static void
nautilus_history_view_initialize_class (NautilusHistoryViewClass *klass)
{
	GtkObjectClass *object_class;
	
	object_class = GTK_OBJECT_CLASS (klass);
	
	object_class->destroy = nautilus_history_view_destroy;
}

static void
nautilus_history_view_initialize (NautilusHistoryView *view)
{
	GtkCList *list;
	GtkWidget *window;

  	list = GTK_CLIST (gtk_clist_new (HISTORY_VIEW_COLUMN_COUNT));
  	gtk_clist_column_titles_hide (list);
	gtk_clist_set_row_height (list, NAUTILUS_ICON_SIZE_SMALLER);
	gtk_clist_set_selection_mode (list, GTK_SELECTION_BROWSE);
	gtk_clist_columns_autosize (list);
	gtk_widget_show (GTK_WIDGET (list));
	
	window = gtk_scrolled_window_new (gtk_clist_get_hadjustment (list),
					  gtk_clist_get_vadjustment (list));
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (window),
					GTK_POLICY_AUTOMATIC,
					GTK_POLICY_AUTOMATIC);
	gtk_container_add (GTK_CONTAINER (window), GTK_WIDGET (list));
	gtk_widget_show (window);
	
	nautilus_view_construct (NAUTILUS_VIEW (view), window);

	gtk_object_ref (GTK_OBJECT (list));
	view->list = list;

	gtk_signal_connect (GTK_OBJECT (list),
			    "button-press-event",
			    button_press_callback,
			    view);
	gtk_signal_connect (GTK_OBJECT (list),
			    "button-release-event",
			    button_release_callback,
			    view);

	gtk_signal_connect (GTK_OBJECT (view),
			    "history_changed", 
			    history_changed_callback,
			    view);
}

static void
nautilus_history_view_destroy (GtkObject *object)
{
	NautilusHistoryView *view;
	
	view = NAUTILUS_HISTORY_VIEW (object);

	if (view->external_destroyed_flag != NULL) {
		*view->external_destroyed_flag = TRUE;
	}

	gtk_object_unref (GTK_OBJECT (view->list));

	EEL_CALL_PARENT (GTK_OBJECT_CLASS, destroy, (object));
}

int
main (int argc, char *argv[])
{
	/* Initialize gettext support */
#ifdef ENABLE_NLS
	bindtextdomain (PACKAGE, GNOMELOCALEDIR);
	textdomain (PACKAGE);
#endif

	return nautilus_view_standard_main ("nautilus_history-view", VERSION,
					    argc, argv,
					    "OAFIID:nautilus_history_view_factory:912d6634-d18f-40b6-bb83-bdfe16f1d15e",
					    "OAFIID:nautilus_history_view:a7a85bdd-2ecf-4bc1-be7c-ed328a29aacb",
					    nautilus_view_create_from_get_type_function,
					    nautilus_history_view_get_type);
}