aboutsummaryrefslogtreecommitdiff
path: root/src/uplanfahrtripview.c
blob: 715724c0a5f962089c84df937daeb3d9642d77c9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
#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
#define COL_DEPARTURE_DELAY_COLOR 7
#define COL_ARRIVAL_DELAY_COLOR 8
#define COL_STATUS_ICON 9
#define COL_STRIKETHROUGH 10

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, const gchar **delay_color)
{
    gchar sign, *str = NULL;

    if (dt) {
        if (delay > 0) {
            sign = '+';
            *delay_color = "red";
        } else if (delay < 0) {
            sign = '-';
            *delay_color = "red";
        } else {
            sign = ' ';
            *delay_color = "green";
        }
        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, *trip;
    LpfTrip *tripobj;
    LpfTripPart *firstpart, *lastpart;
    LpfStop *start, *end;
    GDateTime *arrival, *departure, *rt_departure, *rt_arrival;
    gchar *deptime, *arrtime, *dep_delay, *arr_delay;
    const gchar *dep_delay_color = NULL, *arr_delay_color = NULL,  *icon = NULL;
    gint ddelay, adelay;
    LpfTripStatusFlags flags;
    gboolean strikethrough;

    priv = u_plan_fahr_trip_view_get_instance_private (U_PLAN_FAHR_TRIP_VIEW (self));

    gtk_list_store_clear (priv->liststore);
    for (trip = trips; trip; trip = g_slist_next (trip)) {
        tripobj = trip->data;
        g_object_get(tripobj, "parts", &parts, "status", &flags, 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, &dep_delay_color);
        arr_delay = delay_str (rt_arrival, adelay, &arr_delay_color);

        if (flags & LPF_TRIP_STATUS_FLAGS_CANCELED) {
            icon = "gtk-cancel";
            strikethrough = TRUE;
        } else {
            icon = NULL;
            strikethrough = FALSE;
        }

        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,
                            COL_DEPARTURE_DELAY_COLOR, dep_delay_color,
                            COL_ARRIVAL_DELAY_COLOR, arr_delay_color,
                            COL_STATUS_ICON, icon,
                            COL_STRIKETHROUGH, strikethrough,
                            -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);
    }
    g_slist_free_full (trips, g_object_unref);
}