Debian packaging: 0.0.3-2
[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         CityButton departure_button;
29         CityButton arrival_button;
30         DateButton date;
31         TouchSelector tolerance_selector;
32         Location.GPSDControl control;
33         Location.GPSDevice device;
34         GConf.Client gconf;
35         bool use_location = true;
36         bool want_location;
37
38         construct {
39                 set_title ("Beifahrer");
40
41                 var menu = new AppMenu ();
42                 var settings = new Gtk.Button.with_label (_("Settings"));
43                 settings.show ();
44                 menu.append (settings);
45                 set_main_menu (menu);
46
47                 adac = new AdacMitfahrclub ();
48                 gconf = GConf.Client.get_default ();
49
50                 tolerance_selector = new TouchSelector.text ();
51                 for (int days = 0; days <= 4; days += 1)
52                         tolerance_selector.append_text (_("+/- %d days").printf (days));
53
54                 var alignment = new Alignment (0.0f, 0.0f, 1.0f, 1.0f);
55                 alignment.top_padding = MARGIN_HALF;
56                 alignment.left_padding = MARGIN_DOUBLE;
57                 alignment.right_padding = MARGIN_DOUBLE;
58                 var table = new Table (5, 2, false);
59
60                 departure_button = new CityButton (SizeType.FINGER_HEIGHT,
61                                                    ButtonArrangement.VERTICAL, adac.get_city_list ());
62                 departure_button.set_title (_("Departure"));
63                 departure_button.set_value (_("Please select"));
64                 departure_button.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
65                 table.attach (departure_button, 0, 2, 0, 1, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
66
67                 try {
68                         departure_button.set_active (gconf.get_int ("/apps/beifahrer/departure"));
69                 } catch (Error e) {
70                 }
71
72                 arrival_button = new CityButton (SizeType.FINGER_HEIGHT,
73                                                  ButtonArrangement.VERTICAL, adac.get_city_list ());
74                 arrival_button.set_title (_("Arrival"));
75                 arrival_button.set_value (_("Please select"));
76                 arrival_button.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
77                 table.attach (arrival_button, 0, 2, 1, 2, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
78
79                 try {
80                         arrival_button.set_active (gconf.get_int ("/apps/beifahrer/arrival"));
81                 } catch (Error e) {
82                 }
83
84                 var switch_button = new Gtk.Button.with_label (_("Switch departure and arrival"));
85                 Hildon.gtk_widget_set_theme_size (switch_button, SizeType.FINGER_HEIGHT);
86                 switch_button.set_alignment (0.0f, 0.5f);
87                 table.attach (switch_button, 0, 2, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
88
89                 date = new DateButton (SizeType.FINGER_HEIGHT,
90                                        ButtonArrangement.VERTICAL);
91                 date.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
92                 table.attach (date, 0, 1, 3, 4, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
93
94                 var button = new PickerButton (SizeType.FINGER_HEIGHT,
95                                            ButtonArrangement.VERTICAL);
96                 button.set_selector (tolerance_selector);
97                 button.set_title (_("Tolerance"));
98                 button.set_value (_("+/- 0 days"));
99                 button.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
100                 table.attach (button, 1, 2, 3, 4, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
101
102                 var search_button = new Gtk.Button.with_label (_("Search"));
103                 Hildon.gtk_widget_set_theme_size (search_button, SizeType.FINGER_HEIGHT);
104                 table.attach (search_button, 0, 2, 4, 5, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
105
106                 alignment.add (table);
107                 add (alignment);
108
109                 switch_button.clicked.connect (on_switch_button_clicked);
110                 search_button.clicked.connect (on_search_button_clicked);
111                 settings.clicked.connect (on_settings_clicked);
112
113                 show_all ();
114
115                 control = Location.GPSDControl.get_default ();
116                 control.preferred_method = Location.METHOD_ACWP;
117                 control.preferred_interval = Location.GPSDControlInterval.@1S;
118                 control.error.connect (() => { print ("control error\n"); });
119                 control.error_verbose.connect ((e) => {
120                         switch (e) {
121                         case Location.GPSDControlError.USER_REJECTED_DIALOG:
122                                 print ("\tuser rejected dialog\n");
123                                 break;
124                         case Location.GPSDControlError.USER_REJECTED_SETTINGS:
125                                 print ("\tuser rejected settings\n");
126                                 break;
127                         case Location.GPSDControlError.BT_GPS_NOT_AVAILABLE:
128                                 print ("\tbt gps not available\n");
129                                 break;
130                         case Location.GPSDControlError.METHOD_NOT_ALLOWED_IN_OFFLINE_MODE:
131                                 print ("\tmethod not allowed in offline mode\n");
132                                 break;
133                         case Location.GPSDControlError.SYSTEM:
134                                 Banner.show_information (this, null, _("No GPS available!"));
135                                 break;
136                         }
137                 });
138                 control.gpsd_running.connect (() => { print ("control running\n"); });
139                 control.gpsd_stopped.connect (() => { print ("control stopped\n"); });
140
141                 try {
142                         use_location = gconf.get_bool (GCONF_KEY_USE_LOCATION);
143                 } catch (Error e) {
144                 }
145                 want_location = use_location;
146                 if (want_location)
147                         control.start ();
148
149                 device = (Location.GPSDevice) GLib.Object.@new (typeof (Location.GPSDevice), null);
150                 device.changed.connect (on_location_changed);
151                 device.connected.connect (() => { print ("device connected\n"); });
152                 device.disconnected.connect (() => { print ("device disconnected\n"); });
153
154                 // In case the device already has a fix:
155                 on_location_changed ();
156         }
157
158         void on_location_changed () requires (device.fix != null) {
159                 if (!want_location || device.status == Location.GPSDeviceStatus.NO_FIX)
160                         return;
161
162                 if (Location.GPS_DEVICE_LATLONG_SET in (int) device.fix.fields) {
163                         print ("fix: lat=%f, lon=%f\n", device.fix.latitude, device.fix.longitude);
164
165                         unowned City city = adac.find_nearest_city (device.fix.latitude, device.fix.longitude);
166                         if (city != null) {
167                                 int n = adac.get_city_list ().index (city);
168                                 int n_from = departure_button.get_active ();
169                                 int n_to = arrival_button.get_active ();
170
171                                 print ("city(%d): %s (%d)\n", n, city.name, city.number);
172
173                                 if (n != n_from) {
174                                         departure_button.set_active (n);
175
176                                         // If we are at the previous point of arrival, prepare for return trip
177                                         if (n == n_to)
178                                                 arrival_button.set_active (n_from);
179                                 }
180                         }
181
182                         control.stop ();
183                         want_location = false;
184                 }
185         }
186
187         void on_search_button_clicked (Gtk.Button button) {
188                 int n;
189
190                 n = departure_button.get_active ();
191                 if (n == -1)
192                         return;
193                 string city_from = adac.get_city_list ().nth_data (n).name;
194                 try {
195                         if (gconf.get_int (GCONF_KEY_DEPARTURE) != n)
196                                 gconf.set_int (GCONF_KEY_DEPARTURE, n);
197                 } catch (Error e) {
198                 }
199
200                 n = arrival_button.get_active ();
201                 if (n == -1)
202                         return;
203                 string city_to = adac.get_city_list ().nth_data (n).name;
204                 try {
205                         if (gconf.get_int (GCONF_KEY_ARRIVAL) != n)
206                                 gconf.set_int (GCONF_KEY_ARRIVAL, n);
207                 } catch (Error e) {
208                 }
209
210                 uint year, month, day;
211                 date.get_date (out year, out month, out day);
212                 var date = Date ();
213                 date.set_day ((DateDay) day);
214                 date.set_month ((DateMonth) (month + DateMonth.JANUARY));
215                 date.set_year ((DateYear) year);
216
217                 int tolerance = tolerance_selector.get_active (0);
218
219                 var window = new LiftListWindow (adac);
220                 window.show ();
221                 window.find_lifts.begin (city_from, departure_button.get_radius (), city_to, arrival_button.get_radius (), date, tolerance);
222         }
223
224         // Switch departure and arrival
225         void on_switch_button_clicked () {
226                 int n = departure_button.get_active ();
227                 departure_button.set_active (arrival_button.get_active ());
228                 arrival_button.set_active (n);
229                 n = departure_button.get_radius ();
230                 departure_button.set_radius (arrival_button.get_radius ());
231                 arrival_button.set_radius (n);
232         }
233
234         void on_settings_clicked () {
235                 var dialog = new SettingsDialog (this);
236                 dialog.response.connect (on_settings_response);
237                 dialog.show ();
238         }
239
240         void on_settings_response (int response_id) {
241                 bool old_use_location = use_location;
242                 use_location = gconf.get_bool (GCONF_KEY_USE_LOCATION);
243                 if (!old_use_location && use_location) {
244                         want_location = true;
245                         control.start ();
246                 }
247         }
248 }