aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/gui/component/select-names/e-select-names-popup.c
blob: ef73d79e8ae4d768c79b7207c02ee20c1951a8f0 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 * e-select-names-popup.c
 *
 * Copyright (C) 2001 Ximian, Inc.
 *
 * Developed by Jon Trowbridge <trow@ximian.com>
 */

/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 * 
 * 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.
 */

#include <config.h>

#include <stdio.h>
#include <string.h>

#include <glib.h>
#include <gtk/gtkcheckmenuitem.h>
#include <libgnome/gnome-defs.h>
#include <libgnome/gnome-i18n.h>
#include <libgnomeui/gnome-app.h>
#include <libgnomeui/gnome-app-helper.h>
#include <libgnomeui/gnome-popup-menu.h>
#include <gal/widgets/e-unicode.h>

#include <addressbook/backend/ebook/e-book-util.h>
#include <addressbook/gui/contact-editor/e-contact-editor.h>
#include <addressbook/gui/contact-editor/e-contact-quick-add.h>
#include "e-addressbook-util.h"
#include "e-select-names-popup.h"

typedef struct _PopupInfo PopupInfo;
struct _PopupInfo {
    ESelectNamesModel *model;
    const EDestination *dest;
    gint pos;
    gint index;
};

static PopupInfo *
popup_info_new (ESelectNamesModel *model, const EDestination *dest, gint pos, gint index)
{
    PopupInfo *info = g_new0 (PopupInfo, 1);
    info->model = model;
    info->dest = dest;
    info->pos = pos;
    info->index = index;

    if (model)
        gtk_object_ref (GTK_OBJECT (model));

    if (dest)
        gtk_object_ref (GTK_OBJECT (dest));

    return info;
}

static void
popup_info_free (PopupInfo *info)
{
    if (info) {
        
        if (info->model)
            gtk_object_unref (GTK_OBJECT (info->model));

        if (info->dest)
            gtk_object_unref (GTK_OBJECT (info->dest));

        g_free (info);
    }
}

static void
popup_info_cleanup (GtkWidget *w, gpointer info)
{
    popup_info_free ((PopupInfo *) info);
}

/* You are in a maze of twisty little callbacks, all alike... */

static void
make_contact_editor_cb (EBook *book, gpointer user_data)
{
    if (book) {
        EDestination *dest = E_DESTINATION (user_data);
        EContactEditor *ce;
        ECard *card;

        card = (ECard *) e_destination_get_card (dest);
        ce = e_addressbook_show_contact_editor (book, card, FALSE, TRUE);
        e_contact_editor_raise (ce);
        gtk_object_unref (GTK_OBJECT (dest));
    }
}

static void
edit_contact_info_cb (GtkWidget *w, gpointer user_data)
{
    PopupInfo *info = (PopupInfo *) user_data;
    if (info == NULL)
        return;

    gtk_object_ref (GTK_OBJECT (info->dest));
    e_book_use_local_address_book (make_contact_editor_cb, (gpointer) info->dest);
}

static void
change_email_num_cb (GtkWidget *w, gpointer user_data)
{
    PopupInfo *info = (PopupInfo *) user_data;
    gint n;
    EDestination *dest;
    
    if (info == NULL) 
        return;

    if (! GTK_CHECK_MENU_ITEM (w)->active)
        return;

    n = GPOINTER_TO_INT (gtk_object_get_data (GTK_OBJECT (w), "number"));

    if (n != e_destination_get_email_num (info->dest)) {
        dest = e_destination_new ();
        e_destination_set_card (dest, e_destination_get_card (info->dest), n);
        e_select_names_model_replace (info->model, info->index, dest);

    }
}

static void
remove_recipient_cb (GtkWidget *w, gpointer user_data)
{
    PopupInfo *info = (PopupInfo *) user_data;
    e_select_names_model_delete (info->model, info->index);
}

static void
add_remove_recipient (GnomeUIInfo *uiinfo, PopupInfo *info)
{
    uiinfo->type = GNOME_APP_UI_ITEM;
    uiinfo->label = _("Remove");
    uiinfo->moreinfo = remove_recipient_cb;
}

static void
remove_all_recipients_cb (GtkWidget *w, gpointer user_data)
{
    PopupInfo *info = (PopupInfo *) user_data;
    e_select_names_model_delete_all (info->model);
}

static void
add_remove_all_recipients (GnomeUIInfo *uiinfo, PopupInfo *info)
{
    uiinfo->type = GNOME_APP_UI_ITEM;
    uiinfo->label = _("Remove All");
    uiinfo->moreinfo = remove_all_recipients_cb;
}

static void
toggle_html_mail_cb (GtkWidget *w, gpointer user_data)
{
    PopupInfo *info = (PopupInfo *) user_data;
    GtkCheckMenuItem *item = GTK_CHECK_MENU_ITEM (w);
    const EDestination *dest;

    if (info == NULL)
        return;

    dest = info->dest;

    item = GTK_CHECK_MENU_ITEM (item);
    e_destination_set_html_mail_pref ((EDestination *) dest, item->active);
}

static void
add_html_mail (GnomeUIInfo *uiinfo, PopupInfo *info)
{
    uiinfo->type = GNOME_APP_UI_TOGGLEITEM;
    uiinfo->label = _("Send HTML Mail?");
    uiinfo->moreinfo = toggle_html_mail_cb;
}

static void
init_html_mail (GnomeUIInfo *uiinfo, PopupInfo *info)
{
    gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (uiinfo->widget),
                    e_destination_get_html_mail_pref (info->dest));
    gtk_check_menu_item_set_show_toggle (GTK_CHECK_MENU_ITEM (uiinfo->widget), TRUE);

}

/* Duplicate the string, mapping _ to __.  This is to make sure that underscores in
   e-mail addresses don't get mistaken for keyboard accelerators. */
static gchar *
quote_label (const gchar *str)
{
    gint len = str ? strlen (str) : -1;
    const gchar *c = str;
    gchar *d, *q;

    if (len < 0)
        return NULL;

    while (*c) {
        if (*c == '_')
            ++len;
        ++c;
    }

    q = g_new (gchar, len+1);
    c = str;
    d = q;
    while (*c) {
        *d = *c;
        if (*c == '_') {
            ++d;
            *d = '_';
        }
        ++c;
        ++d;
    }
    *d = '\0';
    return q;
}

#define ARBITRARY_UIINFO_LIMIT 64
static GtkWidget *
popup_menu_card (PopupInfo *info)
{
    GnomeUIInfo uiinfo[ARBITRARY_UIINFO_LIMIT];
    GnomeUIInfo radioinfo[ARBITRARY_UIINFO_LIMIT];
    gboolean using_radio = FALSE;
    ECard *card;
    gint i=0;
    GtkWidget *pop, *label;
    GList *item_children;
    EIterator *iterator;
    gint html_toggle;
    gchar *name_label, *quoted_name_label;

    /*
     * Build up our GnomeUIInfo array.
     */

    memset (uiinfo, 0, sizeof (uiinfo));
    memset (radioinfo, 0, sizeof (radioinfo));

    card = e_destination_get_card (info->dest);

    /* Use an empty label for now, we'll fill it later.
       If we set uiinfo label to contact name here, gnome_popup_menu_new
       could screw it up trying make a "translation". */
    uiinfo[i].type = GNOME_APP_UI_ITEM;
    uiinfo[i].label = "";
    ++i;

    uiinfo[i].type = GNOME_APP_UI_SEPARATOR;
    ++i;

    if (e_list_length (card->email) > 1) {
        gint j = 0;

        using_radio = TRUE;

        iterator = e_list_get_iterator (card->email);
        for (e_iterator_reset (iterator); e_iterator_is_valid (iterator); e_iterator_next (iterator)) {
            gchar *label = (gchar *)e_iterator_get (iterator);
            
            if (label && *label) {
                /* Magically convert embedded XML into an address. */
                if (!strncmp (label, "<?xml", 4)) {
                    EDestination *dest = e_destination_import (label);
                    radioinfo[j].label = g_strdup (e_destination_get_address (dest));
                    gtk_object_unref (GTK_OBJECT (dest));
                    
                }
                else {
                    radioinfo[j].label = g_strdup (label);
                }

                radioinfo[j].type = GNOME_APP_UI_ITEM;
                radioinfo[j].moreinfo = change_email_num_cb;
                ++j;
            }
        }

        radioinfo[j].type = GNOME_APP_UI_ENDOFINFO;

        uiinfo[i].type = GNOME_APP_UI_RADIOITEMS;
        uiinfo[i].moreinfo = radioinfo;
        ++i;

    } else {
        uiinfo[i].type = GNOME_APP_UI_ITEM;
        uiinfo[i].label = (gchar *) e_destination_get_email (info->dest);
        ++i;
    }

    uiinfo[i].type = GNOME_APP_UI_SEPARATOR;
    ++i;

    add_html_mail (&(uiinfo[i]), info);
    html_toggle = i;
    ++i;

    uiinfo[i].type = GNOME_APP_UI_ITEM;
    uiinfo[i].label = N_("Edit Contact Info");
    uiinfo[i].moreinfo = edit_contact_info_cb;
    ++i;

    add_remove_recipient (&(uiinfo[i]), info);
    ++i;
    
    add_remove_all_recipients (&(uiinfo[i]), info);
    ++i;
        
    uiinfo[i].type = GNOME_APP_UI_ENDOFINFO;

    /*
     * Now do something with it...
     */

    pop = gnome_popup_menu_new (uiinfo);

    if (using_radio) {
        gint n = e_destination_get_email_num (info->dest);
        gint j;
        for (j=0; radioinfo[j].type != GNOME_APP_UI_ENDOFINFO; ++j) {
            gtk_object_set_data (GTK_OBJECT (radioinfo[j].widget), "number", GINT_TO_POINTER (j));
            g_free (radioinfo[j].label);
            if (j == n) 
                gtk_check_menu_item_set_active (GTK_CHECK_MENU_ITEM (radioinfo[n].widget), TRUE);
        }
    }

    init_html_mail (&(uiinfo[html_toggle]), info);

    /* Now set label of the first item to contact name */
    name_label = e_utf8_to_locale_string (e_destination_get_name (info->dest));
    quoted_name_label = quote_label (name_label);
    item_children = gtk_container_children (GTK_CONTAINER (uiinfo[0].widget));
    label = item_children->data;
    g_list_free (item_children);
    gtk_label_set_text (GTK_LABEL (label), quoted_name_label);
    g_free (name_label);
    g_free (quoted_name_label);

    return pop;
}

static void
quick_add_cb (GtkWidget *w, gpointer user_data)
{
    PopupInfo *info = (PopupInfo *) user_data;
    e_contact_quick_add_free_form (e_destination_get_address (info->dest), NULL, NULL);
}

static GtkWidget *
popup_menu_nocard (PopupInfo *info)
{
    GnomeUIInfo uiinfo[ARBITRARY_UIINFO_LIMIT];
    gint i=0;
    GtkWidget *pop, *label;
    GList *item_children;
    const gchar *str;
    gchar *name_label, *quoted_name_label;
    gint html_toggle;

    memset (uiinfo, 0, sizeof (uiinfo));

    /* Use an empty label for now, we'll fill it later.
       If we set uiinfo label to contact name here, gnome_popup_menu_new
       could screw it up trying make a "translation". */
    uiinfo[i].type = GNOME_APP_UI_ITEM;
    uiinfo[i].label = "";
    ++i;

    uiinfo[i].type = GNOME_APP_UI_SEPARATOR;
    ++i;

    add_html_mail (&(uiinfo[i]), info);
    html_toggle = i;
    ++i;
    
    uiinfo[i].type = GNOME_APP_UI_ITEM;
    uiinfo[i].label = _("Add to Contacts");
    uiinfo[i].moreinfo = quick_add_cb;
    ++i;

    add_remove_recipient (&(uiinfo[i]), info);
    ++i;

    add_remove_all_recipients (&(uiinfo[i]), info);
    ++i;

    uiinfo[i].type = GNOME_APP_UI_ENDOFINFO;

    pop = gnome_popup_menu_new (uiinfo);

    init_html_mail (&(uiinfo[html_toggle]), info);

    /* Now set label of the first item to contact name */
    str = e_destination_get_name (info->dest);
    if (str == NULL)
        str = e_destination_get_email (info->dest);
    if (str != NULL) {
        name_label = e_utf8_to_locale_string (str);
    } else {
        name_label = g_strdup (_("Unnamed Contact"));
    }
    quoted_name_label = quote_label (name_label);
    item_children = gtk_container_children (GTK_CONTAINER (uiinfo[0].widget));
    label = item_children->data;
    g_list_free (item_children);
    gtk_label_set_text (GTK_LABEL (label), quoted_name_label);
    g_free (name_label);
    g_free (quoted_name_label);
    
    return pop;
}

void
e_select_names_popup (ESelectNamesModel *model, GdkEventButton *ev, gint pos)
{
    GtkWidget *popup;
    PopupInfo *info;
    const EDestination *dest;
    ECard *card;
    gint index;

    g_return_if_fail (model && E_IS_SELECT_NAMES_MODEL (model));
    g_return_if_fail (ev);
    g_return_if_fail (0 <= pos);

    e_select_names_model_text_pos (model, pos, &index, NULL, NULL);
    if (index < 0 || index >= e_select_names_model_count (model))
        return;

    dest = e_select_names_model_get_destination (model, index);
    if (e_destination_is_empty (dest))
        return;

    card = e_destination_get_card (dest);

    info = popup_info_new (model, dest, pos, index);

    popup = card ? popup_menu_card (info) : popup_menu_nocard (info);

    if (popup) {
        /* Clean up our info item after we've made our selection. */
        gtk_signal_connect (GTK_OBJECT (popup),
                    "selection-done",
                    GTK_SIGNAL_FUNC (popup_info_cleanup),
                    info);

        gnome_popup_menu_do_popup (popup, NULL, NULL, ev, info);

    } else {

        popup_info_free (info);

    }
}