diff options
Diffstat (limited to 'src/uplanfahrtripview.c')
-rw-r--r-- | src/uplanfahrtripview.c | 149 |
1 files changed, 149 insertions, 0 deletions
diff --git a/src/uplanfahrtripview.c b/src/uplanfahrtripview.c new file mode 100644 index 0000000..d36673a --- /dev/null +++ b/src/uplanfahrtripview.c @@ -0,0 +1,149 @@ +#include <gtk/gtk.h> + +#include <libplanfahr/libplanfahr.h> + +#include "uplanfahr.h" +#include "uplanfahrtripview.h" + +#define COL_START 0 +#define COL_END 1 +#define COL_DEPARTURE_TIME 2 +#define COL_ARRIVAL_TIME 3 +#define COL_PARTS 4 +#define COL_DEPARTURE_DELAY 5 +#define COL_ARRIVAL_DELAY 6 + +struct _UPlanFahrTripView +{ + GtkTreeView parent; +}; + +struct _UPlanFahrTripViewClass +{ + GtkTreeViewClass parent_class; +}; + +typedef struct _UPlanFahrTripViewPrivate UPlanFahrTripViewPrivate; + +struct _UPlanFahrTripViewPrivate +{ + GtkListStore *liststore; +}; + +G_DEFINE_TYPE_WITH_PRIVATE(UPlanFahrTripView, u_plan_fahr_trip_view, GTK_TYPE_TREE_VIEW) + +static void +u_plan_fahr_trip_view_init (UPlanFahrTripView *trip_view) +{ + gtk_widget_init_template (GTK_WIDGET (trip_view)); +} + +static void +u_plan_fahr_trip_view_dispose (GObject *object) +{ + G_OBJECT_CLASS (u_plan_fahr_trip_view_parent_class)->dispose (object); +} + +static void +u_plan_fahr_trip_view_class_init (UPlanFahrTripViewClass *class) +{ + G_OBJECT_CLASS (class)->dispose = u_plan_fahr_trip_view_dispose; + + gtk_widget_class_set_template_from_resource (GTK_WIDGET_CLASS (class), + "/org/sigxcpu/uplanfahr/tripview.ui"); + gtk_widget_class_bind_template_child_private (GTK_WIDGET_CLASS (class), UPlanFahrTripView, liststore); +} + +GtkWidget * +u_plan_fahr_trip_view_new (void) +{ + return g_object_new (U_PLAN_FAHR_TRIP_VIEW_TYPE, NULL); +} + +static gchar * +delay_str (GDateTime *dt, gint delay) +{ + gchar sign, *str = NULL; + + if (dt) { + if (delay > 0) { + sign = '+'; + } else if (delay < 0) { + sign = '-'; + } else { + sign = ' '; + } + str = g_strdup_printf("%c%d", sign, delay); + } + + return str; +} + +void +u_plan_fahr_trip_view_store_trips (UPlanFahrTripView *self, GSList *trips) +{ + UPlanFahrTripViewPrivate *priv; + GtkTreeIter iter; + + GSList *parts; + LpfTrip *tripobj; + LpfTripPart *firstpart, *lastpart; + LpfStop *start, *end; + GDateTime *arrival, *departure, *rt_departure, *rt_arrival; + gchar *deptime, *arrtime, *dep_delay, *arr_delay; + gint ddelay, adelay; + + priv = u_plan_fahr_trip_view_get_instance_private (U_PLAN_FAHR_TRIP_VIEW (self)); + + gtk_list_store_clear (priv->liststore); + for (; trips; trips = g_slist_next (trips)) { + tripobj = trips->data; + g_object_get(tripobj, "parts", &parts, NULL); + + firstpart = LPF_TRIP_PART(parts->data); + lastpart = LPF_TRIP_PART(g_slist_last (parts)->data); + + g_object_get (firstpart, "start", &start, NULL); + g_object_get (start, + "departure", &departure, + "departure_delay", &ddelay, + "rt_departure", &rt_departure, + NULL); + deptime = g_date_time_format (departure, "%H:%M"); + + g_object_get (lastpart, "end", &end, NULL); + g_object_get (end, + "arrival", &arrival, + "arrival_delay", &adelay, + "rt_arrival", &rt_arrival, + NULL); + arrtime = g_date_time_format (arrival, "%H:%M"); + + dep_delay = delay_str (rt_departure, ddelay); + arr_delay = delay_str (rt_arrival, adelay); + + gtk_list_store_append (priv->liststore, &iter); + gtk_list_store_set (priv->liststore, &iter, + COL_START, start, + COL_END, end, + COL_DEPARTURE_TIME, deptime, + COL_ARRIVAL_TIME, arrtime, + COL_PARTS, g_slist_length(parts) - 1, + COL_DEPARTURE_DELAY, dep_delay, + COL_ARRIVAL_DELAY, arr_delay, + -1); + + g_date_time_unref (departure); + g_date_time_unref (arrival); + g_object_unref (start); + g_object_unref (end); + if (rt_departure) + g_date_time_unref (rt_departure); + if (rt_arrival) + g_date_time_unref (rt_arrival); + g_free (deptime); + g_free (arrtime); + g_free (dep_delay); + g_free (arr_delay); + } +} |