1 /* This file is part of Beifahrer.
3 * Copyright (C) 2010 Philipp Zabel
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.
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.
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/>.
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";
28 TouchSelector city_from_selector;
29 TouchSelector city_to_selector;
30 TouchSelector umkreis_from_selector;
31 TouchSelector umkreis_to_selector;
33 TouchSelector tolerance_selector;
34 Location.GPSDControl control;
35 Location.GPSDevice device;
37 bool use_location = true;
41 set_title ("Beifahrer");
43 var menu = new AppMenu ();
44 var settings = new Gtk.Button.with_label (_("Settings"));
46 menu.append (settings);
49 adac = new AdacMitfahrclub ();
50 gconf = GConf.Client.get_default ();
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);
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));
66 tolerance_selector = new TouchSelector.text ();
67 for (int days = 0; days <= 4; days += 1)
68 tolerance_selector.append_text (_("+/- %d days").printf (days));
70 var table = new Table (5, 2, false);
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);
81 button.set_active (gconf.get_int ("/apps/beifahrer/departure"));
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);
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);
102 button.set_active (gconf.get_int ("/apps/beifahrer/arrival"));
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);
114 var switch_button = new Gtk.Button.with_label (_("Switch departure and arrival"));
115 Hildon.gtk_widget_set_theme_size (switch_button, SizeType.FINGER_HEIGHT);
116 switch_button.set_alignment (0.0f, 0.5f);
117 table.attach (switch_button, 0, 1, 2, 3, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
119 date = new DateButton (SizeType.FINGER_HEIGHT,
120 ButtonArrangement.VERTICAL);
121 date.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
122 table.attach (date, 0, 1, 3, 4, AttachOptions.FILL | AttachOptions.EXPAND, AttachOptions.FILL, 0, 0);
124 button = new PickerButton (SizeType.FINGER_HEIGHT,
125 ButtonArrangement.VERTICAL);
126 button.set_selector (tolerance_selector);
127 button.set_title (_("Tolerance"));
128 button.set_value (_("+/- 0 days"));
129 button.set_alignment (0.0f, 0.0f, 0.5f, 0.5f);
130 table.attach (button, 1, 2, 3, 4, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
132 var search_button = new Gtk.Button.with_label (_("Search"));
133 Hildon.gtk_widget_set_theme_size (search_button, SizeType.FINGER_HEIGHT);
134 table.attach (search_button, 0, 2, 4, 5, AttachOptions.FILL, AttachOptions.FILL, 0, 0);
138 switch_button.clicked.connect (on_switch_button_clicked);
139 search_button.clicked.connect (on_search_button_clicked);
140 settings.clicked.connect (on_settings_clicked);
144 control = Location.GPSDControl.get_default ();
145 control.preferred_method = Location.METHOD_ACWP;
146 control.preferred_interval = Location.GPSDControlInterval.@1S;
147 control.error.connect (() => { print ("control error\n"); });
148 control.error_verbose.connect ((e) => {
150 case Location.GPSDControlError.USER_REJECTED_DIALOG:
151 print ("\tuser rejected dialog\n");
153 case Location.GPSDControlError.USER_REJECTED_SETTINGS:
154 print ("\tuser rejected settings\n");
156 case Location.GPSDControlError.BT_GPS_NOT_AVAILABLE:
157 print ("\tbt gps not available\n");
159 case Location.GPSDControlError.METHOD_NOT_ALLOWED_IN_OFFLINE_MODE:
160 print ("\tmethod not allowed in offline mode\n");
162 case Location.GPSDControlError.SYSTEM:
163 Banner.show_information (this, null, _("No GPS available!"));
167 control.gpsd_running.connect (() => { print ("control running\n"); });
168 control.gpsd_stopped.connect (() => { print ("control stopped\n"); });
171 use_location = gconf.get_bool (GCONF_KEY_USE_LOCATION);
174 want_location = use_location;
178 device = (Location.GPSDevice) GLib.Object.@new (typeof (Location.GPSDevice), null);
179 device.changed.connect (on_location_changed);
180 device.connected.connect (() => { print ("device connected\n"); });
181 device.disconnected.connect (() => { print ("device disconnected\n"); });
183 // In case the device already has a fix:
184 on_location_changed ();
187 void on_location_changed () requires (device.fix != null) {
188 if (!want_location || device.status == Location.GPSDeviceStatus.NO_FIX)
191 if (Location.GPS_DEVICE_LATLONG_SET in (int) device.fix.fields) {
192 print ("fix: lat=%f, lon=%f\n", device.fix.latitude, device.fix.longitude);
194 unowned City city = adac.find_nearest_city (device.fix.latitude, device.fix.longitude);
196 int n = adac.get_city_list ().index (city);
197 int n_from = city_from_selector.get_active (0);
198 int n_to = city_to_selector.get_active (0);
200 print ("city(%d): %s (%d)\n", n, city.name, city.number);
203 city_from_selector.set_active (0, n);
205 // If we are at the previous point of arrival, prepare for return trip
207 city_to_selector.set_active (0, n_from);
212 want_location = false;
216 void on_search_button_clicked (Gtk.Button button) {
219 n = city_from_selector.get_active (0);
222 string city_from = adac.get_city_list ().nth_data (n).name;
224 if (gconf.get_int (GCONF_KEY_DEPARTURE) != n)
225 gconf.set_int (GCONF_KEY_DEPARTURE, n);
229 n = city_to_selector.get_active (0);
232 string city_to = adac.get_city_list ().nth_data (n).name;
234 if (gconf.get_int (GCONF_KEY_ARRIVAL) != n)
235 gconf.set_int (GCONF_KEY_ARRIVAL, n);
239 uint year, month, day;
240 date.get_date (out year, out month, out day);
242 date.set_day ((DateDay) day);
243 date.set_month ((DateMonth) (month + DateMonth.JANUARY));
244 date.set_year ((DateYear) year);
246 int tolerance = tolerance_selector.get_active (0);
248 var window = new LiftListWindow (adac);
250 window.find_lifts.begin (city_from, city_to, date, tolerance);
253 // Switch departure and arrival
254 void on_switch_button_clicked () {
255 int n = city_from_selector.get_active (0);
256 city_from_selector.set_active (0, city_to_selector.get_active (0));
257 city_to_selector.set_active (0, n);
258 n = umkreis_from_selector.get_active (0);
259 umkreis_from_selector.set_active (0, umkreis_to_selector.get_active (0));
260 umkreis_to_selector.set_active (0, n);
263 void on_settings_clicked () {
264 var dialog = new SettingsDialog (this);
265 dialog.response.connect (on_settings_response);
269 void on_settings_response (int response_id) {
270 bool old_use_location = use_location;
271 use_location = gconf.get_bool (GCONF_KEY_USE_LOCATION);
272 if (!old_use_location && use_location) {
273 want_location = true;