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 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593
|
/*
$Id: vcal.c,v 1.20 2007/11/07 18:28:18 pkot Exp $
G N O K I I
A Linux/Unix toolset and driver for the mobile phones.
This file is part of gnokii.
Gnokii 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.
Gnokii 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 gnokii; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
Copyright (C) 2004 Martin Goldhahn
Copyright (C) 2004 Pawel Kot, BORBELY Zoltan
*/
#include "config.h"
#include <string.h>
#include "compat.h"
#include "misc.h"
#include "gnokii.h"
#include "gsm-filetypes.h"
/* We have to do this here since libical defines its own VERSION macro.
* Define it unconditionally to have nicer code below. */
static const char gnokii_version[] = VERSION;
#ifdef HAVE_LIBICAL
/* needs gnokii-internal.h for string_base64() and utf8_encode() */
# include "gnokii-internal.h"
# include "ical.h"
/* mother's little helpers */
static int iterate_cal(icalcomponent *parent, int depth, int *count, icalcomponent **result, icalcomponent_kind kind);
static const char *comp_get_category(icalcomponent *comp);
/* This is a wrapper for fgets. icalparser_parse() requires a function
* argument with different signature than fgets(). */
static char *ical_fgets(char *s, size_t size, void *d)
{
return fgets(s, size, (FILE *)d);
}
#endif /* HAVE_LIBICAL */
/* This string is used so often that we keep it as long as the program is
* running. Due to VERSION being defined in both libical and gnokii we need
* to us a static string instead of a pre-compiler macro.
*/
static char vcal_prodid[64] = {0};
static inline const char *get_prodid()
{
if (!*vcal_prodid) {
snprintf(vcal_prodid, sizeof(vcal_prodid), "//Gnokii.org//NONSGML v%s", gnokii_version);
}
return vcal_prodid;
}
/*
ICALENDAR Reading functions
*/
GNOKII_API int gn_calnote2ical(FILE *f, gn_calnote *calnote)
{
#ifdef HAVE_LIBICAL
# define MAX_PROP_INDEX 5
icalcomponent *pIcal = NULL;
struct icaltimetype stime = {0}, etime = {0}/*, atime = {0}*/;
icalproperty *properties[MAX_PROP_INDEX+1] = {0}; /* order and number of properties vary */
int iprop = 0;
char compuid[64];
/* In at least some Nokia phones it is possible to skip the year of
birth, in this case year == 0xffff, so we set it to the arbitrary
value of 1800 */
stime.year = (calnote->time.year == 0xffff ? 1800 : calnote->time.year);
stime.month = calnote->time.month;
stime.day = calnote->time.day;
stime.hour = calnote->time.hour;
stime.minute = calnote->time.minute;
stime.second = calnote->time.second;
stime.is_daylight = 1;
if (calnote->end_time.year) {
etime.year = (calnote->end_time.year == 0xffff ? 1800 : calnote->end_time.year);
etime.month = calnote->end_time.month;
etime.day = calnote->end_time.day;
etime.hour = calnote->end_time.hour;
etime.minute = calnote->end_time.minute;
etime.second = calnote->end_time.second;
etime.is_daylight = 1;
properties[iprop++] = icalproperty_new_dtend(etime);
}
/* FIXME: how to set alarm?
if (calnote->alarm.enabled) {
atime.year = (calnote->alarm.timestamp.year == 0xffff ? 1800 : calnote->alarm.timestamp.year);
atime.month = calnote->alarm.timestamp.month;
atime.day = calnote->alarm.timestamp.day;
atime.hour = calnote->alarm.timestamp.hour;
atime.minute = calnote->alarm.timestamp.minute;
atime.second = calnote->alarm.timestamp.second;
atime.is_daylight = 1;
properties[iprop++] = icalproperty_new_FIXME(atime);
}
*/
/* TODO: should the strings be configurable? */
switch(calnote->type) {
case GN_CALNOTE_MEMO:
properties[iprop++] = icalproperty_new_categories("MISCELLANEOUS");
properties[iprop++] = icalproperty_new_summary(calnote->text);
break;
case GN_CALNOTE_REMINDER:
properties[iprop++] = icalproperty_new_categories("REMINDER");
properties[iprop++] = icalproperty_new_summary(calnote->text);
break;
case GN_CALNOTE_CALL:
properties[iprop++] = icalproperty_new_categories("PHONE CALL");
properties[iprop++] = icalproperty_new_summary(calnote->phone_number);
properties[iprop++] = icalproperty_new_description(calnote->text);
break;
case GN_CALNOTE_MEETING:
properties[iprop++] = icalproperty_new_categories("MEETING");
properties[iprop++] = icalproperty_new_summary(calnote->text);
if (calnote->mlocation[0])
properties[iprop++] = icalproperty_new_location(calnote->mlocation);
break;
case GN_CALNOTE_BIRTHDAY:
properties[iprop++] = icalproperty_new_categories("ANNIVERSARY");
properties[iprop++] = icalproperty_new_summary(calnote->text);
do {
char rrule[64];
snprintf(rrule, sizeof(rrule), "FREQ=YEARLY;INTERVAL=1;BYMONTH=%d", stime.month);
properties[iprop++] = icalproperty_new_rrule(icalrecurrencetype_from_string(rrule));
} while (0);
stime.is_date = 1;
calnote->recurrence = GN_CALNOTE_YEARLY;
break;
default:
properties[iprop++] = icalproperty_new_categories("UNKNOWN");
properties[iprop++] = icalproperty_new_summary(calnote->text);
break;
}
snprintf(compuid, sizeof(compuid), "guid.gnokii.org_%d_%d", calnote->location, rand());
pIcal = icalcomponent_vanew(ICAL_VCALENDAR_COMPONENT,
icalproperty_new_version("2.0"),
icalproperty_new_prodid(get_prodid()),
icalcomponent_vanew(ICAL_VEVENT_COMPONENT,
icalproperty_new_dtstart(stime),
icalproperty_new_uid(compuid),
icalproperty_new_categories("GNOKII"),
properties[0],
properties[1],
properties[2],
properties[3],
properties[4],
0),
0);
if (pIcal) {
char *icalstrbuf = NULL;
const char *icalstr = icalcomponent_as_ical_string(pIcal);
/* conversion to UTF-8 */
if (string_base64(icalstr)) {
int icalstrlen = strlen(icalstr);
icalstrbuf = malloc(icalstrlen * 2 + 1);
utf8_encode(icalstrbuf, icalstrlen * 2, icalstr, icalstrlen);
icalstr = icalstrbuf;
}
fprintf(f, "%s\n", icalstr);
dprintf("%s\n", icalstr);
if (icalstrbuf)
free(icalstrbuf);
icalcomponent_free(pIcal);
pIcal = NULL;
} else {
dprintf("ERROR in icalcomponent_vanew()\n");
}
return GN_ERR_NONE;
#else
fprintf(f, "BEGIN:VCALENDAR\r\n");
fprintf(f, "VERSION:1.0\r\n");
fprintf(f, "BEGIN:VEVENT\r\n");
fprintf(f, "CATEGORIES:");
switch (calnote->type) {
case GN_CALNOTE_MEMO:
fprintf(f, "MISCELLANEOUS\r\n");
break;
case GN_CALNOTE_REMINDER:
fprintf(f, "REMINDER\r\n");
break;
case GN_CALNOTE_CALL:
fprintf(f, "PHONE CALL\r\n");
fprintf(f, "SUMMARY:%s\r\n", calnote->phone_number);
fprintf(f, "DESCRIPTION:%s\r\n", calnote->text);
break;
case GN_CALNOTE_MEETING:
fprintf(f, "MEETING\r\n");
if (calnote->mlocation[0])
fprintf(f, "LOCATION:%s\r\n", calnote->mlocation);
break;
case GN_CALNOTE_BIRTHDAY:
fprintf(f, "SPECIAL OCCASION\r\n");
break;
default:
fprintf(f, "UNKNOWN\r\n");
break;
}
if (calnote->type != GN_CALNOTE_CALL)
fprintf(f, "SUMMARY:%s\r\n", calnote->text);
fprintf(f, "DTSTART:%04d%02d%02dT%02d%02d%02d\r\n", calnote->time.year,
calnote->time.month, calnote->time.day, calnote->time.hour,
calnote->time.minute, calnote->time.second);
if (calnote->end_time.year) {
fprintf(f, "DTEND:%04d%02d%02dT%02d%02d%02d\r\n", calnote->end_time.year,
calnote->end_time.month, calnote->end_time.day, calnote->end_time.hour,
calnote->end_time.minute, calnote->end_time.second);
}
if (calnote->alarm.enabled) {
fprintf(f, "%sALARM:%04d%02d%02dT%02d%02d%02d\r\n",
(calnote->alarm.tone ? "A" : "D"),
calnote->alarm.timestamp.year,
calnote->alarm.timestamp.month, calnote->alarm.timestamp.day, calnote->alarm.timestamp.hour,
calnote->alarm.timestamp.minute, calnote->alarm.timestamp.second);
}
switch (calnote->recurrence) {
case GN_CALNOTE_NEVER:
break;
case GN_CALNOTE_DAILY:
calnote->occurrences ?
fprintf(f, "RRULE:FREQ=DAILY\r\n") :
fprintf(f, "RRULE:FREQ=DAILY;COUNT=%d\r\n", calnote->occurrences);
break;
case GN_CALNOTE_WEEKLY:
calnote->occurrences ?
fprintf(f, "RRULE:FREQ=WEEKLY\r\n") :
fprintf(f, "RRULE:FREQ=WEEKLY;COUNT=%d\r\n", calnote->occurrences);
break;
case GN_CALNOTE_2WEEKLY:
calnote->occurrences ?
fprintf(f, "RRULE:FREQ=WEEKLY;INTERVAL=2\r\n") :
fprintf(f, "RRULE:FREQ=WEEKLY;INTERVAL=2;COUNT=%d\r\n", calnote->occurrences);
break;
case GN_CALNOTE_MONTHLY:
calnote->occurrences ?
fprintf(f, "RRULE:FREQ=MONTHLY\r\n") :
fprintf(f, "RRULE:FREQ=MONTHLY;COUNT=%d\r\n", calnote->occurrences);
break;
case GN_CALNOTE_YEARLY:
calnote->occurrences ?
fprintf(f, "RRULE:FREQ=YEARLY\r\n") :
fprintf(f, "RRULE:FREQ=YEARLY;COUNT=%d\r\n", calnote->occurrences);
break;
default:
calnote->occurrences ?
fprintf(f, "RRULE:FREQ=HOURLY;INTERVAL=%d\r\n", calnote->recurrence) :
fprintf(f, "RRULE:FREQ=HOURLY;INTERVAL=%d;COUNT=%d\r\n", calnote->recurrence, calnote->occurrences);
break;
}
fprintf(f, "END:VEVENT\r\n");
fprintf(f, "END:VCALENDAR\r\n");
return GN_ERR_NONE;
#endif /* HAVE_LIBICAL */
}
static inline gn_calnote_type str2calnote_type(const char *str)
{
if (!str) {
return GN_CALNOTE_REMINDER;
} else if (!strncasecmp("PHONE CALL", str, 10)) {
return GN_CALNOTE_CALL;
} else if (!strncasecmp("MEETING", str, 7)) {
return GN_CALNOTE_MEETING;
} else if (!strncasecmp("ANNIVERSARY", str, 11)) {
return GN_CALNOTE_BIRTHDAY;
} else if (!strncasecmp("MISCELLANEOUS", str, 13)) {
return GN_CALNOTE_MEMO;
}
return GN_CALNOTE_REMINDER;
}
/* read a vcalendar event given by id from file f and store the data in calnote */
GNOKII_API int gn_ical2calnote(FILE *f, gn_calnote *calnote, int id)
{
int retval = GN_ERR_FAILED;
#ifdef HAVE_LIBICAL
icalparser *parser = NULL;
icalcomponent *comp = NULL, *compresult = NULL;
struct icaltimetype dtstart = {0}, dtend = {0};
parser = icalparser_new();
if (!parser) {
return GN_ERR_FAILED;
}
icalparser_set_gen_data(parser, f);
comp = icalparser_parse(parser, ical_fgets);
if (!comp) {
icalparser_free(parser);
return GN_ERR_FAILED;
}
if (id < 1)
id = 1;
/* iterate through the component */
iterate_cal(comp, 0, &id, &compresult, ICAL_VEVENT_COMPONENT);
if (!compresult) {
dprintf("No component found.\n");
retval = GN_ERR_EMPTYLOCATION;
} else {
const char *str;
icalcomponent *alarm = {0};
retval = GN_ERR_NONE;
calnote->phone_number[0] = 0;
/* type */
str = comp_get_category(compresult);
calnote->type = str2calnote_type(str);
switch (calnote->type) {
case GN_CALNOTE_CALL:
/* description goes into text */
/* TODO: UTF8 --> ascii */
snprintf(calnote->phone_number, GN_CALNOTE_NUMBER_MAX_LENGTH-1, "%s", icalcomponent_get_summary(compresult));
str = icalcomponent_get_description(compresult);
break;
default:
/* summary goes into text */
/* TODO: UTF8 --> ascii */
str = icalcomponent_get_summary(compresult);
break;
}
if (str) {
snprintf(calnote->text, GN_CALNOTE_MAX_LENGTH - 1, "%s", str);
}
/* start time */
dtstart = icalcomponent_get_dtstart(compresult);
if (!icaltime_is_null_time(dtstart)) {
calnote->time.year = dtstart.year;
calnote->time.month = dtstart.month;
calnote->time.day = dtstart.day;
if (!icaltime_is_date(dtstart)) {
calnote->time.hour = dtstart.hour;
calnote->time.minute = dtstart.minute;
calnote->time.second = dtstart.second;
/* TODO: what about time zones? */
}
}
/* end time */
memset(&calnote->end_time, 0, sizeof(calnote->end_time));
dtend = icalcomponent_get_dtend(compresult);
if (!icaltime_is_null_time(dtend)) {
calnote->end_time.year = dtend.year;
calnote->end_time.month = dtend.month;
calnote->end_time.day = dtend.day;
if (!icaltime_is_date(dtend)) {
calnote->end_time.hour = dtend.hour;
calnote->end_time.minute = dtend.minute;
calnote->end_time.second = dtend.second;
/* TODO: what about time zones? */
}
}
/* alarm */
alarm = icalcomponent_get_first_component(comp, ICAL_VALARM_COMPONENT);
if (alarm) {
icalproperty *trigger = NULL;
trigger = icalcomponent_get_first_property(alarm, ICAL_TRIGGER_PROPERTY);
if (trigger) {
struct icaltriggertype trigger_value;
struct icaltimetype alarm_start;
trigger_value = icalvalue_get_trigger(icalproperty_get_value(trigger));
if (icaltriggertype_is_null_trigger(trigger_value) ||
icaltriggertype_is_bad_trigger(trigger_value)) {
calnote->alarm.enabled = 0;
} else {
if (icaltime_is_null_time(trigger_value.time)) {
memcpy(&alarm_start, &dtstart, sizeof(struct icaltimetype));
} else {
memcpy(&alarm_start, &trigger_value.time, sizeof(struct icaltimetype));
}
if (!(icaldurationtype_is_bad_duration(trigger_value.duration) ||
icaldurationtype_is_null_duration(trigger_value.duration))) {
alarm_start = icaltime_add(alarm_start, trigger_value.duration);
}
calnote->alarm.enabled = 1;
calnote->alarm.timestamp.year = alarm_start.year;
calnote->alarm.timestamp.month = alarm_start.month;
calnote->alarm.timestamp.day = alarm_start.day;
calnote->alarm.timestamp.hour = alarm_start.hour;
calnote->alarm.timestamp.minute = alarm_start.minute;
calnote->alarm.timestamp.second = alarm_start.second;
/* TODO: time zone? */
}
}
}
str = icalcomponent_get_location(compresult);
if (!str)
str = "";
snprintf(calnote->mlocation, sizeof(calnote->mlocation), "%s", str);
dprintf("Component found\n%s\n", icalcomponent_as_ical_string(compresult));
}
if (compresult)
icalcomponent_free(compresult);
if (comp)
icalcomponent_free(comp);
if (parser)
icalparser_free(parser);
#else
retval = GN_ERR_NOTIMPLEMENTED;
#endif /* HAVE_LIBICAL */
return retval;
}
GNOKII_API int gn_todo2ical(FILE *f, gn_todo *ctodo)
{
#ifdef HAVE_LIBICAL
icalcomponent *pIcal = NULL;
char compuid[64];
snprintf(compuid, sizeof(compuid), "guid.gnokii.org_%d_%d", ctodo->location, rand());
pIcal = icalcomponent_vanew(ICAL_VCALENDAR_COMPONENT,
icalproperty_new_version("2.0"),
icalproperty_new_prodid(get_prodid()),
icalcomponent_vanew(ICAL_VTODO_COMPONENT,
icalproperty_new_categories("GNOKII"),
icalproperty_new_priority(ctodo->priority),
icalproperty_new_summary(ctodo->text),
0),
0);
if (pIcal) {
char *icalstrbuf = NULL;
const char *icalstr = icalcomponent_as_ical_string(pIcal);
/* conversion to UTF-8 */
if (string_base64(icalstr)) {
int icalstrlen = strlen(icalstr);
icalstrbuf = malloc(icalstrlen * 2 + 1);
utf8_encode(icalstrbuf, icalstrlen * 2, icalstr, icalstrlen);
icalstr = icalstrbuf;
}
fprintf(f, "%s\n", icalstr);
dprintf("%s\n", icalstr);
if (icalstrbuf)
free(icalstrbuf);
icalcomponent_free(pIcal);
pIcal = NULL;
}
return GN_ERR_NONE;
#else
fprintf(f, "BEGIN:VCALENDAR\r\n");
fprintf(f, "VERSION:1.0\r\n");
fprintf(f, "BEGIN:VTODO\r\n");
fprintf(f, "PRIORITY:%i\r\n", ctodo->priority);
fprintf(f, "SUMMARY:%s\r\n", ctodo->text);
fprintf(f, "END:VTODO\r\n");
fprintf(f, "END:VCALENDAR\r\n");
return GN_ERR_NONE;
#endif /* HAVE_LIBICAL */
}
/* read the entry identified by id from the vcal file f and write it to the phone */
GNOKII_API int gn_ical2todo(FILE *f, gn_todo *ctodo, int id)
{
#ifdef HAVE_LIBICAL
icalparser *parser = NULL;
icalcomponent *comp = NULL, *compresult = NULL;
parser = icalparser_new();
if (!parser) {
return GN_ERR_FAILED;
}
icalparser_set_gen_data(parser, f);
comp = icalparser_parse(parser, ical_fgets);
if (!comp) {
icalparser_free(parser);
return GN_ERR_FAILED;
}
if (id < 1)
id = 1;
/* iterate through the component */
iterate_cal(comp, 0, &id, &compresult, ICAL_VTODO_COMPONENT);
if (!compresult) {
dprintf("No component found.\n");
return GN_ERR_EMPTYLOCATION;
} else {
icalproperty *priority = icalcomponent_get_first_property(compresult, ICAL_PRIORITY_PROPERTY);
/* summary goes into text */
/* TODO: UTF8 --> ascii */
snprintf(ctodo->text, GN_CALNOTE_MAX_LENGTH-1, "%s", icalcomponent_get_summary(compresult));
/* priority */
ctodo->priority = icalproperty_get_priority((const icalproperty *)priority);
dprintf("Component found\n%s\n", icalcomponent_as_ical_string(compresult));
}
icalcomponent_free(compresult);
icalcomponent_free(comp);
icalparser_free(parser);
return GN_ERR_NONE;
#else
return GN_ERR_NOTIMPLEMENTED;
#endif /* HAVE_LIBICAL */
}
#ifdef HAVE_LIBICAL
/* This function is called recursively, since objects may be embedded in a
* single VCALENDAR component or each in its own. Returns 0 on an empty set,
* 1 -- otherwise.
*/
static int iterate_cal(icalcomponent *parent, int depth, int *count, icalcomponent **result, icalcomponent_kind kind)
{
icalcomponent *comp = NULL;
icalcompiter compiter;
if (!parent)
return 0;
/* we need to use iterators because we're calling iterate_cal recursively */
compiter = icalcomponent_begin_component(parent, ICAL_ANY_COMPONENT);
comp = icalcompiter_deref(&compiter);
for (; comp && *count; comp = icalcompiter_next(&compiter)) {
if (icalcomponent_isa(comp) == kind) {
/* FIXME: should we only consider items with category GNOKII? */
(*count)--;
}
if (!*count) {
*result = comp;
dprintf("Found component at depth %d\n", depth);
break;
}
iterate_cal(comp, depth+1, count, result, kind);
}
return 1;
}
/* Return the first category that is not equal to GNOKII */
static const char *comp_get_category(icalcomponent *comp)
{
icalproperty *cat = NULL;
const char *str;
for (cat = icalcomponent_get_first_property(comp, ICAL_CATEGORIES_PROPERTY);
cat;
cat = icalcomponent_get_next_property(comp, ICAL_CATEGORIES_PROPERTY)) {
str = icalproperty_get_categories(cat);
if (strncasecmp("GNOKII", str, 6) != 0)
return str;
}
return NULL;
}
#endif /* HAVE_LIBICAL */
|