From 51f7e377cab1a9ad9ef5ada3e36913676fe6eeff Mon Sep 17 00:00:00 2001 From: Damian Minkov Date: Tue, 3 Dec 2013 16:19:10 +0200 Subject: [PATCH] Updates sending sms messages to reuse contacts that have numbers. --- resources/languages/resources.properties | 1 + .../main/call/ChooseCallAccountPopupMenu.java | 54 ++- .../impl/gui/main/chat/ChatPanel.java | 22 ++ .../main/chat/MetaContactChatTransport.java | 21 +- .../impl/gui/main/chat/SendSmsDialog.java | 46 +-- .../impl/gui/utils/SMSManager.java | 344 ++++++++++++++++-- 6 files changed, 393 insertions(+), 95 deletions(-) diff --git a/resources/languages/resources.properties b/resources/languages/resources.properties index d8c9ce95f..6c2c56944 100644 --- a/resources/languages/resources.properties +++ b/resources/languages/resources.properties @@ -132,6 +132,7 @@ service.gui.CHAT_ROOMS=Chat rooms service.gui.CHAT_ROOM=Chat room service.gui.CHAT_ROOM_SUBJECT_CHANGED={0} has changed the subject to {1} service.gui.CHOOSE_CONTACT=Choose contact +service.gui.CHOOSE_NUMBER=Choose number service.gui.CHOOSE_ACCOUNT=Please select one of the listed accounts. service.gui.CITY=City service.gui.COUNTRY=Country diff --git a/src/net/java/sip/communicator/impl/gui/main/call/ChooseCallAccountPopupMenu.java b/src/net/java/sip/communicator/impl/gui/main/call/ChooseCallAccountPopupMenu.java index eb47fa4c3..eedc5a040 100644 --- a/src/net/java/sip/communicator/impl/gui/main/call/ChooseCallAccountPopupMenu.java +++ b/src/net/java/sip/communicator/impl/gui/main/call/ChooseCallAccountPopupMenu.java @@ -44,7 +44,7 @@ public class ChooseCallAccountPopupMenu /** * The invoker component. */ - private final JComponent invoker; + protected final JComponent invoker; /** * The call interface listener, which would be notified once the call @@ -110,7 +110,7 @@ public ChooseCallAccountPopupMenu( { this.invoker = invoker; this.init(GuiActivator.getResources() - .getI18NString("service.gui.CALL_VIA")); + .getI18NString(getI18NKeyCallVia())); for (ProtocolProviderService provider : telephonyProviders) { @@ -148,7 +148,7 @@ public ChooseCallAccountPopupMenu(JComponent invoker, { this.invoker = invoker; this.init(GuiActivator.getResources() - .getI18NString("service.gui.CHOOSE_CONTACT")); + .getI18NString(getI18NKeyChooseContact())); for (Object o : telephonyObjects) { @@ -161,6 +161,26 @@ else if (o instanceof ChatTransport) } } + /** + * Returns the key to use for choose contact string. Can be overridden + * by extenders. + * @return the key to use for choose contact string. + */ + protected String getI18NKeyChooseContact() + { + return "service.gui.CHOOSE_CONTACT"; + } + + /** + * Returns the key to use for choose contact string. Can be overridden + * by extenders. + * @return the key to use for choose contact string. + */ + protected String getI18NKeyCallVia() + { + return "service.gui.CALL_VIA"; + } + /** * Initializes and add some common components. * @@ -258,13 +278,8 @@ public void actionPerformed(ActionEvent e) } else if (providers.size() > 1) { - ChooseCallAccountDialog callAccountDialog - = new ChooseCallAccountDialog( - telephonyContact.getAddress(), opSetClass, providers); - - if (uiContact != null) - callAccountDialog.setUIContact(uiContact); - callAccountDialog.setVisible(true); + itemSelected( + opSetClass, providers, telephonyContact.getAddress()); } else // providersCount == 1 { @@ -470,6 +485,25 @@ protected void itemSelected(Class opSetClass, contact); } + /** + * Item was selected, give a chance for extenders to override. + * + * @param opSetClass the operation set to use. + * @param providers list of available protocol providers + * @param contact the contact address selected + */ + protected void itemSelected(Class opSetClass, + List providers, + String contact) + { + ChooseCallAccountDialog callAccountDialog + = new ChooseCallAccountDialog(contact, opSetClass, providers); + + if (uiContact != null) + callAccountDialog.setUIContact(uiContact); + callAccountDialog.setVisible(true); + } + /** * A custom menu item corresponding to a specific * ProtocolProviderService. diff --git a/src/net/java/sip/communicator/impl/gui/main/chat/ChatPanel.java b/src/net/java/sip/communicator/impl/gui/main/chat/ChatPanel.java index cf1017300..cb652dc3a 100644 --- a/src/net/java/sip/communicator/impl/gui/main/chat/ChatPanel.java +++ b/src/net/java/sip/communicator/impl/gui/main/chat/ChatPanel.java @@ -20,11 +20,13 @@ import javax.swing.text.*; import net.java.sip.communicator.impl.gui.*; +import net.java.sip.communicator.impl.gui.main.call.*; import net.java.sip.communicator.impl.gui.main.chat.conference.*; import net.java.sip.communicator.impl.gui.main.chat.filetransfer.*; import net.java.sip.communicator.impl.gui.utils.*; import net.java.sip.communicator.plugin.desktoputil.*; import net.java.sip.communicator.plugin.desktoputil.SwingWorker; +import net.java.sip.communicator.service.contactlist.*; import net.java.sip.communicator.service.filehistory.*; import net.java.sip.communicator.service.gui.*; import net.java.sip.communicator.service.gui.event.*; @@ -1664,6 +1666,26 @@ public void sendSmsMessage() if(smsChatTransport.askForSMSNumber()) { + Object desc = + smsChatTransport.getParentChatSession().getDescriptor(); + // descriptor will be metacontact + if(desc instanceof MetaContact) + { + UIPhoneUtil contactPhoneUtil = + UIPhoneUtil.getPhoneUtil((MetaContact) desc); + + List uiContactDetailList = + contactPhoneUtil.getAdditionalNumbers(); + + if(uiContactDetailList.size() != 0) + { + SMSManager.sendSMS( + this, uiContactDetailList, messageText, this); + + return; + } + } + smsDialog.setPreferredSize(new Dimension(400, 200)); smsDialog.setVisible(true); } diff --git a/src/net/java/sip/communicator/impl/gui/main/chat/MetaContactChatTransport.java b/src/net/java/sip/communicator/impl/gui/main/chat/MetaContactChatTransport.java index 8808434b8..14869bca4 100644 --- a/src/net/java/sip/communicator/impl/gui/main/chat/MetaContactChatTransport.java +++ b/src/net/java/sip/communicator/impl/gui/main/chat/MetaContactChatTransport.java @@ -11,6 +11,7 @@ import javax.swing.*; +import net.java.sip.communicator.impl.gui.utils.*; import net.java.sip.communicator.plugin.desktoputil.*; import net.java.sip.communicator.service.protocol.*; import net.java.sip.communicator.service.protocol.event.*; @@ -452,14 +453,10 @@ public void sendSmsMessage(String phoneNumber, String messageText) if (!allowsSmsMessage()) return; - OperationSetSmsMessaging smsOpSet - = contact - .getProtocolProvider() - .getOperationSet(OperationSetSmsMessaging.class); - - Message smsMessage = smsOpSet.createMessage(messageText); - - smsOpSet.sendSmsMessage(phoneNumber, smsMessage); + SMSManager.sendSMS( + contact.getProtocolProvider(), + phoneNumber, + messageText); } /** @@ -496,13 +493,7 @@ public void sendSmsMessage(String message) if (!allowsSmsMessage()) return; - OperationSetSmsMessaging smsOpSet - = contact.getProtocolProvider() - .getOperationSet(OperationSetSmsMessaging.class); - - Message smsMessage = smsOpSet.createMessage(message); - - smsOpSet.sendSmsMessage(contact, smsMessage); + SMSManager.sendSMS(contact, message); } /** diff --git a/src/net/java/sip/communicator/impl/gui/main/chat/SendSmsDialog.java b/src/net/java/sip/communicator/impl/gui/main/chat/SendSmsDialog.java index c1b1a400e..1b93dba94 100644 --- a/src/net/java/sip/communicator/impl/gui/main/chat/SendSmsDialog.java +++ b/src/net/java/sip/communicator/impl/gui/main/chat/SendSmsDialog.java @@ -8,13 +8,12 @@ import java.awt.*; import java.awt.event.*; -import java.util.*; import javax.swing.*; import net.java.sip.communicator.impl.gui.*; +import net.java.sip.communicator.impl.gui.utils.*; import net.java.sip.communicator.plugin.desktoputil.*; -import net.java.sip.communicator.service.gui.*; import net.java.sip.communicator.util.*; /** @@ -124,48 +123,7 @@ public void sendSmsMessage(String phoneNumber, String message) chatTransport.getParentChatSession() .setDefaultSmsNumber(phoneNumber); - try - { - if(phoneNumber != null) - chatTransport.sendSmsMessage(phoneNumber, message); - else - chatTransport.sendSmsMessage(message); - } - catch (IllegalStateException ex) - { - logger.error("Failed to send SMS.", ex); - - chatPanel.addMessage( - phoneNumber, - new Date(), - Chat.OUTGOING_MESSAGE, - message, - "text/plain"); - - chatPanel.addErrorMessage( - phoneNumber, - GuiActivator.getResources() - .getI18NString("service.gui.SMS_SEND_CONNECTION_PROBLEM")); - } - catch (Exception ex) - { - logger.error("Failed to send SMS.", ex); - - chatPanel.addMessage( - phoneNumber, - new Date(), - Chat.OUTGOING_MESSAGE, - message, - "text/plain"); - - chatPanel.addErrorMessage( - phoneNumber, - GuiActivator.getResources() - .getI18NString("service.gui.MSG_DELIVERY_ERROR", - new String[]{ex.getMessage()})); - } - - chatPanel.refreshWriteArea(); + SMSManager.sendSMS(phoneNumber, message, chatTransport, chatPanel); this.dispose(); } diff --git a/src/net/java/sip/communicator/impl/gui/utils/SMSManager.java b/src/net/java/sip/communicator/impl/gui/utils/SMSManager.java index 43b364662..6b9d0903c 100644 --- a/src/net/java/sip/communicator/impl/gui/utils/SMSManager.java +++ b/src/net/java/sip/communicator/impl/gui/utils/SMSManager.java @@ -8,8 +8,10 @@ import net.java.sip.communicator.impl.gui.*; import net.java.sip.communicator.impl.gui.main.call.*; +import net.java.sip.communicator.impl.gui.main.chat.*; import net.java.sip.communicator.impl.gui.main.contactlist.*; import net.java.sip.communicator.service.contactlist.*; +import net.java.sip.communicator.service.gui.*; import net.java.sip.communicator.service.protocol.*; import net.java.sip.communicator.util.*; import net.java.sip.communicator.util.account.*; @@ -36,7 +38,20 @@ public class SMSManager * to show popup for choosing provider. * @param to the destination number */ - public static void sendSMS(JComponent invoker, final String to) + public static void sendSMS( + JComponent invoker, String to) + { + sendSMSInternal(invoker, to, null, null); + } + + /** + * Sends sms, chooses provider and sends the sms. + * @param invoker the component invoker, used to get correct location + * to show popup for choosing provider. + * @param to the destination number + */ + private static void sendSMSInternal( + JComponent invoker, String to, String messageText, ChatPanel chatPanel) { List providers = AccountUtils @@ -45,47 +60,78 @@ public static void sendSMS(JComponent invoker, final String to) if(providers.size() == 1) { //send - sendSms(providers.get(0), to); + if(messageText != null) + { + sendSMSInternal( + to, + messageText, + providers.get(0), + null, + chatPanel); + } + else + sendSMSInternal(providers.get(0), to); } else if(providers.size() > 1) { - ChooseCallAccountPopupMenu chooseAccountDialog - = new ChooseCallAccountPopupMenu( + ChooseSMSAccountPopupMenu chooseAccountDialog + = new ChooseSMSAccountPopupMenu( invoker, to, - providers) - { - @Override - protected void itemSelected( - Class opSetClass, - ProtocolProviderService protocolProviderService, - String contact, - UIContactImpl uiContact) - { - sendSms(protocolProviderService, to); - } - - @Override - protected void itemSelected( - Class opSetClass, - ProtocolProviderService protocolProviderService, - String contact) - { - sendSms(protocolProviderService, to); - } - }; + providers, + messageText, + chatPanel); chooseAccountDialog.setLocation(invoker.getLocation()); chooseAccountDialog.showPopupMenu(); } } + /** + * Sends sms message. + * @param protocolProviderService + * @param to the receive number + * @param messageText the text + */ + public static void sendSMS( + ProtocolProviderService protocolProviderService, + String to, + String messageText) + throws Exception + { + OperationSetSmsMessaging smsOpSet + = protocolProviderService + .getOperationSet(OperationSetSmsMessaging.class); + + Message smsMessage = smsOpSet.createMessage(messageText); + + smsOpSet.sendSmsMessage(to, smsMessage); + } + + /** + * Sends sms message. + * @param contact the contact to send sms to + * @param messageText the text. + */ + public static void sendSMS( + Contact contact, + String messageText) + throws Exception + { + OperationSetSmsMessaging smsOpSet = contact.getProtocolProvider() + .getOperationSet(OperationSetSmsMessaging.class); + + Message smsMessage = smsOpSet.createMessage(messageText); + + smsOpSet.sendSmsMessage(contact, smsMessage); + } + /** * Sends sms. * @param protocolProviderService * @param to */ - private static void sendSms( + private static void sendSMSInternal( ProtocolProviderService protocolProviderService, String to) { @@ -106,4 +152,250 @@ private static void sendSms( GuiActivator.getUIService().getChatWindowManager() .startChat(metaContact, contact, true); } + + /** + * Sends sms, chooses phone and chooses provider and sends the sms. + * @param invoker the component invoker, used to get correct location + * to show popup for choosing provider. + * @param additionalNumbers the destination numbers to choose from + */ + public static void sendSMS(final JComponent invoker, + List additionalNumbers, + String messageText, + ChatPanel chatPanel) + { + if(additionalNumbers.size() == 1) + { + sendSMSInternal(invoker, additionalNumbers.get(0).getAddress(), + messageText, chatPanel); + } + else + { + ChooseSMSAccountPopupMenu chooseAccountDialog + = new ChooseSMSAccountPopupMenu( + invoker, + additionalNumbers, + OperationSetSmsMessaging.class, + messageText, + chatPanel); + + chooseAccountDialog.setLocation(invoker.getLocation()); + chooseAccountDialog.showPopupMenu(); + } + } + + /** + * Sends sms message using chatTransport otherwise. + * @param phoneNumber + * @param message + * @param chatTransport the transport to use if protocol provider missing + * @param chatPanel the panel where the message is sent, will be used for + * success or fail messages + */ + public static void sendSMS( + String phoneNumber, + String message, + ChatTransport chatTransport, + ChatPanel chatPanel) + { + sendSMSInternal(phoneNumber, message, null, chatTransport, chatPanel); + } + + /** + * Sends sms message using protocolProviderService if it is not null, + * or using chatTransport otherwise. + * @param phoneNumber + * @param message + * @param protocolProviderService the protocol provider service to use, + * if not null. + * @param chatTransport the transport to use if protocol provider missing + * @param chatPanel the panel where the message is sent, will be used for + * success or fail messages + */ + private static void sendSMSInternal( + String phoneNumber, + String message, + ProtocolProviderService protocolProviderService, + ChatTransport chatTransport, + ChatPanel chatPanel) + { + try + { + if(protocolProviderService != null) + { + sendSMS(protocolProviderService, phoneNumber, message); + } + else + { + if(phoneNumber != null) + chatTransport.sendSmsMessage(phoneNumber, message); + else + chatTransport.sendSmsMessage(message); + } + } + catch (IllegalStateException ex) + { + logger.error("Failed to send SMS.", ex); + + chatPanel.addMessage( + phoneNumber, + new Date(), + Chat.OUTGOING_MESSAGE, + message, + "text/plain"); + + chatPanel.addErrorMessage( + phoneNumber, + GuiActivator.getResources() + .getI18NString("service.gui.SMS_SEND_CONNECTION_PROBLEM")); + } + catch (Exception ex) + { + logger.error("Failed to send SMS.", ex); + + chatPanel.addMessage( + phoneNumber, + new Date(), + Chat.OUTGOING_MESSAGE, + message, + "text/plain"); + + chatPanel.addErrorMessage( + phoneNumber, + GuiActivator.getResources() + .getI18NString("service.gui.MSG_DELIVERY_ERROR", + new String[]{ex.getMessage()})); + } + + chatPanel.refreshWriteArea(); + } + + /** + * Extends ChooseCallAccountPopupMenu to use it for sms functionality. + */ + private static class ChooseSMSAccountPopupMenu + extends ChooseCallAccountPopupMenu + { + private String messageText = null; + private ChatPanel chatPanel = null; + + /** + * Creates popup menu. + * @param invoker + * @param contactToCall + * @param telephonyProviders + */ + public ChooseSMSAccountPopupMenu( + JComponent invoker, + final String contactToCall, + List telephonyProviders, + String messageText, + ChatPanel chatPanel) + { + super(invoker, contactToCall, telephonyProviders, + OperationSetBasicTelephony.class); + + this.messageText = messageText; + this.chatPanel = chatPanel; + } + + /** + * Creates popup menu. + * @param invoker + * @param telephonyObjects + * @param opSetClass + */ + public ChooseSMSAccountPopupMenu( + JComponent invoker, + List telephonyObjects, + Class opSetClass, + String messageText, + ChatPanel chatPanel) + { + super(invoker, telephonyObjects, opSetClass); + + this.messageText = messageText; + this.chatPanel = chatPanel; + } + + /** + * Sends sms when number is selected and several providers are + * available. + * @param opSetClass the operation set to use. + * @param providers list of available protocol providers + * @param contact the contact address selected + */ + @Override + protected void itemSelected( + Class opSetClass, + List providers, + String contact) + { + SMSManager.sendSMSInternal(invoker, contact, messageText, chatPanel); + } + + /** + * Sends sms when we have a number and provider. + * @param opSetClass the operation set to use. + * @param protocolProviderService the protocol provider + * @param contact the contact address + * @param uiContact the MetaContact selected + */ + @Override + protected void itemSelected( + Class opSetClass, + ProtocolProviderService protocolProviderService, + String contact, + UIContactImpl uiContact) + { + if(messageText != null) + { + sendSMSInternal( + contact, + messageText, + protocolProviderService, + null, + chatPanel); + } + else + sendSMSInternal(protocolProviderService, contact); + } + + /** + * Sends sms when we have a number and provider. + * @param opSetClass the operation set to use. + * @param protocolProviderService the protocol provider + * @param contact the contact address selected + */ + @Override + protected void itemSelected( + Class opSetClass, + ProtocolProviderService protocolProviderService, + String contact) + { + if(messageText != null) + { + sendSMSInternal( + contact, + messageText, + protocolProviderService, + null, + chatPanel); + } + else + sendSMSInternal(protocolProviderService, contact); + } + + @Override + protected String getI18NKeyChooseContact() + { + return "service.gui.CHOOSE_NUMBER"; + } + + @Override + protected String getI18NKeyCallVia() + { + return "service.gui.SEND_VIA"; + } + } }