=== modified file 'bin/indicator-weather' --- bin/indicator-weather 2011-04-02 09:09:19 +0000 +++ bin/indicator-weather 2011-04-03 21:14:02 +0000 @@ -18,6 +18,19 @@ ### END LICENSE import sys, os, shutil, tempfile + +# Add project root directory (enable symlink, and trunk execution). +PROJECT_ROOT_DIRECTORY = os.path.abspath( + os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0])))) + +if (os.path.exists(os.path.join(PROJECT_ROOT_DIRECTORY, 'indicator_weather')) + and PROJECT_ROOT_DIRECTORY not in sys.path): + sys.path.insert(0, PROJECT_ROOT_DIRECTORY) + os.putenv('PYTHONPATH', PROJECT_ROOT_DIRECTORY) # for subprocesses + +# need to import gi.repository.Gio before gobject +from indicator_weather.helpers import * + import gtk, pygtk, gobject, pynotify pygtk.require('2.0') import appindicator @@ -42,19 +55,6 @@ from gettext import ngettext as __ gettext.textdomain('indicator-weather') -# Add project root directory (enable symlink, and trunk execution). -PROJECT_ROOT_DIRECTORY = os.path.abspath( - os.path.dirname(os.path.dirname(os.path.realpath(sys.argv[0])))) - -if (os.path.exists(os.path.join(PROJECT_ROOT_DIRECTORY, 'indicator_weather')) - and PROJECT_ROOT_DIRECTORY not in sys.path): - sys.path.insert(0, PROJECT_ROOT_DIRECTORY) - os.putenv('PYTHONPATH', PROJECT_ROOT_DIRECTORY) # for subprocesses - -from indicator_weather.helpers import get_builder -from indicator_weather.helpers import monitor_http_proxy -from indicator_weather.helpers import monitor_upower - class Settings: """ Class to read/write settings """ db = None @@ -669,11 +669,13 @@ # Get sunrise label def get_sunrise_label(self): - return "%s: %s" % (_("Sunrise"), self.__sunrise_t.strftime('%X').replace(":00", "")) + return "%s: %s" % (_("Sunrise"), + TimeFormatter.format_time(self.__sunrise_t)) # Get sunset label def get_sunset_label(self): - return "%s: %s" % (_("Sunset"), self.__sunset_t.strftime('%X').replace(":00", "")) + return "%s: %s" % (_("Sunset"), + TimeFormatter.format_time(self.__sunset_t)) # Additional functions # Convert wind direction from degrees to localized direction @@ -1667,6 +1669,8 @@ log.info("Another instance of this program is already running") sys.exit(_("Another instance of this program is already running")) + # Load indicator-datetime time-format settings + TimeFormatter.monitor_indicator_datetime(log) # Set http proxy support monitor_http_proxy(log) === modified file 'indicator_weather/helpers.py' --- indicator_weather/helpers.py 2011-04-02 08:54:05 +0000 +++ indicator_weather/helpers.py 2011-04-03 21:21:29 +0000 @@ -18,9 +18,15 @@ """Helpers for an Ubuntu application.""" __all__ = [ - 'make_window', + 'get_builder', + 'monitor_upower', + 'monitor_http_proxy', + 'TimeFormatter', ] +# this has to be done before any glib/gtk import +# because it overrides some types +from gi.repository import Gio import os import gtk import urllib2 @@ -139,3 +145,85 @@ on_http_proxy_changed_handler(client, data=log) # set hook for changes client.notify_add("/system/http_proxy", on_http_proxy_changed_handler, log) + +class TimeFormatter: + """ + Formats a time object with respect to the settings of indicator-datetime + """ + + # default format from locale + format = "%X" + + # indicator-datetime dconf schemas + SCHEMAS = ( + "com.canonical.indicator.datetime", #natty + "org.ayatana.indicator.datetime", #maverick + ) + + # time-format values + SETTINGS_TIME_LOCALE = 0 + SETTINGS_TIME_12_HOUR = 1 + SETTINGS_TIME_24_HOUR = 2 + SETTINGS_TIME_CUSTOM = 3 + + @staticmethod + def format_time(t): + """ do the format and strip :00 secs """ + s = t.strftime(TimeFormatter.format) + if t.second == 0: + if t.minute!=0: + return s.replace(":00", "") + else: + return s.replace(":00:00", ":00") + else: + return s + + @staticmethod + def calc_format(settings, changed_key=None): + """ settings init or changed """ + + if changed_key is not None and changed_key not in ( + "time-format", "show-seconds"): + return + + time_format = settings.get_enum("time-format") + show_seconds = settings.get_boolean("show-seconds") + + if time_format == TimeFormatter.SETTINGS_TIME_24_HOUR: + if show_seconds: + TimeFormatter.format = "%H:%M:%S" + else: + TimeFormatter.format = "%H:%M" + + elif time_format == TimeFormatter.SETTINGS_TIME_12_HOUR: + if show_seconds: + TimeFormatter.format = "%l:%M:%S %p" + else: + TimeFormatter.format = "%l:%M %p" + + elif time_format == TimeFormatter.SETTINGS_TIME_CUSTOM: + # ignore this as it might contain date params + #TimeFormatter.format = gsettings.get_string("custom-time-format") + TimeFormatter.format = "%X" + + elif time_format == TimeFormatter.SETTINGS_TIME_LOCALE: + # datetime indicator does not use %X + # it always uses 12 hour format unless translated otherwise + TimeFormatter.format = "%X" + + TimeFormatter.log.debug("TimeFormatter: new time format: %s" % \ + TimeFormatter.format) + + @staticmethod + def monitor_indicator_datetime(log): + TimeFormatter.log=log + schemas = Gio.Settings.list_schemas() + for schema in TimeFormatter.SCHEMAS: + if schema in schemas: + log.debug("TimeFormatter: loading indicator-datetime settings: %s" % schema) + TimeFormatter.settings = Gio.Settings.new(schema) + TimeFormatter.calc_format(TimeFormatter.settings) + TimeFormatter.settings.connect("changed", TimeFormatter.calc_format) + break + else: + log.debug("TimeFormatter: indicator-datetime settings not found")