ContactsHelper.cs 13.1 KB
Newer Older
李晓兵's avatar
李晓兵 committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
/*
    Licensed to the Apache Software Foundation (ASF) under one
    or more contributor license agreements.  See the NOTICE file
    distributed with this work for additional information
    regarding copyright ownership.  The ASF licenses this file
    to you under the Apache License, Version 2.0 (the
    "License"); you may not use this file except in compliance
    with the License.  You may obtain a copy of the License at

        http://www.apache.org/licenses/LICENSE-2.0

    Unless required by applicable law or agreed to in writing,
    software distributed under the License is distributed on an
    "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
    KIND, either express or implied.  See the License for the
    specific language governing permissions and limitations
    under the License.
*/

namespace WPCordovaClassLib.Cordova.Commands
{
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using Microsoft.Phone.UserData;
    using System.IO;

    /// <summary>
    /// Implements helper functionality to serialize contact to JSON string.
    /// </summary>
    internal static class ContactsHelper
    {
        /// <summary>
        /// Converts Contact object to string representation
        /// </summary>
        /// <param name="contact">Contact object</param>
        /// <param name="desiredFields">array of fields names</param>
        /// <returns>JSON string</returns>
        public static string ToJson(this Contact contact, string[] desiredFields)
        {
            var contactFieldsWithJsonVals = contact.PopulateContactDictionary();

            // if desiredFields are not defined, use all avilable fields
            if (desiredFields == null || desiredFields.Length == 0)
            {
                desiredFields = contactFieldsWithJsonVals.Keys.ToArray();
            }

            return FillResultWithFields(desiredFields, contactFieldsWithJsonVals);
        }

        /// <summary>
        /// Returns JSON string with desired fields only.
        /// </summary>
        /// <param name="desiredFields">The desired fields.</param>
        /// <param name="contactFieldsWithJsonVals">The contact fields with JSON values.</param>
        /// <returns>JSON string</returns>
        private static string FillResultWithFields(string[] desiredFields, Dictionary<string, Func<string>> contactFieldsWithJsonVals)
        {
            var result = new StringBuilder();
            for (int i = 0; i < desiredFields.Count(); i++)
            {
                if (!contactFieldsWithJsonVals.ContainsKey(desiredFields[i]))
                {
                    continue;
                }

                result.Append(contactFieldsWithJsonVals[desiredFields[i]]());
                if (i != desiredFields.Count() - 1)
                {
                    result.Append(",");
                }
            }

            return "{" + result + "}";
        }

        /// <summary>
        /// Populates the contact dictionary.
        /// </summary>
        /// <param name="contact">Contact, that converted to JSON</param>
        /// <returns>JSON string with populated data</returns>
        private static Dictionary<string, Func<string>> PopulateContactDictionary(this Contact contact)
        {
            var contactFieldsJsonValsDictionary = new Dictionary<string, Func<string>>(StringComparer.InvariantCultureIgnoreCase)
                {
                    { "id", () => string.Format("\"id\":\"{0}\"", contact.GetHashCode()) },
                    { "displayName", () => string.Format("\"displayName\":\"{0}\"", EscapeJson(contact.DisplayName)) },
                    {
                        "nickname",
                        () =>
                        string.Format(
                            "\"nickname\":\"{0}\"",
                            EscapeJson(contact.CompleteName != null ? contact.CompleteName.Nickname : string.Empty))
                    },
                    { "phoneNumbers", () => string.Format("\"phoneNumbers\":[{0}]", FormatJsonPhoneNumbers(contact)) },
                    { "emails", () => string.Format("\"emails\":[{0}]", FormatJsonEmails(contact)) },
                    { "addresses", () => string.Format("\"addresses\":[{0}]", FormatJsonAddresses(contact)) },
                    { "urls", () => string.Format("\"urls\":[{0}]", FormatJsonWebsites(contact)) },
                    { "photos", () => string.Format("\"photos\":[{0}]", FormatJsonPhotos(contact)) },
                    { "name", () => string.Format("\"name\":{0}", FormatJsonName(contact)) },
                    { "note", () => string.Format("\"note\":\"{0}\"", EscapeJson(contact.Notes.FirstOrDefault())) },
                    {
                        "birthday",
                        () =>
                        string.Format(
                            "\"birthday\":\"{0}\"",
                            EscapeJson(Convert.ToString(contact.Birthdays.FirstOrDefault())))
                    }
                };
            return contactFieldsJsonValsDictionary;
        }

        /// <summary>
        /// Add escape characters to the JSON string.
        /// </summary>
        /// <param name="str">Input JSON formatted string</param>
        /// <returns>Escaped JSON string</returns>
        private static string EscapeJson(string str)
        {
            if (string.IsNullOrEmpty(str))
            {
                return str;
            }

            return str.Replace("\n", "\\n")
                      .Replace("\r", "\\r")
                      .Replace("\t", "\\t")
                      .Replace("\"", "\\\"")
                      .Replace("&", "\\&");
        }

        /// <summary>
        /// Formats phone numbers to JSON string.
        /// </summary>
        /// <param name="con">Contact object</param>
        /// <returns>JSON string</returns>
        private static string FormatJsonPhoneNumbers(Contact con)
        {
            string retVal = string.Empty;
            const string ContactFieldFormat = "\"type\":\"{0}\",\"value\":\"{1}\",\"pref\":\"false\"";
            foreach (ContactPhoneNumber number in con.PhoneNumbers)
            {
                string contactField = string.Format(ContactFieldFormat, number.Kind.ToString(), number.PhoneNumber);
                retVal += "{" + contactField + "},";
            }

            return retVal.TrimEnd(',');
        }

        /*
         *  formatted: The complete name of the contact. (DOMString)
            familyName: The contacts family name. (DOMString)
            givenName: The contacts given name. (DOMString)
            middleName: The contacts middle name. (DOMString)
            honorificPrefix: The contacts prefix (example Mr. or Dr.) (DOMString)
            honorificSuffix: The contacts suffix (example Esq.). (DOMString)
         */

        /// <summary>
        /// Formats the name to JSON string.
        /// </summary>
        /// <param name="con">Contact object</param>
        /// <returns>JSON string</returns>
        private static string FormatJsonName(Contact con)
        {
            string retVal;
            const string FormatStr = "\"formatted\":\"{0}\"," +
                               "\"familyName\":\"{1}\"," +
                               "\"givenName\":\"{2}\"," +
                               "\"middleName\":\"{3}\"," +
                               "\"honorificPrefix\":\"{4}\"," +
                               "\"honorificSuffix\":\"{5}\"";

            if (con.CompleteName != null)
            {
                retVal = string.Format(
                    FormatStr,
                    EscapeJson(con.CompleteName.FirstName + " " + con.CompleteName.LastName),
                    //// TODO: does this need suffix? middlename?
                    EscapeJson(con.CompleteName.LastName),
                    EscapeJson(con.CompleteName.FirstName),
                    EscapeJson(con.CompleteName.MiddleName),
                    EscapeJson(con.CompleteName.Title),
                    EscapeJson(con.CompleteName.Suffix));
            }
            else
            {
                retVal = string.Format(FormatStr, "", "", "", "", "", "");
            }

            return "{" + retVal + "}";
        }

        /// <summary>
        /// Format Emails to JSON string.
        /// </summary>
        /// <param name="con">Contact object</param>
        /// <returns>JSON string</returns>
        private static string FormatJsonEmails(Contact con)
        {
            string retVal = string.Empty;
            const string ContactFieldFormat = "\"type\":\"{0}\",\"value\":\"{1}\",\"pref\":\"false\"";
            foreach (ContactEmailAddress address in con.EmailAddresses)
            {
                string contactField = string.Format(
                    ContactFieldFormat,
                    address.Kind.ToString(),
                    EscapeJson(address.EmailAddress));

                retVal += "{" + contactField + "},";
            }

            return retVal.TrimEnd(',');
        }

        /// <summary>
        /// Format Addresses to JSON string.
        /// </summary>
        /// <param name="con">Contact object</param>
        /// <returns>JSON string</returns>
        private static string FormatJsonAddresses(Contact con)
        {
            string retVal = string.Empty;
            foreach (ContactAddress address in con.Addresses)
            {
                retVal += GetFormattedJsonAddress(address, false) + ",";
            }

            return retVal.TrimEnd(',');
        }

        /// <summary>
        /// Format Websites to JSON string.
        /// </summary>
        /// <param name="con">Contact object</param>
        /// <returns>JSON string</returns>
        private static string FormatJsonWebsites(Contact con)
        {
            string retVal = string.Empty;
            foreach (string website in con.Websites)
            {
                retVal += "\"" + EscapeJson(website) + "\",";
            }

            return retVal.TrimEnd(',');
        }

        /// <summary>
        /// Format single address to JSON string.
        /// </summary>
        /// <param name="address">
        /// Contact address.
        /// </param>
        /// <param name="isPrefered">
        /// Contact is preferred?
        /// </param>
        /// <returns>
        /// JSON string
        /// </returns>
        private static string GetFormattedJsonAddress(ContactAddress address, bool isPrefered)
        {
            const string AddressFormatString = "\"pref\":{0}," + // bool
                                         "\"type\":\"{1}\"," +
                                         "\"formatted\":\"{2}\"," +
                                         "\"streetAddress\":\"{3}\"," +
                                         "\"locality\":\"{4}\"," +
                                         "\"region\":\"{5}\"," +
                                         "\"postalCode\":\"{6}\"," +
                                         "\"country\":\"{7}\"";

            string formattedAddress = EscapeJson(address.PhysicalAddress.AddressLine1 + " "
                + address.PhysicalAddress.AddressLine2 + " "
                + address.PhysicalAddress.City + " "
                + address.PhysicalAddress.StateProvince + " "
                + address.PhysicalAddress.CountryRegion + " "
                + address.PhysicalAddress.PostalCode);

            string jsonAddress = string.Format(
                AddressFormatString,
                isPrefered ? "\"true\"" : "\"false\"",
                address.Kind.ToString(),
                formattedAddress,
                EscapeJson(address.PhysicalAddress.AddressLine1 + " " + address.PhysicalAddress.AddressLine2),
                address.PhysicalAddress.City,
                address.PhysicalAddress.StateProvince,
                address.PhysicalAddress.PostalCode,
                address.PhysicalAddress.CountryRegion);

            return "{" + jsonAddress + "}";
        }

        /// <summary>
        /// Formats contact photos to JSON string.
        /// </summary>
        /// <param name="con">Contact object</param>
        /// <returns>JSON string</returns>
        private static string FormatJsonPhotos(Contact con) {

            // we return single photo since contact object instance contains single picture only
            var photoStream = con.GetPicture();
 
            if (photoStream == null) {
                return "";
            }

            return String.Format("{{value:\"{0}\", type: \"data\", pref: false}}", GetImageContent(photoStream));
        }

        /// <summary>
        /// Returns image content in a form of base64 string
        /// </summary>
        /// <param name="stream">Image stream</param>
        /// <returns>Base64 representation of the image</returns>
        private static string GetImageContent(Stream stream)
        {
            byte[] imageContent = null;

            try
            {
                int streamLength = (int)stream.Length;
                imageContent = new byte[streamLength + 1];
                stream.Read(imageContent, 0, streamLength);

            }
            finally
            {
                stream.Dispose();
            }

            return Convert.ToBase64String(imageContent);
        }
    }
}