Merge lp:~robert-ancell/indicator-session/blank-username into lp:indicator-session/14.04

Proposed by Robert Ancell
Status: Merged
Approved by: Ted Gould
Approved revision: 425
Merged at revision: 425
Proposed branch: lp:~robert-ancell/indicator-session/blank-username
Merge into: lp:indicator-session/14.04
Diff against target: 88 lines (+46/-3)
2 files modified
src/service.c (+16/-3)
tests/test-service.cc (+30/-0)
To merge this branch: bzr merge lp:~robert-ancell/indicator-session/blank-username
Reviewer Review Type Date Requested Status
Ted Gould (community) Approve
PS Jenkins bot (community) continuous-integration Approve
Review via email: mp+199604@code.launchpad.net

Commit message

Use username as label if the real name is empty or whitespace

To post a comment you must log in.
Revision history for this message
PS Jenkins bot (ps-jenkins) wrote :
review: Approve (continuous-integration)
Revision history for this message
Ted Gould (ted) wrote :

What about using g_unichar_isspace() since we expect the real name to be
a UTF8 string?

Revision history for this message
Robert Ancell (robert-ancell) wrote :

I had a long look at what it takes to parse it as UTF-8, I'm not sure if it's worth the effort.
- We really can't be sure it is valid UTF-8 so we have to validate it at the top of the function
- We have to handle invalid UTF8 cases which makes the code more complex
- This only helps if there are only some obscure non-printable UTF8 characters used - it's completely valid to check for the ASCII characters as all the UTF8 will be ignored

So I'd say "don't bother", what do you think?

Revision history for this message
Ted Gould (ted) wrote :

I guess my concern was that the UTF-8 code would be a multibyte code, where one of those bytes is a space. So we'd improperly detect a space when really it was a valid visible UTF-8 character. But since what we're detecting is all spaces, that would require both bytes to be valid ASCII spaces, and the name to be a single multi-byte character in length. Which seems pretty rare.

review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/service.c'
2--- src/service.c 2013-11-08 17:41:36 +0000
3+++ src/service.c 2013-12-19 03:06:21 +0000
4@@ -274,6 +274,19 @@
5 }
6
7 static const char *
8+get_user_label (const IndicatorSessionUser * user)
9+{
10+ const char * c;
11+
12+ /* If blank or whitespace, use username instead */
13+ for (c = user->real_name; *c != '\0' && g_ascii_isspace (*c); c++);
14+ if (*c == '\0')
15+ return user->user_name;
16+
17+ return user->real_name;
18+}
19+
20+static const char *
21 get_current_real_name (IndicatorSessionService * self)
22 {
23 GHashTableIter iter;
24@@ -289,7 +302,7 @@
25 {
26 IndicatorSessionUser * user = value;
27 if (user->is_current_user)
28- return user->real_name;
29+ return get_user_label (user);
30 }
31
32 return "";
33@@ -435,7 +448,7 @@
34 const IndicatorSessionUser * a = *(const IndicatorSessionUser**)ga;
35 const IndicatorSessionUser * b = *(const IndicatorSessionUser**)gb;
36
37- if ((i = g_strcmp0 (a->real_name, b->real_name)))
38+ if ((i = g_strcmp0 (get_user_label (a), get_user_label (b))))
39 return i;
40
41 return g_strcmp0 (a->user_name, b->user_name);
42@@ -536,7 +549,7 @@
43 const IndicatorSessionUser * u = g_ptr_array_index (users, i);
44 GVariant * serialized_icon;
45
46- item = g_menu_item_new (u->real_name, NULL);
47+ item = g_menu_item_new (get_user_label (u), NULL);
48 g_menu_item_set_action_and_target (item, "indicator.switch-to-user", "s", u->user_name);
49 g_menu_item_set_attribute (item, "x-canonical-type", "s", "indicator.user-menu-item");
50
51
52=== modified file 'tests/test-service.cc'
53--- tests/test-service.cc 2013-11-08 19:06:20 +0000
54+++ tests/test-service.cc 2013-12-19 03:06:21 +0000
55@@ -819,3 +819,33 @@
56 wait_for_signal (mock_settings, "changed::last-command");
57 check_last_command_is ("switch-to-user::tbaker");
58 }
59+
60+TEST_F (ServiceTest, UserLabels)
61+{
62+ int pos = 0;
63+ GMenuModel * switch_menu = 0;
64+
65+ // Check label uses username when real name is blank
66+ IndicatorSessionUser * u = g_new0 (IndicatorSessionUser, 1);
67+ u->uid = 100;
68+ u->user_name = g_strdup ("blank");
69+ u->real_name = g_strdup ("");
70+ indicator_session_users_mock_add_user (INDICATOR_SESSION_USERS_MOCK(mock_users), u);
71+ wait_for_menu_resync ();
72+ ASSERT_TRUE (find_menu_item_for_action ("indicator.switch-to-screensaver", &switch_menu, &pos));
73+ check_label ("blank", switch_menu, 2);
74+ g_clear_object (&switch_menu);
75+ indicator_session_users_mock_remove_user (INDICATOR_SESSION_USERS_MOCK(mock_users), 100);
76+
77+ // Check label uses username when real name is all whitespace
78+ u = g_new0 (IndicatorSessionUser, 1);
79+ u->uid = 100;
80+ u->user_name = g_strdup ("whitespace");
81+ u->real_name = g_strdup (" ");
82+ indicator_session_users_mock_add_user (INDICATOR_SESSION_USERS_MOCK(mock_users), u);
83+ wait_for_menu_resync ();
84+ ASSERT_TRUE (find_menu_item_for_action ("indicator.switch-to-screensaver", &switch_menu, &pos));
85+ check_label ("whitespace", switch_menu, 2);
86+ g_clear_object (&switch_menu);
87+ indicator_session_users_mock_remove_user (INDICATOR_SESSION_USERS_MOCK(mock_users), 100);
88+}

Subscribers

People subscribed via source and target branches