aboutsummaryrefslogtreecommitdiffstats
path: root/src/net/java/sip/communicator/impl/protocol/icq/ContactGroupIcqImpl.java
blob: 56bc107d5f0e7354f1e1391148dd20ab15108409 (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
/*
 * 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.kano.joustsim.oscar.oscar.service.ssi.*;
import net.kano.joustsim.*;

/**
 * The ICQ implementation of the ContactGroup interface. Intances of this class
 * (contrary to <code>RootContactGroupIcqImpl</code>) may only contain buddies
 * and cannot have sub groups. Note that instances of this class only use the
 * corresponding joust sim source group for reading their names and only
 * initially fill their <code>buddies</code> <code>java.util.List</code> with
 * the ContactIcqImpl objects corresponding to those contained in the source
 * group at the moment it is being created. They would, however, never try to
 * sync or update their contents ulteriorly. This would have to be done through
 * the addContact()/removeContact() methods.
 *
 * @author Emil Ivov
 */
public class ContactGroupIcqImpl
    extends AbstractContactGroupIcqImpl
{
    private List buddies = new LinkedList();

    /**
     * The JoustSIM Group corresponding to this contact group.
     */
    private MutableGroup joustSimSourceGroup = null;

    /**
     * a list that would always remain empty. We only use it so that we're able
     * to extract empty iterators
     */
    private List dummyGroupsList = new LinkedList();

    /**
     * A variable that we use as a means of detecting changes in the name
     * of this group.
     */
    private String nameCopy = null;

    /**
     * Creates an ICQ group using the specified <code>joustSimGroup</code> as
     * a source. The newly created group will always return the name of the
     * underlying joustSimGroup and would thus automatically adapt to changes.
     * It would, however, not receive or try to poll for modifications of the
     * buddies it contains and would therefore have to be updated manually by
     * ServerStoredContactListImpl.
     * <p>
     * Note that we MUST NOT use the list of buddies obtained through the
     * getBuddiesCopy() of the joustSimGroup arg as we'd later need to be able
     * to directly compare ( == ) instances of buddies we've stored and others
     * that are returned by the framework.
     * <p>
     * @param joustSimGroup the JoustSIM Group correspoinding to the group
     * @param groupMembers the group members that we should add to the group.
     * @param ssclCallback a callback to the server stored contact list
     *
     * we're creating.
     */
    ContactGroupIcqImpl(MutableGroup joustSimGroup,
                        List groupMembers,
                        ServerStoredContactListIcqImpl ssclCallback)
    {
        this.joustSimSourceGroup = joustSimGroup;

        //store a copy of the name now so that we can detect changes in the
        //name of the underlying joustSimSourceGroup
        initNameCopy();

        //do not use the buddies in the joustSimGroup since we want to keep
        //their real addresses and we can only get a list of copies from the
        //group itself.

        for (int i = 0; i < groupMembers.size(); i++)
        {
            addContact( new ContactIcqImpl((Buddy)groupMembers.get(i),
                                           ssclCallback) );
        }

    }

    /**
     * Returns the number of <code>Contact</code> members of this
     * <code>ContactGroup</code>
     *
     * @return an int indicating the number of <code>Contact</code>s,
     *   members of this <code>ContactGroup</code>.
     */
    public int countContacts()
    {
        return buddies.size();
    }

    /**
     * Adds the specified contact at the specified position.
     * @param contact the new contact to add to this group
     * @param index the position where the new contact should be added.
     */
    void addContact(int index, ContactIcqImpl contact)
    {
        buddies.add(index, contact);
    }

    /**
     * Adds the specified contact to the end of this group.
     * @param contact the new contact to add to this group
     */
    void addContact(ContactIcqImpl contact)
    {
        addContact(countContacts(), contact);
    }


    /**
     * Removes the specified contact from this contact group
     * @param contact the contact to remove.
     */
    void removeContact(ContactIcqImpl contact)
    {
        removeContact(buddies.indexOf(contact));
    }

    /**
     * Removes the contact with the specified index.
     * @param index the index of the cntact to remove
     */
    void removeContact(int index)
    {
        buddies.remove(index);
    }

    /**
     * Removes all buddies in this group and reinsterts them as specified
     * by the <code>newOrder</code> param. Contacts not contained in the
     * newOrder list are left at the end of this group.
     *
     * @param newOrder a list containing all contacts in the order that is
     * to be applied.
     *
     */
    void reorderContacts(List newOrder)
    {
        buddies.removeAll(newOrder);
        buddies.addAll(0, newOrder);
    }

    /**
     * Returns an Iterator over all contacts, member of this
     * <code>ContactGroup</code>.
     *
     * @return a java.util.Iterator over all contacts inside this
     *   <code>ContactGroup</code>. In case the group doesn't contain any
     * memebers it will return an empty iterator.
     */
    public Iterator contacts()
    {
        return buddies.iterator();
    }

    /**
     * Returns the <code>Contact</code> with the specified index.
     *
     * @param index the index of the <code>Contact</code> to return.
     * @return the <code>Contact</code> with the specified index.
     */
    public Contact getContact(int index)
    {
        return (ContactIcqImpl) buddies.get(index);
    }

    /**
     * Returns the <code>Contact</code> with the specified address or
     * identifier.
     * @param id the addres or identifier of the <code>Contact</code> we are
     * looking for.
     * @return the <code>Contact</code> with the specified id or address.
     */
    public Contact getContact(String id)
    {
        return this.findContact(id);
    }

    /**
     * Returns the name of this group.
     * @return a String containing the name of this group.
     */
    public String getGroupName()
    {
        return joustSimSourceGroup.getName();
    }

    /**
     * Determines whether the group may contain subgroups or not.
     *
     * @return always false since only the root group may contain subgroups.
     */
    public boolean canContainSubgroups()
    {
        return false;
    }

    /**
     * Returns the subgroup with the specified index (i.e. always null since
     * this group may not contain subgroups).
     *
     * @param index the index of the <code>ContactGroup</code> to retrieve.
     * @return always null
     */
    public ContactGroup getGroup(int index)
    {
        return null;
    }

    /**
     * Returns the subgroup with the specified name.
     * @param groupName the name of the <code>ContactGroup</code> to retrieve.
     * @return the <code>ContactGroup</code> with the specified index.
     */
    public ContactGroup getGroup(String groupName)
    {
        return null;
    }

    /**
     * Returns an empty iterator. Subgroups may only be present in the root
     * group.
     *
     * @return an empty iterator
     */
    public Iterator subGroups()
    {
        return dummyGroupsList.iterator();
    }

    /**
     * Returns the number of subgroups contained by this group, which is
     * always 0 since sub groups in the icq protocol may only be contained
     * by the root group - <code>RootContactGroupIcqImpl</code>.
     * @return a 0 int.
     */
    public int countSubGroups()
    {
        return 0;
    }

    /**
     * Returns a hash code value for the object, which is actually the hashcode
     * value of the groupname.
     *
     * @return  a hash code value for this ContactGroup.
     */
    public int hashCode()
    {
        return getGroupName().hashCode();
    }

    /**
     * Returns the JoustSIM group that this class is encapsulating.
     * @return the JoustSIM group corresponding to this SC group.
     */
    MutableGroup getJoustSimSourceGroup()
    {
        return joustSimSourceGroup;
    }

    /**
     * Indicates whether some other object is "equal to" this group. A group is
     * considered equal to another group if it hase the same sets of (equal)
     * contacts.
     * <p>
     *
     * @param   obj   the reference object with which to compare.
     * @return  <code>true</code> if this object is the same as the obj
     *          argument; <code>false</code> otherwise.
     */
    public boolean equals(Object obj)
    {
        if(    obj == this )
            return true;

        if (obj == null
            || !(obj instanceof ContactGroupIcqImpl) )
               return false;

        if(!((ContactGroup)obj).getGroupName().equals(getGroupName()))
            return false;

        //since ICQ does not support having two groups with the same name
        // at this point we could bravely state that the groups are the same
        // and not bother to compare buddies. (gotta check that though)
        return true;
    }

    /**
     * Returns a string representation of this group, in the form
     * IcqGroup.GroupName[size]{ buddy1.toString(), buddy2.toString(), ...}.
     * @return  a String representation of the object.
     */
    public String toString()
    {
        StringBuffer buff = new StringBuffer("IcqGroup.");
        buff.append(getGroupName());
        buff.append(", childContacts="+countContacts()+":[");

        Iterator contacts = contacts();
        while (contacts.hasNext())
        {
            ContactIcqImpl contact = (ContactIcqImpl) contacts.next();
            buff.append(contact.toString());
            if(contacts.hasNext())
                buff.append(", ");
        }
        return buff.append("]").toString();
    }

    /**
     * Returns the icq contact encapsulating the specified joustSim buddy or null
     * if no such buddy was found.
     *
     * @param joustSimBuddy the buddy whose encapsulating contact we're looking
     * for.
     * @return the <code>ContactIcqImpl</code> corresponding to the specified
     * joustSimBuddy or null if no such contact was found.
     */
    ContactIcqImpl findContact(Buddy joustSimBuddy)
    {
        Iterator contacts = contacts();
        while (contacts.hasNext())
        {
            ContactIcqImpl item = (ContactIcqImpl) contacts.next();
            if(item.getJoustSimBuddy() == joustSimBuddy)
                return item;
        }
        return null;
    }

    /**
     * Returns the index of icq contact encapsulating the specified joustSim
     * buddy or -1 if no such buddy was found.
     *
     * @param joustSimBuddy the buddy whose encapsulating contact's index we're
     * looking for.
     * @return the index of the contact corresponding to the specified
     * joustSimBuddy or null if no such contact was found.
     */
    int findContactIndex(Buddy joustSimBuddy)
    {
        Iterator contacts = contacts();
        int i = 0;
        while (contacts.hasNext())
        {
            ContactIcqImpl item = (ContactIcqImpl) contacts.next();
            if(item.getJoustSimBuddy() == joustSimBuddy)
                return i;
            i++;
        }
        return -1;
    }

    /**
     * Returns the icq contact encapsulating with the spcieified screen name or
     * null if no such contact was found.
     *
     * @param screenName the screenName (or icq UIN) for the contact we're
     * looking for.
     * @return the <code>ContactIcqImpl</code> corresponding to the specified
     * screnname or null if no such contact existed.
     */
    ContactIcqImpl findContact(String screenName)
    {
        Iterator contacts = contacts();
        while (contacts.hasNext())
        {
            ContactIcqImpl item = (ContactIcqImpl) contacts.next();
            if(item.getJoustSimBuddy().getScreenname()
                .equals(new Screenname(screenName)))
                return item;
        }
        return null;
    }

    /**
     * Sets the name copy field that we use as a means of detecing changes in
     * the group name.
     */
    void initNameCopy()
    {
        this.nameCopy = getGroupName();
    }

    /**
     * Returns the name of the group as it was at the last call of initNameCopy.
     * @return a String containing a copy of the name of this group as it was
     * last time when we called <code>initNameCopy</code>.
     */
    String getNameCopy()
    {
        return this.nameCopy;
    }
}