diff options
-rw-r--r-- | data/indicator-datetime.desktop.in | 2 | ||||
-rw-r--r-- | src/timezone-file.cpp | 69 | ||||
-rw-r--r-- | tests/test-timezone-file.cpp | 13 |
3 files changed, 70 insertions, 14 deletions
diff --git a/data/indicator-datetime.desktop.in b/data/indicator-datetime.desktop.in index 4e56947..7d57c8d 100644 --- a/data/indicator-datetime.desktop.in +++ b/data/indicator-datetime.desktop.in @@ -2,7 +2,7 @@ Type=Application Name=Indicator Date & Time Exec=@pkglibexecdir@/indicator-datetime-service -OnlyShowIn=Unity;GNOME; +OnlyShowIn=Unity;GNOME;Pantheon; NoDisplay=true StartupNotify=false Terminal=false diff --git a/src/timezone-file.cpp b/src/timezone-file.cpp index c99897a..bbe48f7 100644 --- a/src/timezone-file.cpp +++ b/src/timezone-file.cpp @@ -22,6 +22,59 @@ #include <cerrno> #include <cstdlib> +namespace +{ + std::string get_timezone_from_file(const std::string& filename) + { + GError * error; + GIOChannel * io_channel; + std::string ret; + + // read through filename line-by-line until we fine a nonempty non-comment line + error = nullptr; + io_channel = g_io_channel_new_file(filename.c_str(), "r", &error); + if (error == nullptr) + { + auto line = g_string_new(nullptr); + + while(ret.empty()) + { + const auto io_status = g_io_channel_read_line_string(io_channel, line, nullptr, &error); + if ((io_status == G_IO_STATUS_EOF) || (io_status == G_IO_STATUS_ERROR)) + break; + if (error != nullptr) + break; + + g_strstrip(line->str); + + if (!line->len) // skip empty lines + continue; + + if (*line->str=='#') // skip comments + continue; + + ret = line->str; + } + + g_string_free(line, true); + } + + if (io_channel != nullptr) + { + g_io_channel_shutdown(io_channel, false, nullptr); + g_io_channel_unref(io_channel); + } + + if (error != nullptr) + { + g_warning("%s Unable to read timezone file '%s': %s", G_STRLOC, filename.c_str(), error->message); + g_error_free(error); + } + + return ret; + } +} + namespace unity { namespace indicator { namespace datetime { @@ -95,20 +148,10 @@ FileTimezone::on_file_changed(gpointer gself) void FileTimezone::reload() { - GError * err = nullptr; - gchar * str = nullptr; + const auto new_timezone = get_timezone_from_file(m_filename); - if (!g_file_get_contents(m_filename.c_str(), &str, nullptr, &err)) - { - g_warning("%s Unable to read timezone file '%s': %s", G_STRLOC, m_filename.c_str(), err->message); - g_error_free(err); - } - else - { - g_strstrip(str); - timezone.set(str); - g_free(str); - } + if (!new_timezone.empty()) + timezone.set(new_timezone); } } // namespace datetime diff --git a/tests/test-timezone-file.cpp b/tests/test-timezone-file.cpp index 453b353..aec597c 100644 --- a/tests/test-timezone-file.cpp +++ b/tests/test-timezone-file.cpp @@ -131,3 +131,16 @@ TEST_F(TimezoneFixture, ChangedValue) ASSERT_TRUE(changed); ASSERT_EQ(changed_timezone, tz.timezone.get()); } + + +/** + * Test that timezone-file picks up the initial value + */ +TEST_F(TimezoneFixture, IgnoreComments) +{ + const std::string comment = "# Created by cloud-init v. 0.7.5 on Thu, 24 Apr 2014 14:03:29 +0000"; + const std::string expected_timezone = "Europe/Berlin"; + set_file(comment + "\n" + expected_timezone); + FileTimezone tz(TIMEZONE_FILE); + ASSERT_EQ(expected_timezone, tz.timezone.get()); +} |