]> git.pld-linux.org Git - packages/abook.git/blob - abook-vcard_import.patch
- add vcard import patch to cvs
[packages/abook.git] / abook-vcard_import.patch
1 diff -ru a/filter.c b/filter.c
2 --- a/filter.c  2006-09-06 07:26:10.000000000 +0200
3 +++ b/filter.c  2008-05-18 20:55:12.000000000 +0200
4 @@ -44,6 +44,7 @@
5  static int     csv_parse_file(FILE *in);
6  static int     allcsv_parse_file(FILE *in);
7  static int     palmcsv_parse_file(FILE *in);
8 +static int     vcard_parse_file(FILE *in);
9  
10  /*
11   * export filter prototypes
12 @@ -75,6 +76,7 @@
13         { "csv", N_("comma separated values"), csv_parse_file },
14         { "allcsv", N_("comma separated values (all fields)"), allcsv_parse_file },
15         { "palmcsv", N_("Palm comma separated values"), palmcsv_parse_file },
16 +       { "vcard", N_("vCard file"), vcard_parse_file },
17         { "\0", NULL, NULL }
18  };
19  
20 @@ -1331,6 +1333,262 @@
21   */
22  
23  /*
24 + * vCard import filter
25 + */
26 +
27 +static char *vcard_fields[] = {
28 +       "FN",                   /* NAME */
29 +       "EMAIL",                /* EMAIL */
30 +       "ADR",                  /* ADDRESS */
31 +       "ADR",                  /* ADDRESS2 - not used */
32 +       "ADR",                  /* CITY */
33 +       "ADR",                  /* STATE */
34 +       "ADR",                  /* ZIP */
35 +       "ADR",                  /* COUNTRY */
36 +       "TEL",                  /* PHONE */
37 +       "TEL",                  /* WORKPHONE */
38 +       "TEL",                  /* FAX */
39 +       "TEL",                  /* MOBILEPHONE */
40 +       "NICKNAME",             /* NICK */
41 +       "URL",                  /* URL */
42 +       "NOTE",                 /* NOTES */
43 +       NULL                    /* not implemented: ANNIVERSARY, ITEM_FIELDS */
44 +};
45 +
46 +/*
47 + * mappings between vCard ADR field and abook's ADDRESS
48 + * see rfc2426 section 3.2.1
49 + */
50 +static int vcard_address_fields[] = {
51 +       -1,                     /* vCard(post office box) - not used */
52 +       -1,                     /* vCard(the extended address) - not used */
53 +       2,                      /* vCard(the street address) - ADDRESS */
54 +       4,                      /* vCard(the locality) - CITY */
55 +       5,                      /* vCard(the region) - STATE */
56 +       6,                      /* vCard(the postal code) - ZIP */
57 +       7                       /* vCard(the country name) - COUNTRY */
58 +};
59 +
60 +enum {
61 +       VCARD_KEY = 0,
62 +       VCARD_KEY_ATTRIBUTE,
63 +       VCARD_VALUE,
64 +};
65 +
66 +static char *
67 +vcard_get_line_element(char *line, int element)
68 +{
69 +       int i;
70 +       char *line_copy = 0;
71 +       char *result = 0;
72 +       char *key = 0;
73 +       char *key_attr = 0;
74 +       char *value = 0;
75 +
76 +       line_copy = xstrdup(line);
77 +
78 +       /* make newline characters if exist end of string */
79 +       for(i=0; line_copy[i]; i++) {
80 +               if(line_copy[i] == '\r' || line_copy[i] == '\n') {
81 +                       line_copy[i] = '\0';
82 +                       break;
83 +               }
84 +       }
85 +
86 +       /* separate key from value */
87 +       for(i=0; line_copy[i]; i++) {
88 +               if(line_copy[i] == ':') {
89 +                       line_copy[i] = '\0';
90 +                       key = line_copy;
91 +                       value = &line_copy[i+1];
92 +                       break;
93 +               }
94 +       }
95 +
96 +       /* separate key from key attributes */
97 +       if (key) {
98 +               for(i=0; key[i]; i++) {
99 +                       if(key[i] == ';') {
100 +                               key[i] = '\0';
101 +                               key_attr = &key[i+1];
102 +                               break;
103 +                       }
104 +               }
105 +       }
106 +
107 +       switch(element) {
108 +       case VCARD_KEY:
109 +               if(key)
110 +                       result = xstrdup(key);
111 +               break;
112 +       case VCARD_KEY_ATTRIBUTE:
113 +               if(key_attr)
114 +                       result = xstrdup(key_attr);
115 +               break;
116 +       case VCARD_VALUE:
117 +               if(value)
118 +                       result = xstrdup(value);
119 +               break;
120 +       }
121 +
122 +       xfree(line_copy);
123 +       return result;
124 +}
125 +
126 +static void
127 +vcard_parse_email(list_item item, char *line)
128 +{
129 +       char *email;
130 +
131 +       email = vcard_get_line_element(line, VCARD_VALUE);
132 +
133 +       if(item[1]) {
134 +               item[1] = strconcat(item[1], ",", email, 0);
135 +               xfree(email);
136 +       }
137 +       else {
138 +               item[1] = email;
139 +       }
140 +}
141 +
142 +static void
143 +vcard_parse_address(list_item item, char *line)
144 +{
145 +       int i;
146 +       int k;
147 +       char *value;
148 +       char *address_field;
149 +
150 +       value = vcard_get_line_element(line, VCARD_VALUE);
151 +       if(!value)
152 +               return;
153 +
154 +       address_field = value;
155 +       for(i=k=0; value[i]; i++) {
156 +               if(value[i] == ';') {
157 +                       value[i] = '\0';
158 +                       if(vcard_address_fields[k] >= 0) {
159 +                               item[vcard_address_fields[k]] = xstrdup(address_field);
160 +                       }
161 +                       address_field = &value[i+1];
162 +                       k++;
163 +                       if((k+1)==(sizeof(vcard_address_fields)/sizeof(*vcard_address_fields)))
164 +                               break;
165 +               }
166 +       }
167 +       item[vcard_address_fields[k]] = xstrdup(address_field);
168 +       xfree(value);
169 +}
170 +
171 +static void
172 +vcard_parse_phone(list_item item, char *line)
173 +{
174 +       int index = 8;
175 +       char *type = vcard_get_line_element(line, VCARD_KEY_ATTRIBUTE);
176 +       char *value = vcard_get_line_element(line, VCARD_VALUE);
177 +
178 +       /* set the standard number */
179 +       if (!type) {
180 +               item[index] = value;
181 +       }
182 +
183 +       /*
184 +        * see rfc2426 section 3.3.1
185 +        */
186 +       else if (strstr(type, "TYPE=") == type){
187 +               if (strcasestr(type, "home")) {
188 +                       item[index] = xstrdup(value);
189 +               }
190 +               if (strcasestr(type, "work")) {
191 +                       item[index+1] = xstrdup(value);
192 +               }
193 +               if (strcasestr(type, "fax")) {
194 +                       item[index+2] = xstrdup(value);
195 +               }
196 +               if (strcasestr(type, "cell")) {
197 +                       item[index+3] = xstrdup(value);
198 +               }
199 +
200 +               xfree(type);
201 +               xfree(value);
202 +       }
203 +}
204 +
205 +static void
206 +vcard_parse_line(list_item item, char *line)
207 +{
208 +       int i;
209 +       char *key;
210 +
211 +       for(i=0; vcard_fields[i]; i++) {
212 +               key = vcard_fields[i];
213 +
214 +               if(!strncmp(key, line, strlen(key))) {
215 +                       if(i == 1) {
216 +                               vcard_parse_email(item, line);
217 +                       }
218 +                       else if(i == 2) {
219 +                               vcard_parse_address(item, line);
220 +                       }
221 +                       else if(i == 8) {
222 +                               vcard_parse_phone(item, line);
223 +                       }
224 +                       else {
225 +                               item[i] = vcard_get_line_element(line, VCARD_VALUE);
226 +                       }
227 +                       break;
228 +               }
229 +       }
230 +}
231 +
232 +static void
233 +vcard_parse_item(FILE *in)
234 +{
235 +       char *line = NULL;
236 +       list_item item = item_create();
237 +
238 +       while(!feof(in)) {
239 +               line = getaline(in);
240 +
241 +               if(line && !strncmp("END:VCARD", line, 9)) {
242 +                       xfree(line);
243 +                       break;
244 +               }
245 +               else if(line) {
246 +                       vcard_parse_line(item, line);
247 +                       xfree(line);
248 +               }
249 +       }
250 +
251 +       add_item2database(item);
252 +       item_free(&item);
253 +}
254 +
255 +static int
256 +vcard_parse_file(FILE *in)
257 +{
258 +       char *line = NULL;
259 +
260 +       while(!feof(in)) {
261 +               line = getaline(in);
262 +
263 +               if(line && !strncmp("BEGIN:VCARD", line, 11)) {
264 +                       xfree(line);
265 +                       vcard_parse_item(in);
266 +               }
267 +               else if(line) {
268 +                       xfree(line);
269 +               }
270 +       }
271 +
272 +       return 0;
273 +}
274 +
275 +/*
276 + * end of vCard import filter
277 + */
278 +
279 +/*
280   * csv addressbook export filters
281   */
282  
283 diff -ru a/misc.c b/misc.c
284 --- a/misc.c    2006-09-04 21:24:18.000000000 +0200
285 +++ b/misc.c    2008-05-18 18:00:33.000000000 +0200
286 @@ -77,6 +77,27 @@
287         return 1;
288  }
289  
290 +char *
291 +strcasestr(char *haystack, char *needle)
292 +{
293 +       int i;
294 +       int k;
295 +
296 +       assert(haystack != NULL);
297 +       assert(needle != NULL);
298 +
299 +       for(i=0; i<strlen(haystack)-strlen(needle)+1; i++) {
300 +               for(k=0; k<strlen(needle); k++, i++) {
301 +                       if (tolower(haystack[i]) != tolower(needle[k]))
302 +                               break;
303 +                       else if ((k+1) == strlen(needle))
304 +                               return &haystack[i];
305 +               }
306 +       }
307 +
308 +       return NULL;
309 +}
310 +
311  
312  #ifdef HAVE_CONFIG_H
313  #      include "config.h"
314 diff -ru a/misc.h b/misc.h
315 --- a/misc.h    2006-09-04 21:24:18.000000000 +0200
316 +++ b/misc.h    2008-05-18 17:55:59.000000000 +0200
317 @@ -18,6 +18,8 @@
318  
319  int            is_number(char *s);
320  
321 +char           *strcasestr(char *haystack, char *needle);
322 +
323  char           *strdup_printf(const char *format, ... );
324  char           *strconcat(const char *str, ...);
325  
This page took 0.057537 seconds and 4 git commands to generate.