home *** CD-ROM | disk | FTP | other *** search
/ Chip 2000 May / Chip_2000-05_cd1.bin / tema / SW602 / Winbase / data1.cab / SDK_Files / C-lang / MFC / WBMFCSSA.CPP < prev    next >
Encoding:
C/C++ Source or Header  |  2000-03-16  |  10.2 KB  |  350 lines

  1. // wbmfcssa.cpp : implementation file
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "winreg.h"
  6. #include "resource.h"
  7. #include "wbmfc.h"
  8. #include "wbmfcssa.h"
  9.  
  10. #ifdef _DEBUG
  11. #define new DEBUG_NEW
  12. #undef THIS_FILE
  13. static char THIS_FILE[] = __FILE__;
  14. #endif
  15.  
  16. BOOL WBSelSrvrAppl(const char *UserName, const char *Password, const char *Appl, char *SelSrvr, char *SelAppl)
  17. {
  18.     CSelSrvrApplDlg Dlg(UserName, Password, Appl, SelSrvr, SelAppl);
  19.     return(Dlg.DoModal() == IDOK);
  20. }
  21. /////////////////////////////////////////////////////////////////////////////
  22. // CSelSrvrApplDlg dialog
  23.  
  24.  
  25. CSelSrvrApplDlg::CSelSrvrApplDlg(const char *UserName, const char *Password, const char *Appl, char *SelSrvr, char *SelAppl, CWnd* pParent /*=NULL*/)
  26.     : CDialog(CSelSrvrApplDlg::IDD, pParent)
  27. {
  28.     //{{AFX_DATA_INIT(CSelSrvrApplDlg)
  29.         // NOTE: the ClassWizard will add member initialization here
  30.     //}}AFX_DATA_INIT
  31.     m_SelServer = NULL;
  32.     m_UserName  = UserName;
  33.     m_Password  = Password;
  34.     m_Appl      = Appl;
  35.     m_NoAppl    = UserName == WBAPPI_NOLOGIN || Appl != NULL;
  36.     m_SelSrvr   = SelSrvr;
  37.     if (SelSrvr)
  38.         *SelSrvr = 0;
  39.     m_SelAppl   = SelAppl;
  40.     if (SelAppl)
  41.         *SelAppl = 0;
  42. }
  43.  
  44.  
  45. BEGIN_MESSAGE_MAP(CSelSrvrApplDlg, CDialog)
  46.     //{{AFX_MSG_MAP(CSelSrvrApplDlg)
  47.     ON_NOTIFY(TVN_SELCHANGED, IDC_SRVRTREE, OnSelchanged)
  48.     ON_NOTIFY(NM_SETFOCUS, IDC_SRVRTREE, OnSelchanged)
  49.     ON_NOTIFY(TVN_ITEMEXPANDING, IDC_SRVRTREE, OnItemexpanding)
  50.     ON_NOTIFY(NM_DBLCLK, IDC_SRVRTREE, OnDblclk)
  51.     ON_EN_CHANGE(IDC_APPLEDIT, OnChangeEdit)
  52.     ON_EN_CHANGE(IDC_SRVREDIT, OnChangeEdit)
  53.     ON_WM_DESTROY()
  54.     //}}AFX_MSG_MAP
  55. END_MESSAGE_MAP()
  56.  
  57. /////////////////////////////////////////////////////////////////////////////
  58. // CSelSrvrApplDlg message handlers
  59.  
  60. BOOL CSelSrvrApplDlg::OnInitDialog() 
  61. {
  62.     CDialog::OnInitDialog();
  63.     
  64.     // TODO: Add extra initialization here
  65.     m_SrvrTree  = (CTreeCtrl *)GetDlgItem(IDC_SRVRTREE);
  66.     GetDlgItem(IDOK)->EnableWindow(FALSE);
  67.     if (!m_SrvrTree)
  68.         return FALSE;
  69.     FillSrvrTree();    
  70.     if (m_NoAppl)
  71.     {
  72.         CWnd *hEdit = GetDlgItem(IDC_APPLEDIT);
  73.         hEdit->EnableWindow(FALSE);
  74.         hEdit->ShowWindow(SW_HIDE);
  75.         GetDlgItem(IDC_APPLSTATIC)->ShowWindow(FALSE);
  76.         CRect Rect;
  77.         GetDlgItem(IDC_SRVREDIT)->GetWindowRect(&Rect);
  78.         int NewyExt = Rect.bottom + 20;
  79.         GetWindowRect(&Rect);
  80.         Rect.bottom = NewyExt;
  81.         MoveWindow(&Rect, FALSE);
  82.         SetWindowLong(*m_SrvrTree, GWL_STYLE, GetWindowLong(*m_SrvrTree, GWL_STYLE) & ~TVS_HASBUTTONS);
  83.     }
  84.     return TRUE;  // return TRUE unless you set the focus to a control
  85.                   // EXCEPTION: OCX Property Pages should return FALSE
  86. }
  87.  
  88. #define IND_APPL     0
  89. #define IND_SRVLOCAL 1
  90. #define IND_SRVREMNR 2
  91. #define IND_SRVRAS   3 
  92.  
  93. BOOL CSelSrvrApplDlg::FillSrvrTree()
  94.     HKEY hKey, hSubKey;    
  95.     char Path[MAX_PATH];
  96.     int  ind = 0;
  97.     tobjname Server;  
  98.  
  99.     LONG Err = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "SOFTWARE\\Software602\\WinBase602\\Database", 0, KEY_READ, &hKey);
  100.     if (Err != ERROR_SUCCESS) 
  101.         return(FALSE);
  102.  
  103.     CBitmap bm;
  104.     bm.LoadBitmap(IDR_SRVRAPPL);
  105.     CImageList *il = new CImageList;
  106.     il->Create(16, 16, ILC_COLOR | ILC_MASK, 4, 0);
  107.     il->Add(&bm, RGB(0, 128, 128)); 
  108.     m_SrvrTree->SetImageList(il, TVSIL_NORMAL);
  109.     bm.DeleteObject();
  110.  
  111.     do
  112.     { 
  113.         DWORD KeyLen = sizeof(Server);
  114.         if (RegEnumKey(hKey, ind++, Server, KeyLen) != ERROR_SUCCESS)
  115.             break;
  116.         // get server info from the registry:
  117.         BOOL local = FALSE, verok = FALSE, ras = FALSE;
  118.         if (RegOpenKeyEx(hKey, Server, 0, KEY_READ, &hSubKey) == ERROR_SUCCESS)
  119.         { 
  120.             KeyLen = MAX_PATH;  
  121.             *Path  = 0;
  122.             RegQueryValueEx(hSubKey, "Path",  NULL, NULL, (BYTE *)Path, &KeyLen);
  123.             local  = *Path != 0;
  124.             KeyLen = MAX_PATH;  
  125.             *Path  = 0;
  126.             RegQueryValueEx(hSubKey, "Version", NULL, NULL, (BYTE *)Path, &KeyLen);
  127.             verok  = !lstrcmp(Path, "5");
  128.             KeyLen = MAX_PATH;  
  129.             *Path  = 0;
  130.             RegQueryValueEx(hSubKey, "ConnectionName", NULL, NULL, (BYTE *)Path, &KeyLen);
  131.             if (*Path) 
  132.                 ras = TRUE;
  133.             KeyLen = MAX_PATH;  
  134.             *(BOOL *)Path = FALSE;
  135.             RegQueryValueEx(hSubKey, "Through Firewall", NULL, NULL, (BYTE *)Path, &KeyLen);
  136.             if (*(BOOL *)Path) 
  137.                 ras = TRUE;
  138.             RegCloseKey(hSubKey);
  139.         }
  140.         if (!verok) 
  141.             continue;
  142.         // select the proper image:
  143.         int iImage;
  144.         if (local)
  145.             iImage  = IND_SRVLOCAL;
  146.         else
  147.               iImage  = ras ? IND_SRVRAS : IND_SRVREMNR;
  148.         HTREEITEM it = m_SrvrTree->InsertItem
  149.                        (
  150.                            TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM,
  151.                            Server,
  152.                            iImage,
  153.                            iImage,
  154.                            0, 0, 
  155.                            0,
  156.                            TVI_ROOT,
  157.                            TVI_LAST
  158.                        );
  159.         m_SrvrTree->InsertItem
  160.         (
  161.             TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM,
  162.             NULL,
  163.             IND_APPL,
  164.             IND_APPL,
  165.             0, 0, 
  166.             1,
  167.             it,
  168.             TVI_LAST
  169.         );
  170. // fictive branch, used to expand the item:
  171.               //is.hParent= anItem;
  172.             //TreeView_InsertItem(hTree, &is);
  173.     } 
  174.     while (TRUE);
  175.     RegCloseKey(hKey);
  176.     return(TRUE);
  177. }
  178.  
  179. void CSelSrvrApplDlg::OnSelchanged(NMHDR* pNMHDR, LRESULT* pResult) 
  180. {
  181.     // TODO: Add your control notification handler code here
  182.     *pResult = 0;
  183.     HTREEITEM hItem = m_SrvrTree->GetSelectedItem();
  184.     if (!hItem)
  185.         return;
  186.  
  187.     if (IsAppl(hItem))
  188.     {
  189.         SetDlgItemText(IDC_APPLEDIT, m_SrvrTree->GetItemText(hItem));
  190.     }
  191.     else
  192.     {
  193.         TV_ITEM ti;
  194.         ti.mask       = TVIF_TEXT;
  195.         ti.hItem      = hItem;
  196.         ti.pszText    = m_SelServerName;
  197.         ti.cchTextMax = sizeof(m_SelServerName);
  198.         m_SrvrTree->GetItem(&ti);
  199.         SetDlgItemText(IDC_SRVREDIT, m_SelServerName);
  200.     }
  201. }
  202.  
  203. void CSelSrvrApplDlg::OnItemexpanding(NMHDR* pNMHDR, LRESULT* pResult) 
  204. {
  205.     NM_TREEVIEW* pNMTreeView = (NM_TREEVIEW*)pNMHDR;
  206.     // TODO: Add your control notification handler code here
  207.     *pResult = 0;
  208.     if (m_NoAppl)
  209.     {
  210.         *pResult = 1;
  211.         return;
  212.     }
  213.     if (pNMTreeView->action == TVE_EXPAND)
  214.     {
  215.         //m_SrvrTree->SetRedraw(FALSE);
  216.         CWBApp *WBApp   = (CWBApp *)AfxGetApp();
  217.         if (m_SelServer)
  218.         {
  219.             WBApp->Logout();
  220.             m_SrvrTree->Expand(m_SelServer, TVE_COLLAPSE);
  221.             m_SelServer = NULL;
  222.         }
  223.         HTREEITEM hItem = pNMTreeView->itemNew.hItem;
  224.         m_SrvrTree->SelectItem(hItem);
  225.         CString Server  = m_SrvrTree->GetItemText(hItem);
  226.         if (!WBApp->WBAppInit(NULL, m_UserName, m_Password, Server))
  227.         {
  228.                *pResult = TRUE;
  229.             return;
  230.         }
  231.         HTREEITEM delItem;
  232.         while ((delItem = m_SrvrTree->GetChildItem(hItem)))
  233.             m_SrvrTree->DeleteItem(delItem);
  234.         m_SelServer = hItem;
  235.         cdp_t cdp = WBApp->GetCdp();
  236.         tcursnum Curs;
  237.         if (cd_Open_cursor_direct(cdp, "SELECT OBJ_NAME FROM OBJTAB WHERE CATEGORY=Chr(7)", &Curs))
  238.         {
  239.             Signalize();
  240.             return;
  241.         }
  242.         tobjname Appl;
  243.         trecnum  Pos = 0;
  244.         while (!cd_Read_ind(cdp, Curs, Pos++, 1, NOINDEX, Appl))
  245.         {
  246.             small_string(Appl, TRUE);
  247.             m_SrvrTree->InsertItem
  248.             (
  249.                 TVIF_TEXT | TVIF_IMAGE | TVIF_SELECTEDIMAGE | TVIF_PARAM,
  250.                 Appl,
  251.                 IND_APPL,
  252.                 IND_APPL,
  253.                 0, 0, 
  254.                 1,
  255.                 hItem,
  256.                 TVI_LAST
  257.             );
  258.         }
  259.         if (cd_Sz_error(cdp) != OUT_OF_TABLE)
  260.             Signalize();
  261.         Close_cursor(Curs);
  262. //redraw:
  263.         //m_SrvrTree->SetRedraw(TRUE);
  264.     }
  265.     else if (pNMTreeView->action == TVE_COLLAPSE)
  266.     {
  267.         ((CWBApp *)AfxGetApp())->Logout();
  268.         m_SelServer = NULL;
  269.     }
  270. }
  271.  
  272. void CSelSrvrApplDlg::OnDblclk(NMHDR* pNMHDR, LRESULT* pResult) 
  273. {
  274.     // TODO: Add your control notification handler code here
  275.     *pResult = 0;
  276.     if (m_NoAppl || IsAppl())
  277.         OnOK();
  278. }
  279.  
  280. void CSelSrvrApplDlg::OnChangeEdit() 
  281. {
  282.     // TODO: Add your control notification handler code here
  283.     tobjname ServerName;
  284.     CWnd *Edit = GetDlgItem(IDC_SRVREDIT);
  285.     int sz = Edit->GetWindowText(ServerName, sizeof(ServerName));
  286.     if (lstrcmp(ServerName, m_SelServerName))
  287.     {
  288.         if (m_SelServer)
  289.         {
  290.             m_SrvrTree->Expand(m_SelServer, TVE_COLLAPSE);
  291.             m_SelServer = NULL;
  292.         }
  293.         ((CWBApp *)AfxGetApp())->Logout();
  294.     }
  295.     Edit = GetDlgItem(IDC_APPLEDIT);
  296.     GetDlgItem(IDOK)->EnableWindow(sz && (m_NoAppl || Edit->GetWindowTextLength()));
  297. }
  298.  
  299. void CSelSrvrApplDlg::OnOK() 
  300. {
  301.     // TODO: Add extra validation here
  302.     tobjname SrvrAppl;
  303.     CWBApp *WBApp = (CWBApp *)AfxGetApp();
  304.     if (!WBApp->Linked())
  305.     {
  306.         if (!GetDlgItemText(IDC_SRVREDIT, SrvrAppl, sizeof(SrvrAppl)))
  307.             return;
  308.         if (!WBApp->WBAppInit(NULL, m_UserName, m_Password, SrvrAppl))
  309.             return;
  310.     }
  311.     if (m_SelSrvr)
  312.         lstrcpy(m_SelSrvr, SrvrAppl);
  313.     if (!m_NoAppl)
  314.     {    
  315.         if (!m_Appl)
  316.         {
  317.             if (!GetDlgItemText(IDC_APPLEDIT, SrvrAppl, sizeof(SrvrAppl)))
  318.                 return;
  319.             m_Appl = SrvrAppl;
  320.         }
  321.         if (!WBApp->SetApplication(m_Appl))
  322.             return;
  323.     }
  324.     if (m_SelAppl && m_Appl)
  325.         lstrcpy(m_SelAppl, m_Appl);
  326.     CDialog::OnOK();
  327. }
  328.  
  329. BOOL CSelSrvrApplDlg::IsAppl(HTREEITEM hItem)
  330. {
  331.     TV_ITEM   ti;
  332.     ti.mask  = TVIF_PARAM;
  333.     ti.hItem = hItem;
  334.     if (!hItem)
  335.         ti.hItem = m_SrvrTree->GetSelectedItem();
  336.     m_SrvrTree->GetItem(&ti);
  337.     return ti.lParam;
  338. }
  339.  
  340. void CSelSrvrApplDlg::OnDestroy() 
  341. {
  342.     CDialog::OnDestroy();
  343.     
  344.     // TODO: Add your message handler code here
  345.     CImageList *il = m_SrvrTree->GetImageList(TVSIL_NORMAL);
  346.     delete il;
  347. }
  348.  
  349.