diff options
-rw-r--r-- | include/datetime/timezone-file.h | 60 | ||||
-rw-r--r-- | include/datetime/timezone.h | 46 | ||||
-rw-r--r-- | src/timezone-file.cpp | 89 | ||||
-rw-r--r-- | tests/test-timezone-file.cc | 133 |
4 files changed, 328 insertions, 0 deletions
diff --git a/include/datetime/timezone-file.h b/include/datetime/timezone-file.h new file mode 100644 index 0000000..39a3d83 --- /dev/null +++ b/include/datetime/timezone-file.h @@ -0,0 +1,60 @@ +/* + * 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> + */ + +#ifndef INDICATOR_DATETIME_FILE_TIMEZONE_H +#define INDICATOR_DATETIME_FILE_TIMEZONE_H + +#include <datetime/timezone.h> // base class + +#include <string> // std::string + +#include <glib.h> +#include <gio/gio.h> + +namespace unity { +namespace indicator { +namespace datetime { + +/** + * \brief A #Timezone that gets its information from monitoring a file, such as /etc/timezone + */ +class FileTimezone: public Timezone +{ +public: + FileTimezone() {} + FileTimezone(const std::string& filename) { setFilename(filename); } + ~FileTimezone() {clear();} + +private: + void setFilename(const std::string& filename); + void clear(); + static void onFileChanged(gpointer gself); + void reload(); + + std::string filename_; + GFileMonitor * monitor_ = nullptr; + unsigned long monitor_handler_id_ = 0; +}; + + +} // namespace datetime +} // namespace indicator +} // namespace unity + +#endif // INDICATOR_DATETIME_FILE_TIMEZONE_H diff --git a/include/datetime/timezone.h b/include/datetime/timezone.h new file mode 100644 index 0000000..218e219 --- /dev/null +++ b/include/datetime/timezone.h @@ -0,0 +1,46 @@ +/* + * 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> + */ + +#ifndef INDICATOR_DATETIME_TIMEZONE_H +#define INDICATOR_DATETIME_TIMEZONE_H + +#include <core/property.h> + +#include <string> + +namespace unity { +namespace indicator { +namespace datetime { + +/** \brief Base class for objects that use various means to detect the system's timezone */ +class Timezone +{ +protected: + Timezone() {} + +public: + //virtual ~Timezone() {} + core::Property<std::string> timezone; +}; + +} // namespace datetime +} // namespace indicator +} // namespace unity + +#endif // INDICATOR_DATETIME_TIMEZONE_H diff --git a/src/timezone-file.cpp b/src/timezone-file.cpp new file mode 100644 index 0000000..f1d0dca --- /dev/null +++ b/src/timezone-file.cpp @@ -0,0 +1,89 @@ +/* + * 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/timezone-file.h> + +namespace unity { +namespace indicator { +namespace datetime { + +void +FileTimezone::clear() +{ + if (monitor_handler_id_) + g_signal_handler_disconnect(monitor_, monitor_handler_id_); + + g_clear_object (&monitor_); + + filename_.clear(); +} + +void +FileTimezone::setFilename(const std::string& filename) +{ + clear(); + + filename_ = filename; + + auto file = g_file_new_for_path(filename.c_str()); + GError * err = nullptr; + monitor_ = g_file_monitor_file(file, G_FILE_MONITOR_NONE, nullptr, &err); + g_object_unref(file); + if (err) + { + g_warning("%s Unable to monitor timezone file '%s': %s", G_STRLOC, TIMEZONE_FILE, err->message); + g_error_free(err); + } + else + { + monitor_handler_id_ = g_signal_connect_swapped(monitor_, "changed", G_CALLBACK(onFileChanged), this); + g_debug("%s Monitoring timezone file '%s'", G_STRLOC, filename.c_str()); + } + + reload(); +} + +void +FileTimezone::onFileChanged(gpointer gself) +{ + static_cast<FileTimezone*>(gself)->reload(); +} + +void +FileTimezone::reload() +{ + GError * err = nullptr; + gchar * str = nullptr; + + if (!g_file_get_contents(filename_.c_str(), &str, nullptr, &err)) + { + g_warning("%s Unable to read timezone file '%s': %s", G_STRLOC, filename_.c_str(), err->message); + g_error_free(err); + } + else + { + g_strstrip(str); + timezone.set(str); + g_free(str); + } +} + +} // namespace datetime +} // namespace indicator +} // namespace unity diff --git a/tests/test-timezone-file.cc b/tests/test-timezone-file.cc new file mode 100644 index 0000000..510d12c --- /dev/null +++ b/tests/test-timezone-file.cc @@ -0,0 +1,133 @@ + +/* + * Copyright 2013 Canonical Ltd. + * + * Authors: + * Charles Kerr <charles.kerr@canonical.com> + * + * 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/>. + */ + +#include "glib-fixture.h" + +#include <datetime/timezone-file.h> + +//#include <condition_variable> +//#include <mutex> +//#include <queue> +//#include <string> +//#include <thread> +//#include <iostream> +//#include <istream> +//#include <fstream> + +#include <cstdio> // fopen() +//#include <sys/stat.h> // chmod() +#include <unistd.h> // sync() + +using unity::indicator::datetime::FileTimezone; + + +/*** +**** +***/ + +#define TIMEZONE_FILE (SANDBOX"/timezone") + +class TimezoneFixture: public GlibFixture +{ + private: + + typedef GlibFixture super; + + protected: + + virtual void SetUp () + { + super::SetUp (); + } + + virtual void TearDown () + { + super::TearDown (); + } + + public: + + /* convenience func to set the timezone file */ + void set_file (const std::string& text) + { + FILE * fp = fopen (TIMEZONE_FILE, "w+"); + fprintf (fp, "%s\n", text.c_str()); + fclose (fp); + sync (); + } +}; + + +/** + * Test that timezone-file warns, but doesn't crash, if the timezone file doesn't exist + */ +TEST_F (TimezoneFixture, NoFile) +{ + remove (TIMEZONE_FILE); + ASSERT_FALSE (g_file_test (TIMEZONE_FILE, G_FILE_TEST_EXISTS)); + + FileTimezone tz (TIMEZONE_FILE); + testLogCount (G_LOG_LEVEL_WARNING, 1); +} + + +/** + * Test that timezone-file picks up the initial value + */ +TEST_F (TimezoneFixture, InitialValue) +{ + const std::string expected_timezone = "America/Chicago"; + set_file (expected_timezone); + FileTimezone tz (TIMEZONE_FILE); + ASSERT_EQ (expected_timezone, tz.timezone.get()); +} + + +/** + * Test that clearing the timezone results in an empty string + */ +TEST_F (TimezoneFixture, ChangedValue) +{ + const std::string initial_timezone = "America/Chicago"; + const std::string changed_timezone = "America/New_York"; + set_file (initial_timezone); + + FileTimezone tz (TIMEZONE_FILE); + ASSERT_EQ (initial_timezone, tz.timezone.get()); + + bool changed = false; + auto connection = tz.timezone.changed().connect( + [&changed, this](const std::string& s){ + g_message ("timezone changed to %s", s.c_str()); + changed = true; + g_main_loop_quit (loop); + }); + + g_idle_add ([](gpointer gself){ + static_cast<TimezoneFixture*>(gself)->set_file ("America/New_York"); + // static_cast<FileTimezone*>(gtz)->timezone.set ("America/New_York"); + return G_SOURCE_REMOVE; + }, this);//&tz); + + g_main_loop_run (loop); + + ASSERT_TRUE (changed); + ASSERT_EQ (changed_timezone, tz.timezone.get()); +} |