aboutsummaryrefslogtreecommitdiffstats
path: root/composer/e-msg-composer-address-entry.c
blob: ce946d203e913a5fef23da11dd9047912011a994 (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
/* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
/* e-msg-composer-address-entry.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
 */

/* This is a custom GtkEntry for entering address lists.  For now, it does not
   have any fancy features, but in the future we might want to make it
   cooler.  */

#include <gnome.h>

#include "e-msg-composer-address-entry.h"


static GtkEntryClass *parent_class = NULL;


/* Initialization.  */

static void
class_init (EMsgComposerAddressEntryClass *klass)
{
    GtkObjectClass *object_class;

    object_class = (GtkObjectClass*) klass;

    parent_class = gtk_type_class (gtk_entry_get_type ());
}

static void
init (EMsgComposerAddressEntry *msg_composer_address_entry)
{
}

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

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

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

    return type;
}


GtkWidget *
e_msg_composer_address_entry_new (void)
{
    GtkWidget *new;

    new = gtk_type_new (e_msg_composer_address_entry_get_type ());

    return new;
}


/**
 * e_msg_composer_address_entry_get_addresses:
 * @entry: An address entry widget
 * 
 * Retrieve the list of addresses stored in @entry.
 * 
 * Return value: A GList of pointers to strings representing the addresses.
 * Notice that the strings must be freed by the caller when not needed anymore.
 **/
GList *
e_msg_composer_address_entry_get_addresses (EMsgComposerAddressEntry *entry)
{
    GList *list;
    const gchar *s;
    const gchar *p, *oldp;
    gboolean in_quotes;

    s = gtk_entry_get_text (GTK_ENTRY (entry));

    in_quotes = FALSE;
    list = NULL;

    p = s;
    oldp = s;

    while (1) {
        if (*p == '"') {
            in_quotes = ! in_quotes;
            p++;
        } else if ((! in_quotes && *p == ',') || *p == 0) {
            if (p != oldp) {
                gchar *new_addr;

                new_addr = g_strndup (oldp, p - oldp);
                new_addr = g_strstrip (new_addr);
                if (*new_addr != '\0')
                    list = g_list_prepend (list, new_addr);
                else
                    g_free (new_addr);
            }

            while (*p == ',' || *p == ' ' || *p == '\t')
                p++;

            if (*p == 0)
                break;

            oldp = p;
        } else {
            p++;
        }
    }

    return g_list_reverse (list);
}

/**
 * e_msg_composer_address_entry_set_list:
 * @entry: An address entry
 * @list: List of pointers to strings representing the addresses that must
 * appear in the entry
 * 
 * Set the address list from @list.
 **/
void
e_msg_composer_address_entry_set_list (EMsgComposerAddressEntry *entry,
                       const GList *list)
{
    GString *string;
    const GList *p;

    g_return_if_fail (entry != NULL);

    if (list == NULL) {
        gtk_editable_delete_text (GTK_EDITABLE (entry), -1, -1);
        return;
    }

    string = g_string_new (NULL);
    for (p = list; p != NULL; p = p->next) {
        if (string->str[0] != '\0')
            g_string_append (string, ", ");
        g_string_append (string, p->data);
    }

    gtk_entry_set_text (GTK_ENTRY (entry), string->str);
    g_string_free (string, TRUE);
}