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

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

43
	gint notify_count;
44
	gint press_row;
45
	BonoboUIHandler *uih;
Elliot Lee's avatar
Elliot Lee committed
46
47
} HistoryView;

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

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

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

65
66
67
68
69
70
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);
	return Bonobo_Unknown_query_interface 
		(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)
{
79
	bonobo_object_release_unref (frame, ev);
80
81
	CORBA_exception_free (ev);
}
82
83

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

90
	bookmark = get_bookmark_from_row (clist, row);
91
	
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
	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);
}

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

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

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

	return list;
129
130
}

131
132


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

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

	hview->notify_count++;

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

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

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

		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);
180
181
		}
	}
182
	CORBA_free (history_list);
183

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

	lock = 0;
}

static void
history_title_changed (NautilusView *view,
		       HistoryView *hview)
{
	history_load_location (view, NULL, hview);
Elliot Lee's avatar
Elliot Lee committed
198
199
}

200

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

225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
	/* 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
241
242
}

243
244
245
static int object_count = 0;

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

254

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

262
  	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
263

264
  	hview = g_new0(HistoryView, 1);
Elliot Lee's avatar
Elliot Lee committed
265

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

280
	gtk_widget_show_all (wtmp);
281

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

287
	hview->clist = (GtkCList *)clist;
288

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

	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
297

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

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

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

319
	factory = bonobo_generic_factory_new_multi ("OAFIID:nautilus_history_view_factory:912d6634-d18f-40b6-bb83-bdfe16f1d15e", make_obj, NULL);
Elliot Lee's avatar
Elliot Lee committed
320

321
322
323
	do {
		bonobo_main();
  	} while(object_count > 0);
Elliot Lee's avatar
Elliot Lee committed
324

325
	return 0;
Elliot Lee's avatar
Elliot Lee committed
326
}