SOL9 Sample: SolNetFwProfileViewer

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// 2012/09/27

#define COMMONCONTROLS_V6

#include <sol\ApplicationView.h>
#include <sol\ole\HTMLView.h>
#include <sol\FileVersionDialog.h>

#include <sol\Locale.h>
#include <sol\com\ApartmentThreadedModel.h>
#include <sol\com\MultiThreadedModel.h>

#include <sol\netfw\NetFwMgr.h>
#include <sol\netfw\NetFwPolicy.h>
#include <sol\netfw\NetFwProfile.h>
#include <sol\FileWriter.h>
#include <sol\Locale.h>
#include <sol\WString.h>

#include "resource.h"

namespace SOL {

#define WM_REFRESH (WM_USER+2012)

/**
 * class SolNetFwProfileViewer
 */
class SolNetFwProfileViewer :public ApplicationView {

private:
  HTMLView htmlView;
  FileVersionDialog fileVersion;

public:
  /**
   * Constructor
   */
  SolNetFwProfileViewer(Application& applet, const TCHAR* name, Args& args)
  :ApplicationView(applet, name, 
      args.set(XmNbackground, (ulong)(COLOR_BTNFACE+1))
          .set(XmNstyle, WS_CLIPCHILDREN) )
  {
    Args ar;
    
    ar.set(XmNstyle, WS_CHILD|WS_VISIBLE);
    htmlView.create(this, _T(""), ar);

    fileVersion.create(this);

    addEventHandler(WM_KEYDOWN, this, (Handler)&SolNetFwProfileViewer::keyDown, NULL);

    addCallback(XmNmenuCallback, IDM_REFRESH, this, (Callback)&SolNetFwProfileViewer::refresh, NULL);
    addCallback(XmNmenuCallback, IDM_EXIT, this, (Callback)&SolNetFwProfileViewer::exit, NULL);

    addCallback(XmNmenuCallback, IDM_VERSION, this, (Callback)&SolNetFwProfileViewer::version, NULL);

    addEventHandler(WM_SIZE, this, (Handler)&SolNetFwProfileViewer::size, NULL);
    addEventHandler(WM_CLOSE, this, (Handler)&SolNetFwProfileViewer::close, NULL);
    addEventHandler(WM_BROWSER_DOCUMENTCOMPLETE, this, (Handler)&SolNetFwProfileViewer::complete, NULL);

    addEventHandler(WM_REFRESH, this, (Handler)&SolNetFwProfileViewer::refresh, NULL);

    restorePlacement();
  }

public:
  long refresh(Event& event)
  {
    Action action;
    refresh(action);
    return 0;
  }

public:
  void refresh(Action& action)
  {
    try {

      TCHAR user[_MAX_PATH];
      TCHAR xmlFileName[_MAX_PATH];

      if(GetEnvironmentVariable(_T("UserProfile"), user, CountOf(user))) {
        _stprintf_s(xmlFileName, CountOf(xmlFileName), _T("%s\\NetFwProfile.xml"), user);
      } else {
        throw -1;
      }

      FileWriter writer(xmlFileName);


      writer.write(L"<?xml version='1.0'?>\n");
      writer.write(L"<NetFwApplication name='%s'>\n", L"SolNetFwProfileViewer");

      writer.write(L"<NetFwPolicy>\n");
      SOL::NetFwMgr mgr;    
      SOL::NetFwPolicy  policy = mgr.getLocalPolicy();
    
      writer.write(L"<CurrentProfile>\n");
      NetFwProfile currentProfile = policy.getProfileByType(NET_FW_PROFILE_CURRENT);
      currentProfile.write(writer);
      writer.write(L"</CurrentProfile>\n");

      writer.write(L"<DomainProfile>\n");
      NetFwProfile domainProfile = policy.getProfileByType(NET_FW_PROFILE_DOMAIN);
      domainProfile.write(writer);
      writer.write(L"</DomainProfile>\n");

      writer.write(L"<StandardProfile>\n");
      NetFwProfile standardProfile = policy.getProfileByType(NET_FW_PROFILE_STANDARD);
      standardProfile.write(writer);
      writer.write(L"</StandardProfile>\n");
    
      writer.write(L"</NetFwPolicy>\n");

      writer.write(L"</NetFwApplication>\n");
      writer.close();

      navigate(xmlFileName);
  
    } catch (Exception& ) {
      //ex.printf();  
    } catch (HRESULT ) {
      //_tprintf(_T("Exception: HRESULT=%x\n"), hr);
    }
  }

public:
  /**
   * WM_KEYDOWN event handler.
   */
  long keyDown(Event& event)
  {
    MSG msg;
    msg.message = event.getMessage();
    msg.lParam = event.getLParam();
    msg.wParam = event.getWParam();

    //Call HTMLView::transateAccelator
    htmlView.translateAccelator(&msg);
    return 0;
  }

public:
  /**
   * WM_SIZE event handler.
   */
  long size(Event& event)
  {
    LPARAM l = event.getLParam();
    htmlView.reshape(0, 0, LOWORD(l), HIWORD(l));
    return 0;
  }

public:
  /**
   * Event handler for a user-defined WM_BROWSER_DOCUMENTCOMPLETE from SOL::BrowserEvent class.
   */
  long complete(Event& event)
  {
    //Do nothing here.
    return 0;
  }

public:
  // File menu [Version] callback.
  void version(Action& action)
  {
    fileVersion.popupAt(action);
  }

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

public:
  void navigate(const TCHAR* url) 
  {
    try {
      htmlView.navigate(url);
    
      TCHAR caption[1024];
      _stprintf_s(caption, CountOf(caption), _T("%s - SolNetFwProfileViewer"), (const TCHAR*)url);
      setText(caption);
    } catch (...) {
    }
  }
};

}

//////////////////////////////////////
// SolNetFwProfileViewer
// Program entry point.

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

  try {
    Locale locale;
    ApartmentThreadedModel model;  

    Application applet(name, argc, argv);

    Args args;
    args.set(XmNclassStyle, CS_DBLCLKS);
    SolNetFwProfileViewer browser(applet, name, args);
    browser.realize();

    browser.post(WM_REFRESH);

    applet.run();

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


Last modified: 1 Feb 2017

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