/** Save application settings to persistent store. */
  public void save() {
    Preferences preferences = getUnderlyingPreferences();

    // Authority certificates
    preferences.putBoolean(KSE3_USECACERTS, useCaCertificates);
    preferences.put(KSE3_CACERTSFILE, caCertificatesFile.toString());
    preferences.putBoolean(KSE3_USEWINTRUSTROOTCERTS, useWindowsTrustedRootCertificates);

    // Trust checks
    preferences.putBoolean(
        KSE3_ENABLEIMPORTTRUSTEDCERTTRUSTCHECK, enableImportTrustedCertTrustCheck);
    preferences.putBoolean(KSE3_ENABLEIMPORTCAREPLYTRUSTCHECK, enableImportCaReplyTrustCheck);

    // Key pair generation
    preferences.put(KSE3_KEYPAIRTYPE, generateKeyPairType.jce());
    preferences.putInt(KSE3_KEYPAIRSIZE, generateKeyPairSize);

    // Secret key generation
    preferences.put(KSE3_SECKEYTYPE, generateSecretKeyType.jce());
    preferences.putInt(KSE3_SECKEYSIZE, generateSecretKeySize);

    // Certificate fingerprint
    preferences.put(KSE3_CERTFINGERTYPE, certificateFingerprintType.jce());

    // Password quality
    preferences.putBoolean(KSE3_PWDQUALENABLE, passwordQualityConfig.getEnabled());
    preferences.putBoolean(KSE3_MINPWDQUALENFORCE, passwordQualityConfig.getEnforced());
    preferences.putInt(KSE3_MINPWDQUAL, passwordQualityConfig.getMinimumQuality());

    // Internet proxy settings
    getCurrentProxySettings(preferences);

    // Application size and position
    preferences.putInt(KSE3_XPOS, sizeAndPosition.x);
    preferences.putInt(KSE3_YPOS, sizeAndPosition.y);
    preferences.putInt(KSE3_WIDTH, sizeAndPosition.width);
    preferences.putInt(KSE3_HEIGHT, sizeAndPosition.height);

    // User interface
    preferences.putBoolean(KSE3_SHOWTOOLBAR, showToolBar);
    preferences.putBoolean(KSE3_SHOWSTATUSBAR, showStatusBar);
    preferences.putInt(KSE3_TABLAYOUT, tabLayout);

    // Recent files
    clearExistingRecentFiles(preferences);
    for (int i = 1; i <= recentFiles.length; i++) {
      preferences.put(KSE3_RECENTFILE + i, recentFiles[i - 1].toString());
    }

    // Current directory
    preferences.put(KSE3_CURRENTDIR, currentDirectory.toString());

    // Look and feel
    preferences.put(KSE3_LOOKFEEL, lookAndFeelClass);
    preferences.putBoolean(KSE3_LOOKFEELDECOR, lookAndFeelDecorated);

    // Licensing
    preferences.putBoolean(KSE3_LICENSEAGREED, licenseAgreed);

    // Tip of the day
    preferences.putBoolean(KSE3_TIPSONSTARTUP, showTipsOnStartUp);
    preferences.putInt(KSE3_TIPINDEX, nextTipIndex);

    // Default distinguished name
    preferences.put(KSE3_DEFAULTDN, defaultDN);

    // SSL host names and ports for "Examine SSL"
    preferences.put(KSE3_SSLHOSTS, getSslHosts());
    preferences.put(KSE3_SSLPORTS, getSslPorts());

    // auto update check settings
    preferences.putBoolean(KSE3_AUTO_UPDATE_CHECK_ENABLED, isAutoUpdateCheckEnabled());
    preferences.putInt(KSE3_AUTO_UPDATE_CHECK_INTERVAL, getAutoUpdateCheckInterval());
    preferences.put(
        KSE3_AUTO_UPDATE_CHECK_LAST_CHECK, DATE_FORMAT.format(getAutoUpdateCheckLastCheck()));

    // PKCS#11 libraries
    preferences.put(KSE3_PKCS11_LIBS, getP11Libs());
  }
  /** Load application settings from persistent store. */
  public void load() {
    Preferences preferences = getUnderlyingPreferences();

    // Authority certificates
    useCaCertificates = preferences.getBoolean(KSE3_USECACERTS, false);
    String cacertsPath =
        preferences.get(
            KSE3_CACERTSFILE, AuthorityCertificates.getDefaultCaCertificatesLocation().toString());
    caCertificatesFile = cleanFilePath(new File(cacertsPath));
    useWindowsTrustedRootCertificates = preferences.getBoolean(KSE3_USEWINTRUSTROOTCERTS, false);

    // Trust checks
    enableImportTrustedCertTrustCheck =
        preferences.getBoolean(KSE3_ENABLEIMPORTTRUSTEDCERTTRUSTCHECK, false);
    enableImportCaReplyTrustCheck =
        preferences.getBoolean(KSE3_ENABLEIMPORTCAREPLYTRUSTCHECK, false);

    // Key pair generation
    generateKeyPairType = KeyPairType.resolveJce(preferences.get(KSE3_KEYPAIRTYPE, RSA.jce()));
    if (generateKeyPairType == null) {
      generateKeyPairType = RSA;
    }
    int defaultKeyPairSize;
    if (generateKeyPairType == RSA) {
      defaultKeyPairSize = 2048;
    } else {
      defaultKeyPairSize = 1024; // DSA
    }
    generateKeyPairSize = preferences.getInt(KSE3_KEYPAIRSIZE, defaultKeyPairSize);

    // Secret key generation
    generateSecretKeyType = SecretKeyType.resolveJce(preferences.get(KSE3_SECKEYTYPE, AES.jce()));
    if (generateSecretKeyType == null) {
      generateSecretKeyType = AES;
    }
    generateSecretKeySize = preferences.getInt(KSE3_SECKEYSIZE, 192);

    // Certificate fingerprint
    certificateFingerprintType =
        DigestType.resolveJce(preferences.get(KSE3_CERTFINGERTYPE, SHA1.jce()));
    if (certificateFingerprintType == null) {
      certificateFingerprintType = SHA1;
    }

    // Password quality
    passwordQualityConfig =
        new PasswordQualityConfig(
            preferences.getBoolean(KSE3_PWDQUALENABLE, false),
            preferences.getBoolean(KSE3_MINPWDQUALENFORCE, false),
            preferences.getInt(KSE3_MINPWDQUAL, 60));

    // Internet proxy settings
    ProxyConfigurationType proxyConfigurationType =
        ProxyConfigurationType.resolve(
            preferences.get(KSE3_PROXY, ProxyConfigurationType.SYSTEM.name()));

    // default should be system settings because of "java.net.useSystemProxies=true", save it for
    // later usage
    SystemProxySelector.setSystemProxySelector(ProxySelector.getDefault());

    switch (proxyConfigurationType) {
      case NONE:
        ProxySelector.setDefault(new NoProxySelector());
        break;
      case PAC:
        // Use PAC URL for proxy configuration
        String pacUrl = preferences.get(KSE3_PACURL, null);
        if (pacUrl != null) {
          ProxySelector.setDefault(new PacProxySelector(pacUrl));
        } else {
          ProxySelector.setDefault(new NoProxySelector());
        }
        break;
      case MANUAL:
        // Use manual settings for HTTP, HTTPS and SOCKS
        ProxyAddress httpProxyAddress = null;
        ProxyAddress httpsProxyAddress = null;
        ProxyAddress socksProxyAddress = null;

        String httpHost = preferences.get(KSE3_HTTPHOST, null);
        int httpPort = preferences.getInt(KSE3_HTTPPORT, 0);

        if (httpHost != null && httpPort > 0) {
          httpProxyAddress = new ProxyAddress(httpHost, httpPort);
        }

        String httpsHost = preferences.get(KSE3_HTTPSHOST, null);
        int httpsPort = preferences.getInt(KSE3_HTTPSPORT, 0);

        if (httpsHost != null && httpsPort > 0) {
          httpsProxyAddress = new ProxyAddress(httpsHost, httpsPort);
        }

        String socksHost = preferences.get(KSE3_SOCKSHOST, null);
        int socksPort = preferences.getInt(KSE3_SOCKSPORT, 0);

        if (socksHost != null && socksPort > 0) {
          socksProxyAddress = new ProxyAddress(socksHost, socksPort);
        }

        if (httpProxyAddress != null || httpsProxyAddress != null) {
          ProxySelector.setDefault(
              new ManualProxySelector(
                  httpProxyAddress, httpsProxyAddress, null, socksProxyAddress));
        } else {
          // no manual settings - use no proxy to connect to the Internet
          ProxySelector.setDefault(new NoProxySelector());
        }
        break;
      case SYSTEM:
      default:
        ProxySelector.setDefault(new SystemProxySelector());
        break;
    }

    // Application size and position
    sizeAndPosition =
        new Rectangle(
            preferences.getInt(KSE3_XPOS, 0),
            preferences.getInt(KSE3_YPOS, 0),
            preferences.getInt(KSE3_WIDTH, KseFrame.DEFAULT_WIDTH),
            preferences.getInt(KSE3_HEIGHT, KseFrame.DEFAULT_HEIGHT));

    // User interface
    showToolBar = preferences.getBoolean(KSE3_SHOWTOOLBAR, true);
    showStatusBar = preferences.getBoolean(KSE3_SHOWSTATUSBAR, true);
    tabLayout = preferences.getInt(KSE3_TABLAYOUT, JTabbedPane.WRAP_TAB_LAYOUT);

    // Recent files
    ArrayList<File> recentFilesList = new ArrayList<File>();
    for (int i = 1; i <= KseFrame.RECENT_FILES_SIZE; i++) {
      String recentFile = preferences.get(KSE3_RECENTFILE + i, null);

      if (recentFile == null) {
        break;
      } else {
        recentFilesList.add(cleanFilePath(new File(recentFile)));
      }
    }
    recentFiles = recentFilesList.toArray(new File[recentFilesList.size()]);

    // Current directory
    String currentDirectoryStr = preferences.get(KSE3_CURRENTDIR, null);
    if (currentDirectoryStr != null) {
      currentDirectory = cleanFilePath(new File(currentDirectoryStr));
    }

    // Look and feel
    lookAndFeelClass = preferences.get(KSE3_LOOKFEEL, null);
    lookAndFeelDecorated = preferences.getBoolean(KSE3_LOOKFEELDECOR, false);

    // Licensing
    licenseAgreed = preferences.getBoolean(KSE3_LICENSEAGREED, false);

    // Tip of the day
    showTipsOnStartUp = preferences.getBoolean(KSE3_TIPSONSTARTUP, true);
    nextTipIndex = preferences.getInt(KSE3_TIPINDEX, 0);

    // Default distinguished name
    defaultDN = preferences.get(KSE3_DEFAULTDN, "");

    // SSL host names and ports for "Examine SSL"
    sslHosts = preferences.get(KSE3_SSLHOSTS, "www.google.com;www.amazon.com");
    sslPorts = preferences.get(KSE3_SSLPORTS, "443");

    // auto update check
    autoUpdateCheckEnabled = preferences.getBoolean(KSE3_AUTO_UPDATE_CHECK_ENABLED, true);
    autoUpdateCheckInterval = preferences.getInt(KSE3_AUTO_UPDATE_CHECK_INTERVAL, 14);
    autoUpdateCheckLastCheck = getDate(preferences, KSE3_AUTO_UPDATE_CHECK_LAST_CHECK, new Date());

    // PKCS#11 libraries
    p11Libs = preferences.get(KSE3_PKCS11_LIBS, "");
  }