~santi698/gazette/gazette

« back to all changes in this revision

Viewing changes to src/Plugs/Weather.vala

  • Committer: Santiago Ocamica
  • Date: 2013-04-20 20:02:02 UTC
  • mfrom: (32.1.5 gazette)
  • Revision ID: santi6982@gmail.com-20130420200202-7aupiy0p4yusieb9
Added update-interval to dconf

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
namespace Gazette {
 
2
    
 
3
    public class Weather : Gazette.Service {
 
4
        
 
5
        public string weather_id = "";
 
6
        public string weather_unit = "'Celsius'";
 
7
 
 
8
        public Weather (){
 
9
            base (_("Weather Forecast"), "preferences-desktop-online-accounts");
 
10
            
 
11
            // VAR INIT
 
12
            try {
 
13
                Process.spawn_command_line_sync ("gsettings get org.pantheon.gazette.weather weather-id", out weather_id);
 
14
                Process.spawn_command_line_sync ("gsettings get org.pantheon.gazette.weather weather-unit", out weather_unit);
 
15
                weather_id = weather_id.escape("");
 
16
            }
 
17
            catch (Error e) {
 
18
                stderr.printf ("Unable to find settings: %s\n", e.message);
 
19
            }
 
20
            
 
21
 
 
22
            // ID LABEL
 
23
            var weatherIdLabel = new Gtk.Label ("* "+ _("Location ID") + " :");
 
24
                    this.container.attach(weatherIdLabel, 0, 0, 1, 1);
 
25
            weatherIdLabel.set_alignment (1, 0);
 
26
 
 
27
            // ID ENTRY            
 
28
            var weatherIdEntry = new Gtk.Entry ();
 
29
            weatherIdEntry.text = this.weather_id.substring(0, this.weather_id.length-2);
 
30
                    this.container.attach(weatherIdEntry, 1, 0, 2, 1);
 
31
 
 
32
            // ID HELP MESSAGE
 
33
            var weatherIdHelp = new Gtk.Label (_("* You can find your city's ID on <a href='http://weather.yahoo.com'>weather.yahoo.com</a>. For example the URL for London is <i>http://weather.yahoo.com/united-kingdom/england/london-44418/</i> so the city's ID is 44418."));
 
34
                    weatherIdHelp.set_use_markup (true);
 
35
                    weatherIdHelp.set_line_wrap (true);
 
36
                    this.container.attach(weatherIdHelp, 0, 10, 3, 1); 
 
37
            weatherIdHelp.set_alignment (0, 0);
 
38
 
 
39
 
 
40
            // UNIT LABEL [F/C]
 
41
            var weatherUnitLabel = new Gtk.Label (_("Unit")+ " :");
 
42
                    this.container.attach(weatherUnitLabel, 0, 1, 1, 1);
 
43
            weatherUnitLabel.set_alignment (1, 0);
 
44
            
 
45
            // UNIT ENTRY
 
46
            var weatherUnitBox = new Gtk.ComboBoxText ();
 
47
                    weatherUnitBox.append_text ("Celsius");
 
48
                    weatherUnitBox.append_text ("Fahrenheit");
 
49
 
 
50
            if (this.weather_unit == "'Celsius'")
 
51
                        weatherUnitBox.active = 1;
 
52
            else
 
53
                weatherUnitBox.active = 0;
 
54
                    this.container.attach(weatherUnitBox, 1, 1, 2, 1);
 
55
 
 
56
 
 
57
            // APPLY BUTTON
 
58
            var applyButton = new Gtk.Button.with_label (_("Apply"));
 
59
                    this.container.attach(applyButton, 1, 2, 1, 1);
 
60
            applyButton.clicked.connect(() => {
 
61
                try {
 
62
                    string str = "gsettings set org.pantheon.gazette.";
 
63
                    Process.spawn_command_line_sync (str.concat("weather weather-id '", weatherIdEntry.get_text(), "'"));
 
64
                    if (weatherUnitBox.get_active_text() == "Celsius")
 
65
                                Process.spawn_command_line_sync (str.concat("weather weather-unit 'Celsius'"));
 
66
                    else        
 
67
                        Process.spawn_command_line_sync (str.concat("weather weather-unit 'Fahrenheit'"));
 
68
 
 
69
                    Process.spawn_command_line_sync ("killall gazette");
 
70
                    Process.spawn_command_line_async ("gazette");
 
71
 
 
72
                }
 
73
                catch (Error e) {
 
74
                    stderr.printf ("Unable to find settings: %s\n", e.message);
 
75
                }
 
76
            });
 
77
 
 
78
 
 
79
            // RESET BUTTON
 
80
            var resetButton = new Gtk.Button.with_label (_("Reset"));
 
81
                    this.container.attach(resetButton, 2, 2, 1, 1);
 
82
            resetButton.clicked.connect(() => {
 
83
                try {
 
84
                    Process.spawn_command_line_sync ("gsettings reset org.pantheon.gazette.weather weather-id");
 
85
                    Process.spawn_command_line_sync ("gsettings reset org.pantheon.gazette.weather weather-unit");
 
86
                    Process.spawn_command_line_sync ("killall gazette");
 
87
                    Process.spawn_command_line_async ("gazette");
 
88
                    Process.spawn_command_line_sync ("gsettings get org.pantheon.gazette.weather weather-id", out this.weather_id);
 
89
                    Process.spawn_command_line_sync ("gsettings get org.pantheon.gazette.weather weather-unit", out this.weather_unit);
 
90
                    weather_id = weather_id.escape("");
 
91
                    weatherIdEntry.text = this.weather_id.substring(0, this.weather_id.length-2);                    
 
92
                    if (this.weather_unit == "'Celsius'")
 
93
                                weatherUnitBox.active = 1;
 
94
                    else
 
95
                        weatherUnitBox.active = 0;
 
96
                }
 
97
                catch (Error e) {
 
98
                    stderr.printf ("Unable to find settings: %s\n", e.message);
 
99
                }
 
100
            });
 
101
 
 
102
 
 
103
 
 
104
        }
 
105
        
 
106
        public override void run (File file){
 
107
            /*
 
108
            string content;
 
109
            try{
 
110
                FileUtils.get_contents (file.get_path (), out content);
 
111
            }catch (Error e){warning (e.message);}
 
112
            
 
113
            var command = "xdg-open 'http://www.wikipedia.org/wiki/"+content+"'";
 
114
            
 
115
            string ret;
 
116
            try{
 
117
                Process.spawn_command_line_sync (command, out ret);
 
118
            }catch (Error e){error (e.message);}
 
119
*/
 
120
        }
 
121
    }
 
122
}