aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/tasks-component.c
blob: 7c70192c909ac378b098001cb2aba414a296d8da (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
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
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
/*
 * 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 <http://www.gnu.org/licenses/>  
 *
 *
 * Authors:
 *      Rodrigo Moya <rodrigo@ximian.com>
 *
 * Copyright (C) 1999-2008 Novell, Inc. (www.novell.com)
 *
 */

#ifdef HAVE_CONFIG_H
#include <config.h>
#endif

#include <errno.h>
#include <string.h>
#include <glib/gi18n.h>
#include <bonobo/bonobo-control.h>
#include <bonobo/bonobo-exception.h>
#include <gconf/gconf-client.h>
#include <libecal/e-cal.h>
#include <libedataserver/e-data-server-util.h>
#include <libedataserver/e-url.h>
#include <libedataserverui/e-source-selector.h>
#include <shell/e-user-creatable-items-handler.h>
#include <shell/e-component-view.h>
#include "e-cal-model.h"
#include "e-tasks.h"
#include "tasks-component.h"
#include "tasks-control.h"
#include "e-comp-editor-registry.h"
#include "migration.h"
#include "comp-util.h"
#include "calendar-config.h"
#include "e-cal-popup.h"
#include "common/authentication.h"
#include "dialogs/calendar-setup.h"
#include "dialogs/comp-editor.h"
#include "dialogs/copy-source-dialog.h"
#include "dialogs/task-editor.h"
#include "misc/e-info-label.h"
#include "e-util/e-error.h"

#define CREATE_TASK_ID               "task"
#define CREATE_TASK_ASSIGNED_ID      "task-assigned"
#define CREATE_TASK_LIST_ID          "task-list"

#define WEB_BASE_URI "webcal://"
#define PERSONAL_RELATIVE_URI "system"

#define PARENT_TYPE bonobo_object_get_type ()

static BonoboObjectClass *parent_class = NULL;

typedef struct
{
    ESourceList *source_list;

    GSList *source_selection;

    ETasks *tasks;
    ETable *table;
    ETableModel *model;

    GtkWidget *source_selector;

    BonoboControl *view_control;

    GList *notifications;

} TasksComponentView;

struct _TasksComponentPrivate {

    ESourceList *source_list;
    GSList *source_selection;

    GList *views;

    ECal *create_ecal;

    GList *notifications;
};

/* Utility functions.  */
/* FIXME Some of these are duplicated from calendar-component.c */
static gboolean
is_in_selection (GSList *selection, ESource *source)
{
    GSList *l;

    for (l = selection; l; l = l->next) {
        ESource *selected_source = l->data;

        if (!strcmp (e_source_peek_uid (selected_source), e_source_peek_uid (source)))
            return TRUE;
    }

    return FALSE;
}

static gboolean
is_in_uids (GSList *uids, ESource *source)
{
    GSList *l;

    for (l = uids; l; l = l->next) {
        const char *uid = l->data;

        if (!strcmp (uid, e_source_peek_uid (source)))
            return TRUE;
    }

    return FALSE;
}

static void
update_uris_for_selection (TasksComponentView *component_view)
{
    GSList *selection, *l, *uids_selected = NULL;

    selection = e_source_selector_get_selection (E_SOURCE_SELECTOR (component_view->source_selector));

    for (l = component_view->source_selection; l; l = l->next) {
        ESource *old_selected_source = l->data;

        if (!is_in_selection (selection, old_selected_source))
            e_tasks_remove_todo_source (component_view->tasks, old_selected_source);
    }

    for (l = selection; l; l = l->next) {
        ESource *selected_source = l->data;

        e_tasks_add_todo_source (component_view->tasks, selected_source);
        uids_selected = g_slist_append (uids_selected, (char *)e_source_peek_uid (selected_source));
    }

    e_source_selector_free_selection (component_view->source_selection);
    component_view->source_selection = selection;

    /* Save the selection for next time we start up */
    calendar_config_set_tasks_selected (uids_selected);
    g_slist_free (uids_selected);
}

static void
update_uri_for_primary_selection (TasksComponentView *component_view)
{
    ESource *source;
    ECalendarTable *cal_table;
    ETable *etable;

    source = e_source_selector_peek_primary_selection (E_SOURCE_SELECTOR (component_view->source_selector));
    if (!source)
        return;

    /* Set the default */
    e_tasks_set_default_source (component_view->tasks, source);

    cal_table = e_tasks_get_calendar_table (component_view->tasks);
    etable = e_calendar_table_get_table (cal_table);

    tasks_control_sensitize_commands (component_view->view_control, component_view->tasks, e_table_selected_count (etable));

    /* Save the selection for next time we start up */
    calendar_config_set_primary_tasks (e_source_peek_uid (source));
}

/* Callbacks.  */
static void
copy_task_list_cb (EPopup *ep, EPopupItem *pitem, void *data)
{
    TasksComponentView *component_view = data;
    ESource *selected_source;

    selected_source = e_source_selector_peek_primary_selection (E_SOURCE_SELECTOR (component_view->source_selector));
    if (!selected_source)
        return;

    copy_source_dialog (GTK_WINDOW (gtk_widget_get_toplevel(ep->target->widget)), selected_source, E_CAL_SOURCE_TYPE_TODO);
}

static void
delete_task_list_cb (EPopup *ep, EPopupItem *pitem, void *data)
{
    TasksComponentView *component_view = data;
    ESource *selected_source;
    ECal *cal;
    char *uri;

    selected_source = e_source_selector_peek_primary_selection (E_SOURCE_SELECTOR (component_view->source_selector));
    if (!selected_source)
        return;

    if (e_error_run((GtkWindow *)gtk_widget_get_toplevel(ep->target->widget),
            "calendar:prompt-delete-task-list", e_source_peek_name(selected_source), NULL) != GTK_RESPONSE_YES)
        return;

    /* first, ask the backend to remove the task list */
    uri = e_source_get_uri (selected_source);
    cal = e_cal_model_get_client_for_uri (
        e_calendar_table_get_model (E_CALENDAR_TABLE (e_tasks_get_calendar_table (component_view->tasks))),
        uri);
    if (!cal)
        cal = e_cal_new_from_uri (uri, E_CAL_SOURCE_TYPE_TODO);
    g_free (uri);
    if (cal) {
        if (e_cal_remove (cal, NULL)) {
            if (e_source_selector_source_is_selected (E_SOURCE_SELECTOR (component_view->source_selector),
                                  selected_source)) {
                e_tasks_remove_todo_source (component_view->tasks, selected_source);
                e_source_selector_unselect_source (E_SOURCE_SELECTOR (component_view->source_selector),
                                   selected_source);
            }

            e_source_group_remove_source (e_source_peek_group (selected_source), selected_source);
            e_source_list_sync (component_view->source_list, NULL);
        }
    }
}

static void
new_task_list_cb (EPopup *ep, EPopupItem *pitem, void *data)
{
    calendar_setup_new_task_list (GTK_WINDOW (gtk_widget_get_toplevel(ep->target->widget)));
}

static void
edit_task_list_cb (EPopup *ep, EPopupItem *pitem, void *data)
{
    TasksComponentView *component_view = data;
    ESource *selected_source;

    selected_source = e_source_selector_peek_primary_selection (E_SOURCE_SELECTOR (component_view->source_selector));
    if (!selected_source)
        return;

    calendar_setup_edit_task_list (GTK_WINDOW (gtk_widget_get_toplevel(ep->target->widget)), selected_source);
}

static EPopupItem etc_source_popups[] = {
    { E_POPUP_ITEM, "10.new", N_("_New Task List"), new_task_list_cb, NULL, "stock_todo", 0, 0 },
    { E_POPUP_ITEM, "15.copy", N_("_Copy..."), copy_task_list_cb, NULL, "edit-copy", 0, E_CAL_POPUP_SOURCE_PRIMARY },

    { E_POPUP_BAR, "20.bar" },
    { E_POPUP_ITEM, "20.delete", N_("_Delete"), delete_task_list_cb, NULL, "edit-delete", 0, E_CAL_POPUP_SOURCE_USER|E_CAL_POPUP_SOURCE_PRIMARY },

    { E_POPUP_BAR, "99.bar" },
    { E_POPUP_ITEM, "99.properties", N_("_Properties"), edit_task_list_cb, NULL, "document-properties", 0, E_CAL_POPUP_SOURCE_PRIMARY },
};

static void
etc_source_popup_free(EPopup *ep, GSList *list, void *data)
{
    g_slist_free(list);
}

static gboolean
popup_event_cb(ESourceSelector *selector, ESource *insource, GdkEventButton *event, TasksComponentView *component_view)
{
    ECalPopup *ep;
    ECalPopupTargetSource *t;
    GSList *menus = NULL;
    int i;
    GtkMenu *menu;

    /** @HookPoint-ECalPopup: Tasks Source Selector Context Menu
     * @Id: org.gnome.evolution.tasks.source.popup
     * @Class: org.gnome.evolution.calendar.popup:1.0
     * @Target: ECalPopupTargetSource
     *
     * The context menu on the source selector in the tasks window.
     */
    ep = e_cal_popup_new("org.gnome.evolution.tasks.source.popup");
    t = e_cal_popup_target_new_source(ep, selector);
    t->target.widget = (GtkWidget *)component_view->tasks;

    for (i=0;i<sizeof(etc_source_popups)/sizeof(etc_source_popups[0]);i++)
        menus = g_slist_prepend(menus, &etc_source_popups[i]);

    e_popup_add_items((EPopup *)ep, menus, NULL,etc_source_popup_free, component_view);

    menu = e_popup_create_menu_once((EPopup *)ep, (EPopupTarget *)t, 0);
    gtk_menu_popup(menu, NULL, NULL, NULL, NULL, event?event->button:0, event?event->time:gtk_get_current_event_time());

    return TRUE;
}

static void
source_selection_changed_cb (ESourceSelector *selector, TasksComponentView *component_view)
{
    update_uris_for_selection (component_view);
}

static void
primary_source_selection_changed_cb (ESourceSelector *selector, TasksComponentView *component_view)
{
    update_uri_for_primary_selection (component_view);
}

static void
source_removed_cb (ETasks *tasks, ESource *source, TasksComponentView *component_view)
{
    e_source_selector_unselect_source (E_SOURCE_SELECTOR (component_view->source_selector), source);
}

static gboolean
update_single_object (ECal *client, icalcomponent *icalcomp)
{
    char *uid;
    icalcomponent *tmp_icalcomp;

    uid = (char *) icalcomponent_get_uid (icalcomp);

    if (e_cal_get_object (client, uid, NULL, &tmp_icalcomp, NULL))
        return e_cal_modify_object (client, icalcomp, CALOBJ_MOD_ALL, NULL);

    return e_cal_create_object (client, icalcomp, &uid, NULL);
}

static gboolean
update_objects (ECal *client, icalcomponent *icalcomp)
{
    icalcomponent *subcomp;
    icalcomponent_kind kind;

    kind = icalcomponent_isa (icalcomp);
    if (kind == ICAL_VTODO_COMPONENT || kind == ICAL_VEVENT_COMPONENT)
        return update_single_object (client, icalcomp);
    else if (kind != ICAL_VCALENDAR_COMPONENT)
        return FALSE;

    subcomp = icalcomponent_get_first_component (icalcomp, ICAL_ANY_COMPONENT);
    while (subcomp) {
        gboolean success;

        kind = icalcomponent_isa (subcomp);
        if (kind == ICAL_VTIMEZONE_COMPONENT) {
            icaltimezone *zone;

            zone = icaltimezone_new ();
            icaltimezone_set_component (zone, subcomp);

            success = e_cal_add_timezone (client, zone, NULL);
            icaltimezone_free (zone, 1);
            if (!success)
                return success;
        } else if (kind == ICAL_VTODO_COMPONENT ||
               kind == ICAL_VEVENT_COMPONENT) {
            success = update_single_object (client, subcomp);
            if (!success)
                return success;
        }

        subcomp = icalcomponent_get_next_component (icalcomp, ICAL_ANY_COMPONENT);
    }

    return TRUE;
}

static void
selector_tree_drag_data_received (GtkWidget *widget,
                  GdkDragContext *context,
                  gint x,
                  gint y,
                  GtkSelectionData *data,
                  guint info,
                  guint time,
                  gpointer user_data)
{
    GtkTreePath *path = NULL;
    GtkTreeViewDropPosition pos;
    gpointer source = NULL;
    GtkTreeModel *model;
    GtkTreeIter iter;
    gboolean success = FALSE;
    icalcomponent *icalcomp = NULL;
    ECal *client = NULL;
    GSList *components, *p;
    TasksComponent *component = TASKS_COMPONENT (user_data);

    if (!gtk_tree_view_get_dest_row_at_pos (GTK_TREE_VIEW (widget),
                        x, y, &path, &pos))
        goto finish;

    model = gtk_tree_view_get_model (GTK_TREE_VIEW (widget));

    if (!gtk_tree_model_get_iter (model, &iter, path))
        goto finish;


    gtk_tree_model_get (model, &iter, 0, &source, -1);

    if (E_IS_SOURCE_GROUP (source) || e_source_get_readonly (source) || !data->data)
        goto finish;

    client = auth_new_cal_from_source (source, E_CAL_SOURCE_TYPE_TODO);

    if (!client || !e_cal_open (client, TRUE, NULL))
        goto  finish;

    components = cal_comp_selection_get_string_list (data);
    for (p = components; p; p = p->next) {
        const char * uid;
        char *old_uid = NULL;
        icalcomponent *tmp_icalcomp = NULL;
        GError *error = NULL;
        char *comp_str; /* do not free this! */

        /* p->data is "source_uid\ncomponent_string" */
        comp_str = strchr (p->data, '\n');
        if (!comp_str)
            continue;

        comp_str [0] = 0;
        comp_str++;
        icalcomp = icalparser_parse_string (comp_str);

        if (!icalcomp)
            continue;

        /* FIXME deal with GDK_ACTION_ASK */
        if (context->action == GDK_ACTION_COPY) {
            old_uid = g_strdup (icalcomponent_get_uid (icalcomp));
            uid = e_cal_component_gen_uid ();
            icalcomponent_set_uid (icalcomp, uid);
        }

        uid = icalcomponent_get_uid (icalcomp);
        if (!old_uid)
            old_uid = g_strdup (uid);

        if (!e_cal_get_object (client, uid, NULL, &tmp_icalcomp, &error)) {
            if ((error != NULL) && (error->code != E_CALENDAR_STATUS_OBJECT_NOT_FOUND))
                g_message ("Failed to search the object in destination task list: %s",error->message);
            else {
                /* this will report success by last item, but we don't care */
                success = update_objects (client, icalcomp);

                if (success && context->action == GDK_ACTION_MOVE) {
                    /* remove components rather here, because we know which has been moved */
                    ESource *source_source;
                    ECal *source_client;

                    source_source = e_source_list_peek_source_by_uid (component->priv->source_list, p->data);

                    if (source_source && !E_IS_SOURCE_GROUP (source_source) && !e_source_get_readonly (source_source)) {
                        source_client = auth_new_cal_from_source (source_source, E_CAL_SOURCE_TYPE_TODO);

                        if (source_client) {
                            gboolean read_only = TRUE;

                            e_cal_is_read_only (source_client, &read_only, NULL);

                            if (!read_only && e_cal_open (source_client, TRUE, NULL))
                                e_cal_remove_object (source_client, old_uid, NULL);
                            else if (!read_only)
                                g_message ("Cannot open source client to remove old task");

                            g_object_unref (source_client);
                        } else
                            g_message ("Cannot create source client to remove old task");
                    }
                }
            }

            g_clear_error (&error);
        } else
            icalcomponent_free (tmp_icalcomp);

        g_free (old_uid);
        icalcomponent_free (icalcomp);
    }
    g_slist_foreach (components, (GFunc)g_free, NULL);
    g_slist_free (components);

 finish:
    if (client)
        g_object_unref (client);
    if (source)
        g_object_unref (source);
    if (path)
        gtk_tree_path_free (path);

    gtk_drag_finish (context, success, context->action == GDK_ACTION_MOVE, time);
}

static void
config_create_ecal_changed_cb (GConfClient *client, guint id, GConfEntry *entry, gpointer data)
{
    TasksComponent *component = data;
    TasksComponentPrivate *priv;

    priv = component->priv;

    g_object_unref (priv->create_ecal);
    priv->create_ecal = NULL;

    priv->notifications = g_list_remove (priv->notifications, GUINT_TO_POINTER (id));
}

static ECal *
setup_create_ecal (TasksComponent *component, TasksComponentView *component_view)
{
    TasksComponentPrivate *priv;
    ESource *source = NULL;
    char *uid;
    guint not;

    priv = component->priv;

    if (component_view) {
        ECal *default_ecal;

        default_ecal = e_tasks_get_default_client (component_view->tasks);
        if (default_ecal)
            return default_ecal;
    }

    if (priv->create_ecal)
        return priv->create_ecal;

    /* Get the current primary calendar, or try to set one if it doesn't already exist */
    uid = calendar_config_get_primary_tasks ();
    if (uid) {
        source = e_source_list_peek_source_by_uid (priv->source_list, uid);
        g_free (uid);

        priv->create_ecal = auth_new_cal_from_source (source, E_CAL_SOURCE_TYPE_TODO);
    }

    if (!priv->create_ecal) {
        /* Try to create a default if there isn't one */
        source = e_source_list_peek_source_any (priv->source_list);
        if (source)
            priv->create_ecal = auth_new_cal_from_source (source, E_CAL_SOURCE_TYPE_TODO);
    }

    if (priv->create_ecal) {
        icaltimezone *zone;

        zone = calendar_config_get_icaltimezone ();
        e_cal_set_default_timezone (priv->create_ecal, zone, NULL);

        if (!e_cal_open (priv->create_ecal, FALSE, NULL)) {
            GtkWidget *dialog;

            dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
                             GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
                             _("Unable to open the task list '%s' for creating events and meetings"),
                             e_source_peek_name (source));
            gtk_dialog_run (GTK_DIALOG (dialog));
            gtk_widget_destroy (dialog);

            return NULL;
        }

    } else {
        GtkWidget *dialog;

        dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
                         GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
                         "%s", _("There is no calendar available for creating tasks"));
        gtk_dialog_run (GTK_DIALOG (dialog));
        gtk_widget_destroy (dialog);

        return NULL;
    }

    /* Handle the fact it may change on us */
    not = calendar_config_add_notification_primary_tasks (config_create_ecal_changed_cb,
                                  component);
    priv->notifications = g_list_prepend (priv->notifications, GUINT_TO_POINTER (not));

    /* Save the primary source for use elsewhere */
    calendar_config_set_primary_tasks (e_source_peek_uid (source));

    return priv->create_ecal ;
}

/* Ensures the calendar is selected */
static void
object_created_cb (CompEditor *ce, ECalendarTable *cal_table)
{
    g_return_if_fail (cal_table != NULL);

    cal_table->user_created_cal = comp_editor_get_client (ce);
    g_signal_emit_by_name (cal_table, "user_created");
    cal_table->user_created_cal = NULL;
}

static gboolean
create_new_todo (TasksComponent *task_component, gboolean is_assigned, TasksComponentView *component_view)
{
    ECal *ecal;
    ECalComponent *comp;
    CompEditor *editor;
    guint32 flags = 0;

    ecal = setup_create_ecal (task_component, component_view);
    if (!ecal)
        return FALSE;

    if (is_assigned)
        flags |= COMP_EDITOR_IS_ASSIGNED;

    flags |= COMP_EDITOR_NEW_ITEM | COMP_EDITOR_USER_ORG;

    editor = task_editor_new (ecal, flags);
    comp = cal_comp_task_new_with_defaults (ecal);

    if (component_view)
        g_signal_connect (editor, "object_created", G_CALLBACK (object_created_cb), e_tasks_get_calendar_table (component_view->tasks));

    comp_editor_edit_comp (editor, comp);
    if (is_assigned)
        task_editor_show_assignment (TASK_EDITOR (editor));
    gtk_window_present (GTK_WINDOW (editor));

    e_comp_editor_registry_add (comp_editor_registry, editor, TRUE);

    return TRUE;
}

static TasksComponentView *
create_component_view (TasksComponent *tasks_component)
{
    TasksComponentPrivate *priv;
    TasksComponentView *component_view;

    priv = tasks_component->priv;

    /* Create the calendar component view */
    component_view = g_new0 (TasksComponentView, 1);

    /* Add the source lists */
    component_view->source_list = g_object_ref (priv->source_list);

    /* Create sidebar selector */
    component_view->source_selector = e_source_selector_new (tasks_component->priv->source_list);

    g_signal_connect (component_view->source_selector, "drag-data-received",
              G_CALLBACK (selector_tree_drag_data_received), tasks_component);

    component_view->tasks = (ETasks *) bonobo_control_get_widget (component_view->view_control);
    component_view->table = e_calendar_table_get_table (e_tasks_get_calendar_table (component_view->tasks));
    component_view->model = E_TABLE_MODEL (e_calendar_table_get_model (e_tasks_get_calendar_table (component_view->tasks)));

    /* This signal is thrown if backends die - we update the selector */
    g_signal_connect (component_view->tasks, "source_removed",
              G_CALLBACK (source_removed_cb), component_view);

    /* connect after setting the initial selections, or we'll get unwanted calls
       to calendar_control_sensitize_calendar_commands */
    g_signal_connect (component_view->source_selector, "selection_changed",
              G_CALLBACK (source_selection_changed_cb), component_view);
    g_signal_connect (component_view->source_selector, "primary_selection_changed",
              G_CALLBACK (primary_source_selection_changed_cb), component_view);
    g_signal_connect (component_view->source_selector, "popup_event",
              G_CALLBACK (popup_event_cb), component_view);

    return component_view;
}

static void
destroy_component_view (TasksComponentView *component_view)
{
    GList *l;

    g_signal_handlers_disconnect_by_func (component_view->table, G_CALLBACK (table_selection_change_cb), component_view);
    g_signal_handlers_disconnect_matched (component_view->model, G_SIGNAL_MATCH_DATA, 0, 0, 0, 0, component_view);

    if (component_view->source_list)
        g_object_unref (component_view->source_list);

    if (component_view->source_selection)
        e_source_selector_free_selection (component_view->source_selection);

    for (l = component_view->notifications; l; l = l->next)
        calendar_config_remove_notification (GPOINTER_TO_UINT (l->data));
    g_list_free (component_view->notifications);

    g_free (component_view);
}

static void
view_destroyed_cb (gpointer data, GObject *where_the_object_was)
{
    TasksComponent *tasks_component = data;
    TasksComponentPrivate *priv;
    GList *l;

    priv = tasks_component->priv;

    for (l = priv->views; l; l = l->next) {
        TasksComponentView *component_view = l->data;

        if (G_OBJECT (component_view->view_control) == where_the_object_was) {
            priv->views = g_list_remove (priv->views, component_view);
            destroy_component_view (component_view);

            break;
        }
    }
}

static void
impl_handleURI (PortableServer_Servant servant, const char *uri, CORBA_Environment *ev)
{
    TasksComponent *tasks_component = TASKS_COMPONENT (bonobo_object_from_servant (servant));
    TasksComponentPrivate *priv;
    GList *l;
    TasksComponentView *view = NULL;

    char *src_uid = NULL;
    char *uid = NULL;
    char *rid = NULL;

    priv = tasks_component->priv;

    l = g_list_last (priv->views);
    if (!l)
        return;

    view = l->data;

    if (!strncmp (uri, "task:", 5)) {
        EUri *euri = e_uri_new (uri);
        const char *p;
        char *header, *content;
        size_t len, clen;

        p = euri->query;
        if (p) {
            while (*p) {
                len = strcspn (p, "=&");

                /* If it's malformed, give up. */
                if (p[len] != '=')
                    break;

                header = (char *) p;
                header[len] = '\0';
                p += len + 1;

                clen = strcspn (p, "&");

                content = g_strndup (p, clen);
                if (!g_ascii_strcasecmp (header, "source-uid")) {
                    src_uid = g_strdup (content);
                } else if (!g_ascii_strcasecmp (header, "comp-uid")) {
                    uid = g_strdup (content);
                } else if (!g_ascii_strcasecmp (header, "comp-rid")) {
                    rid = g_strdup (content);
                }
                g_free (content);

                p += clen;
                if (*p == '&') {
                    p++;
                    if (!strcmp (p, "amp;"))
                        p += 4;
                }
            }

            if (uid && src_uid) {
                e_tasks_open_task_id (view->tasks, src_uid, uid, rid);
            }

            g_free (src_uid);
            g_free (uid);
            g_free (rid);
        }
        e_uri_free (euri);
    }

    return;
}

static void
impl_requestCreateItem (PortableServer_Servant servant,
            const CORBA_char *item_type_name,
            CORBA_Environment *ev)
{
    TasksComponent *tasks_component = TASKS_COMPONENT (bonobo_object_from_servant (servant));

    if (strcmp (item_type_name, CREATE_TASK_ID) == 0) {
        if (!create_new_todo (tasks_component, FALSE, NULL))
            bonobo_exception_set (ev, ex_GNOME_Evolution_Component_Failed);
    } else if (strcmp (item_type_name, CREATE_TASK_ASSIGNED_ID) == 0) {
        if (!create_new_todo (tasks_component, TRUE, NULL))
            bonobo_exception_set (ev, ex_GNOME_Evolution_Component_Failed);
    } else if (strcmp (item_type_name, CREATE_TASK_LIST_ID) == 0) {
        /* FIXME Should we use the last opened window? */
        calendar_setup_new_task_list (NULL);
    } else {
        bonobo_exception_set (ev, ex_GNOME_Evolution_Component_UnknownType);
    }
}

/* GObject methods.  */

static void
impl_dispose (GObject *object)
{
    TasksComponent *tasks_component = TASKS_COMPONENT (object);
    TasksComponentPrivate *priv = tasks_component->priv;
    GList *l;

    if (priv->source_list != NULL) {
        g_object_unref (priv->source_list);
        priv->source_list = NULL;
    }
    if (priv->source_selection != NULL) {
        e_source_selector_free_selection (priv->source_selection);
        priv->source_selection = NULL;
    }

    if (priv->create_ecal) {
        g_object_unref (priv->create_ecal);
        priv->create_ecal = NULL;
    }

    for (l = priv->views; l; l = l->next) {
        TasksComponentView *component_view = l->data;

        g_object_weak_unref (G_OBJECT (component_view->view_control), view_destroyed_cb, tasks_component);
    }
    g_list_free (priv->views);
    priv->views = NULL;

    for (l = priv->notifications; l; l = l->next)
        calendar_config_remove_notification (GPOINTER_TO_UINT (l->data));
    g_list_free (priv->notifications);
    priv->notifications = NULL;

    (* G_OBJECT_CLASS (parent_class)->dispose) (object);
}

static void
impl_finalize (GObject *object)
{
    TasksComponentPrivate *priv = TASKS_COMPONENT (object)->priv;
    GList *l;

    for (l = priv->views; l; l = l->next) {
        TasksComponentView *component_view = l->data;

        destroy_component_view (component_view);
    }
    g_list_free (priv->views);

    g_free (priv);

    (* G_OBJECT_CLASS (parent_class)->finalize) (object);
}

static void
tasks_component_class_init (TasksComponentClass *klass)
{
    POA_GNOME_Evolution_Component__epv *epv = &klass->epv;
    GObjectClass *object_class = G_OBJECT_CLASS (klass);

    parent_class = g_type_class_peek_parent (klass);

    epv->requestCreateItem       = impl_requestCreateItem;
    epv->handleURI               = impl_handleURI;

    object_class->dispose = impl_dispose;
    object_class->finalize = impl_finalize;
}

static void
tasks_component_init (TasksComponent *component, TasksComponentClass *klass)
{
    TasksComponentPrivate *priv;

    priv = g_new0 (TasksComponentPrivate, 1);

    component->priv = priv;
}