aboutsummaryrefslogtreecommitdiffstats
path: root/calendar/gui/dialogs/memo-page.c
blob: 57332a1e0ca2063b931eccc74fe7dd6b06bce35b (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
generated by cgit  (git 2.34.1) at 2025-08-07 16:38:00 +0800
 


3935; font-weight:bold">g_strdup (strip);

            if (itip_organizer_is_user (registry, comp, client) ||
                itip_sentby_is_user (registry, comp, client)) {
                gtk_entry_set_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (priv->org_combo))), string);
            } else {
                GtkComboBox *combo_box;
                GtkListStore *list_store;
                GtkTreeModel *model;
                GtkTreeIter iter;

                combo_box = GTK_COMBO_BOX (priv->org_combo);
                model = gtk_combo_box_get_model (combo_box);
                list_store = GTK_LIST_STORE (model);

                gtk_list_store_clear (list_store);
                gtk_list_store_append (list_store, &iter);
                gtk_list_store_set (list_store, &iter, 0, string, -1);
                gtk_combo_box_set_active (combo_box, 0);
                gtk_editable_set_editable (GTK_EDITABLE (gtk_bin_get_child (GTK_BIN (priv->org_combo))), FALSE);
            }
            g_free (string);
        }
    }

    if (backend_addr)
        g_free (backend_addr);

    /* Source */
    e_source_combo_box_set_active (
        E_SOURCE_COMBO_BOX (priv->source_combo_box),
        e_client_get_source (E_CLIENT (client)));

    if (priv->to_entry && (flags & COMP_EDITOR_IS_SHARED) && !(flags & COMP_EDITOR_NEW_ITEM))
        gtk_entry_set_text (GTK_ENTRY (priv->to_entry), get_recipients (comp));

    sensitize_widgets (mpage);

    return TRUE;
}

static void
memo_page_class_init (MemoPageClass *class)
{
    CompEditorPageClass *editor_page_class;
    GObjectClass *object_class;

    g_type_class_add_private (class, sizeof (MemoPagePrivate));

    object_class = G_OBJECT_CLASS (class);
    object_class->dispose = memo_page_dispose;
    object_class->finalize = memo_page_finalize;

    editor_page_class = COMP_EDITOR_PAGE_CLASS (class);
    editor_page_class->get_widget = memo_page_get_widget;
    editor_page_class->focus_main_widget = memo_page_focus_main_widget;
    editor_page_class->fill_widgets = memo_page_fill_widgets;
    editor_page_class->fill_component = memo_page_fill_component;
}

static void
memo_page_init (MemoPage *mpage)
{
    mpage->priv = MEMO_PAGE_GET_PRIVATE (mpage);
}

/* returns whether changed info text */
static gboolean
check_starts_in_the_past (MemoPage *mpage)
{
    MemoPagePrivate *priv;
    struct icaltimetype start_tt = icaltime_null_time ();

    if ((comp_editor_get_flags (comp_editor_page_get_editor (COMP_EDITOR_PAGE (mpage))) & COMP_EDITOR_NEW_ITEM) == 0)
        return FALSE;

    priv = mpage->priv;

    start_tt.is_date = TRUE;
    if (e_date_edit_get_date (E_DATE_EDIT (priv->start_date), &start_tt.year, &start_tt.month, &start_tt.day) &&
        comp_editor_test_time_in_the_past (start_tt)) {
        gchar *tmp = g_strconcat ("<b>", _("Memo's start date is in the past"), "</b>", NULL);
        memo_page_set_info_string (mpage, GTK_STOCK_DIALOG_WARNING, tmp);
        g_free (tmp);
    } else {
        memo_page_set_info_string (mpage, NULL, NULL);
    }

    return TRUE;
}

static void
sensitize_widgets (MemoPage *mpage)
{
    GtkActionGroup *action_group;
    gboolean read_only, sens = FALSE, sensitize;
    CompEditor *editor;
    CompEditorFlags flags;
    MemoPagePrivate *priv;
    ECalClient *client;

    priv = mpage->priv;

    editor = comp_editor_page_get_editor (COMP_EDITOR_PAGE (mpage));
    client = comp_editor_get_client (editor);
    flags = comp_editor_get_flags (editor);

    read_only = e_client_is_readonly (E_CLIENT (client));

    if (flags & COMP_EDITOR_IS_SHARED)
        sens = flags & COMP_EDITOR_USER_ORG;
    else
        sens = TRUE;

    sensitize = (!read_only && sens);

    if (read_only) {
        gchar *tmp = g_strconcat ("<b>", _("Memo cannot be edited, because the selected memo list is read only"), "</b>", NULL);
        memo_page_set_info_string (mpage, GTK_STOCK_DIALOG_INFO, tmp);
        g_free (tmp);
    } else if (!sens) {
        gchar *tmp = g_strconcat ("<b>", _("Memo cannot be fully edited, because you are not the organizer"), "</b>", NULL);
        memo_page_set_info_string (mpage, GTK_STOCK_DIALOG_INFO, tmp);
        g_free (tmp);
    } else if (!check_starts_in_the_past (mpage)) {
        memo_page_set_info_string (mpage, NULL, NULL);
    }

    /* The list of organizers is set to be non-editable. Otherwise any
    * change in the displayed list causes an 'Account not found' error.
    */
    gtk_editable_set_editable (GTK_EDITABLE (gtk_bin_get_child (GTK_BIN (priv->org_combo))), FALSE);

    gtk_text_view_set_editable (GTK_TEXT_VIEW (priv->memo_content), sensitize);
    gtk_widget_set_sensitive (priv->start_date, sensitize);
    gtk_widget_set_sensitive (priv->categories_btn, !read_only);
    gtk_editable_set_editable (GTK_EDITABLE (priv->categories), !read_only);
    gtk_editable_set_editable (GTK_EDITABLE (priv->summary_entry), sensitize);

    if (flags & COMP_EDITOR_IS_SHARED) {
        if (priv->to_entry) {
            gtk_editable_set_editable (GTK_EDITABLE (priv->to_entry), !read_only);
            gtk_widget_grab_focus (priv->to_entry);
        }
    }

    action_group = comp_editor_get_action_group (editor, "editable");
    gtk_action_group_set_sensitive (action_group, !read_only);

    action_group = comp_editor_get_action_group (editor, "individual");
    gtk_action_group_set_sensitive (action_group, sensitize);
}

/* returns empty string rather than NULL because of simplicity of usage */
static const gchar *
get_recipients (ECalComponent *comp)
{
    icalcomponent *icalcomp;
    icalproperty *icalprop;

    g_return_val_if_fail (comp != NULL, "");

    icalcomp = e_cal_component_get_icalcomponent (comp);

    /* first look if we have there such property */
    for (icalprop = icalcomponent_get_first_property (icalcomp, ICAL_X_PROPERTY);
         icalprop;
         icalprop = icalcomponent_get_next_property (icalcomp, ICAL_X_PROPERTY)) {
        const gchar *xname = icalproperty_get_x_name (icalprop);

        if (xname && strcmp (xname, "X-EVOLUTION-RECIPIENTS") == 0)
            break;
    }

    if (icalprop)
        return icalproperty_get_x (icalprop);

    return "";
}

static gboolean
fill_comp_with_recipients (ENameSelector *name_selector,
                           ECalComponent *comp)
{
    EDestinationStore *destination_store;
    GString *str = NULL;
    GList *l, *destinations;
    ENameSelectorModel *name_selector_model = e_name_selector_peek_model (name_selector);
    icalcomponent *icalcomp;
    icalproperty *icalprop;

    e_name_selector_model_peek_section (name_selector_model, "To",
                        NULL, &destination_store);

    destinations = e_destination_store_list_destinations (destination_store);
    for (l = destinations; l; l = g_list_next (l)) {
        EDestination *destination = l->data, *des = NULL;
        const GList *list_dests = NULL, *l;
        GList card_dest;

        if (e_destination_is_evolution_list (destination)) {
            list_dests = e_destination_list_get_dests (destination);
        } else {
            EContact *contact = e_destination_get_contact (destination);
            /* check if the contact is contact list which is not expanded yet */
            /* we expand it by getting the list again from the server forming the query */
            if (contact && e_contact_get (contact , E_CONTACT_IS_LIST)) {
                EBookClient *book_client = NULL;
                ENameSelectorDialog *dialog;
                ENameSelectorModel *model;
                EContactStore *c_store;
                GSList *clients, *l;
                gchar *uid = e_contact_get (contact, E_CONTACT_BOOK_UID);

                dialog = e_name_selector_peek_dialog (name_selector);
                model = e_name_selector_dialog_peek_model (dialog);
                c_store = e_name_selector_model_peek_contact_store (model);
                clients = e_contact_store_get_clients (c_store);

                for (l = clients; l; l = l->next) {
                    EBookClient *b = l->data;
                    ESource *source;

                    source = e_client_get_source (E_CLIENT (b));

                    if (g_strcmp0 (uid, e_source_get_uid (source)) == 0) {
                        book_client = b;
                        break;
                    }
                }

                if (book_client) {
                    GSList *contacts = NULL;
                    EContact *n_con = NULL;
                    gchar *query;

                    query = g_strdup_printf ("(is \"full_name\" \"%s\")",
                            (gchar *) e_contact_get (contact, E_CONTACT_FULL_NAME));

                    if (!e_book_client_get_contacts_sync (book_client, query, &contacts, NULL, NULL)) {
                        g_warning ("Could not get contact from the book \n");
                    } else {
                        des = e_destination_new ();
                        n_con = contacts->data;

                        e_destination_set_contact (des, n_con, 0);
                        e_destination_set_client (des, book_client);
                        list_dests = e_destination_list_get_dests (des);

                        g_slist_foreach (contacts, (GFunc) g_object_unref, NULL);
                        g_slist_free (contacts);
                    }

                    g_free (query);
                }
                g_slist_free (clients);
            } else {
                card_dest.next = NULL;
                card_dest.prev = NULL;
                card_dest.data = destination;
                list_dests = &card_dest;
            }
        }

        for (l = list_dests; l; l = l->next) {
            EDestination *dest = l->data;
            const gchar *attendee = NULL;

            /* If we couldn't get the attendee prior,
             * get the email address as the default. */
            if (attendee == NULL || *attendee == '\0')
                attendee = e_destination_get_email (dest);

            if (attendee == NULL || *attendee == '\0')
                continue;

            if (!str) {
                str = g_string_new (NULL);
                g_string_prepend (str, attendee);
                continue;
            }
            g_string_prepend_c (str, ';');
            g_string_prepend (str, attendee);
        }
    }

    g_list_free (destinations);

    if (str && *str->str) {
        icalcomp = e_cal_component_get_icalcomponent (comp);
        icalprop = icalproperty_new_x (str->str);
        icalproperty_set_x_name (icalprop, "X-EVOLUTION-RECIPIENTS");
        icalcomponent_add_property (icalcomp, icalprop);

        g_string_free (str, FALSE);
        return TRUE;
    } else
        return FALSE;
}

/* fill_component handler for the memo page */
static gboolean
memo_page_fill_component (CompEditorPage *page,
                          ECalComponent *comp)
{
    MemoPage *mpage;
    MemoPagePrivate *priv;
    CompEditor *editor;
    CompEditorFlags flags;
    ECalClient *client;
    ECalComponentClassification classification;
    ECalComponentDateTime start_date;
    struct icaltimetype start_tt;
    gchar *cat, *str;
    gint i;
    GtkTextBuffer *text_buffer;
    GtkTextIter text_iter_start, text_iter_end;

    mpage = MEMO_PAGE (page);
    priv = mpage->priv;

    editor = comp_editor_page_get_editor (page);
    client = comp_editor_get_client (editor);
    flags = comp_editor_get_flags (editor);

    text_buffer = gtk_text_view_get_buffer (GTK_TEXT_VIEW (priv->memo_content));

    /* Summary */
    str = gtk_editable_get_chars (GTK_EDITABLE (priv->summary_entry), 0, -1);
    if (str == NULL || *str == '\0')
        e_cal_component_set_summary (comp, NULL);
    else {
        ECalComponentText text;

        text.value = str;
        text.altrep = NULL;

        e_cal_component_set_summary (comp, &text);
    }

    g_free (str);
    str = NULL;

    /* Memo Content */

    gtk_text_buffer_get_start_iter (text_buffer, &text_iter_start);
    gtk_text_buffer_get_end_iter   (text_buffer, &text_iter_end);
    str = gtk_text_buffer_get_text (text_buffer, &text_iter_start, &text_iter_end, FALSE);

    if (!str || strlen (str) == 0) {
        e_cal_component_set_description_list (comp, NULL);
    }
    else {
        GSList l;
        ECalComponentText text;
        gchar *p;
        gunichar uc;

        for (i = 0, p = str, uc = g_utf8_get_char_validated (p, -1);
            i < 50 && p && uc < (gunichar) - 2;
            i++, p = g_utf8_next_char (p), uc = g_utf8_get_char_validated (p, -1)) {
            if (uc == '\n' || !uc) {
                p = NULL;
                break;
            }
        }

        text.value = str;
        text.altrep = NULL;
        l.data = &text;
        l.next = NULL;

        e_cal_component_set_description_list (comp, &l);
    }

    g_free (str);

    /* Dates */
    start_tt = icaltime_null_time ();
    start_tt.is_date = 1;
    start_date.value = &start_tt;
    start_date.tzid = NULL;

    if (!e_date_edit_date_is_valid (E_DATE_EDIT (priv->start_date))) {
        comp_editor_page_display_validation_error (page, _("Start date is wrong"), priv->start_date);
        return FALSE;
    }
    if (e_date_edit_get_date (E_DATE_EDIT (priv->start_date),
                           &start_tt.year,
                           &start_tt.month,
                           &start_tt.day))
        e_cal_component_set_dtstart (comp, &start_date);
    else
        e_cal_component_set_dtstart (comp, NULL);

    /* Classification. */
    classification = comp_editor_get_classification (editor);
    e_cal_component_set_classification (comp, classification);

    /* Categories */
    cat = gtk_editable_get_chars (GTK_EDITABLE (priv->categories), 0, -1);
    str = comp_editor_strip_categories (cat);
    g_free (cat);

    e_cal_component_set_categories (comp, str);

    g_free (str);

    /* change recipients only when creating new item, after that no such action is available */
    if ((flags & COMP_EDITOR_IS_SHARED) && (flags & COMP_EDITOR_NEW_ITEM) && fill_comp_with_recipients (priv->name_selector, comp)) {
        ECalComponentOrganizer organizer = {NULL, NULL, NULL, NULL};

        gchar *backend_addr = NULL;
        gchar *backend_mailto = NULL;
        gchar *name = NULL;
        gchar *mailto = NULL;

        e_client_get_backend_property_sync (E_CLIENT (client), CAL_BACKEND_PROPERTY_CAL_EMAIL_ADDRESS, &backend_addr, NULL, NULL);

        /* Find the identity for the organizer or sentby field */
        if (!get_current_identity (mpage, &name, &mailto)) {
            e_notice (
                priv->main, GTK_MESSAGE_ERROR,
                _("An organizer is required."));
            return FALSE;
        }

        /* Prefer the backend addres if we have one. */
        if (backend_addr != NULL && *backend_addr != '\0') {
            backend_mailto = g_strdup_printf (
                "MAILTO:%s", backend_addr);
            if (g_ascii_strcasecmp (backend_mailto, mailto) == 0) {
                g_free (backend_mailto);
                backend_mailto = NULL;
            }
        }

        if (backend_mailto == NULL) {
            organizer.cn = name;
            organizer.value = mailto;
            name = mailto = NULL;
        } else {
            organizer.value = backend_mailto;
            organizer.sentby = mailto;
            backend_mailto = mailto = NULL;
        }

        e_cal_component_set_organizer (comp, &organizer);

        if (flags & COMP_EDITOR_NEW_ITEM)
            comp_editor_set_needs_send (editor, TRUE);

        g_free (backend_addr);
        g_free (backend_mailto);
        g_free (name);
        g_free (mailto);
    }

    return TRUE;
}

void
memo_page_set_show_categories (MemoPage *page,
                               gboolean state)
{
    if (state) {
        gtk_widget_show (page->priv->categories_btn);
        gtk_widget_show (page->priv->categories);
    } else {
        gtk_widget_hide (page->priv->categories_btn);
        gtk_widget_hide (page->priv->categories);
    }
}

/*If the msg has some value set, the icon should always be set */
void
memo_page_set_info_string (MemoPage *mpage,
                           const gchar *icon,
                           const gchar *msg)
{
    MemoPagePrivate *priv;

    priv = mpage->priv;

    gtk_image_set_from_stock (GTK_IMAGE (priv->info_icon), icon, GTK_ICON_SIZE_BUTTON);
    gtk_label_set_markup (GTK_LABEL (priv->info_string), msg);

    if (msg && icon)
        gtk_widget_show (priv->info_hbox);
    else
        gtk_widget_hide (priv->info_hbox);
}

/* Gets the widgets from the XML file and returns if they are all available. */
static gboolean
get_widgets (MemoPage *mpage)
{
    EShell *shell;
    ESourceRegistry *registry;
    CompEditor *editor;
    CompEditorPage *page = COMP_EDITOR_PAGE (mpage);
    GtkEntryCompletion *completion;
    MemoPagePrivate *priv;
    GSList *accel_groups;
    GtkWidget *toplevel;
    GtkWidget *parent;

    priv = mpage->priv;

#define GW(name) e_builder_get_widget (priv->builder, name)

    editor = comp_editor_page_get_editor (page);
    shell = comp_editor_get_shell (editor);
    registry = e_shell_get_registry (shell);

    priv->main = GW ("memo-page");
    if (!priv->main) {
        g_warning ("couldn't find memo-page!");
        return FALSE;
    }

    /* Get the GtkAccelGroup from the toplevel window, so we can install
     * it when the notebook page is mapped. */
    toplevel = gtk_widget_get_toplevel (priv->main);
    accel_groups = gtk_accel_groups_from_object (G_OBJECT (toplevel));
    if (accel_groups)
        page->accel_group = g_object_ref (accel_groups->data);

    g_object_ref (priv->main);
    parent = gtk_widget_get_parent (priv->main);
    gtk_container_remove (GTK_CONTAINER (parent), priv->main);

    priv->info_hbox = GW ("generic-info");
    priv->info_icon = GW ("generic-info-image");
    priv->info_string = GW ("generic-info-msgs");

    priv->org_label = GW ("org-label");
    priv->org_combo = GW ("org-combo");
    gtk_list_store_clear (GTK_LIST_STORE (gtk_combo_box_get_model (GTK_COMBO_BOX (priv->org_combo))));
    gtk_combo_box_set_entry_text_column (GTK_COMBO_BOX (priv->org_combo), 0);

    priv->to_button = GW ("to-button");
    priv->to_hbox = GW ("to-hbox");

    priv->summary_label = GW ("sum-label");
    priv->summary_entry = GW ("sum-entry");

    priv->start_label = GW ("start-label");
    priv->start_date = GW ("start-date");

    priv->memo_content = GW ("memo_content");

    priv->categories_btn = GW ("categories-button");
    priv->categories = GW ("categories");

    priv->source_combo_box = GW ("source");
    e_source_combo_box_set_registry (
        E_SOURCE_COMBO_BOX (priv->source_combo_box), registry);
#undef GW

    completion = e_category_completion_new ();
    gtk_entry_set_completion (GTK_ENTRY (priv->categories), completion);
    g_object_unref (completion);

    if (priv->summary_entry) {
        EShell *shell;
        EShellSettings *shell_settings;
        CompEditor *editor;

        editor = comp_editor_page_get_editor (page);
        shell = comp_editor_get_shell (editor);
        shell_settings = e_shell_get_shell_settings (shell);

        g_object_bind_property (
            shell_settings, "composer-inline-spelling",
            priv->summary_entry, "checking-enabled",
            G_BINDING_SYNC_CREATE);
    }

    return (priv->memo_content
        && priv->categories_btn
        && priv->categories
        && priv->start_date);
}

/* Callback used when the categories button is clicked; we must bring up the
 * category list dialog.
 */
static void
categories_clicked_cb (GtkWidget *button,
                       MemoPage *mpage)
{
    GtkEntry *entry;

    entry = GTK_ENTRY (mpage->priv->categories);
    e_categories_config_open_dialog_for_entry (entry);
}

static void
mpage_client_opened_cb (GObject *source_object,
                        GAsyncResult *result,
                        gpointer user_data)
{
    ESource *source = E_SOURCE (source_object);
    EClient *client = NULL;
    MemoPage *mpage = user_data;
    MemoPagePrivate *priv;
    CompEditor *editor;
    GError *error = NULL;

    if (!e_client_utils_open_new_finish (source, result, &client, &error)) {
        if (g_error_matches (error, E_CLIENT_ERROR, E_CLIENT_ERROR_CANCELLED) ||
            g_error_matches (error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) {
            g_clear_error (&error);
            return;
        }
    }

    editor = comp_editor_page_get_editor (COMP_EDITOR_PAGE (mpage));
    priv = mpage->priv;

    if (error) {
        GtkWidget *dialog;
        ECalClient *old_client;

        old_client = comp_editor_get_client (editor);

        e_source_combo_box_set_active (
            E_SOURCE_COMBO_BOX (priv->source_combo_box),
            e_client_get_source (E_CLIENT (old_client)));

        dialog = gtk_message_dialog_new (NULL, GTK_DIALOG_MODAL,
                         GTK_MESSAGE_WARNING, GTK_BUTTONS_OK,
                         _("Unable to open memos in '%s': %s"),
                         e_source_get_display_name (source),
                         error ? error->message : _("Unknown error"));
        gtk_dialog_run (GTK_DIALOG (dialog));
        gtk_widget_destroy (dialog);

        g_clear_error (&error);
    } else {
        icaltimezone *zone;
        CompEditorFlags flags;
        ECalClient *cal_client = E_CAL_CLIENT (client);

        g_return_if_fail (cal_client != NULL);

        flags = comp_editor_get_flags (editor);
        zone = comp_editor_get_timezone (editor);
        e_cal_client_set_default_timezone (cal_client, zone);

        comp_editor_set_client (editor, cal_client);

        if (client) {
            gchar *backend_addr = NULL;

            e_client_get_backend_property_sync (client, CAL_BACKEND_PROPERTY_CAL_EMAIL_ADDRESS, &backend_addr, NULL, NULL);

            if (flags & COMP_EDITOR_IS_SHARED)
                memo_page_select_organizer (mpage, backend_addr);

            set_subscriber_info_string (mpage, backend_addr);
            g_free (backend_addr);
        }

        sensitize_widgets (mpage);
    }
}

static void
source_changed_cb (ESourceComboBox *source_combo_box,
                   MemoPage *mpage)
{
    MemoPagePrivate *priv = mpage->priv;
    ESource *source;

    if (comp_editor_page_get_updating (COMP_EDITOR_PAGE (mpage)))
        return;

    source = e_source_combo_box_ref_active (source_combo_box);
    g_return_if_fail (source != NULL);

    if (priv->open_cancellable) {
        g_cancellable_cancel (priv->open_cancellable);
        g_object_unref (priv->open_cancellable);
    }
    priv->open_cancellable = g_cancellable_new ();

    e_client_utils_open_new (
        source, E_CLIENT_SOURCE_TYPE_MEMOS,
        FALSE, priv->open_cancellable,
        mpage_client_opened_cb, mpage);

    g_object_unref (source);
}

static void
set_subscriber_info_string (MemoPage *mpage,
                            const gchar *backend_address)
{
    if (!check_starts_in_the_past (mpage))
        memo_page_set_info_string (mpage, NULL, NULL);
}

static void
summary_changed_cb (GtkEntry *entry,
                    CompEditorPage *page)
{
    CompEditor *editor;
    const gchar *text;

    if (comp_editor_page_get_updating (page))
        return;

    editor = comp_editor_page_get_editor (page);
    text = gtk_entry_get_text (entry);
    comp_editor_set_summary (editor, text);
}

static void
to_button_clicked_cb (GtkButton *button,
                      MemoPage *mpage)
{
    e_name_selector_show_dialog (mpage->priv->name_selector,
                     mpage->priv->main);
}

static void
memo_page_start_date_changed_cb (MemoPage *mpage)
{
    check_starts_in_the_past (mpage);
    comp_editor_page_changed (COMP_EDITOR_PAGE (mpage));
}

/* Hooks the widget signals */
static gboolean
init_widgets (MemoPage *mpage)
{
    CompEditor *editor;
    MemoPagePrivate *priv = mpage->priv;
    GtkTextBuffer *buffer;
    GtkTextView *view;
    GtkAction *action;
    gboolean active;

    editor = comp_editor_page_get_editor (COMP_EDITOR_PAGE (mpage));

    /* Generic informative messages */
    gtk_widget_hide (priv->info_hbox);

    /* Summary */
    g_signal_connect (
        priv->summary_entry, "changed",
        G_CALLBACK (summary_changed_cb), mpage);

    /* Memo Content */
    view = GTK_TEXT_VIEW (priv->memo_content);
    buffer = gtk_text_view_get_buffer (view);
    gtk_text_view_set_wrap_mode (view, GTK_WRAP_WORD);
    e_buffer_tagger_connect (view);

    /* Categories button */
    g_signal_connect (
        priv->categories_btn, "clicked",
        G_CALLBACK (categories_clicked_cb), mpage);

    /* Source selector */
    g_signal_connect (
        priv->source_combo_box, "changed",
        G_CALLBACK (source_changed_cb), mpage);

    /* Connect the default signal handler to use to make sure the "changed"
     * field gets set whenever a field is changed. */

    /* Belongs to priv->memo_content */
    g_signal_connect_swapped (
        buffer, "changed",
        G_CALLBACK (comp_editor_page_changed), mpage);

    g_signal_connect_swapped (
        priv->categories, "changed",
        G_CALLBACK (comp_editor_page_changed), mpage);

    g_signal_connect_swapped (
        priv->summary_entry, "changed",
        G_CALLBACK (comp_editor_page_changed), mpage);

    g_signal_connect_swapped (
        priv->source_combo_box, "changed",
        G_CALLBACK (comp_editor_page_changed), mpage);

    g_signal_connect_swapped (
        priv->start_date, "changed",
        G_CALLBACK (memo_page_start_date_changed_cb), mpage);

    if (priv->name_selector) {
        ENameSelectorDialog *name_selector_dialog;

        name_selector_dialog = e_name_selector_peek_dialog (priv->name_selector);

        g_signal_connect (
            name_selector_dialog, "response",
            G_CALLBACK (gtk_widget_hide), NULL);
        g_signal_connect (
            priv->to_button, "clicked",
            G_CALLBACK (to_button_clicked_cb), mpage);
        g_signal_connect_swapped (
            priv->to_entry, "changed",
            G_CALLBACK (comp_editor_page_changed), mpage);
    }

    action = comp_editor_get_action (editor, "view-categories");
    active = gtk_toggle_action_get_active (GTK_TOGGLE_ACTION (action));
    memo_page_set_show_categories (mpage, active);

    return TRUE;
}

static GtkWidget *
get_to_entry (ENameSelector *name_selector)
{
    ENameSelectorModel *name_selector_model;
    ENameSelectorEntry *name_selector_entry;

    name_selector_model = e_name_selector_peek_model (name_selector);
    e_name_selector_model_add_section (name_selector_model, "To", _("To"), NULL);
    name_selector_entry = (ENameSelectorEntry *) e_name_selector_peek_section_list (name_selector, "To");

    return GTK_WIDGET (name_selector_entry);
}

static void
memo_page_select_organizer (MemoPage *mpage,
                            const gchar *backend_address)
{
    MemoPagePrivate *priv = mpage->priv;
    CompEditor *editor;
    CompEditorFlags flags;
    const gchar *default_address;
    gint ii;

    /* Treat an empty backend address as NULL. */
    if (backend_address != NULL && *backend_address == '\0')
        backend_address = NULL;

    editor = comp_editor_page_get_editor (COMP_EDITOR_PAGE (mpage));
    flags = comp_editor_get_flags (editor);

    default_address = priv->fallback_address;

    if (backend_address != NULL) {
        for (ii = 0; priv->address_strings[ii] != NULL; ii++) {
            if (g_strrstr (priv->address_strings[ii], backend_address) != NULL) {
                default_address = priv->address_strings[ii];
                break;
            }
        }
    }

    if (default_address != NULL) {
        if (flags & COMP_EDITOR_NEW_ITEM) {
            gtk_entry_set_text (GTK_ENTRY (gtk_bin_get_child (GTK_BIN (priv->org_combo))), default_address);
        }
    } else
        g_warning ("No potential organizers!");
}

/**
 * memo_page_construct:
 * @mpage: An memo page.
 *
 * Constructs an memo page by loading its Glade data.
 *
 * Return value: The same object as @mpage, or NULL if the widgets could not be
 * created.
 **/
MemoPage *
memo_page_construct (MemoPage *mpage)
{
    MemoPagePrivate *priv;
    EShell *shell;
    CompEditor *editor;
    CompEditorFlags flags;
    ESourceRegistry *registry;

    priv = mpage->priv;

    editor = comp_editor_page_get_editor (COMP_EDITOR_PAGE (mpage));

    flags = comp_editor_get_flags (editor);
    shell = comp_editor_get_shell (editor);

    registry = e_shell_get_registry (shell);

    /* Make sure our custom widget classes are registered with
     * GType before we load the GtkBuilder definition file. */
    E_TYPE_DATE_EDIT;
    E_TYPE_SPELL_ENTRY;

    priv->builder = gtk_builder_new ();
    e_load_ui_builder_definition (priv->builder, "memo-page.ui");

    if (!get_widgets (mpage)) {
        g_message ("memo_page_construct(): "
               "Could not find all widgets in the XML file!");
        return NULL;
    }

    if (flags & COMP_EDITOR_IS_SHARED) {
        GtkComboBox *combo_box;
        GtkListStore *list_store;
        GtkTreeModel *model;
        GtkTreeIter iter;
        gint ii;

        combo_box = GTK_COMBO_BOX (priv->org_combo);
        model = gtk_combo_box_get_model (combo_box);
        list_store = GTK_LIST_STORE (model);

        priv->address_strings = itip_get_user_identities (registry);
        priv->fallback_address = itip_get_fallback_identity (registry);

        /* FIXME Could we just use a GtkComboBoxText? */
        for (ii = 0; priv->address_strings[ii] != NULL; ii++) {
            gtk_list_store_append (list_store, &iter);
            gtk_list_store_set (
                list_store, &iter,
                0, priv->address_strings[ii], -1);
        }

        gtk_combo_box_set_active (combo_box, 0);

        gtk_widget_show (priv->org_label);
        gtk_widget_show (priv->org_combo);

        priv->name_selector = e_name_selector_new (registry);
        priv->to_entry = get_to_entry (priv->name_selector);
        gtk_container_add ((GtkContainer *) priv->to_hbox, priv->to_entry);
        gtk_widget_show (priv->to_hbox);
        gtk_widget_show (priv->to_entry);
        gtk_widget_show (priv->to_button);

        if (!(flags & COMP_EDITOR_NEW_ITEM)) {
            gtk_widget_set_sensitive (priv->to_button, FALSE);
            gtk_widget_set_sensitive (priv->to_entry, FALSE);
        }
    }

    if (!init_widgets (mpage)) {
        g_message ("memo_page_construct(): "
               "Could not initialize the widgets!");
        return NULL;
    }

    return mpage;
}

/**
 * memo_page_new:
 *
 * Creates a new memo page.
 *
 * Return value: A newly-created task page, or NULL if the page could
 * not be created.
 **/
MemoPage *
memo_page_new (CompEditor *editor)
{
    MemoPage *mpage;

    g_return_val_if_fail (IS_COMP_EDITOR (editor), NULL);

    mpage = g_object_new (TYPE_MEMO_PAGE, "editor", editor, NULL);

    if (!memo_page_construct (mpage)) {
        g_object_unref (mpage);
        g_return_val_if_reached (NULL);
    }

    return mpage;
}