aboutsummaryrefslogtreecommitdiffstats
path: root/shell/e-shell-view.c
blob: 763359fd35770a27be6b73392e48dda9a4f58dd3 (plain) (blame)
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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 * E-shell-view.c: Implements a Shell View of Evolution
 *
 * Authors:
 *   Miguel de Icaza (miguel@helixcode.com)
 *
 * (C) 2000 Helix Code, Inc.
 */
#include <config.h>
#include <gnome.h>

#include "shortcut-bar/e-shortcut-bar.h"
#include "e-util/e-util.h"
#include "e-shell-view.h"
#include "e-shell-view-menu.h"
#include "e-shell-shortcut.h"
#include "Evolution.h"

#include <bonobo.h>
#include <libgnorba/gnorba.h>
#include <gtk/gtkprivate.h>

#ifdef USING_OAF

#    define MAIL_CONTROL_ID        "OAFIID:control:evolution-mail:833d5a71-a201-4a0e-b7e6-5475c5c4cb45"
#    define ADDRESSBOOK_CONTROL_ID "OAFIID:control:addressbook:851f883b-2fe7-4c94-a1e3-a1f2a7a03c49"
#    define CALENDAR_CONTROL_ID    "OAFIID:control:calendar:dd34ddae-25c6-486b-a8a8-3e8f0286b54c"

#else

#   define MAIL_CONTROL_ID        "control:evolution-mail"
#   define ADDRESSBOOK_CONTROL_ID "control:addressbook"
#   define CALENDAR_CONTROL_ID    "control:calendar"

#endif

#define PARENT_TYPE gnome_app_get_type ()

static GtkObjectClass *parent_class;

struct _EShellViewPrivate {
    /* a hashtable of e-folders -> widgets */
    GHashTable *folder_views;
    GtkWidget *notebook;
};

static void
destroy_folder_view (gpointer unused, gpointer pfolder_view, gpointer unused2)
{
    GtkWidget *folder_view = GTK_WIDGET (pfolder_view);
    BonoboWidget *bonobo_widget;
    BonoboObject *bonobo_object;    
    CORBA_Object corba_control;
    CORBA_Environment ev;

    g_print ("%s: %s entered\n",
         __FILE__, __FUNCTION__);
    
    g_return_if_fail (BONOBO_IS_WIDGET (folder_view));

    bonobo_widget = BONOBO_WIDGET (folder_view);
    
    bonobo_object = BONOBO_OBJECT (
        bonobo_widget_get_server (bonobo_widget));

    corba_control = bonobo_object_corba_objref (bonobo_object);
    
    g_return_if_fail (corba_control != NULL);
    
    CORBA_exception_init (&ev);

    /* hangs on this! */
    Bonobo_Unknown_unref (corba_control, &ev);
    CORBA_exception_free (&ev);

    g_print ("%s: %s exited\n",
         __FILE__, __FUNCTION__);   
}


static void
esv_destroy (GtkObject *object)
{
    EShellView *eshell_view = E_SHELL_VIEW (object);

    e_shell_unregister_view (eshell_view->eshell, eshell_view);

    g_hash_table_foreach (eshell_view->priv->folder_views,
                  destroy_folder_view, NULL);
    
    g_hash_table_destroy (eshell_view->priv->folder_views); 
    g_free (eshell_view->priv); 
    parent_class->destroy (object);
}

static void
e_shell_view_class_init (GtkObjectClass *object_class)
{
    object_class->destroy = esv_destroy;

    parent_class = gtk_type_class (PARENT_TYPE);
}

static void
e_shell_view_setup (EShellView *eshell_view)
{
    /*
     * FIXME, should load the config if (load_config)....
     */
    gtk_window_set_default_size (GTK_WINDOW (eshell_view), 600, 600);
}




static void
e_shell_view_setup_shortcut_display (EShellView *eshell_view)
{
    eshell_view->shortcut_bar =
        e_shortcut_bar_view_new (eshell_view->eshell->shortcut_bar);
    
    eshell_view->hpaned = e_paned_new (TRUE);

    e_paned_insert (E_PANED (eshell_view->hpaned), 0,
            eshell_view->shortcut_bar,
            100);

    gtk_widget_show_all (eshell_view->hpaned);
    
    gnome_app_set_contents (GNOME_APP (eshell_view),
                eshell_view->hpaned);

    gtk_signal_connect (
        GTK_OBJECT (eshell_view->shortcut_bar), "item_selected",
        GTK_SIGNAL_FUNC (shortcut_bar_item_selected), eshell_view);
}

static GtkWidget *
get_view (EShellView *eshell_view, EFolder *efolder, Bonobo_UIHandler uih)
{
    GtkWidget *w = NULL;
    Evolution_Shell corba_shell = CORBA_OBJECT_NIL;
    EShell *shell_model = eshell_view->eshell;

    /* This type could be E_FOLDER_MAIL, E_FOLDER_CONTACTS, etc */
    EFolderType e_folder_type;

    g_assert (efolder);
    g_assert (eshell_view);
    
    e_folder_type = e_folder_get_folder_type (efolder);
    
    if (shell_model)
        corba_shell = bonobo_object_corba_objref (
            BONOBO_OBJECT (shell_model));
    else 
        g_warning ("The shell Bonobo object does not have "
               "an associated CORBA object\n");
    
    /* depending on the type of folder, 
     * we launch a different bonobo component */
    switch (e_folder_type) {

    case E_FOLDER_MAIL :
        w = bonobo_widget_new_control (MAIL_CONTROL_ID, uih);
        break;

    case E_FOLDER_CONTACTS :
        w = bonobo_widget_new_control (ADDRESSBOOK_CONTROL_ID, uih);
        break;

    case E_FOLDER_CALENDAR : {
        gchar *user_cal_file;
        BonoboPropertyBagClient *pbc;
        BonoboControlFrame *cf;

        w = bonobo_widget_new_control (CALENDAR_CONTROL_ID, uih);

        if (w) {
            cf = bonobo_widget_get_control_frame (BONOBO_WIDGET (w));
            pbc = bonobo_control_frame_get_control_property_bag (cf);
            /*pbc = bonobo_control_get_property_bag (w);*/
            
            user_cal_file =
                g_concat_dir_and_file (gnome_util_user_home (),
                               ".gnome/user-cal.vcf");
            
            bonobo_property_bag_client_set_value_string (pbc,
                                     "calendar_uri",
                                     user_cal_file);
        }
        
        break;
    }

    case E_FOLDER_TASKS :
    case E_FOLDER_OTHER :       
    default : 
        printf ("%s: %s: No bonobo component associated with %s\n",
            __FILE__,
            __FUNCTION__,
            e_folder_get_description (efolder));
        return NULL;
    }

    if (w) {
        Evolution_ServiceRepository corba_sr;
        BonoboObjectClient *server =
            bonobo_widget_get_server (BONOBO_WIDGET (w));
        CORBA_Environment ev;
        CORBA_exception_init (&ev);

        /* Does this control have the "ServiceRepository" interface? */
        corba_sr = (Evolution_ServiceRepository) 
            bonobo_object_client_query_interface (
                server,
                "IDL:Evolution/ServiceRepository:1.0",
                NULL);

        /* If it does, pass our shell interface to it */
        if (corba_sr != CORBA_OBJECT_NIL) {

            Evolution_ServiceRepository_set_shell (corba_sr,
                                   corba_shell,
                                   &ev);
            /* We're done with the service repository interface,
               so now let's get rid of it */
            Bonobo_Unknown_unref (corba_sr, &ev);
            
        } else {
            
            g_print ("The bonobo component for \"%s\" doesn't "
                 "seem to implement the "
                 "Evolution::ServiceRepository interface\n",
                 e_folder_get_description (efolder));
        }

        CORBA_exception_free (&ev);

        gtk_widget_show (w);
    }
    
    return w;
}

void e_shell_view_toggle_shortcut_bar (EShellView *eshell_view)
{
    GtkWidget *shortcut_bar = eshell_view->shortcut_bar;
    GtkWidget *hpaned = eshell_view->hpaned;
    
    if (shortcut_bar->parent) {
        gtk_widget_ref (shortcut_bar);              
        e_paned_remove (E_PANED (hpaned), shortcut_bar);
    }
    else
        e_paned_insert (E_PANED (hpaned), 0, shortcut_bar, 
                100);
    gtk_widget_show_all (GTK_WIDGET (hpaned));
}

void e_shell_view_toggle_treeview (EShellView *eshell_view)
{
    
}

static gboolean
bonobo_widget_is_dead (BonoboWidget *bw)
{
    BonoboObject *boc = BONOBO_OBJECT (bonobo_widget_get_server (bw));
    CORBA_Object obj = bonobo_object_corba_objref (boc);

    CORBA_Environment ev;
    
    gboolean is_dead = FALSE;

    CORBA_exception_init (&ev);
    if (CORBA_Object_non_existent(obj, &ev))
        is_dead = TRUE;
    CORBA_exception_free (&ev);

    return is_dead;
}


void
e_shell_view_set_view (EShellView *eshell_view, EFolder *efolder)
{
    GtkNotebook *notebook;
    GtkWidget *folder_view;
    int current_page;
    BonoboControlFrame *control_frame;

    g_assert (eshell_view);
    g_assert (efolder);

    notebook = GTK_NOTEBOOK (eshell_view->priv->notebook);
    current_page = gtk_notebook_get_current_page (notebook);

    if (current_page != -1) {
        GtkWidget *current;

        current = gtk_notebook_get_nth_page (notebook, current_page);
        control_frame = bonobo_widget_get_control_frame (
            BONOBO_WIDGET (current));
    } else
        control_frame = NULL;

    /* If there's a notebook page in our hash that represents this
     * efolder, switch to it.
     */
    folder_view = g_hash_table_lookup (eshell_view->priv->folder_views,
                       efolder);
    if (folder_view) {
        int notebook_page;

        g_assert (GTK_IS_NOTEBOOK (notebook));
        g_assert (GTK_IS_WIDGET (folder_view));
        
        notebook_page = gtk_notebook_page_num (notebook,
                               folder_view);
        g_assert (notebook_page != -1);
        
        /* a BonoboWidget can be a "zombie" in the sense that its
           actual control is dead; if it's zombie, let's recreate it*/
        if (bonobo_widget_is_dead (BONOBO_WIDGET (folder_view))) {

            GtkWidget *parent = folder_view->parent;
            Bonobo_UIHandler uih =
                bonobo_object_corba_objref (
                    BONOBO_OBJECT (eshell_view->uih));          

            /* out with the old */
            gtk_container_remove (GTK_CONTAINER (parent), folder_view);

            /* in with the new */
            folder_view = get_view (eshell_view, efolder, uih);
            gtk_container_add (GTK_CONTAINER (parent), folder_view);

            /* make sure it's in our hashtable, so we can get to
               it from the shortcut bar */
            g_hash_table_insert (eshell_view->priv->folder_views,
                         efolder, folder_view);
            gtk_widget_show_all (folder_view);
        }
        
        
        gtk_notebook_set_page (notebook, notebook_page);

        return;
        
    } else {
        /* Get a new control that represents this efolder,
         * append it to our notebook, and put it in our hash.
         */
        Bonobo_UIHandler uih =
            bonobo_object_corba_objref (
                BONOBO_OBJECT (eshell_view->uih));
        int new_page_index;

        folder_view = get_view (eshell_view, efolder, uih);
        if (!folder_view) {
            /* FIXME: Report failure.  */
            return;
        }

        gtk_notebook_append_page (notebook, folder_view, NULL);
        new_page_index = gtk_notebook_page_num (notebook,
                            folder_view);
        g_hash_table_insert (eshell_view->priv->folder_views,
                     efolder, folder_view);
        gtk_notebook_set_page (notebook, new_page_index);
    }

    if (control_frame)
        bonobo_control_frame_control_deactivate (control_frame);

    control_frame =
        bonobo_widget_get_control_frame (BONOBO_WIDGET (folder_view));
    bonobo_control_frame_set_autoactivate (control_frame, FALSE);
    bonobo_control_frame_control_activate (control_frame);
}

GtkWidget *
e_shell_view_new (EShell *eshell, EFolder *efolder, gboolean show_shortcut_bar)
{
    EShellView *eshell_view;

    g_return_val_if_fail (eshell != NULL, NULL);
    g_return_val_if_fail (efolder != NULL, NULL);   
    
    eshell_view = gtk_type_new (e_shell_view_get_type ());

    eshell_view->priv = g_new (EShellViewPrivate, 1);
    eshell_view->priv->folder_views =
        g_hash_table_new (g_direct_hash, g_direct_equal);
    eshell_view->priv->notebook = NULL;

    gnome_app_construct (GNOME_APP (eshell_view),
                 "Evolution", "Evolution");

    eshell_view->eshell = eshell;
    e_shell_view_setup (eshell_view);
    e_shell_view_setup_menus (eshell_view);

    e_shell_register_view (eshell, eshell_view);
    eshell_view->shortcut_displayed = show_shortcut_bar;
    e_shell_view_setup_shortcut_display (eshell_view);

    /* create our notebook, if it hasn't been created already */
    if (!eshell_view->priv->notebook) {
        eshell_view->priv->notebook = gtk_notebook_new();

        gtk_notebook_set_show_border (
            GTK_NOTEBOOK (eshell_view->priv->notebook),
            FALSE);
        gtk_notebook_set_show_tabs (
            GTK_NOTEBOOK (eshell_view->priv->notebook),
            FALSE);

        gtk_widget_show (eshell_view->priv->notebook);

        e_paned_insert (E_PANED (eshell_view->hpaned),
                1,
                eshell_view->priv->notebook,
//              gtk_button_new_with_label ("foobar"),
                500);
        
        gtk_widget_show_all (GTK_WIDGET (eshell_view->hpaned));
    }

    e_shell_view_set_view (eshell_view, efolder);
    
    return (GtkWidget *) eshell_view;
}

void
e_shell_view_display_shortcut_bar (EShellView *eshell_view, gboolean display)
{
    g_return_if_fail (eshell_view != NULL);
    g_return_if_fail (E_IS_SHELL_VIEW (eshell_view));

    g_error ("Switching code for the shortcut bar is not written yet");
}

E_MAKE_TYPE (e_shell_view, "EShellView", EShellView, e_shell_view_class_init, NULL, PARENT_TYPE);

void
e_shell_view_new_folder (EShellView *esv)
{
    g_return_if_fail (esv != NULL);
    g_return_if_fail (E_IS_SHELL_VIEW (esv));
}

void
e_shell_view_new_shortcut (EShellView *esv)
{
    g_return_if_fail (esv != NULL);
    g_return_if_fail (E_IS_SHELL_VIEW (esv));
}