📄 contact.java
字号:
/**
* Funambol is a mobile platform developed by Funambol, Inc.
* Copyright (C) 2003 - 2007 Funambol, Inc.
*
* This program is free software; you can redistribute it and/or modify it under
* the terms of the GNU Affero General Public License version 3 as published by
* the Free Software Foundation with the addition of the following permission
* added to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED
* WORK IN WHICH THE COPYRIGHT IS OWNED BY FUNAMBOL, FUNAMBOL DISCLAIMS THE
* WARRANTY OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
*
* 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 Affero General Public License
* along with this program; if not, see http://www.gnu.org/licenses or write to
* the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA.
*
* You can contact Funambol, Inc. headquarters at 643 Bair Island Road, Suite
* 305, Redwood City, CA 94063, USA, or at email address info@funambol.com.
*
* The interactive user interfaces in modified source and object code versions
* of this program must display Appropriate Legal Notices, as required under
* Section 5 of the GNU Affero General Public License version 3.
*
* In accordance with Section 7(b) of the GNU Affero General Public License
* version 3, these Appropriate Legal Notices must retain the display of the
* "Powered by Funambol" logo. If the display of the logo is not reasonably
* feasible for technical reasons, the Appropriate Legal Notices must display
* the words "Powered by Funambol".
*/
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;
import java.io.UnsupportedEncodingException;
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(addr.getVisibleName(),
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: " + 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);
//An invalid row has been intercepted: could be an error, but a
//photo too. It is not necessarily a synctactical wrong vcard
if (colon == -1) {
//A new line without ':' char is found: it could be a photo or a
//error on vcard. The parser just skips this line. In case of a
//photo all the photo is skipped, as the parser is looking for
//the new valid colon character. If the photo is sent it is a
//server problem, but the client cannot skip the contact.
//search for the next newline chars
newline = vCard.indexOf(NL+NL, newline+nlLen+nlLen);
//calculate the new position for colon after the newline chars
colon = vCard.indexOf(':', newline);
continue;
}
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: " + vCard);
}
String value = vCard.substring(colon, newline);
if("END:".equals(name)) { // end of vCard reached
Log.info("vCard parsed");
break;
}
if(name.indexOf("PHOTO")>0) { // end of vCard reached
Log.info("Photo tag found");
break;
}
if ("FN:".equals(name)) { // Full Name
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -