SOL9 Sample: SolFinger

SOL9 2.0 Samples

1 Screenshot


2 Source code

/*
 * SolFinger.cpp 
 * Copyright (c) 2015 Antillia.com TOSHIYUKI ARAI. ALL RIGHTS RESERVED. 
 */


// SOL++2000
// 2000.02.18
 
#define _WINSOCK_DEPRECATED_NO_WARNINGS
 
#define COMMONCONTROLS_V6

#include <sol\ApplicationView.h>
#include <sol\PushButton.h>
#include <sol\LabelGadget.h>
#include <sol\TextField.h>
#include <sol\Profile.h>
#include <sol\ScrolledRichText.h>
#include <sol\SocketStream.h>
#include <sol\MessageFont.h>
#include <sol\ClientDC.h>
#include <sol\PaintDC.h>
#include <sol\stdio.h>
#include <sol\InetAddress.h>
#include <sol\Exception.h>
#include <sol\SocketSelector.h>

#include <sol/StringBufferT.h>

#include "resource.h"

namespace SOL {

class SolFinger :public ApplicationView {
private:
  int          textHeight;
  LabelGadget      hostName;
  LabelGadget      userName;
  TextField      hostField;
  TextField      userField;
  PushButton      query;
  MessageFont        font;
  SocketStream*    sock;
  ScrolledRichText  sctext;

public:
  /**
   * Constructor
   */
  SolFinger(Application& applet, const TCHAR* name, Args& args)
  :ApplicationView(applet, name, args)
  {
    sock = NULL;
    Args ar;

    font.create(9);

    ClientDC dc(this);
    HFONT prevFont = (HFONT)dc.select(&font);
    textHeight = dc.getTextHeight();
    dc.select(prevFont);

    ar.reset();
    sctext.create(this, NULL, ar);
    sctext.setFont(font);
  
    sctext.limitText(20000);
    sctext.setReadOnly();

    sctext.addCallback(XmNmaxTextCallback, this, (Callback)&SolFinger::clear, NULL);

    ar.reset();
    hostName.create(this, _T("Host"), ar);
    hostName.setFont(font);

    ar.reset();
    userName.create(this, _T("User"), ar);
    userName.setFont(font);

    ar.reset();
    hostField.create(this, NULL,   ar);
    hostField.setFont(font);
    hostField.limitText(255);

    ar.reset();
    userField.create(this, NULL,   ar);
    userField.setFont(font);
    userField.limitText(255);

    ar.reset();
    query.create(this, _T("Query"),   ar);
    query.setFont(font);
    query.addCallback(XmNactivateCallback, this, 
      (Callback)&SolFinger::doQuery, NULL);

    addCallback(XmNmenuCallback, IDM_CLEAR, this, 
      (Callback)&SolFinger::clear, NULL);
    addCallback(XmNmenuCallback, IDM_EXIT, this, 
      (Callback)&SolFinger::exit, NULL);

    addEventHandler(WM_CLOSE, this, 
      (Handler)&SolFinger::close, NULL);
    addEventHandler(WM_PAINT, this, 
      (Handler)&SolFinger::paint, NULL);

    restorePlacement();
  }

public:
  ~SolFinger() 
  {
    if (sock) {
      delete sock;
    }
  }

private:
  void clear(Action& action) 
  { 
    hostField.clear();
    userField.clear();
    sctext.clear(); 
  }

private:
  long SolFinger::close(Event& event)
  {
    savePlacement();        
    return defaultProc(event);
  }


private:
  void recv()
  {      
    sctext.clear();
    int bytes = 0;
    char buffer[256];

    if(!sock) {
      return;
    }

    while(1) {
      bytes = sock ->recv(buffer, sizeof(buffer)-1, 0);
      if(bytes <= 0) {
        break;
      }  
      buffer[bytes] = ZERO;
      sctext.append(buffer);
    }
  }


private:
  void doQuery(Action& action)
  {
    char host[256];
    host[0] = ZERO;
    hostField .getText(host, sizeof(host));
    char user[256];
    user[0] = ZERO;
    userField.getText(user, sizeof(user));
    if(strlen(host) == 0 || strlen(user) == 0) {
      showErrorDialog("SolFinger", "Please enter a host name and a user name!",
        MB_ICONERROR|MB_OK);

      return;
    }

    sock = new SocketStream(AF_INET, 0);
  
    // Support a simple query command for finger.
    try {
      InetAddress address(host, IPPORT_FINGER);

      if (sock -> connect(address) == SOCKET_ERROR) {
      //2008/07/04
    
        char message[256];
        sprintf_s(message, sizeof(message), 
          "Could not connect to the host=%s: LastError=0x%x",
          host, WSAGetLastError());
        throw Exception(message,  WSAGetLastError()); 
      }

      StringBufferT<char> sb;
      // Set a verbose mode.
      const char* verbose = "/W ";
      if (strstr(user, verbose) == NULL) {
        sb.append(verbose);
      }
      // Send a user-name with "\r\n".
      sb.append(user);
      sb.append("\r\n");

      sock -> sendAll(sb);

      // Read a reply from server.
      recv();

    } catch (Exception& ex) {
      ex.printf();
      sctext.clear();
      showMessageDialog("Error", ex.getString(), MB_ICONERROR|MB_OK);
    }

    if (sock) {
      sock -> shutdown(2);
      delete sock;
      sock = NULL;
    }
  }

private:
  long paint(Event& event) 
  {
    PaintDC dc(this);
    hostName.draw(&dc);
    userName.draw(&dc);

    return 0L;
  }

private:
  long size(Event& event)
  {
    LPARAM pos = event.getLParam();
    int h = HIWORD(pos);
    int w = LOWORD(pos);
    int y =  textHeight+8;
    int lw = textHeight*2;
    int top = 4;
    int m   = 4;
    int ww, hh;
    query.getSize(ww, hh);

    hostName .setLocation(m, top);
    hostField.reshape(m*2+lw, top, w-m*2-lw-ww-m, y);
    userName .setLocation(m, top+y+10);
    userField.reshape(m*2+lw, top+y+10, w-m*2-lw-ww-m, y);
    query    .reshape(w-ww-m, top, ww, y+2); 

    sctext   .reshape(m*2+lw, top+y+y*2, w-m*2-lw, h-top-y-y*2-m);

    return 0L;
  }
};

}

// SolFinger Main
void  Main(int argc, TCHAR** argv)
{
  ModuleFileName module(argv[0]);
  const TCHAR* name = module.getFileName();
  try {
    Application applet(name, argc, argv);

    Args args;
    args.set(XmNclassName, name);
    args.set(XmNexStyle, (ulong)WS_EX_CLIENTEDGE);
    args.set(XmNbackground, (ulong)(COLOR_BTNFACE+1));
    args.set(XmNexStyle, (ulong)WS_EX_CONTROLPARENT);

    SolFinger solFinger(applet, name, args);
    solFinger.realize();
    applet.run();  

  } catch (Exception& ex) {
    caught(ex);
  } catch (...) {
    caught(UnknownException());
  }
}



Last modified: 1 Feb 2017

Copyright (c) 2017 Antillia.com ALL RIGHTS RESERVED.