Skip to content

Commit e4ba631

Browse files
committed
[INTERNAL][UI] Use new XMPPStore API
This patch introduces the usage of the new XMPPStore API. Renaming UI buttons is not done yet as changing UI values is likely to crash the whole STF.
1 parent 19c958c commit e4ba631

File tree

12 files changed

+116
-96
lines changed

12 files changed

+116
-96
lines changed

eclipse/src/saros/ui/actions/ChangeXMPPAccountAction.java

Lines changed: 22 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
import org.eclipse.jface.action.Action;
55
import org.eclipse.jface.action.ActionContributionItem;
66
import org.eclipse.jface.action.IMenuCreator;
7+
import org.eclipse.jface.dialogs.MessageDialog;
78
import org.eclipse.swt.SWT;
89
import org.eclipse.swt.widgets.Control;
910
import org.eclipse.swt.widgets.Menu;
@@ -74,12 +75,23 @@ public ChangeXMPPAccountAction() {
7475
@Override
7576
public void run() {
7677

78+
final XMPPAccount defaultAccount = accountService.getDefaultAccount();
79+
final boolean isEmpty = accountService.isEmpty();
80+
81+
if (defaultAccount == null || isEmpty) {
82+
if (!MessageDialog.openQuestion(
83+
SWTUtils.getShell(),
84+
"Default account missing",
85+
"A default account has not been set yet. Do you want set a default account?")) return;
86+
87+
SWTUtils.runSafeSWTAsync(LOG, this::openPreferences);
88+
return;
89+
}
90+
7791
if (connectionHandler.isConnected()) {
7892
XMPPConnectionSupport.getInstance().disconnect();
7993
} else {
80-
XMPPConnectionSupport.getInstance()
81-
.connect(
82-
accountService.isEmpty() ? null : accountService.getActiveAccount(), true, false);
94+
XMPPConnectionSupport.getInstance().connect(accountService.getDefaultAccount(), true, false);
8395
}
8496
}
8597

@@ -97,12 +109,16 @@ public Menu getMenu(Menu parent) {
97109
public Menu getMenu(Control parent) {
98110
accountMenu = new Menu(parent);
99111

100-
XMPPAccount activeAccount = null;
112+
XMPPAccount defaultAccount = null;
101113

102-
if (connectionHandler.isConnected()) activeAccount = accountService.getActiveAccount();
114+
/* FIXME obtain the current JID and the discard the entry.
115+
* This logic here only works because we set the account that should connect to be the default one.
116+
* If the user is interested in such a behavior is another question.
117+
*/
118+
if (connectionHandler.isConnected()) defaultAccount = accountService.getDefaultAccount();
103119

104120
for (XMPPAccount account : accountService.getAllAccounts()) {
105-
if (!account.equals(activeAccount)) addMenuItem(account);
121+
if (!account.equals(defaultAccount)) addMenuItem(account);
106122
}
107123

108124
new MenuItem(accountMenu, SWT.SEPARATOR);

eclipse/src/saros/ui/eventhandler/ConnectingFailureHandler.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -64,15 +64,17 @@ private void handleConnectionFailed(Exception exception) {
6464
return;
6565
}
6666

67-
/* FIXME the active/default account is not always the account that is currently used for connecting */
6867
if (DialogUtils.popUpYesNoQuestion(
6968
"Connecting Error",
7069
generateHumanReadableErrorMessage((XMPPException) exception),
7170
false)) {
7271

73-
if (WizardUtils.openEditXMPPAccountWizard(accountStore.getActiveAccount()) == null) return;
72+
/* FIXME the active/default account might not always be the account that is currently used for connecting */
73+
final XMPPAccount accountUsedDuringConnection = accountStore.getDefaultAccount();
7474

75-
final XMPPAccount account = accountStore.getActiveAccount();
75+
if (WizardUtils.openEditXMPPAccountWizard(accountUsedDuringConnection) == null) return;
76+
77+
final XMPPAccount account = accountStore.getDefaultAccount();
7678

7779
SWTUtils.runSafeSWTAsync(
7880
LOG, () -> XMPPConnectionSupport.getInstance().connect(account, false));

eclipse/src/saros/ui/preferencePages/GeneralPreferencePage.java

Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -144,13 +144,16 @@ public void widgetDefaultSelected(SelectionEvent e) {
144144

145145
private void handleEvent() {
146146

147+
XMPPAccount selectedAccount = getSelectedAccount();
148+
149+
if (selectedAccount == null) return;
150+
147151
activateAccountButton.setEnabled(true);
148152
removeAccountButton.setEnabled(true);
149153
editAccountButton.setEnabled(true);
150154

151-
if (getSelectedAccount().equals(accountStore.getActiveAccount())) {
155+
if (selectedAccount.equals(accountStore.getDefaultAccount())) {
152156
activateAccountButton.setEnabled(false);
153-
removeAccountButton.setEnabled(false);
154157
}
155158
}
156159
});
@@ -206,11 +209,11 @@ private void updateList() {
206209
}
207210

208211
private void updateInfoLabel() {
209-
if (!accountStore.isEmpty())
210-
infoLabel.setText(
211-
Messages.GeneralPreferencePage_active
212-
+ createHumanDisplayAbleName(accountStore.getActiveAccount()));
213-
else infoLabel.setText("");
212+
final XMPPAccount defaultAccount = accountStore.getDefaultAccount();
213+
214+
if (defaultAccount != null)
215+
infoLabel.setText("Default account: " + createHumanDisplayAbleName(defaultAccount));
216+
else infoLabel.setText("Default account: none");
214217
}
215218

216219
private String createHumanDisplayAbleName(XMPPAccount account) {
@@ -276,6 +279,8 @@ public void handleEvent(Event event) {
276279
activateAccountButton.setEnabled(false);
277280
removeAccountButton.setEnabled(false);
278281
editAccountButton.setEnabled(false);
282+
283+
updateInfoLabel();
279284
updateList();
280285
}
281286
}
@@ -292,10 +297,9 @@ private void createActivateAccountButton(Composite composite) {
292297
new Listener() {
293298
@Override
294299
public void handleEvent(Event event) {
295-
accountStore.setAccountActive(getSelectedAccount());
300+
accountStore.setDefaultAccount(getSelectedAccount());
296301
updateInfoLabel();
297302
activateAccountButton.setEnabled(false);
298-
removeAccountButton.setEnabled(false);
299303
MessageDialog.openInformation(
300304
GeneralPreferencePage.this.getShell(),
301305
Messages.GeneralPreferencePage_ACTIVATE_ACCOUNT_DIALOG_TITLE,

eclipse/src/saros/ui/util/XMPPConnectionSupport.java

Lines changed: 28 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,11 @@ public XMPPConnectionSupport(
4242
}
4343

4444
/**
45-
* Connects with the current active / default account.
45+
* Connects with the default account.
46+
*
47+
* <p><b>Note:</b> If no default account is available this method silently returns regardless if
48+
* <code>
49+
* failSilently</code> is set to <code>false</code> !
4650
*
4751
* @param failSilently if <code>true</code> suppresses any further error handling
4852
*/
@@ -51,17 +55,27 @@ public void connect(boolean failSilently) {
5155
}
5256

5357
/**
54-
* Connects with given account. If the given account is <code>null<code> the active / default one will be used.
55-
* @param account the account to use
58+
* Connects with given account.
59+
*
60+
* <p><b>Note:</b> If the default account should be used an no default account is available this
61+
* method silently returns regardless if <code>
62+
* failSilently</code> is set to <code>false</code> !
63+
*
64+
* @param account the account to use or <code>null</code> to use the default one
5665
* @param failSilently if <code>true</code> suppresses any further error handling
5766
*/
5867
public void connect(final XMPPAccount account, boolean failSilently) {
5968
connect(account, false, failSilently);
6069
}
6170

6271
/**
63-
* Connects with given account. If the given account is <code>null<code> the active / default one will be used.
64-
* @param account the account to use
72+
* Connects with given account.
73+
*
74+
* <p><b>Note:</b> If the default account should be used an no default account is available this
75+
* method silently returns regardless if <code>
76+
* failSilently</code> is set to <code>false</code> !
77+
*
78+
* @param account account the account to use or <code>null</code> to use the default one
6579
* @param setAsDefault if <code>true</code> the account is set as the default one
6680
* @param failSilently if <code>true</code> suppresses any further error handling
6781
*/
@@ -129,18 +143,18 @@ public void connect(final XMPPAccount account, boolean setAsDefault, boolean fai
129143

130144
final XMPPAccount accountToConnect;
131145

132-
if (account == null && !store.isEmpty()) accountToConnect = store.getActiveAccount();
133-
else if (account != null) accountToConnect = account;
134-
else accountToConnect = null;
146+
accountToConnect = account != null ? account : store.getDefaultAccount();
135147

136-
/*
137-
* some magic, if we connect with null we will trigger an exception that is processed by
138-
* the ConnectingFailureHandler which in turn will open the ConfigurationWizard
139-
*/
140-
if (setAsDefault && accountToConnect != null) {
141-
store.setAccountActive(accountToConnect);
148+
if (accountToConnect == null) {
149+
log.warn(
150+
"unable to establish a connection - no account was provided and no default account could be found");
151+
152+
isConnecting = false;
153+
return;
142154
}
143155

156+
if (setAsDefault) store.setDefaultAccount(accountToConnect);
157+
144158
final boolean disconnectFirst = mustDisconnect;
145159

146160
ThreadUtils.runSafeAsync(

eclipse/src/saros/ui/wizards/ConfigurationWizard.java

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -72,19 +72,18 @@ public void addPages() {
7272
public boolean performFinish() {
7373
setConfiguration();
7474

75-
final XMPPAccount accountToConnect;
75+
final XMPPAccount accountToConnect = addOrGetXMPPAccount();
7676

77-
if (!enterXMPPAccountWizardPage.isExistingAccount()) {
78-
accountToConnect = addXMPPAccount();
79-
} else accountToConnect = null;
77+
assert (accountToConnect != null);
8078

81-
assert accountStore.getActiveAccount() != null;
79+
/* it is possible to finish the wizard multiple times
80+
* (also it makes no sense) so ensure the behavior is always the same.
81+
*/
82+
83+
accountStore.setDefaultAccount(accountToConnect);
8284

8385
if (preferences.getBoolean(PreferenceConstants.AUTO_CONNECT)) {
84-
getShell()
85-
.getDisplay()
86-
.asyncExec(
87-
() -> XMPPConnectionSupport.getInstance().connect(accountToConnect, true, false));
86+
getShell().getDisplay().asyncExec(() -> XMPPConnectionSupport.getInstance().connect(false));
8887
}
8988

9089
return true;
@@ -144,8 +143,9 @@ protected void setConfiguration() {
144143
}
145144

146145
/** Adds the {@link EnterXMPPAccountWizardPage}'s account data to the {@link XMPPAccountStore}. */
147-
private XMPPAccount addXMPPAccount() {
146+
private XMPPAccount addOrGetXMPPAccount() {
148147

148+
boolean isExistingAccount = enterXMPPAccountWizardPage.isExistingAccount();
149149
JID jid = enterXMPPAccountWizardPage.getJID();
150150

151151
String username = jid.getName();
@@ -162,6 +162,9 @@ private XMPPAccount addXMPPAccount() {
162162
boolean useTLS = enterXMPPAccountWizardPage.isUsingTLS();
163163
boolean useSASL = enterXMPPAccountWizardPage.isUsingSASL();
164164

165-
return accountStore.createAccount(username, password, domain, server, port, useTLS, useSASL);
165+
if (isExistingAccount)
166+
return accountStore.createAccount(username, password, domain, server, port, useTLS, useSASL);
167+
168+
return accountStore.getAccount(username, domain, server, port);
166169
}
167170
}

intellij/src/saros/intellij/ui/actions/ConnectServerAction.java

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
import saros.account.XMPPAccount;
1010
import saros.account.XMPPAccountStore;
1111
import saros.communication.connection.ConnectionHandler;
12+
import saros.net.xmpp.JID;
1213
import saros.repackaged.picocontainer.annotations.Inject;
1314

1415
/** Connects to XMPP/Jabber server with given account or active account */
@@ -34,15 +35,22 @@ public String getActionName() {
3435

3536
/** Connects with the given user. */
3637
public void executeWithUser(String user) {
37-
XMPPAccount account = accountStore.findAccount(user);
38-
accountStore.setAccountActive(account);
38+
JID jid = new JID(user);
39+
XMPPAccount account = accountStore.getAccount(jid.getName(), jid.getDomain());
40+
41+
if (account == null) return;
42+
43+
accountStore.setDefaultAccount(account);
3944
connectAccount(account);
4045
}
4146

4247
/** Connects with active account from the {@link XMPPAccountStore}. */
4348
@Override
4449
public void execute() {
45-
XMPPAccount account = accountStore.getActiveAccount();
50+
XMPPAccount account = accountStore.getDefaultAccount();
51+
52+
if (account == null) return;
53+
4654
connectAccount(account);
4755
}
4856

server/src/saros/server/ServerLifecycle.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,11 +65,13 @@ private void connectToXMPPServer(final ContainerContext context) {
6565
* instead
6666
*/
6767
XMPPAccountStore store = context.getComponent(XMPPAccountStore.class);
68-
XMPPAccount account = store.findAccount(jidString);
69-
if (account == null) {
70-
JID jid = new JID(jidString);
71-
account = store.createAccount(jid.getName(), password, jid.getDomain(), "", 0, true, true);
72-
}
68+
69+
// ensure we do not save anything and that the store is empty
70+
store.setAccountFile(null, null);
71+
72+
JID jid = new JID(jidString);
73+
XMPPAccount account =
74+
store.createAccount(jid.getName(), password, jid.getDomain(), "", 0, true, true);
7375

7476
ConnectionHandler connectionHandler = context.getComponent(ConnectionHandler.class);
7577
connectionHandler.connect(account, false);

stf.test/test/saros/stf/test/account/AccountPreferenceTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,8 @@ public void testRemoveAccountButton() throws Exception {
125125

126126
shell.bot().listInGroup(GROUP_TITLE_XMPP_JABBER_ACCOUNTS).select(ALICE.getBaseJid());
127127

128-
assertFalse(
129-
"remove account button must no be enabled when the active account is already selected",
128+
assertTrue(
129+
"remove account button must be enabled when the active account is already selected",
130130
shell
131131
.bot()
132132
.buttonInGroup(BUTTON_REMOVE_ACCOUNT, GROUP_TITLE_XMPP_JABBER_ACCOUNTS)

stf/src/saros/stf/server/rmi/controlbot/manipulation/impl/AccountManipulatorImpl.java

Lines changed: 6 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ public void restoreDefaultAccount(String username, String password, String domai
2929

3030
for (XMPPAccount account : accountStore.getAllAccounts()) {
3131

32-
if (account.equals(accountStore.getActiveAccount())) continue;
32+
if (account.equals(accountStore.getDefaultAccount())) continue;
3333

3434
LOG.debug("deleting account: " + account);
3535

@@ -40,19 +40,6 @@ public void restoreDefaultAccount(String username, String password, String domai
4040
accountStore.createAccount(username, password, domain, "", 0, true, true);
4141
return;
4242
}
43-
44-
XMPPAccount activeAccount = accountStore.getActiveAccount();
45-
46-
if (accountStore.existsAccount(username, domain, "", 0)) return;
47-
48-
XMPPAccount defaultAccount =
49-
accountStore.createAccount(username, password, domain, "", 0, true, true);
50-
51-
LOG.debug("activating account: " + defaultAccount);
52-
accountStore.setAccountActive(defaultAccount);
53-
54-
LOG.debug("deleting account: " + activeAccount);
55-
accountStore.deleteAccount(activeAccount);
5643
}
5744

5845
@Override
@@ -75,27 +62,13 @@ public boolean activateAccount(String username, String domain) throws RemoteExce
7562

7663
final XMPPAccountStore accountStore = getXmppAccountStore();
7764

78-
XMPPAccount activeAccount = null;
65+
final XMPPAccount accountToSetAsDefault = accountStore.getAccount(username, domain);
7966

80-
try {
81-
activeAccount = accountStore.getActiveAccount();
82-
} catch (IllegalStateException e) {
83-
// ignore
84-
}
85-
86-
for (XMPPAccount account : accountStore.getAllAccounts()) {
87-
if (account.getUsername().equals(username) && account.getDomain().equals(domain)) {
67+
if (accountToSetAsDefault == null)
68+
throw new IllegalArgumentException(
69+
"an account with username '" + username + "' and domain '" + domain + "' does not exist");
8870

89-
if (!account.equals(activeAccount)) {
90-
LOG.debug("activating account: " + account);
91-
accountStore.setAccountActive(account);
92-
} else {
93-
LOG.debug("account is already activated: " + account);
94-
}
95-
96-
return !account.equals(activeAccount);
97-
}
98-
}
71+
accountStore.setDefaultAccount(accountToSetAsDefault);
9972

10073
throw new IllegalArgumentException(
10174
"an account with username '" + username + "' and domain '" + domain + "' does not exist");

0 commit comments

Comments
 (0)