1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
|
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-shell-user-creatable-items-handler.c
*
* Copyright (C) 2001 Ximian, Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* 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
* General Public License for more details.
*
* You should have received a copy of the GNU General Public
* License along with this program; if not, write to the
* Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*
* Author: Ettore Perazzoli <ettore@ximian.com>
*/
#ifdef HAVE_CONFIG_H
#include <config.h>
#endif
#include "e-shell-user-creatable-items-handler.h"
#include "e-shell-corba-icon-utils.h"
#include "widgets/misc/e-combo-button.h"
#include "e-util/e-corba-utils.h"
#include <gal/util/e-util.h>
#include <bonobo/bonobo-ui-util.h>
#include <libgnome/gnome-i18n.h>
#include <gtk/gtksignal.h>
#include <stdlib.h>
#include <ctype.h>
#define PARENT_TYPE gtk_object_get_type ()
static GtkObjectClass *parent_class = NULL;
#define VERB_PREFIX "ShellUserCreatableItemVerb"
#define SHELL_VIEW_KEY "EShellUserCreatableItemsHandler:shell_view"
#define COMBO_BUTTON_WIDGET_KEY "EShellUserCreatableItemsHandler:combo_button"
struct _Component {
EvolutionShellComponentClient *component_client;
GNOME_Evolution_UserCreatableItemTypeList *type_list;
};
typedef struct _Component Component;
struct _MenuItem {
const char *label;
char shortcut;
char *verb;
GdkPixbuf *icon;
};
typedef struct _MenuItem MenuItem;
struct _EShellUserCreatableItemsHandlerPrivate {
GSList *components; /* Component */
GSList *menu_items; /* MenuItem */
char *menu_xml;
};
/* Component struct handling. */
static Component *
component_new (const char *id,
EvolutionShellComponentClient *client)
{
CORBA_Environment ev;
Component *new;
GNOME_Evolution_ShellComponent objref;
new = g_new (Component, 1);
new->component_client = client;
gtk_object_ref (GTK_OBJECT (client));
CORBA_exception_init (&ev);
objref = bonobo_object_corba_objref (BONOBO_OBJECT (client));
new->type_list = GNOME_Evolution_ShellComponent__get_userCreatableItemTypes (objref, &ev);
if (ev._major != CORBA_NO_EXCEPTION)
new->type_list = NULL;
CORBA_exception_free (&ev);
return new;
}
static void
component_free (Component *component)
{
gtk_object_unref (GTK_OBJECT (component->component_client));
if (component->type_list != NULL)
CORBA_free (component->type_list);
g_free (component);
}
/* Helper functions. */
static char *
create_verb_from_component_number_and_type_id (int component_num,
const char *type_id)
{
return g_strdup_printf (VERB_PREFIX ":%d:%s", component_num, type_id);
}
/* Setting up menu items for the "File -> New" submenu and the "New" toolbar
button. */
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;
}
}
static void
ensure_menu_items (EShellUserCreatableItemsHandler *handler)
{
EShellUserCreatableItemsHandlerPrivate *priv;
GSList *menu_items;
GSList *p;
int component_num;
priv = handler->priv;
if (priv->menu_items != NULL)
return;
menu_items = NULL;
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 ++) {
const GNOME_Evolution_UserCreatableItemType *type;
MenuItem *item;
type = (const GNOME_Evolution_UserCreatableItemType *) component->type_list->_buffer + i;
item = g_new (MenuItem, 1);
item->label = type->menuDescription;
item->shortcut = type->menuShortcut;
item->verb = create_verb_from_component_number_and_type_id (component_num, type->id);
if (type->icon.width == 0 || type->icon.height == 0)
item->icon = NULL;
else
item->icon = e_new_gdk_pixbuf_from_corba_icon (& type->icon, 16, 16);
menu_items = g_slist_prepend (menu_items, item);
}
}
component_num ++;
}
if (menu_items == NULL)
priv->menu_items = NULL;
else
priv->menu_items = g_slist_sort (menu_items, item_types_sort_func);
}
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)
gdk_pixbuf_unref (item->icon);
g_free (item);
}
g_slist_free (menu_items);
}
static const MenuItem *
find_menu_item_for_verb (EShellUserCreatableItemsHandler *handler,
const char *verb)
{
EShellUserCreatableItemsHandlerPrivate *priv;
GSList *p;
priv = handler->priv;
for (p = priv->menu_items; p != NULL; p = p->next) {
const MenuItem *item;
item = (const MenuItem *) p->data;
if (strcmp (item->verb, verb) == 0)
return item;
}
return NULL;
}
static const MenuItem *
get_default_action_for_view (EShellUserCreatableItemsHandler *handler,
EShellView *shell_view)
{
EShellUserCreatableItemsHandlerPrivate *priv;
const char *view_component_id;
const GSList *p;
int component_num;
priv = handler->priv;
/* FIXME-1.2: This should be based on the folder type not the component
that handles it. For this, we are going to have to make the IDL a
little more complex. Also, this is a pretty brutal and ugly hack. */
view_component_id = e_shell_view_get_current_component_id (shell_view);
for (p = priv->components, component_num = 0; p != NULL; p = p->next, component_num ++) {
const Component *component;
const GNOME_Evolution_UserCreatableItemType *type;
const char *component_id;
component = (const Component *) p->data;
type = & component->type_list->_buffer[0];
component_id = evolution_shell_component_client_get_id (component->component_client);
if (strcmp (component_id, view_component_id) == 0) {
const MenuItem *item;
char *verb;
verb = create_verb_from_component_number_and_type_id (component_num, type->id);
item = find_menu_item_for_verb (handler, verb);
g_free (verb);
return item;
}
}
return NULL;
}
/* The XML description for "File -> New". */
static void
ensure_menu_xml (EShellUserCreatableItemsHandler *handler)
{
EShellUserCreatableItemsHandlerPrivate *priv;
GString *xml;
GSList *p;
priv = handler->priv;
if (priv->menu_xml != NULL)
return;
ensure_menu_items (handler);
xml = g_string_new ("");
g_string_append (xml, "<placeholder name=\"NewItems\">");
for (p = priv->menu_items; p != NULL; p = p->next) {
const MenuItem *item;
char *encoded_label;
item = (const MenuItem *) p->data;
encoded_label = bonobo_ui_util_encode_str (item->label);
g_string_sprintfa (xml, "<menuitem name=\"New:%s\" verb=\"%s\" label=\"%s\"",
item->verb, item->verb, encoded_label);
if (item->shortcut != '\0')
g_string_sprintfa (xml, " accel=\"*Control**Shift*%c\"", item->shortcut);
if (item->icon != NULL)
g_string_sprintfa (xml, " pixtype=\"pixbuf\" pixname=\"%s\"",
bonobo_ui_util_pixbuf_to_xml (item->icon));
g_string_append (xml, "/> ");
g_free (encoded_label);
}
g_string_append (xml, "</placeholder>");
priv->menu_xml = xml->str;
g_string_free (xml, FALSE);
}
/* Verb handling. */
static void
execute_verb (EShellUserCreatableItemsHandler *handler,
EShellView *shell_view,
const char *verb_name)
{
EShellUserCreatableItemsHandlerPrivate *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_assert (p != NULL);
component_number = atoi (p + 1);
p = strchr (p + 1, ':');
g_assert (p != NULL);
id = p + 1;
component_list_item = g_slist_nth (priv->components, component_number);
g_assert (component_list_item != NULL);
component = (const Component *) component_list_item->data;
if (component->type_list == NULL)
return;
for (i = 0; i < component->type_list->_length; i ++) {
if (strcmp (component->type_list->_buffer[i].id, id) == 0) {
CORBA_Environment ev;
CORBA_exception_init (&ev);
GNOME_Evolution_ShellComponent_userCreateNewItem
(bonobo_object_corba_objref (BONOBO_OBJECT (component->component_client)),
id,
e_safe_corba_string (e_shell_view_get_current_physical_uri (shell_view)),
e_safe_corba_string (e_shell_view_get_current_folder_type (shell_view)),
&ev);
if (ev._major != CORBA_NO_EXCEPTION)
g_warning ("Error in userCreateNewItem -- %s", ev._repo_id);
CORBA_exception_free (&ev);
return;
}
}
}
static void
verb_fn (BonoboUIComponent *ui_component,
void *data,
const char *verb_name)
{
EShellUserCreatableItemsHandler *handler;
EShellView *shell_view;
shell_view = gtk_object_get_data (GTK_OBJECT (ui_component), SHELL_VIEW_KEY);
g_assert (E_IS_SHELL_VIEW (shell_view));
handler = E_SHELL_USER_CREATABLE_ITEMS_HANDLER (data);
execute_verb (handler, shell_view, verb_name);
}
static void
add_verbs (EShellUserCreatableItemsHandler *handler,
EShellView *shell_view)
{
EShellUserCreatableItemsHandlerPrivate *priv;
BonoboUIComponent *ui_component;
int component_num;
GSList *p;
priv = handler->priv;
ui_component = e_shell_view_get_bonobo_ui_component (shell_view);
gtk_object_set_data (GTK_OBJECT (ui_component), SHELL_VIEW_KEY, shell_view);
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_from_component_number_and_type_id
(component_num, component->type_list->_buffer[i].id);
bonobo_ui_component_add_verb (ui_component, verb_name, verb_fn, handler);
g_free (verb_name);
}
}
component_num ++;
}
}
/* The "New" button in the toolbar. */
static void
combo_button_activate_default_callback (EComboButton *combo_button,
void *data)
{
EShellView *shell_view;
EShellUserCreatableItemsHandler *handler;
const MenuItem *menu_item;
shell_view = E_SHELL_VIEW (data);
handler = e_shell_get_user_creatable_items_handler (e_shell_view_get_shell (shell_view));
menu_item = get_default_action_for_view (handler, shell_view);
execute_verb (handler, shell_view, menu_item->verb);
}
static void
setup_toolbar_button (EShellUserCreatableItemsHandler *handler,
EShellView *shell_view)
{
EShellUserCreatableItemsHandlerPrivate *priv;
BonoboUIComponent *ui_component;
GtkWidget *combo_button;
GtkWidget *menu;
BonoboControl *control;
priv = handler->priv;
menu = gtk_menu_new ();
combo_button = e_combo_button_new ();
e_combo_button_set_menu (E_COMBO_BUTTON (combo_button), GTK_MENU (menu));
e_combo_button_set_label (E_COMBO_BUTTON (combo_button), _("New"));
gtk_widget_show (combo_button);
gtk_signal_connect (GTK_OBJECT (combo_button), "activate_default",
GTK_SIGNAL_FUNC (combo_button_activate_default_callback),
shell_view);
ui_component = e_shell_view_get_bonobo_ui_component (shell_view);
bonobo_window_add_popup (BONOBO_WINDOW (shell_view), GTK_MENU (menu), "/popups/NewPopup");
control = bonobo_control_new (combo_button);
bonobo_ui_component_object_set (ui_component, "/Toolbar/NewComboButton",
BONOBO_OBJREF (control), NULL);
gtk_object_set_data (GTK_OBJECT (shell_view), COMBO_BUTTON_WIDGET_KEY, combo_button);
}
/* This handles the menus for a given EShellView. We have to rebuild the menu
and set the toolbar button every time the view changes, and clean up when
the view is destroyed. */
static void
shell_view_view_changed_callback (EShellView *shell_view,
const char *evolution_path,
const char *physical_uri,
const char *folder_type,
const char *component_id,
void *data)
{
EShellUserCreatableItemsHandler *handler;
EShellUserCreatableItemsHandlerPrivate *priv;
GtkWidget *combo_button_widget;
const MenuItem *default_menu_item;
handler = E_SHELL_USER_CREATABLE_ITEMS_HANDLER (data);
priv = handler->priv;
combo_button_widget = gtk_object_get_data (GTK_OBJECT (shell_view), COMBO_BUTTON_WIDGET_KEY);
g_assert (E_IS_COMBO_BUTTON (combo_button_widget));
default_menu_item = get_default_action_for_view (handler, shell_view);
if (default_menu_item == NULL) {
gtk_widget_set_sensitive (combo_button_widget, FALSE);
e_combo_button_set_label (E_COMBO_BUTTON (combo_button_widget), _("New"));
e_combo_button_set_icon (E_COMBO_BUTTON (combo_button_widget), NULL);
return;
}
gtk_widget_set_sensitive (combo_button_widget, TRUE);
e_combo_button_set_icon (E_COMBO_BUTTON (combo_button_widget), default_menu_item->icon);
}
/* GtkObject methods. */
static void
impl_destroy (GtkObject *object)
{
EShellUserCreatableItemsHandler *handler;
EShellUserCreatableItemsHandlerPrivate *priv;
GSList *p;
handler = E_SHELL_USER_CREATABLE_ITEMS_HANDLER (object);
priv = handler->priv;
for (p = priv->components; p != NULL; p = p->next)
component_free ((Component *) p->data);
g_slist_free (priv->components);
g_free (priv->menu_xml);
free_menu_items (priv->menu_items);
g_free (priv);
(* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}
static void
class_init (GtkObjectClass *object_class)
{
parent_class = gtk_type_class (PARENT_TYPE);
object_class->destroy = impl_destroy;
}
static void
init (EShellUserCreatableItemsHandler *shell_user_creatable_items_handler)
{
EShellUserCreatableItemsHandlerPrivate *priv;
priv = g_new (EShellUserCreatableItemsHandlerPrivate, 1);
priv->components = NULL;
priv->menu_xml = NULL;
priv->menu_items = NULL;
shell_user_creatable_items_handler->priv = priv;
}
EShellUserCreatableItemsHandler *
e_shell_user_creatable_items_handler_new (void)
{
EShellUserCreatableItemsHandler *new;
new = gtk_type_new (e_shell_user_creatable_items_handler_get_type ());
return new;
}
void
e_shell_user_creatable_items_handler_add_component (EShellUserCreatableItemsHandler *handler,
const char *id,
EvolutionShellComponentClient *shell_component_client)
{
EShellUserCreatableItemsHandlerPrivate *priv;
g_return_if_fail (handler != NULL);
g_return_if_fail (E_IS_SHELL_USER_CREATABLE_ITEMS_HANDLER (handler));
g_return_if_fail (shell_component_client != NULL);
g_return_if_fail (EVOLUTION_IS_SHELL_COMPONENT_CLIENT (shell_component_client));
priv = handler->priv;
g_return_if_fail (priv->menu_xml == NULL);
priv->components = g_slist_prepend (priv->components, component_new (id, shell_component_client));
}
/**
* e_shell_user_creatable_items_handler_attach_menus:
* @handler:
* @shell_view:
*
* Set up the menus and toolbar items for @shell_view. When the shell changes
* view, the menu and the toolbar item will update automatically (i.e. the
* actions for the current folder will go on top etc.).
**/
void
e_shell_user_creatable_items_handler_attach_menus (EShellUserCreatableItemsHandler *handler,
EShellView *shell_view)
{
BonoboUIComponent *ui_component;
EShellUserCreatableItemsHandlerPrivate *priv;
g_return_if_fail (handler != NULL);
g_return_if_fail (E_IS_SHELL_USER_CREATABLE_ITEMS_HANDLER (handler));
g_return_if_fail (shell_view != NULL);
g_return_if_fail (E_IS_SHELL_VIEW (shell_view));
priv = handler->priv;
setup_toolbar_button (handler, shell_view);
gtk_signal_connect (GTK_OBJECT (shell_view), "view_changed",
GTK_SIGNAL_FUNC (shell_view_view_changed_callback), handler);
ensure_menu_xml (handler);
add_verbs (handler, shell_view);
ui_component = e_shell_view_get_bonobo_ui_component (shell_view);
bonobo_ui_component_set (ui_component, "/menu/File/New/NewItems", priv->menu_xml, NULL);
bonobo_ui_component_set (ui_component, "/popups/NewPopup", priv->menu_xml, NULL);
}
E_MAKE_TYPE (e_shell_user_creatable_items_handler,
"EShellUserCreatableItemsHandler", EShellUserCreatableItemsHandler,
class_init, init, PARENT_TYPE)
|