File: abook-completion.c

package info (click to toggle)
balsa 2.4.12-1
  • links: PTS, VCS
  • area: main
  • in suites: jessie, jessie-kfreebsd, wheezy
  • size: 21,456 kB
  • ctags: 8,303
  • sloc: ansic: 93,724; xml: 13,662; sh: 11,146; makefile: 615; awk: 60
file content (131 lines) | stat: -rw-r--r-- 3,155 bytes parent folder | download | duplicates (2)
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
/* -*-mode:c; c-style:k&r; c-basic-offset:4; -*- */
/* Balsa E-Mail Client
 *
 * Copyright (C) 1997-2009 Stuart Parmenter and others,
 *                         See the file AUTHORS for a list.
 *
 * 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, 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.
 */

#if defined(HAVE_CONFIG_H) && HAVE_CONFIG_H
# include "config.h"
#endif                          /* HAVE_CONFIG_H */
#include "abook-completion.h"

#include <string.h>

#include "information.h"
#include "misc.h"

#define CASE_INSENSITIVE_NAME

/*
 * Create a new CompletionData
 */
CompletionData *
completion_data_new(InternetAddress * ia, const gchar * nick_name)
{
    GString *string;
    gchar *address_string;
    gchar *p, *q;
#ifdef CASE_INSENSITIVE_NAME
    gchar *string_n;
#endif
    CompletionData *ret;

    ret = g_new0(CompletionData, 1);

    g_object_ref(ia);
    ret->ia = ia;

    string = g_string_new(nick_name);
    if (string->len > 0)
	g_string_append_c(string, ' ');
    address_string = internet_address_to_string(ia, FALSE);
    /* Remove '"' and '<'. */
    for (p = q = address_string; *p; p++)
        if (*p != '"' && *p != '<')
            *q++ = *p;
    *q = '\0';
    g_string_append(string, address_string);
    g_free(address_string);
#ifdef CASE_INSENSITIVE_NAME
    string_n = g_utf8_normalize(string->str, -1, G_NORMALIZE_ALL);
    g_string_free(string, TRUE);
    ret->string = g_utf8_casefold(string_n, -1);
    g_free(string_n);
#else
    ret->string = g_string_free(string, FALSE);
#endif

    return ret;
}

/*
 * Free a CompletionData
 */
void
completion_data_free(CompletionData * data)
{
    g_object_unref(data->ia);
    g_free(data->string);
    g_free(data);
}

/*
 * The LibBalsaCompletionFunc
 */
gchar *
completion_data_extract(CompletionData * data)
{
    return data->string;
}

gint
address_compare(LibBalsaAddress *a, LibBalsaAddress *b)
{
    g_return_val_if_fail(a != NULL, -1);
    g_return_val_if_fail(b != NULL, 1);

    return g_ascii_strcasecmp(a->full_name, b->full_name);
}

/*
 * A LibBalsaCompletionStrncmpFunc for matching words instead of the
 * whole string.
 *
 * s1 is the user input, s2 is the target.
 */

gint
strncmp_word(const gchar * s1, const gchar * s2, gsize n)
{
    const gchar *match;
    gint retval;

    g_return_val_if_fail(s1 != NULL, -1);
    g_return_val_if_fail(s2 != NULL, 1);

    match = s2;
    do {
	if (!(retval = strncmp(s1, match, n)))
	    break;
	if ((match = strchr(match, ' ')))
	    ++match;
    } while (match);

    return retval;
}