1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
3 * This program is free software; you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of the
6 * License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
13 * You should have received a copy of the GNU General Public License
14 * along with this program; if not, write to the Free Software
15 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA.
18 * James Willcox <jwillcox@cs.indiana.edu>
29 #include <gconf/gconf-client.h>
30 #include <libbonoboui.h>
31 #include <libgnomevfs/gnome-vfs.h>
32 #include "gnome-recent-model.h"
33 #include "gnome-recent-view.h"
34 #include "gnome-recent-view-bonobo.h"
35 #include "gnome-recent-util.h"
36 #include "gnome-recent-marshal.h"
38 #define GNOME_RECENT_VERB_NAME "-uri-"
40 struct _GnomeRecentViewBonobo {
41 GObject parent_instance; /* We emit signals */
43 BonoboUIComponent *uic;
44 gchar *path; /* The menu path where our stuff
50 GnomeRecentModel *model;
53 struct _GnomeRecentViewBonoboClass {
54 GObjectClass parent_class;
56 void (*activate) (GnomeRecentViewBonobo *view, const gchar *uri);
59 struct _GnomeRecentViewBonoboMenuData {
60 GnomeRecentViewBonobo *view;
64 typedef struct _GnomeRecentViewBonoboMenuData GnomeRecentViewBonoboMenuData;
71 /* GObject properties */
78 static guint gnome_recent_view_bonobo_signals[LAST_SIGNAL] = { 0 };
81 gnome_recent_view_bonobo_clear (GnomeRecentView *view_parent)
86 GnomeRecentModel *model;
87 GnomeRecentViewBonobo *view;
89 g_return_if_fail (view_parent);
90 view = GNOME_RECENT_VIEW_BONOBO (view_parent);
91 g_return_if_fail (view->uic);
93 model = gnome_recent_view_get_model (GNOME_RECENT_VIEW (view));
94 appname = gnome_recent_model_get_appname (model);
98 gchar *verb_name = g_strdup_printf ("%s%s%d", appname,GNOME_RECENT_VERB_NAME, i);
99 gchar *item_path = g_strconcat (view->path, "/", verb_name, NULL);
100 if (bonobo_ui_component_path_exists (view->uic, item_path, NULL))
101 bonobo_ui_component_rm (view->uic, item_path, NULL);
115 gnome_recent_view_bonobo_menu_cb (BonoboUIComponent *uic, gpointer data, const char *cname)
118 GnomeRecentViewBonoboMenuData *md = (GnomeRecentViewBonoboMenuData *) data;
119 GnomeRecentModel *model;
121 g_return_if_fail (md);
122 g_return_if_fail (md->uri);
123 g_return_if_fail (md->view);
124 g_return_if_fail (GNOME_IS_RECENT_VIEW_BONOBO (md->view));
127 g_signal_emit (G_OBJECT(md->view),
128 gnome_recent_view_bonobo_signals[ACTIVATE], 0,
132 model = gnome_recent_view_get_model (GNOME_RECENT_VIEW (md->view));
133 gnome_recent_model_delete (model, md->uri);
138 gnome_recent_view_bonobo_menu_data_destroy_cb (gpointer data, GClosure *closure)
140 GnomeRecentViewBonoboMenuData *md = data;
148 gnome_recent_view_bonobo_set_list (GnomeRecentViewBonobo *view, GSList *list)
150 BonoboUIComponent* ui_component;
153 gchar *verb_name = NULL;
155 gchar *escaped_name = NULL;
156 gchar *item_path = NULL;
160 GnomeRecentViewBonoboMenuData *md;
161 GnomeRecentModel *model;
164 g_return_if_fail (view);
166 ui_component = view->uic;
167 g_return_if_fail (BONOBO_IS_UI_COMPONENT (ui_component));
169 model = gnome_recent_view_get_model (GNOME_RECENT_VIEW (view));
170 appname = gnome_recent_model_get_appname (model);
172 gnome_recent_view_bonobo_clear (GNOME_RECENT_VIEW (view));
174 bonobo_ui_component_freeze (ui_component, NULL);
176 for (i = 1; i <= g_slist_length (list); ++i)
179 /* this is what gets passed to our private "activate" callback */
180 md = (GnomeRecentViewBonoboMenuData *)g_malloc (sizeof (GnomeRecentViewBonoboMenuData));
182 md->uri = g_strdup (g_slist_nth_data (list, i-1));
184 /* Maybe we should use a gnome-vfs call here?? */
185 uri = g_path_get_basename (g_slist_nth_data (list, i - 1));
187 escaped_name = gnome_recent_util_escape_underlines (uri);
189 tip = g_strdup_printf (_("Open %s"), uri);
191 verb_name = g_strdup_printf ("%s%s%d", appname,GNOME_RECENT_VERB_NAME, i);
192 cmd = g_strdup_printf ("<cmd name = \"%s\" /> ", verb_name);
193 bonobo_ui_component_set_translate (ui_component, "/commands/", cmd, NULL);
195 closure = g_cclosure_new (G_CALLBACK (gnome_recent_view_bonobo_menu_cb),
196 md, gnome_recent_view_bonobo_menu_data_destroy_cb);
198 bonobo_ui_component_add_verb_full (ui_component, verb_name,
202 label = g_strdup_printf ("_%d. %s", i, escaped_name);
204 label = g_strdup_printf ("%d. %s", i, escaped_name);
208 item_path = g_strconcat (view->path, "/", verb_name, NULL);
210 if (bonobo_ui_component_path_exists (ui_component, item_path, NULL))
212 bonobo_ui_component_set_prop (ui_component, item_path,
213 "label", label, NULL);
215 bonobo_ui_component_set_prop (ui_component, item_path,
222 xml = g_strdup_printf ("<menuitem name=\"%s\" "
224 " _label=\"%s\" _tip=\"%s\" "
226 verb_name, verb_name, label,
229 bonobo_ui_component_set_translate (ui_component, view->path, xml, NULL);
237 g_free (escaped_name);
244 bonobo_ui_component_thaw (ui_component, NULL);
248 model_changed_cb (GnomeRecentModel *model, GSList *list, GnomeRecentViewBonobo *view)
250 gnome_recent_view_bonobo_set_list (view, list);
256 gnome_recent_view_bonobo_populate (GnomeRecentViewBonobo *view)
258 GnomeRecentModel *model;
261 model = gnome_recent_view_get_model (GNOME_RECENT_VIEW (view));
262 list = gnome_recent_model_get_list (model);
264 gnome_recent_view_bonobo_set_list (view, list);
267 static GnomeRecentModel *
268 gnome_recent_view_bonobo_get_model (GnomeRecentView *view_parent)
270 GnomeRecentViewBonobo *view;
272 g_return_val_if_fail (view_parent, NULL);
273 view = GNOME_RECENT_VIEW_BONOBO (view_parent);
279 gnome_recent_view_bonobo_set_model (GnomeRecentView *view_parent, GnomeRecentModel *model)
281 GnomeRecentViewBonobo *view;
283 g_return_if_fail (view_parent);
284 view = GNOME_RECENT_VIEW_BONOBO (view_parent);
287 g_signal_handler_disconnect (G_OBJECT (view->model),
288 view->changed_cb_id);
291 view->changed_cb_id = g_signal_connect (G_OBJECT (model), "changed",
292 G_CALLBACK (model_changed_cb), view);
294 gnome_recent_view_bonobo_populate (view);
298 gnome_recent_view_bonobo_set_property (GObject *object,
303 GnomeRecentViewBonobo *view = GNOME_RECENT_VIEW_BONOBO (object);
307 case PROP_UI_COMPONENT:
308 gnome_recent_view_bonobo_set_ui_component (GNOME_RECENT_VIEW_BONOBO (view),
309 BONOBO_UI_COMPONENT (g_value_get_object (value)));
312 view->path = g_strdup (g_value_get_string (value));
320 gnome_recent_view_bonobo_get_property (GObject *object,
325 GnomeRecentViewBonobo *view = GNOME_RECENT_VIEW_BONOBO (object);
329 case PROP_UI_COMPONENT:
330 g_value_set_pointer (value, view->uic);
333 g_value_set_string (value, g_strdup (view->path));
336 G_OBJECT_WARN_INVALID_PROPERTY_ID (object, prop_id, pspec);
341 gnome_recent_view_bonobo_class_init (GnomeRecentViewBonoboClass * klass)
343 GObjectClass *object_class;
346 object_class = G_OBJECT_CLASS (klass);
348 object_class->set_property = gnome_recent_view_bonobo_set_property;
349 object_class->get_property = gnome_recent_view_bonobo_get_property;
351 gnome_recent_view_bonobo_signals[ACTIVATE] = g_signal_new ("activate",
352 G_OBJECT_CLASS_TYPE (object_class),
354 G_STRUCT_OFFSET (GnomeRecentViewBonoboClass, activate),
356 gnome_recent_BOOLEAN__STRING,
360 g_object_class_install_property (object_class,
362 g_param_spec_object ("ui-component",
364 "BonoboUIComponent for menus.",
365 bonobo_ui_component_get_type(),
368 g_object_class_install_property (object_class,
370 g_param_spec_string ("ui-path",
372 "The path to put the menu items.",
373 "/menus/File/GnomeRecentDocuments",
377 klass->activate = NULL;
381 gnome_recent_view_init (GnomeRecentViewClass *iface)
383 iface->do_clear = gnome_recent_view_bonobo_clear;
384 iface->do_get_model = gnome_recent_view_bonobo_get_model;
385 iface->do_set_model = gnome_recent_view_bonobo_set_model;
390 gnome_recent_view_bonobo_init (GnomeRecentViewBonobo * recent)
392 /* maybe should remove this */
396 gnome_recent_view_bonobo_set_ui_component (GnomeRecentViewBonobo *view, BonoboUIComponent *uic)
398 g_return_if_fail (view);
399 g_return_if_fail (uic);
405 gnome_recent_view_bonobo_set_ui_path (GnomeRecentViewBonobo *view, const gchar *path)
407 g_return_if_fail (view);
408 g_return_if_fail (path);
410 view->path = g_strdup (path);
414 gnome_recent_view_bonobo_get_ui_component (GnomeRecentViewBonobo *view)
416 g_return_val_if_fail (view, NULL);
422 gnome_recent_view_bonobo_get_ui_path (GnomeRecentViewBonobo *view)
424 g_return_val_if_fail (view, NULL);
426 return g_strdup (view->path);
430 * gnome_recent_view_bonobo_new:
431 * @appname: The name of your application.
432 * @limit: The maximum number of items allowed.
434 * This creates a new GnomeRecentViewBonobo object.
436 * Returns: a GnomeRecentViewBonobo object
438 GnomeRecentViewBonobo *
439 gnome_recent_view_bonobo_new (BonoboUIComponent *uic, const gchar *path)
441 GnomeRecentViewBonobo *view;
443 g_return_val_if_fail (uic, NULL);
444 g_return_val_if_fail (path, NULL);
446 view = GNOME_RECENT_VIEW_BONOBO (g_object_new (gnome_recent_view_bonobo_get_type (),
452 g_return_val_if_fail (view, NULL);
458 * gnome_recent_view_bonobo_get_type:
461 * This returns a GType representing a GnomeRecentViewBonobo object.
466 gnome_recent_view_bonobo_get_type (void)
468 static GType gnome_recent_view_bonobo_type = 0;
470 if(!gnome_recent_view_bonobo_type) {
471 static const GTypeInfo gnome_recent_view_bonobo_info = {
472 sizeof (GnomeRecentViewBonoboClass),
473 NULL, /* base init */
474 NULL, /* base finalize */
475 (GClassInitFunc)gnome_recent_view_bonobo_class_init, /* class init */
476 NULL, /* class finalize */
477 NULL, /* class data */
478 sizeof (GnomeRecentViewBonobo),
480 (GInstanceInitFunc) gnome_recent_view_bonobo_init
483 static const GInterfaceInfo view_info =
485 (GInterfaceInitFunc) gnome_recent_view_init,
490 gnome_recent_view_bonobo_type = g_type_register_static (G_TYPE_OBJECT,
491 "GnomeRecentViewBonobo",
492 &gnome_recent_view_bonobo_info, 0);
493 g_type_add_interface_static (gnome_recent_view_bonobo_type,
494 GNOME_TYPE_RECENT_VIEW,
498 return gnome_recent_view_bonobo_type;