1
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 8 -*-
3
* Copyright 2009-2010 Red Hat, Inc,
5
* This program is free software; you can redistribute it and/or modify
6
* it under the terms of the GNU General Public License as published by
7
* the Free Software Foundation; either version 2 of the License, or
8
* (at your option) any later version.
10
* This program is distributed in the hope that it will be useful,
11
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
* GNU General Public License for more details.
15
* You should have received a copy of the GNU General Public License
16
* along with this program; if not, write to the Free Software
17
* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
19
* Written by: Matthias Clasen <mclasen@redhat.com>
26
#include <sys/types.h>
31
#include <glib/gi18n.h>
37
gchar *placeholder_str;
43
static IconShapeData *
44
icon_shape_data_new (const gchar *text,
45
const gchar *placeholder,
50
data = g_new0 (IconShapeData, 1);
52
data->text = g_strdup (text);
53
data->placeholder_str = g_strdup (placeholder);
54
data->placeholder = g_utf8_get_char_validated (placeholder, -1);
55
data->icon = g_object_ref (icon);
61
icon_shape_data_free (gpointer user_data)
63
IconShapeData *data = user_data;
66
g_free (data->placeholder_str);
67
g_object_unref (data->icon);
72
icon_shape_renderer (cairo_t *cr,
77
IconShapeData *data = user_data;
80
cairo_get_current_point (cr, &x, &y);
81
if (GPOINTER_TO_UINT (attr->data) == data->placeholder) {
87
ascent = pango_units_to_double (attr->ink_rect.y);
88
height = pango_units_to_double (attr->ink_rect.height);
89
info = gtk_icon_theme_lookup_by_gicon (gtk_icon_theme_get_default (),
92
GTK_ICON_LOOKUP_FORCE_SIZE | GTK_ICON_LOOKUP_USE_BUILTIN);
93
pixbuf = gtk_icon_info_load_icon (info, NULL);
94
gtk_icon_info_free (info);
96
cairo_set_operator (cr, CAIRO_OPERATOR_OVER);
97
cairo_reset_clip (cr);
98
gdk_cairo_set_source_pixbuf (cr, pixbuf, x, y + ascent);
100
g_object_unref (pixbuf);
104
static PangoAttrList *
105
create_shape_attr_list_for_layout (PangoLayout *layout,
108
PangoAttrList *attrs;
109
PangoFontMetrics *metrics;
110
gint ascent, descent;
111
PangoRectangle ink_rect, logical_rect;
114
gint placeholder_len;
116
/* Get font metrics and prepare fancy shape size */
117
metrics = pango_context_get_metrics (pango_layout_get_context (layout),
118
pango_layout_get_font_description (layout),
120
ascent = pango_font_metrics_get_ascent (metrics);
121
descent = pango_font_metrics_get_descent (metrics);
122
pango_font_metrics_unref (metrics);
125
logical_rect.y = - ascent;
126
logical_rect.width = ascent + descent;
127
logical_rect.height = ascent + descent;
129
ink_rect = logical_rect;
131
attrs = pango_attr_list_new ();
132
text = pango_layout_get_text (layout);
133
placeholder_len = strlen (data->placeholder_str);
134
for (p = text; (p = strstr (p, data->placeholder_str)); p += placeholder_len) {
135
PangoAttribute *attr;
137
attr = pango_attr_shape_new_with_data (&ink_rect,
139
GUINT_TO_POINTER (g_utf8_get_char (p)),
142
attr->start_index = p - text;
143
attr->end_index = attr->start_index + placeholder_len;
145
pango_attr_list_insert (attrs, attr);
152
query_unlock_tooltip (GtkWidget *widget,
155
gboolean keyboard_tooltip,
161
PangoAttrList *attrs;
164
data = g_object_get_data (G_OBJECT (widget), "icon-shape-data");
165
label = g_object_get_data (G_OBJECT (widget), "tooltip-label");
167
label = gtk_label_new (data->text);
168
g_object_ref_sink (label);
169
g_object_set_data_full (G_OBJECT (widget),
170
"tooltip-label", label, g_object_unref);
173
layout = gtk_label_get_layout (GTK_LABEL (label));
174
pango_cairo_context_set_shape_renderer (pango_layout_get_context (layout),
178
attrs = create_shape_attr_list_for_layout (layout, data);
179
gtk_label_set_attributes (GTK_LABEL (label), attrs);
180
pango_attr_list_unref (attrs);
182
gtk_tooltip_set_custom (tooltip, label);
188
setup_tooltip_with_embedded_icon (GtkWidget *widget,
190
const gchar *placeholder,
195
data = g_object_get_data (G_OBJECT (widget), "icon-shape-data");
197
gtk_widget_set_has_tooltip (widget, FALSE);
198
g_signal_handler_disconnect (widget, data->query_id);
199
g_object_set_data (G_OBJECT (widget), "icon-shape-data", NULL);
200
g_object_set_data (G_OBJECT (widget), "tooltip-label", NULL);
204
gtk_widget_set_tooltip_text (widget, text);
208
data = icon_shape_data_new (text, placeholder, icon);
209
g_object_set_data_full (G_OBJECT (widget),
212
icon_shape_data_free);
214
gtk_widget_set_has_tooltip (widget, TRUE);
215
data->query_id = g_signal_connect (widget, "query-tooltip",
216
G_CALLBACK (query_unlock_tooltip), NULL);
221
show_tooltip_now (GtkWidget *widget,
224
GtkSettings *settings;
227
settings = gtk_widget_get_settings (widget);
229
g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);
230
g_object_set (settings, "gtk-tooltip-timeout", 1, NULL);
231
gtk_tooltip_trigger_tooltip_query (gtk_widget_get_display (widget));
232
g_object_set (settings, "gtk-tooltip-timeout", timeout, NULL);
238
query_tooltip (GtkWidget *widget,
241
gboolean keyboard_mode,
247
if (GTK_ENTRY_ICON_SECONDARY == gtk_entry_get_icon_at_pos (GTK_ENTRY (widget), x, y)) {
248
tip = gtk_entry_get_icon_tooltip_text (GTK_ENTRY (widget),
249
GTK_ENTRY_ICON_SECONDARY);
250
gtk_tooltip_set_text (tooltip, tip);
261
icon_released (GtkEntry *entry,
262
GtkEntryIconPosition pos,
266
GtkSettings *settings;
269
settings = gtk_widget_get_settings (GTK_WIDGET (entry));
271
g_object_get (settings, "gtk-tooltip-timeout", &timeout, NULL);
272
g_object_set (settings, "gtk-tooltip-timeout", 1, NULL);
273
gtk_tooltip_trigger_tooltip_query (gtk_widget_get_display (GTK_WIDGET (entry)));
274
g_object_set (settings, "gtk-tooltip-timeout", timeout, NULL);
279
set_entry_validation_error (GtkEntry *entry,
282
g_object_set (entry, "caps-lock-warning", FALSE, NULL);
283
gtk_entry_set_icon_from_icon_name (entry,
284
GTK_ENTRY_ICON_SECONDARY,
285
"dialog-error-symbolic");
286
gtk_entry_set_icon_activatable (entry,
287
GTK_ENTRY_ICON_SECONDARY,
289
g_signal_connect (entry, "icon-release",
290
G_CALLBACK (icon_released), FALSE);
291
g_signal_connect (entry, "query-tooltip",
292
G_CALLBACK (query_tooltip), NULL);
293
g_object_set (entry, "has-tooltip", TRUE, NULL);
294
gtk_entry_set_icon_tooltip_text (entry,
295
GTK_ENTRY_ICON_SECONDARY,
300
clear_entry_validation_error (GtkEntry *entry)
304
g_object_get (entry, "caps-lock-warning", &warning, NULL);
309
g_object_set (entry, "has-tooltip", FALSE, NULL);
310
gtk_entry_set_icon_from_pixbuf (entry,
311
GTK_ENTRY_ICON_SECONDARY,
313
g_object_set (entry, "caps-lock-warning", TRUE, NULL);
317
popup_menu_below_button (GtkMenu *menu,
323
GtkRequisition menu_req;
324
GtkTextDirection direction;
325
GtkAllocation allocation;
327
gtk_widget_get_preferred_size (GTK_WIDGET (menu), NULL, &menu_req);
329
direction = gtk_widget_get_direction (button);
331
gdk_window_get_origin (gtk_widget_get_window (button), x, y);
332
gtk_widget_get_allocation (button, &allocation);
334
*y += allocation.y + allocation.height;
336
if (direction == GTK_TEXT_DIR_LTR)
337
*x += MAX (allocation.width - menu_req.width, 0);
338
else if (menu_req.width > allocation.width)
339
*x -= menu_req.width - allocation.width;
345
rounded_rectangle (cairo_t *cr,
349
gdouble corner_radius,
356
radius = corner_radius / aspect;
357
degrees = G_PI / 180.0;
359
cairo_new_sub_path (cr);
384
cairo_close_path (cr);
388
down_arrow (GtkStyleContext *context,
397
GdkRGBA outline_color;
398
gdouble vertical_overshoot;
401
gdouble x_double, y_double;
405
flags = gtk_style_context_get_state (context);
407
gtk_style_context_get_color (context, flags, &fg_color);
408
gtk_style_context_get_border_color (context, flags, &outline_color);
412
vertical_overshoot = line_width / 2.0 * (1. / tan (G_PI / 8));
413
if (line_width % 2 == 1)
414
vertical_overshoot = ceil (0.5 + vertical_overshoot) - 0.5;
416
vertical_overshoot = ceil (vertical_overshoot);
417
diameter = (gint) MAX (3, width - 2 * vertical_overshoot);
418
diameter -= (1 - (diameter + line_width) % 2);
419
radius = diameter / 2.;
420
x_double = floor ((x + width / 2) - (radius + line_width) / 2.) + (radius + line_width) / 2.;
422
y_double = (y + height / 2) - 0.5;
426
cairo_translate (cr, x_double, y_double);
427
cairo_rotate (cr, angle);
429
cairo_move_to (cr, - radius / 2., - radius);
430
cairo_line_to (cr, radius / 2., 0);
431
cairo_line_to (cr, - radius / 2., radius);
433
cairo_close_path (cr);
435
cairo_set_line_width (cr, line_width);
437
gdk_cairo_set_source_rgba (cr, &fg_color);
439
cairo_fill_preserve (cr);
441
gdk_cairo_set_source_rgba (cr, &outline_color);
448
#define MAXNAMELEN (UT_NAMESIZE - 1)
451
is_username_used (const gchar *username)
453
struct passwd *pwent;
455
if (username == NULL || username[0] == '\0') {
459
pwent = getpwnam (username);
461
return pwent != NULL;
465
is_valid_name (const gchar *name)
469
valid = (strlen (name) > 0);
475
is_valid_username (const gchar *username, gchar **tip)
483
if (username == NULL || username[0] == '\0') {
489
in_use = is_username_used (username);
490
too_long = strlen (username) > MAXNAMELEN;
494
if (!in_use && !empty && !too_long) {
495
/* First char must be a letter, and it must only composed
496
* of ASCII letters, digits, and a '.', '-', '_'
498
for (c = username; *c; c++) {
499
if (! ((*c >= 'a' && *c <= 'z') ||
500
(*c >= 'A' && *c <= 'Z') ||
501
(*c >= '0' && *c <= '9') ||
502
(*c == '_') || (*c == '.') ||
503
(*c == '-' && c != username)))
508
valid = !empty && !in_use && !too_long && valid;
510
if (!empty && (in_use || too_long || !valid)) {
512
*tip = g_strdup_printf (_("A user with the username '%s' already exists"),
516
*tip = g_strdup_printf (_("The username is too long"));
518
else if (username[0] == '-') {
519
*tip = g_strdup (_("The username cannot start with a '-'"));
522
*tip = g_strdup (_("The username must only consist of:\n"
523
" \xe2\x9e\xa3 letters from the English alphabet\n"
524
" \xe2\x9e\xa3 digits\n"
525
" \xe2\x9e\xa3 any of the characters '.', '-' and '_'"));
536
generate_username_choices (const gchar *name,
540
char *lc_name, *ascii_name, *stripped_name;
542
char **words2 = NULL;
545
char *unicode_fallback = "?";
546
GString *first_word, *last_word;
547
GString *item0, *item1, *item2, *item3, *item4;
549
int nwords1, nwords2, i;
553
gtk_list_store_clear (store);
555
ascii_name = g_convert_with_fallback (name, -1, "ASCII//TRANSLIT", "UTF-8",
556
unicode_fallback, NULL, NULL, NULL);
558
lc_name = g_ascii_strdown (ascii_name, -1);
560
/* Remove all non ASCII alphanumeric chars from the name,
561
* apart from the few allowed symbols.
563
* We do remove '.', even though it is usually allowed,
564
* since it often comes in via an abbreviated middle name,
565
* and the dot looks just wrong in the proposals then.
567
stripped_name = g_strnfill (strlen (lc_name) + 1, '\0');
569
for (c = lc_name; *c; c++) {
570
if (!(g_ascii_isdigit (*c) || g_ascii_islower (*c) ||
571
*c == ' ' || *c == '-' || *c == '_' ||
572
/* used to track invalid words, removed below */
576
stripped_name[i] = *c;
580
if (strlen (stripped_name) == 0) {
583
g_free (stripped_name);
587
/* we split name on spaces, and then on dashes, so that we can treat
588
* words linked with dashes the same way, i.e. both fully shown, or
591
words1 = g_strsplit_set (stripped_name, " ", -1);
592
len = g_strv_length (words1);
594
/* The default item is a concatenation of all words without ? */
595
item0 = g_string_sized_new (strlen (stripped_name));
599
g_free (stripped_name);
601
/* Concatenate the whole first word with the first letter of each
602
* word (item1), and the last word with the first letter of each
603
* word (item2). item3 and item4 are symmetrical respectively to
606
* Constant 5 is the max reasonable number of words we may get when
607
* splitting on dashes, since we can't guess it at this point,
608
* and reallocating would be too bad.
610
item1 = g_string_sized_new (strlen (words1[0]) + len - 1 + 5);
611
item3 = g_string_sized_new (strlen (words1[0]) + len - 1 + 5);
613
item2 = g_string_sized_new (strlen (words1[len - 1]) + len - 1 + 5);
614
item4 = g_string_sized_new (strlen (words1[len - 1]) + len - 1 + 5);
616
/* again, guess at the max size of names */
617
first_word = g_string_sized_new (20);
618
last_word = g_string_sized_new (20);
622
for (w1 = words1; *w1; w1++) {
623
if (strlen (*w1) == 0)
626
/* skip words with string '?', most likely resulting
627
* from failed transliteration to ASCII
629
if (strstr (*w1, unicode_fallback) != NULL)
632
nwords1++; /* count real words, excluding empty string */
634
item0 = g_string_append (item0, *w1);
636
words2 = g_strsplit_set (*w1, "-", -1);
637
/* reset last word if a new non-empty word has been found */
638
if (strlen (*words2) > 0)
639
last_word = g_string_set_size (last_word, 0);
641
for (w2 = words2; *w2; w2++) {
642
if (strlen (*w2) == 0)
647
/* part of the first "toplevel" real word */
649
item1 = g_string_append (item1, *w2);
650
first_word = g_string_append (first_word, *w2);
653
item1 = g_string_append_unichar (item1,
654
g_utf8_get_char (*w2));
655
item3 = g_string_append_unichar (item3,
656
g_utf8_get_char (*w2));
659
/* not part of the last "toplevel" word */
660
if (w1 != words1 + len - 1) {
661
item2 = g_string_append_unichar (item2,
662
g_utf8_get_char (*w2));
663
item4 = g_string_append_unichar (item4,
664
g_utf8_get_char (*w2));
667
/* always save current word so that we have it if last one reveals empty */
668
last_word = g_string_append (last_word, *w2);
673
item2 = g_string_append (item2, last_word->str);
674
item3 = g_string_append (item3, first_word->str);
675
item4 = g_string_prepend (item4, last_word->str);
677
items = g_hash_table_new (g_str_hash, g_str_equal);
679
in_use = is_username_used (item0->str);
680
if (!in_use && !g_ascii_isdigit (item0->str[0])) {
681
gtk_list_store_append (store, &iter);
682
gtk_list_store_set (store, &iter, 0, item0->str, -1);
683
g_hash_table_insert (items, item0->str, item0->str);
686
in_use = is_username_used (item1->str);
687
if (nwords2 > 0 && !in_use && !g_ascii_isdigit (item1->str[0])) {
688
gtk_list_store_append (store, &iter);
689
gtk_list_store_set (store, &iter, 0, item1->str, -1);
690
g_hash_table_insert (items, item1->str, item1->str);
693
/* if there's only one word, would be the same as item1 */
695
/* add other items */
696
in_use = is_username_used (item2->str);
697
if (!in_use && !g_ascii_isdigit (item2->str[0]) &&
698
!g_hash_table_lookup (items, item2->str)) {
699
gtk_list_store_append (store, &iter);
700
gtk_list_store_set (store, &iter, 0, item2->str, -1);
701
g_hash_table_insert (items, item2->str, item2->str);
704
in_use = is_username_used (item3->str);
705
if (!in_use && !g_ascii_isdigit (item3->str[0]) &&
706
!g_hash_table_lookup (items, item3->str)) {
707
gtk_list_store_append (store, &iter);
708
gtk_list_store_set (store, &iter, 0, item3->str, -1);
709
g_hash_table_insert (items, item3->str, item3->str);
712
in_use = is_username_used (item4->str);
713
if (!in_use && !g_ascii_isdigit (item4->str[0]) &&
714
!g_hash_table_lookup (items, item4->str)) {
715
gtk_list_store_append (store, &iter);
716
gtk_list_store_set (store, &iter, 0, item4->str, -1);
717
g_hash_table_insert (items, item4->str, item4->str);
720
/* add the last word */
721
in_use = is_username_used (last_word->str);
722
if (!in_use && !g_ascii_isdigit (last_word->str[0]) &&
723
!g_hash_table_lookup (items, last_word->str)) {
724
gtk_list_store_append (store, &iter);
725
gtk_list_store_set (store, &iter, 0, last_word->str, -1);
726
g_hash_table_insert (items, last_word->str, last_word->str);
729
/* ...and the first one */
730
in_use = is_username_used (first_word->str);
731
if (!in_use && !g_ascii_isdigit (first_word->str[0]) &&
732
!g_hash_table_lookup (items, first_word->str)) {
733
gtk_list_store_append (store, &iter);
734
gtk_list_store_set (store, &iter, 0, first_word->str, -1);
735
g_hash_table_insert (items, first_word->str, first_word->str);
739
g_hash_table_destroy (items);
741
g_string_free (first_word, TRUE);
742
g_string_free (last_word, TRUE);
743
g_string_free (item0, TRUE);
744
g_string_free (item1, TRUE);
745
g_string_free (item2, TRUE);
746
g_string_free (item3, TRUE);
747
g_string_free (item4, TRUE);
751
is_using_ecryptfs (const gchar *name)
753
gboolean using_ecryptfs = FALSE;
758
prog = g_find_program_in_path ("ecryptfs-verify");
760
gchar *cmd = g_strdup_printf("'%s' -h -u '%s'", prog, name);
762
if (g_spawn_command_line_sync (cmd, NULL, NULL, &status, NULL)) {
764
using_ecryptfs = TRUE;
771
return using_ecryptfs;