Switch the layout when the screen geometry changes
[beifahrer] / src / lift-list-window.vala
index 3cacf10..4e82d9a 100644 (file)
@@ -20,11 +20,16 @@ using Gtk;
 using Hildon;
 
 public class LiftListWindow : StackableWindow {
+       public const string GCONF_KEY_PRELOAD = "/apps/beifahrer/preload";
+
        AdacMitfahrclub adac;
        ListStore store;
+       Alignment alignment;
        TreeView tree;
        TreeViewColumn route_column;
        Label no_lifts;
+       GConf.Client gconf;
+       bool preload = false;
 
        public LiftListWindow (AdacMitfahrclub _adac) {
                adac = _adac;
@@ -33,8 +38,14 @@ public class LiftListWindow : StackableWindow {
        construct {
                set_title ("Beifahrer");
 
+               gconf = GConf.Client.get_default ();
                store = new ListStore (6, typeof (string), typeof (string), typeof (string), typeof (string), typeof (string), typeof (Lift));
 
+               alignment = new Alignment (0.0f, 0.0f, 1.0f, 1.0f);
+               alignment.top_padding = MARGIN_HALF;
+               alignment.left_padding = MARGIN_DOUBLE;
+               alignment.right_padding = MARGIN_DOUBLE;
+
                tree = (TreeView) Hildon.gtk_tree_view_new_with_model (UIMode.NORMAL, store);
                Hildon.gtk_widget_set_theme_size (tree, SizeType.FINGER_HEIGHT);
 
@@ -46,7 +57,7 @@ public class LiftListWindow : StackableWindow {
                selection.set_mode (SelectionMode.SINGLE);
 
                // Source and destination column
-               route_column = new TreeViewColumn.with_attributes ("Route", new CellRendererText (), "text", 0);
+               route_column = new TreeViewColumn.with_attributes ("Route", new CellRendererText (), "markup", 0);
                route_column.set_reorderable (false);
                route_column.set_sizing (TreeViewColumnSizing.AUTOSIZE);
                route_column.set_expand (true);
@@ -93,38 +104,63 @@ public class LiftListWindow : StackableWindow {
                vbox.pack_start (pannable, true, true, 0);
                vbox.pack_start (no_lifts, false, false, 0);
 
-               add (vbox);
+               alignment.add (vbox);
+               add (alignment);
 
-               vbox.show_all ();
+               alignment.show_all ();
                no_lifts.hide ();
                route_column.set_visible (!BeifahrerProgram.orientation.portrait);
 
                tree.row_activated.connect (this.on_row_activated);
-               BeifahrerProgram.orientation.changed.connect (this.on_orientation_changed);
+               Gdk.Screen.get_default ().size_changed.connect (this.on_orientation_changed);
        }
 
-       public async void find_lifts (string city_from, string city_to, Date date, int tolerance = 0) {
+       public async void find_lifts (string city_from, int radius_from, string city_to, int radius_to, Date date, int tolerance = 0) {
+               TreeIter iter;
                set_title ("%s - %s".printf (city_from, city_to));
                Hildon.gtk_window_set_progress_indicator (this, 1);
 
-               var lift_list = yield adac.get_lift_list (city_from, city_to, date, tolerance);
+               var lift_list = yield adac.get_lift_list (city_from, radius_from, city_to, radius_to, date, tolerance);
                foreach (Lift lift in lift_list) {
-                       TreeIter iter;
                        string icon_name = null;
                        if (LiftFlags.SMOKER in lift.flags)
                                icon_name = "beifahrer_smoker";
                        else if (LiftFlags.NON_SMOKER in lift.flags)
                                icon_name = "beifahrer_non_smoker";
+                       string datetime = "%02d.%02d.%04d".printf (lift.time.day, lift.time.month, lift.time.year);
+                       if (lift.time.hour >= 0)
+                               datetime += ", %d:%02d".printf (lift.time.hour, lift.time.minute);
                        store.insert_with_values (out iter, -1, 0, lift.city_from + " - " + lift.city_to,
-                                                               1, (lift.time != null) ? (lift.date + ", " + lift.time) : lift.date,
+                                                               1, datetime,
                                                                2, _("%d pl.").printf (lift.places),
                                                                3, lift.price,
                                                                4, icon_name,
                                                                5, lift);
                        // (LiftFlags.WOMEN_ONLY,ADAC_MEMBER in lift.flags)
                }
+               if (lift_list.length () > 6)
+                       alignment.right_padding = MARGIN_DEFAULT;
                if (lift_list.length () == 0)
                        no_lifts.show ();
+
+               bool preload = false;
+               try {
+                       preload = gconf.get_bool (GCONF_KEY_PRELOAD);
+               } catch (Error e) {
+               }
+               if (preload) {
+                       if (store.get_iter_first (out iter)) do {
+                               Lift lift2;
+                               store.@get (iter, 5, out lift2);
+                               if (lift2 == null)
+                                       continue;
+                               if (lift2.description == null) {
+                                       yield adac.update_lift_details (lift2);
+                                       store.@set (iter, 0, lift2.city_from + " - " + lift2.city_to + "\n<small>" + lift2.name + "</small>");
+                               }
+                       } while (store.iter_next (ref iter));
+               }
+
                Hildon.gtk_window_set_progress_indicator (this, 0);
        }
 
@@ -137,11 +173,11 @@ public class LiftListWindow : StackableWindow {
                        model.get (iter, 5, out lift);
 
                        var window = new LiftDetailWindow (adac, lift);
-                       window.show_all ();
+                       window.show ();
                }
        }
 
-       private void on_orientation_changed () {
+       private void on_orientation_changed (Gdk.Screen screen) {
                route_column.set_visible (!BeifahrerProgram.orientation.portrait);
        }
 }