summaryrefslogtreecommitdiffstats
path: root/gnome-initial-setup/pages/language/gis-welcome-widget.c
blob: 4d58a37fe87e3830025574105b7aff1d495431b3 (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
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*- */
/*
 * Copyright (C) 2013 Red Hat
 *
 * 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:
 *     Jasper St. Pierre <jstpierre@mecheye.net>
 */

#include "config.h"
#include "gis-welcome-widget.h"

#include <errno.h>
#include <locale.h>
#include <glib/gi18n.h>

#include "cc-common-language.h"

struct _GisWelcomeWidgetPrivate
{
  AdwCarousel *carousel;
  GHashTable *translation_widgets;  /* (element-type owned utf8 unowned GtkWidget) (owned) */

  guint timeout_id;
};
typedef struct _GisWelcomeWidgetPrivate GisWelcomeWidgetPrivate;

#define TIMEOUT 5

G_DEFINE_TYPE_WITH_PRIVATE (GisWelcomeWidget, gis_welcome_widget, ADW_TYPE_BIN);

static gboolean
advance_stack (gpointer user_data)
{
  GisWelcomeWidget *widget = user_data;
  GisWelcomeWidgetPrivate *priv = gis_welcome_widget_get_instance_private (widget);
  GtkWidget *child;
  unsigned int next_page;
  unsigned int n_pages;
  double current_page;

  n_pages = adw_carousel_get_n_pages (priv->carousel);
  if (n_pages == 0)
    goto out;

  current_page = ceil (adw_carousel_get_position (priv->carousel));
  next_page = ((int) current_page + 1) % n_pages;

  child = gtk_widget_get_first_child (GTK_WIDGET (priv->carousel));
  while (next_page-- > 0)
    child = gtk_widget_get_next_sibling (child);

  adw_carousel_scroll_to (priv->carousel, child, TRUE);

 out:
  return G_SOURCE_CONTINUE;
}

static void
gis_welcome_widget_start (GisWelcomeWidget *widget)
{
  GisWelcomeWidgetPrivate *priv = gis_welcome_widget_get_instance_private (widget);

  if (priv->timeout_id > 0)
    return;

  priv->timeout_id = g_timeout_add_seconds (5, advance_stack, widget);
}

static void
gis_welcome_widget_stop (GisWelcomeWidget *widget)
{
  GisWelcomeWidgetPrivate *priv = gis_welcome_widget_get_instance_private (widget);

  if (priv->timeout_id == 0)
    return;

  g_source_remove (priv->timeout_id);
  priv->timeout_id = 0;
}

static void
gis_welcome_widget_map (GtkWidget *widget)
{
  GTK_WIDGET_CLASS (gis_welcome_widget_parent_class)->map (widget);
  gis_welcome_widget_start (GIS_WELCOME_WIDGET (widget));
}

static void
gis_welcome_widget_unmap (GtkWidget *widget)
{
  GTK_WIDGET_CLASS (gis_welcome_widget_parent_class)->unmap (widget);
  gis_welcome_widget_stop (GIS_WELCOME_WIDGET (widget));
}

static const char *
welcome (const char *locale_id)
{
  locale_t locale;
  locale_t old_locale;
  const char *welcome;

  locale = newlocale (LC_MESSAGES_MASK, locale_id, (locale_t) 0);
  if (locale == (locale_t) 0)
    {
      if (errno == ENOENT)
        g_debug ("Failed to create locale %s: %s", locale_id, g_strerror (errno));
      else
        g_warning ("Failed to create locale %s: %s", locale_id, g_strerror (errno));

      return "Welcome!";
    }

  old_locale = uselocale (locale);

  /* Translators: This is meant to be a warm, engaging welcome message,
   * like greeting somebody at the door. If the exclamation mark is not
   * suitable for this in your language you may replace it.
   */
  welcome = _("Welcome!");

  uselocale (old_locale);
  freelocale (locale);

  return welcome;
}

static GtkWidget *
big_label (const char *text)
{
  GtkWidget *label = gtk_label_new (text);

  gtk_style_context_add_class (gtk_widget_get_style_context (label), "title-1");

  return label;
}

static void
fill_carousel (GisWelcomeWidget *widget)
{
  GisWelcomeWidgetPrivate *priv = gis_welcome_widget_get_instance_private (widget);
  g_autoptr(GHashTable) initial = cc_common_language_get_initial_languages ();
  GHashTableIter iter;
  gpointer key, value;
  g_autoptr(GHashTable) added_translations = NULL;

  added_translations = g_hash_table_new (g_str_hash, g_str_equal);

  g_hash_table_iter_init (&iter, initial);
  while (g_hash_table_iter_next (&iter, &key, &value))
    {
      const char *locale_id = key;
      const char *text;
      GtkWidget *label;

      if (!cc_common_language_has_font (locale_id))
        continue;

      text = welcome (locale_id);
      label = g_hash_table_lookup (added_translations, text);
      if (label == NULL) {
        label = big_label (text);
        adw_carousel_append (priv->carousel, label);
        g_hash_table_insert (added_translations, (gpointer) text, label);
      }

      g_hash_table_insert (priv->translation_widgets, g_strdup (locale_id), label);
    }
}

static void
gis_welcome_widget_constructed (GObject *object)
{
  G_OBJECT_CLASS (gis_welcome_widget_parent_class)->constructed (object);

  fill_carousel (GIS_WELCOME_WIDGET (object));
}

static void
gis_welcome_widget_dispose (GObject *object)
{
  GisWelcomeWidget *widget = GIS_WELCOME_WIDGET (object);
  GisWelcomeWidgetPrivate *priv = gis_welcome_widget_get_instance_private (widget);

  g_clear_pointer (&priv->translation_widgets, g_hash_table_unref);

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

static void
gis_welcome_widget_class_init (GisWelcomeWidgetClass *klass)
{
  GObjectClass *object_class = G_OBJECT_CLASS (klass);
  GtkWidgetClass *widget_class = GTK_WIDGET_CLASS (klass);

  gtk_widget_class_set_template_from_resource (widget_class, "/org/gnome/initial-setup/gis-welcome-widget.ui");

  gtk_widget_class_bind_template_child_private (widget_class, GisWelcomeWidget, carousel);

  object_class->constructed = gis_welcome_widget_constructed;
  object_class->dispose = gis_welcome_widget_dispose;
  widget_class->map = gis_welcome_widget_map;
  widget_class->unmap = gis_welcome_widget_unmap;
}

static void
gis_welcome_widget_init (GisWelcomeWidget *widget)
{
  GisWelcomeWidgetPrivate *priv = gis_welcome_widget_get_instance_private (widget);

  priv->translation_widgets = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, NULL);

  gtk_widget_init_template (GTK_WIDGET (widget));
}

void
gis_welcome_widget_show_locale (GisWelcomeWidget *widget,
                                const char       *locale_id)
{
  GisWelcomeWidgetPrivate *priv = gis_welcome_widget_get_instance_private (widget);
  GtkWidget *label;

  /* Restart the widget to reset the timer. */
  gis_welcome_widget_stop (widget);
  gis_welcome_widget_start (widget);

  label = g_hash_table_lookup (priv->translation_widgets, locale_id);
  if (label)
    adw_carousel_scroll_to (priv->carousel, label, FALSE);
}