home *** CD-ROM | disk | FTP | other *** search
/ Borland Programmer's Resource / Borland_Programmers_Resource_CD_1995.iso / winsock / finger10 / finger.cpp next >
Encoding:
C/C++ Source or Header  |  1995-05-19  |  12.7 KB  |  513 lines

  1. //
  2. // Finger Version 1.0, a Windows Sockets Finger Client
  3. //
  4. // Copyright (C) 1994 by Zoran Dukic.
  5. //
  6. // Permission to use, modify, and distribute this software and its
  7. // documentation for any purpose and without fee is hereby granted, provided
  8. // that the above copyright notice appears in all copies and that both
  9. // that copyright notice and this permission notice appear in supporting
  10. // documentation.  Zoran Dukic makes no claims as to the suitability of this 
  11. // software for any purpose.
  12. //
  13. // finger.cpp      Finger's user interface.  This module processes all user
  14. //             input, and displays query results and errors.
  15. // netwrkm.cpp     Finger's network module.  It isolates the network
  16. //               interface from the rest of the program, and uses 
  17. //               asynchronous WS calls to query the remote host.
  18. //
  19. // Module NETWRKM is reconstruction of the same module from the Finger 3.1, 
  20. // for C++, so in netwrkm.cpp is the copyright notice for the Finger 3.1.
  21.  
  22.  
  23.  
  24. #include "finger.h"
  25.  
  26. char     szAppName[] = "Finger";    // application's name
  27.  
  28. ERRENTRY wsErrs[] =                 // error text for windows sockets errors
  29. {
  30.    WSAVERNOTSUPPORTED,  "This version of Windows Sockets is not supported",
  31.    WSASYSNOTREADY,      "Windows Sockets is not present or is not responding",
  32. };
  33.  
  34. ERRENTRY finErrs[] =                // finger specific error text
  35. {
  36.    FE_NOPORT,  "Cannot locate port for finger service",
  37.    FE_NOHOST,  "Unrecognized host name",
  38.    FE_NOSOCK,  "Cannot obtain socket for connection",
  39.    FE_NOCONN,  "Cannot connect to remote server",
  40.    FE_NOSEND,  "Cannot send query to remote server",
  41.    FE_NORECV,  "Error occurred during retrieval"
  42. };
  43.  
  44.  
  45.  
  46.  
  47. /********************************************
  48.  * Function Definition for TFingerApp class *
  49.  ********************************************/
  50.  
  51. void TFingerApp::InitMainWindow()
  52. {
  53.    MainWindow = new TFingerWnd(NULL);
  54. }
  55.  
  56. /**************************************************************************/
  57.  
  58.  
  59. /******************************************
  60.  * Function Definition for THostDlg class *
  61.  ******************************************/
  62.  
  63. THostDlg::THostDlg(PTWindowsObject AParent, LPSTR AName, PTModule)
  64.         :TDialog(AParent, AName)
  65. {  
  66.      pHostCombo = new TComboBox(this, ID_HOSTNAME, 41);
  67.      new TEdit(this, ID_USER, 41);
  68.  
  69.      EnableKBHandler();
  70.      TransferBuffer = (void far*)&(((PTFingerWnd)AParent)->HostTransfer);
  71. }
  72.  
  73.  
  74. void THostDlg::WMControlColor(TMessage& Msg)
  75. {
  76.   switch (Msg.LP.Hi) {
  77.     case CTLCOLOR_BTN:
  78.         Msg.Result = (LRESULT)GetStockObject(NULL_BRUSH);
  79.     break;
  80.     case CTLCOLOR_LISTBOX:
  81.     Msg.Result = (LRESULT)GetStockObject(LTGRAY_BRUSH);
  82.     break;
  83.     case CTLCOLOR_STATIC:
  84.     SetBkMode((HDC)Msg.WParam, TRANSPARENT);
  85.     Msg.Result = (LRESULT)GetStockObject(LTGRAY_BRUSH);
  86.         break;      
  87.     case CTLCOLOR_DLG:
  88.     SetBkMode((HDC)Msg.WParam, TRANSPARENT);
  89.     Msg.Result = (LRESULT)GetStockObject(LTGRAY_BRUSH);;
  90.         break;
  91.     default:
  92.       DefWndProc(Msg);
  93.   }
  94. }
  95.  
  96.  
  97. void THostDlg::HandleFingerMsg(RTMessage)
  98. {
  99.      TransferData(TF_GETDATA);
  100.  
  101.      ((PTFingerWnd)Parent)->FingerStart();
  102. }
  103.  
  104.  
  105. void THostDlg::Cancel(RTMessage)
  106. {
  107.       Parent->CloseWindow();
  108. }
  109.  
  110.  
  111. void THostDlg::HandleCloseMsg(RTMessage)
  112. {
  113.       Parent->CloseWindow();
  114. }
  115.  
  116.  
  117. void THostDlg::HandleAboutMsg(RTMessage)
  118. {
  119.      GetApplication()->ExecDialog(new TDialog(Parent,"ABOUTBOX"));
  120.      SetFocus(GetItemHandle(ID_ABOUT));
  121. }
  122.  
  123.  
  124. void THostDlg::HandleDeleteMsg(RTMessage)
  125. {
  126.       int i;
  127.       char temString[50];
  128.  
  129.       pHostCombo->GetText(temString, 50);
  130.       if((i = pHostCombo->FindExactString(temString, -1)) >= 0)
  131.       {
  132.            pHostCombo->DeleteString(i);
  133.            TransferData(TF_GETDATA);
  134.       }
  135. }
  136.  
  137.  
  138. void THostDlg::HandleSaveMsg(RTMessage)
  139. {
  140.       char temString[50];
  141.  
  142.       pHostCombo->GetText(temString, 50);
  143.       if(((pHostCombo->FindExactString(temString, -1)) < 0) && (strlen(temString) > 0))
  144.       {
  145.            pHostCombo->AddString(temString);
  146.            TransferData(TF_GETDATA);
  147.       }
  148. }
  149.  
  150.  
  151. void THostDlg::WMMouseMove(RTMessage)
  152. {
  153.    SetCursor(((PTFingerWnd)Parent)->hCursor);
  154. }
  155.  
  156. /************************************************************************/
  157.  
  158.  
  159. /********************************************
  160.  * Function Definition for TOutputWnd class *
  161.  ********************************************/
  162.  
  163. TOutputWnd::TOutputWnd(PTWindowsObject AParent)
  164.        :TWindow(AParent, "Output")
  165. {
  166.     Attr.Style = WS_BORDER | WS_VSCROLL | WS_HSCROLL | WS_CHILD;
  167.  
  168.     Scroller = new TScroller(this, 10, 10 , 10, 10);
  169.     Scroller->TrackMode = FALSE;
  170.     Scroller->AutoMode = FALSE;
  171.  
  172.     pViewList = NULL;
  173.     nLineItems = 0;
  174.     maxLen = 0;
  175. }
  176.  
  177.  
  178. void TOutputWnd::Paint(HDC PaintDC, PAINTSTRUCT&)
  179. {
  180.      int i;
  181.      PTLine pline;
  182.  
  183.      SelectObject(PaintDC, GetStockObject(SYSTEM_FIXED_FONT));
  184.  
  185.      for (i = 0; i < nLineItems; i++)
  186.      {
  187.         if ( (pline = (PTLine)&((*pViewList)[i])) != NULL)
  188.         {
  189.         TextOut(PaintDC, 0, i * (Scroller->YUnit), pline->sztext, pline->LLen);
  190.         }
  191.         else
  192.             break;
  193.      } 
  194. }
  195.  
  196.  
  197. //
  198. // SetRange -- sets the vertical scroll range to the length of the display
  199. // list.  The Scrollbar disappears when the list fits within the view.
  200. //
  201. void TOutputWnd::SetRange()
  202. {
  203.    if (pViewList != NULL)nLineItems = pViewList->getItemsInContainer();
  204.  
  205.    if (nLineItems > nClientLines)
  206.    {   
  207.       if (maxLen > nClientLen) Scroller->SetRange(maxLen - nClientLen, nLineItems - nClientLines);
  208.       else Scroller->SetRange(0, nLineItems - nClientLines);
  209.    }
  210.    else
  211.    {
  212.       if (maxLen > nClientLen) Scroller->SetRange(maxLen - nClientLen, 0);
  213.       else Scroller->SetRange(0, 0);
  214.    }
  215. }
  216.  
  217.  
  218. void TOutputWnd::WMSize(RTMessage Msg)
  219. {
  220.   TWindow::WMSize(Msg);
  221.   nClientLines = HIWORD(Msg.LParam) / (Scroller->YUnit);
  222.   nClientLen = LOWORD(Msg.LParam) / (Scroller->XUnit) - 1;
  223.   SetRange();
  224. }
  225.  
  226.  
  227. void TOutputWnd::WMMouseMove(RTMessage)
  228. {
  229.    SetCursor(((PTFingerWnd)Parent)->hCursor);
  230. }
  231.  
  232. /************************************************************************/
  233.  
  234.  
  235. /********************************************
  236.  * Function Definition for TFingerWnd class *
  237.  ********************************************/
  238.  
  239. TFingerWnd::TFingerWnd(PTWindowsObject AParent)
  240.        :TWindow(AParent, "Finger")
  241. {
  242.      Attr.Style = WS_OVERLAPPED | WS_VISIBLE | WS_SYSMENU | WS_MINIMIZEBOX;
  243.      Attr.X = 20;
  244.      Attr.Y = 20;
  245.  
  246.      pOutput = new TOutputWnd(this);
  247.      pHostDlg = new THostDlg(this, "HOSTDLG");
  248.      pNetWnd = new TNetWnd(this);
  249.  
  250.      memset(&HostTransfer, 0, sizeof(THostTransfer));
  251.      HostTransfer.pHost = new TComboBoxData();
  252.  
  253.      ReadHosts();
  254. }
  255.  
  256.  
  257. TFingerWnd::~TFingerWnd()
  258. {
  259.     if(HostTransfer.pHost != NULL)delete HostTransfer.pHost;
  260. };
  261.  
  262.  
  263. void TFingerWnd::SetupWindow()
  264. {
  265.    HDC hdc;                            // handle of device context
  266.    TEXTMETRIC tm;                      // contains font dimensions
  267.    RECT rect, rectHost;                // outer dimensions of window
  268.    
  269.    TWindow::SetupWindow();
  270.  
  271.    hCursor = LoadCursor(NULL, IDC_ARROW);
  272.  
  273.    hdc = GetDC(HWindow);
  274.  
  275.    SelectObject(hdc, GetStockObject(SYSTEM_FIXED_FONT));
  276.  
  277.    GetTextMetrics(hdc, &tm);
  278.    CharY = tm.tmHeight + tm.tmExternalLeading;
  279.  
  280.    ReleaseDC(HWindow, hdc);
  281.  
  282.    (pOutput->Scroller)->SetUnits(tm.tmMaxCharWidth, CharY);
  283.  
  284.    GetApplication()->MakeWindow(pHostDlg);
  285.    GetApplication()->MakeWindow(pOutput);
  286.    GetApplication()->MakeWindow(pNetWnd);
  287.  
  288.    GetWindowRect(HWindow, &rect);
  289.    GetWindowRect(pHostDlg->HWindow, &rectHost);
  290.  
  291.    MoveWindow( HWindow, rect.left, rect.top,
  292.            rectHost.right - rectHost.left,
  293.            rectHost.bottom - rectHost.top + 10 +
  294.            10 * CharY + GetSystemMetrics(SM_CYCAPTION),
  295.            FALSE);
  296.  
  297.    MoveWindow( pOutput->HWindow, 10, rectHost.bottom - rectHost.top,
  298.            rectHost.right - rectHost.left - 20,
  299.            10 * CharY, FALSE);
  300.  
  301.    ShowWindow( pHostDlg->HWindow, SW_SHOWNA);
  302.    ShowWindow( pOutput->HWindow, SW_SHOWNA);
  303.  
  304.    GetApplication()->SetKBHandler(pHostDlg);
  305. }
  306.  
  307.  
  308. LPSTR TFingerWnd::GetClassName()
  309. {
  310.      return "TFingerWnd";
  311. }
  312.  
  313.  
  314. void TFingerWnd::GetWindowClass(WNDCLASS& AWndClass)
  315. {
  316.      TWindow::GetWindowClass(AWndClass);
  317.      AWndClass.hIcon = LoadIcon(GetApplication()->hInstance,"ICON_1");
  318.      AWndClass.hbrBackground = (HBRUSH)GetStockObject(LTGRAY_BRUSH);
  319. }
  320.  
  321.  
  322. BOOL TFingerWnd::CanClose()
  323. {
  324.      WriteHosts();
  325.      return TRUE;
  326. }
  327.  
  328.  
  329. void TFingerWnd::ReadHosts()
  330. {
  331.      char szBuf[256], szHost[30], szEntry[10];
  332.      int i=0;
  333.  
  334.      GetPrivateProfileString("hosts", NULL, "", szBuf, sizeof(szBuf), "finger.ini");
  335.  
  336.      while (szBuf[i] != '\0')
  337.      {
  338.          int j = 0;
  339.      while (szBuf[i] != '\0') szEntry[j++] = szBuf[i++];
  340.      i++;
  341.      GetPrivateProfileString("hosts", szEntry, "", szHost, sizeof(szHost), "finger.ini");
  342.      HostTransfer.pHost->AddString(szHost);
  343.      }
  344.  
  345.      pHostDlg->TransferData(TF_SETDATA);
  346. }
  347.  
  348.  
  349. void TFingerWnd::WriteHosts()
  350. {
  351.      int i;
  352.      String strHost;
  353.      char szEntry[10], tem[5];
  354.  
  355.      WritePrivateProfileString("hosts", NULL, NULL, "finger.ini");
  356.  
  357.      for (i = 0; i < (HostTransfer.pHost->Strings)->getItemsInContainer(); i++)
  358.      {
  359.     if ( (strHost = (RString)(*(HostTransfer.pHost->Strings))[i]) != NULL)
  360.     {
  361.         strcpy(szEntry, "host");
  362.             strcat(szEntry, itoa(i, tem, 10));
  363.         WritePrivateProfileString("hosts", szEntry, strHost, "finger.ini");
  364.         }
  365.         else
  366.             break;
  367.      }
  368. }
  369.  
  370.  
  371. void TFingerWnd::Repaint()
  372. {
  373.    InvalidateRect(pOutput->HWindow, NULL, TRUE);
  374. }
  375.  
  376.  
  377. //
  378. // SetWinCaption -- set the frame window caption according to last
  379. // host fingered.
  380. //
  381. void TFingerWnd::SetWinCaption()
  382. {
  383.    char szcaption[80];
  384.    
  385.    strcpy(szcaption, szAppName);
  386.    strcat(szcaption, " - ");
  387.    strcat(szcaption, pNetWnd->szHostName);
  388.  
  389.    SetWindowText(HWindow, szcaption);
  390. }
  391.  
  392.  
  393. void TFingerWnd::FingerStart()
  394. {
  395.     if(((HostTransfer.pHost)->Selection) != NULL)
  396.     {
  397.            strcpy(pNetWnd->szHostName,(HostTransfer.pHost)->Selection);
  398.            strcpy(pNetWnd->szUser,HostTransfer.pUser);
  399.  
  400.             SetCursor(hCursor = LoadCursor(NULL, IDC_WAIT));
  401.        EnableWindow(pHostDlg->GetItemHandle(ID_FINGER), FALSE);
  402.        pNetWnd->FingerStart();
  403.     }
  404. }
  405.  
  406.  
  407. // FingerFinish -- invoked when the finger operation is complete,
  408. // this function updates the display list & repaints the frame window
  409. // client area if the operation was successful.
  410. //
  411. void TFingerWnd::FingerFinish(UINT Err)
  412. {  
  413.    if (!Err)
  414.    {
  415.  
  416.       if(pOutput->pViewList != NULL)delete pOutput->pViewList;
  417.  
  418.       pOutput->pViewList = pNetWnd->pReceiveList;
  419.       pOutput->maxLen = pNetWnd->maxLineLen;
  420.  
  421.       SetWinCaption();                          // set win title to host name
  422.       pOutput->SetRange();                              // rescale (or delete)
  423.       Repaint();                                // scrollbar & force a repaint
  424.    }
  425.  
  426.    EnableWindow(pHostDlg->GetItemHandle(ID_FINGER), TRUE);
  427.    SetFocus(pHostDlg->HWindow);
  428.    SetCursor(hCursor = LoadCursor(NULL, IDC_ARROW));
  429. }
  430.  
  431.  
  432. void TFingerWnd::ReportFingerErr(UINT Err)
  433. {
  434.      
  435.    int i;
  436.    
  437.    for (i = 0; i < dim(finErrs); i++)
  438.    {
  439.       if (Err == finErrs[i].err)
  440.       {
  441.          MessageBox(HWindow, finErrs[i].sztext, szAppName,
  442.             MB_ICONSTOP | MB_OK);
  443.          return;
  444.       }
  445.    }
  446.  
  447.    MessageBox(HWindow, "Unrecognized finger error", szAppName,
  448.       MB_ICONSTOP | MB_OK);
  449.    
  450. };
  451.  
  452.  
  453. //
  454. // ReportWSError -- prompt user with a windows sockets error message.
  455. //
  456. void TFingerWnd::ReportWSError(UINT Err)
  457. {
  458.    int i;
  459.    char szerr[40];
  460.  
  461.    for (i = 0; i < dim(wsErrs); i++)
  462.    {
  463.       if (Err == wsErrs[i].err)
  464.       {
  465.          MessageBox(HWindow, wsErrs[i].sztext, szAppName,
  466.             MB_ICONSTOP | MB_OK);
  467.          return;
  468.       }
  469.    }
  470.  
  471.    wsprintf(szerr, "Windows Sockets reports error %04x", Err);
  472.    MessageBox(HWindow, szerr, szAppName, MB_ICONSTOP | MB_OK);
  473. }
  474.  
  475.  
  476. void TFingerWnd::ActivationResponse(WORD Activated, BOOL IsIconified)
  477. {
  478.      if(Activated && !IsIconified)
  479.      {
  480.          SetFocus(pHostDlg->HWindow);
  481.          GetApplication()->SetKBHandler(pHostDlg);
  482.      }
  483.      else
  484.      GetApplication()->SetKBHandler(NULL);
  485. };
  486.  
  487.  
  488. void TFingerWnd::WMMouseMove(RTMessage)
  489. {
  490.    SetCursor(hCursor);
  491. }
  492.  
  493.  
  494. /**************************************************************************/
  495.  
  496.  
  497. /************************
  498.  * Main Program WinMain *
  499.  ************************/
  500.  
  501. int PASCAL WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  502.                    LPSTR lpCmdLine, int nCmdShow)
  503. {
  504.      TFingerApp FingerApp("Finger", hInstance, hPrevInstance, lpCmdLine, nCmdShow);
  505.  
  506.      FingerApp.Run();
  507.      return FingerApp.Status;
  508. }
  509.  
  510. /**************************************************************************/
  511.  
  512.  
  513.