From 818ace92b45ef157bf394eaa8e8a0efb5fccc777 Mon Sep 17 00:00:00 2001 From: hristoterezov Date: Wed, 24 Jul 2013 18:15:51 +0300 Subject: [PATCH] Adds functionality to save passwords and nicknames for chat rooms. --- resources/languages/resources.properties | 1 + .../communicator/impl/gui/GuiActivator.java | 25 ++++++ .../main/chat/ChatContactRightButtonMenu.java | 7 +- .../main/chat/conference/ChatRoomWrapper.java | 45 ++++++++++ .../conference/ConferenceChatManager.java | 89 ++++++++++++++++--- .../ChatRoomRightButtonMenu.java | 27 +++++- .../chatroomslist/ChatRoomTableDialog.java | 2 +- .../ChatRoomAuthenticationWindow.java | 47 ++++++---- .../impl/gui/swing.ui.manifest.mf | 3 +- 9 files changed, 212 insertions(+), 34 deletions(-) diff --git a/resources/languages/resources.properties b/resources/languages/resources.properties index f3baae0bd..e95394e05 100644 --- a/resources/languages/resources.properties +++ b/resources/languages/resources.properties @@ -124,6 +124,7 @@ service.gui.CHAT_ROOM_REGISTRATION_REQUIRED=The {0} chat room requires registrat service.gui.CHAT_ROOM_REQUIRES_PASSWORD=The {0} chat room has requested a password. service.gui.CHAT_ROOMS=Chat rooms service.gui.CHAT_ROOM_SUBJECT_CHANGED={0} has changed the subject to {1} +service.gui.CHAT_ROOM_REMEMBER_PASSWORD=Remember password service.gui.CHOOSE_CONTACT=Choose contact service.gui.CHOOSE_ACCOUNT=Please select one of the listed accounts. service.gui.CITY=City diff --git a/src/net/java/sip/communicator/impl/gui/GuiActivator.java b/src/net/java/sip/communicator/impl/gui/GuiActivator.java index c8b7fb41d..19dc5cd13 100644 --- a/src/net/java/sip/communicator/impl/gui/GuiActivator.java +++ b/src/net/java/sip/communicator/impl/gui/GuiActivator.java @@ -15,6 +15,7 @@ import net.java.sip.communicator.service.callhistory.*; import net.java.sip.communicator.service.contactlist.*; import net.java.sip.communicator.service.contactsource.*; +import net.java.sip.communicator.service.credentialsstorage.*; import net.java.sip.communicator.service.customcontactactions.*; import net.java.sip.communicator.service.desktop.*; import net.java.sip.communicator.service.globaldisplaydetails.*; @@ -102,6 +103,8 @@ public class GuiActivator implements BundleActivator private static GlobalDisplayDetailsService globalDisplayDetailsService; private static AlertUIService alertUIService; + + private static CredentialsStorageService credentialsService; private static final Map providerFactoriesMap = new Hashtable(); @@ -829,4 +832,26 @@ public static ProtocolProviderService getPreferredAccount() } return null; } + + /** + * Returns a reference to a CredentialsStorageService implementation + * currently registered in the bundle context or null if no such + * implementation was found. + * + * @return a currently valid implementation of the + * CredentialsStorageService. + */ + public static CredentialsStorageService getCredentialsStorageService() + { + if (credentialsService == null) + { + ServiceReference credentialsReference + = bundleContext.getServiceReference( + CredentialsStorageService.class.getName()); + credentialsService + = (CredentialsStorageService) bundleContext + .getService(credentialsReference); + } + return credentialsService; + } } diff --git a/src/net/java/sip/communicator/impl/gui/main/chat/ChatContactRightButtonMenu.java b/src/net/java/sip/communicator/impl/gui/main/chat/ChatContactRightButtonMenu.java index f386d89f5..503ec6c67 100644 --- a/src/net/java/sip/communicator/impl/gui/main/chat/ChatContactRightButtonMenu.java +++ b/src/net/java/sip/communicator/impl/gui/main/chat/ChatContactRightButtonMenu.java @@ -364,7 +364,12 @@ else if (menuItemName.equals("changeNicknameItem")) { try { - room.setUserNickname(reasonDialog.getReason().trim()); + String nickname = reasonDialog.getReason().trim(); + room.setUserNickname(nickname); + ConfigurationUtils.updateChatRoomProperty( + room.getParentProvider(), + room.getIdentifier(), + "userNickName", nickname); } catch (OperationFailedException ex) { diff --git a/src/net/java/sip/communicator/impl/gui/main/chat/conference/ChatRoomWrapper.java b/src/net/java/sip/communicator/impl/gui/main/chat/conference/ChatRoomWrapper.java index 121283799..95a484509 100644 --- a/src/net/java/sip/communicator/impl/gui/main/chat/conference/ChatRoomWrapper.java +++ b/src/net/java/sip/communicator/impl/gui/main/chat/conference/ChatRoomWrapper.java @@ -6,6 +6,7 @@ */ package net.java.sip.communicator.impl.gui.main.chat.conference; +import net.java.sip.communicator.impl.gui.*; import net.java.sip.communicator.service.protocol.*; import net.java.sip.communicator.util.*; @@ -56,6 +57,13 @@ public class ChatRoomWrapper * If not overridden we query the wrapped room. */ private Boolean persistent = null; + + /** + * The prefix needed by the credential storage service to store the password + * of the chat room. + */ + private String passwordPrefix + = "net.java.sip.communicator.impl.gui.accounts."; /** * Creates a ChatRoomWrapper by specifying the protocol provider, @@ -73,6 +81,10 @@ public ChatRoomWrapper( ChatRoomProviderWrapper parentProvider, this.parentProvider = parentProvider; this.chatRoomID = chatRoomID; this.chatRoomName = chatRoomName; + + passwordPrefix += getParentProvider().getProtocolProvider() + .getAccountID().getAccountUniqueID() + ".chatrooms." + + getChatRoomID() + ".password"; } /** @@ -171,6 +183,39 @@ public void setPersistent(boolean value) { this.persistent = value; } + + + /** + * Stores the password for the chat room. + * + * @param password the password to store + */ + public void savePassword(String password) + { + + GuiActivator.getCredentialsStorageService() + .storePassword(passwordPrefix, password); + } + + /** + * Returns the password for the chat room. + * + * @return the password + */ + public String loadPassword() + { + return GuiActivator.getCredentialsStorageService() + .loadPassword(passwordPrefix); + } + + /** + * Removes the saved password for the chat room. + */ + public void removePassword() + { + GuiActivator.getCredentialsStorageService() + .removePassword(passwordPrefix); + } /** * Is room set to auto join on start-up. diff --git a/src/net/java/sip/communicator/impl/gui/main/chat/conference/ConferenceChatManager.java b/src/net/java/sip/communicator/impl/gui/main/chat/conference/ConferenceChatManager.java index cd2f44677..dcc2bed5c 100644 --- a/src/net/java/sip/communicator/impl/gui/main/chat/conference/ConferenceChatManager.java +++ b/src/net/java/sip/communicator/impl/gui/main/chat/conference/ConferenceChatManager.java @@ -629,6 +629,39 @@ public void rejectInvitation( multiUserChatAdHocOpSet.rejectInvitation(invitation, reason); } + /** + * Joins the given chat room with the given password and manages all the + * exceptions that could occur during the join process. + * + * @param chatRoomWrapper the chat room to join. + * @param nickName the nickname we choose for the given chat room. + * @param password the password. + * @param rememberPassword if true the password should be saved. + */ + public void joinChatRoom( ChatRoomWrapper chatRoomWrapper, + String nickName, + byte[] password, + boolean rememberPassword) + { + ChatRoom chatRoom = chatRoomWrapper.getChatRoom(); + + if(chatRoom == null) + { + new ErrorDialog( + GuiActivator.getUIService().getMainFrame(), + GuiActivator.getResources().getI18NString("service.gui.WARNING"), + GuiActivator.getResources().getI18NString( + "service.gui.CHAT_ROOM_NOT_CONNECTED", + new String[]{chatRoomWrapper.getChatRoomName()})) + .showDialog(); + + return; + } + + new JoinChatRoomTask(chatRoomWrapper, nickName, password, + rememberPassword).execute(); + } + /** * Joins the given chat room with the given password and manages all the * exceptions that could occur during the join process. @@ -658,7 +691,6 @@ public void joinChatRoom( ChatRoomWrapper chatRoomWrapper, new JoinChatRoomTask(chatRoomWrapper, nickName, password).execute(); } - /** * Creates a chat room, by specifying the chat room name, the parent * protocol provider and eventually, the contacts invited to participate in @@ -1461,14 +1493,42 @@ private static class JoinChatRoomTask private final String nickName; private final byte[] password; + + private final boolean rememberPassword; JoinChatRoomTask( ChatRoomWrapper chatRoomWrapper, String nickName, - byte[] password) + byte[] password, + boolean rememberPassword) { this.chatRoomWrapper = chatRoomWrapper; this.nickName = nickName; - this.password = password; + + if(password == null) + { + String passString = chatRoomWrapper.loadPassword(); + if(passString != null) + { + this.password = passString.getBytes(); + } + else + { + this.password = null; + } + } + else + { + this.password = password; + } + this.rememberPassword = rememberPassword; + } + + JoinChatRoomTask( ChatRoomWrapper chatRoomWrapper, + String nickName, + byte[] password) + { + this(chatRoomWrapper, nickName, password, false); + } /** @@ -1540,7 +1600,8 @@ protected void done() if(AUTHENTICATION_FAILED.equals(returnCode)) { ChatRoomAuthenticationWindow authWindow - = new ChatRoomAuthenticationWindow(chatRoomWrapper); + = new ChatRoomAuthenticationWindow(chatRoomWrapper, + nickName); authWindow.setVisible(true); } @@ -1574,13 +1635,21 @@ else if(SUBSCRIPTION_ALREADY_EXISTS.equals(returnCode)) new String[]{chatRoomWrapper.getChatRoomName()}); } - if (!SUCCESS.equals(returnCode) - && !AUTHENTICATION_FAILED.equals(returnCode)) + if (!SUCCESS.equals(returnCode)) { - new ErrorDialog( - GuiActivator.getUIService().getMainFrame(), - GuiActivator.getResources().getI18NString( - "service.gui.ERROR"), errorMessage).showDialog(); + if(!AUTHENTICATION_FAILED.equals(returnCode)) + { + new ErrorDialog( + GuiActivator.getUIService().getMainFrame(), + GuiActivator.getResources().getI18NString( + "service.gui.ERROR"), errorMessage).showDialog(); + } + chatRoomWrapper.removePassword(); + } + + if (SUCCESS.equals(returnCode) && rememberPassword) + { + chatRoomWrapper.savePassword(new String(password)); } } } diff --git a/src/net/java/sip/communicator/impl/gui/main/chatroomslist/ChatRoomRightButtonMenu.java b/src/net/java/sip/communicator/impl/gui/main/chatroomslist/ChatRoomRightButtonMenu.java index bc5388e20..d8a8dfde2 100644 --- a/src/net/java/sip/communicator/impl/gui/main/chatroomslist/ChatRoomRightButtonMenu.java +++ b/src/net/java/sip/communicator/impl/gui/main/chatroomslist/ChatRoomRightButtonMenu.java @@ -195,10 +195,31 @@ else if (itemName.equals("openChatRoom")) } else if(itemName.equals("joinAsChatRoom")) { - ChatRoomAuthenticationWindow authWindow - = new ChatRoomAuthenticationWindow(chatRoomWrapper); + String nickName = null; + + ChatOperationReasonDialog reasonDialog = + new ChatOperationReasonDialog(GuiActivator + .getResources().getI18NString( + "service.gui.CHANGE_NICKNAME"), GuiActivator + .getResources().getI18NString( + "service.gui.CHANGE_NICKNAME_LABEL")); + + reasonDialog.setReasonFieldText(chatRoomWrapper + .getChatRoom().getUserNickname()); - authWindow.setVisible(true); + int result = reasonDialog.showDialog(); + + if (result == MessageDialog.OK_RETURN_CODE) + { + nickName = reasonDialog.getReason().trim(); + + ConfigurationUtils.updateChatRoomProperty( + chatRoomWrapper.getParentProvider().getProtocolProvider(), + chatRoomWrapper.getChatRoomID(), "userNickName", + nickName); + } + GuiActivator.getUIService().getConferenceChatManager() + .joinChatRoom(chatRoomWrapper, nickName, null); } else if(itemName.equals("nickNameChatRoom")) { diff --git a/src/net/java/sip/communicator/impl/gui/main/chatroomslist/ChatRoomTableDialog.java b/src/net/java/sip/communicator/impl/gui/main/chatroomslist/ChatRoomTableDialog.java index 92e65dc54..dce0fde6a 100644 --- a/src/net/java/sip/communicator/impl/gui/main/chatroomslist/ChatRoomTableDialog.java +++ b/src/net/java/sip/communicator/impl/gui/main/chatroomslist/ChatRoomTableDialog.java @@ -417,7 +417,7 @@ else if(sourceButton.equals(okButton)) } GuiActivator.getUIService().getConferenceChatManager() - .joinChatRoom(chatRoomWrapper); + .joinChatRoom(chatRoomWrapper, nickName, null); ChatWindowManager chatWindowManager = GuiActivator.getUIService().getChatWindowManager(); diff --git a/src/net/java/sip/communicator/impl/gui/main/chatroomslist/joinforms/ChatRoomAuthenticationWindow.java b/src/net/java/sip/communicator/impl/gui/main/chatroomslist/joinforms/ChatRoomAuthenticationWindow.java index 413ad7a72..c492c3424 100644 --- a/src/net/java/sip/communicator/impl/gui/main/chatroomslist/joinforms/ChatRoomAuthenticationWindow.java +++ b/src/net/java/sip/communicator/impl/gui/main/chatroomslist/joinforms/ChatRoomAuthenticationWindow.java @@ -15,6 +15,7 @@ import net.java.sip.communicator.impl.gui.main.chat.conference.*; import net.java.sip.communicator.impl.gui.utils.*; import net.java.sip.communicator.plugin.desktoputil.*; +import net.java.sip.communicator.util.*; import net.java.sip.communicator.util.skin.*; /** @@ -31,13 +32,10 @@ public class ChatRoomAuthenticationWindow { private JTextArea infoTextArea = new JTextArea(); - private JLabel idLabel = new JLabel( - GuiActivator.getResources().getI18NString("service.gui.IDENTIFIER")); - private JLabel passwdLabel = new JLabel( GuiActivator.getResources().getI18NString("service.gui.PASSWORD")); - private JTextField idValue; + private JCheckBox rememberPassword; private JPasswordField passwdField = new JPasswordField(15); @@ -52,6 +50,11 @@ public class ChatRoomAuthenticationWindow private JPanel textFieldsPanel = new TransparentPanel(new GridLayout(0, 1, 8, 8)); + + + private JPanel bottomPanel = + new TransparentPanel(new GridLayout(0, 1, 8, 8)); + private JPanel mainPanel = new TransparentPanel(new BorderLayout(10, 10)); @@ -61,14 +64,18 @@ public class ChatRoomAuthenticationWindow private LoginWindowBackground backgroundPanel; private ChatRoomWrapper chatRoom; + + private String nickname; /** * Creates an instance of the LoginWindow. * @param chatRoom the chat room for which we're authenticating */ - public ChatRoomAuthenticationWindow(ChatRoomWrapper chatRoom) + public ChatRoomAuthenticationWindow(ChatRoomWrapper chatRoom, String nickname) { this.chatRoom = chatRoom; + + this.nickname = nickname; ImageIcon logoImage = new ImageIcon(chatRoom.getParentProvider().getImage()); @@ -104,9 +111,7 @@ public ChatRoomAuthenticationWindow(ChatRoomWrapper chatRoom) */ private void init() { - this.idValue = new JTextField( - chatRoom.getParentProvider().getProtocolProvider() - .getAccountID().getUserID()); + this.infoTextArea.setOpaque(false); this.infoTextArea.setLineWrap(true); @@ -118,23 +123,26 @@ private void init() "service.gui.CHAT_ROOM_REQUIRES_PASSWORD", new String[]{chatRoom.getChatRoomName()})); - this.idLabel.setFont(idLabel.getFont().deriveFont(Font.BOLD)); this.passwdLabel.setFont(passwdLabel.getFont().deriveFont(Font.BOLD)); - this.labelsPanel.add(idLabel); + this.labelsPanel.add(passwdLabel); - this.textFieldsPanel.add(idValue); + this.textFieldsPanel.add(passwdField); - + this.rememberPassword = new JCheckBox(GuiActivator.getResources() + .getI18NString("service.gui.CHAT_ROOM_REMEMBER_PASSWORD"), + chatRoom.isPersistent()); + this.bottomPanel.add(rememberPassword); this.buttonsPanel.add(loginButton); this.buttonsPanel.add(cancelButton); - + this.bottomPanel.add(buttonsPanel); this.mainPanel.add(infoTextArea, BorderLayout.NORTH); this.mainPanel.add(labelsPanel, BorderLayout.WEST); this.mainPanel.add(textFieldsPanel, BorderLayout.CENTER); - this.mainPanel.add(buttonsPanel, BorderLayout.SOUTH); - + this.mainPanel.add(bottomPanel, BorderLayout.SOUTH); + + this.backgroundPanel.add(mainPanel, BorderLayout.CENTER); this.loginButton.setName("ok"); @@ -163,7 +171,8 @@ private void setTransparent(boolean transparent) { this.mainPanel.setOpaque(!transparent); this.labelsPanel.setOpaque(!transparent); this.textFieldsPanel.setOpaque(!transparent); - this.buttonsPanel.setOpaque(!transparent); + this.bottomPanel.setOpaque(!transparent); + } /** @@ -181,8 +190,10 @@ public void actionPerformed(ActionEvent evt) if (buttonName.equals("ok")) { GuiActivator.getUIService().getConferenceChatManager() - .joinChatRoom(chatRoom, idValue.getText(), - new String(passwdField.getPassword()).getBytes()); + .joinChatRoom(chatRoom, nickname, + new String(passwdField.getPassword()).getBytes(), + rememberPassword.isSelected()); + } this.dispose(); diff --git a/src/net/java/sip/communicator/impl/gui/swing.ui.manifest.mf b/src/net/java/sip/communicator/impl/gui/swing.ui.manifest.mf index 5cbcd2180..590a08b10 100644 --- a/src/net/java/sip/communicator/impl/gui/swing.ui.manifest.mf +++ b/src/net/java/sip/communicator/impl/gui/swing.ui.manifest.mf @@ -76,4 +76,5 @@ Import-Package: com.apple.eawt, org.jitsi.util.event, org.jitsi.util.swing, org.osgi.framework, - say.swing + say.swing, + net.java.sip.communicator.service.credentialsstorage