source: 3thparty/jmessenger/src/nu/fw/jeti/backend/roster/NormalJIDStatus.java @ 3952

Revision 3952, 9.7 KB checked in by alexandrecorreia, 13 years ago (diff)

Ticket #1710 - Adicao do codigo fonte java do componente jmessenger(jabberit_messenger)

  • Property svn:executable set to *
Line 
1/*
2 *      Jeti, a Java Jabber client, Copyright (C) 2001 E.S. de Boer 
3 *
4 *  This program is free software; you can redistribute it and/or modify
5 *  it under the terms of the GNU General Public License as published by
6 *  the Free Software Foundation; either version 2 of the License, or
7 *  (at your option) any later version.
8 *
9 *  This program is distributed in the hope that it will be useful,
10 *      but WITHOUT ANY WARRANTY; without even the implied warranty of
11 *      MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12 *      GNU General Public License for more details.
13 *
14 *  You should have received a copy of the GNU General Public License
15 *  along with this program; if not, write to the Free Software
16 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
17 *
18 *      For questions, comments etc,
19 *      use the website at http://jeti.jabberstudio.org
20 *  or mail/IM me at jeti@jabber.org
21 *   
22 */
23
24package nu.fw.jeti.backend.roster;
25
26import java.util.*;
27
28import nu.fw.jeti.jabber.JID;
29import nu.fw.jeti.jabber.JIDStatus;
30import nu.fw.jeti.jabber.elements.Presence;
31import nu.fw.jeti.jabber.elements.RosterItem;
32import nu.fw.jeti.util.I18N;
33import nu.fw.jeti.util.Preferences;
34
35/**
36 * Copyright:    Copyright (c) 2001
37 * @author E.S. de Boer
38 */
39
40 //stores jid + status
41
42//inconsistent with equals to make consistant add jid + equals if comletejids are same
43public class NormalJIDStatus extends SecondaryJIDStatus
44{
45        private JID jid;
46        private String nickname;
47        private String subscription;
48        private String waiting;
49        private List groups;
50        private ArrayList resources;
51        private String resource;
52        private String type;
53        private boolean forcedOffline=false;//if transport loggedoff
54       
55        public NormalJIDStatus(RosterItem item)
56        {//call update??
57                jid = item.getJID();
58                completeJID = jid;
59                show = Presence.UNAVAILABLE;
60                resource = completeJID.getResource();
61                JIDStatus server = Roster.getJIDStatus(new JID(jid.getDomain()));
62                if(server == null) type =null;
63                else type = server.getType();
64                update(item);
65        }
66       
67        //add to special transport jid?
68        public boolean isForcedOffline()
69        {
70                return forcedOffline;
71        }
72       
73        public void setForcedOffline(boolean forced)
74        {
75                forcedOffline = forced;
76        }
77       
78        //TODO REMOVE
79        /**
80         * Make jidStatus from jid and nickname (used by groupchat)
81         * @param jid
82         * @param nickname
83         */
84        public NormalJIDStatus(JID jid,String nickname)
85        {//call update??
86                this.jid = jid;
87                completeJID = jid;
88                this.nickname = nickname;
89                if(nickname == null || nickname.equals(""))
90                {
91                   this.nickname = jid.toString();
92                }
93                show = Presence.UNAVAILABLE;
94                resource = completeJID.getResource();
95                //System.out.println("jidstatus made");
96                JIDStatus server = Roster.getJIDStatus(new JID(jid.getDomain()));
97                if(server == null) type =null;
98                else type = server.getType();
99        }
100
101        public void update(RosterItem item)
102        {
103                nickname = item.getName();
104                if(nickname == null || nickname.equals(""))
105                {
106                        nickname = jid.toString();
107                }
108                subscription = item.getSubscription();
109                waiting = item.getAsk();
110                if(item.getGroups()==null)
111                {
112                        groups = new LinkedList();
113                        groups.add(I18N.gettext("main.main.roster.Unfiled"));
114                }
115                else groups = item.getGroups();
116        }
117
118        public void updatePresence(Presence presence)
119        {//resources
120                super.updatePresence(presence);
121                resource = completeJID.getResource();
122        }
123
124        public void sortSecondary()
125        {//swap resources ,highest in normal
126                if(resources == null) return;
127                Collections.sort(resources);
128                if(this.compareTo(resources.get(0)) > 0)
129                {
130                        //System.out.println("swap");
131                        ResourceJIDStatus resourceJIDStatus = (ResourceJIDStatus) resources.get(0);
132                        ResourceJIDStatus tempJIDStatus =(ResourceJIDStatus) resourceJIDStatus.clone();
133                        resourceJIDStatus.update(completeJID,show,status,online);
134                        completeJID = tempJIDStatus.getCompleteJID();
135                        resource = completeJID.getResource();
136                        show = tempJIDStatus.getShow();
137                        status = tempJIDStatus.getStatus();
138                        online = tempJIDStatus.isOnline();
139                        Collections.sort(resources);
140                }
141        }
142
143        public String toString()
144        {//jid
145                if("msn".equals(type)&& Preferences.getBoolean("jeti","showRealNick", false))
146                {
147                        if(getMSNNick()!=null)return getMSNNick();
148                        if(status!=null && jid.getUser()!=null)
149                        {
150                                 return status;
151                        }
152                        return nickname;
153                }
154                return nickname;
155        }
156
157        public JID getJID(){return jid;}
158
159               
160        public void setStatus(String status)
161        {
162                this.status = status;
163        }
164
165        public String getNick()
166        {
167                return nickname;
168        }
169
170        public void setNick(String nick)
171        {
172                nickname = nick;
173        }
174
175        public String getType()
176        {
177                if(type == null) return "jabber";
178                return type;
179        }
180
181        public void setType(String type)
182        {
183                //System.out.println(type);
184                this.type = type;
185        }
186
187
188//      public StringArray getNamespaces()
189//      {
190//              return namespaces;
191//      }
192//
193//      public void setNamespaces(StringArray namespaces)
194//      {
195//              this.namespaces = namespaces;
196//      }
197
198
199        public String getSubscription(){return subscription;}
200
201        public String getWaiting()
202        {
203                return waiting;
204        }
205       
206        public JIDStatus normalJIDStatus()
207        {
208                return this;
209        }
210
211        public SecondaryJIDStatus getSecondaryJIDStatus(String resource)
212        {
213                if(this.resource == null)
214                {
215                        this.resource = resource;
216                        return this;
217                }
218                if(resource.equals(this.resource)) return this;
219                if(resources !=null)
220                {
221                        for(Iterator i = resources.iterator();i.hasNext();)
222                        {
223                                SecondaryJIDStatus secondary = (SecondaryJIDStatus)i.next();
224                                if(resource.equals(secondary.getCompleteJID().getResource())) return secondary;
225                        }
226                }
227                return null;
228        }
229
230        public ResourceJIDStatus addSecondaryJIDStatus(JID jid)
231        {
232                ResourceJIDStatus resource = new ResourceJIDStatus(jid,this);
233                if(resources == null) resources = new ArrayList(4);
234                resources.add(resource);
235                return resource;
236        }
237       
238       
239        public Iterator getSecondaryJIDStatussen()
240        {
241                if(resources ==null)
242                {
243                        return new Iterator()
244                        {
245                                public boolean hasNext()
246                                {
247                                        return false;
248                                }
249                               
250                                public Object next(){throw new NoSuchElementException();}
251                               
252                                public void remove(){}
253                        };
254                }
255                else
256                {
257                        return new Iterator()
258                        {
259                                private int count = resources.size();
260                                                       
261                                public boolean hasNext()
262                                {
263                                        return count > 0;
264                                }
265                               
266                                public Object next()
267                                {
268                                        count--;
269                                        if(count<0)throw new NoSuchElementException();
270                                        return resources.get(count);
271                                }
272                               
273                                public void remove(){}
274                               
275                        };
276                }
277        }
278       
279        public ResourceJIDStatus removeResource(String resource)
280        {
281                //System.out.println(resource + " mee");
282                //System.out.println(this.resource + " eigen");
283               
284                if(resource == null) return null; //no resource so remove nothing
285                if(resource.equals(this.resource))
286                {
287                        //System.out.println(resource + " equal"    );
288                        if(resources != null)
289                        {
290                                ResourceJIDStatus resourceJIDStatus =(ResourceJIDStatus) resources.remove(0);
291                                completeJID = resourceJIDStatus.getCompleteJID();
292                                this.resource =    completeJID.getResource();
293                                show = resourceJIDStatus.getShow();
294                                status = resourceJIDStatus.getStatus();
295                                online = resourceJIDStatus.isOnline();
296
297                                if(resources.isEmpty()) resources = null;
298                                return resourceJIDStatus ;
299                        }
300                        else this.resource = null;
301                        completeJID = jid;
302                        return null;//resource does not change
303                }
304                if(resources==null) return null;// removing something that not exists
305                for(Iterator i = resources.iterator();i.hasNext();)
306                {
307                        ResourceJIDStatus resourceJIDStatus = (ResourceJIDStatus)i.next();
308                        if(resourceJIDStatus.getCompleteJID().getResource().equals(resource))
309                        {
310                                resources.remove(resourceJIDStatus);
311                                if(resources.isEmpty()) resources = null;
312                                return resourceJIDStatus;
313                        }
314                }
315                System.out.println("error"); //hier hoort ie niet te komen
316                return null;
317        }
318
319        public int groupCount()
320        {
321                if(groups == null) return 0;
322                return groups.size();
323        }
324
325        public List getGroupsCopy()
326        {
327                return new LinkedList(groups);
328        }
329
330        public Iterator getGroups()
331        {
332                return groups.iterator();
333        }
334
335        public boolean isGroupPresent(String group)
336        {
337                return groups.contains(group);
338        }
339       
340/*
341        public boolean equals(Object object)
342        {
343                if (object instanceof JIDStatus)
344                {
345                        JIDStatus temp = (JIDStatus)object;
346                        return temp.getCompleteJID().equals(completeJID);
347                }
348                else return false;
349        }
350       
351        public int hashCode()
352        {
353                System.out.println("hash");
354                return completeJID.hashCode(); 
355        }
356*/     
357
358        public int compareTo(Object o)
359        {//compare jid
360                return compareTo(this,(JIDStatus)o);
361        }
362
363        public int compareTo(JIDStatus jidStatus1,JIDStatus jidStatus)
364        {
365                if(jidStatus1.equals(jidStatus)) return 0;
366                if(jidStatus1.getShow() == jidStatus.getShow())
367                {
368                        if(jidStatus1.getJID().getDomain().equals(nu.fw.jeti.backend.Connect.getMyJID().getDomain()))
369                        {//is this jid own server?
370                                if(jidStatus.getJID().getDomain().equals(nu.fw.jeti.backend.Connect.getMyJID().getDomain()))
371                                {//other jid has the same server
372                                        return (jidStatus1.getJID().toStringNoResource().compareTo(jidStatus.getJID().toString()));
373                                }
374                                return -1;
375                        }
376                        //check if other jid has the server you are logged in to
377                        if(jidStatus.getJID().getDomain().equals(nu.fw.jeti.backend.Connect.getMyJID().getDomain())) return 1;
378                        //check servertypes
379                        if(jidStatus1.getType().equals(jidStatus.getType()))
380                        {
381                                return (jidStatus1.getJID().toStringNoResource().compareTo(jidStatus.getJID().toString()));
382                        }
383                        if (getTypeRank(jidStatus1.getType()) < getTypeRank(jidStatus.getType())) return -1;
384                        return 1;
385                }
386                if(jidStatus1.getShow() < jidStatus.getShow()) return -1;
387                return 1;
388        }
389
390//      private int getShowRank(String show)
391//      {
392//              if(show.equals("chat")) return 0;
393//              if(show.equals("available")) return 1;
394//              if(show.equals("away")) return 2;
395//              if(show.equals("dnd")) return 3;
396//              if(show.equals("xa")) return 4;
397//              return 5;
398//      }
399
400        private int getTypeRank(String type)
401        {
402                if(type.equals("jabber")) return 0;
403                if(type.equals("msn")) return 1;
404                if(type.equals("icq")) return 2;
405                return 3;
406        }
407}
408/*
409 * Overrides for emacs
410 * Local variables:
411 * tab-width: 4
412 * End:
413 */
Note: See TracBrowser for help on using the repository browser.