From 74f8897902c99180e721d616614a9962c819d90b Mon Sep 17 00:00:00 2001 From: Charles Kerr Date: Tue, 21 Jan 2014 23:29:19 -0600 Subject: add LiveActions implementation and unit tests --- include/datetime/actions-live.h | 6 +- src/actions-live.cpp | 153 ++++++++++++++++++++++++++++++++++++---- tests/CMakeLists.txt | 9 ++- tests/test-menus.cc | 2 +- 4 files changed, 154 insertions(+), 16 deletions(-) diff --git a/include/datetime/actions-live.h b/include/datetime/actions-live.h index 3d04660..1947e6e 100644 --- a/include/datetime/actions-live.h +++ b/include/datetime/actions-live.h @@ -36,7 +36,7 @@ namespace datetime { class LiveActions: public Actions { public: - LiveActions(const std::shared_ptr& state): Actions(state) {} + LiveActions(const std::shared_ptr& state_in): Actions(state_in) {} ~LiveActions() =default; void open_desktop_settings(); @@ -47,6 +47,10 @@ public: void open_appointment(const std::string& uid); void set_location(const std::string& zone, const std::string& name); void set_calendar_date(const DateTime&); + +protected: + virtual void execute_command(const std::string& command); + virtual void dispatch_url(const std::string& url); }; } // namespace datetime diff --git a/src/actions-live.cpp b/src/actions-live.cpp index c699aff..e4f5e9f 100644 --- a/src/actions-live.cpp +++ b/src/actions-live.cpp @@ -19,6 +19,8 @@ #include +#include + #include namespace unity { @@ -29,46 +31,171 @@ namespace datetime { **** ***/ +void LiveActions::execute_command(const std::string& cmdstr) +{ + const auto cmd = cmdstr.c_str(); + g_debug("Issuing command '%s'", cmd); + + GError* error = nullptr; + if (!g_spawn_command_line_async(cmd, &error)) + { + g_warning("Unable to start \"%s\": %s", cmd, error->message); + g_error_free(error); + } +} + +void LiveActions::dispatch_url(const std::string& url) +{ + url_dispatch_send(url.c_str(), nullptr, nullptr); +} + +/*** +**** +***/ + void LiveActions::open_desktop_settings() { - g_message ("%s", G_STRFUNC); +#ifdef HAVE_CCPANEL + execute_command("gnome-control-center indicator-datetime"); +#else + execute_command("gnome-control-center datetime"); +#endif +} + +void LiveActions::open_planner() +{ + execute_command("evolution -c calendar"); } void LiveActions::open_phone_settings() { - g_message("%s", G_STRFUNC); + dispatch_url("settings:///system/time-date"); } void LiveActions::open_phone_clock_app() { - g_message("%s", G_STRFUNC); + dispatch_url("appid://com.ubuntu.clock/clock/current-user-version"); } -void LiveActions::open_planner() +void LiveActions::open_planner_at(const DateTime& dt) { - g_message("%s", G_STRFUNC); + auto cmd = dt.format("evolution \"calendar:///?startdate=%Y%m%d\""); + execute_command(cmd.c_str()); } -void LiveActions::open_planner_at(const DateTime&) +void LiveActions::open_appointment(const std::string& uid) { - g_message("%s", G_STRFUNC); + for(const auto& appt : state()->planner->upcoming.get()) + { + if(appt.uid != uid) + continue; + + if (!appt.url.empty()) + dispatch_url(appt.url); + break; + } } -void LiveActions::open_appointment(const std::string& uid) +void LiveActions::set_calendar_date(const DateTime& dt) { - g_message("%s - %s", G_STRFUNC, uid.c_str()); + state()->calendar_day.set(dt); } -void LiveActions::set_location(const std::string& zone, const std::string& name) +/*** +**** +***/ + +namespace +{ + +struct setlocation_data { - g_message("%s - %s %s", G_STRFUNC, zone.c_str(), name.c_str()); + std::string tzid; + std::string name; + std::shared_ptr settings; +}; + +static void +on_datetime1_set_timezone_response(GObject * object, + GAsyncResult * res, + gpointer gdata) +{ + GError* err = nullptr; + auto response = g_dbus_proxy_call_finish(G_DBUS_PROXY(object), res, &err); + auto data = static_cast(gdata); + + if (err != nullptr) + { + if (!g_error_matches(err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning("Could not set new timezone: %s", err->message); + + g_error_free(err); + } + else + { + data->settings->timezone_name.set(data->tzid + " " + data->name); + g_variant_unref(response); + } + + delete data; } -void LiveActions::set_calendar_date(const DateTime&) +static void +on_datetime1_proxy_ready (GObject * object G_GNUC_UNUSED, + GAsyncResult * res, + gpointer gdata) { - g_message("%s", G_STRFUNC); + auto data = static_cast(gdata); + + GError * err = nullptr; + auto proxy = g_dbus_proxy_new_for_bus_finish(res, &err); + if (err != NULL) + { + if (!g_error_matches(err, G_IO_ERROR, G_IO_ERROR_CANCELLED)) + g_warning("Could not grab DBus proxy for timedated: %s", err->message); + + g_error_free(err); + + delete data; + } + else + { + g_dbus_proxy_call(proxy, + "SetTimezone", + g_variant_new ("(sb)", data->tzid.c_str(), TRUE), + G_DBUS_CALL_FLAGS_NONE, + -1, + nullptr, + on_datetime1_set_timezone_response, + data); + + g_object_unref (proxy); + } } +} // unnamed namespace + + +void LiveActions::set_location(const std::string& tzid, const std::string& name) +{ + g_return_if_fail(!tzid.empty()); + g_return_if_fail(!name.empty()); + + auto data = new struct setlocation_data; + data->tzid = tzid; + data->name = name; + data->settings = state()->settings; + + g_dbus_proxy_new_for_bus (G_BUS_TYPE_SYSTEM, + G_DBUS_PROXY_FLAGS_NONE, + nullptr, + "org.freedesktop.timedate1", + "/org/freedesktop/timedate1", + "org.freedesktop.timedate1", + nullptr, + on_datetime1_proxy_ready, + data); +} /*** **** diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index a908801..9bc1d75 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -84,6 +84,13 @@ add_test (${TEST_NAME} ${TEST_NAME}) add_dependencies (${TEST_NAME} libindicatordatetimeservice) target_link_libraries (${TEST_NAME} indicatordatetimeservice gtest ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS}) +# test-live-actions +set (TEST_NAME test-live-actions) +add_executable (${TEST_NAME} ${TEST_NAME}.cc) +add_test (${TEST_NAME} ${TEST_NAME}) +add_dependencies (${TEST_NAME} libindicatordatetimeservice) +target_link_libraries (${TEST_NAME} indicatordatetimeservice gtest ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS}) + # test-menus set (TEST_NAME test-menus) add_executable (${TEST_NAME} ${TEST_NAME}.cc) @@ -98,7 +105,7 @@ add_test (${TEST_NAME} ${TEST_NAME}) add_dependencies (${TEST_NAME} libindicatordatetimeservice) target_link_libraries (${TEST_NAME} indicatordatetimeservice gtest ${SERVICE_DEPS_LIBRARIES} ${GTEST_LIBS}) -# test-utils +# test-settings set (TEST_NAME test-settings) add_executable (${TEST_NAME} ${TEST_NAME}.cc) add_test (${TEST_NAME} ${TEST_NAME}) diff --git a/tests/test-menus.cc b/tests/test-menus.cc index 0f86b0d..95ac5c2 100644 --- a/tests/test-menus.cc +++ b/tests/test-menus.cc @@ -148,7 +148,7 @@ protected: auto gdt_tomorrow = g_date_time_add_days(now.get(), 1); auto tomorrow = DateTime(gdt_tomorrow); g_date_time_unref(gdt_tomorrow); - m_clock->set_localtime(tomorrow); + m_mock_state->mock_clock->set_localtime(tomorrow); wait_msec(); section = g_menu_model_get_item_link(submenu, Menu::Calendar, G_MENU_LINK_SECTION); -- cgit v1.2.3