aboutsummaryrefslogtreecommitdiffstats
path: root/widgets/e-msg-composer-address-dialog.c
blob: aed0a61c9dee3f2d04fd38f794ffb549592536e4 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-msg-composer-address-dialog.c
 *
 * Copyright (C) 1999  Helix Code, Inc.
 *
 * 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.
 *
 * Author: Ettore Perazzoli
 */

#include <gnome.h>
#include "e-msg-composer-address-dialog.h"


enum {
    APPLY,
    LAST_SIGNAL
};
static guint signals[LAST_SIGNAL] = { 0 };

static GnomeDialogClass *parent_class = NULL;


/* This function should load the addresses we know of into the dialog.  We
   don't have a precise setup for the addressbook yet, so we will just put some
   fake entries in.  */
static void
load_addresses (EMsgComposerAddressDialog *dialog)
{
    gchar *text[][3] = {
        { "Bertrand Guiheneuf", "Bertrand.Guiheneuf@aful.org", NULL },
        { "Ettore Perazzoli", "ettore@gnu.org", NULL },
        { "Miguel de Icaza", "miguel@gnu.org", NULL },
        { "Nat Friedman", "nat@nat.org", NULL },
        { NULL, NULL, NULL }
    };
    GtkCList *clist;
    guint i;

    clist = GTK_CLIST (glade_xml_get_widget (dialog->gui, "address_clist"));

    for (i = 0; text[i][0] != NULL; i++)
        gtk_clist_append (clist, text[i]);
}

/* This loads the selected address in the address GtkCList into the requested
   GtkList.  */
static void
add_address (EMsgComposerAddressDialog *dialog,
         const gchar *list_name)
{
    GtkCList *src_clist;
    GtkCList *dest_clist;
    guint row;
    gchar *text[2];

    src_clist = GTK_CLIST (glade_xml_get_widget (dialog->gui, "address_clist"));
    dest_clist = GTK_CLIST (glade_xml_get_widget (dialog->gui, list_name));
    row = GPOINTER_TO_INT (src_clist->selection->data);

    gtk_clist_get_text (src_clist, row, 0, &text[0]);
    text[1] = NULL;
    gtk_clist_append (dest_clist, text);
    gtk_clist_set_row_data (dest_clist, dest_clist->rows - 1,
                GINT_TO_POINTER (row));
}


/* Signals.  */

static void
add_to_cb (GtkWidget *widget,
       gpointer data)
{
    add_address (E_MSG_COMPOSER_ADDRESS_DIALOG (data), "to_clist");
}

static void
add_cc_cb (GtkWidget *widget,
       gpointer data)
{
    add_address (E_MSG_COMPOSER_ADDRESS_DIALOG (data), "cc_clist");
}

static void
add_bcc_cb (GtkWidget *widget,
       gpointer data)
{
    add_address (E_MSG_COMPOSER_ADDRESS_DIALOG (data), "bcc_clist");
}

static void
glade_connect (GladeXML *gui,
           const gchar *widget_name,
           const gchar *signal_name,
           GtkSignalFunc callback,
           gpointer callback_data)
{
    GtkWidget *widget;

    widget = glade_xml_get_widget (gui, widget_name);
    if (widget == NULL)
        g_warning ("Widget `%s' was not found.", widget_name);
    else
        gtk_signal_connect (GTK_OBJECT (widget), signal_name,
                    GTK_SIGNAL_FUNC (callback), callback_data);
}

static void
setup_signals (EMsgComposerAddressDialog *dialog)
{
    glade_connect (dialog->gui, "to_add_button", "clicked", add_to_cb,
               dialog);
    glade_connect (dialog->gui, "cc_add_button", "clicked", add_cc_cb,
               dialog);
    glade_connect (dialog->gui, "bcc_add_button", "clicked", add_bcc_cb,
               dialog);
}


/* GtkObject methods.  */

static void
destroy (GtkObject *object)
{
    EMsgComposerAddressDialog *dialog;
    GtkCList *address_clist;
    GList *p;

    dialog = E_MSG_COMPOSER_ADDRESS_DIALOG (object);

    gtk_object_unref (GTK_OBJECT (dialog->gui));

    if (GTK_OBJECT_CLASS (parent_class)->destroy != NULL)
        (* GTK_OBJECT_CLASS (parent_class)->destroy) (object);
}


/* Initialization.  */

static void
class_init (EMsgComposerAddressDialogClass *class)
{
    GtkObjectClass *object_class;

    object_class = GTK_OBJECT_CLASS (class);
    object_class->destroy = destroy;

    parent_class = gtk_type_class (gnome_dialog_get_type ());

    signals[APPLY]
        = gtk_signal_new ("apply",
                  GTK_RUN_FIRST,
                  object_class->type,
                  GTK_SIGNAL_OFFSET (EMsgComposerAddressDialogClass,
                             apply),
                  gtk_marshal_NONE__NONE,
                  GTK_TYPE_NONE, 0);

    gtk_object_class_add_signals (object_class, signals, LAST_SIGNAL);
}

static void
init (EMsgComposerAddressDialog *dialog)
{
    dialog->gui = NULL;
}


GtkType
e_msg_composer_address_dialog_get_type (void)
{
    static GtkType type = 0;

    if (type == 0) {
        static const GtkTypeInfo info = {
            "EMsgComposerAddressDialog",
            sizeof (EMsgComposerAddressDialog),
            sizeof (EMsgComposerAddressDialogClass),
            (GtkClassInitFunc) class_init,
            (GtkObjectInitFunc) init,
            /* reserved_1 */ NULL,
            /* reserved_2 */ NULL,
            (GtkClassInitFunc) NULL,
        };

        type = gtk_type_unique (gnome_dialog_get_type (), &info);
    }

    return type;
}

void
e_msg_composer_address_dialog_construct (EMsgComposerAddressDialog *dialog)
{
    static const gchar *buttons[] = {
        GNOME_STOCK_BUTTON_OK,
        GNOME_STOCK_BUTTON_APPLY,
        GNOME_STOCK_BUTTON_CANCEL,
        NULL
    };

    g_return_if_fail (dialog != NULL);
    g_return_if_fail (E_IS_MSG_COMPOSER_ADDRESS_DIALOG (dialog));

    gnome_dialog_constructv (GNOME_DIALOG (dialog),
                 _("Select recipients' addresses"),
                 buttons);

    dialog->gui = glade_xml_new
        (E_GUIDIR "/e-msg-composer-address-dialog.glade", "main_table");
    if (dialog->gui == NULL) {
        g_warning ("Cannot load `e-msg-composer-address-dialog.glade");
        return;
    }

    gtk_container_add (GTK_CONTAINER (GNOME_DIALOG (dialog)->vbox),
               glade_xml_get_widget (dialog->gui, "main_table"));

    load_addresses (dialog);
    setup_signals (dialog);
}

GtkWidget *
e_msg_composer_address_dialog_new (void)
{
    EMsgComposerAddressDialog *new;

    new = gtk_type_new (e_msg_composer_address_dialog_get_type ());
    e_msg_composer_address_dialog_construct (new);

    return GTK_WIDGET (new);
}


static gchar *
make_full_address (const gchar *name,
           const gchar *email)
{
    /* FIXME handle quoting.  */

    return g_strconcat (name, " <", email, ">", NULL);
}

static GList *
get_list (EMsgComposerAddressDialog *dialog,
      const gchar *clist_name)
{
    GtkCList *address_clist;
    GtkCList *clist;
    GList *list;
    guint i;

    address_clist = GTK_CLIST (glade_xml_get_widget (dialog->gui,
                             "address_clist"));
    clist = GTK_CLIST (glade_xml_get_widget (dialog->gui, clist_name));

    list = NULL;
    for (i = 0; i < clist->rows; i++) {
        gchar *name, *email;
        guint addr_row;

        addr_row = GPOINTER_TO_INT (gtk_clist_get_row_data (clist, i));
        gtk_clist_get_text (clist, addr_row, 0, &name);
        gtk_clist_get_text (clist, addr_row, 0, &email);

        list = g_list_prepend (list, make_full_address (name, email));
    }

    return g_list_reverse (list);
}

GList *
e_msg_composer_address_dialog_get_to_list (EMsgComposerAddressDialog *dialog)
{
    g_return_val_if_fail (dialog != NULL, NULL);
    g_return_val_if_fail (E_IS_MSG_COMPOSER_ADDRESS_DIALOG (dialog), NULL);

    return get_list (dialog, "to_clist");
}

GList *
e_msg_composer_address_dialog_get_cc_list (EMsgComposerAddressDialog *dialog)
{
    g_return_val_if_fail (dialog != NULL, NULL);
    g_return_val_if_fail (E_IS_MSG_COMPOSER_ADDRESS_DIALOG (dialog), NULL);

    return get_list (dialog, "cc_clist");
}

GList *
e_msg_composer_address_dialog_get_bcc_list (EMsgComposerAddressDialog *dialog)
{
    g_return_val_if_fail (dialog != NULL, NULL);
    g_return_val_if_fail (E_IS_MSG_COMPOSER_ADDRESS_DIALOG (dialog), NULL);

    return get_list (dialog, "bcc_clist");
}