nautilus-history-view.c 8.24 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 <gnome.h>
Elliot Lee's avatar
Elliot Lee committed
31
#include <libnautilus/libnautilus.h>
32
#include <libnautilus/nautilus-view-component.h>
33
34
#include <libnautilus-extensions/nautilus-bookmark.h>
#include <libnautilus-extensions/nautilus-icon-factory.h>
35
#include <libgnome/gnome-i18n.h>
36
#include <libgnomevfs/gnome-vfs-init.h>
37
#include <libgnomevfs/gnome-vfs-uri.h>
38
#include <liboaf/liboaf.h>
Elliot Lee's avatar
Elliot Lee committed
39
40

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

45
	gint notify_count;
46
	gint press_row;
Elliot Lee's avatar
Elliot Lee committed
47
48
} HistoryView;

49
50
51
52
#define HISTORY_VIEW_COLUMN_ICON	0
#define HISTORY_VIEW_COLUMN_NAME	1
#define HISTORY_VIEW_COLUMN_COUNT	2

53
static NautilusBookmark *
54
55
get_bookmark_from_row (GtkCList *clist, int row)
{
56
57
	g_assert (NAUTILUS_IS_BOOKMARK (gtk_clist_get_row_data (clist, row)));
	return NAUTILUS_BOOKMARK (gtk_clist_get_row_data (clist, row));  
58
59
}

60
static char *
61
62
get_uri_from_row (GtkCList *clist, int row)
{
63
	return nautilus_bookmark_get_uri (get_bookmark_from_row (clist, row));
64
65
}

66
67
68
69
70
71
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);
72
	return Bonobo_Unknown_queryInterface 
73
74
75
76
77
78
79
		(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)
{
80
	bonobo_object_release_unref (frame, ev);
81
82
	CORBA_exception_free (ev);
}
83
84

static void
85
install_icon (GtkCList *clist, gint row)
86
87
88
{
	GdkPixmap *pixmap;
	GdkBitmap *bitmap;
89
	NautilusBookmark *bookmark;
90

91
	bookmark = get_bookmark_from_row (clist, row);
92
	
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
	if (!nautilus_bookmark_get_pixmap_and_mask (bookmark,
		  				    NAUTILUS_ICON_SIZE_SMALLER,
						    &pixmap,
						    &bitmap))
	{
		return;
	}

	gtk_clist_set_pixmap (clist,	
			      row,
			      HISTORY_VIEW_COLUMN_ICON,
			      pixmap,
			      bitmap);
}

108
static void
109
history_view_update_icons (GtkCList *clist)
110
{
111
	int row;
112
		
113
	for (row = 0; row < clist->rows; ++row) {
114
115
116
117
		install_icon (clist, row);
	}
}

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

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

	return list;
130
131
}

132
133


Elliot Lee's avatar
Elliot Lee committed
134
static void
135
history_load_location (NautilusView *view,
136
137
		       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
	int i;
146
147
148
149
150
151
	static int lock = 0;

	if (lock != 0) {
		return;
	}
	lock = 1;
152
153
154
155

	hview->notify_count++;

	clist = hview->clist;
156
	gtk_clist_freeze (clist);
157
158
159
160
161

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

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

		new_rownum = gtk_clist_append (clist, cols);
		
		gtk_clist_set_row_data_full (clist, new_rownum, bookmark,
					     (GtkDestroyNotify) gtk_object_unref);
		install_icon (clist, new_rownum);
		
		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);
181
182
		}
	}
183
	CORBA_free (history_list);
184

185
	gtk_clist_select_row (clist, 0, 0);
186
	
187
	gtk_clist_thaw (clist);
188
189
	
  	hview->notify_count--;
190
191
192
193
194
195

	lock = 0;
}

static void
history_title_changed (NautilusView *view,
196
		       const char *title,
197
198
199
		       HistoryView *hview)
{
	history_load_location (view, NULL, hview);
Elliot Lee's avatar
Elliot Lee committed
200
201
}

202

Elliot Lee's avatar
Elliot Lee committed
203
static void
204
205
206
207
208
209
210
211
212
213
214
215
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
216
{
217
	char *uri;
218
	int row, column;
219
220
221
222
	
	if(hview->notify_count > 0) {
		return;
	}
223
224
225
	
	/* Get row and column */
	gtk_clist_get_selection_info (clist, event->x, event->y, &row, &column);
Elliot Lee's avatar
Elliot Lee committed
226

227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
	/* 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);
	nautilus_view_open_location (hview->view, uri);
	g_free (uri);
Elliot Lee's avatar
Elliot Lee committed
243
244
}

245
246
247
static int object_count = 0;

static void
248
do_destroy(GtkObject *obj, HistoryView *hview)
249
{
250
251
252
253
	object_count--;
	if(object_count <= 0) {
    		gtk_main_quit();
    	}
254
}
Elliot Lee's avatar
Elliot Lee committed
255

256

257
static BonoboObject *
258
make_obj (BonoboGenericFactory *Factory, const char *goad_id, gpointer closure)
Elliot Lee's avatar
Elliot Lee committed
259
{
260
261
262
	GtkWidget *wtmp;
	GtkCList *clist;
	HistoryView *hview;
Elliot Lee's avatar
Elliot Lee committed
263

264
  	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
265

266
  	hview = g_new0(HistoryView, 1);
Elliot Lee's avatar
Elliot Lee committed
267

268
269
270
271
	/* 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);
272
273
274
	gtk_clist_set_selection_mode (clist, GTK_SELECTION_BROWSE);
	gtk_clist_columns_autosize (clist);
	
275
276
277
278
279
280
	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
281

282
	gtk_widget_show_all (wtmp);
283

284
285
286
287
	/* create object */
	hview->view = nautilus_view_new (wtmp);
	gtk_signal_connect (GTK_OBJECT (hview->view), "destroy", do_destroy, hview);
	object_count++;
288

289
	hview->clist = (GtkCList *)clist;
290

291
	/* handle events */
292
293
	gtk_signal_connect(GTK_OBJECT(hview->view), "load_location", 
			   history_load_location, hview);
294
295
	gtk_signal_connect(GTK_OBJECT(hview->view), "title_changed", 
			   history_title_changed, hview);
296
297
298

	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);
Elliot Lee's avatar
Elliot Lee committed
299

300
	gtk_signal_connect_object_while_alive (nautilus_icon_factory_get (),
301
					 "icons_changed",
302
					 history_view_update_icons,
303
					 GTK_OBJECT (hview->clist));
304
305
					 
	return BONOBO_OBJECT (hview->view);
Elliot Lee's avatar
Elliot Lee committed
306
307
}

308
309
int
main (int argc, char *argv[])
Elliot Lee's avatar
Elliot Lee committed
310
{
311
312
	BonoboGenericFactory *factory;
	CORBA_ORB orb;
313
	char *registration_id;
Elliot Lee's avatar
Elliot Lee committed
314

315
316
317
	gnome_init_with_popt_table ("nautilus-history-view", VERSION, 
				    argc, argv,
				    oaf_popt_options, 0, NULL); 
318
319
320
	orb = oaf_init (argc, argv);
	bonobo_init(orb, CORBA_OBJECT_NIL, CORBA_OBJECT_NIL);
	gnome_vfs_init ();
Elliot Lee's avatar
Elliot Lee committed
321

322
323
324
325
326
	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
327

328
329
330
	do {
		bonobo_main();
  	} while(object_count > 0);
Elliot Lee's avatar
Elliot Lee committed
331

332
	return 0;
Elliot Lee's avatar
Elliot Lee committed
333
}