home *** CD-ROM | disk | FTP | other *** search
/ Chip 2002 February / Chip_2002-02_cd1.bin / sharewar / apaths / APSOURCE.ZIP / ServiceTab.c < prev    next >
C/C++ Source or Header  |  2001-03-26  |  10KB  |  388 lines

  1. /* ServiceTab - March 26th, 2001
  2. **
  3. **      Copyright (c) 1997-2001 by Gregory Braun. All rights reserved.
  4. **
  5. **      This routine displays a list of the RunServices keys found in the
  6. **      MS Windows 95/NT System Registry.
  7. **
  8. **      Private (static) routines are included to initialize the
  9. **      property page sheet when first invoked as well as handle
  10. **      [Create], [Modify] and [Remove] user requests.
  11. **
  12. **      Called:     dlg = handle to the dialog window.
  13. **                  msg = the MS Windows WM_xxxxxx message to be handled.
  14. **                  wp  = the WPARAM specific to the message being sent
  15. **                  lp  = the LPARAM specific to the message being sent
  16. **
  17. **      Returns:    TRUE upon success, or FALSE if the user pressed [Cancel],
  18. **                  or an internal error exists.
  19. */
  20.  
  21. #include "AppPaths.h"
  22.  
  23. #define KILL_QUERY          "Title:\t%s\r" \
  24.                             "Cmd:\t%s\r\r" \
  25.                             "There is no UNDO available for the Remove command.\r" \
  26.                             "Are you sure you want to delete this registery entry?\r"
  27.  
  28. #define CREATE_FAILURE      "The selected MS Windows System Registry\r" \
  29.                             "Service key could not be created."
  30.  
  31. #define KILL_FAILURE        "The selected MS Windows System Registry\r" \
  32.                             "Service key could not be removed."
  33.  
  34. #define MODIFY_FAILURE      "The selected MS Windows System Registry\r" \
  35.                             "Service key could not be modified."
  36.  
  37. #define MEMORY_ERROR        "Memory Error\r\r" \
  38.                             "Windows can not allocate a temporary memory\r" \
  39.                             "buffer for the requested sorting operations."
  40.  
  41.  
  42. static int                  sort = COLUMN_1;
  43.  
  44. static BOOL                 initiate        (HWND dlg);
  45. static BOOL                 selection       (HWND dlg,WPARAM wp,LPARAM lp);
  46. static BOOL                 verify          (HWND dlg);
  47.  
  48. static BOOL                 create          (HWND dlg);
  49. static BOOL                 modify          (HWND dlg);
  50. static BOOL                 kill            (HWND dlg);
  51.  
  52. static BOOL                 reflect         (HWND dlg,BOOL hit);
  53.  
  54. static BOOL                 order           (HWND list,int column);
  55.  
  56. static int                  by_name         (const void * k1,const void * k2);
  57. static int                  by_path         (const void * k1,const void * k2);
  58.  
  59. static int                  cmp_name        (LPCSTR n1,LPCSTR n2);
  60. static int                  cmp_path        (LPCSTR p1,LPCSTR p2);
  61.  
  62. extern UINT CALLBACK ServiceTab (HWND dlg,UINT msg,WPARAM wp,LPARAM lp)
  63. {
  64.     auto int                item    = LOWORD (wp);
  65.  
  66.     auto LPNMHDR            hdr     = (LPNMHDR) lp;
  67.     auto NM_LISTVIEW *      cmd     = (NM_LISTVIEW *) lp;
  68.  
  69.     auto HWND               list;
  70.     auto BOOL               good;
  71.  
  72.     switch (msg) {
  73.  
  74.         case WM_COMMAND :
  75.  
  76.              switch (item) {
  77.  
  78.                  case SRV_CREATE_BTN :
  79.                       create (dlg);
  80.                       break;
  81.  
  82.                  case SRV_MODIFY_BTN :
  83.                       modify (dlg);
  84.                       break;
  85.  
  86.                  case SRV_REMOVE_BTN :
  87.                       kill (dlg);
  88.                       break;
  89.  
  90.                  default :
  91.                       break;
  92.                       }
  93.  
  94.              break;
  95.  
  96.         case WM_HELP :
  97.              HelpTip (dlg,((LPHELPINFO) lp)->iCtrlId);
  98.              break;
  99.  
  100.         case WM_NOTIFY :
  101.  
  102.              switch (((LPNMHDR) lp)->code) {
  103.  
  104.                 case LVN_COLUMNCLICK :
  105.                      list = hdr->hwndFrom;
  106.                      sort = cmd->iSubItem;
  107.                      order (list,sort);
  108.                      break;
  109.  
  110.                 case PSN_SETACTIVE :
  111.                      initiate (dlg);
  112.                      break;
  113.  
  114.                 case PSN_KILLACTIVE :
  115.                      good = verify (dlg);
  116.                      SetWindowLong (dlg,DWL_MSGRESULT,!good);
  117.                      return ((good) ? FALSE : TRUE);
  118.                      break;
  119.  
  120.                 case PSN_APPLY :
  121.                      good = verify (dlg);
  122.                      SetWindowLong (dlg,DWL_MSGRESULT,good);
  123.                      return ((good) ? PSNRET_NOERROR : PSNRET_INVALID_NOCHANGEPAGE);
  124.                      break;
  125.  
  126.                 case PSN_HELP :
  127.                      HelpTopic (dlg,SRV_TAB);
  128.                      break;
  129.  
  130.                 default :
  131.                      selection (dlg,wp,lp);
  132.                      break;
  133.                      }
  134.  
  135.              break;
  136.  
  137.         break;
  138.         }
  139.  
  140.      return (FALSE);
  141. }
  142.  
  143. static BOOL initiate (HWND dlg)
  144. {
  145.     static BOOL     created = FALSE;
  146.  
  147.     auto LPSTR      label[] = { "Title", "Command" };
  148.     auto int        width[] = { 115,300 };
  149.  
  150.     auto HWND       ctl     = GetDlgItem (dlg,SRV_MEMBER_LST);
  151.  
  152.     auto LV_COLUMN  col     = { NIL };
  153.  
  154.     auto int        inx;
  155.  
  156.     if (!created) {
  157.  
  158.         col.mask = LVCF_FMT | LVCF_WIDTH | LVCF_TEXT | LVCF_SUBITEM;
  159.         col.fmt  = LVCFMT_LEFT;
  160.  
  161.         for (inx = NIL; inx < SRV_COLS_MAX; inx++) {
  162.             col.iSubItem = inx;
  163.             col.pszText = label[inx];
  164.             col.cx      = width[inx];
  165.             ListView_InsertColumn (ctl,inx,&col);
  166.             }
  167.  
  168.         created = TRUE;
  169.         }
  170.  
  171.     GetSKeys (ctl);
  172.  
  173.     order (ctl,sort);
  174.     reflect (dlg,TRUE);
  175.  
  176.     return (TRUE);
  177. }
  178.  
  179. static BOOL selection (HWND dlg,WPARAM wp,LPARAM lp)
  180. {
  181.     auto int            item    = (int) wp;
  182.  
  183.     auto LPNMHDR        hdr     = (LPNMHDR) lp;
  184.     auto UINT           code    = hdr->code;
  185.     auto HWND           list    = hdr->hwndFrom;
  186.  
  187.     auto int            hit;
  188.  
  189.     if (item != SRV_MEMBER_LST)
  190.         return (FALSE);
  191.  
  192.     switch (code) {
  193.  
  194.         case NM_CLICK :
  195.         case NM_DBLCLK :
  196.              if ((hit = LV_RowHit (list)) >= NIL) {
  197.                  reflect (dlg,TRUE);
  198.                  if (code == NM_DBLCLK)
  199.                      modify (dlg);
  200.                  }
  201.              else reflect (dlg,FALSE);
  202.              break;
  203.  
  204.         default :
  205.              break;
  206.              }
  207.  
  208.     return (TRUE);
  209. }
  210.  
  211. static BOOL verify (HWND dlg)
  212. {
  213.     UNUSED_ARG (dlg);
  214.  
  215.     return (TRUE);
  216. }
  217.  
  218. static BOOL create (HWND dlg)
  219. {
  220.     auto SRV    srv     = { NIL };
  221.  
  222.     auto HWND   list    = GetDlgItem (dlg,SRV_MEMBER_LST);
  223.  
  224.     if (!EditService (dlg,&srv))
  225.         return (FALSE);
  226.  
  227.     if (!MakeSKey (&srv))
  228.         return (Error (dlg,NULL,CREATE_FAILURE));
  229.  
  230.     SL_AddItem (list,&srv);
  231.  
  232.     return (TRUE);
  233. }
  234.  
  235. static BOOL modify (HWND dlg)
  236. {
  237.     auto SRV    was     = { NIL };
  238.     auto SRV    srv     = { NIL };
  239.  
  240.     auto HWND   list    = GetDlgItem (dlg,SRV_MEMBER_LST);
  241.     auto int    index   = LV_GetCurSel (list);
  242.  
  243.     ListView_EnsureVisible (list,index,FALSE);
  244.  
  245.     SL_GetItem (list,index,&srv);
  246.     was = srv;
  247.  
  248.     if (!EditService (dlg,&srv))
  249.         return (FALSE);
  250.  
  251.     if (!KillSKey (&was))
  252.         return (Error (dlg,NULL,KILL_FAILURE));
  253.  
  254.     if (!MakeSKey (&srv))
  255.         return (Error (dlg,NULL,MODIFY_FAILURE));
  256.  
  257.     SL_PutItem (list,index,&srv);
  258.  
  259.     return (TRUE);
  260. }
  261.  
  262. static BOOL kill (HWND dlg)
  263. {
  264.     auto SRV    srv     = { NIL };
  265.  
  266.     auto HWND   list    = GetDlgItem (dlg,SRV_MEMBER_LST);
  267.     auto int    index   = LV_GetCurSel (list);
  268.  
  269.     auto char   msg[MSTRING];
  270.  
  271.     ListView_EnsureVisible (list,index,FALSE);
  272.  
  273.     SL_GetItem (list,index,&srv);
  274.  
  275.     wsprintf (msg,KILL_QUERY,srv.name,srv.path);
  276.  
  277.     if (!Query (dlg,NULL,msg))
  278.         return (FALSE);
  279.  
  280.     if (!KillSKey (&srv))
  281.         return (Error (dlg,NULL,KILL_FAILURE));
  282.  
  283.     ListView_DeleteItem (list,index);
  284.     reflect (dlg,FALSE);
  285.  
  286.     return (TRUE);
  287. }
  288.  
  289. static BOOL reflect (HWND dlg,BOOL hit)
  290. {
  291.     Button_Enable (GetDlgItem (dlg,SRV_CREATE_BTN),TRUE);
  292.  
  293.     Button_Enable (GetDlgItem (dlg,SRV_MODIFY_BTN),hit);
  294.     Button_Enable (GetDlgItem (dlg,SRV_REMOVE_BTN),hit);
  295.  
  296.     return (TRUE);
  297. }
  298.  
  299. static BOOL order (HWND list,int column)
  300. {
  301.     auto int        count   = ListView_GetItemCount (list);
  302.     auto LPSRV      srv     = (count) ? GlobalAllocPtr (GHND,count * sizeof (SRV)) : NULL;
  303.  
  304.     auto HCURSOR    old;
  305.     auto int        inx;
  306.  
  307.     if (!count) return (FALSE);
  308.  
  309.     if (!srv) return (Error (list,NULL,MEMORY_ERROR));
  310.  
  311.     old = SetCursor (LoadCursor (NULL,IDC_WAIT));
  312.  
  313.     SetWindowRedraw (list,FALSE);
  314.  
  315.     for (inx = NIL; inx < count; inx++)
  316.          SL_GetItem (list,inx,&srv[inx]);
  317.  
  318.     switch (column) {
  319.  
  320.         case COLUMN_2 :
  321.              qsort (srv,count,sizeof (SRV),by_path);
  322.              break;
  323.  
  324.         default :
  325.              qsort (srv,count,sizeof (SRV),by_name);
  326.              break;
  327.              }
  328.  
  329.     for (inx = NIL; inx < count; inx++)
  330.          SL_PutItem (list,inx,&srv[inx]);
  331.  
  332.     SetWindowRedraw (list,TRUE);
  333.  
  334.     LV_SetCurSel (list,NIL);
  335.  
  336.     GlobalFreePtr (srv);
  337.  
  338.     SetCursor (old);
  339.  
  340.     return (TRUE);
  341. }
  342.  
  343. static int by_name (const void * k1,const void * k2)
  344. {
  345.     auto LPSRV      p1      = (LPSRV) k1;
  346.     auto LPSRV      p2      = (LPSRV) k2;
  347.     auto int        result  = cmp_name (p1->name,p2->name);
  348.  
  349.     if (result) return (result);
  350.  
  351.     return (cmp_path (p1->path,p2->path));
  352. }
  353.  
  354. static int by_path (const void * k1,const void * k2)
  355. {
  356.     auto LPSRV      p1      = (LPSRV) k1;
  357.     auto LPSRV      p2      = (LPSRV) k2;
  358.     auto int        result  = cmp_path (p1->path,p2->path);
  359.  
  360.     if (result) return (result);
  361.  
  362.     return (cmp_name (p1->name,p2->name));
  363. }
  364.  
  365. static int cmp_name (LPCSTR n1,LPCSTR n2)
  366. {
  367.     if (!*n1 && !*n2) return (NIL);
  368.  
  369.     if (!*n1) return (1);
  370.  
  371.     if (!*n2) return (-1);
  372.  
  373.     return (lstrcmpi (n1,n2));
  374. }
  375.  
  376. static int cmp_path (LPCSTR p1,LPCSTR p2)
  377. {
  378.     if (!*p1 && !*p2) return (NIL);
  379.  
  380.     if (!*p1) return (1);
  381.  
  382.     if (!*p2) return (-1);
  383.  
  384.     return (lstrcmpi (p1,p2));
  385. }
  386.  
  387. /* end of ServiceTab.c - written by Gregory Braun */
  388.