diff --git a/data/stylesheet.css b/data/stylesheet.css index 34362c2..c578546 100644 --- a/data/stylesheet.css +++ b/data/stylesheet.css @@ -1,60 +1,60 @@ .weather-current-summarybox { - padding-top:5px; +padding-top:5px; } .weather-current-summary { - font-size: 31px; +font-size: 31px; } .weather-current-databox { - padding: 5px 15px 0px; +padding: 5px 15px 0px; } .weather-current-icon { - padding-right: 30px; +padding-right: 30px; } .weather-current-iconbox { - padding-right: 20px; +padding-right: 20px; } .weather-icon { - padding-right: 5px; +padding-right: 5px; } .weather-icon-rtl { - padding-left: 5px; +padding-left: 5px; } .weather-current-databox-captions { - text-align: right; - padding-right: 5px; - color: #999999; +text-align: right; +padding-right: 5px; +color: #999999; } .weather-forecast-icon { - padding-right: 15px; +padding-right: 15px; } .weather-forecast-databox { - padding-right: 30px; +padding-right: 30px; } .weather-forecast-day { - color: #999999; - font-size: 90%; +color: #999999; +font-size: 90%; } .weather-sunrise-icon { - padding-right: 5px; +padding-right: 5px; } .weather-sunset-icon { - padding-left: 15px; - padding-right: 5px; +padding-left: 15px; +padding-right: 5px; } .weather-build-icon { - padding-left: 15px; - padding-right: 5px; +padding-left: 15px; +padding-right: 5px; } diff --git a/po/update.js b/po/update.js old mode 100755 new mode 100644 index 4a2833a..105346c --- a/po/update.js +++ b/po/update.js @@ -30,26 +30,24 @@ Gio = imports.gi.Gio; print("Generate gnome-shell-extension-weather.pot"); var xgettext = Seed.spawn("xgettext -o gnome-shell-extension-weather.pot -L python --from-code=utf-8 --keyword=_ -f POTFILES.in"); - if(xgettext.stderr) - print(xgettext.stderr); - else - { - var file = Gio.file_new_for_path("."); - var enumerator = file.enumerate_children("standard::name,standard::size"); - var linguas = ""; - var n = ""; - var i = 0; - while(child = enumerator.next_file()) - if(child.get_name().search(/.po$/) != -1) - { - print("Generate "+child.get_name()); - linguas += n+(child.get_name().split(".po")[0]); - Seed.spawn("msgmerge -U "+child.get_name()+" gnome-shell-extension-weather.pot"); - n = "\n"; - i++; - } +if (xgettext.stderr) + print(xgettext.stderr); +else { + var file = Gio.file_new_for_path("."); + var enumerator = file.enumerate_children("standard::name,standard::size"); + var linguas = ""; + var n = ""; + var i = 0; + while (child = enumerator.next_file()) + if (child.get_name().search(/.po$/) != -1) { + print("Generate " + child.get_name()); + linguas += n + (child.get_name().split(".po")[0]); + Seed.spawn("msgmerge -U " + child.get_name() + " gnome-shell-extension-weather.pot"); + n = "\n"; + i++; + } - print("Write LINGUAS file"); - Gio.simple_write("LINGUAS",linguas); - print("Successfully generated "+i+" entry"); - } + print("Write LINGUAS file"); + Gio.simple_write("LINGUAS", linguas); + print("Successfully generated " + i + " entry"); +} diff --git a/src/convenience.js b/src/convenience.js index 4fc44ab..1fb9ede 100644 --- a/src/convenience.js +++ b/src/convenience.js @@ -24,7 +24,6 @@ (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ - const Gettext = imports.gettext; const Gio = imports.gi.Gio; @@ -78,15 +77,16 @@ function getSettings(schema) { let schemaSource; if (schemaDir.query_exists(null)) schemaSource = GioSSS.new_from_directory(schemaDir.get_path(), - GioSSS.get_default(), - false); + GioSSS.get_default(), + false); else schemaSource = GioSSS.get_default(); let schemaObj = schemaSource.lookup(schema, true); if (!schemaObj) - throw new Error('Schema ' + schema + ' could not be found for extension ' - + extension.metadata.uuid + '. Please check your installation.'); + throw new Error('Schema ' + schema + ' could not be found for extension ' + extension.metadata.uuid + '. Please check your installation.'); - return new Gio.Settings({ settings_schema: schemaObj }); + return new Gio.Settings({ + settings_schema: schemaObj + }); } diff --git a/src/extension.js b/src/extension.js index 18fcce6..4d1de79 100644 --- a/src/extension.js +++ b/src/extension.js @@ -82,24 +82,24 @@ const WeatherUnits = { } const WeatherWindSpeedUnits = { - KPH: 0, - MPH: 1, - MPS: 2, - KNOTS: 3, - FPS: 4, - BEAUFORT: 5 + KPH: 0, + MPH: 1, + MPS: 2, + KNOTS: 3, + FPS: 4, + BEAUFORT: 5 } const WeatherPressureUnits = { - hPa: 0, - inHg: 1, - bar: 2, - Pa: 3, - kPa: 4, - atm: 5, - at: 6, - Torr: 7, - psi: 8 + hPa: 0, + inHg: 1, + bar: 2, + Pa: 3, + kPa: 4, + atm: 5, + at: 6, + Torr: 7, + psi: 8 } const WeatherPosition = { @@ -118,642 +118,659 @@ const _httpSession = new Soup.SessionAsync(); Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefault()); const WeatherMenuButton = new Lang.Class({ - Name: 'WeatherMenuButton', - - Extends: PanelMenu.Button, - - _init: function() { - // Load settings - this.loadConfig(); - - // Label - this._weatherInfo = new St.Label({ text: _('...') }); - - if(typeof St.TextDirection == "undefined") - { - // Panel icon - this._weatherIcon = new St.Icon({ - icon_name: 'view-refresh'+this.icon_type(), - style_class: 'system-status-icon weather-icon' + (Main.panel.actor.get_text_direction() == Clutter.TextDirection.RTL ? '-rtl' : '') - }); - - // Panel menu item - the current class - let menuAlignment = 0.25; - if (Clutter.get_default_text_direction() == Clutter.TextDirection.RTL) - menuAlignment = 1.0 - menuAlignment; - this.parent(menuAlignment); - } - else - { - // Panel icon - this._weatherIcon = new St.Icon({ - icon_name: 'view-refresh'+this.icon_type(), - style_class: 'system-status-icon weather-icon' + (Main.panel.actor.get_direction() == St.TextDirection.RTL ? '-rtl' : '') - }); - - // Panel menu item - the current class - let menuAlignment = 0.25; - if (St.Widget.get_default_direction() == St.TextDirection.RTL) - menuAlignment = 1.0 - menuAlignment; - PanelMenu.Button.prototype._init.call(this, menuAlignment); - } - - // Putting the panel item together - let topBox = new St.BoxLayout(); - topBox.add_actor(this._weatherIcon); - topBox.add_actor(this._weatherInfo); - this.actor.add_actor(topBox); - - let dummyBox = new St.BoxLayout(); - this.actor.reparent(dummyBox); - dummyBox.remove_actor(this.actor); - dummyBox.destroy(); - - let children = null; - switch (this._position_in_panel) { - case WeatherPosition.LEFT: - children = Main.panel._leftBox.get_children(); - Main.panel._leftBox.insert_child_at_index(this.actor, children.length); - break; - case WeatherPosition.CENTER: - children = Main.panel._centerBox.get_children(); - Main.panel._centerBox.insert_child_at_index(this.actor, children.length); - break; - case WeatherPosition.RIGHT: - children = Main.panel._rightBox.get_children(); - Main.panel._rightBox.insert_child_at_index(this.actor, 0); - break; - } - if(typeof Main.panel._menus == "undefined") - Main.panel.menuManager.addMenu(this.menu); - else - Main.panel._menus.addMenu(this.menu); - - this._old_position_in_panel = this._position_in_panel; - - // Current weather - this._currentWeather = new St.Bin({ style_class: 'current' }); - // Future weather - this._futureWeather = new St.Bin({ style_class: 'forecast'}); - - // Putting the popup item together - let _itemCurrent = new PopupMenu.PopupBaseMenuItem({reactive:false}); - let _itemFuture = new PopupMenu.PopupBaseMenuItem({reactive:false}); - - if(ExtensionUtils.versionCheck(['3.9','3.10'], Config.PACKAGE_VERSION)) - { - _itemCurrent.actor.add_actor(this._currentWeather); - _itemFuture.actor.add_actor(this._futureWeather); - } - else - { - _itemCurrent.addActor(this._currentWeather); - _itemFuture.addActor(this._futureWeather); - } + Name: 'WeatherMenuButton', + + Extends: PanelMenu.Button, + + _init: function() { + // Load settings + this.loadConfig(); + + // Label + this._weatherInfo = new St.Label({ + text: _('...') + }); + + if (typeof St.TextDirection == "undefined") { + // Panel icon + this._weatherIcon = new St.Icon({ + icon_name: 'view-refresh' + this.icon_type(), + style_class: 'system-status-icon weather-icon' + (Main.panel.actor.get_text_direction() == Clutter.TextDirection.RTL ? '-rtl' : '') + }); + + // Panel menu item - the current class + let menuAlignment = 0.25; + if (Clutter.get_default_text_direction() == Clutter.TextDirection.RTL) + menuAlignment = 1.0 - menuAlignment; + this.parent(menuAlignment); + } else { + // Panel icon + this._weatherIcon = new St.Icon({ + icon_name: 'view-refresh' + this.icon_type(), + style_class: 'system-status-icon weather-icon' + (Main.panel.actor.get_direction() == St.TextDirection.RTL ? '-rtl' : '') + }); + + // Panel menu item - the current class + let menuAlignment = 0.25; + if (St.Widget.get_default_direction() == St.TextDirection.RTL) + menuAlignment = 1.0 - menuAlignment; + PanelMenu.Button.prototype._init.call(this, menuAlignment); + } + + // Putting the panel item together + let topBox = new St.BoxLayout(); + topBox.add_actor(this._weatherIcon); + topBox.add_actor(this._weatherInfo); + this.actor.add_actor(topBox); + + let dummyBox = new St.BoxLayout(); + this.actor.reparent(dummyBox); + dummyBox.remove_actor(this.actor); + dummyBox.destroy(); + + let children = null; + switch (this._position_in_panel) { + case WeatherPosition.LEFT: + children = Main.panel._leftBox.get_children(); + Main.panel._leftBox.insert_child_at_index(this.actor, children.length); + break; + case WeatherPosition.CENTER: + children = Main.panel._centerBox.get_children(); + Main.panel._centerBox.insert_child_at_index(this.actor, children.length); + break; + case WeatherPosition.RIGHT: + children = Main.panel._rightBox.get_children(); + Main.panel._rightBox.insert_child_at_index(this.actor, 0); + break; + } + if (typeof Main.panel._menus == "undefined") + Main.panel.menuManager.addMenu(this.menu); + else + Main.panel._menus.addMenu(this.menu); + + this._old_position_in_panel = this._position_in_panel; + + // Current weather + this._currentWeather = new St.Bin({ + style_class: 'current' + }); + // Future weather + this._futureWeather = new St.Bin({ + style_class: 'forecast' + }); + + // Putting the popup item together + let _itemCurrent = new PopupMenu.PopupBaseMenuItem({ + reactive: false + }); + let _itemFuture = new PopupMenu.PopupBaseMenuItem({ + reactive: false + }); + + if (ExtensionUtils.versionCheck(['3.9', '3.10'], Config.PACKAGE_VERSION)) { + _itemCurrent.actor.add_actor(this._currentWeather); + _itemFuture.actor.add_actor(this._futureWeather); + } else { + _itemCurrent.addActor(this._currentWeather); + _itemFuture.addActor(this._futureWeather); + } + + this.menu.addMenuItem(_itemCurrent); + + let item = new PopupMenu.PopupSeparatorMenuItem(); + this.menu.addMenuItem(item); + + this.menu.addMenuItem(_itemFuture); + + let item = new PopupMenu.PopupSeparatorMenuItem(); + this.menu.addMenuItem(item); + + this._selectCity = new PopupMenu.PopupSubMenuMenuItem(_("Locations")); + this.menu.addMenuItem(this._selectCity); + this.rebuildSelectCityItem(); + + let item = new PopupMenu.PopupMenuItem(_("Reload Weather Information")); + item.connect('activate', Lang.bind(this, function() { + this.refreshWeather(false); + })); + this.menu.addMenuItem(item); + + let item = new PopupMenu.PopupMenuItem(_("Weather Settings")); + item.connect('activate', Lang.bind(this, this._onPreferencesActivate)); + this.menu.addMenuItem(item); + + // Items + this.showLoadingUi(); + + this.rebuildCurrentWeatherUi(); + this.rebuildFutureWeatherUi(); + + // Show weather + this.refreshWeather(true); + }, + + stop: function() { + if (this._timeoutS) + Mainloop.source_remove(this._timeoutS); + + if (this._settingsC) { + this._settings.disconnect(this._settingsC); + this._settingsC = 0; + } + + if (this._settingsInterfaceC) { + this._settingsInterface.disconnect(this._settingsInterfaceC); + this._settingsInterfaceC = 0; + } + }, + + loadConfig: function() { + let that = this; + this._settings = Convenience.getSettings(WEATHER_SETTINGS_SCHEMA); + this._settingsC = this._settings.connect("changed", function() { + that.refreshWeather(false); + }); + }, + + loadConfigInterface: function() { + let that = this; + let schemaInterface = "org.gnome.desktop.interface"; + if (Gio.Settings.list_schemas().indexOf(schemaInterface) == -1) + throw _("Schema \"%s\" not found.").replace("%s", schemaInterface); + this._settingsInterface = new Gio.Settings({ + schema: schemaInterface + }); + this._settingsInterfaceC = this._settingsInterface.connect("changed", function() { + that.refreshWeather(false); + }); + }, + + get _clockFormat() { + if (!this._settingsInterface) + this.loadConfigInterface(); + return this._settingsInterface.get_string("clock-format"); + }, + + get _units() { + if (!this._settings) + this.loadConfig(); + return this._settings.get_enum(WEATHER_UNIT_KEY); + }, + + set _units(v) { + if (!this._settings) + this.loadConfig(); + 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 _wind_direction() { + if (!this._settings) + this.loadConfig(); + return this._settings.get_boolean(WEATHER_WIND_DIRECTION_KEY); + }, + + set _wind_direction(v) { + if (!this._settings) + this.loadConfig(); + return this._settings.set_boolean(WEATHER_WIND_DIRECTION_KEY, v); + }, + + get _pressure_units() { + if (!this._settings) + this.loadConfig(); + return this._settings.get_enum(WEATHER_PRESSURE_UNIT_KEY); + }, + + set _pressure_units(v) { + if (!this._settings) + this.loadConfig(); + this._settings.set_enum(WEATHER_PRESSURE_UNIT_KEY, v); + }, + + get _cities() { + if (!this._settings) + this.loadConfig(); + return this._settings.get_string(WEATHER_CITY_KEY); + }, + + set _cities(v) { + if (!this._settings) + this.loadConfig(); + this._settings.set_string(WEATHER_CITY_KEY, v); + }, + + get _actual_city() { + if (!this._settings) + this.loadConfig(); + var a = this._settings.get_int(WEATHER_ACTUAL_CITY_KEY); + var b = a; + var cities = this._cities.split(" && "); + + if (typeof cities != "object") + cities = [cities]; + + var l = cities.length - 1; + + if (a < 0) + a = 0; + + if (l < 0) + l = 0; + + if (a > l) + a = l; + + return a; + }, + + set _actual_city(a) { + if (!this._settings) + this.loadConfig(); + var cities = this._cities.split(" && "); + + if (typeof cities != "object") + cities = [cities]; + + var l = cities.length - 1; + + if (a < 0) + a = 0; - this.menu.addMenuItem(_itemCurrent); - - let item = new PopupMenu.PopupSeparatorMenuItem(); - this.menu.addMenuItem(item); - - this.menu.addMenuItem(_itemFuture); - - let item = new PopupMenu.PopupSeparatorMenuItem(); - this.menu.addMenuItem(item); - - this._selectCity = new PopupMenu.PopupSubMenuMenuItem(_("Locations")); - this.menu.addMenuItem(this._selectCity); - this.rebuildSelectCityItem(); - - let item = new PopupMenu.PopupMenuItem(_("Reload Weather Information")); - item.connect('activate', Lang.bind(this, function(){this.refreshWeather(false);})); - this.menu.addMenuItem(item); - - let item = new PopupMenu.PopupMenuItem(_("Weather Settings")); - item.connect('activate', Lang.bind(this, this._onPreferencesActivate)); - this.menu.addMenuItem(item); - - // Items - this.showLoadingUi(); - - this.rebuildCurrentWeatherUi(); - this.rebuildFutureWeatherUi(); - - // Show weather - this.refreshWeather(true); - }, - - stop : function() - { - if(this._timeoutS) - Mainloop.source_remove(this._timeoutS); - - if(this._settingsC) - { - this._settings.disconnect(this._settingsC); - this._settingsC = 0; - } - - if(this._settingsInterfaceC) - { - this._settingsInterface.disconnect(this._settingsInterfaceC); - this._settingsInterfaceC = 0; - } - }, - - loadConfig : function() - { - let that = this; - this._settings = Convenience.getSettings(WEATHER_SETTINGS_SCHEMA); - this._settingsC = this._settings.connect("changed",function(){that.refreshWeather(false);}); - }, - - loadConfigInterface : function() - { - let that = this; - let schemaInterface = "org.gnome.desktop.interface"; - if (Gio.Settings.list_schemas().indexOf(schemaInterface) == -1) - throw _("Schema \"%s\" not found.").replace("%s",schemaInterface); - this._settingsInterface = new Gio.Settings({ schema: schemaInterface }); - this._settingsInterfaceC = this._settingsInterface.connect("changed",function(){that.refreshWeather(false);}); - }, - - get _clockFormat() - { - if(!this._settingsInterface) - this.loadConfigInterface(); - return this._settingsInterface.get_string("clock-format"); - }, - - get _units() - { - if(!this._settings) - this.loadConfig(); - return this._settings.get_enum(WEATHER_UNIT_KEY); - }, - - set _units(v) - { - if(!this._settings) - this.loadConfig(); - 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 _wind_direction() - { - if(!this._settings) - this.loadConfig(); - return this._settings.get_boolean(WEATHER_WIND_DIRECTION_KEY); - }, - - set _wind_direction(v) - { - if(!this._settings) - this.loadConfig(); - return this._settings.set_boolean(WEATHER_WIND_DIRECTION_KEY,v); - }, - - get _pressure_units() - { - if(!this._settings) - this.loadConfig(); - return this._settings.get_enum(WEATHER_PRESSURE_UNIT_KEY); - }, - - set _pressure_units(v) - { - if(!this._settings) - this.loadConfig(); - this._settings.set_enum(WEATHER_PRESSURE_UNIT_KEY,v); - }, - - get _cities() - { - if(!this._settings) - this.loadConfig(); - return this._settings.get_string(WEATHER_CITY_KEY); - }, - - set _cities(v) - { - if(!this._settings) - this.loadConfig(); - this._settings.set_string(WEATHER_CITY_KEY,v); - }, - - get _actual_city() - { - if(!this._settings) - this.loadConfig(); - var a = this._settings.get_int(WEATHER_ACTUAL_CITY_KEY); - var b = a; - var cities = this._cities.split(" && "); - - if(typeof cities != "object") - cities = [cities]; - - var l = cities.length-1; - - if(a < 0) - a = 0; - - if(l < 0) - l = 0; - - if(a > l) - a = l; - - return a; - }, - - set _actual_city(a) - { - if(!this._settings) - this.loadConfig(); - var cities = this._cities.split(" && "); - - if(typeof cities != "object") - cities = [cities]; - - var l = cities.length-1; - - if(a < 0) - a = 0; - - if(l < 0) - l = 0; - - if(a > l) - a = l; - - this._settings.set_int(WEATHER_ACTUAL_CITY_KEY,a); - }, - - get _city() - { - let cities = this._cities; - let cities = cities.split(" && "); - if(cities && typeof cities == "string") - cities = [cities]; - if(!cities[0]) - return ""; - cities = cities[this._actual_city]; - return cities; - }, - - set _city(v) - { - let cities = this._cities; - cities = cities.split(" && "); - if(cities && typeof cities == "string") - cities = [cities]; - if(!cities[0]) - cities = []; - cities.splice(this.actual_city,1,v); - cities = cities.join(" && "); - if(typeof cities != "string") - cities = cities[0]; - this._cities = cities; - }, - - get _translate_condition() - { - if(!this._settings) - this.loadConfig(); - return this._settings.get_boolean(WEATHER_TRANSLATE_CONDITION_KEY); - }, - - set _translate_condition(v) - { - if(!this._settings) - this.loadConfig(); - this._settings.set_boolean(WEATHER_TRANSLATE_CONDITION_KEY,v); - }, - - get _icon_type() - { - if(!this._settings) - this.loadConfig(); - return this._settings.get_boolean(WEATHER_USE_SYMBOLIC_ICONS_KEY) ? 1 : 0; - }, - - set _icon_type(v) - { - if(!this._settings) - this.loadConfig(); - this._settings.set_boolean(WEATHER_USE_SYMBOLIC_ICONS_KEY,v); - }, - - get _text_in_panel() - { - if(!this._settings) - this.loadConfig(); - return this._settings.get_boolean(WEATHER_SHOW_TEXT_IN_PANEL_KEY); - }, - - set _text_in_panel(v) - { - if(!this._settings) - this.loadConfig(); - this._settings.set_boolean(WEATHER_SHOW_TEXT_IN_PANEL_KEY,v); - }, - - get _position_in_panel() - { - if(!this._settings) - this.loadConfig(); - return this._settings.get_enum(WEATHER_POSITION_IN_PANEL_KEY); - }, - - set _position_in_panel(v) - { - if(!this._settings) - this.loadConfig(); - this._settings.set_enum(WEATHER_POSITION_IN_PANEL_KEY,v); - }, - - get _comment_in_panel() - { - if(!this._settings) - this.loadConfig(); - return this._settings.get_boolean(WEATHER_SHOW_COMMENT_IN_PANEL_KEY); - }, - - set _comment_in_panel(v) - { - if(!this._settings) - this.loadConfig(); - this._settings.set_boolean(WEATHER_SHOW_COMMENT_IN_PANEL_KEY,v); - }, - - get _refresh_interval() - { - if(!this._settings) - this.loadConfig(); - return this._settings.get_int(WEATHER_REFRESH_INTERVAL); - }, - - set _refresh_interval(v) - { - if(!this._settings) - this.loadConfig(); - this._settings.set_int(WEATHER_REFRESH_INTERVAL,v); - }, - - rebuildSelectCityItem : function() - { - let that = this; - this._selectCity.menu.removeAll(); - let item = null; - - let cities = this._cities; - cities = cities.split(" && "); - if(cities && typeof cities == "string") - cities = [cities]; - if(!cities[0]) - return 0; - - for(let i = 0; cities.length > i; i++) - { - item = new PopupMenu.PopupMenuItem(this.extractLocation(cities[i])); - item.location = i; - if(i == this._actual_city) - { - if(ExtensionUtils.versionCheck(['3.9','3.10'], Config.PACKAGE_VERSION)) + if (l < 0) + l = 0; + + if (a > l) + a = l; + + this._settings.set_int(WEATHER_ACTUAL_CITY_KEY, a); + }, + + get _city() { + let cities = this._cities; + let cities = cities.split(" && "); + if (cities && typeof cities == "string") + cities = [cities]; + if (!cities[0]) + return ""; + cities = cities[this._actual_city]; + return cities; + }, + + set _city(v) { + let cities = this._cities; + cities = cities.split(" && "); + if (cities && typeof cities == "string") + cities = [cities]; + if (!cities[0]) + cities = []; + cities.splice(this.actual_city, 1, v); + cities = cities.join(" && "); + if (typeof cities != "string") + cities = cities[0]; + this._cities = cities; + }, + + get _translate_condition() { + if (!this._settings) + this.loadConfig(); + return this._settings.get_boolean(WEATHER_TRANSLATE_CONDITION_KEY); + }, + + set _translate_condition(v) { + if (!this._settings) + this.loadConfig(); + this._settings.set_boolean(WEATHER_TRANSLATE_CONDITION_KEY, v); + }, + + get _icon_type() { + if (!this._settings) + this.loadConfig(); + return this._settings.get_boolean(WEATHER_USE_SYMBOLIC_ICONS_KEY) ? 1 : 0; + }, + + set _icon_type(v) { + if (!this._settings) + this.loadConfig(); + this._settings.set_boolean(WEATHER_USE_SYMBOLIC_ICONS_KEY, v); + }, + + get _text_in_panel() { + if (!this._settings) + this.loadConfig(); + return this._settings.get_boolean(WEATHER_SHOW_TEXT_IN_PANEL_KEY); + }, + + set _text_in_panel(v) { + if (!this._settings) + this.loadConfig(); + this._settings.set_boolean(WEATHER_SHOW_TEXT_IN_PANEL_KEY, v); + }, + + get _position_in_panel() { + if (!this._settings) + this.loadConfig(); + return this._settings.get_enum(WEATHER_POSITION_IN_PANEL_KEY); + }, + + set _position_in_panel(v) { + if (!this._settings) + this.loadConfig(); + this._settings.set_enum(WEATHER_POSITION_IN_PANEL_KEY, v); + }, + + get _comment_in_panel() { + if (!this._settings) + this.loadConfig(); + return this._settings.get_boolean(WEATHER_SHOW_COMMENT_IN_PANEL_KEY); + }, + + set _comment_in_panel(v) { + if (!this._settings) + this.loadConfig(); + this._settings.set_boolean(WEATHER_SHOW_COMMENT_IN_PANEL_KEY, v); + }, + + get _refresh_interval() { + if (!this._settings) + this.loadConfig(); + return this._settings.get_int(WEATHER_REFRESH_INTERVAL); + }, + + set _refresh_interval(v) { + if (!this._settings) + this.loadConfig(); + this._settings.set_int(WEATHER_REFRESH_INTERVAL, v); + }, + + rebuildSelectCityItem: function() { + let that = this; + this._selectCity.menu.removeAll(); + let item = null; + + let cities = this._cities; + cities = cities.split(" && "); + if (cities && typeof cities == "string") + cities = [cities]; + if (!cities[0]) + return 0; + + for (let i = 0; cities.length > i; i++) { + item = new PopupMenu.PopupMenuItem(this.extractLocation(cities[i])); + item.location = i; + if (i == this._actual_city) { + if (ExtensionUtils.versionCheck(['3.9', '3.10'], Config.PACKAGE_VERSION)) item.setOrnament(PopupMenu.Ornament.DOT); else item.setShowDot(true); } - this._selectCity.menu.addMenuItem(item); - item.connect('activate', function(actor,event) - { - that._actual_city = actor.location; - }); - } - - if (cities.length == 1) - this._selectCity.actor.hide(); - else - this._selectCity.actor.show(); - - return 0; - }, - - extractLocation : function() - { - if(!arguments[0]) - return ""; - - if(arguments[0].search(">") == -1) - return _("Invalid city"); - return arguments[0].split(">")[1]; - }, - - extractWoeid : function() - { - if(!arguments[0]) - return 0; - - if(arguments[0].search(">") == -1) - return 0; - return arguments[0].split(">")[0]; - }, - - updateCities : function() - { - let that = this; - let cities = this._cities; - - cities = cities.split(" && "); - if(cities && typeof cities == "string") - cities = [cities]; - if(!cities[0]) - cities = []; - - if(cities.length == 0) - { - this._cities = "2373572>Cambridge, Massachusetts (US)"; - this.updateCities(); - return 0; - } - - for(let a in cities) - { - if(!this.extractWoeid(cities[a])) - { - this.load_json_async(encodeURI("http://query.yahooapis.com/v1/public/yql?format=json&q=select woeid,name,admin1,country from geo.places where text = '"+cities[a]+"' limit 1"),function() - { - let city = arguments[0].query; - if(typeof city == "object" && typeof city.results == "object") - city = city.results.place; - else - return; - let cityText = city.woeid+">"+city.name; - if(city.admin1) - cityText += ", "+city.admin1.content; - - if(city.country) - cityText += " ("+city.country.code+")"; - - cities.splice(a,1,cityText); - cities = cities.join(" && "); - if(typeof cities != "string") - cities = cities[0]; - that._cities = cities; - that.updateCities(); - }); - return 0; - } - else - continue; - } - this.refreshWeather(); - return 0; - }, - - _onPreferencesActivate : function() { - Util.spawn(["gnome-shell-extension-prefs","weather-extension@jenslody.de"]); - return 0; + this._selectCity.menu.addMenuItem(item); + item.connect('activate', function(actor, event) { + that._actual_city = actor.location; + }); + } + + if (cities.length == 1) + this._selectCity.actor.hide(); + else + this._selectCity.actor.show(); + + return 0; + }, + + extractLocation: function() { + if (!arguments[0]) + return ""; + + if (arguments[0].search(">") == -1) + return _("Invalid city"); + return arguments[0].split(">")[1]; + }, + + extractWoeid: function() { + if (!arguments[0]) + return 0; + + if (arguments[0].search(">") == -1) + return 0; + return arguments[0].split(">")[0]; + }, + + updateCities: function() { + let that = this; + let cities = this._cities; + + cities = cities.split(" && "); + if (cities && typeof cities == "string") + cities = [cities]; + if (!cities[0]) + cities = []; + + if (cities.length == 0) { + this._cities = "2373572>Cambridge, Massachusetts (US)"; + this.updateCities(); + return 0; + } + + for (let a in cities) { + if (!this.extractWoeid(cities[a])) { + this.load_json_async(encodeURI("http://query.yahooapis.com/v1/public/yql?format=json&q=select woeid,name,admin1,country from geo.places where text = '" + cities[a] + "' limit 1"), function() { + let city = arguments[0].query; + if (typeof city == "object" && typeof city.results == "object") + city = city.results.place; + else + return; + let cityText = city.woeid + ">" + city.name; + if (city.admin1) + cityText += ", " + city.admin1.content; + + if (city.country) + cityText += " (" + city.country.code + ")"; + + cities.splice(a, 1, cityText); + cities = cities.join(" && "); + if (typeof cities != "string") + cities = cities[0]; + that._cities = cities; + that.updateCities(); + }); + return 0; + } else + continue; + } + this.refreshWeather(); + return 0; + }, + + _onPreferencesActivate: function() { + Util.spawn(["gnome-shell-extension-prefs", "weather-extension@jenslody.de"]); + return 0; }, unit_to_unicode: function() { - if(this._units == WeatherUnits.FAHRENHEIT) - return '\u00B0\F'; - else if(this._units == WeatherUnits.KELVIN) - return 'K'; - else if(this._units == WeatherUnits.RANKINE) - return '\u00B0\Ra'; - else if(this._units == WeatherUnits.REAUMUR) - return '\u00B0\R\u00E9'; - else if(this._units == WeatherUnits.ROEMER) - return '\u00B0\R\u00F8'; - else if(this._units == WeatherUnits.DELISLE) - return '\u00B0\De'; - else if(this._units == WeatherUnits.NEWTON) - return '\u00B0\N'; - else - return '\u00B0\C'; + if (this._units == WeatherUnits.FAHRENHEIT) + return '\u00B0\F'; + else if (this._units == WeatherUnits.KELVIN) + return 'K'; + else if (this._units == WeatherUnits.RANKINE) + return '\u00B0\Ra'; + else if (this._units == WeatherUnits.REAUMUR) + return '\u00B0\R\u00E9'; + else if (this._units == WeatherUnits.ROEMER) + return '\u00B0\R\u00F8'; + else if (this._units == WeatherUnits.DELISLE) + return '\u00B0\De'; + else if (this._units == WeatherUnits.NEWTON) + return '\u00B0\N'; + else + return '\u00B0\C'; }, get_weather_url: function() { - return encodeURI('http://query.yahooapis.com/v1/public/yql?format=json&q=select * from weather.forecast where woeid = '+this.extractWoeid(this._city)+' and u="f"'); + return encodeURI('http://query.yahooapis.com/v1/public/yql?format=json&q=select * from weather.forecast where woeid = ' + this.extractWoeid(this._city) + ' and u="f"'); }, get_weather_icon: function(code) { /* see http://developer.yahoo.com/weather/#codetable */ /* fallback icons are: weather-clear-night weather-clear weather-few-clouds-night weather-few-clouds weather-fog weather-overcast weather-severe-alert weather-showers weather-showers-scattered weather-snow weather-storm */ switch (parseInt(code, 10)) { - case 0:/* tornado */ - return ['weather-severe-alert']; - case 1:/* tropical storm */ - return ['weather-severe-alert']; - case 2:/* hurricane */ - return ['weather-severe-alert']; - case 3:/* severe thunderstorms */ - return ['weather-severe-alert']; - case 4:/* thunderstorms */ - return ['weather-storm']; - case 5:/* mixed rain and snow */ - return ['weather-snow-rain', 'weather-snow']; - case 6:/* mixed rain and sleet */ - return ['weather-snow-rain', 'weather-snow']; - case 7:/* mixed snow and sleet */ - return ['weather-snow']; - case 8:/* freezing drizzle */ - return ['weather-freezing-rain', 'weather-showers']; - case 9:/* drizzle */ - return ['weather-showers']; - case 10:/* freezing rain */ - return ['weather-freezing-rain', 'weather-showers']; - case 11:/* showers */ - return ['weather-showers']; - case 12:/* showers */ - return ['weather-showers']; - case 13:/* snow flurries */ - return ['weather-snow']; - case 14:/* light snow showers */ - return ['weather-snow']; - case 15:/* blowing snow */ - return ['weather-snow']; - case 16:/* snow */ - return ['weather-snow']; - case 17:/* hail */ - return ['weather-snow']; - case 18:/* sleet */ - return ['weather-snow']; - case 19:/* dust */ - return ['weather-fog']; - case 20:/* foggy */ - return ['weather-fog']; - case 21:/* haze */ - return ['weather-fog']; - case 22:/* smoky */ - return ['weather-fog']; - case 23:/* blustery */ - return ['weather-few-clouds']; - case 24:/* windy */ - return ['weather-few-clouds']; - case 25:/* cold */ - return ['weather-few-clouds']; - case 26:/* cloudy */ - return ['weather-overcast']; - case 27:/* mostly cloudy (night) */ - return ['weather-clouds-night', 'weather-few-clouds-night']; - case 28:/* mostly cloudy (day) */ - return ['weather-clouds', 'weather-overcast']; - case 29:/* partly cloudy (night) */ - return ['weather-few-clouds-night']; - case 30:/* partly cloudy (day) */ - return ['weather-few-clouds']; - case 31:/* clear (night) */ - return ['weather-clear-night']; - case 32:/* sunny */ - return ['weather-clear']; - case 33:/* fair (night) */ - return ['weather-clear-night']; - case 34:/* fair (day) */ - return ['weather-clear']; - case 35:/* mixed rain and hail */ - return ['weather-snow-rain', 'weather-showers']; - case 36:/* hot */ - return ['weather-clear']; - case 37:/* isolated thunderstorms */ - return ['weather-storm']; - case 38:/* scattered thunderstorms */ - return ['weather-storm']; - case 39:/* http://developer.yahoo.com/forum/YDN-Documentation/Yahoo-Weather-API-Wrong-Condition-Code/1290534174000-1122fc3d-da6d-34a2-9fb9-d0863e6c5bc6 */ - case 40:/* scattered showers */ - return ['weather-showers-scattered', 'weather-showers']; - case 41:/* heavy snow */ - return ['weather-snow']; - case 42:/* scattered snow showers */ - return ['weather-snow']; - case 43:/* heavy snow */ - return ['weather-snow']; - case 44:/* partly cloudy */ - return ['weather-few-clouds']; - case 45:/* thundershowers */ - return ['weather-storm']; - case 46:/* snow showers */ - return ['weather-snow']; - case 47:/* isolated thundershowers */ - return ['weather-storm']; - case 3200:/* not available */ + case 0: + /* tornado */ + return ['weather-severe-alert']; + case 1: + /* tropical storm */ + return ['weather-severe-alert']; + case 2: + /* hurricane */ + return ['weather-severe-alert']; + case 3: + /* severe thunderstorms */ + return ['weather-severe-alert']; + case 4: + /* thunderstorms */ + return ['weather-storm']; + case 5: + /* mixed rain and snow */ + return ['weather-snow-rain', 'weather-snow']; + case 6: + /* mixed rain and sleet */ + return ['weather-snow-rain', 'weather-snow']; + case 7: + /* mixed snow and sleet */ + return ['weather-snow']; + case 8: + /* freezing drizzle */ + return ['weather-freezing-rain', 'weather-showers']; + case 9: + /* drizzle */ + return ['weather-showers']; + case 10: + /* freezing rain */ + return ['weather-freezing-rain', 'weather-showers']; + case 11: + /* showers */ + return ['weather-showers']; + case 12: + /* showers */ + return ['weather-showers']; + case 13: + /* snow flurries */ + return ['weather-snow']; + case 14: + /* light snow showers */ + return ['weather-snow']; + case 15: + /* blowing snow */ + return ['weather-snow']; + case 16: + /* snow */ + return ['weather-snow']; + case 17: + /* hail */ + return ['weather-snow']; + case 18: + /* sleet */ + return ['weather-snow']; + case 19: + /* dust */ + return ['weather-fog']; + case 20: + /* foggy */ + return ['weather-fog']; + case 21: + /* haze */ + return ['weather-fog']; + case 22: + /* smoky */ + return ['weather-fog']; + case 23: + /* blustery */ + return ['weather-few-clouds']; + case 24: + /* windy */ + return ['weather-few-clouds']; + case 25: + /* cold */ + return ['weather-few-clouds']; + case 26: + /* cloudy */ + return ['weather-overcast']; + case 27: + /* mostly cloudy (night) */ + return ['weather-clouds-night', 'weather-few-clouds-night']; + case 28: + /* mostly cloudy (day) */ + return ['weather-clouds', 'weather-overcast']; + case 29: + /* partly cloudy (night) */ + return ['weather-few-clouds-night']; + case 30: + /* partly cloudy (day) */ + return ['weather-few-clouds']; + case 31: + /* clear (night) */ + return ['weather-clear-night']; + case 32: + /* sunny */ + return ['weather-clear']; + case 33: + /* fair (night) */ + return ['weather-clear-night']; + case 34: + /* fair (day) */ + return ['weather-clear']; + case 35: + /* mixed rain and hail */ + return ['weather-snow-rain', 'weather-showers']; + case 36: + /* hot */ + return ['weather-clear']; + case 37: + /* isolated thunderstorms */ + return ['weather-storm']; + case 38: + /* scattered thunderstorms */ + return ['weather-storm']; + case 39: + /* http://developer.yahoo.com/forum/YDN-Documentation/Yahoo-Weather-API-Wrong-Condition-Code/1290534174000-1122fc3d-da6d-34a2-9fb9-d0863e6c5bc6 */ + case 40: + /* scattered showers */ + return ['weather-showers-scattered', 'weather-showers']; + case 41: + /* heavy snow */ + return ['weather-snow']; + case 42: + /* scattered snow showers */ + return ['weather-snow']; + case 43: + /* heavy snow */ + return ['weather-snow']; + case 44: + /* partly cloudy */ + return ['weather-few-clouds']; + case 45: + /* thundershowers */ + return ['weather-storm']; + case 46: + /* snow showers */ + return ['weather-snow']; + case 47: + /* isolated thundershowers */ + return ['weather-storm']; + case 3200: + /* not available */ default: return ['weather-severe-alert']; } @@ -763,350 +780,373 @@ const WeatherMenuButton = new Lang.Class({ let iconname = this.get_weather_icon(code); for (let i = 0; i < iconname.length; i++) { if (this.has_icon(iconname[i])) - return iconname[i]+this.icon_type(); + return iconname[i] + this.icon_type(); } - return 'weather-severe-alert'+this.icon_type(); - }, + return 'weather-severe-alert' + this.icon_type(); + }, has_icon: function(icon) { - return Gtk.IconTheme.get_default().has_icon(icon+this.icon_type()); + return Gtk.IconTheme.get_default().has_icon(icon + this.icon_type()); }, get_weather_condition: function(code) { - switch (parseInt(code, 10)){ - case 0:/* tornado */ - return _('Tornado'); - case 1:/* tropical storm */ - return _('Tropical storm'); - case 2:/* hurricane */ - return _('Hurricane'); - case 3:/* severe thunderstorms */ - return _('Severe thunderstorms'); - case 4:/* thunderstorms */ - return _('Thunderstorms'); - case 5:/* mixed rain and snow */ - return _('Mixed rain and snow'); - case 6:/* mixed rain and sleet */ - return _('Mixed rain and sleet'); - case 7:/* mixed snow and sleet */ - return _('Mixed snow and sleet'); - case 8:/* freezing drizzle */ - return _('Freezing drizzle'); - case 9:/* drizzle */ - return _('Drizzle'); - case 10:/* freezing rain */ - return _('Freezing rain'); - case 11:/* showers */ - return _('Showers'); - case 12:/* showers */ - return _('Showers'); - case 13:/* snow flurries */ - return _('Snow flurries'); - case 14:/* light snow showers */ - return _('Light snow showers'); - case 15:/* blowing snow */ - return _('Blowing snow'); - case 16:/* snow */ - return _('Snow'); - case 17:/* hail */ - return _('Hail'); - case 18:/* sleet */ - return _('Sleet'); - case 19:/* dust */ - return _('Dust'); - case 20:/* foggy */ - return _('Foggy'); - case 21:/* haze */ - return _('Haze'); - case 22:/* smoky */ - return _('Smoky'); - case 23:/* blustery */ - return _('Blustery'); - case 24:/* windy */ - return _('Windy'); - case 25:/* cold */ - return _('Cold'); - case 26:/* cloudy */ - return _('Cloudy'); - case 27:/* mostly cloudy (night) */ - case 28:/* mostly cloudy (day) */ - return _('Mostly cloudy'); - case 29:/* partly cloudy (night) */ - case 30:/* partly cloudy (day) */ - return _('Partly cloudy'); - case 31:/* clear (night) */ - return _('Clear'); - case 32:/* sunny */ - return _('Sunny'); - case 33:/* fair (night) */ - case 34:/* fair (day) */ - return _('Fair'); - case 35:/* mixed rain and hail */ - return _('Mixed rain and hail'); - case 36:/* hot */ - return _('Hot'); - case 37:/* isolated thunderstorms */ - return _('Isolated thunderstorms'); - case 38:/* scattered thunderstorms */ - case 39:/* scattered thunderstorms */ - return _('Scattered thunderstorms'); - case 40:/* scattered showers */ - return _('Scattered showers'); - case 41:/* heavy snow */ - return _('Heavy snow'); - case 42:/* scattered snow showers */ - return _('Scattered snow showers'); - case 43:/* heavy snow */ - return _('Heavy snow'); - case 44:/* partly cloudy */ - return _('Partly cloudy'); - case 45:/* thundershowers */ - return _('Thundershowers'); - case 46:/* snow showers */ - return _('Snow showers'); - case 47:/* isolated thundershowers */ - return _('Isolated thundershowers'); - case 3200:/* not available */ + switch (parseInt(code, 10)) { + case 0: + /* tornado */ + return _('Tornado'); + case 1: + /* tropical storm */ + return _('Tropical storm'); + case 2: + /* hurricane */ + return _('Hurricane'); + case 3: + /* severe thunderstorms */ + return _('Severe thunderstorms'); + case 4: + /* thunderstorms */ + return _('Thunderstorms'); + case 5: + /* mixed rain and snow */ + return _('Mixed rain and snow'); + case 6: + /* mixed rain and sleet */ + return _('Mixed rain and sleet'); + case 7: + /* mixed snow and sleet */ + return _('Mixed snow and sleet'); + case 8: + /* freezing drizzle */ + return _('Freezing drizzle'); + case 9: + /* drizzle */ + return _('Drizzle'); + case 10: + /* freezing rain */ + return _('Freezing rain'); + case 11: + /* showers */ + return _('Showers'); + case 12: + /* showers */ + return _('Showers'); + case 13: + /* snow flurries */ + return _('Snow flurries'); + case 14: + /* light snow showers */ + return _('Light snow showers'); + case 15: + /* blowing snow */ + return _('Blowing snow'); + case 16: + /* snow */ + return _('Snow'); + case 17: + /* hail */ + return _('Hail'); + case 18: + /* sleet */ + return _('Sleet'); + case 19: + /* dust */ + return _('Dust'); + case 20: + /* foggy */ + return _('Foggy'); + case 21: + /* haze */ + return _('Haze'); + case 22: + /* smoky */ + return _('Smoky'); + case 23: + /* blustery */ + return _('Blustery'); + case 24: + /* windy */ + return _('Windy'); + case 25: + /* cold */ + return _('Cold'); + case 26: + /* cloudy */ + return _('Cloudy'); + case 27: + /* mostly cloudy (night) */ + case 28: + /* mostly cloudy (day) */ + return _('Mostly cloudy'); + case 29: + /* partly cloudy (night) */ + case 30: + /* partly cloudy (day) */ + return _('Partly cloudy'); + case 31: + /* clear (night) */ + return _('Clear'); + case 32: + /* sunny */ + return _('Sunny'); + case 33: + /* fair (night) */ + case 34: + /* fair (day) */ + return _('Fair'); + case 35: + /* mixed rain and hail */ + return _('Mixed rain and hail'); + case 36: + /* hot */ + return _('Hot'); + case 37: + /* isolated thunderstorms */ + return _('Isolated thunderstorms'); + case 38: + /* scattered thunderstorms */ + case 39: + /* scattered thunderstorms */ + return _('Scattered thunderstorms'); + case 40: + /* scattered showers */ + return _('Scattered showers'); + case 41: + /* heavy snow */ + return _('Heavy snow'); + case 42: + /* scattered snow showers */ + return _('Scattered snow showers'); + case 43: + /* heavy snow */ + return _('Heavy snow'); + case 44: + /* partly cloudy */ + return _('Partly cloudy'); + case 45: + /* thundershowers */ + return _('Thundershowers'); + case 46: + /* snow showers */ + return _('Snow showers'); + case 47: + /* isolated thundershowers */ + return _('Isolated thundershowers'); + case 3200: + /* not available */ default: return _('Not available'); } }, - toCelsius: function(t) - { - return String(Math.round((Number(t)-32)/1.8)); - }, - - toKelvin: function(t) - { - return String(Math.round(((Number(t)+459.67)/1.8)*100)/100); - }, - - toRankine: function(t) - { - return String(Math.round((Number(t)+459.67)*100)/100); - }, - - toReaumur: function(t) - { - return String(Math.round((Number(t)-32)/2.25)); - }, - - toRoemer: function(t) - { - return String(Math.round((((Number(t)-32)*7)/24)+7.5)); - }, - - toDelisle: function(t) - { - return String(Math.round(((212-Number(t))*5)/6)); - }, - - toNewton: function(t) - { - return String(Math.round((((Number(t)-32)*11)/60)*10)/10); - }, - - toPascal: function(p,t) - { - return Math.round((p * (3386.39-((t-32)*0.003407143)))); - }, - - toBeaufort: function(w,t) - { - if(w < 1) - return (!t)?"0":"("+_("Calm")+")"; - - else if(w >= 1 && w <= 3) - return (!t)?"1":"("+_("Light air")+")"; - - else if(w >= 4 && w <= 7) - return (!t)?"2":"("+_("Light breeze")+")"; - - else if(w >= 8 && w <= 12) - return (!t)?"3":"("+_("Gentle breeze")+")"; - - else if(w >= 13 && w <= 17) - return (!t)?"4":"("+_("Moderate breeze")+")"; - - else if(w >= 18 && w <= 24) - return (!t)?"5":"("+_("Fresh breeze")+")"; - - else if(w >= 25 && w <= 30) - return (!t)?"6":"("+_("Strong breeze")+")"; - - else if(w >= 31 && w <= 38) - return (!t)?"7":"("+_("Moderate gale")+")"; - - else if(w >= 39 && w <= 46) - return (!t)?"8":"("+_("Fresh gale")+")"; - - else if(w >= 47 && w <= 54) - return (!t)?"9":"("+_("Strong gale")+")"; - - else if(w >= 55 && w <= 63) - return (!t)?"10":"("+_("Storm")+")"; - - else if(w >= 64 && w <= 73) - return (!t)?"11":"("+_("Violent storm")+")"; - - else - return (!t)?"12":"("+_("Hurricane")+")"; - }, - - get_locale_day: function(abr) - { - let days = [_('Sunday'),_('Monday'), _('Tuesday'), _('Wednesday'), _('Thursday'), _('Friday'), _('Saturday')]; - return days[abr]; - }, - - get_wind_direction : function(deg) - { - let arrows = ["\u2193", "\u2199", "\u2190", "\u2196", "\u2191", "\u2197", "\u2192", "\u2198"]; - let letters = [_('N'), _('NE'), _('E'), _('SE'), _('S'), _('SW'), _('W'), _('NW')]; - let idx = Math.round(deg / 45) % arrows.length; - return (this._wind_direction)?arrows[idx]:letters[idx]; - }, - - get_pressure_state : function(state) - { - switch(parseInt(state, 3)) - { - case 0: - return ''; - break; - - case 1: - return '\u2934'; - break; - - case 2: - return '\u2935'; - break; - } - return 0; - }, - - icon_type : function(icon_name) - { - if(!icon_name) - if(this._icon_type) - return "-symbolic"; - else - return ""; - - if(this._icon_type) - if(String(icon_name).search("-symbolic") != -1) - return icon_name; - else - return icon_name+"-symbolic"; - else - if(String(icon_name).search("-symbolic") != -1) - return String(icon_name).replace("-symbolic",""); - else - return icon_name; - }, - - load_json_async: function(url, fun) - { - let here = this; - - let message = Soup.Message.new('GET', url); - - _httpSession.queue_message(message, function(_httpSession, message) - { - if(!message.response_body.data) - { - fun.call(here,0); - return; - } - - try - { - let jp = JSON.parse(message.response_body.data); - fun.call(here, jp); - } - catch(e) - { - fun.call(here,0); - return; - } - }); - return 0; - }, - - refreshWeather: function(recurse) - { - if(!this.extractWoeid(this._city)) - { - this.updateCities(); - return 0; - } - this.load_json_async(this.get_weather_url(), function(json) - { - if(!json) - return 0; - let weather = json.query.results.channel; - let many = 0; - if(typeof weather[0] != "undefined") - { - weather = weather[0]; - many = 1; - } - let weather_c = weather.item.condition; - - this.rebuildSelectCityItem(); - - this._weatherIcon.icon_name = this.icon_type(this._weatherIcon.icon_name); - this._currentWeatherIcon.icon_name = this.icon_type(this._currentWeatherIcon.icon_name); - this._forecast[0].Icon.icon_name = this.icon_type(this._forecast[0].Icon.icon_name); - this._forecast[1].Icon.icon_name = this.icon_type(this._forecast[1].Icon.icon_name); - this._sunriseIcon.icon_name = this.icon_type(this._sunriseIcon.icon_name); - this._sunsetIcon.icon_name = this.icon_type(this._sunsetIcon.icon_name); - this._buildIcon.icon_name = this.icon_type(this._buildIcon.icon_name); - - if(typeof St.IconType != "undefined") - { - this._weatherIcon.icon_type = (this._icon_type) ? St.IconType.SYMBOLIC : St.IconType.FULLCOLOR; - this._currentWeatherIcon.icon_type = (this._icon_type) ? St.IconType.SYMBOLIC : St.IconType.FULLCOLOR; - this._forecast[0].Icon.icon_type = (this._icon_type) ? St.IconType.SYMBOLIC : St.IconType.FULLCOLOR; - this._forecast[1].Icon.icon_type = (this._icon_type) ? St.IconType.SYMBOLIC : St.IconType.FULLCOLOR; - this._sunriseIcon.icon_type = (this._icon_type) ? St.IconType.SYMBOLIC : St.IconType.FULLCOLOR; - this._sunsetIcon.icon_type = (this._icon_type) ? St.IconType.SYMBOLIC : St.IconType.FULLCOLOR; - this._buildIcon.icon_type = (this._icon_type) ? St.IconType.SYMBOLIC : St.IconType.FULLCOLOR; - } - - if(this._old_position_in_panel != this._position_in_panel) - { - switch (this._old_position_in_panel) { - case WeatherPosition.LEFT: - Main.panel._leftBox.remove_actor(this.actor); - break; - case WeatherPosition.CENTER: - Main.panel._centerBox.remove_actor(this.actor); - break; - case WeatherPosition.RIGHT: - Main.panel._rightBox.remove_actor(this.actor); - break; - } - - let children = null; - switch (this._position_in_panel) { - case WeatherPosition.LEFT: - children = Main.panel._leftBox.get_children(); - Main.panel._leftBox.insert_child_at_index(this.actor, children.length); - break; - case WeatherPosition.CENTER: - children = Main.panel._centerBox.get_children(); - Main.panel._centerBox.insert_child_at_index(this.actor, children.length); - break; - case WeatherPosition.RIGHT: - children = Main.panel._rightBox.get_children(); - Main.panel._rightBox.insert_child_at_index(this.actor, 0); - break; - } - this._old_position_in_panel = this._position_in_panel; - } + toCelsius: function(t) { + return String(Math.round((Number(t) - 32) / 1.8)); + }, + + toKelvin: function(t) { + return String(Math.round(((Number(t) + 459.67) / 1.8) * 100) / 100); + }, + + toRankine: function(t) { + return String(Math.round((Number(t) + 459.67) * 100) / 100); + }, + + toReaumur: function(t) { + return String(Math.round((Number(t) - 32) / 2.25)); + }, + + toRoemer: function(t) { + return String(Math.round((((Number(t) - 32) * 7) / 24) + 7.5)); + }, + + toDelisle: function(t) { + return String(Math.round(((212 - Number(t)) * 5) / 6)); + }, + + toNewton: function(t) { + return String(Math.round((((Number(t) - 32) * 11) / 60) * 10) / 10); + }, + + toPascal: function(p, t) { + return Math.round((p * (3386.39 - ((t - 32) * 0.003407143)))); + }, + + toBeaufort: function(w, t) { + if (w < 1) + return (!t) ? "0" : "(" + _("Calm") + ")"; + + else if (w >= 1 && w <= 3) + return (!t) ? "1" : "(" + _("Light air") + ")"; + + else if (w >= 4 && w <= 7) + return (!t) ? "2" : "(" + _("Light breeze") + ")"; + + else if (w >= 8 && w <= 12) + return (!t) ? "3" : "(" + _("Gentle breeze") + ")"; + + else if (w >= 13 && w <= 17) + return (!t) ? "4" : "(" + _("Moderate breeze") + ")"; + + else if (w >= 18 && w <= 24) + return (!t) ? "5" : "(" + _("Fresh breeze") + ")"; + + else if (w >= 25 && w <= 30) + return (!t) ? "6" : "(" + _("Strong breeze") + ")"; + + else if (w >= 31 && w <= 38) + return (!t) ? "7" : "(" + _("Moderate gale") + ")"; + + else if (w >= 39 && w <= 46) + return (!t) ? "8" : "(" + _("Fresh gale") + ")"; + + else if (w >= 47 && w <= 54) + return (!t) ? "9" : "(" + _("Strong gale") + ")"; + + else if (w >= 55 && w <= 63) + return (!t) ? "10" : "(" + _("Storm") + ")"; + + else if (w >= 64 && w <= 73) + return (!t) ? "11" : "(" + _("Violent storm") + ")"; + + else + return (!t) ? "12" : "(" + _("Hurricane") + ")"; + }, + + get_locale_day: function(abr) { + let days = [_('Sunday'), _('Monday'), _('Tuesday'), _('Wednesday'), _('Thursday'), _('Friday'), _('Saturday')]; + return days[abr]; + }, + + get_wind_direction: function(deg) { + let arrows = ["\u2193", "\u2199", "\u2190", "\u2196", "\u2191", "\u2197", "\u2192", "\u2198"]; + let letters = [_('N'), _('NE'), _('E'), _('SE'), _('S'), _('SW'), _('W'), _('NW')]; + let idx = Math.round(deg / 45) % arrows.length; + return (this._wind_direction) ? arrows[idx] : letters[idx]; + }, + + get_pressure_state: function(state) { + switch (parseInt(state, 3)) { + case 0: + return ''; + break; + + case 1: + return '\u2934'; + break; + + case 2: + return '\u2935'; + break; + } + return 0; + }, + + icon_type: function(icon_name) { + if (!icon_name) + if (this._icon_type) + return "-symbolic"; + else + return ""; + + if (this._icon_type) + if (String(icon_name).search("-symbolic") != -1) + return icon_name; + else + return icon_name + "-symbolic"; + else + if (String(icon_name).search("-symbolic") != -1) + return String(icon_name).replace("-symbolic", ""); + else + return icon_name; + }, + + load_json_async: function(url, fun) { + let here = this; + + let message = Soup.Message.new('GET', url); + + _httpSession.queue_message(message, function(_httpSession, message) { + if (!message.response_body.data) { + fun.call(here, 0); + return; + } + + try { + let jp = JSON.parse(message.response_body.data); + fun.call(here, jp); + } catch (e) { + fun.call(here, 0); + return; + } + }); + return 0; + }, + + refreshWeather: function(recurse) { + if (!this.extractWoeid(this._city)) { + this.updateCities(); + return 0; + } + this.load_json_async(this.get_weather_url(), function(json) { + if (!json) + return 0; + let weather = json.query.results.channel; + let many = 0; + if (typeof weather[0] != "undefined") { + weather = weather[0]; + many = 1; + } + let weather_c = weather.item.condition; + + this.rebuildSelectCityItem(); + + this._weatherIcon.icon_name = this.icon_type(this._weatherIcon.icon_name); + this._currentWeatherIcon.icon_name = this.icon_type(this._currentWeatherIcon.icon_name); + this._forecast[0].Icon.icon_name = this.icon_type(this._forecast[0].Icon.icon_name); + this._forecast[1].Icon.icon_name = this.icon_type(this._forecast[1].Icon.icon_name); + this._sunriseIcon.icon_name = this.icon_type(this._sunriseIcon.icon_name); + this._sunsetIcon.icon_name = this.icon_type(this._sunsetIcon.icon_name); + this._buildIcon.icon_name = this.icon_type(this._buildIcon.icon_name); + + if (typeof St.IconType != "undefined") { + this._weatherIcon.icon_type = (this._icon_type) ? St.IconType.SYMBOLIC : St.IconType.FULLCOLOR; + this._currentWeatherIcon.icon_type = (this._icon_type) ? St.IconType.SYMBOLIC : St.IconType.FULLCOLOR; + this._forecast[0].Icon.icon_type = (this._icon_type) ? St.IconType.SYMBOLIC : St.IconType.FULLCOLOR; + this._forecast[1].Icon.icon_type = (this._icon_type) ? St.IconType.SYMBOLIC : St.IconType.FULLCOLOR; + this._sunriseIcon.icon_type = (this._icon_type) ? St.IconType.SYMBOLIC : St.IconType.FULLCOLOR; + this._sunsetIcon.icon_type = (this._icon_type) ? St.IconType.SYMBOLIC : St.IconType.FULLCOLOR; + this._buildIcon.icon_type = (this._icon_type) ? St.IconType.SYMBOLIC : St.IconType.FULLCOLOR; + } + + if (this._old_position_in_panel != this._position_in_panel) { + switch (this._old_position_in_panel) { + case WeatherPosition.LEFT: + Main.panel._leftBox.remove_actor(this.actor); + break; + case WeatherPosition.CENTER: + Main.panel._centerBox.remove_actor(this.actor); + break; + case WeatherPosition.RIGHT: + Main.panel._rightBox.remove_actor(this.actor); + break; + } + + let children = null; + switch (this._position_in_panel) { + case WeatherPosition.LEFT: + children = Main.panel._leftBox.get_children(); + Main.panel._leftBox.insert_child_at_index(this.actor, children.length); + break; + case WeatherPosition.CENTER: + children = Main.panel._centerBox.get_children(); + Main.panel._centerBox.insert_child_at_index(this.actor, children.length); + break; + case WeatherPosition.RIGHT: + children = Main.panel._rightBox.get_children(); + Main.panel._rightBox.insert_child_at_index(this.actor, 0); + break; + } + this._old_position_in_panel = this._position_in_panel; + } let forecast = weather.item.forecast; let location = this.extractLocation(this._city); @@ -1121,7 +1161,7 @@ const WeatherMenuButton = new Lang.Class({ let humidity = weather.atmosphere.humidity + ' %'; let pressure = weather.atmosphere.pressure; let pressure_unit = weather.units.pressure; - let pressure_state = weather.atmosphere.rising; + let pressure_state = weather.atmosphere.rising; let wind_direction = this.get_wind_direction(weather.wind.direction); let wind = weather.wind.speed; let wind_unit = weather.units.speed; @@ -1129,187 +1169,181 @@ const WeatherMenuButton = new Lang.Class({ let sunrise = weather.astronomy.sunrise; let sunset = weather.astronomy.sunset; - if(typeof this.lastBuildId == "undefined") - this.lastBuildId = 0; - - if(typeof this.lastBuildDate == "undefined") - this.lastBuildDate = 0; - - if(this.lastBuildId != weather_c.date || !this.lastBuildDate) - { - this.lastBuildId = weather_c.date; - this.lastBuildDate = new Date(); - } - - let actualDate = new Date(); - let d = Math.floor((actualDate.getTime()-this.lastBuildDate.getTime())/86400000); - - switch(this._pressure_units) - { - case WeatherPressureUnits.inHg: - pressure_unit = "inHg"; - break; - - case WeatherPressureUnits.hPa: - pressure = Math.round(this.toPascal(pressure,temperature)/100); - pressure_unit = "hPa"; - break; - - case WeatherPressureUnits.bar: - pressure = this.toPascal(pressure,temperature)/100000; - pressure_unit = "bar"; - break; - - case WeatherPressureUnits.Pa: - pressure = this.toPascal(pressure,temperature); - pressure_unit = "Pa"; - break; - - case WeatherPressureUnits.kPa: - pressure = Math.round(this.toPascal(pressure,temperature)/100)/10; - pressure_unit = "kPa"; - break; - - case WeatherPressureUnits.atm: - pressure = Math.round((this.toPascal(pressure,temperature)*0.00000986923267)*100000)/100000; - pressure_unit = "atm"; - break; - - case WeatherPressureUnits.at: - pressure = Math.round((this.toPascal(pressure,temperature)*0.0000101971621298)*100000)/100000; - pressure_unit = "at"; - break; - - case WeatherPressureUnits.Torr: - pressure = Math.round((this.toPascal(pressure,temperature)*0.00750061683)*100)/100; - pressure_unit = "Torr"; - break; - - case WeatherPressureUnits.psi: - pressure = Math.round((this.toPascal(pressure,temperature)*0.000145037738)*100)/100; - pressure_unit = "psi"; - break; - } - - switch(this._units) - { - case WeatherUnits.FAHRENHEIT: - break; - - case WeatherUnits.CELSIUS: - temperature = this.toCelsius(temperature); - chill = this.toCelsius(chill); - break; - - case WeatherUnits.KELVIN: - temperature = this.toKelvin(temperature); - chill = this.toKelvin(chill); - break; - - case WeatherUnits.RANKINE: - temperature = this.toRankine(temperature); - chill = this.toRankine(chill); - break; - - case WeatherUnits.REAUMUR: - temperature = this.toReaumur(temperature); - chill = this.toReaumur(chill); - break; - - case WeatherUnits.ROEMER: - temperature = this.toRoemer(temperature); - chill = this.toRoemer(chill); - break; - - case WeatherUnits.DELISLE: - temperature = this.toDelisle(temperature); - chill = this.toDelisle(chill); - break; - - case WeatherUnits.NEWTON: - temperature = this.toNewton(temperature); - chill = this.toNewton(chill); - break; - } - - let lastBuild = (this.lastBuildDate.getHours()%12)+":"+((this.lastBuildDate.getMinutes()<10)?"0":"")+this.lastBuildDate.getMinutes()+" "+((this.lastBuildDate.getHours() >= 12)?"pm":"am"); - - if(this._clockFormat == "24h") - { - sunrise = new Date("3 Mar 1999 "+sunrise); - sunrise = sunrise.getHours()+":"+((sunrise.getMinutes()<10)?"0":"")+sunrise.getMinutes(); - sunset = new Date("3 Mar 1999 "+sunset); - sunset = sunset.getHours()+":"+((sunset.getMinutes()<10)?"0":"")+sunset.getMinutes(); - lastBuild = this.lastBuildDate.getHours()+":"+((this.lastBuildDate.getMinutes()<10)?"0":"")+this.lastBuildDate.getMinutes(); - } - - if(d >= 1) - { - lastBuild = _("Yesterday"); - if(d > 1) - lastBuild = _("%s days ago").replace("%s",d); - } + if (typeof this.lastBuildId == "undefined") + this.lastBuildId = 0; + + if (typeof this.lastBuildDate == "undefined") + this.lastBuildDate = 0; + + if (this.lastBuildId != weather_c.date || !this.lastBuildDate) { + this.lastBuildId = weather_c.date; + this.lastBuildDate = new Date(); + } + + let actualDate = new Date(); + let d = Math.floor((actualDate.getTime() - this.lastBuildDate.getTime()) / 86400000); + + switch (this._pressure_units) { + case WeatherPressureUnits.inHg: + pressure_unit = "inHg"; + break; + + case WeatherPressureUnits.hPa: + pressure = Math.round(this.toPascal(pressure, temperature) / 100); + pressure_unit = "hPa"; + break; + + case WeatherPressureUnits.bar: + pressure = this.toPascal(pressure, temperature) / 100000; + pressure_unit = "bar"; + break; + + case WeatherPressureUnits.Pa: + pressure = this.toPascal(pressure, temperature); + pressure_unit = "Pa"; + break; + + case WeatherPressureUnits.kPa: + pressure = Math.round(this.toPascal(pressure, temperature) / 100) / 10; + pressure_unit = "kPa"; + break; + + case WeatherPressureUnits.atm: + pressure = Math.round((this.toPascal(pressure, temperature) * 0.00000986923267) * 100000) / 100000; + pressure_unit = "atm"; + break; + + case WeatherPressureUnits.at: + pressure = Math.round((this.toPascal(pressure, temperature) * 0.0000101971621298) * 100000) / 100000; + pressure_unit = "at"; + break; + + case WeatherPressureUnits.Torr: + pressure = Math.round((this.toPascal(pressure, temperature) * 0.00750061683) * 100) / 100; + pressure_unit = "Torr"; + break; + + case WeatherPressureUnits.psi: + pressure = Math.round((this.toPascal(pressure, temperature) * 0.000145037738) * 100) / 100; + pressure_unit = "psi"; + break; + } + + switch (this._units) { + case WeatherUnits.FAHRENHEIT: + break; + + case WeatherUnits.CELSIUS: + temperature = this.toCelsius(temperature); + chill = this.toCelsius(chill); + break; + + case WeatherUnits.KELVIN: + temperature = this.toKelvin(temperature); + chill = this.toKelvin(chill); + break; + + case WeatherUnits.RANKINE: + temperature = this.toRankine(temperature); + chill = this.toRankine(chill); + break; + + case WeatherUnits.REAUMUR: + temperature = this.toReaumur(temperature); + chill = this.toReaumur(chill); + break; + + case WeatherUnits.ROEMER: + temperature = this.toRoemer(temperature); + chill = this.toRoemer(chill); + break; + + case WeatherUnits.DELISLE: + temperature = this.toDelisle(temperature); + chill = this.toDelisle(chill); + break; + + case WeatherUnits.NEWTON: + temperature = this.toNewton(temperature); + chill = this.toNewton(chill); + break; + } + + let lastBuild = (this.lastBuildDate.getHours() % 12) + ":" + ((this.lastBuildDate.getMinutes() < 10) ? "0" : "") + this.lastBuildDate.getMinutes() + " " + ((this.lastBuildDate.getHours() >= 12) ? "pm" : "am"); + + if (this._clockFormat == "24h") { + sunrise = new Date("3 Mar 1999 " + sunrise); + sunrise = sunrise.getHours() + ":" + ((sunrise.getMinutes() < 10) ? "0" : "") + sunrise.getMinutes(); + sunset = new Date("3 Mar 1999 " + sunset); + sunset = sunset.getHours() + ":" + ((sunset.getMinutes() < 10) ? "0" : "") + sunset.getMinutes(); + lastBuild = this.lastBuildDate.getHours() + ":" + ((this.lastBuildDate.getMinutes() < 10) ? "0" : "") + this.lastBuildDate.getMinutes(); + } + + if (d >= 1) { + lastBuild = _("Yesterday"); + if (d > 1) + lastBuild = _("%s days ago").replace("%s", d); + } this._currentWeatherIcon.icon_name = this._weatherIcon.icon_name = iconname; - let weatherInfoC = ""; - let weatherInfoT = ""; + let weatherInfoC = ""; + let weatherInfoT = ""; - if (this._comment_in_panel) - weatherInfoC = comment; + if (this._comment_in_panel) + weatherInfoC = comment; - if (this._text_in_panel) - weatherInfoT = parseFloat(temperature).toLocaleString() + ' ' + this.unit_to_unicode(); + if (this._text_in_panel) + weatherInfoT = parseFloat(temperature).toLocaleString() + ' ' + this.unit_to_unicode(); - this._weatherInfo.text = weatherInfoC + ((weatherInfoC && weatherInfoT) ? ", " : "") + weatherInfoT; + this._weatherInfo.text = weatherInfoC + ((weatherInfoC && weatherInfoT) ? ", " : "") + weatherInfoT; this._currentWeatherSummary.text = comment + ", " + parseFloat(temperature).toLocaleString() + ' ' + this.unit_to_unicode(); this._currentWeatherLocation.text = location; this._currentWeatherTemperature.text = parseFloat(chill).toLocaleString() + ' ' + this.unit_to_unicode(); this._currentWeatherHumidity.text = parseFloat(humidity).toLocaleString() + ' %'; - this._currentWeatherPressure.text = parseFloat(pressure).toLocaleString() + ' ' + pressure_unit + ((pressure_state)?" ":"") + this.get_pressure_state(pressure_state); - this._currentWeatherSunrise.text = sunrise; - this._currentWeatherSunset.text = sunset; - this._currentWeatherBuild.text = lastBuild; - - // 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.MPH: - break; - - case WeatherWindSpeedUnits.KPH: - wind = Math.round (wind / WEATHER_CONV_MPH_IN_MPS * WEATHER_CONV_KPH_IN_MPS); - wind_unit = 'km/h'; - break; - - case WeatherWindSpeedUnits.MPS: - wind = Math.round ((wind / WEATHER_CONV_MPH_IN_MPS) * 10)/ 10; - wind_unit = 'm/s'; - break; - - case WeatherWindSpeedUnits.KNOTS: - wind = Math.round (wind / WEATHER_CONV_MPH_IN_MPS * WEATHER_CONV_KNOTS_IN_MPS); - wind_unit = 'kn'; - break; - - case WeatherWindSpeedUnits.FPS: - wind = Math.round (wind / WEATHER_CONV_MPH_IN_MPS * WEATHER_CONV_FPS_IN_MPS); - wind_unit = 'ft/s'; - break; - - case WeatherWindSpeedUnits.BEAUFORT: - wind_unit = this.toBeaufort(wind,true); - wind = this.toBeaufort(wind); - } - - if (!wind) - this._currentWeatherWind.text = '\u2013'; - else if (wind == 0 || !wind_direction) - this._currentWeatherWind.text = parseFloat(wind).toLocaleString() + ' ' + wind_unit; - else // i.e. wind > 0 && wind_direction - this._currentWeatherWind.text = wind_direction + ' ' + parseFloat(wind).toLocaleString() + ' ' + wind_unit; + this._currentWeatherPressure.text = parseFloat(pressure).toLocaleString() + ' ' + pressure_unit + ((pressure_state) ? " " : "") + this.get_pressure_state(pressure_state); + this._currentWeatherSunrise.text = sunrise; + this._currentWeatherSunset.text = sunset; + this._currentWeatherBuild.text = lastBuild; + + // 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.MPH: + break; + + case WeatherWindSpeedUnits.KPH: + wind = Math.round(wind / WEATHER_CONV_MPH_IN_MPS * WEATHER_CONV_KPH_IN_MPS); + wind_unit = 'km/h'; + break; + + case WeatherWindSpeedUnits.MPS: + wind = Math.round((wind / WEATHER_CONV_MPH_IN_MPS) * 10) / 10; + wind_unit = 'm/s'; + break; + + case WeatherWindSpeedUnits.KNOTS: + wind = Math.round(wind / WEATHER_CONV_MPH_IN_MPS * WEATHER_CONV_KNOTS_IN_MPS); + wind_unit = 'kn'; + break; + + case WeatherWindSpeedUnits.FPS: + wind = Math.round(wind / WEATHER_CONV_MPH_IN_MPS * WEATHER_CONV_FPS_IN_MPS); + wind_unit = 'ft/s'; + break; + + case WeatherWindSpeedUnits.BEAUFORT: + wind_unit = this.toBeaufort(wind, true); + wind = this.toBeaufort(wind); + } + + if (!wind) + this._currentWeatherWind.text = '\u2013'; + else if (wind == 0 || !wind_direction) + this._currentWeatherWind.text = parseFloat(wind).toLocaleString() + ' ' + wind_unit; + else // i.e. wind > 0 && wind_direction + this._currentWeatherWind.text = wind_direction + ' ' + parseFloat(wind).toLocaleString() + ' ' + wind_unit; // Refresh forecast for (let i = 0; i <= 1; i++) { @@ -1320,70 +1354,69 @@ const WeatherMenuButton = new Lang.Class({ let t_low = forecastData.low; let t_high = forecastData.high; - switch(this._units) - { - case WeatherUnits.FAHRENHEIT: - break; - - case WeatherUnits.CELSIUS: - t_low = this.toCelsius(t_low); - t_high = this.toCelsius(t_high); - break; - - case WeatherUnits.KELVIN: - t_low = this.toKelvin(t_low); - t_high = this.toKelvin(t_high); - break; - - case WeatherUnits.RANKINE: - t_low = this.toRankine(t_low); - t_high = this.toRankine(t_high); - break; - - case WeatherUnits.REAUMUR: - t_low = this.toReaumur(t_low); - t_high = this.toReaumur(t_high); - break; - - case WeatherUnits.ROEMER: - t_low = this.toRoemer(t_low); - t_high = this.toRoemer(t_high); - break; - - case WeatherUnits.DELISLE: - t_low = this.toDelisle(t_low); - t_high = this.toDelisle(t_high); - break; - - case WeatherUnits.NEWTON: - t_low = this.toNewton(t_low); - t_high = this.toNewton(t_high); - break; - } + switch (this._units) { + case WeatherUnits.FAHRENHEIT: + break; + + case WeatherUnits.CELSIUS: + t_low = this.toCelsius(t_low); + t_high = this.toCelsius(t_high); + break; + + case WeatherUnits.KELVIN: + t_low = this.toKelvin(t_low); + t_high = this.toKelvin(t_high); + break; + + case WeatherUnits.RANKINE: + t_low = this.toRankine(t_low); + t_high = this.toRankine(t_high); + break; + + case WeatherUnits.REAUMUR: + t_low = this.toReaumur(t_low); + t_high = this.toReaumur(t_high); + break; + + case WeatherUnits.ROEMER: + t_low = this.toRoemer(t_low); + t_high = this.toRoemer(t_high); + break; + + case WeatherUnits.DELISLE: + t_low = this.toDelisle(t_low); + t_high = this.toDelisle(t_high); + break; + + case WeatherUnits.NEWTON: + t_low = this.toNewton(t_low); + t_high = this.toNewton(t_high); + break; + } let comment = forecastData.text; if (this._translate_condition) comment = this.get_weather_condition(code); - let forecastDate = new Date(forecastData.date); - let dayLeft = Math.floor((actualDate.getTime()-forecastDate.getTime())/1000/60/60/24); + let forecastDate = new Date(forecastData.date); + let dayLeft = Math.floor((actualDate.getTime() - forecastDate.getTime()) / 1000 / 60 / 60 / 24); - let date_string = _("Today"); - if(dayLeft == -1) - date_string = _("Tomorrow"); - else if(dayLeft < -1) - date_string = _("In %s days").replace("%s",-1*dayLeft); - else if(dayLeft == 1) - date_string = _("Yesterday"); - else if(dayLeft > 1) - date_string = _("%s days ago").replace("%s",dayLeft); + let date_string = _("Today"); + if (dayLeft == -1) + date_string = _("Tomorrow"); + else if (dayLeft < -1) + date_string = _("In %s days").replace("%s", -1 * dayLeft); + else if (dayLeft == 1) + date_string = _("Yesterday"); + else if (dayLeft > 1) + date_string = _("%s days ago").replace("%s", dayLeft); forecastUi.Day.text = date_string + ' (' + this.get_locale_day(forecastDate.getDay()) + ')'; forecastUi.Temperature.text = '\u2193 ' + parseFloat(t_low).toLocaleString() + ' ' + this.unit_to_unicode() + ' \u2191 ' + parseFloat(t_high).toLocaleString() + ' ' + this.unit_to_unicode(); forecastUi.Summary.text = comment; forecastUi.Icon.icon_name = this.get_weather_icon_safely(code); } - return 0; + return 0; }); // Repeatedly refresh weather if recurse is set @@ -1392,7 +1425,7 @@ const WeatherMenuButton = new Lang.Class({ this.refreshWeather(true); })); } - return 0; + return 0; }, destroyCurrentWeather: function() { @@ -1408,8 +1441,12 @@ const WeatherMenuButton = new Lang.Class({ showLoadingUi: function() { this.destroyCurrentWeather(); this.destroyFutureWeather(); - this._currentWeather.set_child(new St.Label({ text: _('Loading current weather ...') })); - this._futureWeather.set_child(new St.Label({ text: _('Loading future weather ...') })); + this._currentWeather.set_child(new St.Label({ + text: _('Loading current weather ...') + })); + this._futureWeather.set_child(new St.Label({ + text: _('Loading future weather ...') + })); }, rebuildCurrentWeatherUi: function() { @@ -1418,25 +1455,25 @@ const WeatherMenuButton = new Lang.Class({ // This will hold the icon for the current weather this._currentWeatherIcon = new St.Icon({ icon_size: 72, - icon_name: 'view-refresh'+this.icon_type(), + icon_name: 'view-refresh' + this.icon_type(), style_class: 'weather-current-icon' }); - this._sunriseIcon = new St.Icon({ + this._sunriseIcon = new St.Icon({ icon_size: 15, - icon_name: 'weather-clear'+this.icon_type(), + icon_name: 'weather-clear' + this.icon_type(), style_class: 'weather-sunrise-icon' }); - this._sunsetIcon = new St.Icon({ + this._sunsetIcon = new St.Icon({ icon_size: 15, - icon_name: 'weather-clear-night'+this.icon_type(), + icon_name: 'weather-clear-night' + this.icon_type(), style_class: 'weather-sunset-icon' }); - this._buildIcon = new St.Icon({ + this._buildIcon = new St.Icon({ icon_size: 15, - icon_name: 'view-refresh'+this.icon_type(), + icon_name: 'view-refresh' + this.icon_type(), style_class: 'weather-build-icon' }); @@ -1445,7 +1482,9 @@ const WeatherMenuButton = new Lang.Class({ text: _('Loading ...'), style_class: 'weather-current-summary' }); - this._currentWeatherLocation = new St.Label({ text: _('Please wait') }); + this._currentWeatherLocation = new St.Label({ + text: _('Please wait') + }); let bb = new St.BoxLayout({ vertical: true, @@ -1454,27 +1493,41 @@ const WeatherMenuButton = new Lang.Class({ bb.add_actor(this._currentWeatherLocation); bb.add_actor(this._currentWeatherSummary); - this._currentWeatherSunrise = new St.Label({ text: '-' }); - this._currentWeatherSunset = new St.Label({ text: '-' }); - this._currentWeatherBuild = new St.Label({ text: '-' }); + this._currentWeatherSunrise = new St.Label({ + text: '-' + }); + this._currentWeatherSunset = new St.Label({ + text: '-' + }); + this._currentWeatherBuild = new St.Label({ + text: '-' + }); - let ab = new St.BoxLayout({ - style_class: 'weather-current-infobox' - }); + let ab = new St.BoxLayout({ + style_class: 'weather-current-infobox' + }); - ab.add_actor(this._sunriseIcon); - ab.add_actor(this._currentWeatherSunrise); - ab.add_actor(this._sunsetIcon); - ab.add_actor(this._currentWeatherSunset); - ab.add_actor(this._buildIcon); - ab.add_actor(this._currentWeatherBuild); - bb.add_actor(ab); + ab.add_actor(this._sunriseIcon); + ab.add_actor(this._currentWeatherSunrise); + ab.add_actor(this._sunsetIcon); + ab.add_actor(this._currentWeatherSunset); + ab.add_actor(this._buildIcon); + ab.add_actor(this._currentWeatherBuild); + bb.add_actor(ab); // Other labels - this._currentWeatherTemperature = new St.Label({ text: '...' }); - this._currentWeatherHumidity = new St.Label({ text: '...' }); - this._currentWeatherPressure = new St.Label({ text: '...' }); - this._currentWeatherWind = new St.Label({ text: '...' }); + this._currentWeatherTemperature = new St.Label({ + text: '...' + }); + this._currentWeatherHumidity = new St.Label({ + text: '...' + }); + this._currentWeatherPressure = new St.Label({ + text: '...' + }); + this._currentWeatherWind = new St.Label({ + text: '...' + }); let rb = new St.BoxLayout({ style_class: 'weather-current-databox' @@ -1490,13 +1543,21 @@ const WeatherMenuButton = new Lang.Class({ rb.add_actor(rb_captions); rb.add_actor(rb_values); - rb_captions.add_actor(new St.Label({text: _('Feels like:')})); + rb_captions.add_actor(new St.Label({ + text: _('Feels like:') + })); rb_values.add_actor(this._currentWeatherTemperature); - rb_captions.add_actor(new St.Label({text: _('Humidity:')})); + rb_captions.add_actor(new St.Label({ + text: _('Humidity:') + })); rb_values.add_actor(this._currentWeatherHumidity); - rb_captions.add_actor(new St.Label({text: _('Pressure:')})); + rb_captions.add_actor(new St.Label({ + text: _('Pressure:') + })); rb_values.add_actor(this._currentWeatherPressure); - rb_captions.add_actor(new St.Label({text: _('Wind:')})); + rb_captions.add_actor(new St.Label({ + text: _('Wind:') + })); rb_values.add_actor(this._currentWeatherWind); let xb = new St.BoxLayout(); @@ -1523,7 +1584,7 @@ const WeatherMenuButton = new Lang.Class({ forecastWeather.Icon = new St.Icon({ icon_size: 48, - icon_name: 'view-refresh'+this.icon_type(), + icon_name: 'view-refresh' + this.icon_type(), style_class: 'weather-forecast-icon' }); forecastWeather.Day = new St.Label({ @@ -1559,7 +1620,7 @@ const WeatherMenuButton = new Lang.Class({ let weatherMenu; function init() { -Convenience.initTranslations('gnome-shell-extension-weather'); + Convenience.initTranslations('gnome-shell-extension-weather'); } function enable() { diff --git a/src/prefs.js b/src/prefs.js index ad3f25e..768683a 100644 --- a/src/prefs.js +++ b/src/prefs.js @@ -26,7 +26,6 @@ * . * */ - const Gtk = imports.gi.Gtk; const GObject = imports.gi.GObject; const GtkBuilder = Gtk.Builder; @@ -62,719 +61,662 @@ Soup.Session.prototype.add_feature.call(_httpSession, new Soup.ProxyResolverDefa let mCities = null; -const WeatherPrefsWidget = new GObject.Class( -{ -Name: 'WeatherExtension.Prefs.Widget', -GTypeName: 'WeatherExtensionPrefsWidget', -Extends: Gtk.Box, - - _init: function(params) - { - this.parent(params); - - this.initWindow(); - - this.refreshUI(); - - this.add(this.MainWidget); - }, - - Window : new Gtk.Builder(), - - initWindow : function() - { - let that = this; - mCities = null; - - this.Window.add_from_file(EXTENSIONDIR+"/weather-settings.ui"); - - this.MainWidget = this.Window.get_object("main-widget"); - this.treeview = this.Window.get_object("tree-treeview"); - this.liststore = this.Window.get_object("liststore"); - this.Iter = this.liststore.get_iter_first(); - - this.Window.get_object("tree-toolbutton-add").connect("clicked",function() - { - that.addCity(); - }); - - this.Window.get_object("tree-toolbutton-remove").connect("clicked",function() - { - that.removeCity(); - }); - - this.Window.get_object("treeview-selection").connect("changed",function(selection) - { - that.selectionChanged(selection); - }); - - this.treeview.set_model(this.liststore); - - let column = new Gtk.TreeViewColumn() - this.treeview.append_column(column); - - let renderer = new Gtk.CellRendererText(); - column.pack_start(renderer,null); - - column.set_cell_data_func(renderer,function() - { - arguments[1].markup = arguments[2].get_value(arguments[3],0); - }); - - this.initConfigWidget(); - this.addLabel(_("Temperature Unit")); - this.addComboBox(["\u00b0C","\u00b0F","K","\u00b0Ra","\u00b0R\u00E9","\u00b0R\u00F8","\u00b0De","\u00b0N"],"units"); - this.addLabel(_("Wind Speed Unit")); - this.addComboBox(["km/h","mph","m/s","kn","ft/s","Beaufort"],"wind_speed_unit"); - this.addLabel(_("Pressure Unit")); - this.addComboBox(["hPa","inHg","bar","Pa","kPa","atm","at","Torr","psi"],"pressure_unit"); - this.addLabel(_("Position in Panel")); - this.addComboBox([_("Center"),_("Right"),_("Left")],"position_in_panel"); - this.addLabel(_("Wind Direction by Arrows")); - this.addSwitch("wind_direction"); - this.addLabel(_("Translate Conditions")); - this.addSwitch("translate_condition"); - this.addLabel(_("Symbolic Icons")); - this.addSwitch("icon_type"); - this.addLabel(_("Temperature in Panel")); - this.addSwitch("text_in_panel"); - this.addLabel(_("Conditions in Panel")); - this.addSwitch("comment_in_panel"); - }, - - refreshUI : function() - { - this.MainWidget = this.Window.get_object("main-widget"); - this.treeview = this.Window.get_object("tree-treeview"); - this.liststore = this.Window.get_object("liststore"); - this.Iter = this.liststore.get_iter_first(); - - this.Window.get_object("tree-toolbutton-remove").sensitive = Boolean(this.city.length); - - if(mCities != this.city) - { - if(typeof this.liststore != "undefined") - this.liststore.clear(); - - if(this.city.length > 0) - { - let city = String(this.city).split(" && "); - - if(city && typeof city == "string") - city = [city]; - - let current = this.liststore.get_iter_first(); - - for(let i in city) - { - current = this.liststore.append(); - this.liststore.set_value(current, 0, this.extractLocation(city[i])); - } - } - - mCities = this.city; - } - - this.changeSelection(); - - let config = this.configWidgets; - for(let i in config) - if(config[i][0].active != this[config[i][1]]) - config[i][0].active = this[config[i][1]]; - }, - - initConfigWidget : function() - { - this.inc(1); - let a = this.Window.get_object("right-widget-table"); - a.visible = 1; - a.can_focus = 0; - this.right_widget = a; - }, - - x : [0,1], - - y : [0,1], - - configWidgets : [], - - inc : function() - { - if(arguments[0]) - { - this.x[0] = 0; - this.x[1] = 1; - this.y[0] = 0; - this.y[1] = 1; - return 0; - } - - if(this.x[0] == 1) - { - this.x[0] = 0; - this.x[1] = 1; - this.y[0] += 1; - this.y[1] += 1; - return 0; - } - else - { - this.x[0] += 1; - this.x[1] += 1; - return 0; - } - }, - - addLabel : function(text) - { - let l = new Gtk.Label({label:text,xalign:0}); - l.visible = 1; - l.can_focus = 0; - this.right_widget.attach(l, this.x[0],this.x[1], this.y[0],this.y[1],0,0,0,0); - this.inc(); - }, - - addComboBox : function(a,b) - { - let that = this; - let cf = new Gtk.ComboBoxText(); - this.configWidgets.push([cf,b]); - cf.visible = 1; - cf.can_focus = 0; - cf.width_request = 100; - for(let i in a) - cf.append_text(a[i]); - cf.active = this[b]; - cf.connect("changed",function(){that[b] = arguments[0].active;}); - this.right_widget.attach(cf, this.x[0],this.x[1], this.y[0],this.y[1],0,0,0,0); - this.inc(); - }, - - addSwitch : function(a) - { - let that = this; - let sw = new Gtk.Switch(); - this.configWidgets.push([sw,a]); - sw.visible = 1; - sw.can_focus = 0; - sw.active = this[a]; - sw.connect("notify::active",function(){that[a] = arguments[0].active;}); - this.right_widget.attach(sw, this.x[0],this.x[1], this.y[0],this.y[1],0,0,0,0); - this.inc(); - }, - - selectionChanged : function(select) - { - let a = select.get_selected_rows(this.liststore)[0][0]; - - if(typeof a != "undefined") - if(this.actual_city != parseInt(a.to_string())) - this.actual_city = parseInt(a.to_string()); - }, - - addCity : function() - { - let that = this; - let textDialog = _("Name of the city"); - let dialog = new Gtk.Dialog({title : ""}); - let entry = new Gtk.Entry(); - let completion = new Gtk.EntryCompletion(); - entry.set_completion(completion); - let completionModel = new Gtk.ListStore; - completionModel.set_column_types([GObject.TYPE_STRING]); - completion.set_model(completionModel); - completion.set_text_column(0); - completion.set_popup_single_match(true); - completion.set_minimum_key_length(1); - completion.set_match_func(function(completion,key,iter) - { - if(iter) - { - if(completionModel.get_value(iter,0)) - return true; - } - return false; - }); - entry.margin_top = 12; - entry.margin_bottom = 12; - let label = new Gtk.Label({label : textDialog}); - - dialog.set_border_width(12); - dialog.set_modal(1); - dialog.set_resizable(0); - //dialog.set_transient_for(***** Need parent Window *****); - - dialog.add_button(Gtk.STOCK_CANCEL, 0); - let d = dialog.add_button(Gtk.STOCK_OK, 1); - - d.set_can_default(true); - d.sensitive = 0; - - dialog.set_default(d); - entry.activates_default = true; - - let testLocation = function(location) - { - if(location.search(/\[/) == -1 || location.search(/\]/) == -1) - return 0; - - let woeid = location.split(/\[/)[1].split(/\]/)[0]; - if(!woeid) - return 0; - - that.loadJsonAsync(encodeURI('http://query.yahooapis.com/v1/public/yql?q=select woeid from geo.places where woeid = "'+woeid+'" limit 1&format=json'),function() - { - d.sensitive = 0; - if(typeof arguments[0].query == "undefined") - return 0; - - let city = arguments[0].query; - if(Number(city.count) == 0) - return 0; - - d.sensitive = 1; - return 0; - },"testLocation"); - return 0; - }; - - let searchLocation = function() - { - let location = entry.get_text(); - if(testLocation(location) == 0) - that.loadJsonAsync(encodeURI('http://query.yahooapis.com/v1/public/yql?q=select woeid,name,admin1,country from geo.places where text = "*'+location+'*" or text = "'+location+'"&format=json'),function() - { - if(!arguments[0]) - return 0; - let city = arguments[0].query; - let n = Number(city.count); - if(n > 0) - city = city.results.place; - else - return 0; - completionModel.clear(); - - let current = this.liststore.get_iter_first(); - - if(n > 1) - { - for(var i in city) - { - if(typeof m == "undefined") - var m = {}; - - current = completionModel.append(); - let cityText = city[i].name; - if(city[i].admin1) - cityText += ", "+city[i].admin1.content; - - if(city[i].country) - cityText += " ("+city[i].country.code+")"; - - cityText += " ["+city[i].woeid+"]"; - - if(m[cityText]) - continue; - else - m[cityText] = 1; - - completionModel.set_value(current,0,cityText); - } - } - else - { - current = completionModel.append(); - let cityText = city.name; - if(city.admin1) - cityText += ", "+city.admin1.content; - - if(city.country) - cityText += " ("+city.country.code+")"; - - cityText += " ["+city.woeid+"]"; - completionModel.set_value(current,0,cityText); - } - completion.complete(); - return 0; - },"getInfo"); - return 0; - }; - - entry.connect("changed",searchLocation); - - let dialog_area = dialog.get_content_area(); - dialog_area.pack_start(label,0,0,0); - dialog_area.pack_start(entry,0,0,0); - dialog.connect("response",function(w, response_id) { - if(response_id) - { - if(entry.get_text().search(/\[/) == -1 || entry.get_text().search(/\]/) == -1) - return 0; - - let woeid = entry.get_text().split(/\[/)[1].split(/\]/)[0]; - if(!woeid) - return 0; - - that.loadJsonAsync(encodeURI('http://query.yahooapis.com/v1/public/yql?format=json&q=select woeid,name,admin1,country from geo.places where woeid = "'+woeid+'" limit 1'),function() - { - let city = arguments[0].query; - if(Number(city.count) > 0) - city = city.results.place; - else - return 0; - - let cityText = city.name; - if(city.admin1) - cityText += ", "+city.admin1.content; - - if(city.country) - cityText += " ("+city.country.code+")"; - - if(that.city) - that.city = that.city+" && "+city.woeid+">"+cityText; - else - that.city = city.woeid+">"+cityText; - return 0; - },"lastTest"); - } - dialog.hide(); - return 0; - }); - - dialog.show_all(); - }, - - removeCity : function() - { - let that = this; - let city = this.city.split(" && "); - if(!city.length) - return 0; - let ac = this.actual_city; - let textDialog = _("Remove %s ?").replace("%s",this.extractLocation(city[ac])); - let dialog = new Gtk.Dialog({title : ""}); - let label = new Gtk.Label({label : textDialog}); - label.margin_bottom = 12; - - dialog.set_border_width(12); - dialog.set_modal(1); - dialog.set_resizable(0); - //dialog.set_transient_for(***** Need parent Window *****); - - dialog.add_button(Gtk.STOCK_NO, 0); - let d = dialog.add_button(Gtk.STOCK_YES, 1); - - d.set_can_default(true); - dialog.set_default(d); - - let dialog_area = dialog.get_content_area(); - dialog_area.pack_start(label,0,0,0); - dialog.connect("response",function(w, response_id) - { - if(response_id) - { - if(city.length == 0) - city = []; - - if(city.length > 0 && typeof city != "object") - city = [city]; - - if(city.length > 0) - city.splice(ac,1); - - if(city.length > 1) - that.city = city.join(" && "); - else if(city[0]) - that.city = city[0]; - else - that.city = ""; - } - dialog.hide(); - return 0; - }); - - dialog.show_all(); - return 0; - }, - - changeSelection : function() - { - let path = this.actual_city; - if(arguments[0]) - path = arguments[0]; - path = Gtk.TreePath.new_from_string(String(path)); - this.treeview.get_selection().select_path(path); - }, - - loadJsonAsync : function(url, fun, id) - { +const WeatherPrefsWidget = new GObject.Class({ + Name: 'WeatherExtension.Prefs.Widget', + GTypeName: 'WeatherExtensionPrefsWidget', + Extends: Gtk.Box, + + _init: function(params) { + this.parent(params); + + this.initWindow(); + + this.refreshUI(); + + this.add(this.MainWidget); + }, + + Window: new Gtk.Builder(), + + initWindow: function() { + let that = this; + mCities = null; + + this.Window.add_from_file(EXTENSIONDIR + "/weather-settings.ui"); + + this.MainWidget = this.Window.get_object("main-widget"); + this.treeview = this.Window.get_object("tree-treeview"); + this.liststore = this.Window.get_object("liststore"); + this.Iter = this.liststore.get_iter_first(); + + this.Window.get_object("tree-toolbutton-add").connect("clicked", function() { + that.addCity(); + }); + + this.Window.get_object("tree-toolbutton-remove").connect("clicked", function() { + that.removeCity(); + }); + + this.Window.get_object("treeview-selection").connect("changed", function(selection) { + that.selectionChanged(selection); + }); + + this.treeview.set_model(this.liststore); + + let column = new Gtk.TreeViewColumn() + this.treeview.append_column(column); + + let renderer = new Gtk.CellRendererText(); + column.pack_start(renderer, null); + + column.set_cell_data_func(renderer, function() { + arguments[1].markup = arguments[2].get_value(arguments[3], 0); + }); + + this.initConfigWidget(); + this.addLabel(_("Temperature Unit")); + this.addComboBox(["\u00b0C", "\u00b0F", "K", "\u00b0Ra", "\u00b0R\u00E9", "\u00b0R\u00F8", "\u00b0De", "\u00b0N"], "units"); + this.addLabel(_("Wind Speed Unit")); + this.addComboBox(["km/h", "mph", "m/s", "kn", "ft/s", "Beaufort"], "wind_speed_unit"); + this.addLabel(_("Pressure Unit")); + this.addComboBox(["hPa", "inHg", "bar", "Pa", "kPa", "atm", "at", "Torr", "psi"], "pressure_unit"); + this.addLabel(_("Position in Panel")); + this.addComboBox([_("Center"), _("Right"), _("Left")], "position_in_panel"); + this.addLabel(_("Wind Direction by Arrows")); + this.addSwitch("wind_direction"); + this.addLabel(_("Translate Conditions")); + this.addSwitch("translate_condition"); + this.addLabel(_("Symbolic Icons")); + this.addSwitch("icon_type"); + this.addLabel(_("Temperature in Panel")); + this.addSwitch("text_in_panel"); + this.addLabel(_("Conditions in Panel")); + this.addSwitch("comment_in_panel"); + }, + + refreshUI: function() { + this.MainWidget = this.Window.get_object("main-widget"); + this.treeview = this.Window.get_object("tree-treeview"); + this.liststore = this.Window.get_object("liststore"); + this.Iter = this.liststore.get_iter_first(); + + this.Window.get_object("tree-toolbutton-remove").sensitive = Boolean(this.city.length); + + if (mCities != this.city) { + if (typeof this.liststore != "undefined") + this.liststore.clear(); + + if (this.city.length > 0) { + let city = String(this.city).split(" && "); + + if (city && typeof city == "string") + city = [city]; + + let current = this.liststore.get_iter_first(); + + for (let i in city) { + current = this.liststore.append(); + this.liststore.set_value(current, 0, this.extractLocation(city[i])); + } + } + + mCities = this.city; + } + + this.changeSelection(); + + let config = this.configWidgets; + for (let i in config) + if (config[i][0].active != this[config[i][1]]) + config[i][0].active = this[config[i][1]]; + }, + + initConfigWidget: function() { + this.inc(1); + let a = this.Window.get_object("right-widget-table"); + a.visible = 1; + a.can_focus = 0; + this.right_widget = a; + }, + + x: [0, 1], + + y: [0, 1], + + configWidgets: [], + + inc: function() { + if (arguments[0]) { + this.x[0] = 0; + this.x[1] = 1; + this.y[0] = 0; + this.y[1] = 1; + return 0; + } + + if (this.x[0] == 1) { + this.x[0] = 0; + this.x[1] = 1; + this.y[0] += 1; + this.y[1] += 1; + return 0; + } else { + this.x[0] += 1; + this.x[1] += 1; + return 0; + } + }, + + addLabel: function(text) { + let l = new Gtk.Label({ + label: text, + xalign: 0 + }); + l.visible = 1; + l.can_focus = 0; + this.right_widget.attach(l, this.x[0], this.x[1], this.y[0], this.y[1], 0, 0, 0, 0); + this.inc(); + }, + + addComboBox: function(a, b) { + let that = this; + let cf = new Gtk.ComboBoxText(); + this.configWidgets.push([cf, b]); + cf.visible = 1; + cf.can_focus = 0; + cf.width_request = 100; + for (let i in a) + cf.append_text(a[i]); + cf.active = this[b]; + cf.connect("changed", function() { + that[b] = arguments[0].active; + }); + this.right_widget.attach(cf, this.x[0], this.x[1], this.y[0], this.y[1], 0, 0, 0, 0); + this.inc(); + }, + + addSwitch: function(a) { + let that = this; + let sw = new Gtk.Switch(); + this.configWidgets.push([sw, a]); + sw.visible = 1; + sw.can_focus = 0; + sw.active = this[a]; + sw.connect("notify::active", function() { + that[a] = arguments[0].active; + }); + this.right_widget.attach(sw, this.x[0], this.x[1], this.y[0], this.y[1], 0, 0, 0, 0); + this.inc(); + }, + + selectionChanged: function(select) { + let a = select.get_selected_rows(this.liststore)[0][0]; + + if (typeof a != "undefined") + if (this.actual_city != parseInt(a.to_string())) + this.actual_city = parseInt(a.to_string()); + }, + + addCity: function() { + let that = this; + let textDialog = _("Name of the city"); + let dialog = new Gtk.Dialog({ + title: "" + }); + let entry = new Gtk.Entry(); + let completion = new Gtk.EntryCompletion(); + entry.set_completion(completion); + let completionModel = new Gtk.ListStore; + completionModel.set_column_types([GObject.TYPE_STRING]); + completion.set_model(completionModel); + completion.set_text_column(0); + completion.set_popup_single_match(true); + completion.set_minimum_key_length(1); + completion.set_match_func(function(completion, key, iter) { + if (iter) { + if (completionModel.get_value(iter, 0)) + return true; + } + return false; + }); + entry.margin_top = 12; + entry.margin_bottom = 12; + let label = new Gtk.Label({ + label: textDialog + }); + + dialog.set_border_width(12); + dialog.set_modal(1); + dialog.set_resizable(0); + //dialog.set_transient_for(***** Need parent Window *****); + + dialog.add_button(Gtk.STOCK_CANCEL, 0); + let d = dialog.add_button(Gtk.STOCK_OK, 1); + + d.set_can_default(true); + d.sensitive = 0; + + dialog.set_default(d); + entry.activates_default = true; + + let testLocation = function(location) { + if (location.search(/\[/) == -1 || location.search(/\]/) == -1) + return 0; + + let woeid = location.split(/\[/)[1].split(/\]/)[0]; + if (!woeid) + return 0; + + that.loadJsonAsync(encodeURI('http://query.yahooapis.com/v1/public/yql?q=select woeid from geo.places where woeid = "' + woeid + '" limit 1&format=json'), function() { + d.sensitive = 0; + if (typeof arguments[0].query == "undefined") + return 0; + + let city = arguments[0].query; + if (Number(city.count) == 0) + return 0; + + d.sensitive = 1; + return 0; + }, "testLocation"); + return 0; + }; + + let searchLocation = function() { + let location = entry.get_text(); + if (testLocation(location) == 0) + that.loadJsonAsync(encodeURI('http://query.yahooapis.com/v1/public/yql?q=select woeid,name,admin1,country from geo.places where text = "*' + location + '*" or text = "' + location + '"&format=json'), function() { + if (!arguments[0]) + return 0; + let city = arguments[0].query; + let n = Number(city.count); + if (n > 0) + city = city.results.place; + else + return 0; + completionModel.clear(); + + let current = this.liststore.get_iter_first(); + + if (n > 1) { + for (var i in city) { + if (typeof m == "undefined") + var m = {}; + + current = completionModel.append(); + let cityText = city[i].name; + if (city[i].admin1) + cityText += ", " + city[i].admin1.content; + + if (city[i].country) + cityText += " (" + city[i].country.code + ")"; + + cityText += " [" + city[i].woeid + "]"; + + if (m[cityText]) + continue; + else + m[cityText] = 1; + + completionModel.set_value(current, 0, cityText); + } + } else { + current = completionModel.append(); + let cityText = city.name; + if (city.admin1) + cityText += ", " + city.admin1.content; + + if (city.country) + cityText += " (" + city.country.code + ")"; + + cityText += " [" + city.woeid + "]"; + completionModel.set_value(current, 0, cityText); + } + completion.complete(); + return 0; + }, "getInfo"); + return 0; + }; + + entry.connect("changed", searchLocation); + + let dialog_area = dialog.get_content_area(); + dialog_area.pack_start(label, 0, 0, 0); + dialog_area.pack_start(entry, 0, 0, 0); + dialog.connect("response", function(w, response_id) { + if (response_id) { + if (entry.get_text().search(/\[/) == -1 || entry.get_text().search(/\]/) == -1) + return 0; + + let woeid = entry.get_text().split(/\[/)[1].split(/\]/)[0]; + if (!woeid) + return 0; + + that.loadJsonAsync(encodeURI('http://query.yahooapis.com/v1/public/yql?format=json&q=select woeid,name,admin1,country from geo.places where woeid = "' + woeid + '" limit 1'), function() { + let city = arguments[0].query; + if (Number(city.count) > 0) + city = city.results.place; + else + return 0; + + let cityText = city.name; + if (city.admin1) + cityText += ", " + city.admin1.content; + + if (city.country) + cityText += " (" + city.country.code + ")"; + + if (that.city) + that.city = that.city + " && " + city.woeid + ">" + cityText; + else + that.city = city.woeid + ">" + cityText; + return 0; + }, "lastTest"); + } + dialog.hide(); + return 0; + }); + + dialog.show_all(); + }, + + removeCity: function() { + let that = this; + let city = this.city.split(" && "); + if (!city.length) + return 0; + let ac = this.actual_city; + let textDialog = _("Remove %s ?").replace("%s", this.extractLocation(city[ac])); + let dialog = new Gtk.Dialog({ + title: "" + }); + let label = new Gtk.Label({ + label: textDialog + }); + label.margin_bottom = 12; + + dialog.set_border_width(12); + dialog.set_modal(1); + dialog.set_resizable(0); + //dialog.set_transient_for(***** Need parent Window *****); + + dialog.add_button(Gtk.STOCK_NO, 0); + let d = dialog.add_button(Gtk.STOCK_YES, 1); + + d.set_can_default(true); + dialog.set_default(d); + + let dialog_area = dialog.get_content_area(); + dialog_area.pack_start(label, 0, 0, 0); + dialog.connect("response", function(w, response_id) { + if (response_id) { + if (city.length == 0) + city = []; + + if (city.length > 0 && typeof city != "object") + city = [city]; + + if (city.length > 0) + city.splice(ac, 1); + + if (city.length > 1) + that.city = city.join(" && "); + else if (city[0]) + that.city = city[0]; + else + that.city = ""; + } + dialog.hide(); + return 0; + }); + + dialog.show_all(); + return 0; + }, + + changeSelection: function() { + let path = this.actual_city; + if (arguments[0]) + path = arguments[0]; + path = Gtk.TreePath.new_from_string(String(path)); + this.treeview.get_selection().select_path(path); + }, + + loadJsonAsync: function(url, fun, id) { let here = this; let message = Soup.Message.new('GET', url); - if(typeof this.asyncSession == "undefined") - this.asyncSession = {}; - - if(typeof this.asyncSession[id] != "undefined" && this.asyncSession[id]) - { - _httpSession.abort(); - this.asyncSession[id] = 0; - } - - this.asyncSession[id] = 1; - _httpSession.queue_message(message, function(_httpSession, message) - { - here.asyncSession[id] = 0; - if(!message.response_body.data) - { - fun.call(here,0); - return 0; - } - - try - { - let jp = JSON.parse(message.response_body.data); - fun.call(here, jp); - } - catch(e) - { - fun.call(here,0); - return 0; - } - return 0; - }); - }, - - loadConfig : function() - { - let that = this; - this.Settings = Convenience.getSettings(WEATHER_SETTINGS_SCHEMA); - this.Settings.connect("changed", function(){that.refreshUI();}); - }, - - get units() - { - if(!this.Settings) - this.loadConfig(); - return this.Settings.get_enum(WEATHER_UNIT_KEY); - }, - - set units(v) - { - if(!this.Settings) - this.loadConfig(); - this.Settings.set_enum(WEATHER_UNIT_KEY,v); - }, - - get pressure_unit() - { - if(!this.Settings) - this.loadConfig(); - return this.Settings.get_enum(WEATHER_PRESSURE_UNIT_KEY); - }, - - set pressure_unit(v) - { - if(!this.Settings) - this.loadConfig(); - this.Settings.set_enum(WEATHER_PRESSURE_UNIT_KEY,v); - }, - - 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 wind_direction() - { - if(!this.Settings) - this.loadConfig(); - return this.Settings.get_boolean(WEATHER_WIND_DIRECTION_KEY); - }, - - set wind_direction(v) - { - if(!this.Settings) - this.loadConfig(); - return this.Settings.set_boolean(WEATHER_WIND_DIRECTION_KEY,v); - }, - - get city() - { - if(!this.Settings) - this.loadConfig(); - return this.Settings.get_string(WEATHER_CITY_KEY); - }, - - set city(v) - { - if(!this.Settings) - this.loadConfig(); - this.Settings.set_string(WEATHER_CITY_KEY,v); - }, - - get actual_city() - { - if(!this.Settings) - this.loadConfig(); - let a = this.Settings.get_int(WEATHER_ACTUAL_CITY_KEY); - let citys = this.city.split(" && "); - - if(citys && typeof citys == "string") - citys = [citys]; - - let l = citys.length-1; - - if(a < 0) - a = 0; - - if(l < 0) - l = 0; - - if(a > l) - a = l; - - return a; - }, - - set actual_city(a) - { - if(!this.Settings) - this.loadConfig(); - let citys = this.city.split(" && "); - - if(citys && typeof citys == "string") - citys = [citys]; - - let l = citys.length-1; - - if(a < 0) - a = 0; - - if(l < 0) - l = 0; - - if(a > l) - a = l; - - this.Settings.set_int(WEATHER_ACTUAL_CITY_KEY,a); - }, - - get translate_condition() - { - if(!this.Settings) - this.loadConfig(); - return this.Settings.get_boolean(WEATHER_TRANSLATE_CONDITION_KEY); - }, - - set translate_condition(v) - { - if(!this.Settings) - this.loadConfig(); - this.Settings.set_boolean(WEATHER_TRANSLATE_CONDITION_KEY,v); - }, - - get icon_type() - { - if(!this.Settings) - this.loadConfig(); - return this.Settings.get_boolean(WEATHER_USE_SYMBOLIC_ICONS_KEY); - }, - - set icon_type(v) - { - if(!this.Settings) - this.loadConfig(); - this.Settings.set_boolean(WEATHER_USE_SYMBOLIC_ICONS_KEY,v); - }, - - get text_in_panel() - { - if(!this.Settings) - this.loadConfig(); - return this.Settings.get_boolean(WEATHER_SHOW_TEXT_IN_PANEL_KEY); - }, - - set text_in_panel(v) - { - if(!this.Settings) - this.loadConfig(); - this.Settings.set_boolean(WEATHER_SHOW_TEXT_IN_PANEL_KEY,v); - }, - - get position_in_panel() - { - if(!this.Settings) - this.loadConfig(); - return this.Settings.get_enum(WEATHER_POSITION_IN_PANEL_KEY); - }, - - set position_in_panel(v) - { - if(!this.Settings) - this.loadConfig(); - this.Settings.set_enum(WEATHER_POSITION_IN_PANEL_KEY,v); - }, - - get comment_in_panel() - { - if(!this.Settings) - this.loadConfig(); - return this.Settings.get_boolean(WEATHER_SHOW_COMMENT_IN_PANEL_KEY); - }, - - set comment_in_panel(v) - { - if(!this.Settings) - this.loadConfig(); - this.Settings.set_boolean(WEATHER_SHOW_COMMENT_IN_PANEL_KEY,v); - }, - - get refresh_interval() - { - if(!this.Settings) - this.loadConfig(); - return this.Settings.get_int(WEATHER_REFRESH_INTERVAL); - }, - - set refresh_interval(v) - { - if(!this.Settings) - this.loadConfig(); - this.Settings.set_int(WEATHER_REFRESH_INTERVAL,v); - }, - - extractLocation : function(a) - { - if(a.search(">") == -1) - return _("Invalid city"); - return a.split(">")[1]; - }, - - extractWoeid : function(a) - { - if(a.search(">") == -1) - return 0; - return a.split(">")[0]; - } + if (typeof this.asyncSession == "undefined") + this.asyncSession = {}; + + if (typeof this.asyncSession[id] != "undefined" && this.asyncSession[id]) { + _httpSession.abort(); + this.asyncSession[id] = 0; + } + + this.asyncSession[id] = 1; + _httpSession.queue_message(message, function(_httpSession, message) { + here.asyncSession[id] = 0; + if (!message.response_body.data) { + fun.call(here, 0); + return 0; + } + + try { + let jp = JSON.parse(message.response_body.data); + fun.call(here, jp); + } catch (e) { + fun.call(here, 0); + return 0; + } + return 0; + }); + }, + + loadConfig: function() { + let that = this; + this.Settings = Convenience.getSettings(WEATHER_SETTINGS_SCHEMA); + this.Settings.connect("changed", function() { + that.refreshUI(); + }); + }, + + get units() { + if (!this.Settings) + this.loadConfig(); + return this.Settings.get_enum(WEATHER_UNIT_KEY); + }, + + set units(v) { + if (!this.Settings) + this.loadConfig(); + this.Settings.set_enum(WEATHER_UNIT_KEY, v); + }, + + get pressure_unit() { + if (!this.Settings) + this.loadConfig(); + return this.Settings.get_enum(WEATHER_PRESSURE_UNIT_KEY); + }, + + set pressure_unit(v) { + if (!this.Settings) + this.loadConfig(); + this.Settings.set_enum(WEATHER_PRESSURE_UNIT_KEY, v); + }, + + 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 wind_direction() { + if (!this.Settings) + this.loadConfig(); + return this.Settings.get_boolean(WEATHER_WIND_DIRECTION_KEY); + }, + + set wind_direction(v) { + if (!this.Settings) + this.loadConfig(); + return this.Settings.set_boolean(WEATHER_WIND_DIRECTION_KEY, v); + }, + + get city() { + if (!this.Settings) + this.loadConfig(); + return this.Settings.get_string(WEATHER_CITY_KEY); + }, + + set city(v) { + if (!this.Settings) + this.loadConfig(); + this.Settings.set_string(WEATHER_CITY_KEY, v); + }, + + get actual_city() { + if (!this.Settings) + this.loadConfig(); + let a = this.Settings.get_int(WEATHER_ACTUAL_CITY_KEY); + let citys = this.city.split(" && "); + + if (citys && typeof citys == "string") + citys = [citys]; + + let l = citys.length - 1; + + if (a < 0) + a = 0; + + if (l < 0) + l = 0; + + if (a > l) + a = l; + + return a; + }, + + set actual_city(a) { + if (!this.Settings) + this.loadConfig(); + let citys = this.city.split(" && "); + + if (citys && typeof citys == "string") + citys = [citys]; + + let l = citys.length - 1; + + if (a < 0) + a = 0; + + if (l < 0) + l = 0; + + if (a > l) + a = l; + + this.Settings.set_int(WEATHER_ACTUAL_CITY_KEY, a); + }, + + get translate_condition() { + if (!this.Settings) + this.loadConfig(); + return this.Settings.get_boolean(WEATHER_TRANSLATE_CONDITION_KEY); + }, + + set translate_condition(v) { + if (!this.Settings) + this.loadConfig(); + this.Settings.set_boolean(WEATHER_TRANSLATE_CONDITION_KEY, v); + }, + + get icon_type() { + if (!this.Settings) + this.loadConfig(); + return this.Settings.get_boolean(WEATHER_USE_SYMBOLIC_ICONS_KEY); + }, + + set icon_type(v) { + if (!this.Settings) + this.loadConfig(); + this.Settings.set_boolean(WEATHER_USE_SYMBOLIC_ICONS_KEY, v); + }, + + get text_in_panel() { + if (!this.Settings) + this.loadConfig(); + return this.Settings.get_boolean(WEATHER_SHOW_TEXT_IN_PANEL_KEY); + }, + + set text_in_panel(v) { + if (!this.Settings) + this.loadConfig(); + this.Settings.set_boolean(WEATHER_SHOW_TEXT_IN_PANEL_KEY, v); + }, + + get position_in_panel() { + if (!this.Settings) + this.loadConfig(); + return this.Settings.get_enum(WEATHER_POSITION_IN_PANEL_KEY); + }, + + set position_in_panel(v) { + if (!this.Settings) + this.loadConfig(); + this.Settings.set_enum(WEATHER_POSITION_IN_PANEL_KEY, v); + }, + + get comment_in_panel() { + if (!this.Settings) + this.loadConfig(); + return this.Settings.get_boolean(WEATHER_SHOW_COMMENT_IN_PANEL_KEY); + }, + + set comment_in_panel(v) { + if (!this.Settings) + this.loadConfig(); + this.Settings.set_boolean(WEATHER_SHOW_COMMENT_IN_PANEL_KEY, v); + }, + + get refresh_interval() { + if (!this.Settings) + this.loadConfig(); + return this.Settings.get_int(WEATHER_REFRESH_INTERVAL); + }, + + set refresh_interval(v) { + if (!this.Settings) + this.loadConfig(); + this.Settings.set_int(WEATHER_REFRESH_INTERVAL, v); + }, + + extractLocation: function(a) { + if (a.search(">") == -1) + return _("Invalid city"); + return a.split(">")[1]; + }, + + extractWoeid: function(a) { + if (a.search(">") == -1) + return 0; + return a.split(">")[0]; + } }); -function init() -{ -Convenience.initTranslations('gnome-shell-extension-weather'); +function init() { + Convenience.initTranslations('gnome-shell-extension-weather'); } -function buildPrefsWidget() -{ -let widget = new WeatherPrefsWidget(); -widget.show_all(); -return widget; +function buildPrefsWidget() { + let widget = new WeatherPrefsWidget(); + widget.show_all(); + return widget; }