@ -45,6 +45,7 @@ const EXTENSIONDIR = Me.dir.get_path();
const OPENWEATHER _SETTINGS _SCHEMA = 'org.gnome.shell.extensions.openweather' ;
const OPENWEATHER _PROVIDER _KEY = 'weather-provider' ;
const OPENWEATHER _GEOLOCATION _PROVIDER _KEY = 'geolocation-provider' ;
const OPENWEATHER _UNIT _KEY = 'unit' ;
const OPENWEATHER _PRESSURE _UNIT _KEY = 'pressure-unit' ;
const OPENWEATHER _WIND _SPEED _UNIT _KEY = 'wind-speed-unit' ;
@ -65,18 +66,26 @@ const OPENWEATHER_DAYS_FORECAST = 'days-forecast';
const OPENWEATHER _DECIMAL _PLACES = 'decimal-places' ;
const OPENWEATHER _OWM _API _KEY = 'appid' ;
const OPENWEATHER _FC _API _KEY = 'appid-fc' ;
const OPENWEATHER _GC _APP _KEY = 'geolocation-appid-mapquest' ;
//URL
const OPENWEATHER _URL _BASE = 'https://open.mapquestapi.com/nominatim/v1/' ;
const OPENWEATHER _URL _FIND = OPENWEATHER _URL _BASE + 'search.php' ;
const OPENWEATHER _URL _REVERSE = OPENWEATHER _URL _BASE + 'reverse.php' ;
const OPENWEATHER _URL _MAPQUEST _BASE = 'https://open.mapquestapi.com/nominatim/v1/' ;
const OPENWEATHER _URL _MAPQUEST _FIND = OPENWEATHER _URL _MAPQUEST _BASE + 'search.php' ;
const OPENWEATHER _URL _GEOCODE _BASE = 'https://www.geocode.farm/v3/' ;
const OPENWEATHER _URL _GEOCODE _FIND = OPENWEATHER _URL _GEOCODE _BASE + 'json/forward' ;
// Keep enums in sync with GSettings schemas
const WeatherProvider = {
DEFAULT : - 1 ,
OPENWEATHERMAP : 0 ,
FORECAST _IO : 1
} ;
const GeolocationProvider = {
GEOCODE : 0 ,
MAPQUEST : 1
} ;
let _httpSession ;
let mCities = null ;
@ -169,11 +178,6 @@ const WeatherPrefsWidget = new GObject.Class({
let location = this . searchName . get _text ( ) . trim ( ) ;
if ( location === "" )
return 0 ;
let params = {
format : 'json' ,
addressdetails : '1' ,
q : location
} ;
let item = new Gtk . MenuItem ( ) ;
if ( this . spinner . get _parent ( ) )
@ -183,37 +187,120 @@ const WeatherPrefsWidget = new GObject.Class({
this . searchMenu . show _all ( ) ;
this . searchMenu . popup ( null , null , Lang . bind ( this , this . placeSearchMenu ) , 0 , this . searchName ) ;
this . loadJsonAsync ( OPENWEATHER _URL _FIND , params , Lang . bind ( this , function ( ) {
if ( ! arguments [ 0 ] )
return 0 ;
this . clearSearchMenu ( ) ;
let newCity = arguments [ 0 ] ;
if ( Number ( newCity . length ) < 1 ) {
if ( this . geolocation _provider == GeolocationProvider . MAPQUEST ) {
if ( this . geolocation _appid _mapquest === '' ) {
this . clearSearchMenu ( ) ;
let item = new Gtk . MenuItem ( {
label : _ ( "\"%s\" not found" ) . format ( location )
label : "You need an AppKey to search on openmapquest."
} ) ;
this . searchMenu . append ( item ) ;
item = new Gtk . MenuItem ( {
label : "Please visit https://developer.mapquest.com/ ."
} ) ;
this . searchMenu . append ( item ) ;
} else {
var m = { } ;
for ( var i in newCity ) {
this . searchMenu . show _all ( ) ;
this . searchMenu . popup ( null , null , Lang . bind ( this , this . placeSearchMenu ) , 0 , this . searchName ) ;
return 0 ;
}
let params = {
key : this . geolocation _appid _mapquest ,
format : 'json' ,
addressdetails : '1' ,
q : location
} ;
this . loadJsonAsync ( OPENWEATHER _URL _MAPQUEST _FIND , params , Lang . bind ( this , function ( ) {
this . clearSearchMenu ( ) ;
if ( ! arguments [ 0 ] ) {
let item = new Gtk . MenuItem ( {
label : _ ( "Invalid data when searching for\"%s\"" ) . format ( location )
} ) ;
this . searchMenu . append ( item ) ;
item = new Gtk . MenuItem ( {
label : "Do you use a valid AppKey to search on openmapquest ?"
} ) ;
this . searchMenu . append ( item ) ;
item = new Gtk . MenuItem ( {
label : "If not, please visit https://developer.mapquest.com/ ."
} ) ;
this . searchMenu . append ( item ) ;
} else {
let newCity = arguments [ 0 ] ;
if ( Number ( newCity . length ) < 1 ) {
let item = new Gtk . MenuItem ( {
label : _ ( "\"%s\" not found" ) . format ( location )
} ) ;
this . searchMenu . append ( item ) ;
} else {
var m = { } ;
for ( var i in newCity ) {
let cityText = newCity [ i ] . display _name ;
let cityCoord = "[" + newCity [ i ] . lat + "," + newCity [ i ] . lon + "]" ;
let item = new Gtk . MenuItem ( {
label : cityText + " " + cityCoord
} ) ;
item . connect ( "activate" , Lang . bind ( this , this . onActivateItem ) ) ;
this . searchMenu . append ( item ) ;
}
}
}
this . searchMenu . show _all ( ) ;
this . searchMenu . popup ( null , null , Lang . bind ( this , this . placeSearchMenu ) , 0 , this . searchName ) ;
return 0 ;
} ) ) ;
} else if ( this . geolocation _provider == GeolocationProvider . GEOCODE ) {
let params = {
addr : location
} ;
let cityText = newCity [ i ] . display _name ;
let cityCoord = "[" + newCity [ i ] . lat + "," + newCity [ i ] . lon + "]" ;
this . loadJsonAsync ( OPENWEATHER _URL _GEOCODE _FIND , params , Lang . bind ( this , function ( ) {
this . clearSearchMenu ( ) ;
if ( ! arguments [ 0 ] ) {
let item = new Gtk . MenuItem ( {
label : cityText + " " + cityCoord
label : _ ( "Invalid data when searching for\"%s\"" ) . format ( location )
} ) ;
item . connect ( "activate" , Lang . bind ( this , this . onActivateItem ) ) ;
this . searchMenu . append ( item ) ;
} else {
let newCity = arguments [ 0 ] . geocoding _results ;
if ( Number ( newCity . length ) < 1 ) {
let item = new Gtk . MenuItem ( {
label : _ ( "Invalid data when searching for\"%s\"" ) . format ( location )
} ) ;
this . searchMenu . append ( item ) ;
} else {
if ( Number ( newCity . STATUS . result _count ) < 1 ) {
let item = new Gtk . MenuItem ( {
label : _ ( "\"%s\" not found" ) . format ( location )
} ) ;
this . searchMenu . append ( item ) ;
} else {
var m = { } ;
newCity = newCity . RESULTS ;
for ( var i in newCity ) {
let cityText = newCity [ i ] . formatted _address ;
let cityCoord = "[" + newCity [ i ] . COORDINATES . latitude + "," + newCity [ i ] . COORDINATES . longitude + "]" ;
let item = new Gtk . MenuItem ( {
label : cityText + " " + cityCoord
} ) ;
item . connect ( "activate" , Lang . bind ( this , this . onActivateItem ) ) ;
this . searchMenu . append ( item ) ;
}
}
}
}
}
this . searchMenu . show _all ( ) ;
this . searchMenu . popup ( null , null , Lang . bind ( this , this . placeSearchMenu ) , 0 , this . searchName ) ;
return 0 ;
} ) ) ;
this . searchMenu . show _all ( ) ;
this . searchMenu . popup ( null , null , Lang . bind ( this , this . placeSearchMenu ) , 0 , this . searchName ) ;
return 0 ;
} ) ) ;
}
return 0 ;
} ) ) ;
@ -529,6 +616,18 @@ const WeatherPrefsWidget = new GObject.Class({
this . Settings . set _enum ( OPENWEATHER _PROVIDER _KEY , v ) ;
} ,
get geolocation _provider ( ) {
if ( ! this . Settings )
this . loadConfig ( ) ;
return this . Settings . get _enum ( OPENWEATHER _GEOLOCATION _PROVIDER _KEY ) ;
} ,
set geolocation _provider ( v ) {
if ( ! this . Settings )
this . loadConfig ( ) ;
this . Settings . set _enum ( OPENWEATHER _GEOLOCATION _PROVIDER _KEY , v ) ;
} ,
get units ( ) {
if ( ! this . Settings )
this . loadConfig ( ) ;
@ -804,6 +903,18 @@ const WeatherPrefsWidget = new GObject.Class({
this . Settings . set _string ( OPENWEATHER _FC _API _KEY , v ) ;
} ,
get geolocation _appid _mapquest ( ) {
if ( ! this . Settings )
this . loadConfig ( ) ;
return this . Settings . get _string ( OPENWEATHER _GC _APP _KEY ) ;
} ,
set geolocation _appid _mapquest ( v ) {
if ( ! this . Settings )
this . loadConfig ( ) ;
this . Settings . set _string ( OPENWEATHER _GC _APP _KEY , v ) ;
} ,
extractLocation : function ( ) {
if ( ! arguments [ 0 ] )
return "" ;