/** 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);
    }
  }
  /**
   * Initializes the server stored list. Synchronize server stored groups and contacts with the
   * local groups and contacts.
   */
  @Override
  public void init() {
    try {
      SipAccountIDImpl accountID = (SipAccountIDImpl) sipProvider.getAccountID();

      if (!accountID.isXiVOEnable()) return;

      boolean useSipCredentials = accountID.isClistOptionUseSipCredentials();

      String serverAddress = accountID.getClistOptionServerUri();
      String username = accountID.getAccountPropertyString(ProtocolProviderFactory.USER_ID);
      Address userAddress = sipProvider.parseAddressString(username);

      if (useSipCredentials) {
        username = ((SipUri) userAddress.getURI()).getUser();
      } else {
        username = accountID.getClistOptionUser();
      }

      try {
        connect(serverAddress);
      } catch (Throwable ex) {
        showError(ex, null, null);
        logger.error("Error connecting to server", ex);
        return;
      }

      Thread thread = new Thread(this, this.getClass().getName());
      thread.setDaemon(true);
      thread.start();

      if (!login(username)) {
        showError(null, null, "Unauthorized. Cannot login.");
        logger.error("Cannot login.");
        return;
      }
    } catch (Throwable t) {
      logger.error("Error init clist from xivo server");
    }
  }