home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 7 / Apprentice-Release7.iso / Utilities / Installers / Smaller Installer 2.0.1 / Preinstalled version / Examples / Hook Procedure Examples / SetNameHook / SetNameHook.c next >
Encoding:
C/C++ Source or Header  |  1996-07-26  |  7.8 KB  |  246 lines  |  [TEXT/CWIE]

  1. /******************************************************************************
  2.     Smaller Installer © 1996 Bill Goodman, All Rights Reserved
  3. *******************************************************************************
  4.  
  5. SaveAs Hook Example
  6.  
  7. This installer hook procedure allows the user to select the destination folder
  8. for an installed item (either a file or folder). It also allows the user to
  9. change the name of the installed item.
  10.  
  11. Limitations: This hook will not work with installers that must "remove" items.
  12. It also will not allow a user to install a folder over an existing folder.
  13.  
  14. To build this hook procedure, compile this code and create a code resource
  15. (Type:SICR, ID:501, non-preloaded, nonpurgeable, unlocked, unprotected,
  16. non-sysheap). Add this resource to the "SetNameHook.rsrc" file. Copy all the
  17. resources in "SetNameHook.rsrc" to your installer's resource file.
  18.  
  19. Set the strings in the STR#:500 resource to the following values:
  20.     1 - Name of item to relocate (must be unique in archive)
  21.     2 - Prompt string for install dialog
  22.  
  23. ******************************************************************************/
  24.  
  25. // This file is compatible with version 2.1 of the universal headers
  26. #include <LowMem.h>
  27. #include <StandardFile.h>
  28. #include <TextUtils.h>
  29.  
  30. #ifdef __MWERKS__
  31. #include <A4Stuff.h>
  32. #endif
  33.  
  34. #ifdef THINK_C
  35. #include <SetUpA4.h>
  36. #endif
  37.  
  38. #include "SIHookProc.h"
  39.  
  40.  
  41. /******************************************************************************
  42.     Module Internal Function Prototypes
  43. ******************************************************************************/
  44. void BeforeItemFunction(void);
  45. void BeginOperationFunction(void);
  46.  
  47.  
  48. /******************************************************************************
  49.     Constant Definitions
  50. ******************************************************************************/
  51. // Dialog Definitions
  52. #define selectDlg            500    // Select dialog
  53.  
  54. // Alert Definitions
  55. #define changeVolAlrt    510    // "If you wish to use a different volume, please use the DRIVE button to select the volume and try again."
  56.  
  57. // Indexed String Definitions
  58. #define genIStrID            500    // Indexed string resource ID
  59. enum
  60.     {
  61.     itemNameIStr = 1,                // Name of item to relocate
  62.     installPromptIStr                // Prompt string for install dialog
  63.     };
  64.  
  65.  
  66. /******************************************************************************
  67.     Module Variable Definitions
  68. ******************************************************************************/
  69. SIHookParmBlk *gParms;                    // Global pointer to parameter block
  70. unsigned char gEmptyStr[] = "\p";    // Global empty string
  71. Str63 gItemName;                            // Name of item to relocate
  72. long gInstallDirID;                        // Directory ID of install item
  73. Str63 gInstallName;                        // Name of install item
  74.  
  75.  
  76. /*****************************************************************************/
  77. pascal void main(
  78.         SIHookParmBlk *parmBlk    // Pointer to parameter block
  79.         )
  80. /******************************************************************************
  81.     This is the main entry point for the installer hook procedure.
  82. ******************************************************************************/
  83. {
  84. #ifdef __MWERKS__
  85. long holdA4;
  86. #endif
  87.  
  88. // Set up access to global variables
  89. #ifdef THINK_C
  90. RememberA0();
  91. SetUpA4();
  92. #endif
  93.  
  94. #ifdef __MWERKS__
  95. holdA4 = SetCurrentA4();
  96. #endif
  97.  
  98. gParms = parmBlk;
  99.  
  100. switch (gParms->function)
  101.     {
  102.     case siHookBeginOperation:
  103.         BeginOperationFunction();
  104.         break;
  105.  
  106.     case siHookBeforeItem:
  107.         BeforeItemFunction();
  108.         break;
  109.     }
  110.  
  111. // Restore original A4 value
  112. #ifdef THINK_C
  113. RestoreA4();
  114. #endif
  115.  
  116. #ifdef __MWERKS__
  117. SetA4(holdA4);
  118. #endif
  119. }
  120.  
  121.  
  122. /*****************************************************************************/
  123. void BeginOperationFunction(void)
  124. /******************************************************************************
  125.     Input parameters:
  126.         "targetVRefNum"    Volume reference number of target volume
  127.         "groupAPFlags"        Groups currently selected
  128.         "groupQUSel"
  129.         "groupVZSel"
  130.         "group32Flags"
  131.         "group64Flags"
  132.         "group96Flags"
  133.         "groupEnvironFlags"
  134.         "passwordPtr"        Pointer to password string
  135.         "filesRemaining"    Number of files remaining to install or remove
  136.         "bytesRemaining"    Number of bytes of data remaining to install or remove
  137.         "doingRemove"        Non-zero if doing remove operation
  138.  
  139.     Returns:
  140.         "passwordPtr"        Pointer to password string
  141.         "result"                Hook result code (siHookNoErr, siHookQuit, siHookAbort)
  142.  
  143.     This function is called when the install button or the remove button is
  144.     clicked to begin installing or removing files.
  145. ******************************************************************************/
  146. {
  147. Str255 promptStr;
  148. Point sfWhere = { -1, -1 };
  149. SFReply sfReply;
  150.  
  151. if (gParms->doingRemove)
  152.     return;    // Skip for remove operations
  153.  
  154. // Display dialog prompting user to select the location to install the specified item
  155. GetIndString(promptStr, genIStrID, installPromptIStr);    // Get install prompt
  156. if (promptStr[0] == 0)
  157.     goto FatalError;
  158. GetIndString(gItemName, genIStrID, itemNameIStr);    // Get item name
  159. if (gItemName[0] == 0)
  160.     goto FatalError;
  161.  
  162. if (gParms->groupEnvironFlags & siHookEnvSystem6OrLower)
  163.     {    // System 6 does not support auto-centering of dialog - set dialog location
  164.     sfWhere.h = 56;
  165.     sfWhere.v = 65;
  166.     }
  167.  
  168. LMSetSFSaveDisk(-gParms->targetVRefNum);    // Set default directory to root
  169. LMSetCurDirStore(fsRtDirID);
  170.  
  171. SFPPutFile(sfWhere, promptStr, gItemName, NULL, &sfReply, selectDlg, NULL);
  172. if (!sfReply.good)
  173.     goto Abort;    // User cancelled
  174.  
  175. if (LMGetSFSaveDisk() != -gParms->targetVRefNum)
  176.     {    // Error - user specified a location off the target volume
  177.     CautionAlert(changeVolAlrt, NULL);
  178.     goto Abort;
  179.     }
  180.  
  181. gInstallDirID = LMGetCurDirStore();    // Save install directory ID
  182. BlockMove(sfReply.fName, gInstallName, sfReply.fName[0] + 1);    // Save install name
  183.  
  184. HDelete(gParms->targetVRefNum, gInstallDirID, gInstallName);    // Delete possible duplicate file
  185. return;    // Good completion
  186.  
  187. // Abort operation
  188. Abort:
  189. gParms->result = siHookAbort;
  190. return;
  191.  
  192. // Fatal error occurred
  193. FatalError:
  194. SysBeep(1);
  195. gParms->result = siHookQuit;    // Force installer to quit
  196. }
  197.  
  198.  
  199. /*****************************************************************************/
  200. void BeforeItemFunction(void)
  201. /******************************************************************************
  202.     Input parameters:
  203.         "targetVRefNum"    Volume reference number of target volume
  204.         "groupAPFlags"        Groups currently selected
  205.         "groupQUSel"
  206.         "groupVZSel"
  207.         "group32Flags"
  208.         "group64Flags"
  209.         "group96Flags"
  210.         "groupEnvironFlags"
  211.         "filesRemaining"    Number of files remaining to install or remove
  212.         "bytesRemaining"    Number of bytes of data remaining to install or remove
  213.         "doingRemove"        Non-zero if doing remove operation
  214.         "anyItemsSkipped"    Non-zero if any item has been skipped during operation
  215.         "desVRefNum"        Volume reference number of destination volume
  216.         "desDirID"            Directory ID of destination directory
  217.         "itemName"            Name of item to install or remove
  218.         "itemIsFolder"        Non-zero if item is a folder
  219.         "fileType"            File type (files only)
  220.         "fileCreator"        File creator (files only)
  221.         "createDate"        Creation date (files only)
  222.         "lastModDate"        Last modification date (files only)
  223.         "rsrcForkLen"        Length of resource fork (files only)
  224.         "dataForkLen"        Length of data fork (files only)
  225.  
  226.     Returns:
  227.         "result"                Hook result code (siHookNoErr, siHookQuit, siHookAbort, siHookSkip, siHookItemDone)
  228.         "desDirID"            Directory ID of destination directory
  229.         "itemName"            Name of item to install or remove
  230.         "itemInfo"            Reference passed to AfterItem call
  231.  
  232.     This function is called before each item is installed or removed.
  233. ******************************************************************************/
  234. {
  235. if (gParms->doingRemove)
  236.     return;    // Skip for remove operations
  237.  
  238. // Check to see if this item is the one to relocate
  239. if (!EqualString(gParms->itemName, gItemName, true, true))
  240.     return;    // Names do not match - no action required
  241.  
  242. // Names match - change the destination directory and the item name
  243. gParms->desDirID = gInstallDirID;
  244. BlockMove(gInstallName, gParms->itemName, gInstallName[0] + 1);
  245. }
  246.