nautilus-history-view.c 7.8 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
	Nautilus_HistoryFrame_unref (frame, ev);
80
81
82
	CORBA_Object_release (frame, ev);
	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
	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_History *
118
119
120
121
get_history_list (HistoryView *hview)
{
	CORBA_Environment ev;
	Nautilus_HistoryFrame view_frame;
122
	Nautilus_History *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
}

Elliot Lee's avatar
Elliot Lee committed
131
static void
132
history_load_location (NautilusView *view,
133
134
                                            const char *location,
                                            HistoryView *hview)
Elliot Lee's avatar
Elliot Lee committed
135
{
136
137
138
139
	char *cols[HISTORY_VIEW_COLUMN_COUNT];
	int new_rownum;
	GtkCList *clist;
	NautilusBookmark *bookmark;
140
	Nautilus_History *history;
141
	Nautilus_HistoryItem *item;
142
	int i;
143
144
145
146

	hview->notify_count++;

	clist = hview->clist;
147
	gtk_clist_freeze (clist);
148
149
150
151
152

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

	/* Populate with data from main history list */	
153
154
155
156
157
158
	history = get_history_list (hview);
	for (i = 0; i < history->list._length; i++) {
		item = &history->list._buffer[i];		
		bookmark = nautilus_bookmark_new (item->location, item->title);
		
		cols[HISTORY_VIEW_COLUMN_ICON] = NULL;
159
		cols[HISTORY_VIEW_COLUMN_NAME] = item->title;
160
161
162
163
164
165
166
167
168
169
170

		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);
171
172
		}
	}
173
	CORBA_free (history);
174

175
	gtk_clist_select_row (clist, 0, 0);
176
	
177
	gtk_clist_thaw (clist);
178
179
	
  	hview->notify_count--;
Elliot Lee's avatar
Elliot Lee committed
180
181
}

182

Elliot Lee's avatar
Elliot Lee committed
183
static void
184
185
186
187
188
189
190
191
192
193
194
195
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
196
{
197
	char *uri;
198
	int row, column;
199
200
201
202
	
	if(hview->notify_count > 0) {
		return;
	}
203
204
205
	
	/* Get row and column */
	gtk_clist_get_selection_info (clist, event->x, event->y, &row, &column);
Elliot Lee's avatar
Elliot Lee committed
206

207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
	/* 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
223
224
}

225
226
227
static int object_count = 0;

static void
228
do_destroy(GtkObject *obj, HistoryView *hview)
229
{
230
231
232
233
	object_count--;
	if(object_count <= 0) {
    		gtk_main_quit();
    	}
234
}
Elliot Lee's avatar
Elliot Lee committed
235

236

237
238
static BonoboObject *
make_obj(BonoboGenericFactory *Factory, const char *goad_id, gpointer closure)
Elliot Lee's avatar
Elliot Lee committed
239
{
240
241
242
	GtkWidget *wtmp;
	GtkCList *clist;
	HistoryView *hview;
Elliot Lee's avatar
Elliot Lee committed
243

244
  	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
245

246
  	hview = g_new0(HistoryView, 1);
Elliot Lee's avatar
Elliot Lee committed
247

248
249
250
251
	/* 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);
252
253
254
	gtk_clist_set_selection_mode (clist, GTK_SELECTION_BROWSE);
	gtk_clist_columns_autosize (clist);
	
255
256
257
258
259
260
	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
261

262
	gtk_widget_show_all (wtmp);
263

264
265
266
267
	/* create object */
	hview->view = nautilus_view_new (wtmp);
	gtk_signal_connect (GTK_OBJECT (hview->view), "destroy", do_destroy, hview);
	object_count++;
268

269
	hview->clist = (GtkCList *)clist;
270

271
	/* handle events */
272
273
274
275
276
	gtk_signal_connect(GTK_OBJECT(hview->view), "load_location", 
			   history_load_location, hview);

	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
277

278
	gtk_signal_connect_object_while_alive (nautilus_icon_factory_get (),
279
					 "icons_changed",
280
					 history_view_update_icons,
281
					 GTK_OBJECT (hview->clist));
282
283
					 
	return BONOBO_OBJECT (hview->view);
Elliot Lee's avatar
Elliot Lee committed
284
285
}

286
287
int
main (int argc, char *argv[])
Elliot Lee's avatar
Elliot Lee committed
288
{
289
290
	BonoboGenericFactory *factory;
	CORBA_ORB orb;
Elliot Lee's avatar
Elliot Lee committed
291

292
293
294
	gnome_init_with_popt_table ("nautilus-history-view", VERSION, 
				    argc, argv,
				    oaf_popt_options, 0, NULL); 
295
296
297
	orb = oaf_init (argc, argv);
	bonobo_init(orb, CORBA_OBJECT_NIL, CORBA_OBJECT_NIL);
	gnome_vfs_init ();
Elliot Lee's avatar
Elliot Lee committed
298

299
	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
300

301
302
303
	do {
		bonobo_main();
  	} while(object_count > 0);
Elliot Lee's avatar
Elliot Lee committed
304

305
	return 0;
Elliot Lee's avatar
Elliot Lee committed
306
}