From c9921bd3b0b7bf90b6e5e2a52141c3fdf0f31f89 Mon Sep 17 00:00:00 2001 From: James Moger Date: Wed, 4 Jun 2014 17:04:17 -0400 Subject: [PATCH] Revise the user profile page and add a preferences form --- .../gitblit/wicket/GitBlitWebApp.properties | 9 +- .../com/gitblit/wicket/pages/UserPage.html | 51 ++++-- .../com/gitblit/wicket/pages/UserPage.java | 153 ++++++++++++++++-- .../wicket/panels/AccessPolicyPanel.html | 2 +- .../wicket/panels/BooleanChoiceOption.html | 2 +- .../gitblit/wicket/panels/BooleanOption.html | 2 +- .../gitblit/wicket/panels/ChoiceOption.html | 2 +- .../com/gitblit/wicket/panels/TextOption.html | 2 +- 8 files changed, 185 insertions(+), 38 deletions(-) diff --git a/src/main/java/com/gitblit/wicket/GitBlitWebApp.properties b/src/main/java/com/gitblit/wicket/GitBlitWebApp.properties index d6fd57e9..81171799 100644 --- a/src/main/java/com/gitblit/wicket/GitBlitWebApp.properties +++ b/src/main/java/com/gitblit/wicket/GitBlitWebApp.properties @@ -722,4 +722,11 @@ gb.originDescription = The url from which this repository was cloned. gb.gc = GC gb.garbageCollection = Garbage Collection gb.garbageCollectionDescription = The garbage collector will pack loose objects pushed from clients and will remove unreferenced objects from the repository. -gb.commitMessageRendererDescription = Commit messages can be displayed as plaintext or as rendered markup. \ No newline at end of file +gb.commitMessageRendererDescription = Commit messages can be displayed as plaintext or as rendered markup. +gb.preferences = preferences +gb.accountPreferences = Account Preferences +gb.accountPreferencesDescription = Specify your account preferences +gb.languagePreference = Language Preference +gb.languagePreferenceDescription = Select your preferred translation for the Gitblit UI +gb.displayNameDescription = The preferred name for display +gb.emailAddressDescription = The primary email address for receiving notifications \ No newline at end of file diff --git a/src/main/java/com/gitblit/wicket/pages/UserPage.html b/src/main/java/com/gitblit/wicket/pages/UserPage.html index 7aaded7a..09267873 100644 --- a/src/main/java/com/gitblit/wicket/pages/UserPage.html +++ b/src/main/java/com/gitblit/wicket/pages/UserPage.html @@ -7,27 +7,19 @@
-
-
-
-
-

-
-
-
+
+
+
- -
- -
+
+ +
+
+
@@ -41,11 +33,36 @@
+ + +
+
+ + +
  • +
    + + +
    +

    +

    +
    + +
    +
    +
    +
    + +
    +
    +
    +
    + \ No newline at end of file diff --git a/src/main/java/com/gitblit/wicket/pages/UserPage.java b/src/main/java/com/gitblit/wicket/pages/UserPage.java index 29b49b33..baad4a06 100644 --- a/src/main/java/com/gitblit/wicket/pages/UserPage.java +++ b/src/main/java/com/gitblit/wicket/pages/UserPage.java @@ -15,19 +15,27 @@ */ package com.gitblit.wicket.pages; +import java.io.Serializable; import java.util.ArrayList; +import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.List; +import java.util.Locale; import org.apache.wicket.PageParameters; +import org.apache.wicket.ajax.AjaxRequestTarget; +import org.apache.wicket.ajax.markup.html.form.AjaxButton; import org.apache.wicket.markup.html.basic.Label; -import org.apache.wicket.markup.html.link.BookmarkablePageLink; +import org.apache.wicket.markup.html.form.Form; +import org.apache.wicket.markup.html.panel.Fragment; import org.apache.wicket.markup.repeater.Item; import org.apache.wicket.markup.repeater.data.DataView; import org.apache.wicket.markup.repeater.data.ListDataProvider; -import org.eclipse.jgit.lib.PersonIdent; +import org.apache.wicket.model.IModel; +import org.apache.wicket.model.Model; +import com.gitblit.GitBlitException; import com.gitblit.Keys; import com.gitblit.models.Menu.ParameterMenuItem; import com.gitblit.models.NavLink; @@ -40,9 +48,10 @@ import com.gitblit.wicket.GitBlitWebApp; import com.gitblit.wicket.GitBlitWebSession; import com.gitblit.wicket.GitblitRedirectException; import com.gitblit.wicket.WicketUtils; -import com.gitblit.wicket.panels.GravatarImage; -import com.gitblit.wicket.panels.LinkPanel; +import com.gitblit.wicket.panels.ChoiceOption; import com.gitblit.wicket.panels.ProjectRepositoryPanel; +import com.gitblit.wicket.panels.TextOption; +import com.gitblit.wicket.panels.UserTitlePanel; public class UserPage extends RootPage { @@ -83,21 +92,18 @@ public class UserPage extends RootPage { user = new UserModel(userName); } - add(new Label("userDisplayName", user.getDisplayName())); - add(new Label("userUsername", user.username)); - LinkPanel email = new LinkPanel("userEmail", null, user.emailAddress, "mailto:#"); - email.setRenderBodyOnly(true); - add(email.setVisible(app().settings().getBoolean(Keys.web.showEmailAddresses, true) && !StringUtils.isEmpty(user.emailAddress))); - PersonIdent person = new PersonIdent(user.getDisplayName(), user.emailAddress == null ? user.getDisplayName() : user.emailAddress); - add(new GravatarImage("gravatar", person, 210)); + add(new UserTitlePanel("userTitlePanel", user, user.username)); UserModel sessionUser = GitBlitWebSession.get().getUser(); - if (sessionUser != null && user.canCreate() && sessionUser.equals(user)) { - // user can create personal repositories - add(new BookmarkablePageLink("newRepository", app().getNewRepositoryPage())); + boolean isMyProfile = sessionUser != null && sessionUser.equals(user); + + if (isMyProfile) { + addPreferences(user); } else { - add(new Label("newRepository").setVisible(false)); + // visiting user + add(new Label("preferencesLink").setVisible(false)); + add(new Label("preferencesTab").setVisible(false)); } List repositories = getRepositories(params); @@ -145,4 +151,121 @@ public class UserPage extends RootPage { navLinks.add(menu); } + + private void addPreferences(UserModel user) { + // add preferences + Form prefs = new Form("prefsForm"); + + List languages = Arrays.asList( + new Language("English","en"), + new Language("Español", "es"), + new Language("Français", "fr"), + new Language("日本語", "ja"), + new Language("한국말", "ko"), + new Language("Nederlands", "nl"), + new Language("Norsk", "no"), + new Language("Język Polski", "pl"), + new Language("Português", "pt_BR"), + new Language("中文", "zh_CN")); + + String lc = user.getPreferences().locale; + if (StringUtils.isEmpty(lc)) { + // user has not specified language preference + // try server default preference + lc = app().settings().getString(Keys.web.forceDefaultLocale, null); + if (StringUtils.isEmpty(lc)) { + // server default language is not configured + // try browser preference + Locale sessionLocale = GitBlitWebSession.get().getLocale(); + if (sessionLocale != null) { + lc = sessionLocale.getLanguage() + "_" + sessionLocale.getCountry(); + } + } + } + Language preferredLanguage = null; + if (!StringUtils.isEmpty(lc)) { + for (Language language : languages) { + if (language.code.equals(lc)) { + // language_COUNTRY match + preferredLanguage = language; + } else if (preferredLanguage != null && lc.startsWith(language.code)) { + // language match, but not COUNTRY match + preferredLanguage = language; + } + } + } + + final IModel displayName = Model.of(user.getDisplayName()); + final IModel emailAddress = Model.of(user.emailAddress == null ? "" : user.emailAddress); + final IModel language = Model.of(preferredLanguage); + + prefs.add(new TextOption("displayName", + getString("gb.displayName"), + getString("gb.displayNameDescription"), + displayName).setVisible(app().authentication().supportsDisplayNameChanges(user))); + + prefs.add(new TextOption("emailAddress", + getString("gb.emailAddress"), + getString("gb.emailAddressDescription"), + emailAddress).setVisible(app().authentication().supportsEmailAddressChanges(user))); + + prefs.add(new ChoiceOption("language", + getString("gb.languagePreference"), + getString("gb.languagePreferenceDescription"), + language, + languages)); + + prefs.add(new AjaxButton("save") { + + private static final long serialVersionUID = 1L; + + @Override + protected void onSubmit(AjaxRequestTarget target, Form form) { + + UserModel user = GitBlitWebSession.get().getUser(); + + user.displayName = displayName.getObject(); + user.emailAddress = emailAddress.getObject(); + + Language lang = language.getObject(); + if (lang != null) { + user.getPreferences().locale = lang.code; + } + + try { + app().gitblit().reviseUser(user.username, user); + + setRedirect(true); + setResponsePage(UserPage.class, WicketUtils.newUsernameParameter(user.username)); + } catch (GitBlitException e) { + // logger.error("Failed to update user " + user.username, e); + // error(getString("gb.failedToUpdateUser"), false); + } + } + }); + + // add the preferences tab + add(new Fragment("preferencesLink", "preferencesLinkFragment", this).setRenderBodyOnly(true)); + Fragment fragment = new Fragment("preferencesTab", "preferencesTabFragment", this); + fragment.add(prefs); + add(fragment.setRenderBodyOnly(true)); + } + + private class Language implements Serializable { + + private static final long serialVersionUID = 1L; + + final String name; + final String code; + + public Language(String name, String code) { + this.name = name; + this.code = code; + } + + @Override + public String toString() { + return name + " (" + code +")"; + } + } } diff --git a/src/main/java/com/gitblit/wicket/panels/AccessPolicyPanel.html b/src/main/java/com/gitblit/wicket/panels/AccessPolicyPanel.html index 87a02068..07050192 100644 --- a/src/main/java/com/gitblit/wicket/panels/AccessPolicyPanel.html +++ b/src/main/java/com/gitblit/wicket/panels/AccessPolicyPanel.html @@ -13,7 +13,7 @@
    - +
    diff --git a/src/main/java/com/gitblit/wicket/panels/BooleanChoiceOption.html b/src/main/java/com/gitblit/wicket/panels/BooleanChoiceOption.html index fb360d12..b1ced8da 100644 --- a/src/main/java/com/gitblit/wicket/panels/BooleanChoiceOption.html +++ b/src/main/java/com/gitblit/wicket/panels/BooleanChoiceOption.html @@ -8,7 +8,7 @@
    - +
    diff --git a/src/main/java/com/gitblit/wicket/panels/ChoiceOption.html b/src/main/java/com/gitblit/wicket/panels/ChoiceOption.html index 8c34c819..e9e48874 100644 --- a/src/main/java/com/gitblit/wicket/panels/ChoiceOption.html +++ b/src/main/java/com/gitblit/wicket/panels/ChoiceOption.html @@ -7,7 +7,7 @@
    -
    +