home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / lib / libnet / mkpadpac.c < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  3.1 KB  |  109 lines

  1. /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
  2.  *
  3.  * The contents of this file are subject to the Netscape Public License
  4.  * Version 1.0 (the "NPL"); you may not use this file except in
  5.  * compliance with the NPL.  You may obtain a copy of the NPL at
  6.  * http://www.mozilla.org/NPL/
  7.  *
  8.  * Software distributed under the NPL is distributed on an "AS IS" basis,
  9.  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the NPL
  10.  * for the specific language governing rights and limitations under the
  11.  * NPL.
  12.  *
  13.  * The Initial Developer of this code under the NPL is Netscape
  14.  * Communications Corporation.  Portions created by Netscape are
  15.  * Copyright (C) 1998 Netscape Communications Corporation.  All Rights
  16.  * Reserved.
  17.  */
  18. /* mkpadpac.c -- Proxy auto-discovery utility function implementation.
  19.    Created: Judson Valeski, 01.15.1997
  20.  */
  21.  
  22. #include "mkpadpac.h"
  23. #include "mkgeturl.h" /* for NET_GetProxyStyle() */
  24. #include "prefapi.h" /* for js prefs stuff */
  25.  
  26. /* Global pad variables */
  27. PUBLIC XP_Bool foundPADPAC=FALSE;
  28. PUBLIC char *MK_padPacURL=NULL;
  29. PUBLIC XP_Bool MK_PadEnabled=TRUE;
  30.  
  31. MODULE_PRIVATE void net_UsePadPac(XP_Bool useIt) {
  32.     if(useIt) {
  33.         net_PadPacURLPrefChanged(NULL, NULL);
  34.     } else {
  35.         MK_PadEnabled=FALSE;
  36.         NET_ProxyAcLoaded=FALSE;
  37.         XP_FREEIF(MK_padPacURL);
  38.         MK_padPacURL=NULL;
  39.         net_SetPACUrl(NULL);
  40.     }
  41. }
  42.  
  43. /* Return whether we're currently using a pac file via proxy autodiscovery. */
  44. PUBLIC XP_Bool NET_UsingPadPac(void) {
  45.     return (MK_PadEnabled && foundPADPAC && (NET_GetProxyStyle() == PROXY_STYLE_NONE) );
  46. }
  47.  
  48. /* Set the MK_padPacURL varialbe to point to the string passed in. */
  49. PUBLIC XP_Bool NET_SetPadPacURL(char * u) {
  50.     char *url=NULL;
  51.     if(!u || *u == '\0')
  52.         return FALSE;
  53.     StrAllocCopy(url, u);
  54.     if(url && *url) {
  55.         char *host=NET_ParseURL(url, GET_HOST_PART);
  56.         if(host    && *host && (sizeof(host) <= MAXHOSTNAMELEN) ) {
  57.             if(MK_padPacURL)
  58.                 XP_FREE(MK_padPacURL);
  59.             MK_padPacURL=url;
  60.             return TRUE;
  61.         } else {
  62.             XP_FREE(url);
  63.             XP_FREEIF(host);
  64.         }
  65.     } else {
  66.         XP_FREEIF(url);
  67.     }
  68.     return FALSE;
  69. }
  70.  
  71. /* Pref for proxy autodiscovery url */
  72. MODULE_PRIVATE int PR_CALLBACK 
  73. net_PadPacURLPrefChanged(const char *pref, void *data) {
  74.     char s[128];
  75.     int len = sizeof(s);
  76.     XP_MEMSET(s, 0, len);
  77.  
  78.     PREF_GetCharPref(pref_padPacURL, s, &len);
  79.     NET_SetPadPacURL(s);
  80.     return PREF_NOERROR;
  81. }
  82.  
  83. /* Pref for allowing (or not) proxy autodiscovery. */
  84. MODULE_PRIVATE int PR_CALLBACK 
  85. net_EnablePadPrefChanged(const char *pref, void *data) {
  86.     XP_Bool x;
  87.     PREF_GetBoolPref(pref_enablePad, &x);
  88.     MK_PadEnabled=x;
  89.     return PREF_NOERROR;
  90. }
  91.  
  92. /* called from mkgeturl.c, NET_InitNetLib(). 
  93.  * Initializes the pad variables and registers pad callbacks */
  94. PUBLIC void
  95. NET_RegisterPadPrefCallbacks(void) {
  96.     XP_Bool x;
  97.     char s[128];
  98.     int len=sizeof(s);
  99.     XP_MEMSET(s, 0, len);
  100.  
  101.     PREF_GetBoolPref(pref_enablePad, &x);
  102.     MK_PadEnabled=x;
  103.     PREF_RegisterCallback(pref_enablePad, net_EnablePadPrefChanged, NULL);
  104.  
  105.     PREF_GetCharPref(pref_padPacURL, s, &len);
  106.     NET_SetPadPacURL(s);
  107.     PREF_RegisterCallback(pref_padPacURL, net_PadPacURLPrefChanged, NULL);
  108. }
  109.