aboutsummaryrefslogtreecommitdiffstats
path: root/addressbook/util/eab-book-util.c
blob: 2c80d6f8a1601e79bf09745eacc43725c4f7f0b3 (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
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */

/*
 * eab-util.c
 *
 * Copyright (C) 2001-2003 Ximian, Inc.
 *
 * Authors: Jon Trowbridge <trow@ximian.com>
 *          Chris Toshok <toshok@ximian.com>
 */

/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of version 2 of the GNU General Public
 * License as published by the Free Software Foundation.
 * 
 * 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 "eab-book-util.h"

#include <string.h>
#include <glib.h>
#include <glib-object.h>
#include <e-util/e-config-listener.h>

EConfigListener *
eab_get_config_database ()
{
    static EConfigListener *config_db;

    if (config_db == NULL)
        config_db = e_config_listener_new ();

    return config_db;
}

/*
 *
 * Specialized Queries
 *
 */

guint
eab_name_and_email_query (EBook *book,
              const gchar *name,
              const gchar *email,
              EBookContactsCallback cb,
              gpointer closure)
{
    gchar *email_query=NULL, *name_query=NULL, *query;
    guint tag;

    g_return_val_if_fail (book && E_IS_BOOK (book), 0);
    g_return_val_if_fail (cb != NULL, 0);

    if (name && !*name)
        name = NULL;
    if (email && !*email)
        email = NULL;

    if (name == NULL && email == NULL)
        return 0;

    /* Build our e-mail query.
     * We only query against the username part of the address, to avoid not matching
     * fred@foo.com and fred@mail.foo.com.  While their may be namespace collisions
     * in the usernames of everyone out there, it shouldn't be that bad.  (Famous last words.)
     */
    if (email) {
        const gchar *t = email;
        while (*t && *t != '@')
            ++t;
        if (*t == '@') {
            email_query = g_strdup_printf ("(beginswith \"email\" \"%.*s@\")", t-email, email);

        } else {
            email_query = g_strdup_printf ("(beginswith \"email\" \"%s\")", email);
        }
    }

    /* Build our name query.
     * We only do name-query stuff if we don't have an e-mail address.  Our basic assumption
     * is that the username part of the email is good enough to keep the amount of stuff returned
     * in the query relatively small.
     */
    if (name && !email)
        name_query = g_strdup_printf ("(or (beginswith \"file_as\" \"%s\") (beginswith \"full_name\" \"%s\"))", name, name);

    /* Assemble our e-mail & name queries */
    if (email_query && name_query) {
        query = g_strdup_printf ("(and %s %s)", email_query, name_query);
    } else if (email_query) {
        query = email_query;
        email_query = NULL;
    } else if (name_query) {
        query = name_query;
        name_query = NULL;
    } else
        return 0;

    tag = e_book_async_get_contacts (book, query, cb, closure);

    g_free (email_query);
    g_free (name_query);
    g_free (query);

    return tag;
}

/*
 * Simple nickname query
 */
guint
eab_nickname_query (EBook                 *book,
            const char            *nickname,
            EBookContactsCallback  cb,
            gpointer               closure)
{
    gchar *query;
    guint retval;

    g_return_val_if_fail (E_IS_BOOK (book), 0);
    g_return_val_if_fail (nickname != NULL, 0);

    /* The empty-string case shouldn't generate a warning. */
    if (! *nickname)
        return 0;

    query = g_strdup_printf ("(is \"nickname\" \"%s\")", nickname);

    retval = e_book_async_get_contacts (book, query, cb, closure);

    g_free (query);

    return retval;
}

GList*
eab_contact_list_from_string (const char *str)
{
    GList *contacts = NULL;
    GString *gstr = g_string_new ("");
    char *p = (char*)str;
    char *q;
    char *blank_line;

    while (*p) {
        if (*p != '\r') g_string_append_c (gstr, *p);
        
        p++;
    }

    p = g_string_free (gstr, FALSE);
    q = p;
    do {
        char *temp;

        blank_line = strstr (q, "\n\n");
        if (blank_line) {
            temp = g_strndup (q, blank_line - q);
        }
        else {
            temp = g_strdup (q);
        }

        contacts = g_list_append (contacts, e_contact_new_from_vcard (temp));

        g_free (temp);

        if (blank_line)
            q = blank_line + 2;
        else
            q = NULL;
    } while (blank_line);

    g_free (p);

    return contacts;
}

char*
eab_contact_list_to_string (GList *contacts)
{
    GString *str = g_string_new ("");
    GList *l;

    for (l = contacts; l; l = l->next) {
        EContact *contact = l->data;
        char *vcard_str = e_vcard_to_string (E_VCARD (contact), EVC_FORMAT_VCARD_30);

        g_string_append (str, vcard_str);
        if (l->next)
            g_string_append (str, "\r\n");
    }

    return g_string_free (str, FALSE);
}

#if notyet
/*
 *  Convenience routine to check for addresses in the local address book.
 */

typedef struct _HaveAddressInfo HaveAddressInfo;
struct _HaveAddressInfo {
    gchar *email;
    EBookHaveAddressCallback cb;
    gpointer closure;
};

static void
have_address_query_cb (EBook *book, EBookSimpleQueryStatus status, const GList *contacts, gpointer closure)
{
    HaveAddressInfo *info = (HaveAddressInfo *) closure;
    
    info->cb (book, 
          info->email,
          contacts && (status == E_BOOK_ERROR_OK) ? E_CONTACT (contacts->data) : NULL,
          info->closure);

    g_free (info->email);
    g_free (info);
}

static void
have_address_book_open_cb (EBook *book, gpointer closure)
{
    HaveAddressInfo *info = (HaveAddressInfo *) closure;

    if (book) {

        e_book_name_and_email_query (book, NULL, info->email, have_address_query_cb, info);

    } else {

        info->cb (NULL, info->email, NULL, info->closure);

        g_free (info->email);
        g_free (info);

    }
}

void
eab_query_address_default (const gchar *email,
               EABHaveAddressCallback cb,
               gpointer closure)
{
    HaveAddressInfo *info;

    g_return_if_fail (email != NULL);
    g_return_if_fail (cb != NULL);

    info = g_new0 (HaveAddressInfo, 1);
    info->email = g_strdup (email);
    info->cb = cb;
    info->closure = closure;

    e_book_use_default_book (have_address_book_open_cb, info);
}
#endif

/* bad place for this i know. */
int
e_utf8_casefold_collate_len (const gchar *str1, const gchar *str2, int len)
{
    gchar *s1 = g_utf8_casefold(str1, len);
    gchar *s2 = g_utf8_casefold(str2, len);
    int rv;

    rv = g_utf8_collate (s1, s2);

    g_free (s1);
    g_free (s2);

    return rv;
}

int
e_utf8_casefold_collate (const gchar *str1, const gchar *str2)
{
    return e_utf8_casefold_collate_len (str1, str2, -1);
}