Simplify client code that reads the user preferences

Use

  Gerrit.getUserPeferences()

instead of

  Gerrit.getUserAccount().getGeneralPreferences()

The first one returns the client side representation of the user
settings (AccountPreferencesInfo) while the second one returns the
entity class for persisting the user preferences in the database
(AccountGeneralPreferences).

Gerrit.getUserPreferences() never returns null. If the user is not
signed in the default preferences are returned. Hence we no longer
need to check with Gerrit.isSignedIn() whether the user is signed in
whenever we access the user preferences.

Change-Id: I6420c221f66299485d0cd913a55563b0cd2a9b11
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2015-07-23 10:46:03 +02:00
parent 6a56672b72
commit 042c394f80
13 changed files with 45 additions and 118 deletions

View File

@@ -59,7 +59,11 @@ public class AccountPreferencesInfo extends JavaScriptObject {
} }
public final short changesPerPage() { public final short changesPerPage() {
return get("changes_per_page", AccountGeneralPreferences.DEFAULT_PAGESIZE); short changesPerPage =
get("changes_per_page", AccountGeneralPreferences.DEFAULT_PAGESIZE);
return 0 < changesPerPage
? changesPerPage
: AccountGeneralPreferences.DEFAULT_PAGESIZE;
} }
private final native short get(String n, int d) private final native short get(String n, int d)
/*-{ return this.hasOwnProperty(n) ? this[n] : d }-*/; /*-{ return this.hasOwnProperty(n) ? this[n] : d }-*/;

View File

@@ -625,10 +625,7 @@ public class Dispatcher {
} }
private static boolean preferUnified() { private static boolean preferUnified() {
return Gerrit.isSignedIn() return DiffView.UNIFIED_DIFF.equals(Gerrit.getUserPreferences().diffView());
&& DiffView.UNIFIED_DIFF.equals(Gerrit.getUserAccount()
.getGeneralPreferences()
.getDiffView());
} }
private static void unified(final String token, private static void unified(final String token,

View File

@@ -274,9 +274,7 @@ public class Gerrit implements EntryPoint {
public static void setHeaderVisible(boolean visible) { public static void setHeaderVisible(boolean visible) {
topMenu.setVisible(visible); topMenu.setVisible(visible);
siteHeader.setVisible(visible && (myAccount != null siteHeader.setVisible(visible && getUserPreferences().showSiteHeader());
? myAccount.getGeneralPreferences().isShowSiteHeader()
: true));
} }
public static boolean isHeaderVisible() { public static boolean isHeaderVisible() {

View File

@@ -22,7 +22,6 @@ import com.google.gerrit.client.rpc.GerritCallback;
import com.google.gerrit.client.ui.Hyperlink; import com.google.gerrit.client.ui.Hyperlink;
import com.google.gerrit.client.ui.Screen; import com.google.gerrit.client.ui.Screen;
import com.google.gerrit.common.PageLinks; import com.google.gerrit.common.PageLinks;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent; import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler; import com.google.gwt.event.dom.client.KeyUpHandler;
@@ -44,7 +43,7 @@ public class GroupListScreen extends Screen {
public GroupListScreen() { public GroupListScreen() {
setRequiresSignIn(true); setRequiresSignIn(true);
configurePageSize(); pageSize = Gerrit.getUserPreferences().changesPerPage();
} }
public GroupListScreen(String params) { public GroupListScreen(String params) {
@@ -65,17 +64,6 @@ public class GroupListScreen extends Screen {
} }
} }
private void configurePageSize() {
if (Gerrit.isSignedIn()) {
final AccountGeneralPreferences p =
Gerrit.getUserAccount().getGeneralPreferences();
final short m = p.getMaximumPageSize();
pageSize = 0 < m ? m : AccountGeneralPreferences.DEFAULT_PAGESIZE;
} else {
pageSize = AccountGeneralPreferences.DEFAULT_PAGESIZE;
}
}
@Override @Override
protected void onLoad() { protected void onLoad() {
super.onLoad(); super.onLoad();

View File

@@ -38,7 +38,6 @@ import com.google.gerrit.client.ui.Hyperlink;
import com.google.gerrit.client.ui.NavigationTable; import com.google.gerrit.client.ui.NavigationTable;
import com.google.gerrit.client.ui.OnEditEnabler; import com.google.gerrit.client.ui.OnEditEnabler;
import com.google.gerrit.common.PageLinks; import com.google.gerrit.common.PageLinks;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gerrit.reviewdb.client.Branch; import com.google.gerrit.reviewdb.client.Branch;
import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.client.Project;
import com.google.gerrit.reviewdb.client.RefNames; import com.google.gerrit.reviewdb.client.RefNames;
@@ -92,18 +91,7 @@ public class ProjectBranchesScreen extends ProjectScreen {
public ProjectBranchesScreen(final Project.NameKey toShow) { public ProjectBranchesScreen(final Project.NameKey toShow) {
super(toShow); super(toShow);
configurePageSize(); pageSize = Gerrit.getUserPreferences().changesPerPage();
}
private void configurePageSize() {
if (Gerrit.isSignedIn()) {
AccountGeneralPreferences p =
Gerrit.getUserAccount().getGeneralPreferences();
short m = p.getMaximumPageSize();
pageSize = 0 < m ? m : AccountGeneralPreferences.DEFAULT_PAGESIZE;
} else {
pageSize = AccountGeneralPreferences.DEFAULT_PAGESIZE;
}
} }
private void parseToken() { private void parseToken() {

View File

@@ -30,7 +30,6 @@ import com.google.gerrit.client.ui.ProjectSearchLink;
import com.google.gerrit.client.ui.ProjectsTable; import com.google.gerrit.client.ui.ProjectsTable;
import com.google.gerrit.client.ui.Screen; import com.google.gerrit.client.ui.Screen;
import com.google.gerrit.common.PageLinks; import com.google.gerrit.common.PageLinks;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gwt.event.dom.client.KeyCodes; import com.google.gwt.event.dom.client.KeyCodes;
import com.google.gwt.event.dom.client.KeyUpEvent; import com.google.gwt.event.dom.client.KeyUpEvent;
import com.google.gwt.event.dom.client.KeyUpHandler; import com.google.gwt.event.dom.client.KeyUpHandler;
@@ -57,10 +56,11 @@ public class ProjectListScreen extends Screen {
private Query query; private Query query;
public ProjectListScreen() { public ProjectListScreen() {
configurePageSize(); pageSize = Gerrit.getUserPreferences().changesPerPage();
} }
public ProjectListScreen(String params) { public ProjectListScreen(String params) {
this();
for (String kvPair : params.split("[,;&]")) { for (String kvPair : params.split("[,;&]")) {
String[] kv = kvPair.split("=", 2); String[] kv = kvPair.split("=", 2);
if (kv.length != 2 || kv[0].isEmpty()) { if (kv.length != 2 || kv[0].isEmpty()) {
@@ -75,18 +75,6 @@ public class ProjectListScreen extends Screen {
start = Integer.parseInt(URL.decodeQueryString(kv[1])); start = Integer.parseInt(URL.decodeQueryString(kv[1]));
} }
} }
configurePageSize();
}
private void configurePageSize() {
if (Gerrit.isSignedIn()) {
final AccountGeneralPreferences p =
Gerrit.getUserAccount().getGeneralPreferences();
final short m = p.getMaximumPageSize();
pageSize = 0 < m ? m : AccountGeneralPreferences.DEFAULT_PAGESIZE;
} else {
pageSize = AccountGeneralPreferences.DEFAULT_PAGESIZE;
}
} }
@Override @Override

View File

@@ -26,7 +26,6 @@ import com.google.gerrit.client.rpc.NativeMap;
import com.google.gerrit.client.rpc.Natives; import com.google.gerrit.client.rpc.Natives;
import com.google.gerrit.client.rpc.RestApi; import com.google.gerrit.client.rpc.RestApi;
import com.google.gerrit.extensions.client.ListChangesOption; import com.google.gerrit.extensions.client.ListChangesOption;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme; import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme;
import com.google.gerrit.reviewdb.client.PatchSet; import com.google.gerrit.reviewdb.client.PatchSet;
import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JavaScriptObject;
@@ -238,9 +237,7 @@ class DownloadBox extends VerticalPanel {
} }
private static String getUserPreference() { private static String getUserPreference() {
if (Gerrit.isSignedIn()) { DownloadScheme pref = Gerrit.getUserPreferences().downloadScheme();
DownloadScheme pref =
Gerrit.getUserAccount().getGeneralPreferences().getDownloadUrl();
if (pref != null) { if (pref != null) {
switch (pref) { switch (pref) {
case ANON_GIT: case ANON_GIT:
@@ -257,18 +254,15 @@ class DownloadBox extends VerticalPanel {
return null; return null;
} }
} }
}
return null; return null;
} }
private void saveScheme() { private void saveScheme() {
DownloadScheme scheme = getSelectedScheme(); DownloadScheme scheme = getSelectedScheme();
AccountGeneralPreferences pref = AccountPreferencesInfo prefs = Gerrit.getUserPreferences();
Gerrit.getUserAccount().getGeneralPreferences();
if (Gerrit.isSignedIn() && scheme != null if (Gerrit.isSignedIn() && scheme != null
&& scheme != pref.getDownloadUrl()) { && scheme != prefs.downloadScheme()) {
pref.setDownloadUrl(scheme); prefs.downloadScheme(scheme);
AccountPreferencesInfo in = AccountPreferencesInfo.create(); AccountPreferencesInfo in = AccountPreferencesInfo.create();
in.downloadScheme(scheme); in.downloadScheme(scheme);
AccountApi.self().view("preferences") AccountApi.self().view("preferences")

View File

@@ -471,8 +471,8 @@ public class FileTable extends FlowPanel {
this.comments = comments; this.comments = comments;
this.drafts = drafts; this.drafts = drafts;
this.hasUser = Gerrit.isSignedIn(); this.hasUser = Gerrit.isSignedIn();
this.showChangeSizeBars = !hasUser this.showChangeSizeBars =
|| Gerrit.getUserAccount().getGeneralPreferences().isSizeBarInChangeTable(); Gerrit.getUserPreferences().sizeBarInChangeTable();
myTable.addStyleName(R.css().table()); myTable.addStyleName(R.css().table());
} }
@@ -648,8 +648,7 @@ public class FileTable extends FlowPanel {
if (Patch.COMMIT_MSG.equals(path)) { if (Patch.COMMIT_MSG.equals(path)) {
sb.append(Util.C.commitMessage()); sb.append(Util.C.commitMessage());
} else if (!hasUser || Gerrit.getUserAccount().getGeneralPreferences() } else if (Gerrit.getUserPreferences().muteCommonPathPrefixes()) {
.isMuteCommonPathPrefixes()) {
int commonPrefixLen = commonPrefix(path); int commonPrefixLen = commonPrefix(path);
if (commonPrefixLen > 0) { if (commonPrefixLen > 0) {
sb.openSpan().setStyleName(R.css().commonPrefix()) sb.openSpan().setStyleName(R.css().commonPrefix())

View File

@@ -71,19 +71,17 @@ public class ChangeTable extends NavigationTable<ChangeInfo> {
private final List<Section> sections; private final List<Section> sections;
private int columns; private int columns;
private boolean showLegacyId; private final boolean showLegacyId;
private List<String> labelNames; private List<String> labelNames;
public ChangeTable() { public ChangeTable() {
super(Util.C.changeItemHelp()); super(Util.C.changeItemHelp());
columns = BASE_COLUMNS; columns = BASE_COLUMNS;
labelNames = Collections.emptyList(); labelNames = Collections.emptyList();
showLegacyId = Gerrit.getUserPreferences().legacycidInChangeTable();
if (Gerrit.isSignedIn()) { if (Gerrit.isSignedIn()) {
keysAction.add(new StarKeyCommand(0, 's', Util.C.changeTableStar())); keysAction.add(new StarKeyCommand(0, 's', Util.C.changeTableStar()));
showLegacyId = Gerrit.getUserAccount()
.getGeneralPreferences()
.isLegacycidInChangeTable();
} }
sections = new ArrayList<>(); sections = new ArrayList<>();
@@ -242,18 +240,14 @@ public class ChangeTable extends NavigationTable<ChangeInfo> {
table.setWidget(row, C_PROJECT, new ProjectLink(c.projectNameKey())); table.setWidget(row, C_PROJECT, new ProjectLink(c.projectNameKey()));
table.setWidget(row, C_BRANCH, new BranchLink(c.projectNameKey(), c table.setWidget(row, C_BRANCH, new BranchLink(c.projectNameKey(), c
.status(), c.branch(), c.topic())); .status(), c.branch(), c.topic()));
if (Gerrit.isSignedIn() if (Gerrit.getUserPreferences().relativeDateInChangeTable()) {
&& Gerrit.getUserAccount().getGeneralPreferences()
.isRelativeDateInChangeTable()) {
table.setText(row, C_LAST_UPDATE, relativeFormat(c.updated())); table.setText(row, C_LAST_UPDATE, relativeFormat(c.updated()));
} else { } else {
table.setText(row, C_LAST_UPDATE, shortFormat(c.updated())); table.setText(row, C_LAST_UPDATE, shortFormat(c.updated()));
} }
int col = C_SIZE; int col = C_SIZE;
if (Gerrit.isSignedIn() if (!Gerrit.getUserPreferences().sizeBarInChangeTable()) {
&& !Gerrit.getUserAccount().getGeneralPreferences()
.isSizeBarInChangeTable()) {
table.setText(row, col, table.setText(row, col,
Util.M.insertionsAndDeletions(c.insertions(), c.deletions())); Util.M.insertionsAndDeletions(c.insertions(), c.deletions()));
} else { } else {
@@ -275,10 +269,8 @@ public class ChangeTable extends NavigationTable<ChangeInfo> {
String user; String user;
String info; String info;
ReviewCategoryStrategy reviewCategoryStrategy = Gerrit.isSignedIn() ReviewCategoryStrategy reviewCategoryStrategy =
? Gerrit.getUserAccount().getGeneralPreferences() Gerrit.getUserPreferences().reviewCategoryStrategy();
.getReviewCategoryStrategy()
: ReviewCategoryStrategy.NONE;
if (label.rejected() != null) { if (label.rejected() != null) {
user = label.rejected().name(); user = label.rejected().name();
info = getReviewCategoryDisplayInfo(reviewCategoryStrategy, info = getReviewCategoryDisplayInfo(reviewCategoryStrategy,

View File

@@ -18,7 +18,6 @@ import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.rpc.ScreenLoadCallback; import com.google.gerrit.client.rpc.ScreenLoadCallback;
import com.google.gerrit.client.ui.Hyperlink; import com.google.gerrit.client.ui.Hyperlink;
import com.google.gerrit.client.ui.Screen; import com.google.gerrit.client.ui.Screen;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gwt.event.dom.client.KeyPressEvent; import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.user.client.History; import com.google.gwt.user.client.History;
import com.google.gwt.user.client.rpc.AsyncCallback; import com.google.gwt.user.client.rpc.AsyncCallback;
@@ -39,15 +38,7 @@ public abstract class PagedSingleListScreen extends Screen {
protected PagedSingleListScreen(String anchorToken, int start) { protected PagedSingleListScreen(String anchorToken, int start) {
anchorPrefix = anchorToken; anchorPrefix = anchorToken;
this.start = start; this.start = start;
pageSize = Gerrit.getUserPreferences().changesPerPage();
if (Gerrit.isSignedIn()) {
final AccountGeneralPreferences p =
Gerrit.getUserAccount().getGeneralPreferences();
final short m = p.getMaximumPageSize();
pageSize = 0 < m ? m : AccountGeneralPreferences.DEFAULT_PAGESIZE;
} else {
pageSize = AccountGeneralPreferences.DEFAULT_PAGESIZE;
}
} }
@Override @Override

View File

@@ -17,7 +17,6 @@ package com.google.gerrit.client.download;
import com.google.gerrit.client.Gerrit; import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.config.DownloadInfo.DownloadCommandInfo; import com.google.gerrit.client.config.DownloadInfo.DownloadCommandInfo;
import com.google.gerrit.client.config.DownloadInfo.DownloadSchemeInfo; import com.google.gerrit.client.config.DownloadInfo.DownloadSchemeInfo;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gwt.user.client.ui.FlowPanel; import com.google.gwt.user.client.ui.FlowPanel;
import com.google.gwt.user.client.ui.InlineLabel; import com.google.gwt.user.client.ui.InlineLabel;
import com.google.gwtexpui.clippy.client.CopyableLabel; import com.google.gwtexpui.clippy.client.CopyableLabel;
@@ -41,14 +40,7 @@ public abstract class DownloadPanel extends FlowPanel {
private void setupWidgets() { private void setupWidgets() {
if (!urls.isEmpty()) { if (!urls.isEmpty()) {
final AccountGeneralPreferences pref; urls.select(Gerrit.getUserPreferences().downloadScheme());
if (Gerrit.isSignedIn()) {
pref = Gerrit.getUserAccount().getGeneralPreferences();
} else {
pref = new AccountGeneralPreferences();
pref.resetToDefaults();
}
urls.select(pref.getDownloadUrl());
FlowPanel p = new FlowPanel(); FlowPanel p = new FlowPanel();
p.setStyleName(Gerrit.RESOURCES.css().downloadLinkHeader()); p.setStyleName(Gerrit.RESOURCES.css().downloadLinkHeader());

View File

@@ -18,7 +18,6 @@ import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.account.AccountApi; import com.google.gerrit.client.account.AccountApi;
import com.google.gerrit.client.config.DownloadInfo.DownloadSchemeInfo; import com.google.gerrit.client.config.DownloadInfo.DownloadSchemeInfo;
import com.google.gerrit.client.info.AccountPreferencesInfo; import com.google.gerrit.client.info.AccountPreferencesInfo;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme; import com.google.gerrit.reviewdb.client.AccountGeneralPreferences.DownloadScheme;
import com.google.gwt.aria.client.Roles; import com.google.gwt.aria.client.Roles;
import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JavaScriptObject;
@@ -110,13 +109,10 @@ public class DownloadUrlLink extends Anchor implements ClickHandler {
select(); select();
AccountGeneralPreferences pref = AccountPreferencesInfo prefs = Gerrit.getUserPreferences();
Gerrit.getUserAccount().getGeneralPreferences();
if (Gerrit.isSignedIn() && scheme != null if (Gerrit.isSignedIn() && scheme != null
&& scheme != pref.getDownloadUrl()) { && scheme != prefs.downloadScheme()) {
// If the user is signed-in, remember this choice for future panels. prefs.downloadScheme(scheme);
//
pref.setDownloadUrl(scheme);
AccountPreferencesInfo in = AccountPreferencesInfo.create(); AccountPreferencesInfo in = AccountPreferencesInfo.create();
in.downloadScheme(scheme); in.downloadScheme(scheme);
AccountApi.self().view("preferences") AccountApi.self().view("preferences")

View File

@@ -262,7 +262,7 @@ class PatchTable extends Composite {
private static boolean isUnifiedPatchLink(final Patch patch) { private static boolean isUnifiedPatchLink(final Patch patch) {
return (patch.getPatchType().equals(PatchType.BINARY) return (patch.getPatchType().equals(PatchType.BINARY)
|| (Gerrit.isSignedIn() || (Gerrit.isSignedIn()
&& Gerrit.getUserAccount().getGeneralPreferences().getDiffView() && Gerrit.getUserPreferences().diffView()
.equals(DiffView.UNIFIED_DIFF))); .equals(DiffView.UNIFIED_DIFF)));
} }