summaryrefslogtreecommitdiffstats
path: root/panels/user-accounts/cc-login-history-dialog.c
blob: fcb7457113fa096602847298076c74367133c1f2 (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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
 *
 * Copyright 2012  Red Hat, Inc,
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses/>.
 *
 * Written by: Ondrej Holy <oholy@redhat.com>
 */

#include "config.h"

#include <unistd.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/wait.h>

#include <adwaita.h>
#include <glib.h>
#include <glib/gi18n.h>
#include <gtk/gtk.h>
#include <act/act.h>

#include "cc-login-history-dialog.h"
#include "cc-user-accounts-resources.h"
#include "cc-util.h"
#include "user-utils.h"

struct _CcLoginHistoryDialog
{
        GtkDialog     parent_instance;

        GtkHeaderBar *header_bar;
        GtkLabel     *title_label;
        GtkListBox   *history_box;
        GtkButton    *next_button;
        GtkButton    *previous_button;

        GDateTime    *week;
        GDateTime    *current_week;

        ActUser      *user;
};

G_DEFINE_TYPE (CcLoginHistoryDialog, cc_login_history_dialog, GTK_TYPE_DIALOG)

typedef struct {
	gint64 login_time;
	gint64 logout_time;
	const gchar *type;
} CcLoginHistory;

static void
show_week_label (CcLoginHistoryDialog *self)
{
        g_autofree gchar *label = NULL;
        GTimeSpan span;

        span = g_date_time_difference (self->current_week, self->week);
        if (span == 0) {
                label = g_strdup (_("This Week"));
        }
        else if (span == G_TIME_SPAN_DAY * 7) {
                label = g_strdup (_("Last Week"));
        }
        else {
                g_autofree gchar *from = NULL;
                g_autofree gchar *to = NULL;
                g_autoptr(GDateTime) date = NULL;

                date = g_date_time_add_days (self->week, 6);
                /* Translators: This is a date format string in the style of "Feb 18",
                   shown as the first day of a week on login history dialog. */
                from = g_date_time_format (self->week, C_("login history week label","%b %e"));
                if (g_date_time_get_year (self->week) == g_date_time_get_year (self->current_week)) {
                        /* Translators: This is a date format string in the style of "Feb 24",
                           shown as the last day of a week on login history dialog. */
                        to = g_date_time_format (date, C_("login history week label","%b %e"));
                }
                else {
                        /* Translators: This is a date format string in the style of "Feb 24, 2013",
                           shown as the last day of a week on login history dialog. */
                        to = g_date_time_format (date, C_("login history week label","%b %e, %Y"));
                }

                /* Translators: This indicates a week label on a login history.
                   The first %s is the first day of a week, and the second %s the last day. */
                label = g_strdup_printf(C_("login history week label", "%s — %s"), from, to);
        }

        gtk_label_set_label (self->title_label, label);
}

static void
clear_history (CcLoginHistoryDialog *self)
{
        GtkWidget *child;

        child = gtk_widget_get_first_child (GTK_WIDGET (self->history_box));
        while (child) {
                GtkWidget *next = gtk_widget_get_next_sibling (child);

                if (ADW_ACTION_ROW (child))
                    gtk_list_box_remove (self->history_box, GTK_WIDGET (child));

                child = next;
        }
}

static GArray *
get_login_history (ActUser *user)
{
	GArray *login_history;
	GVariantIter *iter, *iter2;
	GVariant *variant;
	const GVariant *value;
	const gchar *key;
	CcLoginHistory history;

	login_history = NULL;
	value = act_user_get_login_history (user);
	g_variant_get ((GVariant *) value, "a(xxa{sv})", &iter);
	while (g_variant_iter_loop (iter, "(xxa{sv})", &history.login_time, &history.logout_time, &iter2)) {
		while (g_variant_iter_loop (iter2, "{&sv}", &key, &variant)) {
			if (g_strcmp0 (key, "type") == 0) {
				history.type = g_variant_get_string (variant, NULL);
			}
		}

		if (login_history == NULL) {
			login_history = g_array_new (FALSE, TRUE, sizeof (CcLoginHistory));
		}

		g_array_append_val (login_history, history);
	}

	return login_history;
}

static void
set_sensitivity (CcLoginHistoryDialog *self)
{
        g_autoptr(GArray) login_history = NULL;
        CcLoginHistory history;
        gboolean sensitive = FALSE;

        login_history = get_login_history (self->user);
        if (login_history != NULL) {
                history = g_array_index (login_history, CcLoginHistory, 0);
                sensitive = g_date_time_to_unix (self->week) > history.login_time;
        }
        gtk_widget_set_sensitive (GTK_WIDGET (self->previous_button), sensitive);

        sensitive = (g_date_time_compare (self->current_week, self->week) == 1);
        gtk_widget_set_sensitive (GTK_WIDGET (self->next_button), sensitive);
}

static void
add_record (CcLoginHistoryDialog *self, GDateTime *datetime, gchar *record_string, gint line)
{
        g_autofree gchar *date = NULL;
        g_autofree gchar *time = NULL;
        g_autofree gchar *str = NULL;
        GtkWidget *row;

        date = cc_util_get_smart_date (datetime);
        /* Translators: This is a time format string in the style of "22:58".
           It indicates a login time which follows a date. */
        time = g_date_time_format (datetime, C_("login date-time", "%k:%M"));
        /* Translators: This indicates a login date-time.
           The first %s is a date, and the second %s a time. */
        str = g_strdup_printf(C_("login date-time", "%s, %s"), date, time);

        row = adw_action_row_new ();
        adw_preferences_row_set_title (ADW_PREFERENCES_ROW (row), record_string);
        adw_action_row_set_subtitle (ADW_ACTION_ROW (row), str);

        gtk_list_box_insert (self->history_box, row, line);
}

static void
show_week (CcLoginHistoryDialog *self)
{
        g_autoptr(GArray) login_history = NULL;
        g_autoptr(GDateTime) datetime = NULL;
        g_autoptr(GDateTime) temp = NULL;
        gint64 from, to;
        gint i, line;
        CcLoginHistory history;

        show_week_label (self);
        clear_history (self);
        set_sensitivity (self);

        login_history = get_login_history (self->user);
        if (login_history == NULL) {
                return;
        }

        /* Find first record for week */
        from = g_date_time_to_unix (self->week);
        temp = g_date_time_add_weeks (self->week, 1);
        to = g_date_time_to_unix (temp);
        for (i = login_history->len - 1; i >= 0; i--) {
                history = g_array_index (login_history, CcLoginHistory, i);
                if (history.login_time < to) {
                        break;
                }
        }

        /* Add new session records */
        line = 0;
        for (;i >= 0; i--) {
                history = g_array_index (login_history, CcLoginHistory, i);

                /* Display only x-session and tty records */
                if (!g_str_has_prefix (history.type, ":") &&
                    !g_str_has_prefix (history.type, "tty")) {
                        continue;
                }

                if (history.logout_time > 0 && history.logout_time < from) {
                        break;
                }

                if (history.logout_time > 0 && history.logout_time < to) {
                        datetime = g_date_time_new_from_unix_local (history.logout_time);
                        add_record (self, datetime, _("Session Ended"), line);
                        line++;
                }

                if (history.login_time >= from) {
                        datetime = g_date_time_new_from_unix_local (history.login_time);
                        add_record (self, datetime, _("Session Started"), line);
                        line++;
                }
        }
}

static void
previous_button_clicked_cb (CcLoginHistoryDialog *self)
{
        g_autoptr(GDateTime) temp = NULL;

        temp = self->week;
        self->week = g_date_time_add_weeks (self->week, -1);

        show_week (self);
}

static void
next_button_clicked_cb (CcLoginHistoryDialog *self)
{
        g_autoptr(GDateTime) temp = NULL;

        temp = self->week;
        self->week = g_date_time_add_weeks (self->week, 1);

        show_week (self);
}

static void
cc_login_history_dialog_dispose (GObject *object)
{
        CcLoginHistoryDialog *self = CC_LOGIN_HISTORY_DIALOG (object);

        g_clear_object (&self->user);
        g_clear_pointer (&self->week, g_date_time_unref);
        g_clear_pointer (&self->current_week, g_date_time_unref);

        G_OBJECT_CLASS (cc_login_history_dialog_parent_class)->dispose (object);
}

void
cc_login_history_dialog_class_init (CcLoginHistoryDialogClass *klass)
{
        GObjectClass   *object_class = G_OBJECT_CLASS (klass);
        GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

        object_class->dispose = cc_login_history_dialog_dispose;

        gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/control-center/user-accounts/cc-login-history-dialog.ui");

        gtk_widget_class_bind_template_child (widget_class, CcLoginHistoryDialog, header_bar);
        gtk_widget_class_bind_template_child (widget_class, CcLoginHistoryDialog, title_label);
        gtk_widget_class_bind_template_child (widget_class, CcLoginHistoryDialog, history_box);
        gtk_widget_class_bind_template_child (widget_class, CcLoginHistoryDialog, next_button);
        gtk_widget_class_bind_template_child (widget_class, CcLoginHistoryDialog, previous_button);

        gtk_widget_class_bind_template_callback (widget_class, next_button_clicked_cb);
        gtk_widget_class_bind_template_callback (widget_class, previous_button_clicked_cb);
}

void
cc_login_history_dialog_init (CcLoginHistoryDialog *self)
{
        g_resources_register (cc_user_accounts_get_resource ());

        gtk_widget_init_template (GTK_WIDGET (self));
}

CcLoginHistoryDialog *
cc_login_history_dialog_new (ActUser *user)
{
        CcLoginHistoryDialog *self;
        g_autoptr(GDateTime) temp = NULL;
        g_autoptr(GDateTime) local = NULL;
        g_autofree gchar *title = NULL;

        g_return_val_if_fail (ACT_IS_USER (user), NULL);

        self = g_object_new (CC_TYPE_LOGIN_HISTORY_DIALOG,
                             "use-header-bar", 1,
                             NULL);

        self->user = g_object_ref (user);

        /* Set the first day of this week */
        local = g_date_time_new_now_local ();
        temp = g_date_time_new_local (g_date_time_get_year (local),
                                      g_date_time_get_month (local),
                                      g_date_time_get_day_of_month (local),
                                      0, 0, 0);
        self->week = g_date_time_add_days (temp, 1 - g_date_time_get_day_of_week (temp));
        self->current_week = g_date_time_ref (self->week);

        /* Translators: This is the title of the "Account Activity" dialog.
           The %s is the user real name. */
        title = g_strdup_printf (_("%s — Account Activity"),
                                 act_user_get_real_name (self->user));
        gtk_label_set_label (self->title_label, title);

        show_week (self);

        return self;
}