home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / macfe / central / InternetConfig.cp < prev    next >
Encoding:
Text File  |  1998-04-08  |  13.5 KB  |  610 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.  
  19. // InternetConfig.cp
  20. //
  21. // Created by Tim Craycroft, 2/9/96
  22. //
  23.  
  24. #include "InternetConfig.h"
  25.  
  26. #include <ICAPI.h>
  27. #include <ICKeys.h>
  28.  
  29. #include <LListener.h>
  30. #include <LPeriodical.h>
  31.  
  32. #include "xp_core.h"
  33. #include "xp_str.h"
  34.  
  35. #include "uprefd.h"
  36. #include "prefapi.h"        // ns/modules/libpref
  37. #include "resgui.h"
  38.  
  39. //
  40. // InternetConfig class.
  41. //
  42. // Checks the IC seed on resume events
  43. //
  44. class CInternetConfig
  45. {
  46.     public:
  47.  
  48.         static void    Connect();
  49.         static void    Disconnect();
  50.         
  51.         static    ICError    GetInternetConfigString(ConstStr255Param    icKey,
  52.                                                 Str255                s,
  53.                                                 long                *port = nil);
  54.  
  55.         static    ICError    GetInternetConfigFileMapping(    OSType fileType, 
  56.                                                         OSType creator, 
  57.                                                         ConstStr255Param fileName,
  58.                                                         ICMapEntry *ent);
  59.  
  60.         static    ICError    LaunchInternetConfigApplication(ConstStr255Param key);
  61.  
  62. #ifndef MOZ_MAIL_NEWS
  63.         static    ICError    SendInternetConfigURL(char *address);
  64. #endif
  65.  
  66.         static int        UseICPrefCallback(const char *prefString, void *);
  67.         static void        ResumeEvent();
  68.  
  69.         static    Boolean    CurrentlyUsingIC();
  70.         
  71.         static    Boolean    HaveICInstance();
  72.  
  73.     private:
  74.         
  75.         CInternetConfig();
  76.         ~CInternetConfig();
  77.  
  78.         void    SynchIf();    // iff the seed has changed
  79.         void    SynchFromIC();
  80.         ICError    SynchStringFromIC(    ConstStr255Param    icKey,
  81.                                     CPrefs::PrefEnum    netscapePref,
  82.                                     Boolean                stripPort = false,
  83.                                     const char            *xpPortPrefName = nil,
  84.                                     int32                defaultPort = 0);
  85.                                     
  86.         ICError    SynchSplitStringFromIC(    ConstStr255Param    icKey,
  87.                                         char                 divider,
  88.                                         CPrefs::PrefEnum    firstString,
  89.                                         CPrefs::PrefEnum    secondString,
  90.                                         Boolean                stripPort = false);
  91.  
  92.         ICError    GetICString(    ConstStr255Param    icKey,
  93.                                 Str255                s,
  94.                                 int32                *port = nil);
  95.  
  96.         ICError GetICFileMapping(    OSType fileType, 
  97.                                     OSType creator, 
  98.                                     ConstStr255Param fileName,
  99.                                     ICMapEntry *ent);
  100.  
  101.         ICError    LaunchICApplication(ConstStr255Param key);
  102.                                     
  103. #ifndef MOZ_MAIL_NEWS
  104.         ICError SendICURL(char *address);
  105. #endif
  106.         ICInstance    fInstance;
  107.         SInt32        fSeed;
  108.  
  109.         static     CInternetConfig*    sInternetConfigConnection;
  110. };
  111.  
  112.  
  113. void
  114. CInternetConfigInterface::ResumeEvent()
  115. {
  116.     CInternetConfig::ResumeEvent();
  117. }
  118.  
  119. //
  120. // ConnectToInternetConfig
  121. //
  122. // Only public entrypoint to this InternetConfig module.
  123. //
  124. // gets the folder from CPrefs::GetFilePrototype(prefSpec(, MainFolder)
  125. // 
  126. void
  127. CInternetConfigInterface::ConnectToInternetConfig()
  128. {
  129.     // I assume that this is only called once, at startup.
  130.     const char    *useICPrefName = "browser.mac.use_internet_config";
  131.     PREF_RegisterCallback(useICPrefName, CInternetConfig::UseICPrefCallback, nil);
  132.     try
  133.     {
  134.         if (CInternetConfig::CurrentlyUsingIC())
  135.         {
  136.             CInternetConfig::Connect();
  137.         }
  138.     }
  139.     catch(ICError err)
  140.     {
  141.         // do something ?    a dialog perhaps ?
  142.         // only if there is a real problem, not if 
  143.         // IC just isn't installed.
  144.     }
  145. }    
  146.  
  147. void    
  148. CInternetConfigInterface::DisconnectFromInternetConfig()
  149. {
  150.     CInternetConfig::Disconnect();
  151. }
  152.  
  153. Boolean
  154. CInternetConfigInterface::CurrentlyUsingIC(void)
  155. {
  156.     Boolean returnValue = TRUE;
  157.     returnValue = returnValue && CInternetConfig::CurrentlyUsingIC();
  158.     returnValue = returnValue && CInternetConfig::HaveICInstance();
  159.     
  160.     return returnValue;
  161. }
  162.  
  163. void
  164. CInternetConfigInterface::GetInternetConfigString(    ConstStr255Param icKey,
  165.                                                     Str255 s,
  166.                                                     long *port)
  167. {
  168.     if (CInternetConfig::GetInternetConfigString(icKey, s, port))
  169.     {
  170.         s[0] = 0;
  171.     }
  172. }
  173.  
  174. ICError
  175. CInternetConfigInterface::GetInternetConfigFileMapping(    OSType fileType, 
  176.                                                         OSType creator, 
  177.                                                         ConstStr255Param fileName,
  178.                                                         ICMapEntry *ent)
  179. {
  180.     return CInternetConfig::GetInternetConfigFileMapping(fileType, creator,
  181.                                                             fileName, ent);
  182. }
  183.  
  184. #ifndef MOZ_MAIL_NEWS
  185. ICError
  186. CInternetConfigInterface::SendInternetConfigURL(char *address)
  187. {
  188.     return CInternetConfig::SendInternetConfigURL(address);
  189. }
  190. #endif
  191.  
  192. ICError
  193. CInternetConfigInterface::LaunchInternetConfigApplication(ConstStr255Param key)
  194. {
  195.     return CInternetConfig::LaunchInternetConfigApplication(key);
  196. }
  197.  
  198.  
  199. CInternetConfig*
  200. CInternetConfig::sInternetConfigConnection = nil;
  201.  
  202. Boolean
  203. CInternetConfig::CurrentlyUsingIC()
  204. {
  205.     
  206.     XP_Bool    result;
  207.     const char    *useICPrefName = "browser.mac.use_internet_config";
  208.     if (PREF_NOERROR != PREF_GetBoolPref(useICPrefName, &result))
  209.     {
  210.         result = false;
  211.     }
  212.     return (Boolean)result;
  213. }
  214.  
  215.  
  216. //
  217. // CInternetConfig::Connect
  218. //
  219. // Call once to hook up with IC.  
  220. //
  221. void
  222. CInternetConfig::Connect()
  223. {
  224.     if (!sInternetConfigConnection)
  225.     {
  226.         sInternetConfigConnection = new CInternetConfig();    
  227.     }
  228. }
  229.  
  230. //
  231. // Bail
  232. //
  233. void
  234. CInternetConfig::Disconnect()
  235. {
  236.     if (sInternetConfigConnection != nil)
  237.     { 
  238.         delete sInternetConfigConnection;
  239.         sInternetConfigConnection = nil;
  240.     }
  241. }
  242.  
  243. Boolean
  244. CInternetConfig::HaveICInstance(void)
  245. {
  246.     Boolean returnValue = FALSE;
  247.     
  248.     Connect();
  249.     if (sInternetConfigConnection != nil)
  250.     {
  251.         returnValue = (sInternetConfigConnection->fInstance != NULL);
  252.     }
  253.     
  254.     return returnValue;
  255. }
  256.  
  257. //
  258. // CInternetConfig::CInternetConfig
  259. //
  260. CInternetConfig::CInternetConfig():
  261. fInstance(NULL)
  262. {    
  263.     ICError    err;
  264.         
  265.     // Detect IC, if present
  266.     StValueChanger<EDebugAction> okayToFail(gDebugThrow, debugAction_Nothing);
  267.     err = ::ICStart(&fInstance, emSignature);
  268.     //ThrowIfOSErr_(err);
  269.     if (!err)
  270.     {
  271.         try
  272.         {    
  273.  
  274.             ICDirSpec            prefDir[1];
  275.             ICDirSpecArrayPtr    prefDirArray;
  276.             UInt32                dirCount = 0;
  277.             
  278.             // what a wonderful api...
  279.             //
  280.             // Give IC the directory that contains the pref file we're using
  281.             // so it can look there for an IC config file.
  282.             prefDirArray = (ICDirSpecArrayPtr) &prefDir;
  283.             FSSpec    prefSpec = CPrefs::GetFilePrototype(CPrefs::MainFolder);
  284.             prefDir[0].vRefNum     = prefSpec.vRefNum;
  285.             prefDir[0].dirID    = prefSpec.parID;
  286.             dirCount             = 1;    
  287.             
  288.             err = ::ICFindConfigFile(fInstance, dirCount, prefDirArray);
  289.             ThrowIfOSErr_(err);
  290.  
  291.             // Remember initial seed
  292.             err = ::ICGetSeed(fInstance, &fSeed);
  293.             ThrowIfOSErr_(err);
  294.             
  295.             // Read prefs from IC
  296.             if (CurrentlyUsingIC())
  297.             {
  298.                 SynchFromIC();
  299.             }
  300.         }
  301.         catch(ICError err)
  302.         {
  303.             // Close IC connection and pass the error along
  304.             ::ICStop(fInstance);
  305.             fInstance = NULL;
  306.         //    throw(err);
  307.             // we probably out to delete "this" as well
  308.         }
  309.     }
  310. }
  311.  
  312. int
  313. CInternetConfig::UseICPrefCallback(const char *, void *)
  314. {
  315.     if (CInternetConfig::CurrentlyUsingIC())
  316.     {
  317.         Connect();
  318.         sInternetConfigConnection->SynchFromIC();
  319.     }
  320.     return 0;    // You don't even want to know my opinion of this!
  321. }
  322.  
  323.  
  324. //
  325. // CInternetConfig::~CInternetConfig
  326. //
  327. CInternetConfig::~CInternetConfig()
  328. {
  329.     if (fInstance != NULL)
  330.     {
  331.         ::ICStop(fInstance);        // close IC connection
  332.     }
  333. }
  334.  
  335. //
  336. // CInternetConfig::SynchFromIC
  337. //
  338. // Reads IC settings and converts them to Netscape prefs
  339. //
  340. void
  341. CInternetConfig::SynchFromIC()
  342. {
  343.     ICError    err;
  344.     
  345.     err = ::ICBegin(fInstance, icReadOnlyPerm);
  346.     ThrowIfOSErr_(err);
  347.     
  348.     // Again, this is lame.
  349.     //
  350.     // We should have a table of some sort
  351.     //
  352.     SynchStringFromIC(kICRealName,         CPrefs::UserName);
  353.     SynchStringFromIC(kICEmail,         CPrefs::UserEmail);    
  354.     SynchStringFromIC(kICEmail,         CPrefs::ReplyTo);    // IC has no reply-to    
  355.     SynchSplitStringFromIC(kICMailAccount, '@', CPrefs::PopID, CPrefs::PopHost, true);    
  356.     SynchStringFromIC(kICSMTPHost,         CPrefs::SMTPHost, true);    
  357.     SynchStringFromIC(kICWWWHomePage,    CPrefs::HomePage);
  358.     SynchStringFromIC(kICOrganization,     CPrefs::Organization);
  359.     SynchStringFromIC(kICNNTPHost,        CPrefs::NewsHost, true, "news.server_port", 119);
  360.     
  361.     ::ICEnd(fInstance);
  362. }
  363.  
  364. void
  365. CInternetConfig::SynchIf()
  366. {
  367.     SInt32    seed;
  368.  
  369.     if (::ICGetSeed(fInstance, &seed))
  370.     {
  371.         return;
  372.     }
  373.     if (seed != fSeed)
  374.     {
  375.         try
  376.         {
  377.             SynchFromIC();
  378.         }
  379.         catch(ICError err)
  380.         {
  381.         }
  382.         fSeed = seed;
  383.     }
  384. }
  385.  
  386. //
  387. // CInternetConfig::ResumeEvent
  388. //
  389. //
  390. void
  391. CInternetConfig::ResumeEvent()
  392. {
  393.     if (CurrentlyUsingIC())
  394.     {
  395.         Connect();
  396.         sInternetConfigConnection->SynchIf();
  397.     }
  398. }
  399.  
  400. //
  401. // CInternetConfig::SynchStringFromIC
  402. //
  403. // Set a netscape string from an IC string
  404. ICError
  405. CInternetConfig::SynchStringFromIC(    ConstStr255Param    icKey,
  406.                                     CPrefs::PrefEnum    netscapePref,
  407.                                     Boolean                stripPort,
  408.                                     const char            *xpPortPrefName,
  409.                                     int32                defaultPort)
  410. {                                
  411.     char    s[256];
  412.     ICError    err;
  413.     
  414.     int32    *portPtr = stripPort ? &defaultPort : nil;
  415.  
  416.     err = GetICString(icKey, (unsigned char*) s, portPtr);
  417.     if (err == 0)
  418.     {
  419.         p2cstr((StringPtr)s);
  420.         CPrefs::SetString(s, netscapePref);
  421.         if (xpPortPrefName)
  422.         {
  423.             PREF_SetIntPref(xpPortPrefName, defaultPort);
  424.         }
  425.     }
  426.     return err;
  427. }        
  428.  
  429. //
  430. // CInternetConfig::SynchSplitStringFromIC
  431. //
  432. // Takes a single IC string and splits it into two Netscape strings.
  433. // Useful for machine@host.domain.com, or proxy.address:port type stuff
  434. //
  435. // If the divider can't be found, the entire string is put into the
  436. // first netscape string and the second netscape string is set to '\0'
  437. // 
  438. ICError
  439. CInternetConfig::SynchSplitStringFromIC(    ConstStr255Param    icKey,
  440.                                             char                 divider,
  441.                                             CPrefs::PrefEnum    firstString,
  442.                                             CPrefs::PrefEnum    secondString,
  443.                                             Boolean                stripPort)
  444. {
  445.     char    buffer[256];
  446.     char    *s;
  447.     char    *split;
  448.     ICError    err;
  449.     
  450.     s     = buffer;
  451.     err = GetICString(icKey, (unsigned char *) s);
  452.     if (err != 0) return err;
  453.     
  454.     p2cstr((StringPtr)s);
  455.         
  456.     split = strchr(s, divider);
  457.     if (split != NULL)
  458.     {
  459.         *split = '\0';
  460.         if (stripPort)
  461.         {
  462.             char *colon = strchr(split+1, ':');
  463.             if (colon)
  464.             {
  465.                 *colon = '\0';
  466.             }
  467.         }
  468.         CPrefs::SetString(split+1, secondString);
  469.     }    
  470.     else
  471.     {
  472.         CPrefs::SetString('\0', secondString);
  473.     }    
  474.     CPrefs::SetString(s, firstString);    
  475.     
  476.     return 0;
  477. }            
  478.                     
  479. //
  480. // CInternetConfig::GetICString
  481. //
  482. // Gets an IC string pref
  483. //
  484. ICError
  485. CInternetConfig::GetInternetConfigString(    ConstStr255Param    icKey,
  486.                                             Str255                s,
  487.                                             long                *port)
  488. {
  489.     Connect();
  490.     return sInternetConfigConnection->GetICString(icKey, s, port);
  491. }                                
  492.  
  493. //
  494. // CInternetConfig::GetICString
  495. //
  496. // Gets an IC string pref
  497. //
  498. ICError
  499. CInternetConfig::GetICString(    ConstStr255Param    icKey,
  500.                                 Str255                s,
  501.                                 int32                *port)
  502. {
  503.     ICAttr attr;    
  504.     long size = 256;
  505.     ICError    result;
  506.     result = ::ICGetPref(fInstance, icKey, &attr, (Ptr)s, &size);
  507.     if (!result)
  508.     {
  509.         if (port)
  510.         {
  511.             char    cString[256];
  512.             BlockMoveData(&s[1], cString, s[0]);
  513.             cString[s[0]] = '\0';
  514.             char *colon = strchr(cString, ':');
  515.             if (colon)
  516.             {
  517.                 *colon = '\0';
  518.                 s[0] = colon - cString;
  519.                 ++colon;
  520.                 // IC supposedly supports notations like:
  521.                 //        news.netscape.com:nntp
  522.                 // The protocol services don't seem to work in IC (or I'm to stupid
  523.                 // to make them work), so we just check for this one value ("nntp")
  524.                 // because that is the only protocol for which we support port numbers.
  525.                 if (!XP_STRCASECMP("nntp", colon))
  526.                 {
  527.                     *port = 119;
  528.                 }
  529.                 else
  530.                 {
  531.                     // Add more protocols here if/when we suppor them.
  532.                     long    portFromICString;
  533.                     int numargs = sscanf(colon, "%ld", &portFromICString);
  534.                     if (1 == numargs)
  535.                     {
  536.                         if (portFromICString >= 0)    // negative port numbers are not valid
  537.                         {
  538.                             *port = portFromICString;
  539.                         }
  540.                     }
  541.                     // else we just use the default port
  542.                 }
  543.             }
  544.         }
  545.     }
  546.     return result;
  547. }                                
  548.  
  549. #ifndef MOZ_MAIL_NEWS
  550. ICError
  551. CInternetConfig::SendInternetConfigURL(char *address)
  552. {
  553.     Connect();
  554.     return sInternetConfigConnection->SendICURL(address);
  555. }
  556.  
  557. ICError
  558. CInternetConfig::SendICURL(char *address)
  559. {
  560.     if (address == NULL)
  561.         return icNoURLErr;
  562.         
  563.     long selStart = 0;
  564.     long selEnd = strlen(address);
  565.  
  566.     if( CInternetConfig::HaveICInstance() )
  567.         return ::ICLaunchURL(fInstance, "\p", address, selEnd, &selStart, &selEnd);
  568.     else
  569.         return icPrefNotFoundErr;
  570. }
  571. #endif
  572.  
  573. ICError
  574. CInternetConfig::GetInternetConfigFileMapping(    OSType fileType,
  575.                                                 OSType creator,
  576.                                                 ConstStr255Param filename,
  577.                                                 ICMapEntry *ent)
  578. {
  579.     Connect();
  580.     return sInternetConfigConnection->GetICFileMapping(fileType, creator, filename, ent);
  581. }
  582.  
  583. ICError
  584. CInternetConfig::GetICFileMapping(    OSType fileType,
  585.                                     OSType creator,
  586.                                     ConstStr255Param filename,
  587.                                     ICMapEntry *ent)
  588. {
  589.     if( CInternetConfig::HaveICInstance() )
  590.         return ::ICMapTypeCreator(fInstance, fileType, creator, filename, ent);
  591.     else
  592.         return icPrefNotFoundErr;
  593. }
  594.  
  595. ICError
  596. CInternetConfig::LaunchInternetConfigApplication(ConstStr255Param key)
  597. {
  598.     Connect();
  599.     return sInternetConfigConnection->LaunchICApplication(key);
  600. }
  601.  
  602. ICError
  603. CInternetConfig::LaunchICApplication(ConstStr255Param key)
  604. {
  605.     if (CInternetConfig::HaveICInstance())
  606.         return ::ICEditPreferences(fInstance, key);
  607.     else
  608.         return icPrefNotFoundErr;
  609. }
  610.