1 import org.jivesoftware.smack.filter.*; 2 import org.jivesoftware.smack.packet.*; 3 import org.jivesoftware.smack.*; 4 import whisper.PublicKey; 5 import org.jivesoftware.smack.util.StringUtils; 6 7 39 public final class VCard extends IQ{ 40 41 private boolean set=false; 42 private boolean work_address=false; 43 private boolean home_address=false; 44 45 private String FullName; 46 private String Name_Given; 47 private String Name_Middle; 48 private String Name_Family; 49 private String Name_Prefix; 50 private String Name_Suffix; 51 private String Nickname; 52 private String URL; 53 private String Birthday; 54 private String Title; 55 private String Role; 56 private String Org_Name; 57 private String Org_Unit; 58 private String Tel_Work_Voice; 59 private String Tel_Work_Fax; 60 private String Tel_Work_Msg; 61 private String Tel_Home_Voice; 62 private String Tel_Home_Fax; 63 private String Tel_Home_Msg; 64 private String Address_Work_House; 65 private String Address_Work_Street; 66 private String Address_Work_Locality; 67 private String Address_Work_Region; 68 private String Address_Work_PCode; 69 private String Address_Work_Country; 70 private String Address_Home_House; 71 private String Address_Home_Street; 72 private String Address_Home_Locality; 73 private String Address_Home_Region; 74 private String Address_Home_PCode; 75 private String Address_Home_Country; 76 private String Email_Work; 77 private String Email_Home; 78 private String Description; 79 private String JabberID; 80 81 private PublicKey Key; 82 83 84 public VCard(){ 85 } 87 88 90 public String getFullName(){ 91 return FullName; 92 } 93 public String getName_Given(){ 94 return Name_Given; 95 } 96 public String getName_Middle(){ 97 return Name_Middle; 98 } 99 public String getName_Family(){ 100 return Name_Family; 101 } 102 public String getName_Prefix(){ 103 return Name_Prefix; 104 } 105 public String getName_Suffix(){ 106 return Name_Suffix; 107 } 108 public String getNickname(){ 109 return Nickname; 110 } 111 public String getURL(){ 112 return URL; 113 } 114 public String getBirthday(){ 115 return Birthday; 116 } 117 public String getTitle(){ 118 return Title; 119 } 120 public String getRole(){ 121 return Role; 122 } 123 public String getOrg_Name(){ 124 return Org_Name; 125 } 126 public String getOrg_Unit(){ 127 return Org_Unit; 128 } 129 public String getTel_Work_Voice(){ 130 return Tel_Work_Voice; 131 } 132 public String getTel_Work_Fax(){ 133 return Tel_Work_Fax; 134 } 135 public String getTel_Work_Msg(){ 136 return Tel_Work_Msg; 137 } 138 public String getTel_Home_Voice(){ 139 return Tel_Home_Voice; 140 } 141 public String getTel_Home_Fax(){ 142 return Tel_Home_Fax; 143 } 144 public String getTel_Home_Msg(){ 145 return Tel_Home_Msg; 146 } 147 public String getAddress_Work_House(){ 148 return Address_Work_House; 149 } 150 public String getAddress_Work_Street(){ 151 return Address_Work_Street; 152 } 153 public String getAddress_Work_Locality(){ 154 return Address_Work_Locality; 155 } 156 public String getAddress_Work_Region(){ 157 return Address_Work_Region; 158 } 159 public String getAddress_Work_PCode(){ 160 return Address_Work_PCode; 161 } 162 public String getAddress_Work_Country(){ 163 return Address_Work_Country; 164 } 165 public String getAddress_Home_House(){ 166 return Address_Home_House; 167 } 168 public String getAddress_Home_Street(){ 169 return Address_Home_Street; 170 } 171 public String getAddress_Home_Locality(){ 172 return Address_Home_Locality; 173 } 174 public String getAddress_Home_Region(){ 175 return Address_Home_Region; 176 } 177 public String getAddress_Home_PCode(){ 178 return Address_Home_PCode; 179 } 180 public String getAddress_Home_Country(){ 181 return Address_Home_Country; 182 } 183 public String getEmail_Work(){ 184 return Email_Work; 185 } 186 public String getEmail_Home(){ 187 return Email_Home; 188 } 189 public String getDescription(){ 190 return Description; 191 } 192 public String getJabberID(){ 193 return JabberID; 194 } 195 196 197 public PublicKey getKey(){ 198 return Key; 199 } 200 201 203 public void setFullName(String value){ 204 if(value==null){return;} 205 set=true; 206 FullName=value; 207 } 208 public void setName_Given(String value){ 209 if(value==null){return;} 210 set=true; 211 Name_Given=value; 212 } 213 public void setName_Middle(String value){ 214 if(value==null){return;} 215 set=true; 216 Name_Middle=value; 217 } 218 public void setName_Family(String value){ 219 if(value==null){return;} 220 set=true; 221 Name_Family=value; 222 } 223 public void setName_Prefix(String value){ 224 if(value==null){return;} 225 set=true; 226 Name_Prefix=value; 227 } 228 public void setName_Suffix(String value){ 229 if(value==null){return;} 230 set=true; 231 Name_Suffix=value; 232 } 233 public void setNickname(String value){ 234 if(value==null){return;} 235 set=true; 236 Nickname=value; 237 } 238 public void setURL(String value){ 239 if(value==null){return;} 240 set=true; 241 URL=value; 242 } 243 public void setBirthday(String value){ 244 if(value==null){return;} 245 set=true; 246 Birthday=value; 247 } 248 public void setTitle(String value){ 249 if(value==null){return;} 250 set=true; 251 Title=value; 252 } 253 public void setRole(String value){ 254 if(value==null){return;} 255 set=true; 256 Role=value; 257 } 258 public void setOrg_Name(String value){ 259 if(value==null){return;} 260 set=true; 261 Org_Name=value; 262 } 263 public void setOrg_Unit(String value){ 264 if(value==null){return;} 265 set=true; 266 Org_Unit=value; 267 } 268 public void setTel_Work_Voice(String value){ 269 if(value==null){return;} 270 set=true; 271 Tel_Work_Voice=value; 272 } 273 public void setTel_Work_Fax(String value){ 274 if(value==null){return;} 275 set=true; 276 Tel_Work_Fax=value; 277 } 278 public void setTel_Work_Msg(String value){ 279 if(value==null){return;} 280 set=true; 281 Tel_Work_Msg=value; 282 } 283 public void setTel_Home_Voice(String value){ 284 if(value==null){return;} 285 set=true; 286 Tel_Home_Voice=value; 287 } 288 public void setTel_Home_Fax(String value){ 289 if(value==null){return;} 290 set=true; 291 Tel_Home_Fax=value; 292 } 293 public void setTel_Home_Msg(String value){ 294 if(value==null){return;} 295 set=true; 296 Tel_Home_Msg=value; 297 } 298 public void setAddress_Work_House(String value){ 299 if(value==null){return;} 300 set=work_address=true; 301 Address_Work_House=value; 302 } 303 public void setAddress_Work_Street(String value){ 304 if(value==null){return;} 305 set=work_address=true; 306 Address_Work_Street=value; 307 } 308 public void setAddress_Work_Locality(String value){ 309 if(value==null){return;} 310 set=work_address=true; 311 Address_Work_Locality=value; 312 } 313 public void setAddress_Work_Region(String value){ 314 if(value==null){return;} 315 set=work_address=true; 316 Address_Work_Region=value; 317 } 318 public void setAddress_Work_PCode(String value){ 319 if(value==null){return;} 320 set=work_address=true; 321 Address_Work_PCode=value; 322 } 323 public void setAddress_Work_Country(String value){ 324 if(value==null){return;} 325 set=work_address=true; 326 Address_Work_Country=value; 327 } 328 public void setAddress_Home_House(String value){ 329 if(value==null){return;} 330 set=home_address=true; 331 Address_Home_House=value; 332 } 333 public void setAddress_Home_Street(String value){ 334 if(value==null){return;} 335 set=home_address=true; 336 Address_Home_Street=value; 337 } 338 public void setAddress_Home_Locality(String value){ 339 if(value==null){return;} 340 set=home_address=true; 341 Address_Home_Locality=value; 342 } 343 public void setAddress_Home_Region(String value){ 344 if(value==null){return;} 345 set=home_address=true; 346 Address_Home_Region=value; 347 } 348 public void setAddress_Home_PCode(String value){ 349 if(value==null){return;} 350 set=home_address=true; 351 Address_Home_PCode=value; 352 } 353 public void setAddress_Home_Country(String value){ 354 if(value==null){return;} 355 set=home_address=true; 356 Address_Home_Country=value; 357 } 358 public void setEmail_Work(String value){ 359 if(value==null){return;} 360 set=true; 361 Email_Work=value; 362 } 363 public void setEmail_Home(String value){ 364 if(value==null){return;} 365 set=true; 366 Email_Home=value; 367 } 368 public void setDescription(String value){ 369 if(value==null){return;} 370 set=true; 371 Description=value; 372 } 373 public void setJabberID(String value){ 374 if(value==null){return;} 375 set=true; 376 JabberID=value; 377 } 378 379 public void setKey(PublicKey key){ 380 Key=key; 381 } 382 384 390 public static VCard fetch(XMPPConnection connection) throws XMPPException, NullPointerException { 391 if(connection==null){throw new NullPointerException ("Null connection passed to fetch method");} 392 if(!connection.isConnected()){ 393 throw new XMPPException("Not connected"); 394 } 395 if(connection.isAnonymous()){ 396 throw new XMPPException("User must be logged in"); 397 } 398 VCard iq=new VCard(); 399 iq.setType(IQ.Type.GET); 400 iq.setFrom(connection.getUser()); 401 PacketCollector collector=connection.createPacketCollector(new PacketIDFilter(iq.getPacketID())); 402 connection.sendPacket(iq); 403 VCard result; 404 IQ iqresult=null; 405 try{ 406 iqresult=(IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); 407 result=(VCard) iqresult; 408 } 409 catch (ClassCastException cce){ 410 result=new VCard(); } 413 if(result==null){ 414 throw new XMPPException(new XMPPError(408,"Request Timeout")); 415 } 416 if(result.getError()!=null){ 417 throw new XMPPException(result.getError()); 418 } 419 return result; 420 } 421 422 430 public static VCard fetch(XMPPConnection connection, String user) throws XMPPException, NullPointerException { 431 if(connection==null || user==null){ 432 throw new NullPointerException ("Null parameter passed to fetch method"); 433 } 434 if(!connection.isConnected()){ 435 throw new XMPPException("Not connected"); 436 } 437 VCard iq=new VCard(); 438 iq.setType(IQ.Type.GET); 439 iq.setTo(user); 440 PacketCollector collector=connection.createPacketCollector(new PacketIDFilter(iq.getPacketID())); 441 connection.sendPacket(iq); 442 VCard result; 443 IQ iqresult=null; 444 try{ 445 iqresult=(IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); 446 result=(VCard) iqresult; 447 } 448 catch (ClassCastException cce){ 449 result=new VCard(); } 452 if(iqresult==null){ 453 throw new XMPPException(new XMPPError(408,"Request Timeout")); 454 } 455 if(iqresult.getError()!=null){ 456 throw new XMPPException(iqresult.getError()); 457 } 458 return result; 459 } 460 461 466 public void upload(XMPPConnection connection) throws XMPPException, NullPointerException { 467 if(connection==null){throw new NullPointerException ("Null connection passed to fetch method");} 468 if(!connection.isConnected()){ 469 throw new XMPPException("Not connected"); 470 } 471 if(connection.isAnonymous()){ 472 throw new XMPPException("User must be logged in"); 473 } 474 this.setType(IQ.Type.SET); 475 this.setFrom(connection.getUser()); 476 PacketCollector collector=connection.createPacketCollector(new PacketIDFilter(this.getPacketID())); 477 connection.sendPacket(this); 478 IQ result=(IQ)collector.nextResult(SmackConfiguration.getPacketReplyTimeout()); 479 if(result==null){ 480 throw new XMPPException(new XMPPError(408,"Request Timeout")); 481 } 482 if(result.getError()!=null){ 483 throw new XMPPException(result.getError()); 484 } 485 } 486 487 488 public boolean hasHomeAddress(){ 489 return home_address; 490 } 491 492 493 public boolean hasWorkAddress(){ 494 return work_address; 495 } 496 497 private static String print(String text, String tag){ 498 if(text==null){ 499 return ("<"+tag+"/>"); 500 } 501 return ("<"+tag+">"+text+"</"+tag+">"); 502 } 503 504 505 public String getChildElementXML(){ 506 if(!set){ 507 return ("<vCard xmlns='vcard-temp'/>"); 508 } 509 StringBuffer buf = new StringBuffer (); 510 buf.append("<vCard xmlns='vcard-temp'>"); 511 buf.append(print(FullName,"FN")); 513 buf.append("<N>"); 515 if(Name_Prefix!=null){ 516 buf.append(print(Name_Prefix,"PREFIX")); 517 } 518 buf.append(print(Name_Given,"GIVEN")).append(print(Name_Middle,"MIDDLE")); 519 buf.append(print(Name_Family,"FAMILY")); 520 if(Name_Suffix!=null){ 521 buf.append(print(Name_Suffix,"SUFFIX")); 522 } 523 buf.append("</N>"); 524 buf.append(print(Nickname,"NICKNAME")); 525 if(URL!=null){ 526 buf.append("<URL>"+URL+"</URL>"); 527 } 528 if(Birthday!=null){ 529 buf.append("<BDAY>"+Birthday+"</BDAY>"); 530 } 531 if(Title!=null){ 532 buf.append("<TITLE>"+Title+"</TITLE>"); 533 } 534 if(Role!=null){ 535 buf.append("<ROLE>"+Role+"</ROLE>"); 536 } 537 if(!(Org_Name==null && Org_Unit==null)){ 539 buf.append("<ORG>").append(print(Org_Name,"ORGNAME")).append(print(Org_Unit,"ORGUNIT")).append("</ORG>"); 540 } 541 if(work_address){ 543 buf.append("<ADR>").append("<WORK/>"); 544 buf.append(print(Address_Work_House,"EXTADR")); 545 buf.append(print(Address_Work_Street,"STREET")); 546 buf.append(print(Address_Work_Locality,"LOCALITY")); 547 buf.append(print(Address_Work_Region,"REGION")); 548 buf.append(print(Address_Work_PCode,"PCODE")); 549 buf.append(print(Address_Work_Country,"CTRY")).append("</ADR>"); 550 } 551 if(home_address){ 553 buf.append("<ADR>").append("<HOME/>"); 554 buf.append(print(Address_Home_House,"EXTADR")); 555 buf.append(print(Address_Home_Street,"STREET")); 556 buf.append(print(Address_Home_Locality,"LOCALITY")); 557 buf.append(print(Address_Home_Region,"REGION")); 558 buf.append(print(Address_Home_PCode,"PCODE")); 559 buf.append(print(Address_Home_Country,"CTRY")).append("</ADR>"); 560 } 561 if(Email_Work!=null){ 563 buf.append("<EMAIL><INTERNET/><WORK/><USERID>"+Email_Work+"</USERID></EMAIL>"); 564 } 565 if(Email_Home!=null){ 566 buf.append("<EMAIL><INTERNET/><HOME/><USERID>"+Email_Home+"</USERID></EMAIL>"); 567 } 568 if(Tel_Work_Voice!=null){ 570 buf.append("<TEL><WORK/><VOICE/><NUMBER>"+Tel_Work_Voice+"</NUMBER></TEL>"); 571 } 572 if(Tel_Work_Fax!=null){ 573 buf.append("<TEL><WORK/><FAX/><NUMBER>"+Tel_Work_Fax+"</NUMBER></TEL>"); 574 } 575 if(Tel_Work_Msg!=null){ 576 buf.append("<TEL><WORK/><MSG/><NUMBER>"+Tel_Work_Msg+"</NUMBER></TEL>"); 577 } 578 if(Tel_Home_Voice!=null){ 579 buf.append("<TEL><HOME/><VOICE/><NUMBER>"+Tel_Home_Voice+"</NUMBER></TEL>"); 580 } 581 if(Tel_Home_Fax!=null){ 582 buf.append("<TEL><HOME/><FAX/><NUMBER>"+Tel_Home_Fax+"</NUMBER></TEL>"); 583 } 584 if(Tel_Home_Msg!=null){ 585 buf.append("<TEL><HOME/><MSG/><NUMBER>"+Tel_Home_Msg+"</NUMBER></TEL>"); 586 } 587 if(Description!=null){ 588 buf.append("<DESC>"+Description+"</DESC>"); 589 } 590 if(JabberID!=null){ 591 buf.append("<JABBERID>"+JabberID+"</JABBERID>"); 592 } 593 594 if(Key!=null){ 595 buf.append("<KEY>"+Key.getText()+"</KEY>"); 596 } 597 598 buf.append("</vCard>"); 599 return buf.toString(); 600 } 601 } | Popular Tags |