home *** CD-ROM | disk | FTP | other *** search
/ Amiga ISO Collection / AmigaUtilCD2.iso / Misc / X!-SS12B.LHA / StartupSelector1.2b / Source / StartupSelector.e
Encoding:
Text File  |  1997-03-09  |  11.2 KB  |  417 lines

  1. /*     Startup  Selector     */
  2. /*   by OLIVERES Jean-Marc   */
  3. /* (c) 1996 Moonchild  Prod. */
  4. /*          01.02.97         */
  5.  
  6. OPT REG=3
  7.  
  8. MODULE 'dos/dos','dos/dostags','dos/dosasl','intuition/intuition',
  9.              'intuition/screens','gadtools','libraries/gadtools','reqtools',
  10.              'libraries/reqtools','exec/nodes','exec/lists','amigalib/lists'
  11.  
  12. ENUM ER_NONE,ER_WIN,ER_DIR,ER_NODIR,ER_SCR,ER_MOUSE,ER_EXAM,ER_REQ,ER_GAD,
  13.          ER_FILE,ER_MEM
  14. ENUM WBS=1,USER,LVID,RMB,WBSP
  15.  
  16. RAISE ER_WIN   IF OpenWindowTagList()=NIL,
  17.             ER_SCR   IF LockPubScreen()=NIL,
  18.             ER_MOUSE IF Mouse()<>1,
  19.             ER_MEM   IF New()=NIL
  20.  
  21. CONST BIGGER=-1,SMALLER=1
  22.  
  23. DEF ptrwin=NIL:PTR TO window,glist=NIL
  24. DEF scr=NIL:PTR TO screen,visual,menu
  25. DEF lv_width,lv_heigth,fic_nbr=0,fic_lng=0
  26. DEF info:fileinfoblock,dirscan,dirlock
  27. DEF ch[70]:STRING,count1=0,count2=0,count3=0
  28. DEF choice1,choice2,choice3
  29. DEF list=NIL:PTR TO lh,node=NIL:PTR TO ln
  30. DEF f_hdl,nodename,wbsplus,wbsp_path
  31.  
  32. PROC main() HANDLE
  33.     DEF mes=NIL:PTR TO intuimessage
  34.     DEF id,gad=NIL:PTR TO gadget,i,userdata,idcmp,item
  35.     DEF seconds=0,micros,seconds2,micros2,sel
  36.     init()
  37.     choicerep()
  38.     nodename:=readlog()
  39.     Mouse()
  40.     scr:=LockPubScreen(NIL)
  41.     visual:=GetVisualInfoA(scr,NIL)
  42.     wbsp_path:='SYS:prefs/WBStartup+Prefs'
  43.     wbsplusprefs()
  44.     IF (scanstartupdir()=0) THEN nofile()
  45.     addstartgadget()
  46.     window()
  47.     createmen()
  48.     REPEAT
  49.         IF mes:=Gt_GetIMsg(ptrwin.userport)
  50.             idcmp:=mes.class
  51.             SELECT idcmp
  52.             CASE IDCMP_MENUPICK
  53.                 IF (item:=ItemAddress(menu,mes.code))<>NIL
  54.                     id:=Long(item+34)
  55.                     IF id=1 THEN req('StartupSelector\n\nVersion 1.2a\n\nCopyright © 1996-97\n\n'+
  56.                                                      '03.03.97\n\nMoonchild Prod.')
  57.                     IF id=2 THEN SystemTagList('NewCli',NIL)
  58.                     IF id=3
  59.                         IF ptrwin THEN CloseW(ptrwin)
  60.                         Gt_ReplyIMsg(mes)
  61.                         quit()
  62.                     ENDIF
  63.                 ENDIF
  64.             CASE IDCMP_GADGETUP
  65.                 gad:=mes.iaddress
  66.                 userdata:=gad.userdata
  67.                 SELECT userdata
  68.                 CASE USER
  69.                     count1:=Not(count1)
  70.                 CASE WBS
  71.                     count2:=Not(count2)
  72.                 CASE RMB
  73.                     count3:=Not(count3)
  74.                 CASE WBSP
  75.                     SystemTagList(wbsp_path,NIL)
  76.                 CASE LVID
  77.                     CurrentTime({seconds2},{micros2})
  78.                     node:=list.head
  79.                     FOR i:=1 TO mes.code DO node:=node.succ 
  80.                     IF seconds=0
  81.                         CurrentTime({seconds},{micros})
  82.                         sel:=i
  83.                     ELSEIF DoubleClick(seconds,micros,seconds2,micros2) AND (sel=i)
  84.                         setlog(node.name)
  85.                         Gt_ReplyIMsg(mes)
  86.                         launchstart(node.name)
  87.                     ELSE
  88.                         seconds:=0
  89.                         CurrentTime({seconds},{micros})
  90.                         sel:=i
  91.                     ENDIF
  92.                 ENDSELECT
  93.             ENDSELECT
  94.             Gt_ReplyIMsg(mes)
  95.         ELSE
  96.             WaitPort(ptrwin.userport)
  97.         ENDIF
  98.     UNTIL idcmp=IDCMP_CLOSEWINDOW
  99.     setlog(nodename)
  100.     launchstart(nodename)
  101. EXCEPT
  102.     SELECT exception
  103.         CASE ER_WIN          ;    req('Unable to open window !')
  104.         CASE ER_DIR          ;    req('Can''t find your directory !')
  105.         CASE ER_NODIR      ;    req('Not a directory !')
  106.         CASE ER_SCR          ;    req('Unable to lock Workbench screen !')
  107.         CASE ER_GAD     ; req('Can''t open the Gadtools.library !')
  108.         CASE ER_MEM     ; req('Not enough memory !')
  109.         CASE ER_MOUSE
  110.             IF count3
  111.                 launchstart(nodename)
  112.             ELSE
  113.                 StrCopy(ch,dirscan,ALL)
  114.                 AddPart(ch,'Startup-Sequence',70)
  115.                 IF (dirlock:=Lock(ch,ACCESS_READ))=NIL
  116.                     req('No Startup-Sequence !\nPress ''OK'' to load the Workbench')
  117.                     SystemTagList({lwb},NIL)    
  118.                     quit()
  119.                 ENDIF
  120.             ENDIF
  121.             launchstart('Startup-Sequence')
  122.     ENDSELECT
  123.     quit()
  124. ENDPROC
  125.  
  126. PROC setlog(nodename)
  127.     IF count3
  128.         IF count1 THEN choice1:='Y' ELSE choice1:='N'
  129.         IF count2 THEN choice2:='Y' ELSE choice2:='N'
  130.             writelog(choice1,choice2,'Y',nodename)
  131.         ELSE
  132.             writelog('N','N','N',nodename)
  133.         ENDIF
  134. ENDPROC
  135.  
  136. PROC init()
  137.     VOID '$VER:Startup Selector 1.2a (02.03.97) Moonchild Prod.'
  138.     AssignPath('ENV','ENVARC:')
  139.     reqtoolsbase:=OpenLibrary('reqtools.library',37)
  140.     IF (gadtoolsbase:=OpenLibrary('gadtools.library',37))=NIL THEN Raise(ER_GAD)
  141.     Rename({wbsold},{wbs})
  142.     Rename({wbsoldinfo},{wbsinfo})
  143.     Rename({userold},{user})
  144. ENDPROC
  145.  
  146. PROC writelog(choice1,choice2,choice3,node)
  147.     f_hdl:=Open({sslog},NEWFILE)
  148.     Write(f_hdl,choice1,StrLen(choice1)+1)
  149.     Write(f_hdl,choice2,StrLen(choice2)+1)
  150.     Write(f_hdl,choice3,StrLen(choice3)+1)
  151.     Write(f_hdl,node,StrLen(node)+1)
  152.     Close(f_hdl)
  153. ENDPROC
  154.  
  155. PROC readlog()
  156.     DEF log,f_len
  157.     log:={sslog}
  158.     f_len:=FileLength(log)
  159.     IF f_hdl:=Open(log,OLDFILE)
  160.         choice1:=New(f_len)
  161.         Read(f_hdl,choice1,f_len)
  162.         choice2:=choice1+(StrLen(choice1)+1)
  163.         choice3:=choice1+(StrLen(choice1)+1)+(StrLen(choice2)+1)
  164.         nodename:=choice1+(StrLen(choice1)+1)+(StrLen(choice2)+1)+(StrLen(choice3)+1)
  165.         Close(f_hdl)
  166.         IF OstrCmp(choice1,'Y')=0
  167.             count1:=TRUE
  168.         ENDIF
  169.         IF OstrCmp(choice2,'Y')=0
  170.             count2:=TRUE
  171.         ENDIF
  172.         IF OstrCmp(choice3,'Y')=0
  173.             count3:=TRUE
  174.         ENDIF
  175.     ELSE
  176.         req('''S:startupselector_log'' not found !\nCreating default _log ...\n'+
  177.                 'And starting with it ...')
  178.         writelog('N','N','N','Startup-Sequence')
  179.     ENDIF
  180. ENDPROC nodename
  181.  
  182. PROC req(msg)
  183.     IF reqtoolsbase
  184.         RtEZRequestA(msg,'OK',0,0,[RTEZ_FLAGS ,EZREQF_CENTERTEXT,
  185.                                                             RT_REQPOS   ,REQPOS_CENTERSCR,
  186.                                                             NIL])
  187.     ELSE
  188.         EasyRequestArgs(NIL,[20,0,'Information...',msg,'OK'],0,NIL)
  189.     ENDIF
  190. ENDPROC
  191.  
  192. PROC choicerep()
  193.     DEF myargs:PTR TO LONG,rdargs
  194.      myargs:=[0]
  195.      rdargs:=ReadArgs('PATH/O',myargs,NIL)
  196.     IF myargs[]=0
  197.         dirscan:='S:start/'
  198.     ELSE
  199.         dirscan:=String(StrLen(myargs[0]))
  200.         StrCopy(dirscan,myargs[0])
  201.     ENDIF
  202.     IF rdargs THEN FreeArgs(rdargs)
  203. ENDPROC
  204.  
  205. PROC scanstartupdir()
  206.  
  207. /* faire un tri en fonction de *.info et *.bak. Tant qu'aucune de ces
  208. possibilités n'est trouvée on envoie à la procedure getstartupname()
  209. sinon on continue à tester */
  210.  
  211.     DEF result,p_anchor:PTR TO anchorpath,olddir
  212.     p_anchor:=New(SIZEOF anchorpath)
  213.     IF (dirlock:=Lock(dirscan,ACCESS_READ))=NIL THEN Raise(ER_NODIR)
  214.     olddir:=CurrentDir(dirlock)
  215.     result:=MatchFirst('~(#?.info|#?.bak)',p_anchor)
  216.     NEW list
  217.     newList(list)
  218.     WHILE result=0
  219.         INC fic_nbr
  220.         info:=p_anchor.info
  221.         getstartupname(info.filename)
  222.         result:=MatchNext(p_anchor)
  223.     ENDWHILE
  224.     MatchEnd(p_anchor)
  225.     CurrentDir(olddir)
  226. ENDPROC fic_nbr
  227.  
  228. PROC nofile()
  229.     req('No script in the directory !\nPress ''OK'' to load the Workbench')
  230.     SystemTagList({lwb},NIL)    
  231.     quit()
  232. ENDPROC
  233.  
  234. PROC getstartupname(infofilename)
  235.     DEF fic_chaine,length
  236.     DEF fic_chaineUp[30]:STRING,fic_preUp[30]:STRING,fic_finUp[30]:STRING
  237.     DEF newnode:PTR TO ln
  238.  
  239.     length:=StrLen(infofilename)
  240.     fic_chaine:=String(length)
  241.     StrCopy(fic_chaine,infofilename)
  242.     IF length>fic_lng THEN fic_lng:=length
  243.  
  244.     NEW newnode
  245.     newnode.name:=fic_chaine
  246.  
  247.     StrCopy(fic_chaineUp,fic_chaine)
  248.     UpperStr(fic_chaineUp)
  249.     IF fic_nbr>1
  250.         StrCopy(fic_preUp,list.head.name)
  251.         UpperStr(fic_preUp)
  252.     ENDIF
  253.     IF fic_nbr>2
  254.         StrCopy(fic_finUp,list.tailpred.name)
  255.         UpperStr(fic_finUp)
  256.     ENDIF
  257.  
  258.     IF fic_nbr=1
  259.         AddHead(list,newnode)
  260.     ELSEIF OstrCmp(fic_preUp,fic_chaineUp)=BIGGER
  261.         AddHead(list,newnode)
  262.     ELSEIF OstrCmp(fic_finUp,fic_chaineUp)=SMALLER
  263.         AddTail(list,newnode)
  264.     ELSE
  265.         node:=list.head
  266.         WHILE (node:=node.succ)<>NIL
  267.             StrCopy(fic_finUp,node.name)
  268.             UpperStr(fic_finUp)
  269.             IF OstrCmp(fic_finUp,fic_chaineUp)=BIGGER
  270.                 Insert(list,newnode,node.pred)
  271.                 RETURN
  272.             ENDIF
  273.         ENDWHILE
  274.     ENDIF
  275. ENDPROC
  276.  
  277. PROC addstartgadget()
  278.     DEF gadget,wbspname,wbsplen
  279.     gadget:=CreateContext({glist})
  280.     wbspname:='Call WBStartup+ ?'
  281.     wbsplen:=StrLen(wbspname)*8+8
  282.     lv_width:=fic_lng*8+28
  283.     IF fic_nbr<8
  284.         lv_heigth:=8*8+4
  285.     ELSEIF fic_nbr>29
  286.         lv_heigth:=29*8+4
  287.     ELSE
  288.         lv_heigth:=fic_nbr*8+4
  289.     ENDIF
  290.     gadget:=CreateGadgetA(LISTVIEW_KIND,gadget,
  291.                     [0,0,lv_width,lv_heigth,0,0,0,0,visual,LVID]:newgadget,
  292.                     [GTLV_LABELS,list,GTLV_SELECTED,TRUE,NIL])
  293.     gadget:=CreateGadgetA(CHECKBOX_KIND,gadget,
  294.                     [lv_width+2,0,12,12,0,0,0,0,visual,USER]:newgadget,
  295.                     [GTCB_CHECKED,count1,NIL])
  296.     gadget:=CreateGadgetA(CHECKBOX_KIND,gadget,
  297.           [lv_width+2,12,12,12,0,0,0,0,visual,WBS]:newgadget,
  298.                     [GTCB_CHECKED,count2,NIL])
  299.     gadget:=CreateGadgetA(CHECKBOX_KIND,gadget,
  300.           [lv_width+2,24,12,12,0,0,0,0,visual,RMB]:newgadget,
  301.                     [GTCB_CHECKED,count3,NIL])
  302.     IF wbsplus
  303.         gadget:=CreateGadgetA(BUTTON_KIND,gadget,
  304.                         [((213-wbsplen)/2)+lv_width,lv_heigth-13,wbsplen,12,
  305.                         wbspname,0,0,0,visual,WBSP]:newgadget,NIL)
  306.     ENDIF
  307. ENDPROC
  308.  
  309. PROC window()
  310.     DEF widcmp,wflags,rport,beveltags
  311.     DEF w_width,w_left,w_top
  312.     DEF nodenamelen
  313.     nodenamelen:=StrLen(nodename)*8+8
  314.     w_width:=lv_width+213
  315.     w_left:=(scr.width-w_width)/2
  316.     w_top:=(scr.height-lv_heigth)/2
  317.     beveltags:=[GT_VISUALINFO,visual,GTBB_FRAMETYPE,BBFT_BUTTON,NIL]
  318.     widcmp:=IDCMP_CLOSEWINDOW OR IDCMP_GADGETUP OR IDCMP_MENUPICK OR LISTVIEWIDCMP
  319.     wflags:=WFLG_CLOSEGADGET+WFLG_DRAGBAR+WFLG_GIMMEZEROZERO+WFLG_NEWLOOKMENUS
  320.     ptrwin:=OpenWindowTagList(NIL,[WA_TITLE        ,'Startup-Selector 1.2a',
  321.                                  WA_GADGETS      ,glist,
  322.                                  WA_LEFT         ,w_left,
  323.                                  WA_TOP          ,w_top,
  324.                                  WA_INNERWIDTH   ,w_width,
  325.                                  WA_INNERHEIGHT  ,lv_heigth,
  326.                                  WA_IDCMP        ,widcmp,
  327.                                  WA_FLAGS        ,wflags,
  328.                                                                  WA_AUTOADJUST   ,-1,
  329.                                  WA_ACTIVATE     ,-1,
  330.                                  NIL])
  331.     Gt_RefreshWindow(ptrwin,NIL)
  332.     SetStdRast(ptrwin.rport)
  333.     rport:=ptrwin.rport
  334.     SetAPen(rport,2)
  335.     TextF(lv_width+32,8,'Disable User-Startup ?')
  336.     TextF(lv_width+32,20,'Disable WBStartup ?')
  337.     TextF(lv_width+32,32,'Save settings ?')
  338.     DrawBevelBoxA(rport,lv_width+29,0,184,11,beveltags)
  339.     DrawBevelBoxA(rport,lv_width+29,12,160,11,beveltags)
  340.     DrawBevelBoxA(rport,lv_width+29,24,128,11,beveltags)
  341.     IF wbsplus
  342.         TextF((213-nodenamelen)/2+lv_width,(lv_heigth-13-35)/2+35,nodename)
  343.     ELSE
  344.         TextF((213-nodenamelen)/2+lv_width,(lv_heigth-35)/2+35,nodename)
  345.     ENDIF
  346. ENDPROC
  347.  
  348. PROC createmen()
  349.   menu:=CreateMenusA([1,0,'Projet',0,0,0,0,
  350.                       2,0,'About',0,0,0,1,
  351.                                             2,0,NM_BARLABEL,0,0,0,0,
  352.                                             2,0,'NewCli',0,0,0,2,
  353.                                             2,0,NM_BARLABEL,0,0,0,0,
  354.                                             2,0,'Quit',0,0,0,3,
  355.                       0,0,0,0,0,0,0]:newmenu,[GTMN_FRONTPEN,1,
  356.                                                                                             GTMN_NEWLOOKMENUS,TRUE,
  357.                                                                                             NIL])
  358.   LayoutMenusA(menu,visual,NIL)
  359.   SetMenuStrip(ptrwin,menu)
  360. ENDPROC
  361.  
  362. PROC launchstart(file)
  363.     DEF launch,ch2[108]:STRING
  364.     IF count1
  365.         IF (Rename({user},{userold}))=NIL
  366.             req('Can''t rename User-Startup !')
  367.             RETURN
  368.         ENDIF
  369.     ENDIF
  370.     IF count2
  371.         IF (Rename({wbs},{wbsold}))=NIL OR (Rename({wbsinfo},{wbsoldinfo}))=NIL
  372.             req('Can''t rename WBStartup or WBStartup.info !')
  373.         ENDIF
  374.     ENDIF
  375.     StrCopy(ch2,'C:EXECUTE ',ALL)
  376.     StrCopy(ch,dirscan,ALL)
  377.     AddPart(ch,file,70)
  378.     StringF(ch,'"\s"',ch)
  379.     StrAdd(ch2,ch,ALL)
  380.     StrCopy(ch,ch2,ALL)
  381.     IF scr THEN UnlockPubScreen(NIL,scr)
  382.     IF ptrwin THEN CloseW(ptrwin)
  383.     IF (launch:=SystemTagList(ch,NIL))=TRUE
  384.         req('Can''t execute this script !\nPlease try another one ...')
  385.         RETURN
  386.     ENDIF
  387.     quit()
  388. ENDPROC
  389.  
  390. PROC wbsplusprefs()
  391.     IF dirlock:=Lock(wbsp_path,ACCESS_READ)
  392.         wbsplus:=TRUE
  393.         UnLock(dirlock)
  394.     ENDIF    
  395. ENDPROC
  396.  
  397. PROC quit()
  398.     IF menu THEN FreeMenus(menu)
  399.     IF dirlock THEN UnLock(dirlock)
  400.     IF scr THEN UnlockPubScreen(NIL,scr)
  401.     IF ptrwin THEN ClearMenuStrip(ptrwin)
  402.     IF visual THEN FreeVisualInfo(visual)
  403.     IF gadtoolsbase THEN CloseLibrary(gadtoolsbase)
  404.     IF reqtoolsbase THEN CloseLibrary(reqtoolsbase)
  405.     OpenWorkBench()
  406.     CleanUp(0)
  407. ENDPROC
  408.  
  409. wbs: CHAR 'SYS:WBStartup',0
  410. wbsinfo: CHAR 'SYS:WBStartup.info',0
  411. wbsold: CHAR 'SYS:WBStartupOld',0
  412. wbsoldinfo: CHAR 'SYS:WBStartupOld.info',0
  413. user: CHAR 'S:User-Startup',0
  414. userold: CHAR 'S:User-StartupOld',0
  415. lwb: CHAR 'C:LoadWB',0
  416. sslog: CHAR 'S:startupselector_log',0
  417.