aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/yahoo/OperationSetTypingNotificationsYahooImpl.java
blob: 81e266abdd3b0675c18c74bde4d933bd270cc5b3 (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
/*
 * Jitsi, the OpenSource Java VoIP and Instant Messaging client.
 *
 * Copyright @ 2015 Atlassian Pty Ltd
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package net.java.sip.communicator.impl.protocol.yahoo;

import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.service.protocol.event.*;
import net.java.sip.communicator.util.*;
import ymsg.network.event.*;

/**
 * Maps SIP Communicator typing notifications to those going and coming from
 * smack lib.
 *
 * @author Damian Minkov
 */
public class OperationSetTypingNotificationsYahooImpl
    extends AbstractOperationSetTypingNotifications<ProtocolProviderServiceYahooImpl>
{
    private static final Logger logger =
        Logger.getLogger(OperationSetTypingNotificationsYahooImpl.class);

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

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

        provider.addRegistrationStateChangeListener(new ProviderRegListener());
    }

    /**
     * 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 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 ContactYahooImpl) )
           throw new IllegalArgumentException(
               "The specified contact is not an yahoo contact."
               + notifiedContact);

       if(typingState == OperationSetTypingNotifications.STATE_TYPING)
       {

           parentProvider.getYahooSession().
               keyTyped(notifiedContact.getAddress(),
                       parentProvider.getAccountID().getUserID());
       }
       else
           if(typingState == OperationSetTypingNotifications.STATE_STOPPED ||
           typingState == OperationSetTypingNotifications.STATE_PAUSED)
           {
               parentProvider.getYahooSession().
                   stopTyping(notifiedContact.getAddress(),
                           parentProvider.getAccountID().getUserID());
           }
    }

    private class TypingListener
        extends SessionAdapter
    {
        @Override
        public void notifyReceived(SessionNotifyEvent evt)
        {
            if(evt.isTyping())
            {
                String typingUserID = evt.getFrom();

                if(typingUserID != null)
                {
                    Contact sourceContact =
                        opSetPersPresence.findContactByID(typingUserID);

                    if(sourceContact == null)
                        return;

                    // typing on
                    fireTypingNotificationsEvent(
                        sourceContact,
                        (evt.getMode() == 1) ? STATE_TYPING : STATE_STOPPED);
                }
            }
        }
    }

    /**
     * Our listener that will tell us when we're registered and
     * ready to accept us as a listener.
     */
    private class ProviderRegListener
        implements RegistrationStateChangeListener
    {
        /**
         * The method is called by a ProtocolProvider implementation whenever
         * 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)
        {
            if (logger.isDebugEnabled())
                logger.debug("The provider changed state from: "
                         + evt.getOldState()
                         + " to: " + evt.getNewState());
            if (evt.getNewState() == RegistrationState.REGISTERED)
            {
                opSetPersPresence =
                    (OperationSetPersistentPresenceYahooImpl) parentProvider
                        .getOperationSet(OperationSetPersistentPresence.class);

                parentProvider
                    .getYahooSession().addSessionListener(new TypingListener());
            }
        }
    }
}