2
// Copyright (C) 2009 Robert Dyer
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.
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.
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/>.
19
using System.Collections.Generic;
20
using System.Threading;
24
namespace WeatherDocklet
26
[System.ComponentModel.Category("File")]
27
[System.ComponentModel.ToolboxItem(true)]
28
public partial class WeatherConfiguration : Bin
30
TreeStore locationTreeStore = new TreeStore (typeof (string));
31
TreeStore searchTreeStore = new TreeStore (typeof (string), typeof (string));
33
public WeatherConfiguration ()
37
locationTreeView.Model = locationTreeStore;
38
locationTreeView.Selection.Changed += OnLocationSelectionChanged;
39
locationTreeView.AppendColumn ("Locations", new CellRendererText (), "text", 0);
42
searchTreeView.Model = searchTreeStore;
43
searchTreeView.Selection.Changed += OnSearchSelectionChanged;
44
searchTreeView.AppendColumn ("Search Results", new CellRendererText (), "text", 0);
46
timeout.Value = WeatherPreferences.Timeout;
47
metric.Active = WeatherPreferences.Metric;
48
aboutLabel.Text = WeatherController.Weather.About;
51
foreach (AbstractWeatherSource aws in WeatherController.Service.WeatherSources)
53
source.AppendText (aws.Name);
54
if (aws.Name.Equals(WeatherPreferences.Source))
55
source.Active = selected;
60
void UpdateLocations ()
62
locationTreeStore.Clear ();
63
for (int i = 0; i < WeatherPreferences.Location.Length; i++)
64
locationTreeStore.AppendValues (WeatherPreferences.Location [i]);
67
protected virtual void OnTimeoutFocusOut (object o, Gtk.FocusOutEventArgs args)
69
WeatherPreferences.Timeout = (uint) timeout.ValueAsInt;
72
protected virtual void OnMetricFocusOut (object o, Gtk.FocusOutEventArgs args)
74
WeatherPreferences.Metric = metric.Active;
77
protected virtual void OnSourceChanged (object sender, System.EventArgs e)
79
WeatherPreferences.Source = source.ActiveText;
80
aboutLabel.Text = WeatherController.Weather.About;
81
searchTreeStore.Clear ();
84
protected virtual void OnSearchClicked (object sender, System.EventArgs e)
86
searchTreeStore.Clear ();
87
searchTreeStore.AppendValues ("Currently searching...", "");
91
List<string> vals = new List<string> ();
93
foreach (string s in WeatherController.Weather.SearchLocation (location.Text))
96
Gtk.Application.Invoke (delegate {
97
searchTreeStore.Clear ();
100
for (int i = 0; i < vals.Count; i += 2)
101
searchTreeStore.AppendValues (vals [i], vals [i + 1]);
103
searchTreeStore.AppendValues ("No results found", "");
109
protected virtual void OnLocationSelectionChanged (object o, System.EventArgs args)
114
if (((TreeSelection)o).GetSelected (out model, out iter))
116
string selected = (string) model.GetValue (iter, 0);
117
int index = FindLocationIndex (selected);
120
location.Text = selected;
121
btnMoveUp.Sensitive = index > 0;
122
btnMoveDown.Sensitive = index < WeatherPreferences.Location.Length - 1;
123
btnRemove.Sensitive = true;
128
btnMoveUp.Sensitive = false;
129
btnMoveDown.Sensitive = false;
130
btnRemove.Sensitive = false;
133
protected virtual void OnSearchSelectionChanged (object o, System.EventArgs args)
138
if (((TreeSelection)o).GetSelected (out model, out iter))
140
string val = (string) model.GetValue (iter, 1);
145
protected virtual void OnAddClicked (object sender, System.EventArgs e)
147
if (location.Text.Trim ().Length == 0 || FindLocationIndex (location.Text) != -1)
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 () });
157
int FindLocationIndex (string location)
159
for (int i = 0; i < WeatherPreferences.Location.Length; i++)
160
if (WeatherPreferences.Location [i].Equals (location))
166
protected virtual void OnRemoveClicked (object sender, System.EventArgs e)
171
if (locationTreeView.Selection.GetSelected (out model, out iter))
173
string removedLocation = (string) model.GetValue (iter, 0);
174
int index = FindLocationIndex (removedLocation);
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);
180
WeatherPreferences.Location = locations;
181
locationTreeStore.Remove (ref iter);
183
if (removedLocation.Equals (WeatherController.CurrentLocation))
184
WeatherController.PreviousLocation ();
188
protected virtual void OnMoveDownClicked (object sender, System.EventArgs e)
193
if (locationTreeView.Selection.GetSelected (out model, out iter))
195
TreePath[] paths = locationTreeView.Selection.GetSelectedRows ();
196
int index = FindLocationIndex ((string) model.GetValue (iter, 0));
198
string[] locations = WeatherPreferences.Location;
199
string temp = locations [index];
200
locations [index] = locations [index + 1];
201
locations [index + 1] = temp;
203
WeatherPreferences.Location = locations;
206
locationTreeView.Selection.SelectPath (paths [0]);
207
locationTreeView.ScrollToCell (paths [0], null, false, 0, 0);
211
protected virtual void OnMoveUpClicked (object sender, System.EventArgs e)
216
if (locationTreeView.Selection.GetSelected (out model, out iter))
218
TreePath[] paths = locationTreeView.Selection.GetSelectedRows ();
219
int index = FindLocationIndex ((string) model.GetValue (iter, 0));
221
string[] locations = WeatherPreferences.Location;
222
string temp = locations [index];
223
locations [index] = locations [index - 1];
224
locations [index - 1] = temp;
226
WeatherPreferences.Location = locations;
229
locationTreeView.Selection.SelectPath (paths [0]);
230
locationTreeView.ScrollToCell (paths [0], null, false, 0, 0);