/**
   * Sends command to retrieve phones list.
   *
   * @return is command successful.
   */
  @SuppressWarnings("unchecked")
  private boolean getPhoneList() {
    JSONObject obj = new JSONObject();
    try {
      obj.put("class", "phones");
      obj.put("function", "getlist");

      return send(obj);
    } catch (Exception e) {
      logger.error("Error retrieving phones");
      return false;
    }
  }
  /** Handles new incoming object. */
  private void handle(JSONObject incomingObject) {
    if (!incomingObject.containsKey("class")) return;

    try {
      String classField = (String) incomingObject.get("class");

      if (classField.equals("loginko")) {
        showError(null, null, "Unauthorized. Cannot login: "******"errorstring"));

        logger.error("Error login: "******"errorstring"));

        destroy();

        return;
      } else if (classField.equals("login_id_ok")) {
        SipAccountIDImpl accountID = (SipAccountIDImpl) sipProvider.getAccountID();

        boolean useSipCredentials = accountID.isClistOptionUseSipCredentials();

        String password;
        if (useSipCredentials) {
          password = SipActivator.getProtocolProviderFactory().loadPassword(accountID);
        } else {
          password = accountID.getClistOptionPassword();
        }

        if (!authorize((String) incomingObject.get("sessionid"), password))
          logger.error("Error login authorization!");

        return;
      } else if (classField.equals("login_pass_ok")) {
        if (!sendCapas((JSONArray) incomingObject.get("capalist")))
          logger.error("Error send capas!");

        return;
      } else if (classField.equals("login_capas_ok")) {
        if (!sendFeatures(
            (String) incomingObject.get("astid"), (String) incomingObject.get("xivo_userid")))
          logger.error("Problem send features get!");

        return;
      } else if (classField.equals("features")) {
        if (!getPhoneList()) logger.error("Problem send get phones!");

        return;
      } else if (classField.equals("phones")) {
        phonesRecieved(incomingObject);
        return;
      } else if (classField.equals("disconn")) {
        destroy();
        return;
      } else {
        if (logger.isTraceEnabled()) logger.trace("unhandled classField: " + incomingObject);
        return;
      }
    } catch (Throwable t) {
      logger.error("Error handling incoming object", t);
    }
  }
  /**
   * Send needed command for features.
   *
   * @param astid param from previous command.
   * @param xivoUserId param from previous command.
   * @return is command successful.
   */
  @SuppressWarnings("unchecked")
  private boolean sendFeatures(String astid, String xivoUserId) {
    if (connection == null || astid == null || xivoUserId == null) return false;

    JSONObject obj = new JSONObject();
    try {
      obj.put("class", "featuresget");
      obj.put("userid", astid + "/" + xivoUserId);

      return send(obj);
    } catch (Exception e) {
      logger.error("Error send features get command", e);
      return false;
    }
  }
  /**
   * Sends password command.
   *
   * @param sessionId the session id from previous command.
   * @param password the password to authorize.
   * @return is command successful.
   */
  @SuppressWarnings("unchecked")
  private boolean authorize(String sessionId, String password) {
    if (connection == null || sessionId == null || password == null) return false;

    JSONObject obj = new JSONObject();
    try {
      obj.put("class", "login_pass");
      obj.put("hashedpassword", Sha1Crypto.encode(sessionId + ":" + password));

      return send(obj);
    } catch (Exception e) {
      logger.error("Error login with password", e);
      return false;
    }
  }
  /**
   * Sends login command.
   *
   * @param capalistParam param from previous command.
   * @return is command successful.
   */
  @SuppressWarnings("unchecked")
  private boolean sendCapas(JSONArray capalistParam) {
    if (connection == null || capalistParam == null || capalistParam.isEmpty()) return false;

    JSONObject obj = new JSONObject();
    try {
      obj.put("class", "login_capas");
      obj.put("capaid", capalistParam.get(0));
      obj.put("lastconnwins", "false");
      obj.put("loginkind", "agent");
      obj.put("state", "");

      return send(obj);
    } catch (Exception e) {
      logger.error("Error login", e);
      return false;
    }
  }
  /**
   * Sends login command.
   *
   * @param username the username.
   * @return is command successful.
   */
  @SuppressWarnings("unchecked")
  private boolean login(String username) {
    if (connection == null || username == null) return false;

    JSONObject obj = new JSONObject();
    try {
      obj.put("class", "login_id");
      obj.put("company", "Jitsi");

      String os = "x11";
      if (OSUtils.IS_WINDOWS) os = "win";
      else if (OSUtils.IS_MAC) os = "mac";
      obj.put("ident", username + "@" + os);

      obj.put("userid", username);
      obj.put("version", "9999");
      obj.put("xivoversion", "1.1");

      return send(obj);
    } catch (Exception e) {
      logger.error("Error login", e);
      return false;
    }
  }
  /**
   * parses received phones list and creates/resolves groups and contacts
   *
   * @param objReceived the obj with data.
   */
  private void phonesRecieved(JSONObject objReceived) {
    try {
      if (!objReceived.get("function").equals("sendlist") || !objReceived.containsKey("payload"))
        return;

      JSONObject payload = (JSONObject) objReceived.get("payload");
      /*
       * FIXME The following contains two very inefficient Map-iterating
       * loops.
       */
      Iterator iter = payload.keySet().iterator();
      List<JSONObject> phoneList = new ArrayList<JSONObject>();
      while (iter.hasNext()) {
        JSONObject obj = (JSONObject) payload.get(iter.next());
        Iterator phonesIter = obj.keySet().iterator();
        while (phonesIter.hasNext()) phoneList.add((JSONObject) obj.get(phonesIter.next()));
      }

      for (JSONObject phone : phoneList) {
        try {
          // don't handle non sip phones
          if (!((String) phone.get("tech")).equalsIgnoreCase("sip")) continue;

          String groupName = (String) phone.get("context");

          ContactGroupSipImpl parentGroup = findGroupByName(groupName);

          if (parentGroup == null) {
            parentGroup = new ContactGroupSipImpl(groupName, sipProvider);
            parentGroup.setPersistent(true);
            getRootGroup().addSubgroup(parentGroup);

            fireGroupEvent(parentGroup, ServerStoredGroupEvent.GROUP_CREATED_EVENT);
          }

          String number = (String) phone.get("number");

          Address address = sipProvider.parseAddressString(number);

          // if the contact is already in the contact list
          ContactSipImpl contact = parentOperationSet.resolveContactID(address.toString());

          if (contact == null) {
            contact = new ContactSipImpl(address, sipProvider);
            contact.setDisplayName(phone.get("firstname") + " " + phone.get("lastname"));
            contact.setResolved(true);
            parentGroup.addContact(contact);

            fireContactAdded(parentGroup, contact);
          } else {
            contact.setDisplayName(phone.get("firstname") + " " + phone.get("lastname"));
            contact.setResolved(true);

            fireContactResolved(parentGroup, contact);
          }
        } catch (Throwable t) {
          logger.error("Error parsing " + phone);
        }
      }
    } catch (Throwable t) {
      logger.error("Error init list from server", t);
    }
  }