~cszikszoy/docky/mmv3

« back to all changes in this revision

Viewing changes to StandardPlugins/Weather/src/WeatherConfiguration.cs

  • Committer: Chris S.
  • Date: 2009-10-22 03:23:13 UTC
  • mfrom: (284.1.7 testaddins)
  • Revision ID: chris@szikszoy.com-20091022032313-2s9kikljologdsyt
enable lazy loading of addins

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
//  
 
2
//  Copyright (C) 2009 Robert Dyer
 
3
// 
 
4
//  This program is free software: you can redistribute it and/or modify
 
5
//  it under the terms of the GNU General Public License as published by
 
6
//  the Free Software Foundation, either version 3 of the License, or
 
7
//  (at your option) any later version.
 
8
// 
 
9
//  This program is distributed in the hope that it will be useful,
 
10
//  but WITHOUT ANY WARRANTY; without even the implied warranty of
 
11
//  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
12
//  GNU General Public License for more details.
 
13
// 
 
14
//  You should have received a copy of the GNU General Public License
 
15
//  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 
16
// 
 
17
 
 
18
using System;
 
19
using System.Collections.Generic;
 
20
using System.Threading;
 
21
 
 
22
using Gtk;
 
23
 
 
24
namespace WeatherDocklet
 
25
{
 
26
        [System.ComponentModel.Category("File")]
 
27
        [System.ComponentModel.ToolboxItem(true)]
 
28
        public partial class WeatherConfiguration : Bin
 
29
        {
 
30
                TreeStore locationTreeStore = new TreeStore (typeof (string));
 
31
                TreeStore searchTreeStore = new TreeStore (typeof (string), typeof (string));
 
32
 
 
33
                public WeatherConfiguration ()
 
34
                {                       
 
35
                        Build ();
 
36
                        
 
37
                        locationTreeView.Model = locationTreeStore;
 
38
                        locationTreeView.Selection.Changed += OnLocationSelectionChanged;
 
39
                        locationTreeView.AppendColumn ("Locations", new CellRendererText (), "text", 0);
 
40
                        UpdateLocations ();
 
41
                        
 
42
                        searchTreeView.Model = searchTreeStore;
 
43
                        searchTreeView.Selection.Changed += OnSearchSelectionChanged;
 
44
                        searchTreeView.AppendColumn ("Search Results", new CellRendererText (), "text", 0);
 
45
                        
 
46
                        timeout.Value = WeatherPreferences.Timeout;
 
47
                        metric.Active = WeatherPreferences.Metric;
 
48
                        aboutLabel.Text = WeatherController.Weather.About;
 
49
                        
 
50
                        int selected = 0;
 
51
                        foreach (AbstractWeatherSource aws in WeatherController.Service.WeatherSources)
 
52
                        {
 
53
                                source.AppendText (aws.Name);
 
54
                                if (aws.Name.Equals(WeatherPreferences.Source))
 
55
                                        source.Active = selected;
 
56
                                selected++;
 
57
                        }
 
58
                }
 
59
                
 
60
                void UpdateLocations ()
 
61
                {
 
62
                        locationTreeStore.Clear ();
 
63
                        for (int i = 0; i < WeatherPreferences.Location.Length; i++)
 
64
                                locationTreeStore.AppendValues (WeatherPreferences.Location [i]);
 
65
                }
 
66
                
 
67
                protected virtual void OnTimeoutFocusOut (object o, Gtk.FocusOutEventArgs args)
 
68
                {
 
69
                        WeatherPreferences.Timeout = (uint) timeout.ValueAsInt;
 
70
                }
 
71
                
 
72
                protected virtual void OnMetricFocusOut (object o, Gtk.FocusOutEventArgs args)
 
73
                {
 
74
                        WeatherPreferences.Metric = metric.Active;
 
75
                }
 
76
                
 
77
                protected virtual void OnSourceChanged (object sender, System.EventArgs e)
 
78
                {
 
79
                        WeatherPreferences.Source = source.ActiveText;
 
80
                        aboutLabel.Text = WeatherController.Weather.About;
 
81
                        searchTreeStore.Clear ();
 
82
                }
 
83
                
 
84
                protected virtual void OnSearchClicked (object sender, System.EventArgs e)
 
85
                {
 
86
                        searchTreeStore.Clear ();
 
87
                        searchTreeStore.AppendValues ("Currently searching...", "");
 
88
                        
 
89
                        new Thread(() => {
 
90
                                try {
 
91
                                        List<string> vals = new List<string> ();
 
92
                
 
93
                                        foreach (string s in WeatherController.Weather.SearchLocation (location.Text))
 
94
                                                vals.Add (s);
 
95
                                        
 
96
                                        Gtk.Application.Invoke (delegate {
 
97
                                                searchTreeStore.Clear ();
 
98
                                                
 
99
                                                if (vals.Count > 0)
 
100
                                                        for (int i = 0; i < vals.Count; i += 2)
 
101
                                                                searchTreeStore.AppendValues (vals [i], vals [i + 1]);
 
102
                                                else
 
103
                                                        searchTreeStore.AppendValues ("No results found", "");
 
104
                                        });
 
105
                                } catch {}
 
106
                        }).Start ();
 
107
                }
 
108
 
 
109
                protected virtual void OnLocationSelectionChanged (object o, System.EventArgs args)
 
110
                {
 
111
                        TreeIter iter;
 
112
                        TreeModel model;
 
113
                        
 
114
                        if (((TreeSelection)o).GetSelected (out model, out iter))
 
115
                        {
 
116
                                string selected = (string) model.GetValue (iter, 0);
 
117
                                int index = FindLocationIndex (selected);
 
118
                                
 
119
                                if (index != -1) {
 
120
                                        location.Text = selected;
 
121
                                        btnMoveUp.Sensitive = index > 0;
 
122
                                        btnMoveDown.Sensitive = index < WeatherPreferences.Location.Length - 1;
 
123
                                        btnRemove.Sensitive = true;
 
124
                                        return;
 
125
                                }
 
126
                        }
 
127
                        
 
128
                        btnMoveUp.Sensitive = false;
 
129
                        btnMoveDown.Sensitive = false;
 
130
                        btnRemove.Sensitive = false;
 
131
                }
 
132
                
 
133
                protected virtual void OnSearchSelectionChanged (object o, System.EventArgs args)
 
134
                {
 
135
                        TreeIter iter;
 
136
                        TreeModel model;
 
137
                        
 
138
                        if (((TreeSelection)o).GetSelected (out model, out iter))
 
139
                        {
 
140
                                string val = (string) model.GetValue (iter, 1);
 
141
                                location.Text = val;
 
142
                        }
 
143
                }
 
144
 
 
145
                protected virtual void OnAddClicked (object sender, System.EventArgs e)
 
146
                {
 
147
                        if (location.Text.Trim ().Length == 0 || FindLocationIndex (location.Text) != -1)
 
148
                                return;
 
149
                        
 
150
                        string[] locations = new string [WeatherPreferences.Location.Length + 1];
 
151
                        Array.Copy (WeatherPreferences.Location, 0, locations, 0, WeatherPreferences.Location.Length);
 
152
                        locations [WeatherPreferences.Location.Length] = location.Text.Trim ();
 
153
                        WeatherPreferences.Location = locations;
 
154
                        locationTreeStore.AppendValues (new string[] { location.Text.Trim () });
 
155
                }
 
156
 
 
157
                int FindLocationIndex (string location)
 
158
                {
 
159
                        for (int i = 0; i < WeatherPreferences.Location.Length; i++)
 
160
                                if (WeatherPreferences.Location [i].Equals (location))
 
161
                                        return i;
 
162
                        
 
163
                        return -1;
 
164
                }
 
165
 
 
166
                protected virtual void OnRemoveClicked (object sender, System.EventArgs e)
 
167
                {
 
168
                        TreeIter iter;
 
169
                        TreeModel model;
 
170
                        
 
171
                        if (locationTreeView.Selection.GetSelected (out model, out iter))
 
172
                        {
 
173
                                string removedLocation = (string) model.GetValue (iter, 0);
 
174
                                int index = FindLocationIndex (removedLocation);
 
175
                                
 
176
                                string[] locations = new string [WeatherPreferences.Location.Length - 1];
 
177
                                Array.Copy (WeatherPreferences.Location, 0, locations, 0, index);
 
178
                                Array.Copy (WeatherPreferences.Location, index + 1, locations, index, WeatherPreferences.Location.Length - index - 1);
 
179
                                
 
180
                                WeatherPreferences.Location = locations;
 
181
                                locationTreeStore.Remove (ref iter);
 
182
                                
 
183
                                if (removedLocation.Equals (WeatherController.CurrentLocation))
 
184
                                        WeatherController.PreviousLocation ();
 
185
                        }
 
186
                }
 
187
 
 
188
                protected virtual void OnMoveDownClicked (object sender, System.EventArgs e)
 
189
                {
 
190
                        TreeIter iter;
 
191
                        TreeModel model;
 
192
                        
 
193
                        if (locationTreeView.Selection.GetSelected (out model, out iter))
 
194
                        {
 
195
                                TreePath[] paths = locationTreeView.Selection.GetSelectedRows ();
 
196
                                int index = FindLocationIndex ((string) model.GetValue (iter, 0));
 
197
                                
 
198
                                string[] locations = WeatherPreferences.Location;
 
199
                                string temp = locations [index];
 
200
                                locations [index] = locations [index + 1];
 
201
                                locations [index + 1] = temp;
 
202
                                
 
203
                                WeatherPreferences.Location = locations;
 
204
                                UpdateLocations ();
 
205
                                paths [0].Next ();
 
206
                                locationTreeView.Selection.SelectPath (paths [0]);
 
207
                                locationTreeView.ScrollToCell (paths [0], null, false, 0, 0);
 
208
                        }
 
209
                }
 
210
 
 
211
                protected virtual void OnMoveUpClicked (object sender, System.EventArgs e)
 
212
                {
 
213
                        TreeIter iter;
 
214
                        TreeModel model;
 
215
                        
 
216
                        if (locationTreeView.Selection.GetSelected (out model, out iter))
 
217
                        {
 
218
                                TreePath[] paths = locationTreeView.Selection.GetSelectedRows ();
 
219
                                int index = FindLocationIndex ((string) model.GetValue (iter, 0));
 
220
                                
 
221
                                string[] locations = WeatherPreferences.Location;
 
222
                                string temp = locations [index];
 
223
                                locations [index] = locations [index - 1];
 
224
                                locations [index - 1] = temp;
 
225
                                
 
226
                                WeatherPreferences.Location = locations;
 
227
                                UpdateLocations ();
 
228
                                paths [0].Prev ();
 
229
                                locationTreeView.Selection.SelectPath (paths [0]);
 
230
                                locationTreeView.ScrollToCell (paths [0], null, false, 0, 0);
 
231
                        }
 
232
                }
 
233
        }
 
234
}