aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/icq/OperationSetTypingNotificationsIcqImpl.java
blob: 20c1257b46c47fccf366cacb38cc5e03d14db1d1 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
/*
 * SIP Communicator, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Distributable under LGPL license.
 * See terms of license at gnu.org.
 */
package net.java.sip.communicator.impl.protocol.icq;

import java.util.*;

import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
import net.kano.joustsim.*;
import net.kano.joustsim.oscar.oscar.service.icbm.*;

/**
 * Maps SIP Communicator typing notifications to those going and coming from
 * joust sim.
 *
 * @author Emil Ivov
 */
public class OperationSetTypingNotificationsIcqImpl
    extends AbstractOperationSetTypingNotifications<ProtocolProviderServiceIcqImpl>
{
    private static final Logger logger =
        Logger.getLogger(OperationSetTypingNotificationsIcqImpl.class);

    /**
     * An active instance of the opSetPersPresence operation set. We're using
     * it to map incoming events to contacts in our contact list.
     */
    private OperationSetPersistentPresenceIcqImpl opSetPersPresence = null;

    /**
     * The joust sim listener that we use for catching chat events.
     */
    private JoustSimIcbmListener joustSimIcbmListener
        = new JoustSimIcbmListener();

    /**
     * That's the listener that would gather the typing notifications
     * themselves.
     */
    private JoustSimTypingListener joustSimTypingListener
        = new JoustSimTypingListener();

    /**
     * We use this listener to ceise the moment when the protocol provider
     * has been successfully registered.
     */
    private ProviderRegListener providerRegListener = new ProviderRegListener();

    /**
     * @param icqProvider a ref to the <tt>ProtocolProviderServiceIcqImpl</tt>
     * that created us and that we'll use for retrieving the underlying aim
     * connection.
     */
    OperationSetTypingNotificationsIcqImpl(
        ProtocolProviderServiceIcqImpl icqProvider)
    {
        super(icqProvider);

        icqProvider.addRegistrationStateChangeListener(providerRegListener);
    }

    /**
     * Converts the <tt>typingState</tt> variable to its corresponding joustsim
     * TypingState instance
     * @param typingState one of the <tt>STATE_XXX</tt> int fields of this
     * operation set.
     * @return the <tt>TypingState</tt> corresponding to the
     * <tt>typingState</tt> argument.
     */
    private TypingState intToTypingState(int typingState)
    {
        switch (typingState)
        {
            case STATE_PAUSED:   return TypingState.PAUSED;
            case STATE_TYPING:   return TypingState.TYPING;
            case STATE_STOPPED:  return TypingState.NO_TEXT;
        }
        //if unknown return STOPPED
        return TypingState.NO_TEXT;
    }

    /**
     * Returns the int var (one of the STATE_XXX fields in
     * OperationSetTypingNotifications) that best corresponds to <tt>state</tt>
     * @param state the <tt>TypingState</tt> that we'd like to translate.
     * @return one of the <tt>STATE_XXX</tt> int fields that best corresponds
     * to <tt>state</tt>
     */
    private int typingStateToInt(TypingState state)
    {
        if (state == TypingState.TYPING)
            return STATE_TYPING;
        else if(state == TypingState.PAUSED)
            return STATE_PAUSED;
        else if(state == TypingState.NO_TEXT)
            return STATE_STOPPED;

        return STATE_UNKNOWN;
    }

    /**
     * Sends a notification to <tt>notifiedContatct</tt> that we have entered
     * <tt>typingState</tt>.
     *
     * @param notifiedContact the <tt>Contact</tt> to notify
     * @param typingState the typing state that we have entered.
     *
     * @throws java.lang.IllegalStateException if the underlying ICQ stack is
     * not registered and initialized.
     * @throws java.lang.IllegalArgumentException if <tt>notifiedContact</tt> is
     * not an instance belonging to the underlying implementation.
     */
    public void sendTypingNotification(Contact notifiedContact, int typingState)
        throws IllegalStateException, IllegalArgumentException
    {
        assertConnected();

        if( !(notifiedContact instanceof ContactIcqImpl) )
           throw new IllegalArgumentException(
               "The specified contact is not an ICQ contact."
               + notifiedContact);


        parentProvider
            .getAimConnection()
                .getIcbmService()
                    .getImConversation(
                            new Screenname(notifiedContact.getAddress()))
                        .setTypingState(intToTypingState(typingState));
    }

    /**
     * Our listener that will tell us when we're registered to icq and joust
     * sim is ready to accept us as a listener.
     */
    private class ProviderRegListener
        implements RegistrationStateChangeListener
    {
        /**
         * The method is called by a ProtocolProvider implementation whenver
         * a change in the registration state of the corresponding provider had
         * occurred.
         * @param evt ProviderStatusChangeEvent the event describing the status
         * change.
         */
        public void registrationStateChanged(RegistrationStateChangeEvent evt)
        {
            logger.debug("The ICQ provider changed state from: "
                         + evt.getOldState()
                         + " to: " + evt.getNewState());
            if (evt.getNewState() == RegistrationState.FINALIZING_REGISTRATION)
            {
                parentProvider.getAimConnection().getIcbmService()
                    .addIcbmListener(joustSimIcbmListener);

                opSetPersPresence =
                    (OperationSetPersistentPresenceIcqImpl) parentProvider
                        .getOperationSet(OperationSetPersistentPresence.class);
            }
        }
    }

    /**
     * We track newly created conversations and register a typing listener with
     * every one of them so that we could fire events for typing events.
     */
    private class JoustSimIcbmListener implements IcbmListener
    {
        /**
         * All we do here is add a listener that would snoop for typing events
         * sent by oscar.jar
         * @param service the IcbmService that is sending the event
         * @param conv the Conversation where we're to add ourselves as a
         * listener.
         */
        public void newConversation(IcbmService service, Conversation conv)
        {
            conv.addConversationListener(joustSimTypingListener);
        }

        public void buddyInfoUpdated(IcbmService service, Screenname buddy,
                                     IcbmBuddyInfo info)
        {
            logger.debug("buddyInfoUpdated for:"+buddy+" info: " +info);
        }

        public void sendAutomaticallyFailed(
            IcbmService service,
            net.kano.joustsim.oscar.oscar.service.icbm.Message message,
            Set<Conversation> triedConversations)
        {
        }
    }

    /**
     * The oscar.jar lib sends us typing events through this listener.
     */
    private class JoustSimTypingListener implements IcbmListener, TypingListener
    {
        public void gotTypingState(Conversation conversation,
                                   TypingInfo typingInfo)
        {
            Contact sourceContact = opSetPersPresence.findContactByID(
                conversation.getBuddy().getFormatted());

            if(sourceContact == null)
            {
                logger.debug("Received a typing notification from an unknown "+
                             "buddy=" + conversation.getBuddy());
                //create the volatile contact
                sourceContact = opSetPersPresence
                    .createVolatileContact(
                        conversation.getBuddy().getFormatted());
            }

            fireTypingNotificationsEvent(
                sourceContact, typingStateToInt(typingInfo.getTypingState()));
        }

        //the follwoing methods only have dummy implementations here as they
        //do not interest us. complete implementatios are provider in the
        //basic instant messaging operation set.
        public void buddyInfoUpdated(IcbmService service, Screenname buddy,
                                     IcbmBuddyInfo info){}
        public void conversationClosed(Conversation conv){}
        public void gotMessage(Conversation conv, MessageInfo minfo){}
        public void gotOtherEvent(Conversation conversation,
                                  ConversationEventInfo event){}
        public void sentOtherEvent(Conversation conversation,
                                   ConversationEventInfo event){}
        public void canSendMessageChanged(Conversation conv, boolean canSend){}
        public void conversationOpened(Conversation conv){}
        public void newConversation(IcbmService service, Conversation conv){}
        public void sentMessage(Conversation conv, MessageInfo minfo){}

        public void sendAutomaticallyFailed(IcbmService service,
                                            net.kano.joustsim.oscar.oscar.
                                            service.icbm.Message message,
                                            Set<Conversation> triedConversations)
        {
        }
    }
}