Make user preferences accessible by plugins

Add methods to the JavaScript API that allows plugins to get and
refresh the preferences of the currently signed in user.

Change-Id: Icabaf8188ccdd19b877856a23d292493cd50a215
Signed-off-by: Edwin Kempin <edwin.kempin@sap.com>
This commit is contained in:
Edwin Kempin
2015-07-22 15:01:59 +02:00
parent ebbad5ecab
commit 936d1ed8d1
8 changed files with 115 additions and 74 deletions

View File

@@ -65,6 +65,15 @@ self.get(url, callback)
Returns the currently signed in user's AccountInfo data; empty account Returns the currently signed in user's AccountInfo data; empty account
data if no user is currently signed in. data if no user is currently signed in.
[[Gerrit_getUserPreferences]]
=== Gerrit.getUserPreferences()
Returns the preferences of the currently signed in user; the default
preferences if no user is currently signed in.
[[Gerrit_refreshUserPreferences]]
=== Gerrit.refreshUserPreferences()
Refreshes the preferences of the current user.
[[self_getPluginName]] [[self_getPluginName]]
=== self.getPluginName() === self.getPluginName()
Returns the name this plugin was installed as by the server Returns the name this plugin was installed as by the server

View File

@@ -30,9 +30,6 @@ public class AccountPreferencesInfo extends JavaScriptObject {
public static AccountPreferencesInfo create(AccountGeneralPreferences in, public static AccountPreferencesInfo create(AccountGeneralPreferences in,
List<TopMenuItem> myMenus) { List<TopMenuItem> myMenus) {
AccountPreferencesInfo p = createObject().cast(); AccountPreferencesInfo p = createObject().cast();
if (in == null) {
in = AccountGeneralPreferences.createDefault();
}
p.changesPerPage(in.getMaximumPageSize()); p.changesPerPage(in.getMaximumPageSize());
p.showSiteHeader(in.isShowSiteHeader()); p.showSiteHeader(in.isShowSiteHeader());
p.useFlashClipboard(in.isUseFlashClipboard()); p.useFlashClipboard(in.isUseFlashClipboard());
@@ -47,10 +44,16 @@ public class AccountPreferencesInfo extends JavaScriptObject {
p.muteCommonPathPrefixes(in.isMuteCommonPathPrefixes()); p.muteCommonPathPrefixes(in.isMuteCommonPathPrefixes());
p.reviewCategoryStrategy(in.getReviewCategoryStrategy()); p.reviewCategoryStrategy(in.getReviewCategoryStrategy());
p.diffView(in.getDiffView()); p.diffView(in.getDiffView());
p.setMyMenus(myMenus); if (myMenus != null) {
p.setMyMenus(myMenus);
}
return p; return p;
} }
public static AccountPreferencesInfo createDefault() {
return create(AccountGeneralPreferences.createDefault(), null);
}
public final short changesPerPage() { public final short changesPerPage() {
return get("changes_per_page", AccountGeneralPreferences.DEFAULT_PAGESIZE); return get("changes_per_page", AccountGeneralPreferences.DEFAULT_PAGESIZE);
} }

View File

@@ -15,8 +15,8 @@
package com.google.gerrit.client; package com.google.gerrit.client;
import com.google.gerrit.client.info.AccountInfo; import com.google.gerrit.client.info.AccountInfo;
import com.google.gerrit.client.info.AccountPreferencesInfo;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gwt.i18n.client.DateTimeFormat; import com.google.gwt.i18n.client.DateTimeFormat;
import java.util.Date; import java.util.Date;
@@ -31,19 +31,10 @@ public class FormatUtil {
private static DateTimeFormat mDate; private static DateTimeFormat mDate;
private static DateTimeFormat dtfmt; private static DateTimeFormat dtfmt;
public static void setPreferences(AccountGeneralPreferences pref) { public static void setPreferences(AccountPreferencesInfo prefs) {
if (pref == null) { String fmt_sTime = prefs.timeFormat().getFormat();
if (Gerrit.isSignedIn()) { String fmt_sDate = prefs.dateFormat().getShortFormat();
pref = Gerrit.getUserAccount().getGeneralPreferences(); String fmt_mDate = prefs.dateFormat().getLongFormat();
} else {
pref = new AccountGeneralPreferences();
pref.resetToDefaults();
}
}
String fmt_sTime = pref.getTimeFormat().getFormat();
String fmt_sDate = pref.getDateFormat().getShortFormat();
String fmt_mDate = pref.getDateFormat().getLongFormat();
sTime = DateTimeFormat.getFormat(fmt_sTime); sTime = DateTimeFormat.getFormat(fmt_sTime);
sDate = DateTimeFormat.getFormat(fmt_sDate); sDate = DateTimeFormat.getFormat(fmt_sDate);
@@ -115,7 +106,7 @@ public class FormatUtil {
private static void ensureInited() { private static void ensureInited() {
if (dtfmt == null) { if (dtfmt == null) {
setPreferences(null); setPreferences(Gerrit.getUserPreferences());
} }
} }

View File

@@ -49,7 +49,6 @@ import com.google.gerrit.common.data.SystemInfoService;
import com.google.gerrit.extensions.client.GerritTopMenu; import com.google.gerrit.extensions.client.GerritTopMenu;
import com.google.gerrit.reviewdb.client.Account; import com.google.gerrit.reviewdb.client.Account;
import com.google.gerrit.reviewdb.client.AccountDiffPreference; import com.google.gerrit.reviewdb.client.AccountDiffPreference;
import com.google.gerrit.reviewdb.client.AccountGeneralPreferences;
import com.google.gerrit.reviewdb.client.Project; import com.google.gerrit.reviewdb.client.Project;
import com.google.gwt.aria.client.Roles; import com.google.gwt.aria.client.Roles;
import com.google.gwt.core.client.EntryPoint; import com.google.gwt.core.client.EntryPoint;
@@ -112,6 +111,7 @@ public class Gerrit implements EntryPoint {
private static String myHost; private static String myHost;
private static ServerInfo myServerInfo; private static ServerInfo myServerInfo;
private static AccountPreferencesInfo myPrefs;
private static boolean hasDocumentation; private static boolean hasDocumentation;
private static String docUrl; private static String docUrl;
private static HostPageData.Theme myTheme; private static HostPageData.Theme myTheme;
@@ -316,6 +316,11 @@ public class Gerrit implements EntryPoint {
return xGerritAuth; return xGerritAuth;
} }
/** @return the preferences of the currently signed in user, the default preferences if not signed in */
public static AccountPreferencesInfo getUserPreferences() {
return myPrefs;
}
/** @return the currently signed in users's diff preferences; null if no diff preferences defined for the account */ /** @return the currently signed in users's diff preferences; null if no diff preferences defined for the account */
public static AccountDiffPreference getAccountDiffPreference() { public static AccountDiffPreference getAccountDiffPreference() {
return myAccountDiffPref; return myAccountDiffPref;
@@ -388,6 +393,7 @@ public class Gerrit implements EntryPoint {
static void deleteSessionCookie() { static void deleteSessionCookie() {
myAccount = null; myAccount = null;
myAccountDiffPref = null; myAccountDiffPref = null;
myPrefs = AccountPreferencesInfo.createDefault();
xGerritAuth = null; xGerritAuth = null;
refreshMenuBar(); refreshMenuBar();
@@ -462,9 +468,20 @@ public class Gerrit implements EntryPoint {
} }
if (result.accountDiffPref != null) { if (result.accountDiffPref != null) {
myAccountDiffPref = result.accountDiffPref; myAccountDiffPref = result.accountDiffPref;
applyUserPreferences();
} }
onModuleLoad2(result); if (isSignedIn()) {
AccountApi.self().view("preferences")
.get(new GerritCallback<AccountPreferencesInfo>() {
@Override
public void onSuccess(AccountPreferencesInfo prefs) {
myPrefs = prefs;
onModuleLoad2(result);
}
});
} else {
myPrefs = AccountPreferencesInfo.createDefault();
onModuleLoad2(result);
}
} }
})); }));
} }
@@ -581,7 +598,7 @@ public class Gerrit implements EntryPoint {
applyUserPreferences(); applyUserPreferences();
populateBottomMenu(bottomMenu, hpd); populateBottomMenu(bottomMenu, hpd);
refreshMenuBar(false); refreshMenuBar();
History.addValueChangeHandler(new ValueChangeHandler<String>() { History.addValueChangeHandler(new ValueChangeHandler<String>() {
@Override @Override
@@ -595,13 +612,9 @@ public class Gerrit implements EntryPoint {
if (hpd.messages != null) { if (hpd.messages != null) {
new MessageOfTheDayBar(hpd.messages).show(); new MessageOfTheDayBar(hpd.messages).show();
} }
CallbackGroup cbg = new CallbackGroup();
if (isSignedIn()) {
AccountApi.self().view("preferences").get(cbg.add(createMyMenuBarCallback()));
}
PluginLoader.load(hpd.plugins, PluginLoader.load(hpd.plugins,
hpd.pluginsLoadTimeout, hpd.pluginsLoadTimeout,
cbg.addFinal(new GerritCallback<VoidResult>() { new GerritCallback<VoidResult>() {
@Override @Override
public void onSuccess(VoidResult result) { public void onSuccess(VoidResult result) {
String token = History.getToken(); String token = History.getToken();
@@ -612,7 +625,7 @@ public class Gerrit implements EntryPoint {
} }
display(token); display(token);
} }
})); });
} }
private void saveDefaultTheme() { private void saveDefaultTheme() {
@@ -622,10 +635,6 @@ public class Gerrit implements EntryPoint {
} }
public static void refreshMenuBar() { public static void refreshMenuBar() {
refreshMenuBar(true);
}
private static void refreshMenuBar(boolean populateMyMenu) {
menuLeft.clear(); menuLeft.clear();
menuRight.clear(); menuRight.clear();
@@ -645,9 +654,22 @@ public class Gerrit implements EntryPoint {
if (signedIn) { if (signedIn) {
LinkMenuBar myBar = new LinkMenuBar(); LinkMenuBar myBar = new LinkMenuBar();
menuBars.put(GerritTopMenu.MY.menuName, myBar); menuBars.put(GerritTopMenu.MY.menuName, myBar);
if (populateMyMenu) {
AccountApi.self().view("preferences").get(createMyMenuBarCallback()); if (myPrefs.my() != null) {
myBar.clear();
String url = null;
List<TopMenuItem> myMenuItems = Natives.asList(myPrefs.my());
if (!myMenuItems.isEmpty()) {
if (myMenuItems.get(0).getUrl().startsWith("#")) {
url = myMenuItems.get(0).getUrl().substring(1);
}
for (TopMenuItem item : myMenuItems) {
addExtensionLink(myBar, item);
}
}
defaultScreenToken = url;
} }
menuLeft.add(myBar, C.menuMine()); menuLeft.add(myBar, C.menuMine());
menuLeft.selectTab(1); menuLeft.selectTab(1);
} else { } else {
@@ -822,6 +844,37 @@ public class Gerrit implements EntryPoint {
}); });
} }
public static void refreshUserPreferences() {
if (isSignedIn()) {
AccountApi.self().view("preferences")
.get(new GerritCallback<AccountPreferencesInfo>() {
@Override
public void onSuccess(AccountPreferencesInfo prefs) {
setUserPreferences(prefs);
}
});
} else {
setUserPreferences(AccountPreferencesInfo.createDefault());
}
}
public static void setUserPreferences(AccountPreferencesInfo prefs) {
myPrefs = prefs;
applyUserPreferences();
refreshMenuBar();
}
private static void applyUserPreferences() {
CopyableLabel.setFlashEnabled(myPrefs.useFlashClipboard());
if (siteHeader != null) {
siteHeader.setVisible(myPrefs.showSiteHeader());
}
if (siteFooter != null) {
siteFooter.setVisible(myPrefs.showSiteHeader());
}
FormatUtil.setPreferences(myPrefs);
}
private static void getDocIndex(final AsyncCallback<DocInfo> cb) { private static void getDocIndex(final AsyncCallback<DocInfo> cb) {
RequestBuilder req = RequestBuilder req =
new RequestBuilder(RequestBuilder.HEAD, GWT.getHostPageBaseURL() new RequestBuilder(RequestBuilder.HEAD, GWT.getHostPageBaseURL()
@@ -853,41 +906,6 @@ public class Gerrit implements EntryPoint {
} }
} }
private static AsyncCallback<AccountPreferencesInfo> createMyMenuBarCallback() {
return new GerritCallback<AccountPreferencesInfo>() {
@Override
public void onSuccess(AccountPreferencesInfo prefs) {
LinkMenuBar myBar = menuBars.get(GerritTopMenu.MY.menuName);
myBar.clear();
List<TopMenuItem> myMenuItems = Natives.asList(prefs.my());
String url = null;
if (!myMenuItems.isEmpty()) {
if (myMenuItems.get(0).getUrl().startsWith("#")) {
url = myMenuItems.get(0).getUrl().substring(1);
}
for (TopMenuItem item : myMenuItems) {
addExtensionLink(myBar, item);
}
}
defaultScreenToken = url;
}
};
}
public static void applyUserPreferences() {
if (myAccount != null) {
final AccountGeneralPreferences p = myAccount.getGeneralPreferences();
CopyableLabel.setFlashEnabled(p.isUseFlashClipboard());
if (siteHeader != null) {
siteHeader.setVisible(p.isShowSiteHeader());
}
if (siteFooter != null) {
siteFooter.setVisible(p.isShowSiteHeader());
}
FormatUtil.setPreferences(myAccount.getGeneralPreferences());
}
}
private static void whoAmI(boolean canLogOut) { private static void whoAmI(boolean canLogOut) {
AccountInfo account = getUserAccountInfo(); AccountInfo account = getUserAccountInfo();
final UserPopupPanel userPopup = final UserPopupPanel userPopup =

View File

@@ -334,7 +334,7 @@ public class MyPreferencesScreen extends SettingsScreen {
} }
private void doSave() { private void doSave() {
final AccountGeneralPreferences p = new AccountGeneralPreferences(); AccountGeneralPreferences p = new AccountGeneralPreferences();
p.setShowSiteHeader(showSiteHeader.getValue()); p.setShowSiteHeader(showSiteHeader.getValue());
p.setUseFlashClipboard(useFlashClipboard.getValue()); p.setUseFlashClipboard(useFlashClipboard.getValue());
p.setCopySelfOnEmails(copySelfOnEmails.getValue()); p.setCopySelfOnEmails(copySelfOnEmails.getValue());
@@ -369,11 +369,9 @@ public class MyPreferencesScreen extends SettingsScreen {
new GerritCallback<AccountPreferencesInfo>() { new GerritCallback<AccountPreferencesInfo>() {
@Override @Override
public void onSuccess(AccountPreferencesInfo prefs) { public void onSuccess(AccountPreferencesInfo prefs) {
Gerrit.getUserAccount().setGeneralPreferences(p); Gerrit.setUserPreferences(prefs);
Gerrit.applyUserPreferences();
enable(true); enable(true);
display(prefs); display(prefs);
Gerrit.refreshMenuBar();
} }
@Override @Override

View File

@@ -17,6 +17,7 @@ package com.google.gerrit.client.api;
import com.google.gerrit.client.ErrorDialog; import com.google.gerrit.client.ErrorDialog;
import com.google.gerrit.client.Gerrit; import com.google.gerrit.client.Gerrit;
import com.google.gerrit.client.info.AccountInfo; import com.google.gerrit.client.info.AccountInfo;
import com.google.gerrit.client.info.AccountPreferencesInfo;
import com.google.gwt.core.client.JavaScriptObject; import com.google.gwt.core.client.JavaScriptObject;
import com.google.gwt.core.client.JsArray; import com.google.gwt.core.client.JsArray;
import com.google.gwt.user.client.History; import com.google.gwt.user.client.History;
@@ -72,6 +73,8 @@ public class ApiGlue {
isSignedIn: @com.google.gerrit.client.api.ApiGlue::isSignedIn(), isSignedIn: @com.google.gerrit.client.api.ApiGlue::isSignedIn(),
showError: @com.google.gerrit.client.api.ApiGlue::showError(Ljava/lang/String;), showError: @com.google.gerrit.client.api.ApiGlue::showError(Ljava/lang/String;),
getCurrentUser: @com.google.gerrit.client.api.ApiGlue::getCurrentUser(), getCurrentUser: @com.google.gerrit.client.api.ApiGlue::getCurrentUser(),
getUserPreferences: @com.google.gerrit.client.api.ApiGlue::getUserPreferences(),
refreshUserPreferences: @com.google.gerrit.client.api.ApiGlue::refreshUserPreferences(),
on: function (e,f){(this.events[e] || (this.events[e]=[])).push(f)}, on: function (e,f){(this.events[e] || (this.events[e]=[])).push(f)},
onAction: function (t,n,c){this._onAction(this.getPluginName(),t,n,c)}, onAction: function (t,n,c){this._onAction(this.getPluginName(),t,n,c)},
@@ -255,6 +258,14 @@ public class ApiGlue {
return Gerrit.getUserAccountInfo(); return Gerrit.getUserAccountInfo();
} }
private static final AccountPreferencesInfo getUserPreferences() {
return Gerrit.getUserPreferences();
}
private static final void refreshUserPreferences() {
Gerrit.refreshUserPreferences();
}
private static final void refreshMenuBar() { private static final void refreshMenuBar() {
Gerrit.refreshMenuBar(); Gerrit.refreshMenuBar();
} }

View File

@@ -51,6 +51,8 @@ final class Plugin extends JavaScriptObject {
@com.google.gerrit.client.api.Plugin::TYPE.prototype = { @com.google.gerrit.client.api.Plugin::TYPE.prototype = {
getPluginName: function(){return this.name}, getPluginName: function(){return this.name},
getCurrentUser: @com.google.gerrit.client.api.ApiGlue::getCurrentUser(), getCurrentUser: @com.google.gerrit.client.api.ApiGlue::getCurrentUser(),
getUserPreferences: @com.google.gerrit.client.api.ApiGlue::getUserPreferences(),
refreshUserPreferences: @com.google.gerrit.client.api.ApiGlue::refreshUserPreferences(),
go: @com.google.gerrit.client.api.ApiGlue::go(Ljava/lang/String;), go: @com.google.gerrit.client.api.ApiGlue::go(Ljava/lang/String;),
refresh: @com.google.gerrit.client.api.ApiGlue::refresh(), refresh: @com.google.gerrit.client.api.ApiGlue::refresh(),
refreshMenuBar: @com.google.gerrit.client.api.ApiGlue::refreshMenuBar(), refreshMenuBar: @com.google.gerrit.client.api.ApiGlue::refreshMenuBar(),

View File

@@ -16,6 +16,7 @@ package com.google.gerrit.plugin.client;
import com.google.gerrit.client.GerritUiExtensionPoint; import com.google.gerrit.client.GerritUiExtensionPoint;
import com.google.gerrit.client.info.AccountInfo; import com.google.gerrit.client.info.AccountInfo;
import com.google.gerrit.client.info.AccountPreferencesInfo;
import com.google.gerrit.plugin.client.extension.Panel; import com.google.gerrit.plugin.client.extension.Panel;
import com.google.gerrit.plugin.client.screen.Screen; import com.google.gerrit.plugin.client.screen.Screen;
import com.google.gwt.core.client.GWT; import com.google.gwt.core.client.GWT;
@@ -57,6 +58,14 @@ public final class Plugin extends JavaScriptObject {
public final native void refreshMenuBar() public final native void refreshMenuBar()
/*-{ return this.refreshMenuBar() }-*/; /*-{ return this.refreshMenuBar() }-*/;
/** @return the preferences of the currently signed in user, the default preferences if not signed in */
public final native AccountPreferencesInfo getUserPreferences()
/*-{ return this.getUserPreferences() }-*/;
/** Refresh the user preferences of the current user. */
public final native void refreshUserPreferences()
/*-{ return this.refreshUserPreferences() }-*/;
/** @return the current user */ /** @return the current user */
public final native AccountInfo getCurrentUser() public final native AccountInfo getCurrentUser()
/*-{ return this.getCurrentUser() }-*/; /*-{ return this.getCurrentUser() }-*/;