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

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

typedef struct {
38
  NautilusView *view;
Elliot Lee's avatar
Elliot Lee committed
39
40
41

  GtkCList *clist;

42
  gint notify_count;
43

44
  BonoboUIHandler *uih;
Elliot Lee's avatar
Elliot Lee committed
45
46
} HistoryView;

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

51
52
53
54
55
56
57
static const NautilusBookmark *
get_bookmark_from_row (GtkCList *clist, int row)
{
  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 const char *
get_uri_from_row (GtkCList *clist, int row)
{
61
  return nautilus_bookmark_get_uri (get_bookmark_from_row (clist, row));
62
63
64
65
}


static void
66
install_icon (GtkCList *clist, gint row)
67
68
69
{
	GdkPixmap *pixmap;
	GdkBitmap *bitmap;
70
	const NautilusBookmark *bookmark;
71

72
	bookmark = get_bookmark_from_row (clist, row);
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
	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);
}

88
static void
89
history_view_update_icons (GtkCList *clist)
90
91
92
93
94
95
96
97
98
{
  int row;

  for (row = 0; row < clist->rows; ++row) 
    {
      install_icon (clist, row);
    }
}

Elliot Lee's avatar
Elliot Lee committed
99
static void
100
101
102
hyperbola_navigation_history_load_location (NautilusView *view,
                                            const char *location,
                                            HistoryView *hview)
Elliot Lee's avatar
Elliot Lee committed
103
{
104
  char *cols[HISTORY_VIEW_COLUMN_COUNT];
Elliot Lee's avatar
Elliot Lee committed
105
106
  int new_rownum;
  GtkCList *clist;
107
  NautilusBookmark *bookmark;
108
  int i;
109
110
  GnomeVFSURI *vfs_uri;
  char *short_name;
Elliot Lee's avatar
Elliot Lee committed
111
112
113
114

  hview->notify_count++;

  clist = hview->clist;
115
  gtk_clist_freeze(clist);
Elliot Lee's avatar
Elliot Lee committed
116

117
118
119
120
121
  /* FIXME bugzilla.eazel.com 206: 
   * Get the bookmark info from the Nautilus window instead of
   * keeping a parallel mechanism here. That will get us the right
   * short name for different locations.
   */
122
  vfs_uri = gnome_vfs_uri_new (location);
123
  if (vfs_uri == NULL) {
124
    short_name = g_strdup (location);
125
126
127
128
  } else {
    short_name = gnome_vfs_uri_extract_short_name (vfs_uri);
    gnome_vfs_uri_unref (vfs_uri);
  }
129
  bookmark = nautilus_bookmark_new (location, short_name);
130
131
132
  g_free (short_name);

  
Elliot Lee's avatar
Elliot Lee committed
133

134
135
136
137
138
139
140
141
142
143
144
145
146
  /* If a bookmark for this location was already in list, remove it
   * (no duplicates in list, new one goes at top)
   */
  for (i = 0; i < clist->rows; ++i)
    {
      if (nautilus_bookmark_compare_with (get_bookmark_from_row (clist, i), 
      					  bookmark)
      		== 0)
        {
          gtk_clist_remove (clist, i);
          /* Since we check with each insertion, no need to check further */
          break;
        }
Elliot Lee's avatar
Elliot Lee committed
147
148
    }

149
150
151
  cols[HISTORY_VIEW_COLUMN_ICON] = NULL;
  /* Ugh. Gotta cast away the const */
  cols[HISTORY_VIEW_COLUMN_NAME] = (char *)nautilus_bookmark_get_name (bookmark);
152
  new_rownum = gtk_clist_prepend(clist, cols);
153
154
155
156
  gtk_clist_set_row_data_full (clist,
  			       new_rownum,
  			       bookmark,
  			       (GtkDestroyNotify)gtk_object_unref);
157
  install_icon (clist, new_rownum);
158

Elliot Lee's avatar
Elliot Lee committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  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);

  gtk_clist_select_row(clist, new_rownum, 0);

  gtk_clist_thaw(clist);

  hview->notify_count--;
}

static void
hyperbola_navigation_history_select_row(GtkCList *clist, gint row, gint column, GdkEvent *event,
					HistoryView *hview)
{
  if(hview->notify_count > 0)
    return;

178
179
180
  /* First row is always current location, by definition, so don't activate */
  if (row == 0)
      return;
Elliot Lee's avatar
Elliot Lee committed
181

182
  /* FIXME bugzilla.eazel.com 702: There are bugs here if you drag up & down */
Elliot Lee's avatar
Elliot Lee committed
183

184
  gtk_clist_freeze(clist);
Elliot Lee's avatar
Elliot Lee committed
185
186
187
188

  if(gtk_clist_row_is_visible(clist, row) != GTK_VISIBILITY_FULL)
    gtk_clist_moveto(clist, row, -1, 0.5, 0.0);

189
  nautilus_view_open_location (hview->view, get_uri_from_row (clist, row));
Elliot Lee's avatar
Elliot Lee committed
190
191
192
193

  gtk_clist_thaw(clist);
}

194
195
196
static int object_count = 0;

static void
197
do_destroy(GtkObject *obj, HistoryView *hview)
198
199
200
201
202
{
  object_count--;
  if(object_count <= 0)
    gtk_main_quit();
}
Elliot Lee's avatar
Elliot Lee committed
203

204
205
static BonoboObject *
make_obj(BonoboGenericFactory *Factory, const char *goad_id, gpointer closure)
Elliot Lee's avatar
Elliot Lee committed
206
{
207
  GtkWidget *wtmp;
208
  GtkCList *clist;
Elliot Lee's avatar
Elliot Lee committed
209
210
  HistoryView *hview;

211
  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
212
213
214
215

  hview = g_new0(HistoryView, 1);

  /* create interface */
216
217
218
219
220
221
222
  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);
  gtk_clist_set_selection_mode(clist, GTK_SELECTION_BROWSE);
  gtk_clist_columns_autosize(clist);
  wtmp = gtk_scrolled_window_new(gtk_clist_get_hadjustment(clist),
				 gtk_clist_get_vadjustment(clist));
Elliot Lee's avatar
Elliot Lee committed
223
224
225
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(wtmp),
				 GTK_POLICY_AUTOMATIC,
				 GTK_POLICY_AUTOMATIC);
226
  gtk_container_add(GTK_CONTAINER(wtmp), GTK_WIDGET (clist));
Elliot Lee's avatar
Elliot Lee committed
227

228
229
230
  gtk_widget_show_all(wtmp);

  /* create object */
Darin Adler's avatar
Darin Adler committed
231
  hview->view = nautilus_view_new (wtmp);
232
233
234
  gtk_signal_connect (GTK_OBJECT (hview->view), "destroy", do_destroy, hview);
  object_count++;

235
236
  hview->clist = (GtkCList *)clist;

Elliot Lee's avatar
Elliot Lee committed
237
  /* handle events */
238
239
  gtk_signal_connect(GTK_OBJECT(hview->view), "load_location",
                     hyperbola_navigation_history_load_location, hview);
Elliot Lee's avatar
Elliot Lee committed
240
  gtk_signal_connect(GTK_OBJECT(clist), "select_row", hyperbola_navigation_history_select_row, hview);
Elliot Lee's avatar
Elliot Lee committed
241

242
  gtk_signal_connect_object_while_alive (nautilus_icon_factory_get (),
243
					 "icons_changed",
244
					 history_view_update_icons,
245
					 GTK_OBJECT (hview->clist));
246

Elliot Lee's avatar
Elliot Lee committed
247

248
  return BONOBO_OBJECT (hview->view);
Elliot Lee's avatar
Elliot Lee committed
249
250
251
252
}

int main(int argc, char *argv[])
{
253
  BonoboGenericFactory *factory;
Elliot Lee's avatar
Elliot Lee committed
254
255
  CORBA_ORB orb;

256
  gnome_init_with_popt_table("nautilus-history-view", VERSION, 
257
258
259
                             argc, argv,
                             oaf_popt_options, 0, NULL); 
  orb = oaf_init (argc, argv);
260

Elliot Lee's avatar
Elliot Lee committed
261
  bonobo_init(orb, CORBA_OBJECT_NIL, CORBA_OBJECT_NIL);
262
  gnome_vfs_init ();
Elliot Lee's avatar
Elliot Lee committed
263

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

266
267
268
  do {
    bonobo_main();
  } while(object_count > 0);
Elliot Lee's avatar
Elliot Lee committed
269
270
271

  return 0;
}