home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 10 Tools / 10-Tools.zip / vclassrc.zip / install.c next >
Text File  |  1999-01-03  |  10KB  |  322 lines

  1. /*  <Install, part of the VClassed package, a PM OS/2 WorkPlace Shell class manager>
  2.     Copyright (C) 1996, 1997, 1998  Daniele Vistalli
  3.  
  4.     This program is free software; you can redistribute it and/or modify
  5.     it under the terms of the GNU General Public License as published by
  6.     the Free Software Foundation; either version 2 of the License, or
  7.     (at your option) any later version.
  8.  
  9.     This program is distributed in the hope that it will be useful,
  10.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.     GNU General Public License for more details.
  13.  
  14.     You should have received a copy of the GNU General Public License
  15.     along with this program; if not, write to the Free Software
  16.     Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  17.  
  18.  
  19.     You can contact me :
  20.     Daniele Vistalli :
  21.             dvistalli@tin.it
  22.         virusface@usa.net
  23.  
  24. */
  25. #define INCL_WIN
  26. #define INCL_PM
  27. #define INCL_DOSPROCESS
  28. #define INCL_DOSSESMGR
  29. #define INCL_DOSERRORS
  30.  
  31. #define HF_STDOUT 1
  32.  
  33. #include <os2.h>
  34. #include <stdlib.h>
  35. #include <stdio.h>
  36. #include <string.h>
  37.  
  38. #include "install.h"
  39.  
  40. static BOOL installed = FALSE, installing = FALSE; // Flags to determine program state
  41. static HFILE UnpackPipe; // The pipe from where to read the unpack output
  42. static char programpath[255]; // The execution path
  43.  
  44. void setButtons(HWND wnd)
  45. {
  46.    if (installing)
  47.     { WinEnableWindow(WinWindowFromID(wnd,BTN_EXIT),FALSE);
  48.       WinEnableWindow(WinWindowFromID(wnd,BTN_INSTALL),FALSE); }
  49.    else
  50.     { WinEnableWindow(WinWindowFromID(wnd,BTN_EXIT),TRUE);
  51.       WinEnableWindow(WinWindowFromID(wnd,BTN_INSTALL),TRUE);
  52.     }
  53.  
  54.    if (installed)
  55.     { WinEnableWindow(WinWindowFromID(wnd,BTN_EXIT),TRUE);
  56.       WinEnableWindow(WinWindowFromID(wnd,BTN_INSTALL),FALSE); }
  57.    else
  58.     { WinEnableWindow(WinWindowFromID(wnd,BTN_EXIT),TRUE);
  59.       WinEnableWindow(WinWindowFromID(wnd,BTN_INSTALL),TRUE);
  60.     }
  61. }
  62.  
  63. int PathCreate(const char aPath[])
  64. { char tmp[240];
  65.   char *c;
  66.  
  67.   strcpy(tmp,aPath);
  68.  
  69.   if (DosSetCurrentDir(tmp) != NO_ERROR)
  70.    { c = strrchr(tmp,'\\');
  71.      if (*(c-1) != ':') *c = 0;
  72.      else *(c+1) = 0;
  73.      if (!PathCreate(tmp)) return 0;
  74.      if (DosCreateDir(aPath,NULL) != NO_ERROR) return 0;
  75.    }
  76.  
  77.   return 1;
  78. }
  79.  
  80. VOID _System UnpackReader(ULONG parm)
  81. {
  82.    HAB rhab;
  83.    HMQ mhmq;
  84.    CHAR str[100], c, dbg[10];
  85.    int cnt = 0;
  86.    ULONG ulRead;
  87.    int endcnt = 0;
  88.  
  89.    rhab = WinInitialize(0);
  90.    mhmq = WinCreateMsgQueue(rhab,0);
  91.  
  92.    str[0] = 0;
  93.    c = 0;
  94.  
  95.    do { DosRead(UnpackPipe,
  96.                  &c,
  97.                  1,
  98.                  &ulRead);
  99.         if (c != 13) str[cnt++] = c;
  100.         else
  101.           { if (str[0] == 32) endcnt++;
  102.             WinInsertLboxItem(WinWindowFromID((HWND)parm,LB_MSGLIST),LIT_END,str);
  103.             cnt = 0;
  104.             memset(str,0,100);
  105.             DosRead(UnpackPipe,&c,1,&ulRead);
  106.           }
  107.      } while (endcnt != 2);
  108.  
  109.   DosClose(UnpackPipe);
  110.  
  111.   WinDestroyMsgQueue(mhmq);
  112.   WinTerminate(rhab);
  113.   DosExit(0,0);
  114. }
  115.  
  116. VOID _System InstallThread(ULONG parm) // Parm is the dialog handle
  117. {
  118.   HPIPE hReadPipe;
  119.   HFILE hW, hW1;
  120.   CHAR szFailName[CCHMAXPATH], szArg[255], destdir[200];
  121.   ULONG ulSessID    = 0;          /* Session ID returned          */
  122.   PID pid = 0;
  123.   TID tid;
  124.   STARTDATA SData       = {0};
  125.  
  126.   HFILE hfSave = -1, hfNew = HF_STDOUT;
  127.  
  128.   HAB rhab;
  129.   HMQ mhmq;
  130.  
  131.   rhab = WinInitialize(0);
  132.   mhmq = WinCreateMsgQueue(rhab,0);
  133.  
  134.   WinQueryDlgItemText((HWND)parm,IF_DEST,200,destdir); // Extract the destination path
  135.  
  136.   if (strlen(destdir) == 0)
  137.    { WinMessageBox(HWND_DESKTOP,(HWND)parm,"You must provide a destination directory","Attention",0,MB_OK | MB_ICONQUESTION | MB_MOVEABLE);
  138.      installing = FALSE;
  139.      installed = FALSE;
  140.      setButtons((HWND)parm);
  141.      WinDestroyMsgQueue(mhmq);
  142.      WinTerminate(rhab);
  143.      DosExit(0,0);
  144.    }
  145.  
  146.   if (destdir[strlen(destdir)-1] == '\\') destdir[strlen(destdir)-1] = 0;
  147.  
  148.   if (DosSetCurrentDir(destdir) != NO_ERROR)
  149.     {
  150.        if (WinMessageBox(HWND_DESKTOP,(HWND)parm,"Destination directory doesn't exist, should I create it ?","Attention",0,MB_YESNO | MB_ICONQUESTION | MB_MOVEABLE) != MBID_YES)
  151.         {
  152.           installing = FALSE;
  153.           installed = FALSE;
  154.           setButtons((HWND)parm);
  155.           WinDestroyMsgQueue(mhmq);
  156.           WinTerminate(rhab);
  157.           DosExit(0,0);
  158.         }
  159.        strcpy(szArg,destdir);
  160.  
  161.        if (!PathCreate(szArg))
  162.         {
  163.           WinMessageBox(HWND_DESKTOP,(HWND)parm,"I've got an error while creating the destination directory","Error",0,MB_OK | MB_ICONASTERISK | MB_MOVEABLE);
  164.           installing = FALSE;
  165.           installed = FALSE;
  166.           setButtons((HWND)parm);
  167.           WinDestroyMsgQueue(mhmq);
  168.           WinTerminate(rhab);
  169.           DosExit(0,0);
  170.         }
  171.  
  172.        strcpy(szArg,"Created destination directory ...");
  173.        WinInsertLboxItem(WinWindowFromID((HWND)parm,LB_MSGLIST),LIT_END,szArg);
  174.     }
  175.  
  176.   DosDupHandle(HF_STDOUT,&hfSave);
  177.   DosCreatePipe( &hReadPipe, &hW1, 0 ) ;
  178.   hW = 1;
  179.   DosDupHandle( hW1, &hW ) ;
  180.  
  181.   memset(szArg,0,sizeof(szArg)); // Create the parameters string
  182.   strcat(szArg,programpath);
  183.   strcat(szArg,"VCLASSED.PAK \"");
  184.   strcat(szArg,destdir);
  185.   strcat(szArg,"\"");
  186.  
  187.   SData.Length  = sizeof(STARTDATA);
  188.   SData.Related = SSF_RELATED_CHILD; /* start an independent session */
  189.   SData.FgBg    = SSF_FGBG_BACK;           /* start session in foreground  */
  190.   SData.TraceOpt = SSF_TRACEOPT_NONE;      /* No trace                     */
  191.              /* Start an OS/2 session using "CMD.EXE /K" */
  192.   SData.PgmTitle = "";
  193.   SData.PgmName = "UNPACK.EXE";
  194.   SData.PgmInputs = szArg;                     /* Keep session up           */
  195.  
  196.   SData.TermQ = 0;                            /* No termination queue      */
  197.   SData.Environment = 0;                      /* No environment string     */
  198.   SData.InheritOpt = SSF_INHERTOPT_PARENT;     /* Inherit shell's environ.  */
  199.   SData.SessionType = SSF_TYPE_DEFAULT; /* Windowed VIO session      */
  200.   SData.IconFile = 0;                         /* No icon association       */
  201.   SData.PgmHandle = 0;
  202.             /* Open the session VISIBLE and MAXIMIZED */
  203.   SData.PgmControl = SSF_CONTROL_INVISIBLE;
  204.   SData.Reserved = 0;
  205.   SData.ObjectBuffer  = szFailName; /* Contains info if DosExecPgm fails */
  206.   SData.ObjectBuffLen = CCHMAXPATH;
  207.  
  208.   UnpackPipe = hReadPipe;
  209.  
  210.   DosCreateThread(&tid,UnpackReader,parm,CREATE_READY,9000L);
  211.  
  212.   DosStartSession(&SData, &ulSessID, &pid);  /* Start the session */
  213.  
  214.   DosDupHandle(hfSave,&hfNew);
  215.  
  216.   DosWaitThread(&tid,DCWW_WAIT);
  217.  
  218.   if (SHORT1FROMMR(WinSendMsg(WinWindowFromID((HWND)parm,CB_CREATEOBJECTS),BM_QUERYCHECK,0,0)) == 1)
  219.    {
  220.      memset(szArg,0,sizeof(szArg));
  221.      strcat(szArg,"ICONFILE=");
  222.      strcat(szArg,destdir);
  223.      strcat(szArg,"\\VCLASSED.ICO;");
  224.      strcat(szArg,"OBJECTID=<VCLASSED>");
  225.      WinCreateObject("WPFolder","VClassed 1.6",szArg,"<WP_DESKTOP>",CO_REPLACEIFEXISTS);
  226.      WinInsertLboxItem(WinWindowFromID((HWND)parm,LB_MSGLIST),LIT_END,"Created : VClassed's Folder");
  227.  
  228.      memset(szArg,0,sizeof(szArg));
  229.      strcat(szArg,"EXENAME=");
  230.      strcat(szArg,destdir);
  231.      strcat(szArg,"\\VCLASSED.EXE;PROGTYPE=PM");
  232.      WinCreateObject("WPProgram","VClassed",szArg,"<VCLASSED>",CO_REPLACEIFEXISTS);
  233.      WinInsertLboxItem(WinWindowFromID((HWND)parm,LB_MSGLIST),LIT_END,"Created : VClassed's Program Object");
  234.  
  235.      memset(szArg,0,sizeof(szArg));
  236.      strcat(szArg,"SHADOWID=");
  237.      strcat(szArg,destdir);
  238.      strcat(szArg,"\\README.TXT");
  239.      WinCreateObject("WPShadow","VClassed's Readme",szArg,"<VCLASSED>",CO_REPLACEIFEXISTS);
  240.      WinInsertLboxItem(WinWindowFromID((HWND)parm,LB_MSGLIST),LIT_END,"Created : VClassed's Readme");
  241.    }
  242.  
  243.   installing = FALSE;
  244.   installed = TRUE;
  245.  
  246.   setButtons((HWND)parm);
  247.  
  248.   WinDestroyMsgQueue(mhmq);
  249.   WinTerminate(rhab);
  250.   DosExit(0,0);
  251. }
  252.  
  253. MRESULT EXPENTRY InstProc (HWND wnd,ULONG msg,MPARAM mp1,MPARAM mp2)
  254. {
  255.   TID tid;
  256.    switch (msg) {
  257.      case WM_INITDLG :
  258.            return(0);
  259.      case WM_COMMAND :
  260.        switch (SHORT1FROMMP(mp1)) {
  261.         case BTN_INSTALL :
  262.                setButtons(wnd);
  263.                installing = TRUE;
  264.                DosCreateThread(&tid,InstallThread,(ULONG)wnd,CREATE_READY,20000L);
  265.                return(0);
  266.         case DID_OK :
  267.         case DID_CANCEL :
  268.         case BTN_EXIT :
  269.                 WinPostMsg(wnd,WM_CLOSE,0,0);
  270.                 return(0);
  271.        }
  272.      case WM_CLOSE : if (installing) return((MPARAM) 0); // Don't close if installing
  273.  
  274.                      if (installed) return(WinDefDlgProc(wnd,msg,mp1,mp2)); // If installed don't ask for exit confirmation
  275.  
  276.                      if (WinMessageBox(HWND_DESKTOP,wnd,"Are you sure you want to quit VClassed's Install?","Confirm",0,MB_YESNO | MB_ICONQUESTION | MB_MOVEABLE) == MBID_YES)
  277.                        { return(WinDefDlgProc(wnd,msg,mp1,mp2)); }
  278.                      else
  279.                        { return((MPARAM) 0); }
  280.      case WM_DESTROY :
  281.         return(0);
  282.      default:
  283.        return(WinDefDlgProc(wnd,msg,mp1,mp2));
  284.      }
  285. }
  286.  
  287. void main(int argc, char *argv[])
  288. {
  289.   HAB mhab;
  290.   HMQ mhmq;
  291.   HPOINTER icon;
  292.   RECTL mrect;
  293.   HWND dlgwnd;
  294.   char *c;
  295.  
  296.  /* Startup */
  297.   mhab = WinInitialize(0);
  298.   mhmq = WinCreateMsgQueue(mhab,0);
  299.  
  300.   strcpy(programpath,argv[0]);
  301.   if ((c = strrchr(programpath,'\\')) != NULL)
  302.    { c++;
  303.      *c = 0;
  304.    }
  305.  
  306.  /* Main block */
  307.   icon = WinLoadPointer(HWND_DESKTOP,NULLHANDLE,1); // Load pointer for Main dialog
  308.   dlgwnd = WinLoadDlg(HWND_DESKTOP,HWND_DESKTOP,&InstProc,NULLHANDLE, DLG_INSTALLVCLASSED,NULL); // Load main dialog
  309.   WinSendMsg(dlgwnd,WM_SETICON,(MPARAM) icon,0); // Set new dialog icon
  310.   WinQueryWindowRect(dlgwnd,&mrect);
  311.   WinSetWindowPos(dlgwnd,0,(WinQuerySysValue(HWND_DESKTOP,SV_CXSCREEN) - (mrect.xRight-mrect.xLeft)) / 2,
  312.                            (WinQuerySysValue(HWND_DESKTOP,SV_CYSCREEN) - (mrect.yTop-mrect.yBottom)) / 2, 0,0, SWP_MOVE | SWP_SHOW);
  313.  
  314.  
  315.  
  316.   WinProcessDlg(dlgwnd);
  317.  
  318.  /* Close'n'Clean */
  319.   WinDestroyWindow(dlgwnd);
  320.   WinDestroyMsgQueue(mhmq);
  321.   WinTerminate(mhab);
  322. }