home *** CD-ROM | disk | FTP | other *** search
/ Tools / WinSN5.0Ver.iso / NETSCAP.50 / WIN1998.ZIP / ns / cmd / winfe / profile.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1998-04-08  |  13.3 KB  |  538 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. #include "stdafx.h"
  20.  
  21. #include "property.h"
  22. #include "styles.h"
  23.  
  24. #include "helper.h"
  25. #include "display.h"
  26. #include "dialog.h"
  27.  
  28. #include "secnav.h"
  29. #include "custom.h"
  30. #include "cxabstra.h"
  31. #include "setupwiz.h"
  32. #include "logindg.h"
  33. #include "prefapi.h"
  34.  
  35. #include "profile.h"
  36. #include <ctype.h>
  37.  
  38. #define BUFSZ MAX_PATH+1
  39.  
  40. #ifdef _DEBUG
  41. #undef THIS_FILE
  42. static char BASED_CODE THIS_FILE[] = __FILE__;
  43. #endif
  44.  
  45. CUserProfileDB::CUserProfileDB( )
  46. {
  47. #ifdef XP_WIN
  48.     m_hUser.Empty();
  49. #else
  50. #endif
  51.  
  52.     m_csUserAddr.Empty();
  53.     m_csProfileDirectory.Empty();
  54.  
  55.     OpenUserProfileDB();
  56. }
  57.  
  58. CUserProfileDB::~CUserProfileDB( )
  59. {
  60.     // m_cslProfiles->RemoveAll();
  61.  
  62.     CloseUserProfileDB();
  63. }
  64.  
  65. HPROFILE CUserProfileDB::OpenUserProfileDB( /* Handle Mac Parms somehow */ )
  66. {
  67. //============================================================================
  68. #ifdef XP_WIN32
  69.     CString csTmp = "SOFTWARE\\Netscape\\Netscape Navigator\\Users\\";
  70.  
  71.     int result = RegOpenKeyEx(HKEY_LOCAL_MACHINE, csTmp, NULL,
  72.                   KEY_READ | KEY_WRITE, &m_hProfileDB);
  73.     if ( result != ERROR_SUCCESS )
  74.         m_hProfileDB = NULL;
  75.  
  76. //============================================================================
  77. #else // XP_WIN16
  78.     login_GetIniFilePath(m_hProfileDB);
  79. #endif
  80.  
  81.     return m_hProfileDB;
  82. }
  83.  
  84. int CUserProfileDB::CloseUserProfileDB( )
  85. {
  86.     int ret = TRUE;
  87.  
  88. //============================================================================
  89. #ifdef XP_WIN32
  90.     if (m_hProfileDB)
  91.         ret = RegCloseKey(m_hProfileDB);
  92.     m_hProfileDB = NULL;
  93.  
  94. //============================================================================
  95. #else
  96.     m_hProfileDB = "";
  97. #endif
  98.  
  99.     return ret;
  100. }
  101.  
  102. void CUserProfileDB::GetUserProfilesList( CStringList *cslProfiles )
  103. {
  104.     int  idx = 0;
  105.     char lpBuf[MAX_PATH + 1];
  106.  
  107.     // Loop and delete entries?
  108.     cslProfiles->RemoveAll();
  109.  
  110.     if (!m_hProfileDB)
  111.         OpenUserProfileDB();
  112.  
  113. //============================================================================
  114. #ifdef XP_WIN32
  115.     while(RegEnumKey(m_hProfileDB,idx,lpBuf,MAX_PATH+1) == ERROR_SUCCESS) {
  116.         cslProfiles->AddHead(lpBuf);
  117.         idx++;
  118.     }
  119.  
  120. //============================================================================
  121. #else    
  122.     auto char ca_iniBuff[_MAX_PATH];
  123.  
  124.     // load the user list from the ini file
  125.     ::GetPrivateProfileString("Users", NULL, "", ca_iniBuff, _MAX_PATH, m_hProfileDB);
  126.  
  127.     while (ca_iniBuff[idx]) {
  128.         XP_STRCPY(lpBuf, &ca_iniBuff[idx]);
  129.         cslProfiles->AddHead(lpBuf);
  130.         idx += XP_STRLEN(lpBuf) + 1; // skip the single null and the prof we just read
  131.     }
  132. #endif
  133.  
  134. }
  135.  
  136. //-----------------------------------------------
  137. // Returns string allocated with XP_ALLOC or NULL
  138. //
  139. LPSTR CUserProfileDB::GetUserProfileValue( 
  140.     HUSER   hUser, 
  141.     CString csName )
  142. {
  143. //============================================================================
  144. #ifdef XP_WIN32
  145.     HKEY hKeyRet = NULL;
  146.     CString csSub = "SOFTWARE\\Netscape\\Netscape Navigator\\Users\\" + hUser;
  147.     DWORD type, size = 0;
  148.         char *pString = NULL;
  149.     int result;
  150.  
  151.     // Unfortunately, this open is _necessary_!
  152.     result = RegOpenKeyEx(  HKEY_LOCAL_MACHINE,
  153.                 csSub,
  154.                 NULL,
  155.                 KEY_QUERY_VALUE,
  156.                 &hKeyRet);
  157.  
  158.     if(result != ERROR_SUCCESS)
  159.         goto Fail;
  160.  
  161.     result = RegQueryValueEx(hKeyRet, (const char *) csName, 
  162.                 NULL, &type, NULL, &size);
  163.     if ( result != ERROR_SUCCESS || size == 0 )  
  164.         goto Fail;
  165.  
  166.         // allocate space to hold the string
  167.         pString = (char *) XP_ALLOC((size + 1) * sizeof(char));
  168.  
  169.         // actually load the string now that we have the space
  170.         result = RegQueryValueEx(hKeyRet, (const char *) csName, NULL, 
  171.                  &type, (LPBYTE) pString, &size);
  172.         if ( result != ERROR_SUCCESS || size == 0 )
  173.     {
  174.         XP_FREE(pString);
  175.         pString = NULL;
  176.         goto Fail;
  177.     }
  178.  
  179.     pString[size] = '\0';
  180.  
  181. Fail:
  182.     if (hKeyRet) RegCloseKey(hKeyRet);
  183.  
  184. //============================================================================
  185. #else // XP_WIN16
  186.     CString csSection;
  187.     CString csResource;
  188.     char *  pString = NULL;
  189.     auto char ca_iniBuff[_MAX_PATH];
  190.  
  191.     if ( csName == "DirRoot" )
  192.     {
  193.         // This one comes from the base [Users] section, <user>=value
  194.         csSection  = "Users";
  195.         csResource = hUser;
  196.     }
  197.     else
  198.     {
  199.         // These come from the [<username>] section, name=value
  200.         csSection  = hUser;
  201.         csResource = csName;
  202.     }
  203.  
  204.     if (::GetPrivateProfileString( csSection, csResource, "", 
  205.                        ca_iniBuff, _MAX_PATH, 
  206.                        m_hProfileDB) )
  207.     {
  208.         // allocate space to hold the string
  209.         pString = XP_STRDUP(ca_iniBuff);
  210.     }
  211. #endif
  212.  
  213.     return pString;    
  214. }
  215.  
  216.  
  217. int CUserProfileDB::SetUserProfileValue( 
  218.     HUSER   hUser, 
  219.     CString csName,
  220.     CString csValue )
  221. {
  222.     int result;
  223.  
  224. //============================================================================
  225. #ifdef XP_WIN32
  226.     HKEY hKeyRet = NULL;
  227.     CString csSub = "SOFTWARE\\Netscape\\Netscape Navigator\\Users\\" + hUser;
  228.     const char *pValue = (const char *) csValue;
  229.  
  230.     // Unfortunately, this open is _necessary_!
  231.     result = RegOpenKeyEx(  HKEY_LOCAL_MACHINE,
  232.                 csSub,
  233.                 NULL,
  234.                 KEY_QUERY_VALUE,
  235.                 &hKeyRet);
  236.  
  237.     if(result == ERROR_SUCCESS)
  238.         result = RegSetValueEx( m_hProfileDB, csName, NULL, 
  239.                     REG_SZ, 
  240.                     (const BYTE *) pValue, 
  241.                     strlen(pValue) );
  242.  
  243.     if (hKeyRet) RegCloseKey(hKeyRet);
  244.  
  245. //============================================================================
  246. #else // XP_WIN16
  247.     CString csSection;
  248.     CString csResource;
  249.  
  250.     if ( csName == "DirRoot" )
  251.     {
  252.         // This one comes from the base [Users] section, <user>=value
  253.         csSection  = "Users";
  254.         csResource = hUser;
  255.     }
  256.     else
  257.     {
  258.         // These come from the [<username>] section, name=value
  259.         csSection  = hUser;
  260.         csResource = csName;
  261.     }
  262.  
  263.     result = ::WritePrivateProfileString( csSection, csResource, 
  264.                           (const char *) csValue, 
  265.                           m_hProfileDB);
  266.  
  267. #endif
  268.  
  269.     return result;
  270. }
  271.  
  272.  
  273. void CUserProfileDB::GetUserProfile( HUSER hUser )
  274. {
  275.     char *pString;
  276.  
  277. //============================================================================
  278. #ifdef XP_WIN
  279.     m_hUser = hUser;
  280.  
  281.     /*
  282.     ** munge user name to profile name 
  283.     */
  284.     int iAtSign = m_hUser.Find('@');
  285.     if (iAtSign != -1) 
  286.         m_hUser = m_hUser.Left(iAtSign);
  287. #endif
  288.  
  289.     // Find the profile directory
  290.     pString = GetUserProfileValue( m_hUser, "DirRoot" );
  291.     if (pString)
  292.     {
  293.         m_csProfileDirectory = pString;
  294.         XP_FREE(pString);
  295.     }
  296.     else
  297.         m_csProfileDirectory.Empty();
  298.  
  299.     // Find the EMail Address
  300.     pString = GetUserProfileValue( m_hUser, "EmailAddr" );
  301.     if (pString)
  302.     {
  303.         m_csUserAddr = pString;
  304.         XP_FREE(pString);
  305.     }
  306.     else
  307.         m_csUserAddr.Empty();
  308.  
  309.     // Add filling in any new profile members here...
  310.  
  311.  
  312.     return ;
  313. }
  314.  
  315.  
  316. int CUserProfileDB::BuildDirectoryPath( const char *path )
  317. {
  318.     int ret;
  319.     XP_StatStruct statinfo; 
  320.  
  321.     ret = _stat((char *) path, &statinfo);
  322.     if(ret == -1) {
  323.         // see if we can just create it
  324.         char * slash = strchr(path,'\\');
  325.         while (slash) {
  326.             slash[0] = NULL;
  327.             ret = _mkdir(path);
  328.             slash[0] = '\\';
  329.             if (slash+1) slash = strchr(slash+1,'\\');
  330.         }
  331.         ret = _mkdir(path);
  332.         if ( ret == -1 ) return FALSE;
  333.         // ERROR: Create Directory failed
  334.     }
  335.  
  336.     // If directory exists already, then what?
  337.     return TRUE;
  338. }
  339.  
  340. int CUserProfileDB::AddNewProfile( 
  341.     HUSER   hUser,
  342.     CString csProfileDirectory, 
  343.     int     iUpgradeOption )
  344. {
  345.     int ret;
  346.  
  347.     // Using _mkdir for win16/win32 compatibility
  348.     // Do we need new error constants if this gets incorporated into the setupwiz dialogs?
  349.  
  350.     m_hUser = hUser;
  351.     m_csProfileDirectory = csProfileDirectory;
  352.  
  353.     /*
  354.     ** Create the directory if necessary 
  355.     */
  356.     BuildDirectoryPath(  (const char *) csProfileDirectory );
  357.  
  358.     /*
  359.     ** Handle upgrades.  The Move operation moves data and leaves links behind.
  360.     ** The Copy operation copies the data and changes are not reflected to old 
  361.     ** space.  Ignore leaves old space alone and creates new profile.
  362.     */
  363.     if (iUpgradeOption == UPGRADE_MOVE || iUpgradeOption == UPGRADE_COPY) {
  364.         ret = login_UpdateFilesToNewLocation( csProfileDirectory, NULL, 
  365.             (iUpgradeOption == UPGRADE_COPY) );
  366.  
  367.         // ret value is hosed, doesn't actually do any checking!
  368.         if ( !ret ) return NULL;              
  369.         // ERROR: Couldn't Move/Copy old profile
  370.  
  371.         ret = login_UpdatePreferencesToJavaScript( csProfileDirectory );
  372.         // ret value is hosed, doesn't actually do any checking!
  373.         if ( !ret ) return NULL;              
  374.         // ERROR: Couldn't update old preferences to JS
  375.  
  376.     } else {
  377.         // just create the directories --
  378.         login_CreateEmptyProfileDir( csProfileDirectory, NULL, FALSE);
  379.     }
  380.  
  381.     /*
  382.     ** Create the user and add the profile directory to the user's registry entry 
  383.     ** !! Don't do this until all other operations have succeeded!
  384.     */
  385.     ret = login_CreateNewUserKey( hUser, csProfileDirectory ); 
  386.     if ( ret != ERROR_SUCCESS ) return NULL;
  387.     // ERROR: Create User Entry failed
  388.  
  389.  
  390.     /*
  391.     ** Now that we've done all this work, let's fill the profile up with stuff
  392.     **/
  393.     PREF_SavePrefFile();                          
  394.     // ERROR: ?
  395.  
  396.     return 1;
  397. }
  398.  
  399.  
  400.  
  401. int CUserProfileDB::DeleteUserProfile( HUSER hUser )
  402. {
  403.     char *pString;
  404.  
  405.  
  406.     pString = GetUserProfileValue( hUser, "DirRoot" );
  407.     if (pString)
  408.     {
  409.         XP_RemoveDirectoryRecursive(pString, xpURL);
  410.         XP_FREE(pString);
  411.     }
  412.  
  413.     if (m_hUser && hUser == m_hUser)
  414.         m_csProfileDirectory.Empty();
  415.  
  416.     return login_DeleteUserKey( hUser );
  417. }
  418.  
  419. // Given a profile name, return a profile directory path in Program Files
  420. BOOL CUserProfileDB::AssignProfileDirectoryName(HUSER hUser, CString &csUserDirectory)
  421. {
  422.     CString install("");
  423.     CString userDir = "\\Users\\";
  424.  
  425.     // first chance of default is based on any existing user dir
  426.  
  427.     char *pExistingUser = NULL;
  428.     pExistingUser = login_GetCurrentUser();
  429.     if (pExistingUser) {
  430.         char *pDir = login_GetUserProfileDir();
  431.         if (pDir && *pDir) {
  432.             install = pDir;
  433.             
  434.             int iLastSlash = -1;
  435.             iLastSlash = install.ReverseFind('\\');
  436.             if (iLastSlash > 2) {
  437.                 int iPos = install.GetLength()-iLastSlash;
  438.                 install= install.Left(install.GetLength()-iPos+1);
  439.             }
  440.             // already have the user dir as part of the path so empty out the string
  441.             userDir.Empty();
  442.             XP_FREE(pDir);
  443.         }
  444.     }
  445.  
  446.     if (install.IsEmpty())
  447.     {
  448.         // Try to find the official install directory value in the registry
  449. #ifdef XP_WIN32
  450.         CString csProduct;
  451.         csProduct.LoadString(IDS_NETHELP_REGISTRY);
  452.  
  453.         csProduct = FEU_GetCurrentRegistry(csProduct);
  454.         if (!csProduct.IsEmpty())
  455.             install = FEU_GetInstallationDirectory(csProduct, szLoadString(IDS_INSTALL_DIRECTORY));
  456. #else 
  457.         
  458.         install = FEU_GetInstallationDirectory(szLoadString(IDS_NETSCAPE_REGISTRY), szLoadString(IDS_INSTALL_DIRECTORY));
  459.         if (!install.IsEmpty())
  460.         {
  461.                 // Assuming we got a name back, remove the Program part - which is only there for 16 bit, not for 32 bit.
  462.                 int iLastSlash = install.ReverseFind('\\');
  463.  
  464.                 if (iLastSlash != -1)
  465.                 {
  466.                         CString subdir = install.Mid(iLastSlash+1);
  467.                         if (!subdir.CompareNoCase("Program"))
  468.                                 install = install.Left(iLastSlash);
  469.                 }
  470.         }
  471. #endif
  472.     }
  473.  
  474.     // If we can't get it from the registry, try the program name
  475.     if (install.IsEmpty())
  476.     {
  477.         char aPath[_MAX_PATH];
  478.  
  479.         if (::GetModuleFileName(theApp.m_hInstance, aPath, sizeof(aPath)))
  480.         {
  481.             // WIN16 default: c:\Netscape\Comm\Program\netscape.exe
  482.             // WIN32 default: c:\Program Files\Netscape\Communicator\Program\netscape.exe
  483.  
  484.             // Remove the trailing netscape.exe stuff
  485.             char *pSlash = ::strrchr(aPath, '\\');
  486.             if(pSlash)
  487.                 *pSlash = '\0';
  488.  
  489.             // Remove the Program part
  490.             pSlash = ::strrchr(aPath, '\\');
  491.             if (pSlash)
  492.                 *pSlash = '\0';
  493.  
  494.             install = aPath;
  495.         }
  496.  
  497.     }
  498.     
  499.     // If we still can't get it, use these defaults
  500.     if (install.IsEmpty())
  501.     {
  502. #ifdef XP_WIN32
  503.             install = "C:\\Program Files\\Netscape";
  504. #else
  505.             install = "C:\\Netscape";
  506. #endif
  507.     }
  508.     else
  509.     {
  510.         // Assuming we got a name back somehow, remove the Comm[unicator] part
  511.         int iLastSlash = install.ReverseFind('\\');
  512.  
  513.         if (iLastSlash != -1)
  514.         {
  515.             CString subdir = install.Mid(iLastSlash+1);
  516. #ifdef XP_WIN32
  517.             if ((!subdir.CompareNoCase("Communicator")) || (!subdir.CompareNoCase("Commun~1")))
  518. #else 
  519.             if (!subdir.CompareNoCase("Comm"))
  520. #endif
  521.                 install = install.Left(iLastSlash);
  522.         }
  523.     }
  524.     //we need to make sure that the hUser is valid characters
  525.     int idx =0;
  526.     for (idx =0; idx < hUser.GetLength(); idx++) {
  527.         if (!__iscsym(hUser.GetAt(idx)))
  528.             hUser.SetAt(idx,'_');
  529.     }
  530. #ifdef XP_WIN32
  531.     csUserDirectory = install + userDir + hUser;
  532. #else
  533.     csUserDirectory = install + userDir + hUser.Left(8);
  534. #endif
  535.  
  536.     return TRUE;
  537. }
  538.