aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/rss/UriHandlerRssImpl.java
blob: 6a9cc12c2235b2f7501ca653b6184c3b7c2a2275 (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
249
250
251
252
/*
 * 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.rss;

import java.util.*;

import org.osgi.framework.*;

import net.java.sip.communicator.service.argdelegation.*;
import net.java.sip.communicator.service.gui.*;
import net.java.sip.communicator.service.protocol.*;
import net.java.sip.communicator.util.*;

/**
 * The RSS implementation of the URI handler. This class handles RSS feeds by
 * adding them to your contact list.
 *
 * @author Emil Ivov
 */
public class UriHandlerRssImpl
    implements UriHandler,
               ServiceListener

{
    private static final Logger logger =
        Logger.getLogger(UriHandlerRssImpl.class);

    /**
     * The protocol provider factory that created us.
     */
    private ProtocolProviderFactory protoFactory = null;

    /**
     * A reference to the OSGi registration we create with this handler.
     */
    private ServiceRegistration ourServiceRegistration = null;

    /**
     * The object that we are using to synchronize our service registration.
     */
    private Object registrationLock = new Object();

    /**
     * Creates an instance of this uri handler, so that it would start handling
     * URIs by passing them to the RSS providers.
     */
    protected UriHandlerRssImpl()
        throws NullPointerException
    {
    }

    /**
     * Registers this UriHandler with the bundle context so that it could
     * start handling URIs
     */
    public void registerHandlerService()
    {
        synchronized(registrationLock)
        {
            if (ourServiceRegistration != null)
            {
                // ... we are already registered (this is probably
                // happening during startup)
                return;
            }

            Hashtable<String, String> registrationProperties
            = new Hashtable<String, String>();

            registrationProperties.put(UriHandler.PROTOCOL_PROPERTY,
                            getProtocol());

            ourServiceRegistration = RssActivator.bundleContext
                            .registerService(UriHandler.class.getName(), this,
                                            registrationProperties);
        }

    }

    /**
     * Unregisters this UriHandler from the bundle context.
     */
    public void unregisterHandlerService()
    {
        synchronized(registrationLock)
        {
            ourServiceRegistration.unregister();
            ourServiceRegistration = null;
        }
    }

    /**
     * Returns the protocol that this handler is responsible for. In this case
     * this would be the "feed" protocol.
     *
     * @return the protocol that this handler is responsible for.
     */
    public String getProtocol()
    {
        return "feed";
    }

    /**
     * Parses the specified URI and creates a call with the currently active
     * telephony operation set.
     *
     * @param uri the feed: URI that we have to call.
     */
    public void handleUri(String uri)
    {
        byte[] icon = RssActivator.getResources()
            .getImageInBytes("pageImageRss");
        int answer = RssActivator.getUIService().getPopupDialog()
            .showConfirmPopupDialog(
                        "Would you like to add the following feed to "
                        +"your list of contacts?\n"
                        +uri,
                        "Add RSS feed?",
                        PopupDialog.YES_NO_OPTION,
                        PopupDialog.QUESTION_MESSAGE,
                        icon);

        if (answer != PopupDialog.YES_OPTION)
        {
            return;
        }

        ProtocolProviderService provider;
        try
        {
            provider = getRssProvider();
        }
        catch (OperationFailedException exc)
        {
            // The operation has been canceled by the user. Bail out.
            logger.trace("User canceled handling of uri " + uri);
            return;
        }

        //if provider is null then we need to tell the user to create an account
        if(provider == null)
        {
            showErrorMessage(
                "You need to configure at least one "
                + "RSS" +" account to be able to add the feed\n"
                + uri,
                null);
            return;
        }

        OperationSetPresence presenceOpSet
            = (OperationSetPresence) provider
                .getOperationSet(OperationSetPresence.class);

        try
        {
            presenceOpSet.subscribe(uri);
        }
        catch (OperationFailedException exc)
        {
            showErrorMessage("Failed to subscribe to the following feed\n"
                            + uri,
                            exc);
        }
    }

    /**
     * The point of implementing a service listener here is so that we would
     * only register our own uri handling service and thus only handle URIs
     * while the factory is available as an OSGi service. We remove ourselves
     * when our factory unregisters its service reference.
     *
     * @param event the OSGi <tt>ServiceEvent</tt>
     */
    public void serviceChanged(ServiceEvent event)
    {
        Object sourceService = RssActivator.bundleContext
            .getService(event.getServiceReference());

        //ignore anything but our protocol factory.
        if( ! (sourceService instanceof ProtocolProviderFactoryRssImpl)
            || (sourceService != protoFactory))
        {
            return;
        }

        if(event.getType() == ServiceEvent.REGISTERED)
        {
            //our factory has just been registered as a service ...
            registerHandlerService();
        }
        else if(event.getType() == ServiceEvent.UNREGISTERING)
        {
            //our factory just died - seppuku.
            unregisterHandlerService();
        }
        else if(event.getType() == ServiceEvent.MODIFIED)
        {
            //we don't care.
        }
    }

    /**
     * Uses the <tt>UIService</tt> to show an error <tt>message</tt> and log
     * and <tt>exception</tt>.
     *
     * @param message the message that we'd like to show to the user.
     * @param exc the exception that we'd like to log
     */
    private void showErrorMessage(String message, Exception exc)
    {
        RssActivator.getUIService().getPopupDialog().showMessagePopupDialog(
                        message,
                        "Failed to create call!",
                        PopupDialog.ERROR_MESSAGE);
        logger.error(message, exc);
    }

    /**
     * Returns the default provider that we are supposed to add feeds to.
     *
     * @return the provider that we should add the  URIs to or null if no
     * provider was found.
     */
    public ProtocolProviderService getRssProvider()
        throws OperationFailedException
    {
        //get a reference to the provider
        ServiceReference[] serRefs = null;
        String osgiFilter = "(" + ProtocolProviderFactory.PROTOCOL
            + "=" + ProtocolNames.RSS + ")";

        try {
            serRefs = RssActivator.bundleContext.getServiceReferences(
                ProtocolProviderService.class.getName(), osgiFilter);
        } catch (InvalidSyntaxException ise)
        {
            //shouldn't happen as the filter is static (typos maybe? :D)
            return null;
        }

        if(serRefs == null || serRefs.length == 0)
            return null;

        return (ProtocolProviderService)RssActivator.getBundleContext()
            .getService(serRefs[0]);
    }
}