home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / System / lpDaemon SRC / lpr Sources / lprInit.C < prev    next >
Encoding:
C/C++ Source or Header  |  1993-03-16  |  9.7 KB  |  465 lines  |  [TEXT/KAHL]

  1. #include "LPR.H"
  2. #include "lprProtos.H"
  3. #include <Traps.H>
  4. #include "TCPStream.H"
  5.  
  6.  
  7. Boolean TrapAvailable(LongWord trap);
  8. void ReadPreferences(void);
  9. void WritePreferences(void);
  10.  
  11. void Fatal(integer errNo);
  12. void GetParameters(void);
  13.  
  14. integer EnumerateFiles(integer wdRef, integer *anIndex, AppFile *file);
  15.  
  16. static Boolean init_it = FALSE;
  17.  
  18.  
  19. /************************************************************************
  20.  ************************************************************************/
  21. void Initialise()
  22. {
  23.     integer    i;
  24.     WindowPtr    WhichWindow;
  25.     SysEnvRec    envir;
  26.  
  27.     MaxApplZone();
  28.  
  29.     for (i = 0; i < 16; i++)
  30.         MoreMasters();
  31.  
  32.     ShowCursor();
  33.     InitGraf((Ptr)&thePort);
  34.     ShowCursor();
  35.     InitFonts();
  36.     ShowCursor();
  37.     FlushEvents(everyEvent, 0);
  38.     ShowCursor();
  39.     InitWindows();
  40.     InitMenus();
  41.     TEInit();
  42.     InitDialogs((ProcPtr) NIL);
  43.  
  44.     haveWaitEvnt = TrapAvailable(_WaitNextEvent);
  45.     SysEnvirons(1, &envir);        /* look for at least 128K ROMS */
  46.     if (envir.machineType < 0) Fatal(eWrongROM);
  47.  
  48.     cursRgn = NewRgn();
  49.  
  50.     InitCursor();
  51.     ShowCursor();
  52.     GetWMgrPort(&WhichWindow);
  53.     SetPort(WhichWindow);
  54.     screenRect = WhichWindow->portRect;
  55.     RectRgn(cursRgn, &screenRect);
  56.     screenRect.top += MBarHeight;
  57.  
  58.     ShowCursor();
  59.  
  60.     ReadPreferences();
  61.  
  62.     GetParameters();
  63.  
  64.     ExitToShell();
  65. }
  66.  
  67.  
  68. static Boolean textFlag, folderFlag;
  69. static integer wdRefNum;
  70.  
  71.  
  72. /************************************************************************
  73.  ************************************************************************/
  74. pascal integer dlgHook(integer item, DialogPtr dial);
  75. pascal integer dlgHook(integer item, DialogPtr dial)
  76. {
  77.     switch (item)
  78.         {
  79.         case 11 :
  80.             item = 1;
  81.             folderFlag = TRUE;
  82.             break;
  83.         case 12 :
  84.             ToggleDialCtlVal(dial, item);
  85.             textFlag = GetDialCtlVal(dial, item);
  86.             item = 101;
  87.             break;
  88.         }
  89.     return item;
  90. }
  91.  
  92.  
  93. /************************************************************************
  94.  ************************************************************************/
  95. pascal Boolean fileFilter(ParmBlkPtr file);
  96. pascal Boolean fileFilter(ParmBlkPtr file)
  97. {
  98.     wdRefNum = file->fileParam.ioVRefNum;
  99.  
  100.     if (file->fileParam.ioFlAttrib & 0x10)
  101.         return TRUE;
  102.  
  103.     if (textFlag && (file->fileParam.ioFlFndrInfo.fdType != 'TEXT'))
  104.         return TRUE;
  105.  
  106.     return FALSE;    /* show this file */
  107. }
  108.  
  109.  
  110.  
  111. /************************************************************************
  112. in SegLoad.H
  113.  
  114. struct AppFile {
  115.     short vRefNum;
  116.     OSType fType;
  117.     short versNum;  //versNum in high byte
  118.     Str255 fName;
  119. };
  120.  
  121. typedef struct AppFile AppFile;
  122.  ************************************************************************/
  123. static void GetParameters()
  124. {
  125.     integer count, message, i, index = 1;
  126.     AppFile    aFile;
  127.     SFReply    reply;
  128.     Boolean    sfd = FALSE;
  129.     Point    where = { 80, 80 };
  130.     SFTypeList    types;
  131.  
  132.     CountAppFiles(&message, &count);
  133.  
  134.     if (!count)
  135.         {
  136.         textFlag = FALSE;
  137.         folderFlag = FALSE;
  138.         SFPGetFile(where,
  139.                     "\pSelect Files To Print :",
  140.                     fileFilter /* file filter */,
  141.                     0,
  142.                     types,
  143.                     dlgHook /* dlgHook */,
  144.                     &reply,
  145.                     130,
  146.                     NIL /* filterProc */ );
  147.  
  148.         sfd = TRUE;
  149.         if (!reply.good)
  150.             Fatal(eNoFiles);
  151.  
  152.         BlockMove(reply.fName, aFile.fName, reply.fName[0]+1);
  153.         aFile.vRefNum = reply.vRefNum;
  154.         aFile.fType = reply.fType;
  155.         count = 1;
  156.         }
  157.  
  158.     doAbout(init_it);
  159.  
  160.     if (!sfd)
  161.         GetAppFiles(1, &aFile);
  162.     else if (folderFlag && EnumerateFiles(reply.vRefNum, &index, &aFile) == 0)
  163.         Fatal(eNoFiles);
  164.  
  165.     if ( StartJob(aFile.fName) )    /* setup failed */
  166.         {
  167.         SysBeep(5);
  168.         ExitTCPS();
  169.         SysBeep(5);
  170.         Exit();
  171.         }
  172.  
  173.     if (!sfd)
  174.         {
  175.         for (i = 1; i <= count; i++)
  176.             {
  177.             GetAppFiles(i, &aFile);
  178.     
  179.             if ( LPR(aFile.fName, aFile.vRefNum, aFile.fType == 'TEXT') )
  180.                 {
  181.                 SysBeep(5);
  182.                 EndJob();
  183.                 return;    /* something went wrong, drop out now */
  184.                 }
  185.             }
  186.         }
  187.     else if (folderFlag)
  188.         {
  189.         index = 1;
  190.         while (EnumerateFiles(reply.vRefNum, &index, &aFile) != 0)
  191.             {
  192.             if ( LPR(aFile.fName, aFile.vRefNum, aFile.fType == 'TEXT') )
  193.                 {
  194.                 SysBeep(5);
  195.                 EndJob();
  196.                 return;    /* something went wrong, drop out now */
  197.                 }
  198.             }
  199.         }
  200.     else
  201.         {
  202.         if ( LPR(aFile.fName, aFile.vRefNum, aFile.fType == 'TEXT') )
  203.             {
  204.             SysBeep(5);
  205.             EndJob();
  206.             return;    /* something went wrong, drop out now */
  207.             }
  208.         }
  209.  
  210.     SendCtlFile();
  211.     EndJob();
  212. }
  213.  
  214. /************************************************************************
  215.  ************************************************************************/
  216. /* Used whenever a fatal error happens */
  217. static void Fatal(integer error)
  218. {
  219.     AlertUser(error);
  220.     ExitToShell();
  221. }
  222.  
  223. /************************************************************************
  224.  ************************************************************************/
  225. static StringPtr PrefsName = "\pLPR_Prefs";
  226.  
  227. /************************************************************************
  228.  ************************************************************************/
  229. void ReadPreferences()
  230. {
  231.     integer wdRef, rRef;
  232.     LongInt wdID;
  233.     integer vol;
  234.     Handle    tmpH, versH;
  235.     integer    *tmp, id;
  236.     ResType    type;
  237.  
  238.  
  239.     /* now read the prefs file */
  240.     GetWDir(&wdRef, &wdID, &vol);
  241.     SetPrefsFolder();
  242.  
  243.     /* we are in the preferences folder */
  244.     if ((rRef = OpenResFile((StringPtr)PrefsName)) == -1)
  245.         {
  246.         init_it = TRUE;
  247.         WritePreferences();
  248.         }
  249.     else
  250.         {
  251.         if (tmpH = GetResource('Pref', 0))
  252.             {
  253.             HLock(tmpH);
  254.             tmp = (integer*)(*tmpH);
  255.             job_number = tmp[0];
  256.             HUnlock(tmpH);
  257.             }
  258.         else
  259.             init_it = TRUE;
  260.         
  261.         if (tmpH = GetResource('STR ', 256))
  262.             {
  263.             HLock(tmpH);
  264.             BlockMove((StringPtr)*tmpH, my_user, GetHandleSize(tmpH));
  265.             HUnlock(tmpH);
  266.             }
  267.         else if ( tmpH = GetResource('STR ', -16096) )
  268.             {    /* get chooser name */
  269.             HLock(tmpH);
  270.             BlockMove((StringPtr)*tmpH, my_user, GetHandleSize(tmpH));
  271.             HUnlock(tmpH);
  272.             ReleaseResource(tmpH);
  273.             init_it = TRUE;
  274.             }
  275.         else
  276.             {
  277.             init_it = TRUE;
  278.             my_user[0] = 0;
  279.             }
  280.  
  281.         if (tmpH = GetResource('STR ', 257))
  282.             {
  283.             HLock(tmpH);
  284.             BlockMove((StringPtr)*tmpH, my_host, GetHandleSize(tmpH));
  285.             HUnlock(tmpH);
  286.             }
  287.         else
  288.             {
  289.             init_it = TRUE;
  290.             my_host[0] = 0;
  291.             }
  292.  
  293.         if (tmpH = GetResource('STR ', 258))
  294.             {
  295.             HLock(tmpH);
  296.             BlockMove((StringPtr)*tmpH, prt_name, GetHandleSize(tmpH));
  297.             HUnlock(tmpH);
  298.             }
  299.         else
  300.             {
  301.             init_it = TRUE;
  302.             BlockMove("\plp", prt_name, 3);
  303.             }
  304.  
  305.         if (tmpH = GetResource('STR ', 259))
  306.             {
  307.             HLock(tmpH);
  308.             BlockMove((StringPtr)*tmpH, prt_server, GetHandleSize(tmpH));
  309.             HUnlock(tmpH);
  310.             }
  311.         else
  312.             {
  313.             init_it = TRUE;
  314.             prt_server[0] = 0;
  315.             }
  316.  
  317.         CloseResFile(rRef);
  318.         WritePreferences();    /* updated values */
  319.         }
  320.  
  321.     SetWDir(wdRef, wdID, vol);
  322.  
  323. }
  324.  
  325.  
  326. /************************************************************************
  327.  ************************************************************************/
  328. void WritePreferences()
  329. {
  330.     integer wdRef, rRef;
  331.     LongInt wdID;
  332.     integer vol;
  333.     Handle    tmpH, versH;
  334.     integer    *tmp, id;
  335.  
  336.     /* now read the prefs file */
  337.     GetWDir(&wdRef, &wdID, &vol);
  338.     SetPrefsFolder();
  339.  
  340.     /* we are in the preferences folder */
  341.     if ((rRef = OpenResFile((StringPtr)PrefsName)) == -1)
  342.         {
  343.         Create((StringPtr)PrefsName, 0, 'Lpr ', 'Pref');
  344.         CreateResFile((StringPtr)PrefsName);
  345.         if ( (rRef = OpenResFile((StringPtr)PrefsName)) != -1 )
  346.             AddResource(NewHandle(0), 'Pref', 0, NIL);
  347.         }
  348.     if (rRef != -1)
  349.         {
  350.         tmpH = GetResource('Pref', 0);    /* the screen prefs */
  351.         if (!tmpH)
  352.             AddResource(tmpH = NewHandle(0), 'Pref', 0, NIL);
  353.         if (tmpH)
  354.             {
  355.             if ( GetHandleSize(tmpH) < (2*sizeof(integer)) )
  356.                 SetHandleSize(tmpH, 2*sizeof(integer));
  357.             HLock(tmpH);
  358.             tmp = (integer*)(*tmpH);
  359.             tmp[0] = job_number+1;
  360.             HUnlock(tmpH);
  361.             ChangedResource(tmpH);
  362.             WriteResource(tmpH);
  363.             }
  364.  
  365.         tmpH = GetResource('STR ', 256);
  366.         if (!tmpH)
  367.             AddResource(tmpH = NewHandle(0), 'STR ', 256, NIL);
  368.         if (tmpH)
  369.             {
  370.             SetHandleSize(tmpH, my_user[0]+1);
  371.             HLock(tmpH);
  372.             BlockMove(my_user, *tmpH, my_user[0]+1);
  373.             HUnlock(tmpH);
  374.             ChangedResource(tmpH);
  375.             WriteResource(tmpH);
  376.             }
  377.  
  378.         tmpH = GetResource('STR ', 257);
  379.         if (!tmpH)
  380.             AddResource(tmpH = NewHandle(0), 'STR ', 257, NIL);
  381.         if (tmpH)
  382.             {
  383.             SetHandleSize(tmpH, my_host[0]+1);
  384.             HLock(tmpH);
  385.             BlockMove(my_host, *tmpH, my_host[0]+1);
  386.             HUnlock(tmpH);
  387.             ChangedResource(tmpH);
  388.             WriteResource(tmpH);
  389.             }
  390.  
  391.         tmpH = GetResource('STR ', 258);
  392.         if (!tmpH)
  393.             AddResource(tmpH = NewHandle(0), 'STR ', 258, NIL);
  394.         if (tmpH)
  395.             {
  396.             SetHandleSize(tmpH, prt_name[0]+1);
  397.             HLock(tmpH);
  398.             BlockMove(prt_name, *tmpH, prt_name[0]+1);
  399.             HUnlock(tmpH);
  400.             ChangedResource(tmpH);
  401.             WriteResource(tmpH);
  402.             }
  403.  
  404.         tmpH = GetResource('STR ', 259);
  405.         if (!tmpH)
  406.             AddResource(tmpH = NewHandle(0), 'STR ', 259, NIL);
  407.         if (tmpH)
  408.             {
  409.             SetHandleSize(tmpH, prt_server[0]+1);
  410.             HLock(tmpH);
  411.             BlockMove(prt_server, *tmpH, prt_server[0]+1);
  412.             HUnlock(tmpH);
  413.             ChangedResource(tmpH);
  414.             WriteResource(tmpH);
  415.             }
  416.  
  417.         CloseResFile(rRef);
  418.         }
  419.  
  420.     SetWDir(wdRef, wdID, vol);
  421. }
  422.  
  423. /************************************************************************
  424.  ************************************************************************/
  425. integer EnumerateFiles(integer wdRef, integer *anIndex, AppFile *file)
  426. {
  427.     CInfoPBRec        myCIPB;
  428.     integer            retVal = FALSE, osVal, fRef,
  429.                     index = 1;
  430.  
  431.     if (anIndex) index = *anIndex;
  432.  
  433.     myCIPB.dirInfo.ioVRefNum = wdRef;
  434.     myCIPB.dirInfo.ioNamePtr = file->fName;
  435.     myCIPB.dirInfo.ioFDirIndex = index++;
  436.     myCIPB.dirInfo.ioDrDirID = 0;
  437.     file->fName[0]=0;
  438.  
  439.                     /* get info about object with this name */
  440.     while ( ! PBGetCatInfo(&myCIPB, FALSE) )
  441.         {
  442.         if ( !(myCIPB.dirInfo.ioFlAttrib & 0x10) )
  443.             {                                /* it's not a directory    */
  444.             if (anIndex) *anIndex = index;
  445.             file->vRefNum = wdRef;
  446.             file->fType = myCIPB.hFileInfo.ioFlFndrInfo.fdType;
  447.             file->versNum = myCIPB.hFileInfo.ioFVersNum << 8;  /*versNum in high byte*/
  448.  
  449.             if (textFlag)
  450.                 {
  451.                 if (myCIPB.hFileInfo.ioFlFndrInfo.fdType == 'TEXT')
  452.                     return -1;
  453.                 }
  454.             else
  455.                 return -1;
  456.             }
  457.  
  458.         myCIPB.dirInfo.ioFDirIndex = index++;
  459.         myCIPB.dirInfo.ioDrDirID = 0;
  460.         file->fName[0] = 0;
  461.         }
  462.  
  463.     return 0;
  464. }
  465.