104
103
*** walk through the sources to build the appointment list
107
auto task_deleter = [](Task* task){
108
// give the caller the (sorted) finished product
109
auto& a = task->appointments;
110
std::sort(a.begin(), a.end(), [](const Appointment& a, const Appointment& b){return a.begin < b.begin;});
112
// we're done; delete the task
113
g_debug("time to delete task %p", (void*)task);
117
std::shared_ptr<Task> main_task(new Task(this, func), task_deleter);
106
auto gtz = default_timezone != nullptr
107
? g_time_zone_new(icaltimezone_get_location(default_timezone))
108
: g_time_zone_new_local();
109
auto main_task = std::make_shared<Task>(this, func, default_timezone, gtz, begin, end);
119
111
for (auto& kv : m_clients)
121
113
auto& client = kv.second;
122
114
if (default_timezone != nullptr)
123
115
e_cal_client_set_default_timezone(client, default_timezone);
116
g_debug("calling e_cal_client_generate_instances for %p", (void*)client);
125
// start a new subtask to enumerate all the components in this client.
126
118
auto& source = kv.first;
127
119
auto extension = e_source_get_extension(source, E_SOURCE_EXTENSION_CALENDAR);
128
120
const auto color = e_source_selectable_get_color(E_SOURCE_SELECTABLE(extension));
129
g_debug("calling e_cal_client_generate_instances for %p", (void*)client);
130
auto subtask = new AppointmentSubtask(main_task,
136
e_cal_client_generate_instances(client,
140
my_get_appointments_foreach,
142
[](gpointer g){delete static_cast<AppointmentSubtask*>(g);});
122
auto begin_str = isodate_from_time_t(begin.to_unix());
123
auto end_str = isodate_from_time_t(end.to_unix());
124
auto sexp_fmt = g_strdup_printf("(%%s? (make-time \"%s\") (make-time \"%s\"))", begin_str, end_str);
125
g_clear_pointer(&begin_str, g_free);
126
g_clear_pointer(&end_str, g_free);
128
// ask EDS about alarms that occur in this window...
129
auto sexp = g_strdup_printf(sexp_fmt, "has-alarms-in-range");
130
g_debug("%s alarm sexp is %s", G_STRLOC, sexp);
131
e_cal_client_get_object_list_as_comps(
135
on_alarm_component_list_ready,
136
new ClientSubtask(main_task, client, color));
137
g_clear_pointer(&sexp, g_free);
139
// ask EDS about events that occur in this window...
140
sexp = g_strdup_printf(sexp_fmt, "occur-in-time-range");
141
g_debug("%s event sexp is %s", G_STRLOC, sexp);
142
e_cal_client_get_object_list_as_comps(
146
on_event_component_list_ready,
147
new ClientSubtask(main_task, client, color));
148
g_clear_pointer(&sexp, g_free);
150
g_clear_pointer(&sexp_fmt, g_free);
401
413
static_cast<Impl*>(gself)->set_dirty_soon();
420
// old ubuntu-clock-app alarms created VTODO VALARMS without the
421
// required 'TRIGGER' property... http://pad.lv/1465806
423
void ensure_client_alarms_have_triggers(ECalClient* client)
425
// ask the EDS server for all the ubuntu-clock-app alarms...
427
auto sexp = g_strdup_printf("has-categories? '%s'", TAG_ALARM);
429
e_cal_client_get_object_list_as_comps(
433
ensure_client_alarms_have_triggers_async_cb,
436
g_clear_pointer(&sexp, g_free);
439
static void ensure_client_alarms_have_triggers_async_cb(
444
ECalClient * client = E_CAL_CLIENT(oclient);
445
GError * error = nullptr;
446
GSList * components = nullptr;
448
if (e_cal_client_get_object_list_as_comps_finish(client,
453
auto self = static_cast<Impl*>(gself);
454
self->ensure_canonical_alarms_have_triggers(client, components);
455
e_cal_client_free_ecalcomp_slist(components);
457
else if (error != nullptr)
459
if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
460
g_warning("can't get clock-app alarm list: %s", error->message);
466
void ensure_canonical_alarms_have_triggers(ECalClient * client,
469
GSList * modify_slist = nullptr;
471
// for each component..
472
for (auto l=components; l!=nullptr; l=l->next)
474
bool changed = false;
477
auto component = E_CAL_COMPONENT(l->data);
478
auto auids = e_cal_component_get_alarm_uids(component);
479
for(auto l=auids; l!=nullptr; l=l->next)
481
auto auid = static_cast<const char*>(l->data);
482
auto alarm = e_cal_component_get_alarm(component, auid);
483
if (alarm == nullptr)
486
// if the alarm has no trigger, add one.
487
ECalComponentAlarmTrigger trigger;
488
e_cal_component_alarm_get_trigger(alarm, &trigger);
489
if (trigger.type == E_CAL_COMPONENT_ALARM_TRIGGER_NONE)
491
trigger.type = E_CAL_COMPONENT_ALARM_TRIGGER_RELATIVE_START;
492
trigger.u.rel_duration = icaldurationtype_from_int(0);
493
e_cal_component_alarm_set_trigger (alarm, trigger);
497
g_clear_pointer(&alarm, e_cal_component_alarm_free);
499
g_clear_pointer(&auids, cal_obj_uid_list_free);
503
auto icc = e_cal_component_get_icalcomponent(component); // icc owned by ecc
504
modify_slist = g_slist_prepend(modify_slist, icc);
508
if (modify_slist != nullptr)
510
e_cal_client_modify_objects(client,
514
ensure_canonical_alarms_have_triggers_async_cb,
517
g_clear_pointer(&modify_slist, g_slist_free);
521
// log a warning if e_cal_client_modify_objects() failed
522
static void ensure_canonical_alarms_have_triggers_async_cb(
527
GError * error = nullptr;
529
e_cal_client_modify_objects_finish (E_CAL_CLIENT(oclient), res, &error);
531
if (error != nullptr)
533
if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
534
g_warning("couldn't add alarm triggers: %s", error->message);
404
545
typedef std::function<void(const std::vector<Appointment>&)> appointment_func;
409
550
appointment_func func;
551
icaltimezone* default_timezone; // pointer owned by libical
410
553
std::vector<Appointment> appointments;
411
Task(Impl* p_in, const appointment_func& func_in): p(p_in), func(func_in) {}
554
const DateTime begin;
558
appointment_func func_in,
561
const DateTime& begin_in,
562
const DateTime& end_in):
565
default_timezone{tz_in},
571
g_clear_pointer(>z, g_time_zone_unref);
572
// give the caller the sorted finished product
573
auto& a = appointments;
574
std::sort(a.begin(), a.end(), [](const Appointment& a, const Appointment& b){return a.begin < b.begin;});
414
struct AppointmentSubtask
416
581
std::shared_ptr<Task> task;
417
582
ECalClient* client;
418
583
std::string color;
419
icaltimezone* default_timezone;
423
AppointmentSubtask(const std::shared_ptr<Task>& task_in,
424
ECalClient* client_in,
425
const char* color_in,
426
icaltimezone* default_tz,
585
ClientSubtask(const std::shared_ptr<Task>& task_in,
586
ECalClient* client_in,
587
const char* color_in):
431
default_timezone(default_tz),
436
592
color = color_in;
484
my_get_appointments_foreach(ECalComponent* component,
640
on_alarm_component_list_ready(GObject * oclient,
644
GError * error = NULL;
645
GSList * comps_slist = NULL;
646
auto subtask = static_cast<ClientSubtask*>(gsubtask);
648
if (e_cal_client_get_object_list_as_comps_finish(E_CAL_CLIENT(oclient),
653
// _generate_alarms takes a GList, so make a shallow one
654
GList * comps_list = nullptr;
655
for (auto l=comps_slist; l!=nullptr; l=l->next)
656
comps_list = g_list_prepend(comps_list, l->data);
658
constexpr std::array<ECalComponentAlarmAction,1> omit = {
659
(ECalComponentAlarmAction)-1
660
}; // list of action types to omit, terminated with -1
661
GSList * comp_alarms = nullptr;
662
e_cal_util_generate_alarms_for_list(
664
subtask->task->begin.to_unix(),
665
subtask->task->end.to_unix(),
666
const_cast<ECalComponentAlarmAction*>(omit.data()),
668
e_cal_client_resolve_tzid_cb,
670
subtask->task->default_timezone);
672
// walk the alarms & add them
673
for (auto l=comp_alarms; l!=nullptr; l=l->next)
674
add_alarms_to_subtask(static_cast<ECalComponentAlarms*>(l->data), subtask, subtask->task->gtz);
677
e_cal_free_alarms(comp_alarms);
678
g_list_free(comps_list);
679
e_cal_client_free_ecalcomp_slist(comps_slist);
681
else if (error != nullptr)
683
if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
684
g_warning("can't get ecalcomponent list: %s", error->message);
693
on_event_component_list_ready(GObject * oclient,
697
GError * error = NULL;
698
GSList * comps_slist = NULL;
699
auto subtask = static_cast<ClientSubtask*>(gsubtask);
701
if (e_cal_client_get_object_list_as_comps_finish(E_CAL_CLIENT(oclient),
706
for (auto l=comps_slist; l!=nullptr; l=l->next)
707
add_event_to_subtask(static_cast<ECalComponent*>(l->data), subtask, subtask->task->gtz);
709
e_cal_client_free_ecalcomp_slist(comps_slist);
711
else if (error != nullptr)
713
if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED))
714
g_warning("can't get ecalcomponent list: %s", error->message);
723
datetime_from_component_date_time(const ECalComponentDateTime & in,
724
GTimeZone * default_timezone)
728
g_return_val_if_fail(in.value != nullptr, out);
730
auto gtz = in.tzid == nullptr ? g_time_zone_ref(default_timezone)
731
: g_time_zone_new(in.tzid);
739
g_time_zone_unref(gtz);
744
is_component_interesting(ECalComponent * component)
746
// we only want calendar events and vtodos
489
747
const auto vtype = e_cal_component_get_vtype(component);
490
auto subtask = static_cast<AppointmentSubtask*>(gsubtask);
492
if ((vtype == E_CAL_COMPONENT_EVENT) || (vtype == E_CAL_COMPONENT_TODO))
494
const gchar* uid = nullptr;
495
e_cal_component_get_uid(component, &uid);
497
auto status = ICAL_STATUS_NONE;
498
e_cal_component_get_status(component, &status);
500
// get the timezone we want to use for generated Appointments/Alarms
501
const char * location = icaltimezone_get_location(subtask->default_timezone);
502
auto gtz = g_time_zone_new(location);
503
g_debug("timezone abbreviation is %s", g_time_zone_get_abbreviation (gtz, 0));
505
const DateTime begin_dt { gtz, begin };
506
const DateTime end_dt { gtz, end };
507
g_debug ("got appointment from %s to %s, uid %s status %d",
508
begin_dt.format("%F %T %z").c_str(),
509
end_dt.format("%F %T %z").c_str(),
513
// look for the in-house tags
514
bool disabled = false;
515
Appointment::Type type = Appointment::EVENT;
516
GSList * categ_list = nullptr;
517
e_cal_component_get_categories_list (component, &categ_list);
518
for (GSList * l=categ_list; l!=nullptr; l=l->next) {
519
auto tag = static_cast<const char*>(l->data);
520
if (!g_strcmp0(tag, TAG_ALARM))
521
type = Appointment::UBUNTU_ALARM;
522
if (!g_strcmp0(tag, TAG_DISABLED))
525
e_cal_component_free_categories_list(categ_list);
527
if ((uid != nullptr) &&
529
(status != ICAL_STATUS_COMPLETED) &&
530
(status != ICAL_STATUS_CANCELLED))
532
constexpr std::array<ECalComponentAlarmAction,1> omit = { (ECalComponentAlarmAction)-1 }; // list of action types to omit, terminated with -1
533
Appointment appointment;
535
ECalComponentText text {};
536
e_cal_component_get_summary(component, &text);
538
appointment.summary = text.value;
540
appointment.begin = begin_dt;
541
appointment.end = end_dt;
542
appointment.color = subtask->color;
543
appointment.uid = uid;
544
appointment.type = type;
546
icalcomponent * icc = e_cal_component_get_icalcomponent(component);
547
g_debug("%s", icalcomponent_as_ical_string(icc)); // libical owns this string; no leak
549
auto e_alarms = e_cal_util_generate_alarms_for_comp(component,
552
const_cast<ECalComponentAlarmAction*>(omit.data()),
553
e_cal_client_resolve_tzid_cb,
555
subtask->default_timezone);
557
std::map<DateTime,Alarm> alarms;
559
if (e_alarms != nullptr)
561
for (auto l=e_alarms->alarms; l!=nullptr; l=l->next)
563
auto ai = static_cast<ECalComponentAlarmInstance*>(l->data);
564
auto a = e_cal_component_get_alarm(component, ai->auid);
568
const DateTime alarm_begin{gtz, ai->trigger};
569
auto& alarm = alarms[alarm_begin];
571
if (alarm.text.empty())
572
alarm.text = get_alarm_text(a);
573
if (alarm.audio_url.empty())
574
alarm.audio_url = get_alarm_sound_url(a);
575
if (!alarm.time.is_set())
576
alarm.time = alarm_begin;
578
e_cal_component_alarm_free(a);
582
e_cal_component_alarms_free(e_alarms);
584
// Hm, no alarm triggers?
585
// That's a bug in alarms created by some versions of ubuntu-ui-toolkit.
586
// If that's what's happening here, let's handle those alarms anyway
587
// by effectively injecting a TRIGGER;VALUE=DURATION;RELATED=START:PT0S
588
else if (appointment.is_ubuntu_alarm())
591
tmp.time = appointment.begin;
593
auto auids = e_cal_component_get_alarm_uids(component);
594
for(auto l=auids; l!=nullptr; l=l->next)
596
const auto auid = static_cast<const char*>(l->data);
597
auto a = e_cal_component_get_alarm(component, auid);
600
if (tmp.text.empty())
601
tmp.text = get_alarm_text(a);
602
if (tmp.audio_url.empty())
603
tmp.audio_url = get_alarm_sound_url(a);
604
e_cal_component_alarm_free(a);
607
cal_obj_uid_list_free(auids);
609
alarms[tmp.time] = tmp;
612
appointment.alarms.reserve(alarms.size());
613
for (const auto& it : alarms)
614
appointment.alarms.push_back(it.second);
616
subtask->task->appointments.push_back(appointment);
619
g_time_zone_unref(gtz);
622
return G_SOURCE_CONTINUE;
748
if ((vtype != E_CAL_COMPONENT_EVENT) &&
749
(vtype != E_CAL_COMPONENT_TODO))
752
// we're not interested in completed or cancelled components
753
auto status = ICAL_STATUS_NONE;
754
e_cal_component_get_status(component, &status);
755
if ((status == ICAL_STATUS_COMPLETED) ||
756
(status == ICAL_STATUS_CANCELLED))
759
// we don't want disabled alarms
760
bool disabled = false;
761
GSList * categ_list = nullptr;
762
e_cal_component_get_categories_list (component, &categ_list);
763
for (GSList * l=categ_list; l!=nullptr; l=l->next) {
764
auto tag = static_cast<const char*>(l->data);
765
if (!g_strcmp0(tag, TAG_DISABLED))
768
e_cal_component_free_categories_list(categ_list);
776
get_appointment(ECalComponent * component, GTimeZone * gtz)
778
Appointment baseline;
780
// get appointment.uid
781
const gchar* uid = nullptr;
782
e_cal_component_get_uid(component, &uid);
786
// get appointment.summary
787
ECalComponentText text {};
788
e_cal_component_get_summary(component, &text);
790
baseline.summary = text.value;
792
// get appointment.begin
793
ECalComponentDateTime eccdt_tmp {};
794
e_cal_component_get_dtstart(component, &eccdt_tmp);
795
baseline.begin = datetime_from_component_date_time(eccdt_tmp, gtz);
796
e_cal_component_free_datetime(&eccdt_tmp);
798
// get appointment.end
799
e_cal_component_get_dtend(component, &eccdt_tmp);
800
baseline.end = eccdt_tmp.value != nullptr
801
? datetime_from_component_date_time(eccdt_tmp, gtz)
803
e_cal_component_free_datetime(&eccdt_tmp);
805
// get appointment.activation_url from x-props
806
auto icc = e_cal_component_get_icalcomponent(component); // icc owned by component
807
auto icalprop = icalcomponent_get_first_property(icc, ICAL_X_PROPERTY);
808
while (icalprop != nullptr) {
809
const char * x_name = icalproperty_get_x_name(icalprop);
810
if ((x_name != nullptr) && !g_ascii_strcasecmp(x_name, X_PROP_ACTIVATION_URL)) {
811
const char * url = icalproperty_get_value_as_string(icalprop);
812
if ((url != nullptr) && baseline.activation_url.empty())
813
baseline.activation_url = url;
815
icalprop = icalcomponent_get_next_property(icc, ICAL_X_PROPERTY);
818
// get appointment.type
819
baseline.type = Appointment::EVENT;
820
GSList * categ_list = nullptr;
821
e_cal_component_get_categories_list (component, &categ_list);
822
for (GSList * l=categ_list; l!=nullptr; l=l->next) {
823
auto tag = static_cast<const char*>(l->data);
824
if (!g_strcmp0(tag, TAG_ALARM))
825
baseline.type = Appointment::UBUNTU_ALARM;
827
e_cal_component_free_categories_list(categ_list);
829
g_debug("%s got appointment from %s to %s: %s", G_STRLOC,
830
baseline.begin.format("%F %T %z").c_str(),
831
baseline.end.format("%F %T %z").c_str(),
832
icalcomponent_as_ical_string(icc) /* string owned by ical */);
838
add_event_to_subtask(ECalComponent * component,
839
ClientSubtask * subtask,
842
// events with alarms are covered by add_alarm_to_subtask(),
844
auto auids = e_cal_component_get_alarm_uids(component);
845
const bool has_alarms = auids != nullptr;
846
cal_obj_uid_list_free(auids);
850
// add it. simple, eh?
851
if (is_component_interesting(component))
853
Appointment appointment = get_appointment(component, gtz);
854
appointment.color = subtask->color;
855
subtask->task->appointments.push_back(appointment);
860
add_alarms_to_subtask(ECalComponentAlarms * comp_alarms,
861
ClientSubtask * subtask,
864
auto& component = comp_alarms->comp;
866
if (!is_component_interesting(component))
869
Appointment baseline = get_appointment(component, gtz);
870
baseline.color = subtask->color;
873
*** Now loop through comp_alarms to get information that we need
874
*** to build the instance appointments and their alarms.
876
*** Outer map key is the instance component's start + end time.
877
*** We build Appointment.begin and .end from that.
879
*** inner map key is the alarm trigger, we build Alarm.time from that.
881
*** inner map value is the Alarm.
883
*** We map the alarms based on their trigger time so that we
884
*** can fold together multiple valarms that trigger for the
885
*** same componeng at the same time. This is commonplace;
886
*** e.g. one valarm will have a display action and another
887
*** will specify a sound to be played.
889
std::map<std::pair<DateTime,DateTime>,std::map<DateTime,Alarm>> alarms;
890
for (auto l=comp_alarms->alarms; l!=nullptr; l=l->next)
892
auto ai = static_cast<ECalComponentAlarmInstance*>(l->data);
893
auto a = e_cal_component_get_alarm(component, ai->auid);
897
auto instance_time = std::make_pair(DateTime{gtz, ai->occur_start},
898
DateTime{gtz, ai->occur_end});
899
auto trigger_time = DateTime{gtz, ai->trigger};
901
auto& alarm = alarms[instance_time][trigger_time];
903
if (alarm.text.empty())
904
alarm.text = get_alarm_text(a);
905
if (alarm.audio_url.empty())
906
alarm.audio_url = get_alarm_sound_url(a);
907
if (!alarm.time.is_set())
908
alarm.time = trigger_time;
910
e_cal_component_alarm_free(a);
913
for (auto& i : alarms)
915
Appointment appointment = baseline;
916
appointment.begin = i.first.first;
917
appointment.end = i.first.second;
918
appointment.alarms.reserve(i.second.size());
919
for (auto& j : i.second)
920
appointment.alarms.push_back(j.second);
921
subtask->task->appointments.push_back(appointment);