diff options
-rw-r--r-- | include/datetime/engine-eds.h | 4 | ||||
-rw-r--r-- | include/datetime/engine-mock.h | 68 | ||||
-rw-r--r-- | include/datetime/engine.h (renamed from include/datetime/planner-eds.h) | 49 | ||||
-rw-r--r-- | include/datetime/planner-range.h | 36 | ||||
-rw-r--r-- | src/CMakeLists.txt | 1 | ||||
-rw-r--r-- | src/main.cpp | 18 | ||||
-rw-r--r-- | src/planner-eds.cpp | 67 | ||||
-rw-r--r-- | src/planner-range.cpp | 45 | ||||
-rw-r--r-- | tests/planner-mock.h | 29 | ||||
-rw-r--r-- | tests/test-planner.cpp | 2 |
10 files changed, 190 insertions, 129 deletions
diff --git a/include/datetime/engine-eds.h b/include/datetime/engine-eds.h index e269167..4b260a8 100644 --- a/include/datetime/engine-eds.h +++ b/include/datetime/engine-eds.h @@ -20,6 +20,8 @@ #ifndef INDICATOR_DATETIME_ENGINE_EDS__H #define INDICATOR_DATETIME_ENGINE_EDS__H +#include <datetime/engine.h> + #include <datetime/appointment.h> #include <datetime/date-time.h> #include <datetime/timezone.h> @@ -40,7 +42,7 @@ namespace datetime { * * @see EdsPlanner */ -class EdsEngine +class EdsEngine: public Engine { public: EdsEngine(); diff --git a/include/datetime/engine-mock.h b/include/datetime/engine-mock.h new file mode 100644 index 0000000..ecbf102 --- /dev/null +++ b/include/datetime/engine-mock.h @@ -0,0 +1,68 @@ +/* + * Copyright 2014 Canonical Ltd. + * + * This program is free software: you can redistribute it and/or modify it + * under the terms of the GNU General Public License version 3, as published + * by the Free Software Foundation. + * + * This program is distributed in the hope that it will be useful, but + * WITHOUT ANY WARRANTY; without even the implied warranties of + * MERCHANTABILITY, SATISFACTORY QUALITY, 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 this program. If not, see <http://www.gnu.org/licenses/>. + * + * Authors: + * Charles Kerr <charles.kerr@canonical.com> + */ + +#ifndef INDICATOR_DATETIME_ENGINE_MOCK__H +#define INDICATOR_DATETIME_ENGINE_MOCK__H + +#include <datetime/engine.h> + +namespace unity { +namespace indicator { +namespace datetime { + +/**** +***** +****/ + +/** + * A no-op #Engine + * + * @see Engine + */ +class MockEngine: public Engine +{ +public: + MockEngine() =default; + ~MockEngine() =default; + + void get_appointments(const DateTime& /*begin*/, + const DateTime& /*end*/, + const Timezone& /*default_timezone*/, + std::function<void(const std::vector<Appointment>&)> appointment_func) { + appointment_func(m_appointments); + } + + core::Signal<>& changed() { + return m_changed; + } + +private: + core::Signal<> m_changed; + std::vector<Appointment> m_appointments; +}; + +/*** +**** +***/ + +} // namespace datetime +} // namespace indicator +} // namespace unity + +#endif // INDICATOR_DATETIME_ENGINE_NOOP__H diff --git a/include/datetime/planner-eds.h b/include/datetime/engine.h index 95b5d79..2e8237e 100644 --- a/include/datetime/planner-eds.h +++ b/include/datetime/engine.h @@ -1,5 +1,5 @@ /* - * Copyright 2013 Canonical Ltd. + * Copyright 2014 Canonical Ltd. * * This program is free software: you can redistribute it and/or modify it * under the terms of the GNU General Public License version 3, as published @@ -17,43 +17,52 @@ * Charles Kerr <charles.kerr@canonical.com> */ -#ifndef INDICATOR_DATETIME_PLANNER_EDS_H -#define INDICATOR_DATETIME_PLANNER_EDS_H +#ifndef INDICATOR_DATETIME_ENGINE__H +#define INDICATOR_DATETIME_ENGINE__H -#include <datetime/planner-range.h> - -#include <datetime/engine-eds.h> +#include <datetime/appointment.h> +#include <datetime/date-time.h> #include <datetime/timezone.h> -#include <memory> // shared_ptr, unique_ptr +#include <functional> +#include <vector> namespace unity { namespace indicator { namespace datetime { +/**** +***** +****/ + /** - * \brief An EDS-based #RangePlanner + * Class wrapper around the backend that generates appointments + * + * @see EdsEngine + * @see EdsPlanner */ -class EdsPlanner: public RangePlanner +class Engine { public: - EdsPlanner(const std::shared_ptr<EdsEngine>& eds_engine, - const std::shared_ptr<Timezone>& timezone); - virtual ~EdsPlanner(); + virtual ~Engine() =default; - core::Property<std::vector<Appointment>>& appointments(); + virtual void get_appointments(const DateTime& begin, + const DateTime& end, + const Timezone& default_timezone, + std::function<void(const std::vector<Appointment>&)> appointment_func) =0; -protected: - void rebuild_now(); + virtual core::Signal<>& changed() =0; -private: - std::shared_ptr<EdsEngine> m_engine; - std::shared_ptr<Timezone> m_timezone; - core::Property<std::vector<Appointment>> m_appointments; +protected: + Engine() =default; }; +/*** +**** +***/ + } // namespace datetime } // namespace indicator } // namespace unity -#endif // INDICATOR_DATETIME_PLANNER_EDS_H +#endif // INDICATOR_DATETIME_ENGINE__H diff --git a/include/datetime/planner-range.h b/include/datetime/planner-range.h index 5306cdc..25334a6 100644 --- a/include/datetime/planner-range.h +++ b/include/datetime/planner-range.h @@ -23,6 +23,7 @@ #include <datetime/planner.h> #include <datetime/date-time.h> +#include <datetime/engine.h> namespace unity { namespace indicator { @@ -36,25 +37,46 @@ namespace datetime { class RangePlanner: public Planner { public: - virtual ~RangePlanner(); - core::Property<std::pair<DateTime,DateTime>>& range(); + virtual ~RangePlanner() =default; + virtual core::Property<std::pair<DateTime,DateTime>>& range() =0; protected: - RangePlanner(); + RangePlanner() =default; +}; - void rebuild_soon(); - virtual void rebuild_now() =0; +/** + * \brief A #RangePlanner that uses an #Engine to generate appointments + * + * @see Planner + */ +class SimpleRangePlanner: public RangePlanner +{ +public: + SimpleRangePlanner(const std::shared_ptr<Engine>& engine, + const std::shared_ptr<Timezone>& timezone); + virtual ~SimpleRangePlanner(); + + core::Property<std::vector<Appointment>>& appointments(); + core::Property<std::pair<DateTime,DateTime>>& range(); private: + // rebuild scaffolding + void rebuild_soon(); + virtual void rebuild_now(); static gboolean rebuild_now_static(gpointer); guint m_rebuild_tag = 0; + + std::shared_ptr<Engine> m_engine; + std::shared_ptr<Timezone> m_timezone; core::Property<std::pair<DateTime,DateTime>> m_range; + core::Property<std::vector<Appointment>> m_appointments; // we've got a GSignal tag here, so disable copying - RangePlanner(const RangePlanner&) =delete; - RangePlanner& operator=(const RangePlanner&) =delete; + SimpleRangePlanner(const RangePlanner&) =delete; + SimpleRangePlanner& operator=(const RangePlanner&) =delete; }; + } // namespace datetime } // namespace indicator } // namespace unity diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 5660e6a..9bc22f2 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -22,7 +22,6 @@ add_library (${SERVICE_LIB} STATIC locations.cpp locations-settings.cpp menu.cpp - planner-eds.cpp planner-month.cpp planner-range.cpp planner-upcoming.cpp diff --git a/src/main.cpp b/src/main.cpp index 3e16555..c7b35e5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -20,11 +20,12 @@ #include <datetime/actions-live.h> #include <datetime/clock.h> #include <datetime/clock-watcher.h> +#include <datetime/engine-mock.h> #include <datetime/engine-eds.h> #include <datetime/exporter.h> #include <datetime/locations-settings.h> #include <datetime/menu.h> -#include <datetime/planner-eds.h> +#include <datetime/planner-range.h> #include <datetime/settings-live.h> #include <datetime/snap.h> #include <datetime/state.h> @@ -53,25 +54,32 @@ main(int /*argc*/, char** /*argv*/) bindtextdomain(GETTEXT_PACKAGE, GNOMELOCALEDIR); textdomain(GETTEXT_PACKAGE); + // we don't show appointments in the greeter, + // so no need to connect to EDS there... + std::shared_ptr<Engine> engine; + if (!g_strcmp0("lightdm", g_get_user_name())) + engine.reset(new MockEngine); + else + engine.reset(new EdsEngine); + // build the state, actions, and menufactory std::shared_ptr<State> state(new State); std::shared_ptr<Settings> live_settings(new LiveSettings); std::shared_ptr<Timezones> live_timezones(new LiveTimezones(live_settings, TIMEZONE_FILE)); std::shared_ptr<Clock> live_clock(new LiveClock(live_timezones)); std::shared_ptr<Timezone> file_timezone(new FileTimezone(TIMEZONE_FILE)); - std::shared_ptr<EdsEngine> eds_engine (new EdsEngine); const auto now = live_clock->localtime(); state->settings = live_settings; state->clock = live_clock; state->locations.reset(new SettingsLocations(live_settings, live_timezones)); - auto calendar_month = new MonthPlanner(std::shared_ptr<RangePlanner>(new EdsPlanner(eds_engine, file_timezone)), now); + auto calendar_month = new MonthPlanner(std::shared_ptr<RangePlanner>(new SimpleRangePlanner(engine, file_timezone)), now); state->calendar_month.reset(calendar_month); - state->calendar_upcoming.reset(new UpcomingPlanner(std::shared_ptr<RangePlanner>(new EdsPlanner(eds_engine, file_timezone)), now)); + state->calendar_upcoming.reset(new UpcomingPlanner(std::shared_ptr<RangePlanner>(new SimpleRangePlanner(engine, file_timezone)), now)); std::shared_ptr<Actions> actions(new LiveActions(state)); MenuFactory factory(actions, state); // snap decisions - std::shared_ptr<UpcomingPlanner> upcoming_planner(new UpcomingPlanner(std::shared_ptr<RangePlanner>(new EdsPlanner (eds_engine, file_timezone)), now)); + std::shared_ptr<UpcomingPlanner> upcoming_planner(new UpcomingPlanner(std::shared_ptr<RangePlanner>(new SimpleRangePlanner(engine, file_timezone)), now)); ClockWatcherImpl clock_watcher(live_clock, upcoming_planner); Snap snap; clock_watcher.alarm_reached().connect([&snap](const Appointment& appt){ diff --git a/src/planner-eds.cpp b/src/planner-eds.cpp deleted file mode 100644 index f3f28ee..0000000 --- a/src/planner-eds.cpp +++ /dev/null @@ -1,67 +0,0 @@ -/* - * Copyright 2013 Canonical Ltd. - * - * This program is free software: you can redistribute it and/or modify it - * under the terms of the GNU General Public License version 3, as published - * by the Free Software Foundation. - * - * This program is distributed in the hope that it will be useful, but - * WITHOUT ANY WARRANTY; without even the implied warranties of - * MERCHANTABILITY, SATISFACTORY QUALITY, 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 this program. If not, see <http://www.gnu.org/licenses/>. - * - * Authors: - * Charles Kerr <charles.kerr@canonical.com> - */ - -#include <datetime/planner-eds.h> -#include <datetime/engine-eds.h> - -namespace unity { -namespace indicator { -namespace datetime { - -/*** -**** -***/ - -EdsPlanner::EdsPlanner(const std::shared_ptr<EdsEngine>& engine, - const std::shared_ptr<Timezone>& timezone): - m_engine(engine), - m_timezone(timezone) -{ - m_engine->changed().connect([this](){ - g_debug("EdsPlanner %p rebuilding soon because EdsEngine %p emitted 'changed' signal%p", this, m_engine.get()); - rebuild_soon(); - }); -} - -EdsPlanner::~EdsPlanner() =default; - -void EdsPlanner::rebuild_now() -{ - const auto& r = range().get(); - - auto on_appointments_fetched = [this](const std::vector<Appointment>& a){ - g_debug("EdsPlanner %p got %zu appointments", this, a.size()); - m_appointments.set(a); - }; - - m_engine->get_appointments(r.first, r.second, *m_timezone.get(), on_appointments_fetched); -} - -core::Property<std::vector<Appointment>>& EdsPlanner::appointments() -{ - return m_appointments; -} - -/*** -**** -***/ - -} // namespace datetime -} // namespace indicator -} // namespace unity diff --git a/src/planner-range.cpp b/src/planner-range.cpp index 13a1492..93946e0 100644 --- a/src/planner-range.cpp +++ b/src/planner-range.cpp @@ -27,22 +27,46 @@ namespace datetime { **** ***/ -RangePlanner::RangePlanner(): +SimpleRangePlanner::SimpleRangePlanner(const std::shared_ptr<Engine>& engine, + const std::shared_ptr<Timezone>& timezone): + m_engine(engine), + m_timezone(timezone), m_range(std::pair<DateTime,DateTime>(DateTime::NowLocal(), DateTime::NowLocal())) { + engine->changed().connect([this](){ + g_debug("RangePlanner %p rebuilding soon because Engine %p emitted 'changed' signal%p", this, m_engine.get()); + rebuild_soon(); + }); + range().changed().connect([this](const std::pair<DateTime,DateTime>&){ g_debug("rebuilding because the date range changed"); rebuild_soon(); }); } -RangePlanner::~RangePlanner() +SimpleRangePlanner::~SimpleRangePlanner() { if (m_rebuild_tag) g_source_remove(m_rebuild_tag); } -void RangePlanner::rebuild_soon() +/*** +**** +***/ + +void SimpleRangePlanner::rebuild_now() +{ + const auto& r = range().get(); + + auto on_appointments_fetched = [this](const std::vector<Appointment>& a){ + g_debug("RangePlanner %p got %zu appointments", this, a.size()); + appointments().set(a); + }; + + m_engine->get_appointments(r.first, r.second, *m_timezone.get(), on_appointments_fetched); +} + +void SimpleRangePlanner::rebuild_soon() { static const int ARBITRARY_BATCH_MSEC = 200; @@ -50,15 +74,24 @@ void RangePlanner::rebuild_soon() m_rebuild_tag = g_timeout_add(ARBITRARY_BATCH_MSEC, rebuild_now_static, this); } -gboolean RangePlanner::rebuild_now_static(gpointer gself) +gboolean SimpleRangePlanner::rebuild_now_static(gpointer gself) { - auto self = static_cast<RangePlanner*>(gself); + auto self = static_cast<SimpleRangePlanner*>(gself); self->m_rebuild_tag = 0; self->rebuild_now(); return G_SOURCE_REMOVE; } -core::Property<std::pair<DateTime,DateTime>>& RangePlanner::range() +/*** +**** +***/ + +core::Property<std::vector<Appointment>>& SimpleRangePlanner::appointments() +{ + return m_appointments; +} + +core::Property<std::pair<DateTime,DateTime>>& SimpleRangePlanner::range() { return m_range; } diff --git a/tests/planner-mock.h b/tests/planner-mock.h index 67e550c..53109cf 100644 --- a/tests/planner-mock.h +++ b/tests/planner-mock.h @@ -26,23 +26,6 @@ namespace unity { namespace indicator { namespace datetime { -#if 0 -/** - * \brief Planner which does nothing on its own. - * It requires its client must set its appointments property. - */ -class MockPlanner: public Planner -{ -public: - MockPlanner() =default; - virtual ~MockPlanner() =default; - core::Property<std::vector<Appointment>>& appointments() { return m_appointments; } - -private: - core::Property<std::vector<Appointment>> m_appointments; -}; -#endif - /** * \brief #RangePlanner which does nothing on its own. * Its controller must set its appointments property. @@ -50,15 +33,19 @@ private: class MockRangePlanner: public RangePlanner { public: - MockRangePlanner() =default; + MockRangePlanner(): + m_range(std::pair<DateTime,DateTime>(DateTime::NowLocal(), DateTime::NowLocal())) + { + } + ~MockRangePlanner() =default; - core::Property<std::vector<Appointment>>& appointments() { return m_appointments; } -protected: - void rebuild_now(){} + core::Property<std::vector<Appointment>>& appointments() { return m_appointments; } + core::Property<std::pair<DateTime,DateTime>>& range() { return m_range; } private: core::Property<std::vector<Appointment>> m_appointments; + core::Property<std::pair<DateTime,DateTime>> m_range; }; diff --git a/tests/test-planner.cpp b/tests/test-planner.cpp index 4694cf5..8f1590c 100644 --- a/tests/test-planner.cpp +++ b/tests/test-planner.cpp @@ -24,7 +24,7 @@ #include <datetime/clock-mock.h> #include <datetime/date-time.h> #include <datetime/planner.h> -#include <datetime/planner-eds.h> +#include <datetime/planner-range.h> #include <langinfo.h> #include <locale.h> |