📄 contact.java
字号:
/**
* Copyright (C) 2006-2007 Funambol
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
package com.funambol.mailclient.cm;
import com.funambol.mail.Address;
import com.funambol.mail.MailException;
import com.funambol.util.Log;
import com.funambol.util.QuotedPrintable;
import com.funambol.util.StringUtil;
public class Contact {
private static final String VCARD_BEGIN = "BEGIN:VCARD\r\n";
private static final String NL = "\r\n";
/**Record number on the contact store (given after contact has been added)*/
private int contactId;
/** Contact's NickName (optional)*/
private String nickName;
/** Contact's first Name (optional)*/
private String firstName;
/** Contact's last Name (optional)*/
private String lastName;
/** Contact's email (mandatory)*/
private String defaultEmail;
/** Contact's second email (optional)*/
private String email_2;
/** Contact's third email (optional)*/
private String email_3;
/** Contact's home phone (optional)*/
private String homePhone;
/** Contact's work phone (optional)*/
private String jobPhone;
/** Contact's mobile phone (optional)*/
private String mobilePhone;
/** Constant for default email */
public static final int DEFAULT_EMAIL =0;
/** Constant for secondary email */
public static final int SECONDARY_EMAIL = 1;
/** Constant for tertiary email */
public static final int TERTIARY_EMAIL = 2;
//-------------------------------------------------------------- Contructors
public Contact() {
}
public Contact(String firstName, String lastName, String email)
throws ContactManagerException {
this( null,
firstName,
lastName,
email,
null,
null,
null,
null,
null
);
}
public Contact(String newNickName, String email)
throws ContactManagerException {
this( newNickName,
null,
null,
email,
null,
null,
null,
null,
null
);
}
public Contact(String email) throws ContactManagerException {
this( null,
null,
null,
email,
null,
null,
null,
null,
null
);
}
/**
* Instances a new Contact Object
*/
public Contact(Address addr) throws ContactManagerException {
this(null,
null,
null,
addr.getEmail(),
null,
null,
null,
null,
null);
}
/**
* Creates a new instance of Contact
*/
public Contact(
String newNickName,
String newFirstName,
String newLastName,
String newEmail_1,
String newEmail_2,
String newEmail_3,
String newHomePhone,
String newJobPhone,
String newMobilePhone) throws ContactManagerException {
if (StringUtil.isNullOrEmpty(newEmail_1)) {
throw new ContactManagerException("Null value for contact's email"
+ "Email value is mandatory");
}
if (!StringUtil.isNullOrEmpty(newEmail_1)) {
if (newEmail_1.indexOf("@")==-1) {
throw new ContactManagerException("Email doesn't contain '@'" +
" character");
}
}
if (!StringUtil.isNullOrEmpty(newEmail_2)) {
if (newEmail_2.indexOf("@")==-1) {
throw new ContactManagerException("Email doesn't contain '@'" +
" character");
}
}
if (!StringUtil.isNullOrEmpty(newEmail_3)) {
if (newEmail_3.indexOf("@")==-1) {
throw new ContactManagerException("Email doesn't contain '@'" +
" character");
}
}
this.contactId = 0;
this.nickName = newNickName;
this.firstName = newFirstName;
this.lastName = newLastName;
this.defaultEmail = newEmail_1;
this.email_2 = newEmail_2;
this.email_3 = newEmail_3;
this.homePhone = newHomePhone;
this.jobPhone = newJobPhone;
this.mobilePhone= newMobilePhone;
}
public String format() {
StringBuffer vCard = new StringBuffer();
//mandatory field
vCard.append(VCARD_BEGIN);
//mandatory field
vCard.append("FN:").append(this.getVisibleName()).append(NL);
//mandatory field (N:lastname;firstname;)
vCard.append("N:")
.append((lastName != null) ? lastName : "").append(";")
.append((firstName != null) ? firstName : "").append(NL);
//optional fields
appendField(vCard, "EMAIL;INTERNET:", this.getDefaultEmail());
appendField(vCard, "EMAIL;INTERNET;HOME:", this.getEmail_2());
appendField(vCard, "EMAIL;INTERNET;WORK:", this.getEmail_3());
appendField(vCard, "TEL;VOICE;WORK:", this.getJobPhone());
appendField(vCard, "TEL;VOICE;HOME:", this.getHomePhone());
appendField(vCard, "TEL;CELL:", this.getMobilePhone());
//mandatory field
vCard.append("VERSION:2.1\r\n");
vCard.append("END:VCARD\r\n");
return new String(vCard);
}
public void parse(String vCard) throws ContactManagerException {
//Log.debug(" Formatted vCard: ");
int vCardBegin = vCard.indexOf(VCARD_BEGIN);
if (vCardBegin == -1) {
throw new ContactManagerException("Invalid vCard");
}
vCardBegin += VCARD_BEGIN.length();
int vCardLen = vCard.length();
int newline = vCard.indexOf(NL, vCardBegin);
int nlLen = NL.length();
while (newline != -1 && newline <= vCardLen) {
// Find first colon
int colon = vCard.indexOf(':', newline);
if (colon == -1) {
Log.info("wrong vcard: " + vCard);
throw new ContactManagerException("Error in vCard.");
}
String name = vCard.substring(newline+nlLen, ++colon);
// Find next newline
newline = vCard.indexOf(NL, colon);
if (colon == -1) { // If the newline is not found, is an error
Log.info("wrong vcard: " + vCard);
throw new ContactManagerException("unexpected end of vCard. ");
}
String value = vCard.substring(colon, newline);
if("END:".equals(name)) { // end of vCard reached
Log.info("vCard parsed");
break;
}
if ("FN:".equals(name)) { // Full Name
setNickName(value);
}
else if ("FN;ENCODING=QUOTED-PRINTABLE;CHARSET=UTF-8:".equals(name)) {
String decoded = QuotedPrintable.decode(value.getBytes(), "UTF-8");
setNickName(decoded);
}
else if ("N:".equals(name)) { // Name
String[] names = StringUtil.split(value, ";");
//We set only the first given name.
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -