/**
   * Create a <tt>SourceContact</tt> from a <tt>GoogleContactsEntry</tt>.
   *
   * @param entry <tt>GoogleContactsEntry</tt>
   */
  private void onGoogleContactsEntry(GoogleContactsEntry entry) {
    String displayName = entry.getFullName();
    if (displayName == null || displayName.length() == 0) {
      if ((entry.getGivenName() == null || entry.getGivenName().length() == 0)
          && (entry.getFamilyName() == null || entry.getFamilyName().length() == 0)) {
        return;
      }

      displayName = entry.getGivenName() + " " + entry.getFamilyName();
    }

    List<ContactDetail> contactDetails = getContactDetails(entry);

    if (!contactDetails.isEmpty()) {
      GenericSourceContact sourceContact =
          new GenericSourceContact(getContactSource(), displayName, contactDetails);

      try {
        byte img[] =
            GoogleContactsServiceImpl.downloadPhoto(
                ((GoogleContactsEntryImpl) entry).getPhotoLink(),
                getContactSource().getConnection().getGoogleService());
        sourceContact.setImage(img);
      } catch (OutOfMemoryError oome) {
        // Ignore it, the image is not vital.
      }

      addQueryResult(sourceContact);
    }
  }
  /**
   * Gets the <tt>contactDetails</tt> to be set on a <tt>SourceContact</tt>.
   *
   * @param entry <tt>GoogleContactsEntry</tt>
   * @return the <tt>contactDetails</tt> to be set on a <tt>SourceContact</tt>
   */
  private List<ContactDetail> getContactDetails(GoogleContactsEntry entry) {
    List<ContactDetail> ret = new LinkedList<ContactDetail>();
    List<String> homeMails = entry.getHomeMails();
    List<String> workMails = entry.getWorkMails();
    List<String> mobilePhones = entry.getMobilePhones();
    List<String> homePhones = entry.getHomePhones();
    List<String> workPhones = entry.getWorkPhones();
    Map<String, GoogleContactsEntry.IMProtocol> ims = entry.getIMAddresses();
    ContactDetail detail = null;

    for (String mail : homeMails) {
      List<Class<? extends OperationSet>> supportedOpSets =
          new ArrayList<Class<? extends OperationSet>>(1);
      // can be added as contacts
      supportedOpSets.add(OperationSetPersistentPresence.class);

      detail =
          new ContactDetail(
              mail,
              ContactDetail.Category.Email,
              new ContactDetail.SubCategory[] {ContactDetail.SubCategory.Home});
      detail.setSupportedOpSets(supportedOpSets);
      ret.add(detail);
    }
    for (String mail : workMails) {
      List<Class<? extends OperationSet>> supportedOpSets =
          new ArrayList<Class<? extends OperationSet>>(1);
      // can be added as contacts
      supportedOpSets.add(OperationSetPersistentPresence.class);

      detail =
          new ContactDetail(
              mail,
              ContactDetail.Category.Email,
              new ContactDetail.SubCategory[] {ContactDetail.SubCategory.Work});
      detail.setSupportedOpSets(supportedOpSets);
      ret.add(detail);
    }

    for (String homePhone : homePhones) {
      List<Class<? extends OperationSet>> supportedOpSets =
          new ArrayList<Class<? extends OperationSet>>(2);

      supportedOpSets.add(OperationSetBasicTelephony.class);
      // can be added as contacts
      supportedOpSets.add(OperationSetPersistentPresence.class);
      homePhone = PhoneNumberI18nService.normalize(homePhone);
      detail =
          new ContactDetail(
              homePhone,
              ContactDetail.Category.Phone,
              new ContactDetail.SubCategory[] {ContactDetail.SubCategory.Home});
      detail.setSupportedOpSets(supportedOpSets);
      ret.add(detail);
    }

    for (String workPhone : workPhones) {
      List<Class<? extends OperationSet>> supportedOpSets =
          new ArrayList<Class<? extends OperationSet>>(2);

      supportedOpSets.add(OperationSetBasicTelephony.class);
      // can be added as contacts
      supportedOpSets.add(OperationSetPersistentPresence.class);
      workPhone = PhoneNumberI18nService.normalize(workPhone);
      detail =
          new ContactDetail(
              workPhone,
              ContactDetail.Category.Phone,
              new ContactDetail.SubCategory[] {ContactDetail.SubCategory.Work});
      detail.setSupportedOpSets(supportedOpSets);
      ret.add(detail);
    }

    for (String mobilePhone : mobilePhones) {
      List<Class<? extends OperationSet>> supportedOpSets =
          new ArrayList<Class<? extends OperationSet>>(2);

      supportedOpSets.add(OperationSetBasicTelephony.class);
      // can be added as contacts
      supportedOpSets.add(OperationSetPersistentPresence.class);
      mobilePhone = PhoneNumberI18nService.normalize(mobilePhone);
      detail =
          new ContactDetail(
              mobilePhone,
              ContactDetail.Category.Phone,
              new ContactDetail.SubCategory[] {ContactDetail.SubCategory.Mobile});
      detail.setSupportedOpSets(supportedOpSets);
      ret.add(detail);
    }

    for (Map.Entry<String, GoogleContactsEntry.IMProtocol> im : ims.entrySet()) {
      if (im.getValue() != GoogleContactsEntry.IMProtocol.OTHER) {
        ContactDetail.SubCategory imSubCat;
        switch (im.getValue()) {
          case AIM:
            imSubCat = ContactDetail.SubCategory.AIM;
            break;
          case ICQ:
            imSubCat = ContactDetail.SubCategory.ICQ;
            break;
          case YAHOO:
            imSubCat = ContactDetail.SubCategory.Yahoo;
            break;
          case JABBER:
            imSubCat = ContactDetail.SubCategory.Jabber;
            break;
          case MSN:
            imSubCat = ContactDetail.SubCategory.MSN;
            break;
          case GOOGLETALK:
            imSubCat = ContactDetail.SubCategory.GoogleTalk;
            break;
          default:
            imSubCat = null;
            break;
        }

        detail =
            new ContactDetail(
                im.getKey(),
                ContactDetail.Category.InstantMessaging,
                new ContactDetail.SubCategory[] {imSubCat});

        setIMCapabilities(detail, im.getValue());

        // can be added as contacts
        detail.getSupportedOperationSets().add(OperationSetPersistentPresence.class);

        ret.add(detail);
      }
    }

    return ret;
  }