/*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2 of the License, or (at your option) version 3.
*
* This program 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with the program; if not, see
*
*
* Authors:
* Ettore Perazzoli
*
* Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
*
*/
#ifdef HAVE_CONFIG_H
#include
#endif
#include "e-user-creatable-items-handler.h"
#include
#include "Evolution.h"
#include "e-util/e-corba-utils.h"
#include "misc/e-combo-button.h"
#include
#include
#include
#include
#include
#include
#include
#include
#include
struct _Component {
char *id, *alias;
GNOME_Evolution_Component component;
GNOME_Evolution_CreatableItemTypeList *type_list;
};
typedef struct _Component Component;
/* Representation of a single menu item. */
struct _MenuItem {
const char *label;
char shortcut;
char *verb;
char *tooltip;
char *component;
GdkPixbuf *icon;
GdkPixbuf *icon_toolbar;
};
typedef struct _MenuItem MenuItem;
struct _EUserCreatableItemsHandlerPrivate {
/* This component's alias */
char *this_component;
/* For creating items on the view */
EUserCreatableItemsHandlerCreate create_local;
void *create_data;
/* The components that register user creatable items. */
GSList *components; /* Component */
/* The "New ..." menu items. */
GSList *objects; /* MenuItem */
GSList *folders; /* MenuItem */
/* The default item (the mailer's "message" item). To be used when the
component in the view we are in doesn't provide a default user
creatable type. This pointer always points to one of the menu items
in ->objects. */
const MenuItem *fallback_menu_item;
const MenuItem *default_menu_item;
char *menu_xml;
GtkWidget *new_button, *new_menu;
BonoboControl *new_control;
GtkAccelGroup *accel_group;
};
enum {
PROP_THIS_COMPONENT = 1,
LAST_PROP
};
G_DEFINE_TYPE (EUserCreatableItemsHandler, e_user_creatable_items_handler, G_TYPE_OBJECT)
/* Component struct handling. */
static Component *
component_new (const char *id,
const char *component_alias,
GNOME_Evolution_Component component)
{
CORBA_Environment ev;
Component *new;
CORBA_exception_init (&ev);
new = g_new (Component, 1);
new->id = g_strdup (id);
new->alias = g_strdup (component_alias);
new->type_list = GNOME_Evolution_Component__get_userCreatableItems (component, &ev);
if (BONOBO_EX (&ev))
new->type_list = NULL;
new->component = component;
Bonobo_Unknown_ref (new->component, &ev);
if (ev._major != CORBA_NO_EXCEPTION)
new->type_list = NULL;
CORBA_exception_free (&ev);
return new;
}
static void
component_free (Component *component)
{
CORBA_Environment ev;
CORBA_exception_init (&ev);
Bonobo_Unknown_unref (component->component, &ev);
g_free (component->id);
g_free (component->alias);
if (component->type_list != NULL)
CORBA_free (component->type_list);
CORBA_exception_free (&ev);
g_free (component);
}
static const char *component_query =
"repo_ids.has ('IDL:GNOME/Evolution/Component:" BASE_VERSION "')";
static void
get_components_from_bonobo (EUserCreatableItemsHandler *handler)
{
Bonobo_ServerInfoList *info_list;
Bonobo_ActivationProperty *property;
CORBA_Environment ev;
char *iid, *alias;
GNOME_Evolution_Component corba_component;
Component *component;
int i;
CORBA_exception_init (&ev);
info_list = bonobo_activation_query (component_query, NULL, &ev);
if (BONOBO_EX (&ev)) {
char *ex_text = bonobo_exception_get_text (&ev);
g_warning ("Cannot query for components: %s\n", ex_text);
g_free (ex_text);
CORBA_exception_free (&ev);
return;
}
for (i = 0; i < info_list->_length; i++) {
iid = info_list->_buffer[i].iid;
corba_component = bonobo_activation_activate_from_id (iid, Bonobo_ACTIVATION_FLAG_EXISTING_ONLY, NULL, &ev);
if (BONOBO_EX (&ev)) {
CORBA_exception_free (&ev);
continue;
}
property = bonobo_server_info_prop_find (&info_list->_buffer[i],
"evolution:component_alias");
alias = property ? property->v._u.value_string : "unknown";
component = component_new (iid, alias, corba_component);
handler->priv->components = g_slist_prepend (handler->priv->components, component);
}
CORBA_free (info_list);
}
/* Helper functions. */
static gboolean
item_is_default (const MenuItem *item,
const char *component)
{
if (component == NULL)
return FALSE;
if (strcmp (item->component, component) == 0)
return TRUE;
else
return FALSE;
}
static char *
create_verb (EUserCreatableItemsHandler *handler, int component_num, const char *comp, const char *type_id)
{
return g_strdup_printf ("EUserCreatableItemsHandler-%s:%d:%s", comp, component_num, type_id);
}
/* Setting up menu items for the "File -> New" submenu and the "New" toolbar
button. */
static void
ensure_menu_items (EUserCreatableItemsHandler *handler)
{
EUserCreatableItemsHandlerPrivate *priv;
GSList *objects, *folders;
GSList *p;
int component_num;
const char *default_verb;
priv = handler->priv;
if (priv->objects != NULL)
return;
objects = folders = NULL;
component_num = 0;
default_verb = NULL;
for (p = priv->components; p != NULL; p = p->next) {
const Component *component;
int i;
component = (const Component *) p->data;
if (component->type_list != NULL) {
for (i = 0; i < component->type_list->_length; i ++) {
const GNOME_Evolution_CreatableItemType *corba_item;
MenuItem *item;
corba_item = (const GNOME_Evolution_CreatableItemType *) component->type_list->_buffer + i;
item = g_new (MenuItem, 1);
item->label = corba_item->menuDescription;
item->shortcut = corba_item->menuShortcut;
item->verb = create_verb (handler, component_num, component->alias, corba_item->id);
item->tooltip = corba_item->tooltip;
item->component = g_strdup (component->alias);
if (strcmp (item->component, "mail") == 0
&& strcmp (corba_item->id, "message") == 0)
default_verb = item->verb;
if (corba_item->iconName == NULL || *corba_item->iconName == '\0') {
item->icon = NULL;
item->icon_toolbar = NULL;
} else {
item->icon = e_icon_factory_get_icon (corba_item->iconName, E_ICON_SIZE_MENU);
if (item_is_default (item, component->alias))
item->icon_toolbar = e_icon_factory_get_icon (corba_item->iconName, E_ICON_SIZE_LARGE_TOOLBAR);
else
item->icon_toolbar = NULL;
}
if (corba_item->type == GNOME_Evolution_CREATABLE_OBJECT)
objects = g_slist_prepend (objects, item);
else
folders = g_slist_prepend (folders, item);
}
}
component_num ++;
}
priv->objects = g_slist_reverse (objects);
priv->folders = g_slist_reverse (folders);
priv->fallback_menu_item = NULL;
if (default_verb != NULL) {
for (p = priv->objects; p != NULL; p = p->next) {
const MenuItem *item;
item = (const MenuItem *) p->data;
if (strcmp (item->verb, default_verb) == 0)
priv->fallback_menu_item = item;
}
}
}
static void
free_menu_items (GSList *menu_items)
{
GSList *p;
if (menu_items == NULL)
return;
for (p = menu_items; p != NULL; p = p->next) {
MenuItem *item;
item = (MenuItem *) p->data;
g_free (item->verb);
if (item->icon != NULL)
g_object_unref (item->icon);
if (item->icon_toolbar != NULL)
g_object_unref (item->icon_toolbar);
g_free (item->component);
g_free (item);
}
g_slist_free (menu_items);
}
static const MenuItem *
get_default_action_for_view (EUserCreatableItemsHandler *handler)
{
EUserCreatableItemsHandlerPrivate *priv;
const GSList *p;
priv = handler->priv;
for (p = priv->objects; p != NULL; p = p->next) {
const MenuItem *item;
item = (const MenuItem *) p->data;
if (item_is_default (item, priv->this_component))
return item;
}
return priv->fallback_menu_item;
}
/* Verb handling. */
static void
execute_verb (EUserCreatableItemsHandler *handler,
const char *verb_name)
{
EUserCreatableItemsHandlerPrivate *priv;
const Component *component;
int component_number;
const char *p;
const char *id;
GSList *component_list_item;
int i;
priv = handler->priv;
p = strchr (verb_name, ':');
g_return_if_fail (p != NULL);
component_number = atoi (p + 1);
p = strchr (p + 1, ':');
g_return_if_fail (p != NULL);
id = p + 1;
component_list_item = g_slist_nth (priv->components, component_number);
g_return_if_fail (component_list_item != NULL);
component = (const Component *) component_list_item->data;
if (component->type_list == NULL)
return;
/* TODO: why do we actually iterate this? Is it just to check we have it in the menu? The
search isn't used otherwise */
for (i = 0; i < component->type_list->_length; i ++) {
if (strcmp (component->type_list->_buffer[i].id, id) == 0) {
if (priv->create_local && priv->this_component && strcmp(priv->this_component, component->alias) == 0) {
priv->create_local(handler, id, priv->create_data);
} else {
CORBA_Environment ev;
CORBA_exception_init (&ev);
GNOME_Evolution_Component_requestCreateItem (component->component, id, &ev);
if (ev._major != CORBA_NO_EXCEPTION)
g_warning ("Error in requestCreateItem -- %s", BONOBO_EX_REPOID (&ev));
CORBA_exception_free (&ev);
}
return;
}
}
}
static void
verb_fn (BonoboUIComponent *ui_component,
void *data,
const char *verb_name)
{
EUserCreatableItemsHandler *handler=
E_USER_CREATABLE_ITEMS_HANDLER (data);
execute_verb (handler, verb_name);
}
static void
add_verbs (EUserCreatableItemsHandler *handler,
BonoboUIComponent *ui_component)
{
EUserCreatableItemsHandlerPrivate *priv;
int component_num;
GSList *p;
priv = handler->priv;
component_num = 0;
for (p = priv->components; p != NULL; p = p->next) {
const Component *component;
int i;
component = (const Component *) p->data;
if (component->type_list != NULL) {
for (i = 0; i < component->type_list->_length; i ++) {
char *verb_name;
verb_name = create_verb (handler,
component_num,
component->alias,
component->type_list->_buffer[i].id);
bonobo_ui_component_add_verb (ui_component, verb_name, verb_fn, handler);
g_free (verb_name);
}
}
component_num ++;
}
}
/* Generic menu construction code */
static int
item_types_sort_func (const void *a,
const void *b)
{
const MenuItem *item_a;
const MenuItem *item_b;
const char *p1, *p2;
item_a = (const MenuItem *) a;
item_b = (const MenuItem *) b;
p1 = item_a->label;
p2 = item_b->label;
while (*p1 != '\0' && *p2 != '\0') {
if (*p1 == '_') {
p1 ++;
continue;
}
if (*p2 == '_') {
p2 ++;
continue;
}
if (toupper ((int) *p1) < toupper ((int) *p2))
return -1;
else if (toupper ((int) *p1) > toupper ((int) *p2))
return +1;
p1 ++, p2 ++;
}
if (*p1 == '\0') {
if (*p2 == '\0')
return 0;
else
return -1;
} else {
return +1;
}
}
typedef void (*EUserCreatableItemsHandlerMenuItemFunc) (EUserCreatableItemsHandler *, gpointer, MenuItem *, gboolean);
typedef void (*EUserCreatableItemsHandlerSeparatorFunc) (EUserCreatableItemsHandler *, gpointer, int);
static void
construct_menu (EUserCreatableItemsHandler *handler, gpointer menu,
EUserCreatableItemsHandlerMenuItemFunc menu_item_func,
EUserCreatableItemsHandlerSeparatorFunc separator_func)
{
EUserCreatableItemsHandlerPrivate *priv;
MenuItem *item;
GSList *p, *items;
gboolean first = TRUE;
priv = handler->priv;
/* First add the current component's creatable objects */
for (p = priv->objects; p != NULL; p = p->next) {
item = p->data;
if (item_is_default (item, priv->this_component)) {
menu_item_func (handler, menu, item, first);
first = FALSE;
}
}
/* Then its creatable folders */
for (p = priv->folders; p != NULL; p = p->next) {
item = p->data;
if (item_is_default (item, priv->this_component))
menu_item_func (handler, menu, item, FALSE);
}
/* Then a separator */
separator_func (handler, menu, 1);
/* Then the objects from other components. */
items = NULL;
for (p = priv->objects; p != NULL; p = p->next) {
item = p->data;
if (! item_is_default (item, priv->this_component))
items = g_slist_prepend (items, item);
}
items = g_slist_sort (items, item_types_sort_func);
for (p = items; p != NULL; p = p->next)
menu_item_func (handler, menu, p->data, FALSE);
g_slist_free (items);
/* Another separator */
separator_func (handler, menu, 2);
/* And finally the folders from other components */
items = NULL;
for (p = priv->folders; p != NULL; p = p->next) {
item = p->data;
if (! item_is_default (item, priv->this_component))
items = g_slist_prepend (items, item);
}
items = g_slist_sort (items, item_types_sort_func);
for (p = items; p != NULL; p = p->next)
menu_item_func (handler, menu, p->data, FALSE);
g_slist_free (items);
}
/* The XML description for "File -> New". */
static void
xml_menu_item_func (EUserCreatableItemsHandler *handler, gpointer menu,
MenuItem *item, gboolean first)
{
GString *xml = menu;
char *encoded_label;
char *encoded_tooltip;
encoded_label = bonobo_ui_util_encode_str (item->label);
g_string_append_printf (xml, "