nautilus-history-view.c 8.79 KB
Newer Older
1
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
Elliot Lee's avatar
Elliot Lee committed
2
3
4
5

/*
 *  Nautilus
 *
Elliot Lee's avatar
Elliot Lee committed
6
 *  Copyright (C) 1999, 2000 Red Hat, Inc.
Elliot Lee's avatar
Elliot Lee committed
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
 *
 *  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.
 *
 *  Author: Elliot Lee <sopwith@redhat.com>
 *
 */
25
 
26
#include <config.h>
Elliot Lee's avatar
Elliot Lee committed
27

28
29
#include <bonobo/bonobo-generic-factory.h>
#include <bonobo/bonobo-main.h>
30
#include <bonobo/bonobo-ui-util.h>
31
#include <gnome.h>
Elliot Lee's avatar
Elliot Lee committed
32
#include <libnautilus/libnautilus.h>
33
#include <libnautilus/nautilus-view-component.h>
34
#include <libnautilus-extensions/nautilus-gdk-pixbuf-extensions.h>
35
36
#include <libnautilus-extensions/nautilus-bookmark.h>
#include <libnautilus-extensions/nautilus-icon-factory.h>
37
#include <libgnome/gnome-i18n.h>
38
#include <libgnomevfs/gnome-vfs-init.h>
39
#include <libgnomevfs/gnome-vfs-uri.h>
40
#include <liboaf/liboaf.h>
Elliot Lee's avatar
Elliot Lee committed
41
42

typedef struct {
43
44
45
	NautilusView *view;
	Nautilus_HistoryFrame history_frame;
	GtkCList *clist;
Elliot Lee's avatar
Elliot Lee committed
46

47
	gint notify_count;
48
	gint press_row;
Elliot Lee's avatar
Elliot Lee committed
49
50
} HistoryView;

51
52
53
54
#define HISTORY_VIEW_COLUMN_ICON	0
#define HISTORY_VIEW_COLUMN_NAME	1
#define HISTORY_VIEW_COLUMN_COUNT	2

55
56
57
58
59

static void history_load_location 	(NautilusView 	*view, 
					const char 	*location, 
					HistoryView 	*hview);

60
static NautilusBookmark *
61
62
get_bookmark_from_row (GtkCList *clist, int row)
{
63
64
	g_assert (NAUTILUS_IS_BOOKMARK (gtk_clist_get_row_data (clist, row)));
	return NAUTILUS_BOOKMARK (gtk_clist_get_row_data (clist, row));  
65
66
}

67
static char *
68
69
get_uri_from_row (GtkCList *clist, int row)
{
70
	return nautilus_bookmark_get_uri (get_bookmark_from_row (clist, row));
71
72
}

73
74
75
76
77
78
static Nautilus_HistoryFrame
history_view_frame_call_begin (NautilusView *view, CORBA_Environment *ev)
{
	g_return_val_if_fail (NAUTILUS_IS_VIEW (view), CORBA_OBJECT_NIL);
	
	CORBA_exception_init (ev);
79
	return Bonobo_Unknown_queryInterface 
80
81
82
83
84
85
86
		(bonobo_control_get_control_frame (nautilus_view_get_bonobo_control (view)),
		 "IDL:Nautilus/HistoryFrame:1.0", ev);
}

static void
history_view_frame_call_end (Nautilus_HistoryFrame frame, CORBA_Environment *ev)
{
87
	bonobo_object_release_unref (frame, ev);
88
89
	CORBA_exception_free (ev);
}
90
91

static void
92
install_icon (GtkCList *clist, gint row, GdkPixbuf *pixbuf)
93
94
{
	GdkPixmap *pixmap;
95
	GdkBitmap *mask;
96
	NautilusBookmark *bookmark;
97

98
	bookmark = get_bookmark_from_row (clist, row);
99

100
101
102
103
104
105
106
107
108
109
110
	if (pixbuf != NULL) {
		gdk_pixbuf_render_pixmap_and_mask (pixbuf, &pixmap, &mask, 
						   NAUTILUS_STANDARD_ALPHA_THRESHHOLD);
	} else {
		if (!nautilus_bookmark_get_pixmap_and_mask (bookmark, NAUTILUS_ICON_SIZE_SMALLER, 
							   &pixmap, &mask)) {
			return;
		}
	}
	
	gtk_clist_set_pixmap (clist, row, HISTORY_VIEW_COLUMN_ICON, pixmap, mask);
111
112
}

113
static void
114
history_view_update_icons (GtkObject *ignored, HistoryView *hview)
115
{
116
117
	/* Reload all bookmarks and pixbufs */
	history_load_location (hview->view, NULL, hview);
118
119
}

120
static Nautilus_HistoryList *
121
122
123
124
get_history_list (HistoryView *hview)
{
	CORBA_Environment ev;
	Nautilus_HistoryFrame view_frame;
125
	Nautilus_HistoryList *list;
126
127

	view_frame = history_view_frame_call_begin (hview->view, &ev);
128
	list = Nautilus_HistoryFrame_get_history_list (view_frame, &ev);		
129
130
131
	history_view_frame_call_end (view_frame, &ev);

	return list;
132
133
}

134
135


Elliot Lee's avatar
Elliot Lee committed
136
static void
137
history_load_location (NautilusView *view, const char *location, HistoryView *hview)
Elliot Lee's avatar
Elliot Lee committed
138
{
139
140
141
142
	char *cols[HISTORY_VIEW_COLUMN_COUNT];
	int new_rownum;
	GtkCList *clist;
	NautilusBookmark *bookmark;
143
	Nautilus_HistoryList *history_list;
144
	Nautilus_HistoryItem *item;
145
	GdkPixbuf *pixbuf;
146
	guint i;
147
	
148
149
150
151
152
153
	static int lock = 0;

	if (lock != 0) {
		return;
	}
	lock = 1;
154
155
156
157

	hview->notify_count++;

	clist = hview->clist;
158
	gtk_clist_freeze (clist);
159
160
161
162
163

	/* Clear out list */
	gtk_clist_clear (clist);

	/* Populate with data from main history list */	
164
	history_list = get_history_list (hview);
165
	
166
	for (i = 0; i < history_list->_length; i++) {
167
		item = &history_list->_buffer[i];
168
169
170
		bookmark = nautilus_bookmark_new (item->location, item->title);
		
		cols[HISTORY_VIEW_COLUMN_ICON] = NULL;
171
		cols[HISTORY_VIEW_COLUMN_NAME] = item->title;
172
173
174
175
176

		new_rownum = gtk_clist_append (clist, cols);
		
		gtk_clist_set_row_data_full (clist, new_rownum, bookmark,
					     (GtkDestroyNotify) gtk_object_unref);
177
178
179
180
181
182
183
184

		pixbuf = bonobo_ui_util_xml_to_pixbuf (item->icon);
		if (pixbuf != NULL) {
			install_icon (clist, new_rownum, pixbuf);
			gdk_pixbuf_unref (pixbuf);
		} else {
			install_icon (clist, new_rownum, NULL);
		}
185
186
187
188
189
		
		gtk_clist_columns_autosize (clist);
		
		if (gtk_clist_row_is_visible(clist, new_rownum) != GTK_VISIBILITY_FULL) {
			gtk_clist_moveto(clist, new_rownum, -1, 0.5, 0.0);
190
191
		}
	}
192
	CORBA_free (history_list);
193

194
	gtk_clist_select_row (clist, 0, 0);
195
	
196
	gtk_clist_thaw (clist);
197
198
	
  	hview->notify_count--;
199
200
201
202
203
204

	lock = 0;
}

static void
history_title_changed (NautilusView *view,
205
		       const char *title,
206
207
208
		       HistoryView *hview)
{
	history_load_location (view, NULL, hview);
Elliot Lee's avatar
Elliot Lee committed
209
210
}

211

Elliot Lee's avatar
Elliot Lee committed
212
static void
213
214
215
216
217
218
219
220
221
222
223
224
history_button_press (GtkCList *clist, GdkEventButton *event, HistoryView *hview)
{
	int row, column;
		
	/* Get row and column */
	gtk_clist_get_selection_info (clist, event->x, event->y, &row, &column);

	hview->press_row = row;
}

static void
history_button_release (GtkCList *clist, GdkEventButton *event, HistoryView *hview)
Elliot Lee's avatar
Elliot Lee committed
225
{
226
	char *uri;
227
	int row, column;
228
229
230
231
	
	if(hview->notify_count > 0) {
		return;
	}
232
233
234
	
	/* Get row and column */
	gtk_clist_get_selection_info (clist, event->x, event->y, &row, &column);
Elliot Lee's avatar
Elliot Lee committed
235

236
237
238
239
240
241
242
243
244
245
246
247
248
249
	/* 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;
	}
	
 	/* Return if the row does not match the rwo we stashed on the mouse down. */
	if (row != hview->press_row) {
		return;
	}
	
	/* Navigate to uri */
	uri = get_uri_from_row (clist, row);
250
	nautilus_view_open_location_in_this_window (hview->view, uri);
251
	g_free (uri);
Elliot Lee's avatar
Elliot Lee committed
252
253
}

254
255
256
static int object_count = 0;

static void
257
do_destroy(GtkObject *obj, HistoryView *hview)
258
{
259
260
261
262
	object_count--;
	if(object_count <= 0) {
    		gtk_main_quit();
    	}
263
264
    	
    	gtk_signal_disconnect_by_data (nautilus_icon_factory_get (), hview);
265
}
Elliot Lee's avatar
Elliot Lee committed
266

267

268
static BonoboObject *
269
make_obj (BonoboGenericFactory *Factory, const char *goad_id, gpointer closure)
Elliot Lee's avatar
Elliot Lee committed
270
{
271
272
273
	GtkWidget *wtmp;
	GtkCList *clist;
	HistoryView *hview;
Elliot Lee's avatar
Elliot Lee committed
274

275
  	g_return_val_if_fail(!strcmp(goad_id, "OAFIID:nautilus_history_view:a7a85bdd-2ecf-4bc1-be7c-ed328a29aacb"), NULL);
Elliot Lee's avatar
Elliot Lee committed
276

277
  	hview = g_new0(HistoryView, 1);
Elliot Lee's avatar
Elliot Lee committed
278

279
280
281
282
	/* create interface */
  	clist = GTK_CLIST (gtk_clist_new (HISTORY_VIEW_COLUMN_COUNT));
  	gtk_clist_column_titles_hide (clist);
	gtk_clist_set_row_height (clist, NAUTILUS_ICON_SIZE_SMALLER);
283
284
285
	gtk_clist_set_selection_mode (clist, GTK_SELECTION_BROWSE);
	gtk_clist_columns_autosize (clist);
	
286
287
288
289
290
291
	wtmp = gtk_scrolled_window_new (gtk_clist_get_hadjustment (clist),
					gtk_clist_get_vadjustment (clist));
	gtk_scrolled_window_set_policy (GTK_SCROLLED_WINDOW (wtmp),
					GTK_POLICY_AUTOMATIC,
					GTK_POLICY_AUTOMATIC);
	gtk_container_add (GTK_CONTAINER (wtmp), GTK_WIDGET (clist));
Elliot Lee's avatar
Elliot Lee committed
292

293
	gtk_widget_show_all (wtmp);
294

295
296
297
298
	/* create object */
	hview->view = nautilus_view_new (wtmp);
	gtk_signal_connect (GTK_OBJECT (hview->view), "destroy", do_destroy, hview);
	object_count++;
299

300
	hview->clist = (GtkCList *)clist;
301

302
	/* handle events */
303
	gtk_signal_connect (GTK_OBJECT(hview->view), "load_location", 
304
			   history_load_location, hview);
305
	gtk_signal_connect (GTK_OBJECT(hview->view), "title_changed", 
306
			   history_title_changed, hview);
307

308
309
310
	gtk_signal_connect (GTK_OBJECT (clist), "button-press-event", history_button_press, hview);
	gtk_signal_connect (GTK_OBJECT (clist), "button-release-event", history_button_release, hview);
	gtk_signal_connect (nautilus_icon_factory_get (), "icons_changed", history_view_update_icons, hview);
Elliot Lee's avatar
Elliot Lee committed
311

312
	return BONOBO_OBJECT (hview->view);
Elliot Lee's avatar
Elliot Lee committed
313
314
}

315
316
int
main (int argc, char *argv[])
Elliot Lee's avatar
Elliot Lee committed
317
{
318
319
	BonoboGenericFactory *factory;
	CORBA_ORB orb;
320
	char *registration_id;
Elliot Lee's avatar
Elliot Lee committed
321

322
323
324
	gnome_init_with_popt_table ("nautilus-history-view", VERSION, 
				    argc, argv,
				    oaf_popt_options, 0, NULL); 
325
	orb = oaf_init (argc, argv);
326
	bonobo_init (orb, CORBA_OBJECT_NIL, CORBA_OBJECT_NIL);
327
	gnome_vfs_init ();
Elliot Lee's avatar
Elliot Lee committed
328

329
330
331
332
333
	registration_id = oaf_make_registration_id ("OAFIID:nautilus_history_view_factory:912d6634-d18f-40b6-bb83-bdfe16f1d15e", g_getenv ("DISPLAY"));

	factory = bonobo_generic_factory_new_multi (registration_id, make_obj, NULL);

	g_free (registration_id);
Elliot Lee's avatar
Elliot Lee committed
334

335
336
337
	do {
		bonobo_main();
  	} while(object_count > 0);
Elliot Lee's avatar
Elliot Lee committed
338

339
	return 0;
Elliot Lee's avatar
Elliot Lee committed
340
}