1 /** 2 * --------- 3 * |.##> <##.| Open Smart Card Development Platform (www.openscdp.org) 4 * |# #| 5 * |# #| Copyright (c) 1999-2011 CardContact Software & System Consulting 6 * |'##> <##'| Andreas Schwier, 32429 Minden, Germany (www.cardcontact.de) 7 * --------- 8 * 9 * This file is part of OpenSCDP. 10 * 11 * OpenSCDP is free software; you can redistribute it and/or modify 12 * it under the terms of the GNU General Public License version 2 as 13 * published by the Free Software Foundation. 14 * 15 * OpenSCDP is distributed in the hope that it will be useful, 16 * but WITHOUT ANY WARRANTY; without even the implied warranty of 17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 18 * GNU General Public License for more details. 19 * 20 * You should have received a copy of the GNU General Public License 21 * along with OpenSCDP; if not, write to the Free Software 22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 23 * 24 * @fileoverview Create a vCard object 25 */ 26 27 function Vcard() { 28 29 } 30 31 32 /** 33 * Set Name. 34 */ 35 Vcard.prototype.setName = function(firstName, lastName) { 36 this.n = "N:" + lastName + ";" + firstName + "\n"; 37 } 38 39 40 /** 41 * Check if the Name is present. 42 * @return {Boolean} 43 */ 44 Vcard.prototype.hasName = function() { 45 return this.n != undefined; 46 } 47 48 49 /** 50 * Set Formatted Name with the structure: "first name last name". 51 * @param {String} fname 52 */ 53 Vcard.prototype.setFormattedName = function(fname) { 54 this.fn = "FN:" + fname + "\n"; 55 } 56 57 /** 58 * Check if Formatted Name is present. 59 * @return {Boolean} 60 */ 61 Vcard.prototype.hasFormattedName = function() { 62 return this.fn != undefined; 63 } 64 65 66 /** 67 * Set Nickname. 68 * @param {String} nick 69 */ 70 Vcard.prototype.setNickname = function(nick) { 71 this.nickname = "NICKNAME:" + nick + "\n"; 72 } 73 74 75 /** 76 * Check if Nickname is present. 77 * @return {Boolean} 78 */ 79 Vcard.prototype.hasNickname = function() { 80 return this.nickname != undefined; 81 } 82 83 84 /** 85 * Set Birthday 86 * @param {String} bday 87 */ 88 Vcard.prototype.setBirthday = function(bday) { 89 this.bday = "BDAY:" + bday + "\n"; 90 } 91 92 93 /** 94 * Check if Birthday is present. 95 * @return {Boolean} 96 */ 97 Vcard.prototype.hasBirthday = function() { 98 return this.bday != undefined; 99 } 100 101 102 /** 103 * Set Organization. 104 * @param {String} org 105 */ 106 Vcard.prototype.setOrganization = function(org) { 107 this.org = "ORG:" + org + "\n"; 108 } 109 110 111 /** 112 * Check if Organization is present. 113 * @return {Boolean} 114 */ 115 Vcard.prototype.hasOrganization = function() { 116 return this.org != undefined; 117 } 118 119 120 /** 121 * Set Version 122 * @param {String} version 123 */ 124 Vcard.prototype.setVersion = function(version) { 125 this.version = "VERSION:" + version + "\n"; 126 } 127 128 129 /** 130 * Check if Version is present. 131 * @return {Boolean} 132 */ 133 Vcard.prototype.hasVersion = function() { 134 return this.version != undefined; 135 } 136 137 138 /** 139 * Add a telephone number to the buffer. 140 * 141 * @param {String} type The type parameter specify the use of the number or null if not present. 142 * The types are: home, msg, work, pref , fax, pager, etc. 143 * A number can have serveral type which are seperated with commas. 144 * 145 * @param {Number} tel The telephone number. 146 */ 147 Vcard.prototype.addTelephone = function(type, tel) { 148 if (type == null) { 149 tel = "TEL:" + tel + "\n"; 150 } 151 else { 152 tel = "TEL;TYPE=" + type + ":" + tel + "\n"; 153 } 154 if (this.tel == undefined) { 155 this.tel = new ByteBuffer(tel, ASCII); 156 } 157 else { 158 this.tel.append(tel); 159 } 160 } 161 162 163 /** 164 * Check if Telephone Number is present. 165 * @return {Boolean} 166 */ 167 Vcard.prototype.hasTelephone = function() { 168 return this.tel != undefined; 169 } 170 171 172 /** 173 * Add an Email to the buffer. 174 * @param {String} email 175 */ 176 Vcard.prototype.addEmail = function(email) { 177 email = "EMAIL;TYPE=internet:" + email + "\n"; 178 if (this.email == undefined) { 179 this.email = new ByteBuffer(email, ASCII); 180 } 181 else { 182 this.email.append(email, ASCII); 183 } 184 } 185 186 187 /** 188 * Check if Email Address is present. 189 * @return {Boolean} 190 */ 191 Vcard.prototype.hasEmail = function() { 192 return this.email != undefined; 193 } 194 195 196 /** 197 * Set an Address. 198 * @param {String} street 199 * @param {String} city 200 * @param {String} postalCode 201 * @param {String} country 202 */ 203 Vcard.prototype.setAddress = function(street, city, postalCode, country) { 204 this.adr = "ADR:;;" + street + city + ";;" + postalCode + country + "\n"; 205 } 206 207 208 /** 209 * Check if Address is present. 210 * @return {Boolean} 211 */ 212 Vcard.prototype.hasAddress = function() { 213 return this.adr != undefined; 214 } 215 216 217 /** 218 * Set a URL. 219 * @param {String} url 220 */ 221 Vcard.prototype.setUrl = function(url) { 222 this.url = "URL:" + url + "\n"; 223 } 224 225 226 /** 227 * Check if URL is present. 228 * @return {Boolean} 229 */ 230 Vcard.prototype.hasUrl = function() { 231 return this.url != undefined; 232 } 233 234 235 /** 236 * Return the encoded vCard object 237 * @return {ByteString} the encoded vCard 238 */ 239 Vcard.prototype.getEncoded = function() { 240 encoded = new ByteBuffer(); 241 encoded.append(new ByteString("BEGIN:VCARD\n", ASCII)); 242 if (this.hasName()) { 243 encoded.append(this.n); 244 } 245 if (this.hasFormattedName()) { 246 encoded.append(this.fn); 247 } 248 if (this.hasNickname()) { 249 encoded.append(this.nickname); 250 } 251 if (this.hasBirthday()) { 252 encoded.append(this.bday); 253 } 254 if (this.hasOrganization()) { 255 encoded.append(this.org); 256 } 257 if (this.hasVersion()) { 258 encoded.append(this.version); 259 } 260 if (this.hasTelephone()) { 261 encoded.append(this.tel.toByteString()); 262 } 263 if (this.hasEmail()) { 264 encoded.append(this.email.toByteString()); 265 } 266 if (this.hasAddress()) { 267 encoded.append(this.adr); 268 } 269 if (this.hasUrl()) { 270 encoded.append(this.url); 271 } 272 encoded.append(new ByteString("END:VCARD", ASCII)); 273 return encoded.toByteString(); 274 }