aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/rss/ContactRssImpl.java
blob: 7d4cca6c4a9e6a7ffe6b04e1d09f79f59772fc05 (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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
/*
 * 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.awt.*;
import java.awt.image.*;
import java.io.*;
import java.net.*;
import java.util.*;

import javax.imageio.*;

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

import com.ctreber.aclib.image.ico.*;

/**
 * An implementation of a rss Contact.
 *
 * @author Jean-Albert Vescovo
 * @author Mihai Balan
 */
public class ContactRssImpl
    implements Contact
{
    private static final Logger logger
        = Logger.getLogger(ContactRssImpl.class);

    /**
     * Item key identifying the last item retrieved and displayed.
     */
    //private RssItemKey lastItem = new RssItemKey(new Date(0));

    /**
     * Contact's nickname.
     */
    private String nickName = null;

    /**
     * Stores the contact's display image to avoid downloading it multiple times.
     */
    private byte[] icon;

    /**
     * This contact's URL (URL of the RSS feed).
     */
    //private URL rssURL = null;

    /**
     * This contact id (http://... or feed://...)
     */
    private String contactID = null;

    /**
     * The provider that created us.
     */
    private ProtocolProviderServiceRssImpl parentProvider = null;

    /**
     * The group that the contact belongs to.
     */
    private ContactGroupRssImpl parentGroup = null;

    /**
     * The presence status of the contact.
     */
    private PresenceStatus presenceStatus = RssStatusEnum.OFFLINE;

    /**
     * Determines whether this contact is persistent, i.e. member of the contact
     * list or whether it is here only temporarily.
     */
    private boolean isPersistent = true;

    /**
     * Determines whether the contact has been resolved (i.e. we have a
     * confirmation that it is still on the server contact list).
     */
    private boolean isResolved = false;

    /**
     * The feed reader that we'll be using to retrieve the RSS flow associated
     * with this contact.
     */
    private RssFeedReader rssFeedReader = null;

    /**
     * Creates an instance of a meta contact with the specified string used
     * as a name and identifier.
     *
     * @param rssURL the URL of the rss feed that this contact will be wrapping.
     * @param rssFeedReader the feed reader that we'll be using to retrieve
     * the rss flow associated with this contact.
     * @param parentProvider the provider that created us.
     */
    public ContactRssImpl(String contactID,
                          URL rssURL,
                          //RssFeedReader rssFeedReader,
                          String persistentData,
                          ProtocolProviderServiceRssImpl parentProvider)
        throws OperationFailedException, FileNotFoundException
    {
        this.contactID = contactID;
        //this.rssURL = rssURL;
        this.parentProvider = parentProvider;
        if(persistentData == null)
        {
            this.rssFeedReader = new RssFeedReader(rssURL);
        }
        else
        {
            this.rssFeedReader = RssFeedReader.deserialize(rssURL, persistentData);
            this.nickName = this.rssFeedReader.getTitle();
        }
        //this.rssFeedReader = rssFeedReader;
    }

    /**
     * This method is only called when the contact is added to a new
     * <tt>ContactGroupRssImpl</tt> by the
     * <tt>ContactGroupRssImpl</tt> itself.
     *
     * @param newParentGroup the <tt>ContactGroupRssImpl</tt> that is now
     * parent of this <tt>ContactRssImpl</tt>
     */
    void setParentGroup(ContactGroupRssImpl newParentGroup)
    {
        this.parentGroup = newParentGroup;
    }

    /**
     * Returns a String that can be used for identifying the contact.
     *
     * @return a String id representing that uniquely identifies the contact.
     */
    public String getAddress()
    {
        return contactID;
    }

    /**
     * Returns the URL that this contact is representing.
     *
     * @return the URL of the RSS flow that this contact represents.
     */
    /*public URL getRssURL()
    {
        return rssURL;
    }*/

    /**
     * Returns a String that could be used by any user interacting modules
     * for referring to this contact.
     *
     * @return a String that can be used for referring to this contact when
     *   interacting with the user.
     */
    public String getDisplayName()
    {
        if(nickName == null)
        {
            return contactID;
        }
        else
        {
            return nickName;
        }
    }

    /***
     * Sets the contact's nickname.
     * @param nickName
     */
    public void setDisplayName(String nickName)
    {
        this.nickName = nickName;
    }

    /***
     * Returns a <tt>RssItemKey</tt> object that identifies the last retrieved
     * item in the feed.
     * @return key identifying the last item in the feed.
     */
    /*public RssItemKey getLastItemKey()
    {
        return this.lastItem;
    }*/

    /***
     * Sets the key identifying the last item in the feed. It's usually used in
     * conjunction with a new <tt>RssItemKey</tt> object. For instance:
     * <code>contact.setLastItemKey(new RssItemKey(new Date()));</code>
     *
     * @param key key identifying the last item in the feed or (at least)
     * allowing differencing for newer items.
     *
     * @see RssItemKey
     */
    /*public void setLastItemKey(RssItemKey key)
    {
        this.lastItem= key;
    }*/

    /**
     * Returns an avatar if one is already present or <tt>null</tt> in case it
     * is not in which case it the method also queues the contact for image
     * updates.
     *
     * @return the avatar of this contact or <tt>null</tt> if no avatar is
     * currently available.
     */
    public byte[] getImage()
    {
        return getImage(true);
    }

    /**
     * Returns a reference to the image assigned to this contact. If no image
     * is present and the retrieveIfNecessary flag is true, we schedule the
     * image for retrieval from the server.
     *
     * @param retrieveIfNecessary specifies whether the method should queue
     * this contact for avatar update from the server.
     *
     * @return a reference to the image currently stored by this contact.
     */
    byte[] getImage(boolean retrieveIfNecessary)
    {
        if(icon == null && retrieveIfNecessary)
        {
            OperationSetPersistentPresenceRssImpl pres
                = (OperationSetPersistentPresenceRssImpl)this.parentProvider
                    .getOperationSet(OperationSetPersistentPresence.class);

            pres.getImageRetriever().addContact(this);
        }

        return icon;
    }

    /**
     *  Set the image of the contact.
     *
     * @param the bytes of the image that we'd like to set.
     */
    void setImage(byte[] imgBytes)
    {
        this.icon = imgBytes;
    }

    /**
     * Returns the status of the contact.
     *
     * @return Current presence status of the contact.
     */
    public PresenceStatus getPresenceStatus()
    {
        return this.presenceStatus;
    }

    /**
     * Sets <tt>rssPresenceStatus</tt> as the PresenceStatus that this
     * contact is currently in.
     * @param rssPresenceStatus the <tt>RssPresenceStatus</tt>
     * currently valid for this contact.
     */
    public void setPresenceStatus(PresenceStatus rssPresenceStatus)
    {
        this.presenceStatus = rssPresenceStatus;
    }

    /**
     * Returns a reference to the protocol provider that created the contact.
     *
     * @return a reference to an instance of the ProtocolProviderService
     */
    public ProtocolProviderService getProtocolProvider()
    {
        return parentProvider;
    }

    /**
     * Determines whether or not this contact represents our own identity.
     *
     * @return true in case this is a contact that represents ourselves and
     *   false otherwise.
     */
    public boolean isLocal()
    {
        return false;
    }

    /**
     * Returns the group that contains this contact.
     * @return a reference to the <tt>ContactGroupRssImpl</tt> that
     * contains this contact.
     */
    public ContactGroup getParentContactGroup()
    {
        return this.parentGroup;
    }

    /**
     * Returns a string representation of this contact, containing most of its
     * representative details.
     *
     * @return  a string representation of this contact.
     */
    public String toString()
    {
        StringBuffer buff
            = new StringBuffer("ContactRssImpl[ DisplayName=")
                .append(getDisplayName()).append("]");

        return buff.toString();
    }

    /**
     * Determines whether or not this contact is being stored by the server.
     * Non persistent contacts are common in the case of simple, non-persistent
     * presence operation sets. They could however also be seen in persistent
     * presence operation sets when for example we have received an event
     * from someone not on our contact list. Non persistent contacts are
     * volatile even when coming from a persistent presence operation set. They
     * would only exist until the application is closed and will not be there
     * next time it is loaded.
     *
     * @return true if the contact is persistent and false otherwise.
     */
    public boolean isPersistent()
    {
        return isPersistent;
    }

    /**
     * Specifies whether or not this contact is being stored by the server.
     * Non persistent contacts are common in the case of simple, non-persistent
     * presence operation sets. They could however also be seen in persistent
     * presence operation sets when for example we have received an event
     * from someone not on our contact list. Non persistent contacts are
     * volatile even when coming from a persistent presence operation set. They
     * would only exist until the application is closed and will not be there
     * next time it is loaded.
     *
     * @param isPersistent true if the contact is persistent and false
     * otherwise.
     */
    public void setPersistent(boolean isPersistent)
    {
        this.isPersistent = isPersistent;
    }

    /***
     * Produces a textual representation of contact data that can be used to
     * restore the contact even before the network connection has been
     * initialized. This data contains the key identifying the last displayed
     * item, so that upon restart, items that have already been displayed in
     * older sessions don't get displayed again.
     *
     * @see #setPersistentData(String)
     */
    public String getPersistentData()
    {
        /*RssItemKey lastItem = rssFeedReader.getLastItemKey();
        if (lastItem != null)
            return lastItem.serialize();
        else
            return null;*/
        return  rssFeedReader.serialize();
    }

    /***
     * Restores feed item identification data from their textual representation.
     * @param persistentData textual representation of item key.
     *
     * #setPersistentData()
     */
    /*public void setPersistentData(String persistentData)
    {
        lastItem = RssItemKey.deserialize(persistentData);
    }*/

    /**
     * Determines whether or not this contact has been resolved against the
     * server. Unresolved contacts are used when initially loading a contact
     * list that has been stored in a local file until the presence operation
     * set has managed to retrieve all the contact list from the server and has
     * properly mapped contacts to their on-line buddies.
     *
     * @return true if the contact has been resolved (mapped against a buddy)
     * and false otherwise.
     */
    public boolean isResolved()
    {
        return isResolved;
    }

    /**
     * Makes the contact resolved or unresolved.
     *
     * @param resolved true to make the contact resolved; false to
     * make it unresolved
     */
    public void setResolved(boolean resolved)
    {
        this.isResolved = resolved;
    }

    /**
     * Indicates whether some other object is "equal to" this one which in terms
     * of contacts translates to having equal IDs. The resolved status of the
     * contacts is deliberately ignored so that contacts would be declared equal
     * even if one contact is resolved and the other is not.
     * <p>
     * @param obj the reference object with which to compare.
     * @return  <code>true</code> if this contact has the same ID as that of the
     * <code>obj</code> argument.
     */
    public boolean equals(Object obj)
    {
        if (obj == null
                || ! (obj instanceof ContactRssImpl))
            return false;

        ContactRssImpl rssContact = (ContactRssImpl) obj;

        return this.getAddress().equals(rssContact.getAddress());
    }

    /**
     * Overrides <tt>hashCode</tt> from <tt>Object</tt> to ensure that
     * equal objects have same hashcode
     *
     * http://java.sun.com/j2se/1.4.2/docs/api/java/lang/Object.html#equals(java.lang.Object)
     */
    public int hashCode() {
        return getAddress().hashCode();
    }

    /**
     * Returns the persistent presence operation set that this contact belongs
     * to.
     *
     * @return the <tt>OperationSetPersistentPresenceRssImpl</tt> that
     * this contact belongs to.
     */
    public OperationSetPersistentPresenceRssImpl
                                            getParentPresenceOperationSet()
    {
        return (OperationSetPersistentPresenceRssImpl)parentProvider
            .getOperationSet(OperationSetPersistentPresence.class);
    }

    /**
     * Returns the RSS feed reader that we are using to retrieve flows
     * associated with this contact.
     *
     * @return a reference to the <tt>RssFeedReader</tt> that we are using to
     * retrieve the flow associated with this contact.
     */
    public RssFeedReader getRssFeedReader()
    {
        return rssFeedReader;
    }

    /**
     * Return the current status message of this contact.
     *
     * @return null as the protocol has no support of status messages
     */
    public String getStatusMessage()
    {
        return null;
    }
}