/**
   * Fired when softphone is unregistering
   *
   * @param evt RegistrationEvent
   */
  public void unregistering(RegistrationEvent evt) {
    status = SipRegisterStatus.Unregistered;

    registerStatusChanged(
        new RegisterEvent(this, SipRegisterStatus.Unregistering, evt.getReason()));

    int waitUnreg = SIPConfig.getWaitUnregistration();
    if (waitUnreg != -1) {
      try {
        int delay = waitUnreg;
        // we get here through a _synchronous_ call from shutdown so
        // let's try
        // and wait for unregistrations confirmation in case the
        // registrar has requested authorization
        // before conriming unregistration
        if (delay > 0)
          synchronized (unregistrationLock) {
            unregistrationLock.wait(delay);
          }
      } catch (InterruptedException ex) {
        Log.error("unregistering", ex);
      } catch (NumberFormatException ex) {
        Log.error("unregistering", ex);
      }
    }
  }
  /** Create the softphone handlers and stack */
  public void createSoftPhone(String server) throws MediaException {

    this.server = server;
    SIPConfig.setServer(server);

    if (sipManager != null) {
      destroySoftPhone();
    }

    sipManager = new SipManager();
    softPhoneMedia = new SoftPhoneMedia();
    softPhoneSecurity = new SoftPhoneSecurity();

    sipManager.addCommunicationsListener(this);
    sipManager.setSecurityAuthority(softPhoneSecurity);

    try {
      // put in a seperate thread
      sipManager.start();
      if (sipManager.isStarted()) {
        Log.debug("createSoftPhone", "SIP STARTED");
      }
    } catch (CommunicationsException exc) {
      Log.error("createSoftPhone", exc);
    }
  }
  /** Initializes the core phone objects. */
  private void initializePhone() {
    // Load Preferences
    loadPreferences();

    if (preferences == null) {
      return;
    }

    guiManager = new GuiManager();
    guiManager.addUserActionListener(this);
    logManager = new LogManagerImpl(this);

    this.getLogManager().setRemoteLogging(true);

    try {
      EventQueue.invokeAndWait(
          new Runnable() {
            @Override
            public void run() {
              registerMenu = new JCheckBoxMenuItem(PhoneRes.getIString("phone.enabled"));
            }
          });
    } catch (Exception e) {
      Log.error(e);
    }

    registerMenu.addActionListener(
        new ActionListener() {
          public void actionPerformed(ActionEvent actionEvent) {
            if (getStatus() == SipRegisterStatus.Unregistered
                || getStatus() == SipRegisterStatus.RegistrationFailed) {

              register();
            } else {
              handleUnregisterRequest();
            }
          }
        });

    SIPConfig.setPreferredNetworkAddress(preferences.getPreferredAddress());
    NetworkAddressManager.start();

    try {
      EventQueue.invokeAndWait(
          new Runnable() {
            @Override
            public void run() {
              // Initialize Missed calls
              missedCalls = new MissedCalls();
            }
          });
    } catch (Exception e) {
      Log.error(e);
    }

    final JMenu actionsMenu =
        SparkManager.getMainWindow().getMenuByName(Res.getString("menuitem.actions"));
    actionsMenu.add(registerMenu);
  }
  private void loadLocalPreferences() {
    preference = new SipPreference();

    PreferenceManager pm = SparkManager.getPreferenceManager();
    pm.addPreference(preference);

    preferences = (SipPreferences) preference.getData();

    SIPConfig.setUseStun(preferences.isUseStun());
    SIPConfig.setStunServer(preferences.getStunServer());
    SIPConfig.setStunPort(preferences.getStunPort());
    SIPConfig.setPreferredNetworkAddress(preferences.getPreferredAddress());

    preference.setCommitSettings(true);

    if (preferences.isRegisterAtStart()) {
      register();
    }
  }
  /**
   * Setup the Preferences according to remote server
   *
   * @param con XMPPConnection connection
   */
  private void setupRemotePreferences(XMPPConnection con) {

    try {
      ProviderManager.addIQProvider(
          SipAccountPacket.ELEMENT_NAME,
          SipAccountPacket.NAMESPACE,
          new SipAccountPacket.Provider());

      ProviderManager.addIQProvider(
          LogPacket.ELEMENT_NAME, LogPacket.NAMESPACE, new LogPacket.Provider());

      SipAccountPacket sp = SipAccountPacket.getSipSettings(con);

      if (sp != null) {

        SipAccount sipAccount = sp.getSipAccount();

        if (sipAccount != null) {
          this.saccount = sipAccount;
          preference = new SipPreference();
          preference.setShowGUI(false);
          preference.setCommitSettings(false);

          preferences = new SipPreferences();

          preferences.setUserName(sipAccount.getSipUsername());
          preferences.setAuthUserName(sipAccount.getAuthUsername());
          preferences.setServer(sipAccount.getServer());
          preferences.setPassword(sipAccount.getPassword());
          preferences.setRegisterAtStart(true);

          preferences.setStunServer(sipAccount.getStunServer());
          preferences.setStunPort(sipAccount.getStunPort());
          preferences.setUseStun(sipAccount.isUseStun());
          preferences.setVoicemail(sipAccount.getVoiceMailNumber());
          preferences.setOutboundproxy(sipAccount.getOutboundproxy());
          preferences.setPromptCredentials(sipAccount.isPromptCredentials());

          SIPConfig.setUseStun(preferences.isUseStun());
          SIPConfig.setStunServer(preferences.getStunServer());
          SIPConfig.setStunPort(preferences.getStunPort());
          SIPConfig.setOutboundProxy(preferences.getOutboundproxy());

          preference.setData(preferences);
          SIPConfig.setPreferredNetworkAddress(
              NetworkAddressManager.getLocalHost(false).getHostAddress());

          if (preferences.isRegisterAtStart()) {
            register();
          }

          if (preferences.isPromptCredentials()) {
            loadLocalPreferences();
          }

        } else {

        }
      }

    } catch (Exception e) {
      Log.error("setupRemotePreferences", e);
    }
  }