From f84d3bdb7006eee351942a31d033ca0d62f3292a Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Sun, 7 Dec 2014 20:52:50 -0600 Subject: disable one-time alarms after their notifications are shown. --- src/engine-eds.cpp | 82 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.cpp | 5 +++- 2 files changed, 86 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/engine-eds.cpp b/src/engine-eds.cpp index 23e2883..210ef42 100644 --- a/src/engine-eds.cpp +++ b/src/engine-eds.cpp @@ -136,6 +136,22 @@ public: } } + void disable_ubuntu_alarm(const Appointment& appointment) + { + if (appointment.is_ubuntu_alarm()) + { + for (auto& kv : m_clients) // find the matching icalcomponent + { + e_cal_client_get_object(kv.second, + appointment.uid.c_str(), + nullptr, + m_cancellable, + on_object_ready_for_disable, + this); + } + } + } + private: void set_dirty_now() @@ -506,6 +522,67 @@ private: return G_SOURCE_CONTINUE; } + + /*** + **** + ***/ + + static void on_object_ready_for_disable(GObject * client, + GAsyncResult * result, + gpointer gself) + { + icalcomponent * icc = nullptr; + if (e_cal_client_get_object_finish (E_CAL_CLIENT(client), result, &icc, nullptr)) + { + struct icaltimetype itt = icalcomponent_get_recurrenceid(icc); + if (icaltime_is_null_time(itt)) + { + g_debug("'%s' appears to be a one-time alarm... adding 'disabled' tag.", + icalcomponent_as_ical_string(icc)); + + auto ecc = e_cal_component_new_from_icalcomponent (icc); // takes ownership of icc + icc = nullptr; + + if (ecc != nullptr) + { + // add TAG_DISABLED to the list of categories + GSList * old_categories = nullptr; + e_cal_component_get_categories_list(ecc, &old_categories); + auto new_categories = g_slist_copy(old_categories); + new_categories = g_slist_append(new_categories, const_cast(TAG_DISABLED)); + e_cal_component_set_categories_list(ecc, new_categories); + g_slist_free(new_categories); + e_cal_component_free_categories_list(old_categories); + e_cal_client_modify_object(E_CAL_CLIENT(client), + e_cal_component_get_icalcomponent(ecc), + E_CAL_OBJ_MOD_THIS, + static_cast(gself)->m_cancellable, + on_disable_done, + nullptr); + + g_clear_object(&ecc); + } + } + + g_clear_pointer(&icc, icalcomponent_free); + } + } + + static void on_disable_done (GObject* gclient, GAsyncResult *res, gpointer) + { + GError * error = nullptr; + if (!e_cal_client_modify_object_finish (E_CAL_CLIENT(gclient), res, &error)) + { + if (!g_error_matches(error, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning("indicator-datetime cannot mark one-time alarm as disabled: %s", error->message); + + g_error_free(error); + } + } + + /*** + **** + ***/ core::Signal<> m_changed; std::set m_sources; @@ -541,6 +618,11 @@ void EdsEngine::get_appointments(const DateTime& begin, p->get_appointments(begin, end, tz, func); } +void EdsEngine::disable_ubuntu_alarm(const Appointment& appointment) +{ + p->disable_ubuntu_alarm(appointment); +} + /*** **** ***/ diff --git a/src/main.cpp b/src/main.cpp index aa8f829..1761f84 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -139,7 +139,10 @@ main(int /*argc*/, char** /*argv*/) auto alarm_queue = create_simple_alarm_queue(state->clock, snooze_planner, engine, timezone_); auto on_snooze = [snooze_planner](const Appointment& a) {snooze_planner->add(a);}; auto on_ok = [](const Appointment&){}; - auto on_alarm_reached = [&snap, &on_snooze, &on_ok](const Appointment& a) {(*snap)(a, on_snooze, on_ok);}; + auto on_alarm_reached = [&engine, &snap, &on_snooze, &on_ok](const Appointment& a) { + (*snap)(a, on_snooze, on_ok); + engine->disable_ubuntu_alarm(a); + }; alarm_queue->alarm_reached().connect(on_alarm_reached); // create the menus -- cgit v1.2.3