Initial commit - version 0.0.1
[beifahrer] / src / query-window.vala
1 /* This file is part of Beifahrer.
2  *
3  * Copyright (C) 2010 Philipp Zabel
4  *
5  * Beifahrer is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * Beifahrer is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with Beifahrer. If not, see <http://www.gnu.org/licenses/>.
17  */
18
19 using Gtk;
20 using Hildon;
21
22 public class QueryWindow : StackableWindow {
23         public const string GCONF_KEY_ARRIVAL = "/apps/beifahrer/arrival";
24         public const string GCONF_KEY_DEPARTURE = "/apps/beifahrer/departure";
25         public const string GCONF_KEY_USE_LOCATION = "/apps/beifahrer/use_location";
26
27         AdacMitfahrclub adac;
28         TouchSelector city_from_selector;
29         TouchSelector city_to_selector;
30         TouchSelector umkreis_from_selector;
31         TouchSelector umkreis_to_selector;
32         DateButton date;
33         TouchSelector tolerance_selector;
34         Location.GPSDControl control;
35         Location.GPSDevice device;
36         GConf.Client gconf;
37         bool use_location = true;
38         bool want_location;
39
40         construct {
41                 set_title ("Beifahrer");
42
43                 var menu = new AppMenu ();
44                 var settings = new Gtk.Button.with_label (_("Settings"));
45                 settings.show ();
46                 menu.append (settings);
47                 set_main_menu (menu);
48
49                 adac = new AdacMitfahrclub ();
50                 gconf = GConf.Client.get_default ();
51
52                 city_from_selector = new TouchSelectorEntry.text ();
53                 city_to_selector = new TouchSelectorEntry.text ();
54                 foreach (unowned City city in adac.get_city_list ()) {
55                         city_from_selector.append_text (city.name);
56                         city_to_selector.append_text (city.name);
57                 }
58
59                 umkreis_from_selector = new TouchSelector.text ();
60                 umkreis_to_selector = new TouchSelector.text ();
61                 for (int km = 0; km <= 50; km += 10) {
62                         umkreis_from_selector.append_text ("%d km".printf (km));
63                         umkreis_to_selector.append_text ("%d km".printf (km));
64                 }
65
66                 tolerance_selector = new TouchSelector.text ();
67                 for (int days = 0; days <= 4; days += 1)
68                         tolerance_selector.append_text (_("+/- %d days").printf (days));
69
70                 var table = new Table (4, 2, false);
71
72                 var button = new PickerButton (SizeType.FINGER_HEIGHT,
73                                                ButtonArrangement.VERTICAL);
74                 button.set_selector (city_from_selector);
75                 button.set_title (_("Departure"));
76                 button.set_value (_("Please select"));
77                 button.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
78                 table.attach (button, 0, 1, 0, 1, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
79
80                 try {
81                         button.set_active (gconf.get_int ("/apps/beifahrer/departure"));
82                 } catch (Error e) {
83                 }
84
85                 button = new PickerButton (SizeType.FINGER_HEIGHT,
86                                            ButtonArrangement.VERTICAL);
87                 button.set_selector (umkreis_from_selector);
88                 button.set_title (_("Vicinity"));
89                 button.set_value ("0 km");
90                 button.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
91                 table.attach (button, 1, 2, 0, 1, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
92
93                 button = new PickerButton (SizeType.FINGER_HEIGHT,
94                                            ButtonArrangement.VERTICAL);
95                 button.set_selector (city_to_selector);
96                 button.set_title (_("Arrival"));
97                 button.set_value (_("Please select"));
98                 button.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
99                 table.attach (button, 0, 1, 1, 2, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
100
101                 try {
102                         button.set_active (gconf.get_int ("/apps/beifahrer/arrival"));
103                 } catch (Error e) {
104                 }
105
106                 button = new PickerButton (SizeType.FINGER_HEIGHT,
107                                            ButtonArrangement.VERTICAL);
108                 button.set_selector (umkreis_to_selector);
109                 button.set_title (_("Vicinity"));
110                 button.set_value ("0 km");
111                 button.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
112                 table.attach (button, 1, 2, 1, 2, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
113
114                 date = new DateButton (SizeType.FINGER_HEIGHT,
115                                        ButtonArrangement.VERTICAL);
116                 date.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
117                 table.attach (date, 0, 1, 2, 3, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
118
119                 button = new PickerButton (SizeType.FINGER_HEIGHT,
120                                            ButtonArrangement.VERTICAL);
121                 button.set_selector (tolerance_selector);
122                 button.set_title (_("Tolerance"));
123                 button.set_value (_("+/- 0 days"));
124                 button.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
125                 table.attach (button, 1, 2, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
126
127                 var search_button = new Gtk.Button.with_label (_("Search"));
128                 Hildon.gtk_widget_set_theme_size (search_button, SizeType.FINGER_HEIGHT);
129                 table.attach (search_button, 0, 2, 3, 4, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
130
131                 add (table);
132
133                 search_button.clicked.connect (on_search_button_clicked);
134                 settings.clicked.connect (on_settings_clicked);
135
136                 show_all ();
137
138                 control = Location.GPSDControl.get_default ();
139                 control.preferred_method = Location.METHOD_CWP | Location.METHOD_ACWP;
140                 control.preferred_interval = Location.GPSDControlInterval.@1S;
141                 control.error.connect (() => { print ("control error\n"); });
142                 control.error_verbose.connect ((e) => {
143                         switch (e) {
144                         case Location.GPSDControlError.USER_REJECTED_DIALOG:
145                                 print ("\tuser rejected dialog\n");
146                                 break;
147                         case Location.GPSDControlError.USER_REJECTED_SETTINGS:
148                                 print ("\tuser rejected settings\n");
149                                 break;
150                         case Location.GPSDControlError.BT_GPS_NOT_AVAILABLE:
151                                 print ("\tbt gps not available\n");
152                                 break;
153                         case Location.GPSDControlError.METHOD_NOT_ALLOWED_IN_OFFLINE_MODE:
154                                 print ("\tmethod not allowed in offline mode\n");
155                                 break;
156                         case Location.GPSDControlError.SYSTEM:
157                                 Banner.show_information (this, null, _("No GPS available!"));
158                                 break;
159                         }
160                 });
161                 control.gpsd_running.connect (() => { print ("control running\n"); });
162                 control.gpsd_stopped.connect (() => { print ("control stopped\n"); });
163
164                 try {
165                         use_location = gconf.get_bool (GCONF_KEY_USE_LOCATION);
166                 } catch (Error e) {
167                 }
168                 want_location = use_location;
169                 if (want_location)
170                         control.start ();
171
172                 device = (Location.GPSDevice) GLib.Object.@new (typeof (Location.GPSDevice), null);
173                 device.changed.connect (on_location_changed);
174                 device.connected.connect (() => { print ("device connected\n"); });
175                 device.disconnected.connect (() => { print ("device disconnected\n"); });
176
177                 // In case the device already has a fix:
178                 on_location_changed ();
179         }
180
181         void on_location_changed () requires (device.fix != null) {
182                 if (!want_location)
183                         return;
184
185                 if (Location.GPS_DEVICE_LATLONG_SET in (int) device.fix.fields) {
186                         print ("fix: lat=%f, lon=%f\n", device.fix.latitude, device.fix.longitude);
187
188                         unowned City city = adac.find_nearest_city (device.fix.latitude, device.fix.longitude);
189                         if (city != null) {
190                                 int n = adac.get_city_list ().index (city);
191                                 int n_from = city_from_selector.get_active (0);
192                                 int n_to = city_to_selector.get_active (0);
193
194                                 print ("city(%d): %s (%d)\n", n, city.name, city.number);
195
196                                 if (n != n_from) {
197                                         city_from_selector.set_active (0, n);
198
199                                         // If we are at the previous point of arrival, prepare for return trip
200                                         if (n == n_to)
201                                                 city_to_selector.set_active (0, n_from);
202                                 }
203                         }
204
205                         control.stop ();
206                         want_location = false;
207                 }
208         }
209
210         void on_search_button_clicked (Gtk.Button button) {
211                 int n;
212
213                 n = city_from_selector.get_active (0);
214                 if (n == -1)
215                         return;
216                 string city_from = adac.get_city_list ().nth_data (n).name;
217                 try {
218                         if (gconf.get_int (GCONF_KEY_DEPARTURE) != n)
219                                 gconf.set_int (GCONF_KEY_DEPARTURE, n);
220                 } catch (Error e) {
221                 }
222
223                 n = city_to_selector.get_active (0);
224                 if (n == -1)
225                         return;
226                 string city_to = adac.get_city_list ().nth_data (n).name;
227                 try {
228                         if (gconf.get_int (GCONF_KEY_ARRIVAL) != n)
229                                 gconf.set_int (GCONF_KEY_ARRIVAL, n);
230                 } catch (Error e) {
231                 }
232
233                 uint year, month, day;
234                 date.get_date (out year, out month, out day);
235                 var date = Date ();
236                 date.set_day ((DateDay) day);
237                 date.set_month ((DateMonth) (month + DateMonth.JANUARY));
238                 date.set_year ((DateYear) year);
239
240                 Hildon.gtk_window_set_progress_indicator (this, 1);
241                 var window = new LiftListWindow (adac);
242                 window.show_all ();
243                 window.find_lifts (city_from, city_to, date);
244                 Hildon.gtk_window_set_progress_indicator (this, 0);
245         }
246
247         void on_settings_clicked () {
248                 var dialog = new SettingsDialog (this);
249                 dialog.response.connect (on_settings_response);
250                 dialog.show ();
251         }
252
253         void on_settings_response (int response_id) {
254                 bool old_use_location = use_location;
255                 use_location = gconf.get_bool (GCONF_KEY_USE_LOCATION);
256                 if (!old_use_location && use_location) {
257                         want_location = true;
258                         control.start ();
259                 }
260         }
261 }