From 04df3eede6a39856fc23f8250d1d8db67e31fbea Mon Sep 17 00:00:00 2001 From: Neroth Date: Fri, 27 Apr 2012 02:37:56 +0200 Subject: [PATCH] Control for the wind speed unit --- po/fr.po | 4 ++ src/extension.js | 66 ++++++++++++++++++- ...me.shell.extensions.weather.gschema.xml.in | 13 +++- src/weather-settings.js.in | 17 +++++ 4 files changed, 98 insertions(+), 2 deletions(-) diff --git a/po/fr.po b/po/fr.po index 8f4910f..29f8f52 100644 --- a/po/fr.po +++ b/po/fr.po @@ -291,6 +291,10 @@ msgstr "Traduire les conditions" msgid "Temperature Unit" msgstr "Unité de température" +#: weather-settings.js +msgid "Wind Speed Unit" +msgstr "Unité de vitesse du vent" + #: weather-settings.js msgid "Position in Panel" msgstr "Position sur le panel" diff --git a/src/extension.js b/src/extension.js index b9a10c5..0bccf98 100644 --- a/src/extension.js +++ b/src/extension.js @@ -49,6 +49,7 @@ const PopupMenu = imports.ui.popupMenu; // Settings const WEATHER_SETTINGS_SCHEMA = 'org.gnome.shell.extensions.weather'; const WEATHER_UNIT_KEY = 'unit'; +const WEATHER_WIND_SPEED_UNIT_KEY = 'wind-speed-unit'; const WEATHER_CITY_KEY = 'city'; const WEATHER_ACTUAL_CITY_KEY = 'actual-city'; const WEATHER_TRANSLATE_CONDITION_KEY = 'translate-condition'; @@ -63,12 +64,24 @@ const WeatherUnits = { CELSIUS: 0, FAHRENHEIT: 1 } + +const WeatherWindSpeedUnits = { + KPH: 0, + MPH: 1, + MPS: 2, + KNOTS: 3 +} + const WeatherPosition = { CENTER: 0, RIGHT: 1, LEFT: 2 } +const WEATHER_CONV_MPH_IN_MPS = 2.23693629; +const WEATHER_CONV_KPH_IN_MPS = 3.6; +const WEATHER_CONV_KNOTS_IN_MPS = 1.94384449; + // Soup session (see https://bugzilla.gnome.org/show_bug.cgi?id=661323#c64) (Simon Legner) const _httpSession = new Soup.SessionAsync(); Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefault()); @@ -221,6 +234,20 @@ WeatherMenuButton.prototype = { this._settings.set_enum(WEATHER_UNIT_KEY,v); }, + get _wind_speed_units() + { + if(!this._settings) + this.loadConfig(); + return this._settings.get_enum(WEATHER_WIND_SPEED_UNIT_KEY); + }, + + set _wind_speed_units(v) + { + if(!this._settings) + this.loadConfig(); + this._settings.set_enum(WEATHER_WIND_SPEED_UNIT_KEY,v); + }, + get _cities() { if(!this._settings) @@ -739,10 +766,47 @@ global.log(a); this._currentWeatherTemperature.text = temperature + ' ' + this.unit_to_unicode(); this._currentWeatherHumidity.text = humidity; this._currentWeatherPressure.text = pressure + ' ' + pressure_unit; - this._currentWeatherWind.text = (wind_direction && wind > 0 ? wind_direction + ' ' : '') + wind + ' ' + wind_unit; this._currentWeatherSunrise.text = sunrise; this._currentWeatherSunset.text = sunset; + // Override wind units with our preference + // Need to consider what units the Yahoo API has returned it in + switch (this._wind_speed_units) { + case WeatherWindSpeedUnits.KPH: + // Round to whole units + if (this._units == WeatherUnits.FAHRENHEIT) { + wind = Math.round (wind / WEATHER_CONV_MPH_IN_MPS * WEATHER_CONV_KPH_IN_MPS); + wind_unit = 'km/h'; + } + // Otherwise no conversion needed - already in correct units + break; + case WeatherWindSpeedUnits.MPH: + // Round to whole units + if (this._units == WeatherUnits.CELSIUS) { + wind = Math.round (wind / WEATHER_CONV_KPH_IN_MPS * WEATHER_CONV_MPH_IN_MPS); + wind_unit = 'mph'; + } + // Otherwise no conversion needed - already in correct units + break; + case WeatherWindSpeedUnits.MPS: + // Precision to one decimal place as 1 m/s is quite a large unit + if (this._units == WeatherUnits.CELSIUS) + wind = Math.round ((wind / WEATHER_CONV_KPH_IN_MPS) * 10)/ 10; + else + wind = Math.round ((wind / WEATHER_CONV_MPH_IN_MPS) * 10)/ 10; + wind_unit = 'm/s'; + break; + case WeatherWindSpeedUnits.KNOTS: + // Round to whole units + if (this._units == WeatherUnits.CELSIUS) + wind = Math.round (wind / WEATHER_CONV_KPH_IN_MPS * WEATHER_CONV_KNOTS_IN_MPS); + else + wind = Math.round (wind / WEATHER_CONV_MPH_IN_MPS * WEATHER_CONV_KNOTS_IN_MPS); + wind_unit = 'knots'; + break; + } + this._currentWeatherWind.text = (wind_direction && wind > 0 ? wind_direction + ' ' : '') + wind + ' ' + wind_unit; + // Refresh forecast let date_string = [_('Today'), _('Tomorrow')]; for (let i = 0; i <= 1; i++) { diff --git a/src/org.gnome.shell.extensions.weather.gschema.xml.in b/src/org.gnome.shell.extensions.weather.gschema.xml.in index 78e75d2..58a0a1c 100644 --- a/src/org.gnome.shell.extensions.weather.gschema.xml.in +++ b/src/org.gnome.shell.extensions.weather.gschema.xml.in @@ -3,6 +3,12 @@ + + + + + + @@ -10,9 +16,14 @@ - 'celsius' + 'fahrenheit' <_summary>Temperature Unit + + 'mph' + <_summary>Wind Speed Units + <_description>Choose the units used for wind speed. Allowed values are 'kph', 'mph', 'm/s' or 'knots'. + 'Cambridge, MA' <_summary>City to be displayed diff --git a/src/weather-settings.js.in b/src/weather-settings.js.in index 4896138..dc5319f 100644 --- a/src/weather-settings.js.in +++ b/src/weather-settings.js.in @@ -33,6 +33,7 @@ const _ = Gettext.gettext; const WEATHER_SETTINGS_SCHEMA = 'org.gnome.shell.extensions.weather'; const WEATHER_UNIT_KEY = 'unit'; +const WEATHER_WIND_SPEED_UNIT_KEY = 'wind-speed-unit'; const WEATHER_CITY_KEY = 'city'; const WEATHER_ACTUAL_CITY_KEY = 'actual-city'; const WEATHER_TRANSLATE_CONDITION_KEY = 'translate-condition'; @@ -180,6 +181,20 @@ WeatherSetting.prototype = this.Settings.set_enum(WEATHER_UNIT_KEY,arguments[0]); }, + get wind_speed_unit() + { + if(!this.Settings) + this.loadConfig(); + return this.Settings.get_enum(WEATHER_WIND_SPEED_UNIT_KEY); + }, + + set wind_speed_unit(v) + { + if(!this.Settings) + this.loadConfig(); + this.Settings.set_enum(WEATHER_WIND_SPEED_UNIT_KEY,v); + }, + get city() { if(!this.Settings) @@ -471,6 +486,8 @@ WeatherSetting.prototype = this.initConfigWidget(); this.addLabel(_("Temperature Unit")); this.addComboBox(["°C","°F"],"units"); + this.addLabel(_("Wind Speed Unit")); + this.addComboBox(["km/h","mph","m/s","knots"],"wind_speed_unit"); this.addLabel(_("Position in Panel")); this.addComboBox([_("Center"),_("Right"),_("Left")],"position_in_panel"); this.addLabel(_("Translate Conditions"));