summaryrefslogtreecommitdiffstats
path: root/panels/common/cc-time-editor.c
blob: 572fc85f03b775f1f108d7a2a4963ca8aa570c90 (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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
/* -*- mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*- */
/* cc-time-editor.c
 *
 * Copyright 2020 Purism SPC
 *
 * 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/>.
 *
 * Author(s):
 *   Mohammed Sadiq <sadiq@sadiqpk.org>
 *
 * SPDX-License-Identifier: GPL-3.0-or-later
 */

#undef G_LOG_DOMAIN
#define G_LOG_DOMAIN "cc-time-editor"

#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

#define GNOME_DESKTOP_USE_UNSTABLE_API
#include <libgnome-desktop/gnome-wall-clock.h>
#include <gtk/gtk.h>
#include <glib/gi18n.h>

#include "cc-time-entry.h"
#include "cc-time-editor.h"


#define TIMEOUT_INITIAL  500
#define TIMEOUT_REPEAT    50

#define FILECHOOSER_SCHEMA "org.gtk.Settings.FileChooser"
#define CLOCK_SCHEMA       "org.gnome.desktop.interface"
#define CLOCK_FORMAT_KEY   "clock-format"
#define SECONDS_PER_MINUTE  (60)
#define SECONDS_PER_HOUR    (60 * 60)
#define SECONDS_PER_DAY     (60 * 60 * 24)


struct _CcTimeEditor
{
  AdwBin     parent_instance;

  GtkButton *am_pm_button;
  GtkStack  *am_pm_stack;
  GtkLabel  *am_label;
  GtkLabel  *pm_label;
  GtkButton *hour_up_button;
  GtkButton *hour_down_button;
  GtkButton *minute_up_button;
  GtkButton *minute_down_button;
  CcTimeEntry *time_entry;

  GtkButton *clicked_button; /* The button currently being clicked */
  GSettings *clock_settings;
  GSettings *filechooser_settings;

  guint      timer_id;
};

G_DEFINE_TYPE (CcTimeEditor, cc_time_editor, ADW_TYPE_BIN)


enum {
  TIME_CHANGED,
  N_SIGNALS
};

static guint signals[N_SIGNALS];

static void
time_editor_clock_changed_cb (CcTimeEditor *self)
{
  GDesktopClockFormat value;
  gboolean is_am_pm;

  g_assert (CC_IS_TIME_EDITOR (self));

  value = g_settings_get_enum (self->clock_settings, CLOCK_FORMAT_KEY);

  is_am_pm = value == G_DESKTOP_CLOCK_FORMAT_12H;
  cc_time_entry_set_am_pm (self->time_entry, is_am_pm);
  gtk_widget_set_visible (GTK_WIDGET (self->am_pm_button), is_am_pm);

  if (is_am_pm)
    {
      if (cc_time_entry_get_is_am (self->time_entry))
        gtk_stack_set_visible_child (self->am_pm_stack, GTK_WIDGET (self->am_label));
      else
        gtk_stack_set_visible_child (self->am_pm_stack, GTK_WIDGET (self->pm_label));
    }
}

static void
time_editor_time_changed_cb (CcTimeEditor *self)
{
  g_assert (CC_IS_TIME_EDITOR (self));

  time_editor_clock_changed_cb (self);
  g_signal_emit (self, signals[TIME_CHANGED], 0);
}

static void
editor_change_time_clicked_cb (CcTimeEditor *self,
                               GtkButton    *button)
{
  g_assert (CC_IS_TIME_EDITOR (self));

  if (button == NULL)
    return;

  if (button == self->hour_up_button)
    {
      gtk_editable_set_position (GTK_EDITABLE (self->time_entry), 0);
      g_signal_emit_by_name (self->time_entry, "change-value", GTK_SCROLL_STEP_UP);
    }
  else if (button == self->hour_down_button)
    {
      gtk_editable_set_position (GTK_EDITABLE (self->time_entry), 0);
      g_signal_emit_by_name (self->time_entry, "change-value", GTK_SCROLL_STEP_DOWN);
    }
  else if (button == self->minute_up_button)
    {
      gtk_editable_set_position (GTK_EDITABLE (self->time_entry), 3);
      g_signal_emit_by_name (self->time_entry, "change-value", GTK_SCROLL_STEP_UP);
    }
  else if (button == self->minute_down_button)
    {
      gtk_editable_set_position (GTK_EDITABLE (self->time_entry), 3);
      g_signal_emit_by_name (self->time_entry, "change-value", GTK_SCROLL_STEP_DOWN);
    }
}

static gboolean
editor_change_time_repeat (CcTimeEditor *self)
{
  if (self->clicked_button == NULL)
    {
      self->timer_id = 0;

      return G_SOURCE_REMOVE;
    }

  editor_change_time_clicked_cb (self, self->clicked_button);

  return G_SOURCE_CONTINUE;
}

static gboolean
editor_change_time_cb (CcTimeEditor *self)
{
  g_assert (CC_IS_TIME_EDITOR (self));
  g_clear_handle_id (&self->timer_id, g_source_remove);

  editor_change_time_clicked_cb (self, self->clicked_button);
  self->timer_id = g_timeout_add (TIMEOUT_REPEAT,
                                  (GSourceFunc)editor_change_time_repeat,
                                  self);
  return G_SOURCE_REMOVE;
}

static gboolean
editor_change_time_pressed_cb (CcTimeEditor    *self,
                               gint             n_press,
                               gdouble          x,
                               gdouble          y,
                               GtkGestureClick *click_gesture)
{
  GtkWidget *button;

  g_assert (CC_IS_TIME_EDITOR (self));

  button = gtk_event_controller_get_widget (GTK_EVENT_CONTROLLER (click_gesture));

  self->clicked_button = GTK_BUTTON (button);
  /* Keep changing time until the press is released */
  self->timer_id = g_timeout_add (TIMEOUT_INITIAL,
                                  (GSourceFunc)editor_change_time_cb,
                                  self);
  editor_change_time_clicked_cb (self, GTK_BUTTON (button));
  return FALSE;
}

static gboolean
editor_change_time_released_cb (CcTimeEditor *self)
{
  self->clicked_button = NULL;
  g_clear_handle_id (&self->timer_id, g_source_remove);

  return FALSE;
}

static void
editor_am_pm_button_clicked_cb (CcTimeEditor *self)
{
  gboolean is_am;

  g_assert (CC_IS_TIME_EDITOR (self));
  g_assert (cc_time_entry_get_am_pm (self->time_entry));

  is_am = cc_time_entry_get_is_am (self->time_entry);
  /* Toggle AM PM */
  cc_time_entry_set_is_am (self->time_entry, !is_am);
  time_editor_clock_changed_cb (self);
}

static void
editor_am_pm_stack_changed_cb (CcTimeEditor *self)
{
  GtkWidget *label;
  const gchar *text;

  g_assert (CC_IS_TIME_EDITOR (self));

  label = gtk_stack_get_visible_child (self->am_pm_stack);
  text = gtk_label_get_text (GTK_LABEL (label));
  gtk_accessible_update_property (GTK_ACCESSIBLE (self->am_pm_button),
                                  GTK_ACCESSIBLE_PROPERTY_LABEL, text,
                                  -1);
}

static void
cc_time_editor_constructed (GObject *object)
{
  CcTimeEditor *self = (CcTimeEditor *)object;
  GDateTime *date;
  char *label;

  G_OBJECT_CLASS (cc_time_editor_parent_class)->constructed (object);

  /* Set localized identifier for AM */
  date = g_date_time_new_utc (1, 1, 1, 0, 0, 0);
  label = g_date_time_format (date, "%p");
  gtk_label_set_label (self->am_label, label);
  g_date_time_unref (date);
  g_free (label);

  /* Set localized identifier for PM */
  date = g_date_time_new_utc (1, 1, 1, 12, 0, 0);
  label = g_date_time_format (date, "%p");
  gtk_label_set_label (self->pm_label, label);
  g_date_time_unref (date);
  g_free (label);
}

static void
cc_time_editor_finalize (GObject *object)
{
  CcTimeEditor *self = (CcTimeEditor *)object;

  g_clear_handle_id (&self->timer_id, g_source_remove);
  g_clear_object (&self->clock_settings);
  g_clear_object (&self->filechooser_settings);

  G_OBJECT_CLASS (cc_time_editor_parent_class)->finalize (object);
}

static void
cc_time_editor_class_init (CcTimeEditorClass *klass)
{
  GObjectClass   *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  object_class->constructed = cc_time_editor_constructed;
  object_class->finalize = cc_time_editor_finalize;

  signals[TIME_CHANGED] =
    g_signal_new ("time-changed",
                  G_TYPE_FROM_CLASS (klass),
                  G_SIGNAL_RUN_FIRST,
                  0, NULL, NULL,
                  NULL,
                  G_TYPE_NONE, 0);

  gtk_widget_class_set_template_from_resource (widget_class,
                                               "/org/gnome/control-center/"
                                               "common/cc-time-editor.ui");

  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, am_pm_button);
  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, am_pm_stack);
  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, am_label);
  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, pm_label);
  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, hour_up_button);
  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, hour_down_button);
  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, minute_up_button);
  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, minute_down_button);
  gtk_widget_class_bind_template_child (widget_class, CcTimeEditor, time_entry);

  gtk_widget_class_bind_template_callback (widget_class, editor_change_time_pressed_cb);
  gtk_widget_class_bind_template_callback (widget_class, editor_change_time_released_cb);
  gtk_widget_class_bind_template_callback (widget_class, editor_am_pm_button_clicked_cb);
  gtk_widget_class_bind_template_callback (widget_class, editor_am_pm_stack_changed_cb);

  g_type_ensure (CC_TYPE_TIME_ENTRY);
}

static void
cc_time_editor_init (CcTimeEditor *self)
{
  gtk_widget_init_template (GTK_WIDGET (self));

  self->clock_settings = g_settings_new (CLOCK_SCHEMA);
  self->filechooser_settings = g_settings_new (FILECHOOSER_SCHEMA);

  g_signal_connect_object (self->clock_settings, "changed::" CLOCK_FORMAT_KEY,
                           G_CALLBACK (time_editor_clock_changed_cb), self,
                           G_CONNECT_SWAPPED);
  g_signal_connect_swapped (self->time_entry, "time-changed",
                            G_CALLBACK (time_editor_time_changed_cb), self);
  time_editor_clock_changed_cb (self);
}

CcTimeEditor *
cc_time_editor_new (void)
{
  return g_object_new (CC_TYPE_TIME_EDITOR, NULL);
}

void
cc_time_editor_set_time (CcTimeEditor *self,
                         guint         hour,
                         guint         minute)
{
  g_return_if_fail (CC_IS_TIME_EDITOR (self));

  cc_time_entry_set_time (self->time_entry, hour, minute);
}

guint
cc_time_editor_get_hour (CcTimeEditor *self)
{
  g_return_val_if_fail (CC_IS_TIME_EDITOR (self), 0);

  return cc_time_entry_get_hour (self->time_entry);
}

guint
cc_time_editor_get_minute (CcTimeEditor *self)
{
  g_return_val_if_fail (CC_IS_TIME_EDITOR (self), 0);

  return cc_time_entry_get_minute (self->time_entry);
}

gboolean
cc_time_editor_get_am_pm (CcTimeEditor *self)
{
  g_return_val_if_fail (CC_IS_TIME_EDITOR (self), TRUE);

  return TRUE;
}

void
cc_time_editor_set_am_pm (CcTimeEditor *self,
                          gboolean      is_am_pm)
{
  g_return_if_fail (CC_IS_TIME_EDITOR (self));

  cc_time_entry_set_am_pm (self->time_entry, is_am_pm);
}