From 34f5b09c09ba282b1673548a195e714cf88b53f2 Mon Sep 17 00:00:00 2001 From: Jens Lody Date: Thu, 31 Oct 2013 10:04:28 +0100 Subject: [PATCH] Add possibility to use personal API key from openweathermap.org --- ...me.shell.extensions.weather.gschema.xml.in | 4 ++ data/weather-settings.ui | 4 +- src/extension.js | 24 +++++++++- src/prefs.js | 44 +++++++++++++++++++ 4 files changed, 73 insertions(+), 3 deletions(-) diff --git a/data/org.gnome.shell.extensions.weather.gschema.xml.in b/data/org.gnome.shell.extensions.weather.gschema.xml.in index e1600c5..ff46bf5 100644 --- a/data/org.gnome.shell.extensions.weather.gschema.xml.in +++ b/data/org.gnome.shell.extensions.weather.gschema.xml.in @@ -100,5 +100,9 @@ 2 <_summary>Number of days in forecast + + '' + <_summary>Your personal API key from openweathermap.org + diff --git a/data/weather-settings.ui b/data/weather-settings.ui index a16ef2a..ee727c3 100644 --- a/data/weather-settings.ui +++ b/data/weather-settings.ui @@ -100,10 +100,10 @@ True False - 6 + 12 2 36 - 20 + 12 36 diff --git a/src/extension.js b/src/extension.js index 6bfcbcf..bfe0558 100644 --- a/src/extension.js +++ b/src/extension.js @@ -71,6 +71,7 @@ const WEATHER_REFRESH_INTERVAL_CURRENT = 'refresh-interval-current'; const WEATHER_REFRESH_INTERVAL_FORECAST = 'refresh-interval-forecast'; const WEATHER_CENTER_FORECAST_KEY = 'center-forecast'; const WEATHER_DAYS_FORECAST = 'days-forecast'; +const WEATHER_OWM_API_KEY = 'appid'; //URL const WEATHER_URL_BASE = 'http://api.openweathermap.org/data/2.5/'; @@ -552,6 +553,19 @@ const WeatherMenuButton = new Lang.Class({ this._settings.set_int(WEATHER_DAYS_FORECAST, v); }, + get _appid() { + if (!this._settings) + this.loadConfig(); + let key = this._settings.get_string(WEATHER_OWM_API_KEY); + return (key.length == 32)?key:''; + }, + + set _appid(v) { + if (!this._settings) + this.loadConfig(); + this._settings.set_string(WEATHER_OWM_API_KEY, v); + }, + rebuildSelectCityItem: function() { let that = this; this._selectCity.menu.removeAll(); @@ -638,6 +652,9 @@ const WeatherMenuButton = new Lang.Class({ q: cities[a], type: 'like' }; + if(this._appid) + params['APPID'] = this._appid; + this.load_json_async(WEATHER_URL_CURRENT, params, function() { let city = arguments[0]; @@ -1083,6 +1100,9 @@ weather-storm.png = weather-storm-symbolic.svg q: this.extractCity(this._city), units: 'metric' }; + if(this._appid) + params['APPID'] = this._appid; + this.load_json_async(WEATHER_URL_CURRENT, params, function(json) { if (!json) return 0; @@ -1338,8 +1358,10 @@ weather-storm.png = weather-storm-symbolic.svg let params = { q: this.extractCity(this._city), units: 'metric', - cnt : '10', + cnt : '10' }; + if(this._appid) + params['APPID'] = this._appid; this.load_json_async(WEATHER_URL_FORECAST, params, function(json) { if (!json) diff --git a/src/prefs.js b/src/prefs.js index 5d1030f..a1b0587 100644 --- a/src/prefs.js +++ b/src/prefs.js @@ -27,6 +27,7 @@ * */ const Gtk = imports.gi.Gtk; +const Gdk = imports.gi.Gdk; const GObject = imports.gi.GObject; const GtkBuilder = Gtk.Builder; const Gio = imports.gi.Gio; @@ -57,6 +58,7 @@ const WEATHER_REFRESH_INTERVAL_CURRENT = 'refresh-interval-current'; const WEATHER_REFRESH_INTERVAL_FORECAST = 'refresh-interval-forecast'; const WEATHER_CENTER_FORECAST_KEY = 'center-forecast'; const WEATHER_DAYS_FORECAST = 'days-forecast'; +const WEATHER_OWM_API_KEY = 'appid'; //URL const WEATHER_URL_BASE = 'http://api.openweathermap.org/data/2.5/'; @@ -144,6 +146,8 @@ const WeatherPrefsWidget = new GObject.Class({ this.addSwitch("center_forecast"); this.addLabel(_("Number of days in forecast")); this.addComboBox(["2", "3", "4", "5", "6", "7", "8", "9", "10"], "days_forecast"); + this.addLabel(_("Enter your personal Api key from openweather.org")); + this.addAppidEntry(("appid")); }, refreshUI: function() { @@ -261,6 +265,30 @@ const WeatherPrefsWidget = new GObject.Class({ this.inc(); }, + addAppidEntry: function(a) { + let that = this; + let en = new Gtk.Entry(); + this.configWidgets.push([en, a]); + en.visible = 1; + en.can_focus = 1; + en.set_width_chars(32); + en.text = this[a]; + if (this[a].length != 32) + en.set_icon_from_icon_name(Gtk.PositionType.LEFT, 'dialog-warning'); + + en.connect("notify::text", function() { + let key = arguments[0].text; + let rgba = new Gdk.Color(); + that[a] = key; + if (key.length == 32) + en.set_icon_from_icon_name(Gtk.PositionType.LEFT, ''); + else + en.set_icon_from_icon_name(Gtk.PositionType.LEFT, 'dialog-warning'); + }); + this.right_widget.attach(en, 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]; @@ -350,6 +378,8 @@ const WeatherPrefsWidget = new GObject.Class({ units: 'metric', q: location }; + if(this._appid) + params['APPID'] = this._appid; if (testLocation(location) == 0) that.loadJsonAsync(WEATHER_URL_FIND, params, function() { if (!arguments[0]) @@ -414,6 +444,8 @@ const WeatherPrefsWidget = new GObject.Class({ q: name, type: 'accurate' }; + if(this._appid) + params['APPID'] = this._appid; that.loadJsonAsync(WEATHER_URL_CURRENT, params, function() { if (!arguments[0]) return 0; @@ -766,6 +798,18 @@ const WeatherPrefsWidget = new GObject.Class({ this.Settings.set_int(WEATHER_DAYS_FORECAST, v + 2); }, + get appid() { + if (!this.Settings) + this.loadConfig(); + return this.Settings.get_string(WEATHER_OWM_API_KEY); + }, + + set appid(v) { + if (!this.Settings) + this.loadConfig(); + this.Settings.set_string(WEATHER_OWM_API_KEY, v); + }, + extractLocation: function(a) { if (a.search(">") == -1) return _("Invalid city");