ntl-history-view.c 7.73 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 25 26 27
 *
 *  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>
 *
 */
/* ntl-view.c: Implementation of the object representing a data view,
   and its associated CORBA object for proxying requests into this
   object. */
Elliot Lee's avatar
Elliot Lee committed
28 29 30
#include "config.h"

#include <libnautilus/libnautilus.h>
31 32
#include <libnautilus/nautilus-bookmark.h>
#include <libnautilus/nautilus-icon-factory.h>
33
#include <libgnome/gnome-i18n.h>
34
#include <libgnomevfs/gnome-vfs-init.h>
35
#include <libgnorba/gnorba.h>
Elliot Lee's avatar
Elliot Lee committed
36 37

typedef struct {
38
  NautilusViewFrame *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 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
}


static void
install_icon (GtkCList *clist, const NautilusBookmark *bookmark, gint row)
{
	GdkPixmap *pixmap;
	GdkBitmap *bitmap;

	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);
}

Elliot Lee's avatar
Elliot Lee committed
86
static void
87
hyperbola_navigation_history_notify_location_change (NautilusViewFrame *view,
Elliot Lee's avatar
Elliot Lee committed
88 89 90
						     Nautilus_NavigationInfo *loci,
						     HistoryView *hview)
{
91
  char *cols[HISTORY_VIEW_COLUMN_COUNT];
Elliot Lee's avatar
Elliot Lee committed
92 93
  int new_rownum;
  GtkCList *clist;
94
  NautilusBookmark *bookmark;
95
  int i;
Elliot Lee's avatar
Elliot Lee committed
96 97 98 99

  hview->notify_count++;

  clist = hview->clist;
100
  gtk_clist_freeze(clist);
Elliot Lee's avatar
Elliot Lee committed
101

102
  bookmark = nautilus_bookmark_new (loci->requested_uri, loci->requested_uri);
Elliot Lee's avatar
Elliot Lee committed
103

104 105 106 107 108 109 110 111 112 113 114 115 116
  /* 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
117 118
    }

119 120 121
  cols[HISTORY_VIEW_COLUMN_ICON] = NULL;
  /* Ugh. Gotta cast away the const */
  cols[HISTORY_VIEW_COLUMN_NAME] = (char *)nautilus_bookmark_get_name (bookmark);
122
  new_rownum = gtk_clist_prepend(clist, cols);
123 124 125 126 127 128
  gtk_clist_set_row_data_full (clist,
  			       new_rownum,
  			       bookmark,
  			       (GtkDestroyNotify)gtk_object_unref);
  install_icon (clist, bookmark, new_rownum);

Elliot Lee's avatar
Elliot Lee committed
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149
  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)
{
  Nautilus_NavigationRequestInfo reqi;

  if(hview->notify_count > 0)
    return;

150 151 152
  /* First row is always current location, by definition, so don't activate */
  if (row == 0)
      return;
Elliot Lee's avatar
Elliot Lee committed
153

154
  /* FIXME: There are bugs here if you drag up & down */
Elliot Lee's avatar
Elliot Lee committed
155

156
  gtk_clist_freeze(clist);
Elliot Lee's avatar
Elliot Lee committed
157 158 159 160

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

161 162
  /* FIXME: gotta cast away const because requested_uri isn't defined correctly */
  reqi.requested_uri = (char *)get_uri_from_row (clist, row);
Elliot Lee's avatar
Elliot Lee committed
163 164 165 166

  reqi.new_window_default = reqi.new_window_suggested = Nautilus_V_FALSE;
  reqi.new_window_enforced = Nautilus_V_FALSE;

167
  nautilus_view_frame_request_location_change(hview->view, &reqi);
Elliot Lee's avatar
Elliot Lee committed
168 169 170 171

  gtk_clist_thaw(clist);
}

172 173 174 175 176 177 178 179 180
static int object_count = 0;

static void
do_destroy(GtkObject *obj)
{
  object_count--;
  if(object_count <= 0)
    gtk_main_quit();
}
Elliot Lee's avatar
Elliot Lee committed
181

182 183 184 185 186
static void
menu_setup(BonoboObject *ctl, HistoryView *hview)
{
  Bonobo_UIHandler remote_uih;
  GnomeUIInfo history_menu[] = {
187
    GNOMEUIINFO_MENU_NEW_ITEM("_Do nothing (menu merge test)", "Testing", NULL, NULL),
188 189 190
    GNOMEUIINFO_END
  };

191
  hview->uih = bonobo_control_get_ui_handler(BONOBO_CONTROL(ctl));
192 193 194 195 196 197 198 199 200
  remote_uih = bonobo_control_get_remote_ui_handler(BONOBO_CONTROL(ctl));
  bonobo_ui_handler_set_container(hview->uih, remote_uih);

  bonobo_ui_handler_menu_new_subtree(hview->uih, "/History", _("H_istory"), NULL, -1, BONOBO_UI_HANDLER_PIXMAP_NONE,
                                     NULL, 0, 0);
  bonobo_ui_handler_menu_add_tree(hview->uih, "/History",
                                  bonobo_ui_handler_menu_parse_uiinfo_tree(history_menu));
}

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

  g_return_val_if_fail(!strcmp(goad_id, "ntl_history_view"), NULL);

  hview = g_new0(HistoryView, 1);
211 212
  frame = gtk_widget_new(nautilus_meta_view_frame_get_type(), NULL);
  gtk_signal_connect(GTK_OBJECT(frame), "destroy", do_destroy, NULL);
213
  object_count++;
Elliot Lee's avatar
Elliot Lee committed
214

215
  ctl = nautilus_view_frame_get_bonobo_object(NAUTILUS_VIEW_FRAME(frame));
216 217 218 219 220
  gtk_signal_connect(GTK_OBJECT(ctl), "set_frame", menu_setup, hview);

  /* set description */
  nautilus_meta_view_frame_set_label(NAUTILUS_META_VIEW_FRAME(frame),
                                     _("History"));
Elliot Lee's avatar
Elliot Lee committed
221 222

  /* create interface */
223 224 225 226 227 228 229
  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
230 231 232
  gtk_scrolled_window_set_policy(GTK_SCROLLED_WINDOW(wtmp),
				 GTK_POLICY_AUTOMATIC,
				 GTK_POLICY_AUTOMATIC);
233
  gtk_container_add(GTK_CONTAINER(frame), wtmp);
234
  gtk_container_add(GTK_CONTAINER(wtmp), GTK_WIDGET (clist));
Elliot Lee's avatar
Elliot Lee committed
235

236
  gtk_widget_show_all(frame);
Elliot Lee's avatar
Elliot Lee committed
237 238
  
  /* handle events */
239
  gtk_signal_connect(GTK_OBJECT(frame), "notify_location_change", hyperbola_navigation_history_notify_location_change, 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
  hview->view = (NautilusViewFrame *)frame;
Elliot Lee's avatar
Elliot Lee committed
243 244 245 246 247 248 249
  hview->clist = (GtkCList *)clist;

  return ctl;
}

int main(int argc, char *argv[])
{
250
  BonoboGenericFactory *factory;
Elliot Lee's avatar
Elliot Lee committed
251 252 253 254 255 256 257
  CORBA_ORB orb;
  CORBA_Environment ev;

  CORBA_exception_init(&ev);
  orb = gnome_CORBA_init_with_popt_table("ntl-history-view", VERSION, &argc, argv, NULL, 0, NULL,
					 GNORBA_INIT_SERVER_FUNC, &ev);
  bonobo_init(orb, CORBA_OBJECT_NIL, CORBA_OBJECT_NIL);
258
  gnome_vfs_init ();
Elliot Lee's avatar
Elliot Lee committed
259

260
  factory = bonobo_generic_factory_new_multi("ntl_history_view_factory", make_obj, NULL);
Elliot Lee's avatar
Elliot Lee committed
261

262 263 264
  do {
    bonobo_main();
  } while(object_count > 0);
Elliot Lee's avatar
Elliot Lee committed
265 266 267

  return 0;
}