home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 4 / Apprentice-Release4.iso / Source Code / C++ / Snippets / IC++ 1.0b1 / CInternetConfig.cpp next >
Encoding:
C/C++ Source or Header  |  1995-12-16  |  5.7 KB  |  256 lines  |  [TEXT/CWIE]

  1. /***********************************************************************\
  2.     CInternetConfig.h
  3.     
  4.     A C++ class for using Internet Config
  5.     
  6.     by Dan Crevier
  7.     
  8.     version 1.0
  9.  
  10. \***********************************************************************/
  11.  
  12. #include "CInternetConfig.h"
  13.  
  14. #ifndef __ICAPI__
  15. #include "ICAPI.h"
  16. #endif
  17.  
  18. #ifndef __COMPONENTS__
  19. #include <Components.h>
  20. #endif
  21.  
  22. /***********************************************************************\
  23.     CInternetConfig - constructor
  24.         If prefFile is NULL, the default preferences are used
  25. \***********************************************************************/
  26.  
  27. CInternetConfig::CInternetConfig(OSType applicationSig, FSSpec *prefFile)
  28. : installed(false), mappings(0)
  29. {
  30.     ComponentDescription ICDescription = {'PREF', 'ICAp', 0L, 0L, 0L};
  31.     ICError err;
  32.  
  33.     // first, see if IC is installed
  34.     if (CountComponents(&ICDescription))
  35.     {
  36.         if (ICStart(&inst, applicationSig) == noErr)
  37.         {
  38.             if (prefFile == NULL) // use default prefs
  39.             {
  40.                 err = ICFindConfigFile(inst, 0, NULL);
  41.             }
  42.             else
  43.             {
  44.                 ICDirSpec prefDir;
  45.                 
  46.                 prefDir.vRefNum = prefFile->vRefNum;
  47.                 prefDir.dirID = prefFile->parID;
  48.                 err = ICFindUserConfigFile(inst, &prefDir);
  49.             }
  50.             if (err == noErr) // startup was successful
  51.             {
  52.                  installed = true;
  53.             }
  54.             else
  55.             {
  56.                 // if ICStart was called, but pref couldn't be found, we
  57.                 // need to call ICStop
  58.  
  59.                 ICStop(inst);
  60.             }
  61.         }
  62.     }
  63. }
  64.  
  65. /***********************************************************************\
  66.     ~CInternetConfig - destructor
  67. \***********************************************************************/
  68.  
  69. CInternetConfig::~CInternetConfig()
  70. {
  71.     if (installed)
  72.     {
  73.         ICStop(inst);
  74.     }
  75. }
  76.  
  77. /***********************************************************************\
  78.     GetEmailAddress - return the user's email address
  79. \***********************************************************************/
  80.  
  81. ICError CInternetConfig::GetEmailAddress(Str255 emailAddress)
  82. {
  83.     ICAttr junkAttr;
  84.     long size = 256;
  85.     
  86.     if (installed)
  87.     {
  88.         return ICGetPref(inst, kICEmail, &junkAttr, (char *)emailAddress, &size);
  89.     }
  90.     else
  91.     {
  92.         return -1;
  93.     }
  94. }
  95.  
  96. /***********************************************************************\
  97.     GetRealName - return the user's real name
  98. \***********************************************************************/
  99.  
  100. ICError CInternetConfig::GetRealName(Str255 realName)
  101. {
  102.     ICAttr junkAttr;
  103.     long size = 256;
  104.     
  105.     if (installed)
  106.     {
  107.         return ICGetPref(inst, kICRealName, &junkAttr, (char *)realName, &size);
  108.     }
  109.     else
  110.     {
  111.         return -1;
  112.     }
  113. }
  114.  
  115. /***********************************************************************\
  116.     GetMailHost - return the user's mail (SMTP) host
  117. \***********************************************************************/
  118.  
  119. ICError CInternetConfig::GetMailHost(Str255 mailHost)
  120. {
  121.     ICAttr junkAttr;
  122.     long size = 256;
  123.     
  124.     if (installed)
  125.     {
  126.         return ICGetPref(inst, kICSMTPHost, &junkAttr, (char *)mailHost, &size);
  127.     }
  128.     else
  129.     {
  130.         return -1;
  131.     }
  132. }
  133.  
  134. /***********************************************************************\
  135.     DoURL - launch the URL past in
  136. \***********************************************************************/
  137.  
  138. ICError CInternetConfig::DoURL(StringPtr theURL)
  139. {
  140.     long start = 0, end;
  141.     
  142.     if (installed)
  143.     {
  144.         end = theURL[0];
  145.         return ICLaunchURL(inst, "\p", (char *)theURL+1, end, &start, &end);
  146.     }
  147.     else
  148.     {
  149.         return -1;
  150.     }
  151. }
  152.  
  153. /***********************************************************************\
  154.     MapFilename - return the map entry for the given filename
  155. \***********************************************************************/
  156.  
  157. ICError CInternetConfig::MapFilename(StringPtr name, ICMapEntry *entry)
  158. {
  159.     if (installed)
  160.     {
  161.         return ICMapFilename(inst, name, entry);
  162.     }
  163.     else
  164.     {
  165.         return -1;
  166.     }
  167. }
  168.  
  169. /***********************************************************************\
  170.     MapFilename - return the map entry for the given filename
  171.         takes name as C string
  172. \***********************************************************************/
  173.  
  174. ICError CInternetConfig::MapFilename(char *name, ICMapEntry *entry)
  175. {
  176.     short i;
  177.     Str255 newName;
  178.     
  179.     // convert name to P string
  180.     for(i=0; name[i] && i<255; i++)
  181.     {
  182.         newName[i+1] = name[i];
  183.     }
  184.     newName[0] = i;
  185.     
  186.     return MapFilename(newName, entry);
  187. }
  188.  
  189. /***********************************************************************\
  190.     StartMapIteration -- starts iterating over the mappings
  191.         Note: It calls ICBegin() and FinishMapIteration calls
  192.             ICEnd(), so you have can't handle events in between
  193. \***********************************************************************/
  194.  
  195. ICError CInternetConfig::StartMapIteration(void)
  196. {
  197.     ICError err;
  198.     ICAttr            attr = 0;
  199.     
  200.     currentPos = 1;
  201.  
  202.     if (!installed) return -1;
  203.     
  204.     err = ICBegin(inst, icReadOnlyPerm);
  205.     if (err != noErr) return err;
  206.     
  207.     err = ICGetPrefHandle(inst, kICMapping, &attr, &mappings);
  208.     
  209.     if (!err)
  210.     {
  211.         err = ICCountMapEntries(inst, mappings, &numMappings);
  212.     }
  213.     
  214.     if (err)
  215.     {
  216.         ICEnd(inst);
  217.     }
  218.     
  219.     return err;
  220. }
  221.  
  222. /***********************************************************************\
  223.     NextMapEntry - get the next map entry after calling
  224.         StartMapIteration.  Returns false if there are no more entries    
  225. \***********************************************************************/
  226.  
  227. Boolean CInternetConfig::NextMapEntry(ICMapEntry *entry)
  228. {
  229.     long pos;
  230.  
  231.     if (!installed || mappings == NULL) return false;
  232.     
  233.     // are we done?
  234.     if (currentPos > numMappings) return false;
  235.     
  236.     ICGetIndMapEntry(inst, mappings, currentPos, &pos, entry);
  237.  
  238.     currentPos++;
  239.     
  240.     return true;
  241. }
  242.  
  243. /***********************************************************************\
  244.     FinishMapIteration -- call after doing a map iteration    
  245. \***********************************************************************/
  246.  
  247. ICError CInternetConfig::FinishMapIteration(void)
  248. {
  249.     if (!installed || mappings == NULL) return -1;
  250.     
  251.     DisposeHandle(mappings);
  252.     mappings = NULL;
  253.     
  254.     return ICEnd(inst);
  255. }
  256.