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