Lines 44-49
Link Here
|
44 |
static int csv_parse_file(FILE *in); |
44 |
static int csv_parse_file(FILE *in); |
45 |
static int allcsv_parse_file(FILE *in); |
45 |
static int allcsv_parse_file(FILE *in); |
46 |
static int palmcsv_parse_file(FILE *in); |
46 |
static int palmcsv_parse_file(FILE *in); |
|
|
47 |
static int vcard_parse_file(FILE *in); |
47 |
|
48 |
|
48 |
/* |
49 |
/* |
49 |
* export filter prototypes |
50 |
* export filter prototypes |
Lines 75-80
Link Here
|
75 |
{ "csv", N_("comma separated values"), csv_parse_file }, |
76 |
{ "csv", N_("comma separated values"), csv_parse_file }, |
76 |
{ "allcsv", N_("comma separated values (all fields)"), allcsv_parse_file }, |
77 |
{ "allcsv", N_("comma separated values (all fields)"), allcsv_parse_file }, |
77 |
{ "palmcsv", N_("Palm comma separated values"), palmcsv_parse_file }, |
78 |
{ "palmcsv", N_("Palm comma separated values"), palmcsv_parse_file }, |
|
|
79 |
{ "vcard", N_("vCard file"), vcard_parse_file }, |
78 |
{ "\0", NULL, NULL } |
80 |
{ "\0", NULL, NULL } |
79 |
}; |
81 |
}; |
80 |
|
82 |
|
Lines 1331-1336
Link Here
|
1331 |
*/ |
1333 |
*/ |
1332 |
|
1334 |
|
1333 |
/* |
1335 |
/* |
|
|
1336 |
* vCard import filter |
1337 |
*/ |
1338 |
|
1339 |
static char *vcard_fields[] = { |
1340 |
"FN", /* NAME */ |
1341 |
"EMAIL", /* EMAIL */ |
1342 |
"ADR", /* ADDRESS */ |
1343 |
"ADR", /* ADDRESS2 - not used */ |
1344 |
"ADR", /* CITY */ |
1345 |
"ADR", /* STATE */ |
1346 |
"ADR", /* ZIP */ |
1347 |
"ADR", /* COUNTRY */ |
1348 |
"TEL", /* PHONE */ |
1349 |
"TEL", /* WORKPHONE */ |
1350 |
"TEL", /* FAX */ |
1351 |
"TEL", /* MOBILEPHONE */ |
1352 |
"NICKNAME", /* NICK */ |
1353 |
"URL", /* URL */ |
1354 |
"NOTE", /* NOTES */ |
1355 |
NULL /* not implemented: ANNIVERSARY, ITEM_FIELDS */ |
1356 |
}; |
1357 |
|
1358 |
/* |
1359 |
* mappings between vCard ADR field and abook's ADDRESS |
1360 |
* see rfc2426 section 3.2.1 |
1361 |
*/ |
1362 |
static int vcard_address_fields[] = { |
1363 |
-1, /* vCard(post office box) - not used */ |
1364 |
-1, /* vCard(the extended address) - not used */ |
1365 |
2, /* vCard(the street address) - ADDRESS */ |
1366 |
4, /* vCard(the locality) - CITY */ |
1367 |
5, /* vCard(the region) - STATE */ |
1368 |
6, /* vCard(the postal code) - ZIP */ |
1369 |
7 /* vCard(the country name) - COUNTRY */ |
1370 |
}; |
1371 |
|
1372 |
enum { |
1373 |
VCARD_KEY = 0, |
1374 |
VCARD_KEY_ATTRIBUTE, |
1375 |
VCARD_VALUE, |
1376 |
}; |
1377 |
|
1378 |
static char * |
1379 |
vcard_get_line_element(char *line, int element) |
1380 |
{ |
1381 |
int i; |
1382 |
char *line_copy = 0; |
1383 |
char *result = 0; |
1384 |
char *key = 0; |
1385 |
char *key_attr = 0; |
1386 |
char *value = 0; |
1387 |
|
1388 |
line_copy = xstrdup(line); |
1389 |
|
1390 |
/* make newline characters if exist end of string */ |
1391 |
for(i=0; line_copy[i]; i++) { |
1392 |
if(line_copy[i] == '\r' || line_copy[i] == '\n') { |
1393 |
line_copy[i] = '\0'; |
1394 |
break; |
1395 |
} |
1396 |
} |
1397 |
|
1398 |
/* separate key from value */ |
1399 |
for(i=0; line_copy[i]; i++) { |
1400 |
if(line_copy[i] == ':') { |
1401 |
line_copy[i] = '\0'; |
1402 |
key = line_copy; |
1403 |
value = &line_copy[i+1]; |
1404 |
break; |
1405 |
} |
1406 |
} |
1407 |
|
1408 |
/* separate key from key attributes */ |
1409 |
if (key) { |
1410 |
for(i=0; key[i]; i++) { |
1411 |
if(key[i] == ';') { |
1412 |
key[i] = '\0'; |
1413 |
key_attr = &key[i+1]; |
1414 |
break; |
1415 |
} |
1416 |
} |
1417 |
} |
1418 |
|
1419 |
switch(element) { |
1420 |
case VCARD_KEY: |
1421 |
if(key) |
1422 |
result = xstrdup(key); |
1423 |
break; |
1424 |
case VCARD_KEY_ATTRIBUTE: |
1425 |
if(key_attr) |
1426 |
result = xstrdup(key_attr); |
1427 |
break; |
1428 |
case VCARD_VALUE: |
1429 |
if(value) |
1430 |
result = xstrdup(value); |
1431 |
break; |
1432 |
} |
1433 |
|
1434 |
xfree(line_copy); |
1435 |
return result; |
1436 |
} |
1437 |
|
1438 |
static void |
1439 |
vcard_parse_email(list_item item, char *line) |
1440 |
{ |
1441 |
char *email; |
1442 |
|
1443 |
email = vcard_get_line_element(line, VCARD_VALUE); |
1444 |
|
1445 |
if(item[1]) { |
1446 |
item[1] = strconcat(item[1], ",", email, 0); |
1447 |
xfree(email); |
1448 |
} |
1449 |
else { |
1450 |
item[1] = email; |
1451 |
} |
1452 |
} |
1453 |
|
1454 |
static void |
1455 |
vcard_parse_address(list_item item, char *line) |
1456 |
{ |
1457 |
int i; |
1458 |
int k; |
1459 |
char *value; |
1460 |
char *address_field; |
1461 |
|
1462 |
value = vcard_get_line_element(line, VCARD_VALUE); |
1463 |
if(!value) |
1464 |
return; |
1465 |
|
1466 |
address_field = value; |
1467 |
for(i=k=0; value[i]; i++) { |
1468 |
if(value[i] == ';') { |
1469 |
value[i] = '\0'; |
1470 |
if(vcard_address_fields[k] >= 0) { |
1471 |
item[vcard_address_fields[k]] = xstrdup(address_field); |
1472 |
} |
1473 |
address_field = &value[i+1]; |
1474 |
k++; |
1475 |
if((k+1)==(sizeof(vcard_address_fields)/sizeof(*vcard_address_fields))) |
1476 |
break; |
1477 |
} |
1478 |
} |
1479 |
item[vcard_address_fields[k]] = xstrdup(address_field); |
1480 |
xfree(value); |
1481 |
} |
1482 |
|
1483 |
static void |
1484 |
vcard_parse_phone(list_item item, char *line) |
1485 |
{ |
1486 |
int index = 8; |
1487 |
char *type = vcard_get_line_element(line, VCARD_KEY_ATTRIBUTE); |
1488 |
char *value = vcard_get_line_element(line, VCARD_VALUE); |
1489 |
|
1490 |
/* set the standard number */ |
1491 |
if (!type) { |
1492 |
item[index] = value; |
1493 |
} |
1494 |
|
1495 |
/* |
1496 |
* see rfc2426 section 3.3.1 |
1497 |
*/ |
1498 |
else if (strstr(type, "TYPE=") == type){ |
1499 |
if (strcasestr(type, "home")) { |
1500 |
item[index] = xstrdup(value); |
1501 |
} |
1502 |
if (strcasestr(type, "work")) { |
1503 |
item[index+1] = xstrdup(value); |
1504 |
} |
1505 |
if (strcasestr(type, "fax")) { |
1506 |
item[index+2] = xstrdup(value); |
1507 |
} |
1508 |
if (strcasestr(type, "cell")) { |
1509 |
item[index+3] = xstrdup(value); |
1510 |
} |
1511 |
|
1512 |
xfree(type); |
1513 |
xfree(value); |
1514 |
} |
1515 |
} |
1516 |
|
1517 |
static void |
1518 |
vcard_parse_line(list_item item, char *line) |
1519 |
{ |
1520 |
int i; |
1521 |
char *key; |
1522 |
|
1523 |
for(i=0; vcard_fields[i]; i++) { |
1524 |
key = vcard_fields[i]; |
1525 |
|
1526 |
if(!strncmp(key, line, strlen(key))) { |
1527 |
if(i == 1) { |
1528 |
vcard_parse_email(item, line); |
1529 |
} |
1530 |
else if(i == 2) { |
1531 |
vcard_parse_address(item, line); |
1532 |
} |
1533 |
else if(i == 8) { |
1534 |
vcard_parse_phone(item, line); |
1535 |
} |
1536 |
else { |
1537 |
item[i] = vcard_get_line_element(line, VCARD_VALUE); |
1538 |
} |
1539 |
break; |
1540 |
} |
1541 |
} |
1542 |
} |
1543 |
|
1544 |
static void |
1545 |
vcard_parse_item(FILE *in) |
1546 |
{ |
1547 |
char *line = NULL; |
1548 |
list_item item = item_create(); |
1549 |
|
1550 |
while(!feof(in)) { |
1551 |
line = getaline(in); |
1552 |
|
1553 |
if(line && !strncmp("END:VCARD", line, 9)) { |
1554 |
xfree(line); |
1555 |
break; |
1556 |
} |
1557 |
else if(line) { |
1558 |
vcard_parse_line(item, line); |
1559 |
xfree(line); |
1560 |
} |
1561 |
} |
1562 |
|
1563 |
add_item2database(item); |
1564 |
item_free(&item); |
1565 |
} |
1566 |
|
1567 |
static int |
1568 |
vcard_parse_file(FILE *in) |
1569 |
{ |
1570 |
char *line = NULL; |
1571 |
|
1572 |
while(!feof(in)) { |
1573 |
line = getaline(in); |
1574 |
|
1575 |
if(line && !strncmp("BEGIN:VCARD", line, 11)) { |
1576 |
xfree(line); |
1577 |
vcard_parse_item(in); |
1578 |
} |
1579 |
else if(line) { |
1580 |
xfree(line); |
1581 |
} |
1582 |
} |
1583 |
|
1584 |
return 0; |
1585 |
} |
1586 |
|
1587 |
/* |
1588 |
* end of vCard import filter |
1589 |
*/ |
1590 |
|
1591 |
/* |
1334 |
* csv addressbook export filters |
1592 |
* csv addressbook export filters |
1335 |
*/ |
1593 |
*/ |
1336 |
|
1594 |
|