Add possibility to use personal API key from openweathermap.org

openweathermap_3.6
Jens Lody 11 years ago
parent dcee31dcfc
commit 34f5b09c09
  1. 4
      data/org.gnome.shell.extensions.weather.gschema.xml.in
  2. 4
      data/weather-settings.ui
  3. 24
      src/extension.js
  4. 44
      src/prefs.js

@ -100,5 +100,9 @@
<default>2</default>
<_summary>Number of days in forecast</_summary>
</key>
<key name="appid" type="s">
<default>''</default>
<_summary>Your personal API key from openweathermap.org</_summary>
</key>
</schema>
</schemalist>

@ -100,10 +100,10 @@
<object class="GtkTable" id="right-widget-table">
<property name="visible">True</property>
<property name="can_focus">False</property>
<property name="n_rows">6</property>
<property name="n_rows">12</property>
<property name="n_columns">2</property>
<property name="column_spacing">36</property>
<property name="row_spacing">20</property>
<property name="row_spacing">12</property>
<property name="margin_left">36</property>
</object>
</child>

@ -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)

@ -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");

Loading…
Cancel
Save