[gnome-control-center] user-accounts: Move non-shared function
- From: Robert Ancell <rancell src gnome org>
- To: commits-list gnome org
- Cc:
- Subject: [gnome-control-center] user-accounts: Move non-shared function
- Date: Tue, 13 Nov 2018 21:16:37 +0000 (UTC)
commit a41780267b212cb9f5fd7f43bd3118fee96af0e7
Author: Robert Ancell <robert ancell canonical com>
Date: Sat Nov 10 09:59:14 2018 +1300
user-accounts: Move non-shared function
The generate_username_choices function is only used in CcAddUserDialog
panels/user-accounts/cc-add-user-dialog.c | 228 +++++++++++++++++++++++++++++
panels/user-accounts/um-utils.c | 235 +-----------------------------
panels/user-accounts/um-utils.h | 5 +-
3 files changed, 237 insertions(+), 231 deletions(-)
---
diff --git a/panels/user-accounts/cc-add-user-dialog.c b/panels/user-accounts/cc-add-user-dialog.c
index 7a09747e9..70a768b01 100644
--- a/panels/user-accounts/cc-add-user-dialog.c
+++ b/panels/user-accounts/cc-add-user-dialog.c
@@ -383,6 +383,234 @@ local_name_entry_focus_out_event_cb (CcAddUserDialog *self)
return FALSE;
}
+static void
+generate_username_choices (const gchar *name,
+ GtkListStore *store)
+{
+ gboolean in_use, same_as_initial;
+ char *lc_name, *ascii_name, *stripped_name;
+ char **words1;
+ char **words2 = NULL;
+ char **w1, **w2;
+ char *c;
+ char *unicode_fallback = "?";
+ GString *first_word, *last_word;
+ GString *item0, *item1, *item2, *item3, *item4;
+ int len;
+ int nwords1, nwords2, i;
+ GHashTable *items;
+ GtkTreeIter iter;
+ gsize max_name_length;
+
+ gtk_list_store_clear (store);
+
+ ascii_name = g_convert_with_fallback (name, -1, "ASCII//TRANSLIT", "UTF-8",
+ unicode_fallback, NULL, NULL, NULL);
+
+ lc_name = g_ascii_strdown (ascii_name, -1);
+
+ /* Remove all non ASCII alphanumeric chars from the name,
+ * apart from the few allowed symbols.
+ *
+ * We do remove '.', even though it is usually allowed,
+ * since it often comes in via an abbreviated middle name,
+ * and the dot looks just wrong in the proposals then.
+ */
+ stripped_name = g_strnfill (strlen (lc_name) + 1, '\0');
+ i = 0;
+ for (c = lc_name; *c; c++) {
+ if (!(g_ascii_isdigit (*c) || g_ascii_islower (*c) ||
+ *c == ' ' || *c == '-' || *c == '_' ||
+ /* used to track invalid words, removed below */
+ *c == '?') )
+ continue;
+
+ stripped_name[i] = *c;
+ i++;
+ }
+
+ if (strlen (stripped_name) == 0) {
+ g_free (ascii_name);
+ g_free (lc_name);
+ g_free (stripped_name);
+ return;
+ }
+
+ /* we split name on spaces, and then on dashes, so that we can treat
+ * words linked with dashes the same way, i.e. both fully shown, or
+ * both abbreviated
+ */
+ words1 = g_strsplit_set (stripped_name, " ", -1);
+ len = g_strv_length (words1);
+
+ /* The default item is a concatenation of all words without ? */
+ item0 = g_string_sized_new (strlen (stripped_name));
+
+ g_free (ascii_name);
+ g_free (lc_name);
+ g_free (stripped_name);
+
+ /* Concatenate the whole first word with the first letter of each
+ * word (item1), and the last word with the first letter of each
+ * word (item2). item3 and item4 are symmetrical respectively to
+ * item1 and item2.
+ *
+ * Constant 5 is the max reasonable number of words we may get when
+ * splitting on dashes, since we can't guess it at this point,
+ * and reallocating would be too bad.
+ */
+ item1 = g_string_sized_new (strlen (words1[0]) + len - 1 + 5);
+ item3 = g_string_sized_new (strlen (words1[0]) + len - 1 + 5);
+
+ item2 = g_string_sized_new (strlen (words1[len - 1]) + len - 1 + 5);
+ item4 = g_string_sized_new (strlen (words1[len - 1]) + len - 1 + 5);
+
+ /* again, guess at the max size of names */
+ first_word = g_string_sized_new (20);
+ last_word = g_string_sized_new (20);
+
+ nwords1 = 0;
+ nwords2 = 0;
+ for (w1 = words1; *w1; w1++) {
+ if (strlen (*w1) == 0)
+ continue;
+
+ /* skip words with string '?', most likely resulting
+ * from failed transliteration to ASCII
+ */
+ if (strstr (*w1, unicode_fallback) != NULL)
+ continue;
+
+ nwords1++; /* count real words, excluding empty string */
+
+ item0 = g_string_append (item0, *w1);
+
+ words2 = g_strsplit_set (*w1, "-", -1);
+ /* reset last word if a new non-empty word has been found */
+ if (strlen (*words2) > 0)
+ last_word = g_string_set_size (last_word, 0);
+
+ for (w2 = words2; *w2; w2++) {
+ if (strlen (*w2) == 0)
+ continue;
+
+ nwords2++;
+
+ /* part of the first "toplevel" real word */
+ if (nwords1 == 1) {
+ item1 = g_string_append (item1, *w2);
+ first_word = g_string_append (first_word, *w2);
+ }
+ else {
+ item1 = g_string_append_unichar (item1,
+ g_utf8_get_char (*w2));
+ item3 = g_string_append_unichar (item3,
+ g_utf8_get_char (*w2));
+ }
+
+ /* not part of the last "toplevel" word */
+ if (w1 != words1 + len - 1) {
+ item2 = g_string_append_unichar (item2,
+ g_utf8_get_char (*w2));
+ item4 = g_string_append_unichar (item4,
+ g_utf8_get_char (*w2));
+ }
+
+ /* always save current word so that we have it if last one reveals empty */
+ last_word = g_string_append (last_word, *w2);
+ }
+
+ g_strfreev (words2);
+ }
+ item2 = g_string_append (item2, last_word->str);
+ item3 = g_string_append (item3, first_word->str);
+ item4 = g_string_prepend (item4, last_word->str);
+
+ max_name_length = get_username_max_length ();
+
+ g_string_truncate (first_word, max_name_length);
+ g_string_truncate (last_word, max_name_length);
+
+ g_string_truncate (item0, max_name_length);
+ g_string_truncate (item1, max_name_length);
+ g_string_truncate (item2, max_name_length);
+ g_string_truncate (item3, max_name_length);
+ g_string_truncate (item4, max_name_length);
+
+ items = g_hash_table_new (g_str_hash, g_str_equal);
+
+ in_use = is_username_used (item0->str);
+ if (!in_use && !g_ascii_isdigit (item0->str[0])) {
+ gtk_list_store_append (store, &iter);
+ gtk_list_store_set (store, &iter, 0, item0->str, -1);
+ g_hash_table_insert (items, item0->str, item0->str);
+ }
+
+ in_use = is_username_used (item1->str);
+ same_as_initial = (g_strcmp0 (item0->str, item1->str) == 0);
+ if (!same_as_initial && nwords2 > 0 && !in_use && !g_ascii_isdigit (item1->str[0])) {
+ gtk_list_store_append (store, &iter);
+ gtk_list_store_set (store, &iter, 0, item1->str, -1);
+ g_hash_table_insert (items, item1->str, item1->str);
+ }
+
+ /* if there's only one word, would be the same as item1 */
+ if (nwords2 > 1) {
+ /* add other items */
+ in_use = is_username_used (item2->str);
+ if (!in_use && !g_ascii_isdigit (item2->str[0]) &&
+ !g_hash_table_lookup (items, item2->str)) {
+ gtk_list_store_append (store, &iter);
+ gtk_list_store_set (store, &iter, 0, item2->str, -1);
+ g_hash_table_insert (items, item2->str, item2->str);
+ }
+
+ in_use = is_username_used (item3->str);
+ if (!in_use && !g_ascii_isdigit (item3->str[0]) &&
+ !g_hash_table_lookup (items, item3->str)) {
+ gtk_list_store_append (store, &iter);
+ gtk_list_store_set (store, &iter, 0, item3->str, -1);
+ g_hash_table_insert (items, item3->str, item3->str);
+ }
+
+ in_use = is_username_used (item4->str);
+ if (!in_use && !g_ascii_isdigit (item4->str[0]) &&
+ !g_hash_table_lookup (items, item4->str)) {
+ gtk_list_store_append (store, &iter);
+ gtk_list_store_set (store, &iter, 0, item4->str, -1);
+ g_hash_table_insert (items, item4->str, item4->str);
+ }
+
+ /* add the last word */
+ in_use = is_username_used (last_word->str);
+ if (!in_use && !g_ascii_isdigit (last_word->str[0]) &&
+ !g_hash_table_lookup (items, last_word->str)) {
+ gtk_list_store_append (store, &iter);
+ gtk_list_store_set (store, &iter, 0, last_word->str, -1);
+ g_hash_table_insert (items, last_word->str, last_word->str);
+ }
+
+ /* ...and the first one */
+ in_use = is_username_used (first_word->str);
+ if (!in_use && !g_ascii_isdigit (first_word->str[0]) &&
+ !g_hash_table_lookup (items, first_word->str)) {
+ gtk_list_store_append (store, &iter);
+ gtk_list_store_set (store, &iter, 0, first_word->str, -1);
+ g_hash_table_insert (items, first_word->str, first_word->str);
+ }
+ }
+
+ g_hash_table_destroy (items);
+ g_strfreev (words1);
+ g_string_free (first_word, TRUE);
+ g_string_free (last_word, TRUE);
+ g_string_free (item0, TRUE);
+ g_string_free (item1, TRUE);
+ g_string_free (item2, TRUE);
+ g_string_free (item3, TRUE);
+ g_string_free (item4, TRUE);
+}
+
static void
local_name_entry_changed_cb (CcAddUserDialog *self)
{
diff --git a/panels/user-accounts/um-utils.c b/panels/user-accounts/um-utils.c
index e34272327..5f1b30974 100644
--- a/panels/user-accounts/um-utils.c
+++ b/panels/user-accounts/um-utils.c
@@ -336,9 +336,13 @@ clear_entry_validation_error (GtkEntry *entry)
* result in failure when running useradd. We could check UT_NAMESIZE instead,
* but that is nonstandard. Better to use POSIX utmpx.
*/
-#define MAXNAMELEN (sizeof (((struct utmpx *)NULL)->ut_user))
+gsize
+get_username_max_length (void)
+{
+ return sizeof (((struct utmpx *)NULL)->ut_user);
+}
-static gboolean
+gboolean
is_username_used (const gchar *username)
{
struct passwd *pwent;
@@ -397,7 +401,7 @@ is_valid_username (const gchar *username, gchar **tip)
} else {
empty = FALSE;
in_use = is_username_used (username);
- too_long = strlen (username) > MAXNAMELEN;
+ too_long = strlen (username) > get_username_max_length ();
}
valid = TRUE;
@@ -437,228 +441,3 @@ is_valid_username (const gchar *username, gchar **tip)
return valid;
}
-
-void
-generate_username_choices (const gchar *name,
- GtkListStore *store)
-{
- gboolean in_use, same_as_initial;
- char *lc_name, *ascii_name, *stripped_name;
- char **words1;
- char **words2 = NULL;
- char **w1, **w2;
- char *c;
- char *unicode_fallback = "?";
- GString *first_word, *last_word;
- GString *item0, *item1, *item2, *item3, *item4;
- int len;
- int nwords1, nwords2, i;
- GHashTable *items;
- GtkTreeIter iter;
-
- gtk_list_store_clear (store);
-
- ascii_name = g_convert_with_fallback (name, -1, "ASCII//TRANSLIT", "UTF-8",
- unicode_fallback, NULL, NULL, NULL);
-
- lc_name = g_ascii_strdown (ascii_name, -1);
-
- /* Remove all non ASCII alphanumeric chars from the name,
- * apart from the few allowed symbols.
- *
- * We do remove '.', even though it is usually allowed,
- * since it often comes in via an abbreviated middle name,
- * and the dot looks just wrong in the proposals then.
- */
- stripped_name = g_strnfill (strlen (lc_name) + 1, '\0');
- i = 0;
- for (c = lc_name; *c; c++) {
- if (!(g_ascii_isdigit (*c) || g_ascii_islower (*c) ||
- *c == ' ' || *c == '-' || *c == '_' ||
- /* used to track invalid words, removed below */
- *c == '?') )
- continue;
-
- stripped_name[i] = *c;
- i++;
- }
-
- if (strlen (stripped_name) == 0) {
- g_free (ascii_name);
- g_free (lc_name);
- g_free (stripped_name);
- return;
- }
-
- /* we split name on spaces, and then on dashes, so that we can treat
- * words linked with dashes the same way, i.e. both fully shown, or
- * both abbreviated
- */
- words1 = g_strsplit_set (stripped_name, " ", -1);
- len = g_strv_length (words1);
-
- /* The default item is a concatenation of all words without ? */
- item0 = g_string_sized_new (strlen (stripped_name));
-
- g_free (ascii_name);
- g_free (lc_name);
- g_free (stripped_name);
-
- /* Concatenate the whole first word with the first letter of each
- * word (item1), and the last word with the first letter of each
- * word (item2). item3 and item4 are symmetrical respectively to
- * item1 and item2.
- *
- * Constant 5 is the max reasonable number of words we may get when
- * splitting on dashes, since we can't guess it at this point,
- * and reallocating would be too bad.
- */
- item1 = g_string_sized_new (strlen (words1[0]) + len - 1 + 5);
- item3 = g_string_sized_new (strlen (words1[0]) + len - 1 + 5);
-
- item2 = g_string_sized_new (strlen (words1[len - 1]) + len - 1 + 5);
- item4 = g_string_sized_new (strlen (words1[len - 1]) + len - 1 + 5);
-
- /* again, guess at the max size of names */
- first_word = g_string_sized_new (20);
- last_word = g_string_sized_new (20);
-
- nwords1 = 0;
- nwords2 = 0;
- for (w1 = words1; *w1; w1++) {
- if (strlen (*w1) == 0)
- continue;
-
- /* skip words with string '?', most likely resulting
- * from failed transliteration to ASCII
- */
- if (strstr (*w1, unicode_fallback) != NULL)
- continue;
-
- nwords1++; /* count real words, excluding empty string */
-
- item0 = g_string_append (item0, *w1);
-
- words2 = g_strsplit_set (*w1, "-", -1);
- /* reset last word if a new non-empty word has been found */
- if (strlen (*words2) > 0)
- last_word = g_string_set_size (last_word, 0);
-
- for (w2 = words2; *w2; w2++) {
- if (strlen (*w2) == 0)
- continue;
-
- nwords2++;
-
- /* part of the first "toplevel" real word */
- if (nwords1 == 1) {
- item1 = g_string_append (item1, *w2);
- first_word = g_string_append (first_word, *w2);
- }
- else {
- item1 = g_string_append_unichar (item1,
- g_utf8_get_char (*w2));
- item3 = g_string_append_unichar (item3,
- g_utf8_get_char (*w2));
- }
-
- /* not part of the last "toplevel" word */
- if (w1 != words1 + len - 1) {
- item2 = g_string_append_unichar (item2,
- g_utf8_get_char (*w2));
- item4 = g_string_append_unichar (item4,
- g_utf8_get_char (*w2));
- }
-
- /* always save current word so that we have it if last one reveals empty */
- last_word = g_string_append (last_word, *w2);
- }
-
- g_strfreev (words2);
- }
- item2 = g_string_append (item2, last_word->str);
- item3 = g_string_append (item3, first_word->str);
- item4 = g_string_prepend (item4, last_word->str);
-
- g_string_truncate (first_word, MAXNAMELEN);
- g_string_truncate (last_word, MAXNAMELEN);
-
- g_string_truncate (item0, MAXNAMELEN);
- g_string_truncate (item1, MAXNAMELEN);
- g_string_truncate (item2, MAXNAMELEN);
- g_string_truncate (item3, MAXNAMELEN);
- g_string_truncate (item4, MAXNAMELEN);
-
- items = g_hash_table_new (g_str_hash, g_str_equal);
-
- in_use = is_username_used (item0->str);
- if (!in_use && !g_ascii_isdigit (item0->str[0])) {
- gtk_list_store_append (store, &iter);
- gtk_list_store_set (store, &iter, 0, item0->str, -1);
- g_hash_table_insert (items, item0->str, item0->str);
- }
-
- in_use = is_username_used (item1->str);
- same_as_initial = (g_strcmp0 (item0->str, item1->str) == 0);
- if (!same_as_initial && nwords2 > 0 && !in_use && !g_ascii_isdigit (item1->str[0])) {
- gtk_list_store_append (store, &iter);
- gtk_list_store_set (store, &iter, 0, item1->str, -1);
- g_hash_table_insert (items, item1->str, item1->str);
- }
-
- /* if there's only one word, would be the same as item1 */
- if (nwords2 > 1) {
- /* add other items */
- in_use = is_username_used (item2->str);
- if (!in_use && !g_ascii_isdigit (item2->str[0]) &&
- !g_hash_table_lookup (items, item2->str)) {
- gtk_list_store_append (store, &iter);
- gtk_list_store_set (store, &iter, 0, item2->str, -1);
- g_hash_table_insert (items, item2->str, item2->str);
- }
-
- in_use = is_username_used (item3->str);
- if (!in_use && !g_ascii_isdigit (item3->str[0]) &&
- !g_hash_table_lookup (items, item3->str)) {
- gtk_list_store_append (store, &iter);
- gtk_list_store_set (store, &iter, 0, item3->str, -1);
- g_hash_table_insert (items, item3->str, item3->str);
- }
-
- in_use = is_username_used (item4->str);
- if (!in_use && !g_ascii_isdigit (item4->str[0]) &&
- !g_hash_table_lookup (items, item4->str)) {
- gtk_list_store_append (store, &iter);
- gtk_list_store_set (store, &iter, 0, item4->str, -1);
- g_hash_table_insert (items, item4->str, item4->str);
- }
-
- /* add the last word */
- in_use = is_username_used (last_word->str);
- if (!in_use && !g_ascii_isdigit (last_word->str[0]) &&
- !g_hash_table_lookup (items, last_word->str)) {
- gtk_list_store_append (store, &iter);
- gtk_list_store_set (store, &iter, 0, last_word->str, -1);
- g_hash_table_insert (items, last_word->str, last_word->str);
- }
-
- /* ...and the first one */
- in_use = is_username_used (first_word->str);
- if (!in_use && !g_ascii_isdigit (first_word->str[0]) &&
- !g_hash_table_lookup (items, first_word->str)) {
- gtk_list_store_append (store, &iter);
- gtk_list_store_set (store, &iter, 0, first_word->str, -1);
- g_hash_table_insert (items, first_word->str, first_word->str);
- }
- }
-
- g_hash_table_destroy (items);
- g_strfreev (words1);
- g_string_free (first_word, TRUE);
- g_string_free (last_word, TRUE);
- g_string_free (item0, TRUE);
- g_string_free (item1, TRUE);
- g_string_free (item2, TRUE);
- g_string_free (item3, TRUE);
- g_string_free (item4, TRUE);
-}
diff --git a/panels/user-accounts/um-utils.h b/panels/user-accounts/um-utils.h
index 9a963d9ef..482350684 100644
--- a/panels/user-accounts/um-utils.h
+++ b/panels/user-accounts/um-utils.h
@@ -37,11 +37,10 @@ void set_entry_validation_error (GtkEntry *entry,
const gchar *text);
void clear_entry_validation_error (GtkEntry *entry);
+gsize get_username_max_length (void);
+gboolean is_username_used (const gchar *username);
gboolean is_valid_name (const gchar *name);
gboolean is_valid_username (const gchar *name,
gchar **tip);
-void generate_username_choices (const gchar *name,
- GtkListStore *store);
-
G_END_DECLS
[
Date Prev][
Date Next] [
Thread Prev][
Thread Next]
[
Thread Index]
[
Date Index]
[
Author Index]