Advertisement
Guest User

Weather gnome-shell french

a guest
May 24th, 2011
1,625
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  
  3.  *  Weather extension for Gnome shell
  4.  *  - Displays a small weather information on the top panel
  5.  *  - On click, gives a popup with details about the weather
  6.  
  7.     Copyright (C) 2011
  8.         ecyrbe <ecyrbe@gmail.com>
  9.         Timur Kristóf <venemo@msn.com>,
  10.         Elad Alfassa <elad@fedoraproject.org>,
  11.         Simon Legner <Simon.Legner@gmail.com>
  12.  
  13.     This file is part of gnome-shell-extension-weather.
  14.  
  15.     gnome-shell-extension-weather is free software: you can redistribute it and/or modify
  16.     it under the terms of the GNU General Public License as published by
  17.     the Free Software Foundation, either version 3 of the License, or
  18.     (at your option) any later version.
  19.  
  20.     gnome-shell-extension-weather is distributed in the hope that it will be useful,
  21.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  22.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23.     GNU General Public License for more details.
  24.  
  25.     You should have received a copy of the GNU General Public License
  26.     along with gnome-shell-extension-weather.  If not, see <http://www.gnu.org/licenses/>.
  27.  
  28. */
  29.  
  30. const GLib = imports.gi.GLib;
  31. const Gio = imports.gi.Gio;
  32. const Lang = imports.lang;
  33. const Mainloop = imports.mainloop;
  34. const Cairo = imports.cairo;
  35. const Clutter = imports.gi.Clutter;
  36. const Shell = imports.gi.Shell;
  37. const St = imports.gi.St;
  38. const Gettext = imports.gettext.domain('gnome-shell');
  39. const _ = Gettext.gettext;
  40.  
  41. const Json = imports.gi.Json;
  42. const Main = imports.ui.main;
  43. const PanelMenu = imports.ui.panelMenu;
  44. const PopupMenu = imports.ui.popupMenu;
  45. const Soup = imports.gi.Soup;
  46. const Util = imports.misc.util;
  47.  
  48. const UNITS = 'c'; // Units for temperature (case sensitive). f: Fahrenheit. c: Celsius
  49. const YAHOO_ID = 'FRXX6724'; // PUT your YAHOO ZIP code here
  50. const WEATHER_URL = 'http://weather.yahooapis.com/forecastjson?u=' + UNITS + '&p=' + YAHOO_ID;
  51. const FORECAST_URL = 'http://query.yahooapis.com/v1/public/yql?format=json&q=select%20item.forecast%20from%20weather.forecast%20where%20location%3D%22' + YAHOO_ID + '%22%20%20and%20u="' + UNITS + '"';
  52.  
  53.  
  54. function WeatherMenuButton() {
  55.     this._init();
  56. }
  57.  
  58. WeatherMenuButton.prototype = {
  59.     __proto__: PanelMenu.Button.prototype,
  60.  
  61.     _init: function() {
  62.    
  63.         // Panel icon
  64.         this._weatherIcon = new St.Icon({
  65.             icon_type: St.IconType.FULLCOLOR,
  66.             icon_size: Main.panel.button.get_child().height - 4,
  67.             icon_name: 'view-refresh-symbolic',
  68.             style_class: 'weather-icon' + (Main.panel.actor.get_direction() == St.TextDirection.RTL ? '-rtl' : '')
  69.         });
  70.        
  71.         // Label
  72.         this._weatherInfo = new St.Label({ text: _('...') });
  73.  
  74.         // Panel menu item - the current class
  75.         let menuAlignment = 0.25;
  76.         if (St.Widget.get_default_direction() == St.TextDirection.RTL)
  77.             menuAlignment = 1.0 - menuAlignment;
  78.         PanelMenu.Button.prototype._init.call(this, menuAlignment);
  79.  
  80.         // Putting the panel item together
  81.         let topBox = new St.BoxLayout();        
  82.         topBox.add_actor(this._weatherIcon);
  83.         topBox.add_actor(this._weatherInfo);
  84.         this.actor.set_child(topBox);
  85.         Main.panel._centerBox.add(this.actor, { y_fill: true });
  86.  
  87.         // Current weather
  88.         this._currentWeather = new St.Bin({style_class: 'current'});
  89.         // Future weather
  90.         this._futureWeather = new St.Bin({style_class: 'forecast'/*, x_align: St.Align.START*/});
  91.        
  92.         // Separator (copied from Gnome shell's popupMenu.js)
  93.         this._separatorArea = new St.DrawingArea({ style_class: 'popup-separator-menu-item' });
  94.         this._separatorArea.width = 200;
  95.         this._separatorArea.connect('repaint', Lang.bind(this, this._onSeparatorAreaRepaint));
  96.        
  97.         // Putting the popup item together
  98.         let mainBox = new St.BoxLayout({vertical: true});
  99.         mainBox.add_actor(this._currentWeather);
  100.         mainBox.add_actor(this._separatorArea);
  101.         mainBox.add_actor(this._futureWeather);
  102.        
  103.         this.menu.addActor(mainBox);
  104.        
  105.         // Items
  106.         this.showLoadingUi();
  107.        
  108.         this.rebuildCurrentWeatherUi();
  109.         this.rebuildFutureWeatherUi();
  110.  
  111.         // Show weather
  112.         here = this;
  113.         Mainloop.timeout_add(3000, function() {
  114.             here.refreshWeather();
  115.         });
  116.  
  117.     },
  118.  
  119.     get_weather_icon: function(code) {
  120.         switch (parseInt(code, 10)){
  121.             case 4:
  122.                 return 'weather-storm';
  123.             case 5:
  124.             case 10:
  125.             case 11:
  126.             case 12:
  127.             case 39:
  128.             case 40:
  129.                 return 'weather-showers';
  130.             case 26:
  131.                 return 'weather-overcast';
  132.             case 28:
  133.             case 30:
  134.             case 44:
  135.                 return 'weather-few-clouds';
  136.             case 32:
  137.             case 34:
  138.             case 36:
  139.                 return 'weather-clear';
  140.             default:
  141.                 return 'weather-snow';
  142.         }
  143.     },
  144.    
  145.     get_weather_condition: function(code) {
  146.         switch (parseInt(code,10)){
  147.            case 0 :  return 'Tornade';
  148.            case 1 :  return 'Tempete tropicale';
  149.            case 2 :  return 'Ouragan';
  150.            case 3 :  return 'Orage severe';
  151.            case 4 :  return 'Orage';
  152.            case 5 :  return 'Neige et pluie';
  153.            case 6 :  return 'Pluie et grele';
  154.            case 7 :  return 'Neige et grele';
  155.            case 8 :  return 'Pluie fine froides';
  156.            case 9 :  return 'Pluie fine';
  157.            case 10:  return 'Pluie verglassante';
  158.            case 11 :
  159.            case 12 : return 'Averses';
  160.            case 13 :
  161.            case 14 :
  162.            case 15 :
  163.            case 16 : return 'Neige';
  164.            case 17 : return 'Grele';
  165.            case 18 : return 'Neige fondue';
  166.            case 19 : return 'Poussière';
  167.            case 20 : return 'Brouillard';
  168.            case 21 : return 'Brume';
  169.            case 22 : return 'Polution';
  170.            case 23 : return 'Bourrasques de vent';
  171.            case 24 : return 'Venteux';
  172.            case 25 : return 'Froid';
  173.            case 26 :
  174.            case 27 :
  175.            case 28 :
  176.            case 29 :
  177.            case 30 : return 'Nuageux';
  178.            case 31 : return 'Degage';
  179.            case 32 : return 'Ensoleille'
  180.            case 33 :
  181.            case 34 : return 'Beau temps'
  182.            case 35 : return 'Pluie et grele'
  183.            case 36 : return 'Chaud';
  184.            case 37 :
  185.            case 38 :
  186.            case 39 : return 'Eclairs orageux';
  187.            case 40 : return 'Pluie et Eclaicies';
  188.            case 41 :
  189.            case 42 :
  190.            case 43 : return 'Tempete de neige';
  191.            case 44 : return 'Eclaicies';
  192.            case 45 : return 'Orages';
  193.            case 46 : return 'Tempête de neige';
  194.            case 47 : return 'Orages intermittant';
  195.            default: return 'Inconnu';
  196.         }
  197.     },
  198.  
  199.     load_json: function(url) {
  200.         var session = new Soup.SessionSync();
  201.         var message = Soup.Message.new('GET', url);
  202.         stat = session.send_message(message);
  203.         jp = new Json.Parser();
  204.         jp.load_from_data(message.response_body.data, -1);
  205.         return jp.get_root();
  206.     },
  207.  
  208.     load_json_async: function(url, fun) {
  209.         here = this;
  210.         let session = new Soup.SessionAsync();
  211.         let message = Soup.Message.new('GET', url);
  212.         session.queue_message(message, function(session, message) {
  213.             jp = new Json.Parser();
  214.             jp.load_from_data(message.response_body.data, -1);
  215.             fun.call(here, jp.get_root().get_object());
  216.         });
  217.     },
  218.    
  219.     refreshWeather: function() {
  220.  
  221.         // Refresh current weather
  222.         this.load_json_async(WEATHER_URL, function(weather) {
  223.  
  224.             let location = weather.get_object_member('location').get_string_member('city');
  225.             let comment =  this.get_weather_condition(weather.get_object_member('condition').get_string_member('code')); //weather.get_object_member('condition').get_string_member('text');
  226.             let temperature = weather.get_object_member('condition').get_double_member('temperature');
  227.             let temperature_unit = weather.get_object_member('units').get_string_member('temperature');
  228.             let humidity = weather.get_object_member('atmosphere').get_string_member('humidity') + ' %';
  229.             let pressure = weather.get_object_member('atmosphere').get_double_member('pressure');
  230.             pressure_unit = weather.get_object_member('units').get_string_member('pressure');
  231.             let wind_direction = weather.get_object_member('wind').get_string_member('direction');
  232.             let wind = weather.get_object_member('wind').get_double_member('speed');
  233.             wind_unit = weather.get_object_member('units').get_string_member('speed');
  234.             let iconname = this.get_weather_icon(weather.get_object_member('condition').get_string_member('code'));
  235.  
  236.             this._currentWeatherIcon.icon_name = this._weatherIcon.icon_name = iconname;
  237.             this._weatherInfo.text = (temperature + ' ' + temperature_unit);
  238.  
  239.             this._currentWeatherSummary.text = comment;
  240.             this._currentWeatherLocation.text = location;
  241.             this._currentWeatherTemperature.text = temperature + ' ' + temperature_unit;
  242.             this._currentWeatherHumidity.text = humidity;
  243.             this._currentWeatherPressure.text = pressure + ' ' + pressure_unit;
  244.             this._currentWeatherWind.text = wind + ' ' + wind_unit;
  245.  
  246.         });
  247.  
  248.         // Refresh forecast
  249.         this.load_json_async(FORECAST_URL, function(forecast) {
  250.  
  251.             date_string = ['Ajourd\'hui', 'Demain'];
  252.             forecast2 = forecast.get_object_member('query').get_object_member('results').get_array_member('channel').get_elements();
  253.             for (let i = 0; i <= 1; i++) {
  254.                 let forecastUi = this._forecast[i];
  255.                 let forecastData = forecast2[i].get_object().get_object_member('item').get_object_member('forecast');
  256.  
  257.                 let code = forecastData.get_string_member('code');
  258.                 let comment = this.get_weather_condition(code);//forecastData.get_string_member('text');
  259.                 let t_low = forecastData.get_string_member('low');
  260.                 let t_high = forecastData.get_string_member('high');
  261.  
  262.                 forecastUi.Day.text = date_string[i]; //+ ' (' + forecastData.get_string_member('day') + ')';
  263.                 forecastUi.Temperature.text = t_low + '\u2013' + t_high + ' ' + UNITS.toUpperCase();
  264.                 forecastUi.Summary.text = comment;
  265.                 forecastUi.Icon.icon_name = this.get_weather_icon(code);
  266.             }
  267.  
  268.         });
  269.  
  270.         // Repeatedly refresh weather
  271.         Mainloop.timeout_add_seconds(60*4, Lang.bind(this, this.refreshWeather));
  272.        
  273.     },
  274.    
  275.     destroyCurrentWeather: function() {
  276.         if (this._currentWeather.get_child() != null)
  277.             this._currentWeather.get_child().destroy();        
  278.     },
  279.    
  280.     destroyFutureWeather: function() {
  281.         if (this._futureWeather.get_child() != null)
  282.             this._futureWeather.get_child().destroy();        
  283.     },
  284.    
  285.     showLoadingUi: function() {
  286.         this.destroyCurrentWeather();
  287.         this.destroyFutureWeather();
  288.         this._currentWeather.set_child(new St.Label({ text: _('Loading current weather ...') }));
  289.         this._futureWeather.set_child(new St.Label({ text: _('Loading future weather ...') }));
  290.     },
  291.    
  292.     rebuildCurrentWeatherUi: function() {
  293.         this.destroyCurrentWeather();
  294.        
  295.         // This will hold the icon for the current weather
  296.         this._currentWeatherIcon = new St.Icon({
  297.             icon_type: St.IconType.FULLCOLOR,
  298.             icon_size: 64,
  299.             icon_name: 'view-refresh-symbolic',
  300.             style_class: 'weather-current-icon'
  301.         });
  302.        
  303.         // The summary of the current weather
  304.         this._currentWeatherSummary = new St.Label({
  305.             text: _('Loading ...'),
  306.             style_class: 'weather-current-summary'
  307.         });
  308.         this._currentWeatherLocation = new St.Label({ text: _('Please wait') });
  309.  
  310.         let bb = new St.BoxLayout({vertical: true, style_class: 'weather-current-summarybox'});
  311.         bb.add_actor(this._currentWeatherLocation);
  312.         bb.add_actor(this._currentWeatherSummary);
  313.        
  314.         // Other labels
  315.         this._currentWeatherTemperature = new St.Label({ text: '...' });
  316.         this._currentWeatherHumidity = new St.Label({ text:  '...' });
  317.         this._currentWeatherPressure = new St.Label({ text: '...' });
  318.         this._currentWeatherWind = new St.Label({ text: '...' });
  319.        
  320.         let rb = new St.BoxLayout({style_class: 'weather-current-databox'});
  321.         rb_captions = new St.BoxLayout({vertical: true, style_class: 'weather-current-databox-captions'});
  322.         rb_values = new St.BoxLayout({vertical: true, style_class: 'weather-current-databox-values'});
  323.         rb.add_actor(rb_captions);
  324.         rb.add_actor(rb_values);
  325.  
  326.         rb_captions.add_actor(new St.Label({text: _('Temperature:')}));
  327.         rb_values.add_actor(this._currentWeatherTemperature);
  328.         rb_captions.add_actor(new St.Label({text: _('Humidite:')}));
  329.         rb_values.add_actor(this._currentWeatherHumidity);
  330.         rb_captions.add_actor(new St.Label({text: _('Pression:')}));
  331.         rb_values.add_actor(this._currentWeatherPressure);
  332.         rb_captions.add_actor(new St.Label({text: _('Vent:')}));
  333.         rb_values.add_actor(this._currentWeatherWind);
  334.        
  335.         let xb = new St.BoxLayout();
  336.         xb.add_actor(bb);
  337.         xb.add_actor(rb);
  338.        
  339.         let box = new St.BoxLayout({style_class: 'weather-current-iconbox'});
  340.         box.add_actor(this._currentWeatherIcon);
  341.         box.add_actor(xb);
  342.         this._currentWeather.set_child(box);
  343.        
  344.     },
  345.    
  346.     rebuildFutureWeatherUi: function() {
  347.         this.destroyFutureWeather();
  348.  
  349.         this._forecast = [];
  350.         this._forecastBox = new St.BoxLayout();
  351.         this._futureWeather.set_child(this._forecastBox);
  352.  
  353.         for (let i = 0; i <= 1; i++) {
  354.             let forecastWeather = {};
  355.  
  356.             forecastWeather.Icon = new St.Icon({
  357.                 icon_type: St.IconType.FULLCOLOR,
  358.                 icon_size: 48,
  359.                 icon_name: 'view-refresh-symbolic',
  360.                 style_class: 'weather-forecast-icon'
  361.             });
  362.             forecastWeather.Day = new St.Label({style_class: 'weather-forecast-day'});
  363.             forecastWeather.Summary = new St.Label({style_class: 'weather-forecast-summary'});
  364.             forecastWeather.Temperature = new St.Label({style_class: 'weather-forecast-temperature'});
  365.  
  366.             let by = new St.BoxLayout({vertical: true, style_class: 'weather-forecast-databox'});
  367.             by.add_actor(forecastWeather.Day);
  368.             by.add_actor(forecastWeather.Summary);
  369.             by.add_actor(forecastWeather.Temperature);
  370.  
  371.             let bb = new St.BoxLayout({style_class: 'weather-forecast-box'});
  372.             bb.add_actor(forecastWeather.Icon);
  373.             bb.add_actor(by);
  374.  
  375.             this._forecast[i] = forecastWeather;
  376.             this._forecastBox.add_actor(bb);
  377.  
  378.         }
  379.        
  380.     },
  381.    
  382.     // Copied from Gnome shell's popupMenu.js
  383.     _onSeparatorAreaRepaint: function(area) {
  384.         let cr = area.get_context();
  385.         let themeNode = area.get_theme_node();
  386.         let [width, height] = area.get_surface_size();
  387.         let margin = themeNode.get_length('-margin-horizontal');
  388.         let gradientHeight = themeNode.get_length('-gradient-height');
  389.         let startColor = themeNode.get_color('-gradient-start');
  390.         let endColor = themeNode.get_color('-gradient-end');
  391.  
  392.         let gradientWidth = (width - margin * 2);
  393.         let gradientOffset = (height - gradientHeight) / 2;
  394.         let pattern = new Cairo.LinearGradient(margin, gradientOffset, width - margin, gradientOffset + gradientHeight);
  395.         pattern.addColorStopRGBA(0, startColor.red / 255, startColor.green / 255, startColor.blue / 255, startColor.alpha / 255);
  396.         pattern.addColorStopRGBA(0.5, endColor.red / 255, endColor.green / 255, endColor.blue / 255, endColor.alpha / 255);
  397.         pattern.addColorStopRGBA(1, startColor.red / 255, startColor.green / 255, startColor.blue / 255, startColor.alpha / 255);
  398.         cr.setSource(pattern);
  399.         cr.rectangle(margin, gradientOffset, gradientWidth, gradientHeight);
  400.         cr.fill();
  401.     }
  402. };
  403.  
  404. function main() {
  405.  
  406.     this._weatherMenu = new WeatherMenuButton();
  407. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement