nsresult
nsUnixSystemProxySettings::GetProxyFromGConf(const nsACString& aScheme,
                                             const nsACString& aHost,
                                             PRInt32 aPort,
                                             nsACString& aResult)
{
  PRBool masterProxySwitch = PR_FALSE;
  mGConf->GetBool(NS_LITERAL_CSTRING("/system/http_proxy/use_http_proxy"), &masterProxySwitch);
  if (!IsProxyMode("manual") || !masterProxySwitch) {
    aResult.AppendLiteral("DIRECT");
    return NS_OK;
  }
  
  nsCOMPtr<nsIArray> ignoreList;
  if (NS_SUCCEEDED(mGConf->GetStringList(NS_LITERAL_CSTRING("/system/http_proxy/ignore_hosts"),
                                         getter_AddRefs(ignoreList))) && ignoreList) {
    PRUint32 len = 0;
    ignoreList->GetLength(&len);
    for (PRUint32 i = 0; i < len; ++i) {
      nsCOMPtr<nsISupportsString> str = do_QueryElementAt(ignoreList, i);
      if (str) {
        nsAutoString s;
        if (NS_SUCCEEDED(str->GetData(s)) && !s.IsEmpty()) {
          if (GConfIgnoreHost(NS_ConvertUTF16toUTF8(s), aHost)) {
            aResult.AppendLiteral("DIRECT");
            return NS_OK;
          }
        }
      }
    }
  }

  PRBool useHttpProxyForAll = PR_FALSE;
  // This setting sometimes doesn't exist, don't bail on failure
  mGConf->GetBool(NS_LITERAL_CSTRING("/system/http_proxy/use_same_proxy"), &useHttpProxyForAll);

  nsresult rv;
  if (!useHttpProxyForAll) {
    rv = SetProxyResultFromGConf("/system/proxy/socks_", "SOCKS", aResult);
    if (NS_SUCCEEDED(rv))
      return rv;
  }
  
  if (aScheme.LowerCaseEqualsLiteral("http") || useHttpProxyForAll) {
    rv = SetProxyResultFromGConf("/system/http_proxy/", "PROXY", aResult);
  } else if (aScheme.LowerCaseEqualsLiteral("https")) {
    rv = SetProxyResultFromGConf("/system/proxy/secure_", "PROXY", aResult);
  } else if (aScheme.LowerCaseEqualsLiteral("ftp")) {
    rv = SetProxyResultFromGConf("/system/proxy/ftp_", "PROXY", aResult);
  } else {
    rv = NS_ERROR_FAILURE;
  }
  
  if (NS_FAILED(rv)) {
    aResult.AppendLiteral("DIRECT");
  }
  return NS_OK;
}
nsresult
nsUnixSystemProxySettings::GetPACURI(nsACString& aResult)
{
  if (!mGConf || !IsProxyMode("auto")) {
    // Return an empty string in this case
    aResult.Truncate();
    return NS_OK;
  }

  return mGConf->GetString(NS_LITERAL_CSTRING("/system/proxy/autoconfig_url"),
                           aResult);
}
nsresult
nsUnixSystemProxySettings::GetPACURI(nsACString& aResult)
{
  if (mProxySettings) {
    nsCString proxyMode;
    // Check if mode is auto
    nsresult rv = mProxySettings->GetString(NS_LITERAL_CSTRING("mode"), proxyMode);
    if (rv == NS_OK && proxyMode.EqualsLiteral("auto")) {
      return mProxySettings->GetString(NS_LITERAL_CSTRING("autoconfig-url"), aResult);
    }
    /* The org.gnome.system.proxy schema has been found, but auto mode is not set.
     * Don't try the GConf and return empty string. */
    aResult.Truncate();
    return NS_OK;
  }

  if (mGConf && IsProxyMode("auto")) {
    return mGConf->GetString(NS_LITERAL_CSTRING("/system/proxy/autoconfig_url"),
                             aResult);
  }
  // Return an empty string when auto mode is not set.
  aResult.Truncate();
  return NS_OK;
}