Back to index

lightning-sunbird  0.9+nobinonly
PalmSyncInstall.cpp
Go to the documentation of this file.
00001 /* ***** BEGIN LICENSE BLOCK *****
00002  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00003  *
00004  * The contents of this file are subject to the Mozilla Public License Version
00005  * 1.1 (the "License"); you may not use this file except in compliance with
00006  * the License. You may obtain a copy of the License at
00007  * http://www.mozilla.org/MPL/
00008  *
00009  * Software distributed under the License is distributed on an "AS IS" basis,
00010  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00011  * for the specific language governing rights and limitations under the
00012  * License.
00013  *
00014  * The Original Code is mozilla.org code.
00015  *
00016  * The Initial Developer of the Original Code is
00017  * Netscape Communications Corporation
00018  *
00019  * Portions created by the Initial Developer are Copyright (C) 2002
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *    Rajiv Dayal <rdayal@netscape.com>
00024  *            David Bienvenu <bienvenu@nventure.com>
00025  *
00026  * Alternatively, the contents of this file may be used under the terms of
00027  * either the GNU General Public License Version 2 or later (the "GPL"), or
00028  * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00029  * in which case the provisions of the GPL or the LGPL are applicable instead
00030  * of those above. If you wish to allow use of your version of this file only
00031  * under the terms of either the GPL or the LGPL, and not to allow others to
00032  * use your version of this file under the terms of the MPL, indicate your
00033  * decision by deleting the provisions above and replace them with the notice
00034  * and other provisions required by the GPL or the LGPL. If you do not delete
00035  * the provisions above, a recipient may use your version of this file under
00036  * the terms of any one of the MPL, the GPL or the LGPL.
00037  *
00038  * ***** END LICENSE BLOCK ***** */
00039 
00040 #include <windows.h>
00041 #include <io.h>
00042 #include <stdlib.h>
00043 #include <stdio.h>
00044 #include <tchar.h>
00045 #include <Winreg.h>
00046 
00047 #include "CondMgr.h"
00048 #include "HSAPI.h"
00049 #include "resource.h"
00050 
00051 #define MOZ_PALMSYNC_PROXY      ".\\PalmSyncProxy.dll"
00052 #define CREATOR                  "addr"
00053 #define CONDUIT_FILENAME         "mozABConduit.dll"
00054 #define REMOTE_DB                "AddressDB"
00055 #define CONDUIT_NAME             "address"
00056 #define CONDUIT_PRIORITY         2
00057 
00058 #define CONDMGR_FILENAME         "CondMgr.dll"
00059 #define HSAPI_FILENAME           "HsApi.dll"
00060 #define DIRECTORY_SEPARATOR      '\\'
00061 #define DIRECTORY_SEPARATOR_STR  "\\"
00062 #define EXECUTABLE_EXTENSION     ".exe"
00063 #define HOTSYNC_MAX_WAIT         30         // wait for HotSync to start/stop in seconds
00064 
00065 #define MAX_LOADSTRING 256
00066 
00067 // Define any Conduit Manager function pointer types
00068 typedef int (WINAPI *CmGetCorePathPtr)(TCHAR *pPath, int *piSize);
00069 typedef int (WINAPI *CmGetHotSyncExecPathPtr)(char *szPath, int *iSize);
00070 
00071 typedef int (WINAPI *CmInstallCreatorPtr)(const char *pCreator, int iType);
00072 typedef int (WINAPI *CmRemoveConduitByCreatorIDPtr)(const char *pCreator);
00073 typedef int (WINAPI *CmRestoreHotSyncSettingsPtr)(BOOL bToDefaults);
00074 typedef int (WINAPI *CmSetCreatorRemotePtr)(const char *pCreator, const TCHAR *pRemote);
00075 typedef int (WINAPI *CmSetCreatorNamePtr)(const char *pCreator, const TCHAR *pConduitName);
00076 typedef int (WINAPI *CmSetCreatorTitlePtr)(const char *pCreator, const TCHAR *pConduitTitle);
00077 typedef int (WINAPI *CmSetCreatorFilePtr)(const char *pCreator, const TCHAR *pConduitFile);
00078 typedef int (WINAPI *CmSetCreatorDirectoryPtr)(const char *pCreator, const TCHAR *pConduitDirectory);
00079 
00080 typedef int (WINAPI *CmSetCreatorPriorityPtr)(const char *pCreator, DWORD dwPriority);
00081 typedef int (WINAPI *CmSetCreatorIntegratePtr)(const char *pCreator, DWORD dwIntegrate);
00082 typedef int (WINAPI *CmSetCreatorValueDwordPtr)(const char *pCreator, TCHAR *pValue, DWORD dwValue);
00083 typedef int (WINAPI *CmSetCreatorValueStringPtr)(const char *pCreator, TCHAR *pValueName, TCHAR *value);
00084 typedef int (WINAPI *CmSetCorePathPtr) (const char *pPath);
00085 typedef int (WINAPI *CmSetHotSyncExePathPtr) (const char *pPath);
00086 typedef int (WINAPI *CmSetCreatorModulePtr) (const char *pCreatorID, const TCHAR *pModule);
00087 
00088 typedef int (WINAPI *CmGetCreatorNamePtr)(const char *pCreator, TCHAR *pConduitName, int *pSize);
00089 typedef int (WINAPI *CmGetCreatorTitlePtr)(const char *pCreator, TCHAR *pConduitTitle, int *pSize);
00090 typedef int (WINAPI *CmGetCreatorPriorityPtr)(const char *pCreator, DWORD *dwPriority);
00091 typedef int (WINAPI *CmGetCreatorTypePtr)(const char *pCreator);
00092 typedef int (WINAPI *CmGetCreatorIntegratePtr)(const char *pCreator, DWORD *dwIntegrate);
00093 typedef int (WINAPI *CmGetCreatorValueDwordPtr)(const char *pCreator, TCHAR *pValueName, DWORD dwValue, DWORD dwDefault);
00094 typedef int (WINAPI *CmGetCreatorValueStringPtr)(const char *pCreator, TCHAR *pValueName, TCHAR *pValue, int *pSize, TCHAR *pDefault);
00095 typedef int (WINAPI *CmGetCreatorFilePtr) (const TCHAR *pCreatorID, TCHAR *pFile, int *piSize);
00096 typedef int (WINAPI *CmGetCreatorDirectoryPtr) (const TCHAR *pCreatorID, TCHAR *pFile, int *piSize);
00097 typedef int (WINAPI *CmGetCreatorModulePtr) (const char *pCreatorID, TCHAR *pModule, int *piSize);
00098 typedef int (WINAPI *CmGetCreatorRemotePtr)(const char *pCreator, const TCHAR *pRemote, int*pSize);
00099 
00100 // Define any HSAPI function pointer types
00101 typedef int (WINAPI *HsCheckApiStatusPtr)(void);
00102 typedef int (WINAPI *HsGetSyncStatusPtr)(DWORD *dwStatus);
00103 typedef int (WINAPI *HsSetAppStatusPtr)(HsStatusType statusType, DWORD dwStartFlags);
00104 
00105 // Define general registration fn pointer types
00106 typedef int (WINAPI *mozDllRegisterServerPtr)(void);
00107 typedef int (WINAPI *mozDllUnregisterServerPtr)(void);
00108 
00109 // forward declaration
00110 int  InstallConduit(HINSTANCE hInstance, TCHAR *installPath);
00111 int UninstallConduit();
00112 void ConstructMessage(HINSTANCE hInstance, DWORD dwMessageId, TCHAR *formattedMsg);
00113 
00114 // Global vars
00115 BOOL    gWasHotSyncRunning = FALSE;
00116 
00117 void ConstructMessage(HINSTANCE hInstance, DWORD dwMessageId, TCHAR *formattedMsg)
00118 {
00119   // Load brand name and the format string.
00120   TCHAR brandName[MAX_LOADSTRING];
00121   TCHAR formatString[MAX_LOADSTRING];
00122   LoadString(hInstance, IDS_BRAND_NAME, brandName, MAX_LOADSTRING-1);
00123   LoadString(hInstance, dwMessageId, formatString, MAX_LOADSTRING-1);
00124 
00125   // A few msgs needs two brand name substitutions.
00126   if ((dwMessageId == IDS_SUCCESS_INSTALL) ||
00127       (dwMessageId == IDS_CONFIRM_INSTALL) ||
00128       (dwMessageId == IDS_ERR_REGISTERING_MOZ_DLL))
00129     _sntprintf(formattedMsg, MAX_LOADSTRING-1, formatString, brandName, brandName);
00130   else
00131     _sntprintf(formattedMsg, MAX_LOADSTRING-1, formatString, brandName);
00132 
00133   formattedMsg[MAX_LOADSTRING-1]='\0';
00134 }
00135 
00136 int APIENTRY WinMain(HINSTANCE hInstance,
00137                      HINSTANCE hPrevInstance,
00138                      LPSTR     lpCmdLine,
00139                      int       nCmdShow)
00140 {
00141     TCHAR appTitle[MAX_LOADSTRING];
00142     TCHAR msgStr[MAX_LOADSTRING];
00143 
00144     int strResource=0;
00145     int res=-1;
00146 
00147     // /p can only be used with a standard install, i.e., non-silent install
00148     char *installDir = strstr(lpCmdLine, "/p");
00149     if (installDir)
00150       installDir += 2; // advance past "/p", e.g., "/pC:/program files/mozilla/dist/bin"
00151 
00152     if(!strcmpi(lpCmdLine,"/u")) // un-install
00153     {
00154         ConstructMessage(hInstance, IDS_APP_TITLE_UNINSTALL, appTitle);
00155         ConstructMessage(hInstance, IDS_CONFIRM_UNINSTALL, msgStr);
00156         if (MessageBox(NULL, msgStr, appTitle, MB_YESNO | MB_TOPMOST) == IDYES) 
00157         {
00158             res = UninstallConduit();
00159             if(!res)
00160                 res = IDS_SUCCESS_UNINSTALL;
00161         }
00162         else
00163           return 0;
00164     }
00165     else if (!strcmpi(lpCmdLine,"/us")) // silent un-install
00166     {
00167         return UninstallConduit();
00168     }
00169     else if (!strcmpi(lpCmdLine,"/s")) // silent install
00170     {
00171         return InstallConduit(hInstance, installDir);
00172     }
00173     else // install
00174     {
00175         ConstructMessage(hInstance, IDS_APP_TITLE_INSTALL, appTitle);
00176         ConstructMessage(hInstance, IDS_CONFIRM_INSTALL, msgStr);
00177         if (MessageBox(NULL, msgStr, appTitle, MB_YESNO) == IDYES) 
00178         {
00179             res = InstallConduit(hInstance, installDir);
00180             if(!res)
00181                 res = IDS_SUCCESS_INSTALL;
00182         }
00183     }
00184 
00185     if(res > IDS_ERR_MAX || res < IDS_ERR_GENERAL)
00186         res = IDS_ERR_GENERAL;
00187 
00188     ConstructMessage(hInstance, res, msgStr);
00189     MessageBox(NULL, msgStr, appTitle, MB_OK);
00190 
00191     return 0;
00192 }
00193 
00194 // this function gets the install dir for installation
00195 int GetPalmDesktopInstallDirectory(TCHAR *pPDInstallDirectory, unsigned long *pSize)
00196 {
00197     HKEY   key;
00198     // open the key
00199     LONG rc = ::RegOpenKey(HKEY_CURRENT_USER, "Software\\U.S. Robotics\\Pilot Desktop\\Core", &key);
00200     if (rc == ERROR_SUCCESS) {
00201         // get key value
00202         rc = ::RegQueryValueEx(key, "Path", NULL, NULL, 
00203                                (LPBYTE)pPDInstallDirectory, pSize);
00204         if (rc == ERROR_SUCCESS) {
00205             *pSize = _tcslen(pPDInstallDirectory); // windows only
00206             rc=0; // 0 is success for us
00207         }
00208         // close the key
00209         ::RegCloseKey(key);
00210     }
00211 
00212     if(rc) {
00213         HKEY   key2;
00214         // open the key
00215         rc = ::RegOpenKey(HKEY_LOCAL_MACHINE, "Software\\Microsoft\\Windows\\CurrentVersion\\App Paths\\palm.exe", &key2);
00216         if (rc == ERROR_SUCCESS) {
00217             // get the default key value
00218             rc = ::RegQueryValueEx(key2, "", NULL, NULL, 
00219                                    (LPBYTE)pPDInstallDirectory, pSize);
00220             // get only the path (ie, strip out the exe name). note that we don't use string match
00221             // for the exe name here since it's possilbe that the exe name in the default setting
00222             // is different from the exe name in RegOpenKey() call. For example, the exe name in
00223             // the default setting for "Software\\...\\App Paths\\pbrush.exe" is mspaint.exe.
00224             if (rc == ERROR_SUCCESS) {
00225               TCHAR *end = pPDInstallDirectory + _tcslen(pPDInstallDirectory);
00226               while ((*end != '\\') && (end != pPDInstallDirectory))
00227                 end--;
00228               *end = '\0';
00229               rc=0; // 0 is success for us
00230             }
00231             // close the key
00232             ::RegCloseKey(key2);
00233         }
00234     }
00235     
00236     return rc;
00237 }
00238 
00239 // this function loads the Conduit Manager
00240 int LoadConduitManagerDll(HINSTANCE* hCondMgrDll, const TCHAR * szPalmDesktopDirectory)
00241 {
00242     // Initialize the return value
00243     *hCondMgrDll=NULL;
00244     
00245     // Construct the path of the Palm Desktop Conduit Manager
00246     TCHAR   szPDCondMgrPath[_MAX_PATH];
00247     // take care of any possible string overwrites
00248     if((strlen(szPalmDesktopDirectory) + strlen(DIRECTORY_SEPARATOR_STR) +  strlen(CONDMGR_FILENAME)) >= _MAX_PATH)
00249         return IDS_ERR_LOADING_CONDMGR;
00250     strcpy(szPDCondMgrPath, szPalmDesktopDirectory);
00251     strcat(szPDCondMgrPath, DIRECTORY_SEPARATOR_STR);
00252     strcat(szPDCondMgrPath, CONDMGR_FILENAME);
00253     // Load the Conduit Manager library from the Palm Desktop directory
00254     if( (*hCondMgrDll=LoadLibrary(szPDCondMgrPath)) != NULL )
00255         // Successfully loaded CondMgr Library from Palm Desktop Directory
00256         return 0;
00257 
00258     return IDS_ERR_LOADING_CONDMGR;
00259 }
00260 
00261 // this function loads the Hsapi Dll
00262 int LoadHsapiDll(HINSTANCE* hHsapiDLL, const TCHAR * szPalmDesktopDirectory)
00263 {
00264     // Initialize the return value
00265     *hHsapiDLL=NULL;
00266     
00267     TCHAR   szHsapiPath[_MAX_PATH];
00268     // take care of any possible string overwrites
00269     if((strlen(szPalmDesktopDirectory) + strlen(DIRECTORY_SEPARATOR_STR) +  strlen(HSAPI_FILENAME)) >= _MAX_PATH)
00270         return IDS_ERR_LOADING_CONDMGR;
00271     strcpy(szHsapiPath, szPalmDesktopDirectory);
00272     strcat(szHsapiPath, DIRECTORY_SEPARATOR_STR);
00273     strcat(szHsapiPath, HSAPI_FILENAME);
00274     if( (*hHsapiDLL=LoadLibrary(szHsapiPath)) != NULL )
00275         // Successfully loaded HSAPI Library from Palm Desktop Directory
00276         return 0;
00277 
00278     // If we get here, then there was an error loading the library
00279     return IDS_ERR_HSAPI_NOT_FOUND;
00280 }
00281 
00282 // finds if HotSync Manager is running
00283 BOOL IsHotSyncRunning(HINSTANCE hHsapiDLL)
00284 {
00285     BOOL    bRetVal = FALSE;
00286     
00287     if(!hHsapiDLL)
00288         return bRetVal;
00289     
00290     // Prepare to use the HSAPI functions
00291     HsCheckApiStatusPtr lpfnHsCheckApiStatus;
00292     lpfnHsCheckApiStatus = (HsCheckApiStatusPtr) GetProcAddress(hHsapiDLL, "HsCheckApiStatus");
00293     
00294     if( lpfnHsCheckApiStatus )
00295     {
00296         if( (*lpfnHsCheckApiStatus)() == 0 )
00297             bRetVal = TRUE;
00298     }
00299     return bRetVal;
00300 }
00301 
00302 // finds if a sync process is going on
00303 BOOL IsHotSyncInProgress(HINSTANCE hHsapiDLL)
00304 {
00305     DWORD dwStatus;
00306     
00307     if(!hHsapiDLL)
00308         return FALSE;
00309     
00310     if(IsHotSyncRunning(hHsapiDLL))
00311     {
00312         // Prepare to use the HSAPI functions
00313         HsGetSyncStatusPtr  lpfnHsGetSyncStatus;
00314         lpfnHsGetSyncStatus = (HsGetSyncStatusPtr) GetProcAddress(hHsapiDLL, "HsGetSyncStatus");
00315     
00316         if( lpfnHsGetSyncStatus )
00317         {
00318             if( (*lpfnHsGetSyncStatus)(&dwStatus) == 0 )
00319                 if( dwStatus == HOTSYNC_STATUS_IDLE )
00320                     return FALSE;
00321         }
00322     }
00323     return TRUE;
00324 }
00325 
00326 // shuts down the HotSync Manager
00327 void ShutdownHotSync(HINSTANCE hHsapiDLL)
00328 {
00329     if(!hHsapiDLL)
00330         return;
00331 
00332     BOOL    bHotSyncRunning=IsHotSyncRunning(hHsapiDLL);
00333     
00334     if(bHotSyncRunning)
00335     {
00336         // Prepare to use the HSAPI functions
00337         HsSetAppStatusPtr   lpfnHsSetAppStatus;
00338         lpfnHsSetAppStatus = (HsSetAppStatusPtr) GetProcAddress(hHsapiDLL, "HsSetAppStatus");
00339     
00340         if( lpfnHsSetAppStatus )
00341             (*lpfnHsSetAppStatus)(HsCloseApp, HSFLAG_NONE);
00342         
00343         // Wait for HotSync to stop
00344         for( int i=0; (i<HOTSYNC_MAX_WAIT*2) && bHotSyncRunning; i++ )
00345         {
00346             if( (bHotSyncRunning=IsHotSyncRunning(hHsapiDLL)) == TRUE )
00347                 Sleep(500);
00348         }
00349     }
00350 }
00351 
00352 // starts HotSync Manager if not runnning
00353 void StartHotSync(HINSTANCE hHsapiDLL)
00354 {
00355     if(!hHsapiDLL)
00356         return;
00357 
00358     BOOL    bHotSyncRunning=IsHotSyncRunning(hHsapiDLL);
00359     
00360     if(!bHotSyncRunning)
00361     {
00362         // Prepare to use the HSAPI functions
00363         HsSetAppStatusPtr   lpfnHsSetAppStatus;
00364         lpfnHsSetAppStatus = (HsSetAppStatusPtr) GetProcAddress(hHsapiDLL, "HsSetAppStatus");
00365     
00366         if( lpfnHsSetAppStatus )
00367             (*lpfnHsSetAppStatus)(HsStartApp, HSFLAG_NONE);
00368         
00369         // Wait for HotSync to start
00370         for( int i=0; (i<HOTSYNC_MAX_WAIT*2) && !bHotSyncRunning; i++ )
00371         {
00372             if( (bHotSyncRunning=IsHotSyncRunning(hHsapiDLL)) == FALSE )
00373                 Sleep(500);
00374         }
00375     }
00376 }
00377 
00378 int RegisterMozPalmSyncDll()
00379 {
00380     HINSTANCE hMozPalmSyncProxyDll = NULL;
00381     if( (hMozPalmSyncProxyDll=LoadLibrary(MOZ_PALMSYNC_PROXY)) != NULL ) {
00382         mozDllRegisterServerPtr lpfnmozDllRegisterServer;
00383         lpfnmozDllRegisterServer = (mozDllRegisterServerPtr) GetProcAddress(hMozPalmSyncProxyDll, "DllRegisterServer");
00384         DWORD dwReturnCode = (*lpfnmozDllRegisterServer)();
00385         if(dwReturnCode == S_OK)
00386             // Successfully registered
00387             return 0;
00388     }
00389 
00390     return IDS_ERR_REGISTERING_MOZ_DLL;
00391 }
00392 
00393 int UnregisterMozPalmSyncDll()
00394 {
00395     HINSTANCE hMozPalmSyncProxyDll = NULL;
00396     if( (hMozPalmSyncProxyDll=LoadLibrary(MOZ_PALMSYNC_PROXY)) != NULL ) {
00397         mozDllUnregisterServerPtr lpfnmozDllUnregisterServer;
00398         lpfnmozDllUnregisterServer = (mozDllUnregisterServerPtr) GetProcAddress(hMozPalmSyncProxyDll, "DllUnregisterServer");
00399         DWORD dwReturnCode = (*lpfnmozDllUnregisterServer)();
00400         if(dwReturnCode == S_OK)
00401             // Successfully registered
00402             return 0;
00403     }
00404 
00405     return IDS_ERR_UNREGISTERING_MOZ_DLL;
00406 }
00407 
00408 
00409 char *mystrsep(char **stringp, char delim)
00410 {
00411   char *endStr = strchr(*stringp, delim);
00412   char *retStr;
00413   if (endStr)
00414   {
00415     bool foundDelim = (*endStr == delim);
00416     *endStr = '\0';
00417     retStr = *stringp;
00418     *stringp = endStr + !!foundDelim;
00419   }
00420   else
00421     return NULL;
00422   return retStr;
00423 }
00424 
00425 char oldSettingsStr[500];
00426 static char             gSavedCwd[_MAX_PATH];
00427 
00428 // installs our Conduit
00429 int InstallConduit(HINSTANCE hInstance, TCHAR *installDir)
00430 { 
00431     int dwReturnCode;
00432     BOOL    bHotSyncRunning = FALSE;
00433 
00434     // Prepare the full path of the conduit.
00435     // Applications should not place conduits in the Palm Desktop directory.
00436     // The Palm Desktop installer only manages the Palm Desktop conduits.
00437 
00438     TCHAR szConduitPath[_MAX_PATH];
00439     if (!installDir)
00440     {
00441       if(!GetModuleFileName(NULL, szConduitPath, _MAX_PATH))
00442           return IDS_ERR_CONDUIT_NOT_FOUND;
00443       // extract the dir path (without the module name)
00444       int index = strlen(szConduitPath)-1;
00445       while((szConduitPath[index] != DIRECTORY_SEPARATOR) && index)
00446           index--;
00447       szConduitPath[index] = 0;
00448     }
00449     else
00450       strncpy(szConduitPath, installDir, sizeof(szConduitPath) - 1);
00451 
00452 
00453     // take care of any possible string overwrites
00454     if((strlen(szConduitPath) + strlen(DIRECTORY_SEPARATOR_STR) + strlen(CONDUIT_FILENAME)) > _MAX_PATH)
00455         return IDS_ERR_LOADING_CONDMGR;
00456     // might already have conduit filename in szConduitPath if we're called recursively
00457     if (!strstr(szConduitPath, CONDUIT_FILENAME)) 
00458     {
00459       if (szConduitPath[strlen(szConduitPath) - 1] != DIRECTORY_SEPARATOR)
00460         strcat(szConduitPath, DIRECTORY_SEPARATOR_STR);
00461       strcat(szConduitPath, CONDUIT_FILENAME);
00462     }
00463     // Make sure the conduit dll exists
00464     struct _finddata_t dll_file;
00465     long hFile;
00466     if( (hFile = _findfirst( szConduitPath, &dll_file )) == -1L )
00467         return IDS_ERR_CONDUIT_NOT_FOUND;
00468 
00469     // now register the Mozilla Palm Sync Support Dll
00470     if( (dwReturnCode = RegisterMozPalmSyncDll()) != 0)
00471         return dwReturnCode;
00472     
00473     // Get the Palm Desktop Installation directory
00474     TCHAR   szPalmDesktopDir[_MAX_PATH];
00475     unsigned long desktopSize=_MAX_PATH;
00476     
00477     // old conduit settings - MAX_PATH is arbitrarily long...
00478     TCHAR szOldCreatorName[_MAX_PATH];
00479     TCHAR szOldRemote[_MAX_PATH];
00480     TCHAR szOldCreatorTitle[_MAX_PATH];
00481     TCHAR szOldCreatorFile[_MAX_PATH];
00482     TCHAR szOldCreatorDirectory[_MAX_PATH];
00483     DWORD oldPriority;
00484     DWORD oldIntegrate;
00485     int   oldType;
00486 
00487     // Load the Conduit Manager DLL.
00488     HINSTANCE hConduitManagerDLL;
00489     if( (dwReturnCode = GetPalmDesktopInstallDirectory(szPalmDesktopDir, &desktopSize)) == 0 ) 
00490     {
00491         // need to switch current working directory to directory with palm dlls
00492         // because of a bug in Palm Desktop 6.01
00493 
00494         GetCurrentDirectory(sizeof(gSavedCwd), gSavedCwd);
00495         SetCurrentDirectory(szPalmDesktopDir);
00496 
00497         if( (dwReturnCode = LoadConduitManagerDll(&hConduitManagerDLL, szPalmDesktopDir)) != 0 )
00498             return dwReturnCode;
00499     }
00500     else 
00501         return IDS_ERR_CONDUIT_NOT_FOUND;
00502     
00503     // Prepare to install the conduit using Conduit Manager functions
00504     CmInstallCreatorPtr lpfnCmInstallCreator;
00505     lpfnCmInstallCreator = (CmInstallCreatorPtr) GetProcAddress(hConduitManagerDLL, "CmInstallCreator");
00506     CmSetCreatorRemotePtr   lpfnCmSetCreatorRemote;
00507     lpfnCmSetCreatorRemote = (CmSetCreatorRemotePtr) GetProcAddress(hConduitManagerDLL, "CmSetCreatorRemote");
00508     CmSetCreatorNamePtr lpfnCmSetCreatorName;
00509     lpfnCmSetCreatorName = (CmSetCreatorNamePtr) GetProcAddress(hConduitManagerDLL, "CmSetCreatorName");
00510     CmSetCreatorTitlePtr lpfnCmSetCreatorTitle;
00511     lpfnCmSetCreatorTitle = (CmSetCreatorTitlePtr) GetProcAddress(hConduitManagerDLL, "CmSetCreatorTitle");
00512     CmSetCreatorPriorityPtr lpfnCmSetCreatorPriority;
00513     lpfnCmSetCreatorPriority = (CmSetCreatorPriorityPtr) GetProcAddress(hConduitManagerDLL, "CmSetCreatorPriority");
00514     CmSetCreatorIntegratePtr    lpfnCmSetCreatorIntegrate;
00515     lpfnCmSetCreatorIntegrate = (CmSetCreatorIntegratePtr) GetProcAddress(hConduitManagerDLL, "CmSetCreatorIntegrate");
00516     CmRemoveConduitByCreatorIDPtr   lpfnCmRemoveConduitByCreatorID;
00517     lpfnCmRemoveConduitByCreatorID = (CmRemoveConduitByCreatorIDPtr) GetProcAddress(hConduitManagerDLL, "CmRemoveConduitByCreatorID");
00518     CmSetCreatorValueStringPtr lpfnCmSetCreatorValueString = (CmSetCreatorValueStringPtr) GetProcAddress(hConduitManagerDLL, "CmSetCreatorValueString");
00519 
00520     CmGetCreatorRemotePtr   lpfnCmGetCreatorRemote;
00521     lpfnCmGetCreatorRemote = (CmGetCreatorRemotePtr) GetProcAddress(hConduitManagerDLL, "CmGetCreatorRemote");
00522     CmGetCreatorNamePtr lpfnCmGetCreatorName;
00523     lpfnCmGetCreatorName = (CmGetCreatorNamePtr) GetProcAddress(hConduitManagerDLL, "CmGetCreatorName");
00524     CmGetCreatorTitlePtr lpfnCmGetCreatorTitle;
00525     lpfnCmGetCreatorTitle = (CmGetCreatorTitlePtr) GetProcAddress(hConduitManagerDLL, "CmGetCreatorTitle");
00526     CmGetCreatorPriorityPtr lpfnCmGetCreatorPriority;
00527     lpfnCmGetCreatorPriority = (CmGetCreatorPriorityPtr) GetProcAddress(hConduitManagerDLL, "CmGetCreatorPriority");
00528     CmGetCreatorIntegratePtr    lpfnCmGetCreatorIntegrate;
00529     lpfnCmGetCreatorIntegrate = (CmGetCreatorIntegratePtr) GetProcAddress(hConduitManagerDLL, "CmGetCreatorIntegrate");
00530     CmGetCreatorTypePtr lpfnCmGetCreatorType = (CmGetCreatorTypePtr) GetProcAddress(hConduitManagerDLL, "CmGetCreatorType");
00531     CmGetCreatorFilePtr lpfnCmGetCreatorFile = (CmGetCreatorFilePtr) GetProcAddress(hConduitManagerDLL, "CmGetCreatorFile");
00532     CmGetCreatorDirectoryPtr lpfnCmGetCreatorDirectory = (CmGetCreatorDirectoryPtr) GetProcAddress(hConduitManagerDLL, "CmGetCreatorDirectory");
00533     if( (lpfnCmInstallCreator == NULL) 
00534         || (lpfnCmSetCreatorRemote == NULL)
00535         || (lpfnCmSetCreatorName == NULL)
00536         || (lpfnCmSetCreatorTitle == NULL)
00537         || (lpfnCmSetCreatorPriority == NULL)
00538         || (lpfnCmSetCreatorIntegrate == NULL)
00539         )
00540     {
00541         // Return error code.
00542         return(IDS_ERR_LOADING_CONDMGR);
00543     }
00544  
00545     szOldCreatorTitle[0] = '\0';
00546     szOldCreatorName[0] = '\0';
00547     szOldRemote[0] = '\0';
00548     szOldCreatorTitle[0] = '\0';
00549     szOldCreatorFile[0] = '\0';
00550     szOldCreatorDirectory[0] = '\0';
00551     // get settings for old conduit
00552     int remoteBufSize = sizeof(szOldRemote);
00553     (*lpfnCmGetCreatorRemote) (CREATOR, szOldRemote, &remoteBufSize);
00554     int creatorBufSize = sizeof(szOldCreatorName);
00555     (*lpfnCmGetCreatorName)(CREATOR, szOldCreatorName, &creatorBufSize);
00556     int creatorTitleBufSize = sizeof(szOldCreatorTitle);
00557     (*lpfnCmGetCreatorTitle)(CREATOR, szOldCreatorTitle, &creatorTitleBufSize);
00558     int creatorFileBufSize = sizeof(szOldCreatorFile);
00559     int creatorDirectoryBufSize = sizeof(szOldCreatorDirectory);
00560     (*lpfnCmGetCreatorFile)(CREATOR, szOldCreatorFile, &creatorFileBufSize);
00561     (*lpfnCmGetCreatorDirectory)(CREATOR, szOldCreatorDirectory, &creatorDirectoryBufSize);
00562     (*lpfnCmGetCreatorPriority)(CREATOR, &oldPriority);
00563     (*lpfnCmGetCreatorIntegrate)(CREATOR, &oldIntegrate);
00564     oldType = (*lpfnCmGetCreatorType) (CREATOR);
00565 
00566     // if not set by previous pass through here
00567     if (!oldSettingsStr[0])
00568       _snprintf(oldSettingsStr, sizeof(oldSettingsStr), "%s,%s,%s,%s,%s,%d,%d,%d", szOldRemote, szOldCreatorName, 
00569         szOldCreatorTitle, szOldCreatorFile, szOldCreatorDirectory, oldType, oldPriority, oldIntegrate);
00570 
00571     // Load the HSAPI DLL.
00572     HINSTANCE hHsapiDLL;
00573     if( (dwReturnCode = LoadHsapiDll(&hHsapiDLL, szPalmDesktopDir)) != 0 )
00574         return dwReturnCode;
00575         
00576     // Shutdown the HotSync Process if it is running
00577     if( (bHotSyncRunning=IsHotSyncRunning(hHsapiDLL)) )
00578     {
00579         // Check for any synchronizations in progress
00580         if( IsHotSyncInProgress(hHsapiDLL) )
00581             return IDS_ERR_HOTSYNC_IN_PROGRESS;
00582             
00583         ShutdownHotSync(hHsapiDLL);
00584         // store the flag in temp global so that in the recursive call it is restarted
00585         gWasHotSyncRunning = TRUE;
00586     }
00587     
00588     // Actually install the conduit as an Application Conduit
00589     dwReturnCode = (*lpfnCmInstallCreator)(CREATOR, CONDUIT_APPLICATION);
00590     if(dwReturnCode == ERR_CREATORID_ALREADY_IN_USE) {
00591         dwReturnCode = (*lpfnCmRemoveConduitByCreatorID)(CREATOR);
00592         if(dwReturnCode >= 0 ) {
00593             //free the library so that the existing AB Conduit is unloaded properly
00594             FreeLibrary(hConduitManagerDLL);
00595             FreeLibrary(hHsapiDLL);
00596             return InstallConduit(hInstance, szConduitPath);
00597         }
00598     }
00599     if( dwReturnCode == 0 )
00600     {
00601         (*lpfnCmSetCreatorValueString) (CREATOR, "oldConduitSettings", oldSettingsStr);
00602         dwReturnCode = (*lpfnCmSetCreatorName)(CREATOR, szConduitPath);
00603         if( dwReturnCode != 0 ) return dwReturnCode;
00604         TCHAR title[MAX_LOADSTRING];
00605         // Construct conduit title (the one displayed in HotSync Mgr's Custom...list)..
00606         ConstructMessage(hInstance, IDS_CONDUIT_TITLE, title);
00607         dwReturnCode = (*lpfnCmSetCreatorTitle)(CREATOR, title);
00608         if( dwReturnCode != 0 ) return dwReturnCode;
00609         dwReturnCode = (*lpfnCmSetCreatorRemote)(CREATOR, REMOTE_DB);
00610         if( dwReturnCode != 0 ) return dwReturnCode;
00611         dwReturnCode = (*lpfnCmSetCreatorPriority)(CREATOR, CONDUIT_PRIORITY);
00612         if( dwReturnCode != 0 ) return dwReturnCode;
00613         // Applications should always set Integrate to 0
00614         dwReturnCode = (*lpfnCmSetCreatorIntegrate)(CREATOR, (DWORD)0);
00615         (*lpfnCmSetCreatorValueString) (CREATOR, "oldConduitSettings", oldSettingsStr);
00616     }
00617     
00618     // Re-start HotSync if it was running before
00619     if( gWasHotSyncRunning )
00620         StartHotSync(hHsapiDLL);
00621 
00622     // restore cwd, if we changed it.        
00623     if (gSavedCwd[0])
00624       SetCurrentDirectory(gSavedCwd);
00625 
00626     return(dwReturnCode);
00627 }
00628 
00629 // uninstalls our conduit
00630 int UninstallConduit()
00631 { 
00632     int dwReturnCode;
00633     BOOL    bHotSyncRunning = FALSE;
00634 
00635     // Get the Palm Desktop Installation directory
00636     TCHAR   szPalmDesktopDir[_MAX_PATH];
00637     unsigned long desktopSize=_MAX_PATH;
00638     // Load the Conduit Manager DLL.
00639     HINSTANCE hConduitManagerDLL;
00640     if( (dwReturnCode=GetPalmDesktopInstallDirectory(szPalmDesktopDir, &desktopSize)) == 0 )
00641     {
00642         if( (dwReturnCode = LoadConduitManagerDll(&hConduitManagerDLL, szPalmDesktopDir)) != 0 )
00643                 return(dwReturnCode);
00644     }
00645     // if registery key not load it from local dir if present by any chance
00646     else 
00647         return(dwReturnCode);
00648     
00649     // need to switch current working directory to directory with palm dlls
00650     // because of a bug in Palm Desktop 6.01
00651 
00652     GetCurrentDirectory(sizeof(gSavedCwd), gSavedCwd);
00653     SetCurrentDirectory(szPalmDesktopDir);
00654 
00655     // Prepare to uninstall the conduit using Conduit Manager functions
00656     CmRemoveConduitByCreatorIDPtr   lpfnCmRemoveConduitByCreatorID;
00657     lpfnCmRemoveConduitByCreatorID = (CmRemoveConduitByCreatorIDPtr) GetProcAddress(hConduitManagerDLL, "CmRemoveConduitByCreatorID");
00658     if( (lpfnCmRemoveConduitByCreatorID == NULL) )
00659         return(IDS_ERR_LOADING_CONDMGR);
00660     CmSetCorePathPtr lpfnCmSetCorePath = (CmSetCorePathPtr) GetProcAddress(hConduitManagerDLL, "CmSetCorePath");
00661     CmSetHotSyncExePathPtr lpfnCmSetHotSyncExePath = (CmSetHotSyncExePathPtr) GetProcAddress(hConduitManagerDLL, "CmSetHotSyncExecPath");
00662     CmRestoreHotSyncSettingsPtr lpfnCmRestoreHotSyncSettings;
00663     lpfnCmRestoreHotSyncSettings = (CmRestoreHotSyncSettingsPtr) GetProcAddress(hConduitManagerDLL, "CmRestoreHotSyncSettings");
00664     CmGetCreatorValueStringPtr lpfnCmGetCreatorValueString = (CmGetCreatorValueStringPtr) GetProcAddress(hConduitManagerDLL, "CmGetCreatorValueString");
00665     CmInstallCreatorPtr lpfnCmInstallCreator;
00666     lpfnCmInstallCreator = (CmInstallCreatorPtr) GetProcAddress(hConduitManagerDLL, "CmInstallCreator");
00667     CmSetCreatorRemotePtr   lpfnCmSetCreatorRemote;
00668     lpfnCmSetCreatorRemote = (CmSetCreatorRemotePtr) GetProcAddress(hConduitManagerDLL, "CmSetCreatorRemote");
00669     CmSetCreatorNamePtr lpfnCmSetCreatorName;
00670     lpfnCmSetCreatorName = (CmSetCreatorNamePtr) GetProcAddress(hConduitManagerDLL, "CmSetCreatorName");
00671     CmSetCreatorTitlePtr lpfnCmSetCreatorTitle;
00672     lpfnCmSetCreatorTitle = (CmSetCreatorTitlePtr) GetProcAddress(hConduitManagerDLL, "CmSetCreatorTitle");
00673     CmSetCreatorPriorityPtr lpfnCmSetCreatorPriority;
00674     lpfnCmSetCreatorPriority = (CmSetCreatorPriorityPtr) GetProcAddress(hConduitManagerDLL, "CmSetCreatorPriority");
00675     CmSetCreatorIntegratePtr    lpfnCmSetCreatorIntegrate;
00676     lpfnCmSetCreatorIntegrate = (CmSetCreatorIntegratePtr) GetProcAddress(hConduitManagerDLL, "CmSetCreatorIntegrate");
00677     CmSetCreatorFilePtr lpfnCmSetCreatorFile = (CmSetCreatorFilePtr) GetProcAddress(hConduitManagerDLL, "CmSetCreatorFile");
00678     CmSetCreatorDirectoryPtr lpfnCmSetCreatorDirectory = (CmSetCreatorDirectoryPtr) GetProcAddress(hConduitManagerDLL, "CmSetCreatorDirectory");
00679     if( (lpfnCmRestoreHotSyncSettings == NULL) )
00680         return(IDS_ERR_LOADING_CONDMGR);
00681     
00682     // Load the HSAPI DLL.
00683     HINSTANCE hHsapiDLL;
00684     if( (dwReturnCode = LoadHsapiDll(&hHsapiDLL, szPalmDesktopDir)) != 0 )
00685           return(dwReturnCode);
00686         
00687     // Shutdown the HotSync Process if it is running
00688     if( (bHotSyncRunning=IsHotSyncRunning(hHsapiDLL)) )
00689     {
00690         // Check for any synchronizations in progress
00691         if( IsHotSyncInProgress(hHsapiDLL) )
00692             return IDS_ERR_HOTSYNC_IN_PROGRESS;
00693             
00694         ShutdownHotSync(hHsapiDLL);
00695     }
00696     
00697     TCHAR oldConduitSettings[500];
00698     int strSize = sizeof(oldConduitSettings);
00699     dwReturnCode = (*lpfnCmGetCreatorValueString)(CREATOR, "oldConduitSettings", oldConduitSettings, &strSize, "");
00700 
00701     // Actually uninstall the conduit
00702     dwReturnCode = (*lpfnCmRemoveConduitByCreatorID)(CREATOR);
00703 
00704     if(dwReturnCode >= 0) 
00705     {
00706         // uninstall Mozilla Palm Sync Support Proxy Dll
00707         dwReturnCode = UnregisterMozPalmSyncDll();
00708 //        dwReturnCode = (*lpfnCmRestoreHotSyncSettings)(TRUE);
00709     }
00710 
00711     if (dwReturnCode >= 0)
00712     {
00713       char * szOldCreatorName;
00714       char *szOldRemote;
00715       char *szOldCreatorTitle;
00716       char *szOldCreatorFile;
00717       char *szOldCreatorDirectory;
00718       char *oldIntStr;
00719       DWORD oldPriority;
00720       DWORD oldIntegrate;
00721       int   oldType;
00722 
00723       char *strPtr = oldConduitSettings;
00724       szOldRemote = mystrsep(&strPtr, ',');
00725       szOldCreatorName = mystrsep(&strPtr, ',');
00726       szOldCreatorTitle = mystrsep(&strPtr, ',');
00727       szOldCreatorFile = mystrsep(&strPtr, ',');
00728       szOldCreatorDirectory = mystrsep(&strPtr, ',');
00729       oldIntStr = mystrsep(&strPtr, ',');
00730       oldType = (oldIntStr) ? atoi(oldIntStr) : 0;
00731       oldIntStr = mystrsep(&strPtr, ',');
00732       oldPriority = (oldIntStr) ? atoi(oldIntStr) : 0;
00733       oldIntStr = mystrsep(&strPtr, ',');
00734       oldIntegrate = (oldIntStr) ? atoi(oldIntStr) : 0;
00735 
00736       dwReturnCode = (*lpfnCmInstallCreator)(CREATOR, oldType);
00737       if( dwReturnCode == 0 )
00738       {
00739           dwReturnCode = (*lpfnCmSetCreatorName)(CREATOR, szOldCreatorName);
00740           if( dwReturnCode != 0 ) return dwReturnCode;
00741           // Construct conduit title (the one displayed in HotSync Mgr's Custom...list)..
00742           dwReturnCode = (*lpfnCmSetCreatorTitle)(CREATOR, szOldCreatorTitle);
00743           if( dwReturnCode != 0 ) return dwReturnCode;
00744           dwReturnCode = (*lpfnCmSetCreatorRemote)(CREATOR, szOldRemote);
00745           if( dwReturnCode != 0 ) return dwReturnCode;
00746           dwReturnCode = (*lpfnCmSetCreatorFile)(CREATOR, szOldCreatorFile);
00747           if( dwReturnCode != 0 ) return dwReturnCode;
00748           dwReturnCode = (*lpfnCmSetCreatorDirectory)(CREATOR, szOldCreatorDirectory);
00749           if( dwReturnCode != 0 ) return dwReturnCode;
00750           dwReturnCode = (*lpfnCmSetCreatorPriority)(CREATOR, oldPriority);
00751           if( dwReturnCode != 0 ) return dwReturnCode;
00752           // Applications should always set Integrate to 0
00753           dwReturnCode = (*lpfnCmSetCreatorIntegrate)(CREATOR, oldIntegrate);
00754       }
00755     }
00756 
00757 //    if (lpfnCmSetCorePath)
00758 //      (*lpfnCmSetCorePath)(szPalmDesktopDir);
00759 //    if (lpfnCmSetHotSyncExePath)
00760 //      (*lpfnCmSetHotSyncExePath)(szPalmHotSyncInstallDir);
00761     
00762     // this registry key is set by the RestoreHotSyncSettings to point incorrectly to Mozilla dir
00763     // this should point to the Palm directory to enable sync with Palm Desktop.
00764 #if 0
00765     HKEY key;
00766     LONG rc = RegOpenKeyEx(HKEY_CURRENT_USER, "Software\\U.S. Robotics\\Pilot Desktop\\Core",
00767                                 0, KEY_ALL_ACCESS, &key);
00768     if(rc == ERROR_SUCCESS)
00769         ::RegSetValueEx(key, "Path", 0, REG_SZ, (const BYTE *) szPalmDesktopDir, desktopSize);
00770  
00771     if(rc == ERROR_SUCCESS)
00772         ::RegSetValueEx(key, "HotSyncPath", 0, REG_SZ, (const BYTE *) szPalmHotSyncInstallDir, installSize);
00773 #endif
00774     // Re-start HotSync if it was running before
00775     if( bHotSyncRunning )
00776         StartHotSync(hHsapiDLL);
00777         
00778     // restore cwd, if we changed it.        
00779     if (gSavedCwd[0])
00780       SetCurrentDirectory(gSavedCwd);
00781 
00782     if( dwReturnCode < 0 ) 
00783         return dwReturnCode;
00784     else 
00785         return(0);
00786 }
00787