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 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 1283 1284 1285 1286 1287 1288 1289 1290 1291 1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302 1303 1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316 1317 1318 1319 1320
|
/*
* Copyright (C) 2002, 2006 Philip Blundell <philb@gnu.org>
* 2006, Florian Boor <florian@kernelconcepts.de>
* Copyright (C) 2006 Neal H. Walfield <neal@walfield.org>
*
* 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 of the License, or (at your option) any later version.
*/
#include <glib-object.h>
#include <glib.h>
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>
#include <unistd.h>
#include "gpe/event-db.h"
#include "event.h"
#include "event-db.h"
#include "event-cal.h"
static void event_class_init (gpointer klass, gpointer klass_data);
static void event_init (GTypeInstance *instance, gpointer klass);
static void event_dispose (GObject *obj);
static void event_finalize (GObject *object);
static GObjectClass *event_parent_class;
GType
event_get_type (void)
{
static GType type;
if (! type)
{
static const GTypeInfo info =
{
sizeof (EventClass),
NULL,
NULL,
event_class_init,
NULL,
NULL,
sizeof (struct _Event),
0,
event_init
};
type = g_type_register_static (G_TYPE_OBJECT, "Event", &info, 0);
}
return type;
}
static void
event_class_init (gpointer klass, gpointer klass_data)
{
GObjectClass *object_class;
EventClass *event_class;
event_parent_class = g_type_class_peek_parent (klass);
object_class = G_OBJECT_CLASS (klass);
object_class->finalize = event_finalize;
object_class->dispose = event_dispose;
event_class = (EventClass *) klass;
}
static void
event_init (GTypeInstance *instance, gpointer klass)
{
}
static void
event_dispose (GObject *obj)
{
/* Chain up to the parent class */
G_OBJECT_CLASS (event_parent_class)->dispose (obj);
}
static void
event_finalize (GObject *object)
{
Event *event = EVENT (object);
if (event->clone_source)
g_object_unref (event->clone_source);
G_OBJECT_CLASS (event_parent_class)->finalize (object);
}
static void event_source_class_init (gpointer klass, gpointer klass_data);
static void event_source_init (GTypeInstance *instance, gpointer klass);
static void event_source_dispose (GObject *obj);
static void event_source_finalize (GObject *object);
static GObjectClass *event_source_parent_class;
GType
event_source_get_type (void)
{
static GType type;
if (! type)
{
static const GTypeInfo info =
{
sizeof (EventSourceClass),
NULL,
NULL,
event_source_class_init,
NULL,
NULL,
sizeof (struct _EventSource),
0,
event_source_init
};
type = g_type_register_static (TYPE_EVENT, "EventSource", &info, 0);
}
return type;
}
static void
event_source_class_init (gpointer klass, gpointer klass_data)
{
GObjectClass *object_class;
EventSourceClass *event_source_class;
event_source_parent_class = g_type_class_peek_parent (klass);
object_class = G_OBJECT_CLASS (klass);
object_class->finalize = event_source_finalize;
object_class->dispose = event_source_dispose;
event_source_class = (EventSourceClass *) klass;
}
static void
event_source_init (GTypeInstance *instance, gpointer klass)
{
g_object_add_toggle_ref (G_OBJECT (instance),
event_source_toggle_ref_notify,
NULL);
}
static void
event_source_dispose (GObject *obj)
{
/* Chain up to the parent class */
G_OBJECT_CLASS (event_source_parent_class)->dispose (obj);
}
static void
event_source_finalize (GObject *object)
{
EventSource *ev = RESOLVE_CLONE (EVENT (object));
if (ev->modified)
/* Flush to disk. */
/* XXX: Can this error be propagated better? */
EVENT_DB_GET_CLASS (ev->edb)->event_flush (ev, NULL);
/* Free any details. */
if (ev->description)
g_free (ev->description);
if (ev->location)
g_free (ev->location);
if (ev->summary)
g_free (ev->summary);
g_slist_free (ev->categories);
g_free (ev->eventid);
g_slist_free (ev->exceptions);
event_recurrence_byday_free (ev->byday);
/* EV may not have been inserted into the hash if we created a
new event and were unable to insert it into the DB. In this
case, EV->UID will be 0. */
if (ev->uid)
{
gboolean removed = g_hash_table_remove (ev->edb->events,
(gpointer) ev->uid);
g_assert (removed);
}
G_OBJECT_CLASS (event_parent_class)->finalize (object);
}
gint
event_compare_func (gconstpointer a, gconstpointer b)
{
Event *i = EVENT (a);
Event *j = EVENT (b);
if (i->start < j->start)
return -1;
if (j->start < i->start)
return 1;
if (event_get_duration (i) < event_get_duration (j))
return -1;
if (event_get_duration (j) < event_get_duration (i))
return 1;
return 0;
}
gint
event_alarm_compare_func (gconstpointer a, gconstpointer b)
{
Event *i = EVENT (a);
Event *j = EVENT (b);
EventSource *is = RESOLVE_CLONE (i);
EventSource *js = RESOLVE_CLONE (j);
return (i->start - is->alarm) - (j->start - js->alarm);
}
gboolean
event_flush (Event *event, GError **error)
{
if (event->dead)
return TRUE;
EventSource *ev = RESOLVE_CLONE (event);
if (! EVENT_DB_GET_CLASS (ev->edb)->event_flush (ev, error))
return FALSE;
g_signal_emit
(ev->edb, EVENT_DB_GET_CLASS (ev->edb)->event_modified_signal, 0, ev);
return TRUE;
}
/* Remove any instantiations of EV's source from the upcoming alarm
list. */
static void
event_remove_upcoming_alarms (EventSource *ev)
{
GSList *i;
GSList *next = ev->edb->upcoming_alarms;
while (next)
{
i = next;
next = i->next;
Event *e = EVENT (i->data);
if (RESOLVE_CLONE (e) == ev)
{
g_object_unref (e);
ev->edb->upcoming_alarms
= g_slist_delete_link (ev->edb->upcoming_alarms, i);
}
}
}
gboolean
event_remove (Event *event, GError **error)
{
if (event->dead)
return TRUE;
EventSource *ev = RESOLVE_CLONE (event);
/* Do our best to remove the event. */
GError *alarm_e = NULL;
if (ev->alarm)
{
/* If the event was unacknowledged, acknowledge it now. */
event_acknowledge (EVENT (ev), &alarm_e);
/* And remove it from the upcoming alarm list. */
if (! alarm_e)
event_remove_upcoming_alarms (ev);
}
GError *e = NULL;
EVENT_DB_GET_CLASS (ev->edb)->event_remove (ev, &e);
if (e)
{
if (alarm_e)
g_error_free (alarm_e);
SIGNAL_ERROR_GERROR (ev->edb, error, e);
return FALSE;
}
EVENT (ev)->dead = TRUE;
g_signal_emit (ev->edb,
EVENT_DB_GET_CLASS (ev->edb)->event_removed_signal,
0, ev);
if (alarm_e)
SIGNAL_ERROR_GERROR (ev->edb, error, alarm_e);
return TRUE;
}
Event *
event_new (EventDB *edb, EventCalendar *ec, const char *eventid,
GError **error)
{
EventSource *ev = EVENT_SOURCE (g_object_new (event_source_get_type (),
NULL));
ev->edb = edb;
if (ec)
ev->calendar = ec->uid;
else
ev->calendar = edb->default_calendar;
if (eventid)
{
GError *e = NULL;
Event *t = event_db_find_by_eventid (edb, eventid, &e);
if (e)
{
SIGNAL_ERROR_GERROR (edb, error, e);
g_object_unref (ev);
return NULL;
}
if (t)
{
SIGNAL_ERROR (edb, error,
"Attempted to add event with an eventid "
"which is already present in the database.");
g_object_unref (t);
return NULL;
}
ev->eventid = g_strdup (eventid);
}
else
{
static int seeded;
if (! seeded)
{
srand (time (NULL));
seeded = 1;
}
static char *hostname;
static char buffer[512];
if (! hostname)
{
if (gethostname (buffer, sizeof (buffer) - 1) == 0 && buffer[0])
hostname = buffer;
else if (errno == ENAMETOOLONG)
{
buffer[sizeof (buffer)] = 0;
hostname = buffer;
}
else
hostname = "localhost";
}
ev->eventid = g_strdup_printf ("%lu.%lu%d@%s",
(unsigned long) time (NULL),
(unsigned long) getpid(), rand (),
hostname);
}
if (! EVENT_DB_GET_CLASS (edb)->event_new (ev, error))
goto error;
g_hash_table_insert (edb->events, (gpointer) ev->uid, ev);
g_signal_emit (edb, EVENT_DB_GET_CLASS (edb)->event_new_signal, 0, ev);
return EVENT (ev);
error:
g_object_unref (ev);
return NULL;
}
void
event_acknowledge (Event *event, GError **error)
{
if (event->dead)
return;
EventSource *ev = RESOLVE_CLONE (event);
EVENT_DB_GET_CLASS (ev->edb)->event_mark_acknowledged (ev, error);
}
/* EV is new or has recently changed: check to see if it has an alarm
which will go off in the near future. */
static void
event_add_upcoming_alarms (EventSource *ev, GError **error)
{
if (! ev->edb->alarm)
/* Alarms have not yet been activated. */
return;
time_t now = time (NULL);
GSList *list = event_list (ev, now, ev->edb->period_end, 0, TRUE, error);
if (! list)
return;
ev->edb->upcoming_alarms = g_slist_concat (list, ev->edb->upcoming_alarms);
/* We remove the timeout source and call buzzer. Although no alarm
has fired, it will calculate when the next timeout needs to
fire. */
if (ev->edb->alarm)
g_source_remove (ev->edb->alarm);
buzzer (ev->edb);
}
/* Makes sure that #ev's details structure is in core. */
static void
event_details (EventSource *ev, gboolean fill_from_disk, GError **error)
{
if (ev->details)
return;
if (! fill_from_disk)
return;
GError *e = NULL;
EVENT_DB_GET_CLASS (ev->edb)->event_load_details (ev, &e);
if (e)
{
SIGNAL_ERROR_GERROR (ev->edb, error, e);
return;
}
ev->details = TRUE;
}
void
event_list_unref (GSList *l)
{
GSList *iter;
for (iter = l; iter; iter = g_slist_next (iter))
{
Event *ev = iter->data;
g_object_unref (ev);
}
g_slist_free (l);
}
/**
* event_clone:
* @ev: Event to clone.
*
* Clones a given event for localized instantiation (e.g. clone a
* recurring event and change the clone's start time to a particular
* recurrence).
*
* Returns: The event clone.
*/
static Event *
event_clone (EventSource *ev)
{
Event *n = EVENT (g_object_new (event_get_type (), NULL));
n->clone_source = ev;
g_object_ref (ev);
return n;
}
/* Interpret T as a localtime and advance it by ADVANCE days. */
static time_t
time_add_days (time_t t, int advance)
{
struct tm tm;
int days;
localtime_r (&t, &tm);
tm.tm_isdst = -1;
while (advance > 0)
{
days = g_date_get_days_in_month (tm.tm_mon + 1, 1900 + tm.tm_year);
if (tm.tm_mday + advance > days)
{
advance -= days - tm.tm_mday + 1;
tm.tm_mday = 1;
tm.tm_mon ++;
if (tm.tm_mon == 12)
{
tm.tm_mon = 0;
tm.tm_year ++;
}
}
else
{
tm.tm_mday += advance;
break;
}
}
return mktime (&tm);
}
GSList *
event_list (EventSource *ev, time_t period_start, time_t period_end, int max,
gboolean per_alarm, GError **error)
{
int event_count = 0;
if (per_alarm && ! ev->alarm)
/* We are looking for alarms but this event doesn't have
one. */
return NULL;
/* End of recurrence period. */
time_t recur_end;
if (event_is_recurrence (EVENT (ev)))
{
if (! ev->end)
/* Never ends. */
recur_end = 0;
else
recur_end = ev->end;
}
else
recur_end = ev->event.start + event_get_duration (EVENT (ev));
if (recur_end && recur_end <= period_start)
/* Event finishes prior to PERIOD_START. */
return NULL;
/* Start of first instance. */
time_t recur_start = ev->event.start;
if (period_end && period_end < recur_start - (per_alarm ? ev->alarm : 0))
/* Event starts after PERIOD_END. */
return NULL;
if (event_is_recurrence (EVENT (ev)))
{
/* Cache the representation of S. */
struct tm orig;
localtime_r (&recur_start, &orig);
/* Days in the current month. */
int days_in_month;
/* The effective daymask, 0 based. */
int daymask = 0;
/* Build the daymask for the current month. */
void bydaymonthly (void)
{
struct tm start;
localtime_r (&recur_start, &start);
start.tm_mday = 1;
start.tm_isdst = -1;
time_t t = mktime (&start);
localtime_r (&t, &start);
days_in_month = g_date_get_days_in_month (start.tm_mon + 1,
1900 + start.tm_year);
struct tm end = start;
end.tm_mday = days_in_month;
end.tm_isdst = -1;
t = mktime (&end);
localtime_r (&t, &end);
daymask = 0;
GSList *i;
for (i = ev->byday; i; i = i->next)
{
char *tail;
int prefix = strtol (i->data, &tail, 10);
while (*tail == ' ')
tail ++;
int day = -1;
if (strcmp (tail, "SU") == 0)
day = 0;
else if (strcmp (tail, "MO") == 0)
day = 1;
else if (strcmp (tail, "TU") == 0)
day = 2;
else if (strcmp (tail, "WE") == 0)
day = 3;
else if (strcmp (tail, "TH") == 0)
day = 4;
else if (strcmp (tail, "FR") == 0)
day = 5;
else if (strcmp (tail, "SA") == 0)
day = 6;
if (day == -1)
continue;
if (prefix == 0)
/* Every week. */
{
int d = day - start.tm_wday;
if (d < 0)
d += 7;
for (; d < days_in_month; d += 7)
daymask |= 1 << d;
}
else if (prefix > 0)
/* From start. */
{
int d = day - start.tm_wday;
if (d < 0)
d += 7;
d += 7 * (prefix - 1);
if (d < days_in_month)
daymask |= 1 << d;
}
else
/* From end. */
{
int d = days_in_month - 1 + day - end.tm_wday;
if (d >= days_in_month)
d -= 7;
d += 7 * (prefix + 1);
if (d >= 0)
daymask |= 1 << d;
}
}
}
int increment = ev->increment > 0 ? ev->increment : 1;
if (ev->type == RECUR_MONTHLY && ev->byday)
{
bydaymonthly ();
struct tm tm = orig;
if (daymask && ! (daymask & (1 << (orig.tm_mday - 1))))
/* The start date is not included in the mask. */
{
int s;
if (~((1 << (orig.tm_mday - 1 + 1)) - 1) & daymask)
/* There is a day in this month following the
start. */
s = orig.tm_mday - 1 + 1;
else
/* Advance to the first day of the next month. */
{
tm.tm_mday = 1;
recur_start = mktime (&tm);
int i;
for (i = increment; i > 0; i --)
{
int d = g_date_get_days_in_month (tm.tm_mon + 1,
1900 + tm.tm_year);
recur_start = time_add_days (recur_start, d);
localtime_r (&recur_start, &tm);
}
bydaymonthly ();
s = 0;
}
int j;
for (j = s; j < days_in_month; j ++)
if ((1 << j) & daymask)
break;
g_assert (j != days_in_month);
tm.tm_mday = j + 1;
recur_start = mktime (&tm);
}
}
else if (ev->type == RECUR_WEEKLY && ev->byday)
/* This is a weekly recurrence with a byday field: find the
first day which, starting with RECUR_START, occurs in
DAYMASK. */
{
GSList *l;
for (l = ev->byday; l; l = l->next)
if (strcmp (l->data, "SU") == 0)
daymask |= 1 << 0;
else if (strcmp (l->data, "MO") == 0)
daymask |= 1 << 1;
else if (strcmp (l->data, "TU") == 0)
daymask |= 1 << 2;
else if (strcmp (l->data, "WE") == 0)
daymask |= 1 << 3;
else if (strcmp (l->data, "TH") == 0)
daymask |= 1 << 4;
else if (strcmp (l->data, "FR") == 0)
daymask |= 1 << 5;
else if (strcmp (l->data, "SA") == 0)
daymask |= 1 << 6;
struct tm tm;
localtime_r (&recur_start, &tm);
int i;
for (i = tm.tm_wday; i < tm.tm_wday + 7; i ++)
if ((1 << (i % 7)) & daymask)
break;
if (i != tm.tm_wday)
recur_start = time_add_days (recur_start, i - tm.tm_wday);
}
GSList *list = NULL;
int count;
for (count = 0;
(! period_end
|| recur_start - (per_alarm ? ev->alarm : 0) <= period_end)
&& (! recur_end || recur_start < recur_end)
&& (ev->count == 0 || count < ev->count);
count ++)
{
if ((per_alarm && period_start <= recur_start - ev->alarm)
|| (! per_alarm && period_start
< recur_start + event_get_duration (EVENT (ev))))
/* This instance occurs during the period. Add
it to LIST... */
{
GSList *i;
/* ... unless there happens to be an exception. */
for (i = ev->exceptions; i; i = g_slist_next (i))
if ((long) i->data == recur_start)
break;
if (! i)
/* No exception found: instantiate this recurrence
and add it to LIST. */
{
Event *clone = event_clone (ev);
clone->start = recur_start;
list = g_slist_prepend (list, clone);
event_count ++;
if (event_count == max)
break;
}
}
/* Advance to the next recurrence. */
switch (ev->type)
{
case RECUR_DAILY:
/* Advance S by INCREMENT days. */
recur_start = time_add_days (recur_start, increment);
break;
case RECUR_WEEKLY:
if (! daymask)
/* Empty day mask, simply advance S by
INCREMENT weeks. */
recur_start = time_add_days (recur_start, 7 * increment);
else
{
struct tm tm;
localtime_r (&recur_start, &tm);
int i;
for (i = tm.tm_wday + 1; i < tm.tm_wday + 1 + 7; i ++)
{
if ((i % 7) == orig.tm_wday)
/* We wrapped a week: increment by
INCREMENT - 1 weeks as well. */
recur_start
= time_add_days (recur_start,
7 * (increment - 1));
if ((1 << (i % 7)) & daymask)
{
recur_start
= time_add_days (recur_start, i - tm.tm_wday);
break;
}
}
}
break;
case RECUR_MONTHLY:
{
int s = 0;
struct tm tm;
localtime_r (&recur_start, &tm);
if (daymask
/* Is there another day in this month? */
&& (~((1 << (tm.tm_mday - 1 + 1)) - 1) & daymask))
s = tm.tm_mday - 1 + 1;
if (! s)
/* Advance by increment months. */
{
if (daymask)
/* Start at the beginning of the month. */
{
tm.tm_mday = 1;
recur_start = mktime (&tm);
}
int i;
for (i = increment; i > 0; i --)
{
int d = g_date_get_days_in_month (tm.tm_mon + 1,
1900 + tm.tm_year);
recur_start = time_add_days (recur_start, d);
localtime_r (&recur_start, &tm);
}
if (daymask)
/* Recalculate the mask. */
bydaymonthly ();
}
if (daymask)
/* Apply the mask. */
{
int j;
for (j = s; j < days_in_month; j ++)
if ((1 << j) & daymask)
break;
g_assert (j != days_in_month);
tm.tm_mday = j + 1;
recur_start = mktime (&tm);
}
break;
}
case RECUR_YEARLY:
{
struct tm tm;
localtime_r (&recur_start, &tm);
tm.tm_year += increment;
if (tm.tm_mon == 1 && tm.tm_mday == 29
&& g_date_get_days_in_month (tm.tm_mon + 1,
tm.tm_year + 1900) == 28)
/* XXX: If the recurrence is Feb 29th and there
is no Feb 29th this year then we simply clamp
to the 28th. */
tm.tm_mday = 28;
else
{
if (tm.tm_mon == 1 && tm.tm_mday == 28)
/* This recurrence is Feb 28th. Are we
supposed to recur on the 29th? */
{
if (orig.tm_mday == 29
&& g_date_get_days_in_month
(tm.tm_mon + 1, tm.tm_year + 1900) == 29)
/* Yes, and moreover, this year, Feb has
a 29th. */
tm.tm_mday = 29;
}
}
recur_start = mktime (&tm);
break;
}
default:
g_critical ("Event %s has an invalid recurrence type: %d\n",
ev->eventid, ev->type);
return NULL;
}
}
return list;
}
else
/* Not a recurrence. */
{
if (! per_alarm
|| (per_alarm
&& period_start <= recur_start - ev->alarm
&& (! period_end || recur_start - ev->alarm <= period_end)))
{
g_object_ref (ev);
return g_slist_append (NULL, ev);
}
return NULL;
}
}
EventDB *
event_get_event_db (Event *e)
{
EventSource *ev = RESOLVE_CLONE (e);
return ev->edb;
}
EventCalendar *
event_get_calendar (Event *event, GError **error)
{
EventSource *ev = RESOLVE_CLONE (event);
GError *e = NULL;
EventCalendar *ec
= event_db_find_calendar_by_uid (ev->edb, ev->calendar, &e);
if (e)
{
SIGNAL_ERROR_GERROR (ev->edb, error, e);
return NULL;
}
if (! ec)
{
g_warning ("%s: Encountered orphaned event %s (%ld):"
" being adopted by default calendar",
__func__, ev->summary ?: "", ev->uid);
ec = event_db_get_default_calendar (ev->edb, NULL, &e);
if (e)
{
SIGNAL_ERROR_GERROR (ev->edb, error, e);
return NULL;
}
event_set_calendar (EVENT (ev), ec, &e);
if (e)
{
SIGNAL_ERROR_GERROR (ev->edb, error, e);
g_object_unref (ec);
return NULL;
}
return ec;
}
else
{
g_object_ref (ec);
return ec;
}
}
void
event_set_calendar (Event *event, EventCalendar *ec, GError **error)
{
EventSource *ev = RESOLVE_CLONE (event);
if (! ec)
{
GError *e = NULL;
ec = event_db_get_default_calendar (ev->edb, NULL, &e);
if (e)
{
SIGNAL_ERROR_GERROR (ev->edb, error, e);
return;
}
}
if (ev->calendar == ec->uid)
return;
ev->calendar = ec->uid;
STAMP (ev, error);
}
gboolean
event_get_color (Event *event, struct _GdkColor *color, GError **error)
{
EventSource *ev = RESOLVE_CLONE (event);
GError *e = NULL;
EventCalendar *ec = event_get_calendar (event, &e);
if (e)
{
SIGNAL_ERROR_GERROR (ev->edb, error, e);
return FALSE;
}
do
{
int ret = event_calendar_get_color (ec, color, &e);
g_object_unref (ec);
if (e)
{
SIGNAL_ERROR_GERROR (ev->edb, error, e);
return FALSE;
}
if (ret)
return ret;
ec = event_calendar_get_parent (ec, &e);
if (e)
{
SIGNAL_ERROR_GERROR (ev->edb, error, e);
return FALSE;
}
}
while (ec);
return FALSE;
}
gboolean
event_get_visible (Event *event, GError **error)
{
EventSource *ev = RESOLVE_CLONE (event);
GError *e = NULL;
EventCalendar *ec = event_get_calendar (event, &e);
if (e)
{
SIGNAL_ERROR_GERROR (ev->edb, error, e);
/* The better default is to show the event on error. */
return TRUE;
}
do
{
int v = event_calendar_get_visible (ec, &e);
g_object_unref (ec);
if (e)
{
SIGNAL_ERROR_GERROR (ev->edb, error, e);
return TRUE;
}
if (! v)
return FALSE;
ec = event_calendar_get_parent (ec, &e);
if (e)
{
SIGNAL_ERROR_GERROR (ev->edb, error, e);
return TRUE;
}
}
while (ec);
return TRUE;
}
time_t
event_get_start (Event *ev)
{
/* This is local, don't resolve the clone. */
return ev->start;
}
unsigned long
event_get_duration (Event *event)
{
EventSource *ev = RESOLVE_CLONE (event);
if (ev->untimed && ev->duration == 0)
/* This is a special case: its an all day event. */
return 24 * 60 * 60;
else
return ev->duration;
}
void
event_set_duration (Event *event, unsigned long duration, GError **error)
{
EventSource *ev = RESOLVE_CLONE (event);
if (ev->duration == duration)
return;
ev->duration = duration;
STAMP (ev, error);
}
#define GET(type, name, field) \
type \
event_get_##name (Event *event) \
{ \
EventSource *ev = RESOLVE_CLONE (event); \
return ev->field; \
}
#define GET_ERROR(type, name, field, detail) \
type \
event_get_##name (Event *event, GError **error) \
{ \
EventSource *ev = RESOLVE_CLONE (event); \
if (detail) \
{ \
GError *e = NULL; \
event_details (ev, TRUE, &e); \
if (e) \
{ \
SIGNAL_ERROR_GERROR (ev->edb, error, e); \
return 0; \
} \
} \
return ev->field; \
}
#define SET(type, name, field, alarm_hazard, detail) \
void \
event_set_##name (Event *event, type value, GError **error) \
{ \
EventSource *ev = RESOLVE_CLONE (event); \
if (detail) \
{ \
GError *e = NULL; \
event_details (ev, TRUE, &e); \
if (e) \
{ \
SIGNAL_ERROR_GERROR (ev->edb, error, e); \
return; \
} \
} \
\
if (ev->field == value) \
return; \
\
if ((alarm_hazard) && ev->alarm) \
{ \
GError *e = NULL; \
event_acknowledge (EVENT (ev), &e); \
if (e) \
{ \
SIGNAL_ERROR_GERROR (ev->edb, error, e); \
return; \
} \
event_remove_upcoming_alarms (ev); \
} \
ev->field = value; \
\
GError *e = NULL; \
if ((alarm_hazard) && ev->alarm) \
event_add_upcoming_alarms (ev, &e); \
\
STAMP (ev, error); \
if (! error && e) \
SIGNAL_ERROR_GERROR (ev->edb, error, e); \
else if (e) \
g_error_free (e); \
}
#define GET_SET(type, name, field, alarm_hazard, detail) \
GET (type, name, field) \
SET (type, name, field, alarm_hazard, detail)
#define GET_ERROR_SET(type, name, field, alarm_hazard, detail) \
GET_ERROR (type, name, field, detail) \
SET (type, name, field, alarm_hazard, detail)
GET(time_t, last_modification, last_modified)
GET_SET (unsigned long, alarm, alarm, TRUE, FALSE)
GET_ERROR_SET (guint32, sequence, sequence, FALSE, TRUE)
GET_SET (enum event_recurrence_type, recurrence_type, type, TRUE, FALSE)
GET (time_t, recurrence_start, event.start)
void
event_set_recurrence_start (Event *event, time_t start, GError **error)
{
EventSource *ev = RESOLVE_CLONE (event);
if (ev->event.start == start)
return;
if (ev->alarm)
{
/* If the event was unacknowledged, acknowledge it now. */
GError *e = NULL;
event_acknowledge (EVENT (ev), &e);
/* And remove it from the upcoming alarm list. */
event_remove_upcoming_alarms (ev);
}
ev->event.start = start;
if (ev->alarm)
/* And remove it from the upcoming alarm list. */
event_add_upcoming_alarms (ev, error);
STAMP (ev, error);
}
GET_SET (time_t, recurrence_end, end, TRUE, FALSE)
GET_SET (guint32, recurrence_count, count, TRUE, FALSE)
GET_SET (guint32, recurrence_increment, increment, TRUE, FALSE)
GET_SET (gboolean, untimed, untimed, FALSE, FALSE);
GET (unsigned long, uid, uid)
char *
event_get_eventid (Event *event, GError **error) \
{
EventSource *ev = RESOLVE_CLONE (event);
return g_strdup (ev->eventid);
}
#define GET_SET_STRING(field) \
char * \
event_get_##field (Event *event, GError **error) \
{ \
EventSource *ev = RESOLVE_CLONE (event); \
GError *e = NULL; \
event_details (ev, TRUE, &e); \
if (e) \
{ \
SIGNAL_ERROR_GERROR (ev->edb, error, e); \
return NULL; \
} \
return g_strdup (ev->field); \
} \
\
void \
event_set_##field (Event *event, const char *field, GError **error) \
{ \
EventSource *ev = RESOLVE_CLONE (event); \
\
GError *e = NULL; \
event_details (ev, TRUE, &e); \
if (e) \
{ \
SIGNAL_ERROR_GERROR (ev->edb, error, e); \
return; \
} \
\
if ((ev->field && field \
&& strcmp (ev->field, field) == 0) \
|| (! ev->field && ! field)) \
/* Identical. */ \
return; \
\
if (ev->field) \
free (ev->field); \
if (field) \
ev->field = g_strdup (field); \
else \
ev->field = NULL; \
STAMP (ev, error); \
}
GET_SET_STRING(summary)
GET_SET_STRING(location)
GET_SET_STRING(description)
GSList *
event_get_categories (Event *event, GError **error)
{
EventSource *ev = RESOLVE_CLONE (event);
event_details (ev, TRUE, error);
/* If we failed to load the details, EV->CATEGORIES will be
NULL. */
return g_slist_copy (ev->categories);
}
void
event_add_category (Event *event, int category, GError **error)
{
EventSource *ev = RESOLVE_CLONE (event);
GError *e = NULL;
event_details (ev, TRUE, &e);
if (e)
{
SIGNAL_ERROR_GERROR (ev->edb, error, e);
return;
}
ev->categories = g_slist_prepend (ev->categories, GINT_TO_POINTER(category));
STAMP (ev, error);
}
void
event_set_categories (Event *event, GSList *categories, GError **error)
{
EventSource *ev = RESOLVE_CLONE (event);
GError *e = NULL;
event_details (ev, TRUE, &e);
if (e)
{
SIGNAL_ERROR_GERROR (ev->edb, error, e);
return;
}
g_slist_free (ev->categories);
ev->categories = categories;
STAMP (ev, error);
}
GSList *
event_get_recurrence_byday (Event *event, GError **error)
{
EventSource *ev = RESOLVE_CLONE (event);
GSList *list = NULL;
GSList *l;
for (l = ev->byday; l; l = l->next)
list = g_slist_prepend (list, g_strdup (l->data));
return list;
}
void
event_set_recurrence_byday (Event *event, GSList *list, GError **error)
{
EventSource *ev = RESOLVE_CLONE (event);
GSList *l;
for (l = ev->byday; l; l = l->next)
g_free (l->data);
g_slist_free (ev->byday);
ev->byday = list;
STAMP (ev, error);
}
void
event_add_recurrence_exception (Event *event, time_t start, GError **error)
{
EventSource *ev = RESOLVE_CLONE (event);
ev->exceptions = g_slist_append (ev->exceptions, (void *) start);
STAMP (ev, error);
}
|