root / trunk / web / dojo / dojox / xmpp / RosterService.js @ 9
History | View | Annotate | Download (5.73 KB)
1 |
/*
|
---|---|
2 |
Copyright (c) 2004-2010, The Dojo Foundation All Rights Reserved.
|
3 |
Available via Academic Free License >= 2.1 OR the modified BSD license.
|
4 |
see: http://dojotoolkit.org/license for details
|
5 |
*/
|
6 |
|
7 |
|
8 |
if(!dojo._hasResource["dojox.xmpp.RosterService"]){ |
9 |
dojo._hasResource["dojox.xmpp.RosterService"]=true; |
10 |
dojo.provide("dojox.xmpp.RosterService");
|
11 |
dojox.xmpp.roster={ADDED:101,CHANGED:102,REMOVED:103}; |
12 |
dojo.declare("dojox.xmpp.RosterService",null,{constructor:function(_1){ |
13 |
this.session=_1;
|
14 |
},addRosterItem:function(_2,_3,_4){ |
15 |
if(!_2){
|
16 |
throw new Error("Roster::addRosterItem() - User ID is null"); |
17 |
} |
18 |
var _5=this.session.getNextIqId(); |
19 |
var _6={id:_5,from:this.session.jid+"/"+this.session.resource,type:"set"}; |
20 |
var _7=new dojox.string.Builder(dojox.xmpp.util.createElement("iq",_6,false)); |
21 |
_7.append(dojox.xmpp.util.createElement("query",{xmlns:"jabber:iq:roster"},false)); |
22 |
_2=dojox.xmpp.util.encodeJid(_2); |
23 |
if(_2.indexOf("@")==-1){ |
24 |
_2=_2+"@"+this.session.domain; |
25 |
} |
26 |
_7.append(dojox.xmpp.util.createElement("item",{jid:_2,name:dojox.xmpp.util.xmlEncode(_3)},false)); |
27 |
if(_4){
|
28 |
for(var i=0;i<_4.length;i++){ |
29 |
_7.append("<group>");
|
30 |
_7.append(_4[i]); |
31 |
_7.append("</group>");
|
32 |
} |
33 |
} |
34 |
_7.append("</item></query></iq>");
|
35 |
var _8=this.session.dispatchPacket(_7.toString(),"iq",_6.id); |
36 |
_8.addCallback(this,"verifyRoster"); |
37 |
return _8;
|
38 |
},updateRosterItem:function(_9,_a,_b){ |
39 |
if(_9.indexOf("@")==-1){ |
40 |
_9+=_9+"@"+this.session.domain; |
41 |
} |
42 |
var _c={id:this.session.getNextIqId(),from:this.session.jid+"/"+this.session.resource,type:"set"}; |
43 |
var _d=new dojox.string.Builder(dojox.xmpp.util.createElement("iq",_c,false)); |
44 |
_d.append(dojox.xmpp.util.createElement("query",{xmlns:"jabber:iq:roster"},false)); |
45 |
var i=this.session.getRosterIndex(_9); |
46 |
if(i==-1){ |
47 |
return;
|
48 |
} |
49 |
var _e={jid:_9}; |
50 |
if(_a){
|
51 |
_e.name=_a; |
52 |
}else{
|
53 |
if(this.session.roster[i].name){ |
54 |
_e.name=this.session.roster[i].name;
|
55 |
} |
56 |
} |
57 |
if(_e.name){
|
58 |
_e.name=dojox.xmpp.util.xmlEncode(_e.name); |
59 |
} |
60 |
_d.append(dojox.xmpp.util.createElement("item",_e,false)); |
61 |
var _f=_b?_b:this.session.roster[i].groups; |
62 |
if(_f){
|
63 |
for(var x=0;x<_f.length;x++){ |
64 |
_d.append("<group>");
|
65 |
_d.append(_f[x]); |
66 |
_d.append("</group>");
|
67 |
} |
68 |
} |
69 |
_d.append("</item></query></iq>");
|
70 |
var def=this.session.dispatchPacket(_d.toString(),"iq",_c.id); |
71 |
def.addCallback(this,"verifyRoster"); |
72 |
return def;
|
73 |
},verifyRoster:function(res){ |
74 |
if(res.getAttribute("type")=="result"){ |
75 |
}else{
|
76 |
var err=this.session.processXmppError(res); |
77 |
this.onAddRosterItemFailed(err);
|
78 |
} |
79 |
return res;
|
80 |
},addRosterItemToGroup:function(jid,_10){ |
81 |
if(!jid){
|
82 |
throw new Error("Roster::addRosterItemToGroup() JID is null or undefined"); |
83 |
} |
84 |
if(!_10){
|
85 |
throw new Error("Roster::addRosterItemToGroup() group is null or undefined"); |
86 |
} |
87 |
var _11=this.session.getRosterIndex(jid); |
88 |
if(_11==-1){ |
89 |
return;
|
90 |
} |
91 |
var _12=this.session.roster[_11]; |
92 |
var _13=[];
|
93 |
var _14=false; |
94 |
for(var i=0;((_12<_12.groups.length)&&(!_14));i++){ |
95 |
if(_12.groups[i]!=_10){
|
96 |
continue;
|
97 |
} |
98 |
_14=true;
|
99 |
} |
100 |
if(!_14){
|
101 |
return this.updateRosterItem(jid,_12.name,_12.groups.concat(_10),_11); |
102 |
} |
103 |
return dojox.xmpp.xmpp.INVALID_ID;
|
104 |
},removeRosterGroup:function(_15){ |
105 |
var _16=this.session.roster; |
106 |
for(var i=0;i<_16.length;i++){ |
107 |
var _17=_16[i];
|
108 |
if(_17.groups.length>0){ |
109 |
for(var j=0;j<_17.groups.length;j++){ |
110 |
if(_17.groups[j]==_15){
|
111 |
_17.groups.splice(j,1);
|
112 |
this.updateRosterItem(_17.jid,_17.name,_17.groups);
|
113 |
} |
114 |
} |
115 |
} |
116 |
} |
117 |
},renameRosterGroup:function(_18,_19){ |
118 |
var _1a=this.session.roster; |
119 |
for(var i=0;i<_1a.length;i++){ |
120 |
var _1b=_1a[i];
|
121 |
if(_1b.groups.length>0){ |
122 |
for(var j=0;j<_1b.groups.length;j++){ |
123 |
if(_1b.groups[j]==_18){
|
124 |
_1b.groups[j]=_19; |
125 |
this.updateRosterItem(_1b.jid,_1b.name,_1b.groups);
|
126 |
} |
127 |
} |
128 |
} |
129 |
} |
130 |
},removeRosterItemFromGroup:function(jid,_1c){ |
131 |
if(!jid){
|
132 |
throw new Error("Roster::addRosterItemToGroup() JID is null or undefined"); |
133 |
} |
134 |
if(!_1c){
|
135 |
throw new Error("Roster::addRosterItemToGroup() group is null or undefined"); |
136 |
} |
137 |
var _1d=this.session.getRosterIndex(jid); |
138 |
if(_1d==-1){ |
139 |
return;
|
140 |
} |
141 |
var _1e=this.session.roster[_1d]; |
142 |
var _1f=false; |
143 |
for(var i=0;((i<_1e.groups.length)&&(!_1f));i++){ |
144 |
if(_1e.groups[i]!=_1c){
|
145 |
continue;
|
146 |
} |
147 |
_1f=true;
|
148 |
_1d=i; |
149 |
} |
150 |
if(_1f==true){ |
151 |
_1e.groups.splice(_1d,1);
|
152 |
return this.updateRosterItem(jid,_1e.name,_1e.groups); |
153 |
} |
154 |
return dojox.xmpp.xmpp.INVALID_ID;
|
155 |
},rosterItemRenameGroup:function(jid,_20,_21){ |
156 |
if(!jid){
|
157 |
throw new Error("Roster::rosterItemRenameGroup() JID is null or undefined"); |
158 |
} |
159 |
if(!_21){
|
160 |
throw new Error("Roster::rosterItemRenameGroup() group is null or undefined"); |
161 |
} |
162 |
var _22=this.session.getRosterIndex(jid); |
163 |
if(_22==-1){ |
164 |
return;
|
165 |
} |
166 |
var _23=this.session.roster[_22]; |
167 |
var _24=false; |
168 |
for(var i=0;((i<_23.groups.length)&&(!_24));i++){ |
169 |
if(_23.groups[i]==_20){
|
170 |
_23.groups[i]=_21; |
171 |
_24=true;
|
172 |
} |
173 |
} |
174 |
if(_24==true){ |
175 |
return this.updateRosterItem(jid,_23.name,_23.groups); |
176 |
} |
177 |
return dojox.xmpp.xmpp.INVALID_ID;
|
178 |
},renameRosterItem:function(jid,_25){ |
179 |
if(!jid){
|
180 |
throw new Error("Roster::addRosterItemToGroup() JID is null or undefined"); |
181 |
} |
182 |
if(!_25){
|
183 |
throw new Error("Roster::addRosterItemToGroup() New Name is null or undefined"); |
184 |
} |
185 |
var _26=this.session.getRosterIndex(jid); |
186 |
if(_26==-1){ |
187 |
return;
|
188 |
} |
189 |
return this.updateRosterItem(jid,_25,this.session.roster.groups,_26); |
190 |
},removeRosterItem:function(jid){ |
191 |
if(!jid){
|
192 |
throw new Error("Roster::addRosterItemToGroup() JID is null or undefined"); |
193 |
} |
194 |
var req={id:this.session.getNextIqId(),from:this.session.jid+"/"+this.session.resource,type:"set"}; |
195 |
var _27=new dojox.string.Builder(dojox.xmpp.util.createElement("iq",req,false)); |
196 |
_27.append(dojox.xmpp.util.createElement("query",{xmlns:"jabber:iq:roster"},false)); |
197 |
if(jid.indexOf("@")==-1){ |
198 |
jid+=jid+"@"+this.session.domain; |
199 |
} |
200 |
_27.append(dojox.xmpp.util.createElement("item",{jid:jid,subscription:"remove"},true)); |
201 |
_27.append("</query></iq>");
|
202 |
var def=this.session.dispatchPacket(_27.toString(),"iq",req.id); |
203 |
def.addCallback(this,"verifyRoster"); |
204 |
return def;
|
205 |
},getAvatar:function(jid){ |
206 |
},publishAvatar:function(_28,_29){ |
207 |
},onVerifyRoster:function(id){ |
208 |
},onVerifyRosterFailed:function(err){ |
209 |
}}); |
210 |
} |