SOL9 Sample: SolServiceWatcher

SOL9 2.0 Samples

1 Screenshot


2 Source code

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



// SOL9
// 2009/01/23
// 2012/03/13 Modified to use an inner class ServiceListView.

#include <sol\ApplicationView.h>
#include <sol\StatusBar.h>
#include <sol\FileDialog.h>
#include <sol\SystemImageList.h>
#include <sol\ListView.h>
#include <sol\FileVersionDialog.h>
#include <sol\COMInitializer.h>

#include "SolServiceWatcherThread.h"


#include "resource.h"

namespace SOL {

/**
 * SolServiceWatcher class is a sample Appplication program to 
 * display a modified instance properties of a service.
 */
class SolServiceWatcher :public ApplicationView {

private:
  //2012/03/13
  //<InnerClass>
  class ServiceListView :public ListView {
  private:
    SystemImageList  imagelist;
    static const int NAME_WIDTH       = 100;
    static const int DISPLAYNAME_WIDTH= 200;
    static const int STARTMODE_WIDTH  = 80;
    static const int STATE_WIDTH      = 80;
    static const int STATUS_WIDTH     = 80;
    static const int __PATHNAME_WIDTH = 240; 

  public:
    Boolean create(View* parent, const TCHAR* name, Args& ar)
    {
      Boolean rc = ListView::create(parent, name, ar);
      setImageList(&imagelist, LVSIL_SMALL);

      ListViewColumn items[] = {
        {_T("Name"),    LVCFMT_LEFT, NAME_WIDTH},
        {_T("DisplayName"), LVCFMT_LEFT, DISPLAYNAME_WIDTH},
        {_T("PathName"),   LVCFMT_LEFT, __PATHNAME_WIDTH},

        {_T("StartMode"),  LVCFMT_LEFT, STARTMODE_WIDTH},
        {_T("State"),    LVCFMT_LEFT, STATE_WIDTH},
        {_T("Status"),    LVCFMT_LEFT, STATUS_WIDTH},
      };
      setColumn(items, sizeof(items)/sizeof(items[0]));

      return rc;
    }

  public:
    void reshape(int x, int y, int w, int h)
    {
      ListView::reshape(x, y, w, h);
      int cw, ch;
      getClientSize(cw, ch);

      int PATHNAME_WIDTH = cw - NAME_WIDTH - DISPLAYNAME_WIDTH -
        STARTMODE_WIDTH - STATE_WIDTH - STATUS_WIDTH;
      if (PATHNAME_WIDTH < __PATHNAME_WIDTH) {
        PATHNAME_WIDTH = __PATHNAME_WIDTH;
      }
      setColumnWidth(0, NAME_WIDTH);
      setColumnWidth(1, DISPLAYNAME_WIDTH);
      setColumnWidth(2, PATHNAME_WIDTH);
      setColumnWidth(3, STARTMODE_WIDTH);
      setColumnWidth(4, STATE_WIDTH);
      setColumnWidth(5, STATUS_WIDTH);
    }
  };
  //</InnerClass>
  //---------------------------------------------------------

private:
  ServiceListView   listv;
  SystemImageList  imagelist;

  StatusBar  statusbar;

  FileDialog fileDialog;

  DWORD watcherThreadId;

  FileVersionDialog fileVersion;

public:
  /**
   * Constructor
   */
  SolServiceWatcher(Application& applet, const TCHAR* name, Args& args)
  :ApplicationView(applet, name, 
      args.set(XmNbackground, (ulong)(COLOR_BTNFACE+1)))
  {
    //Create a window of SOL::ListView.
    Args ar;
    ar.reset();
    ar.set(XmNexStyle, (ulong)WS_EX_CLIENTEDGE);
    ar.set(XmNstyle, (ulong)LVS_REPORT//|LVS_SORTASCENDING
          |LVS_NOLABELWRAP|LVS_SHOWSELALWAYS
          |LVS_SHAREIMAGELISTS);
    listv.create(this, _T(""), ar);  

    this->watcherThreadId = 0;

    ar.reset();
    statusbar.create(this, _T(""), ar);
    statusbar.setText(_T("SolServiceWatcher is ready! Please select [Start] item from Watcher menu."));
  
    ar.reset();
    ar.set(XmNaccessMode, FileDialog::OPEN);
    fileDialog.create(this, _T(""), ar);

    fileVersion.create(this);

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

    addCallback(XmNmenuCallback, ID_WATCHER_START, this, 
      (Callback)&SolServiceWatcher::start, NULL);
    addCallback(XmNmenuCallback, ID_WATCHER_STOP, this, 
      (Callback)&SolServiceWatcher::stop, NULL);

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

    addEventHandler(WM_SIZE, this, 
      (Handler)&SolServiceWatcher::size, NULL);

    addEventHandler(WM_CLOSE, this, 
      (Handler)&SolServiceWatcher::close, NULL);

    HICON hIcon = loadIcon(IDI_SOLSERVICEWATCHER);
    send(WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
    send(WM_SETICON, ICON_BIG, (LPARAM)hIcon);

    restorePlacement();
  }


public:
  ~SolServiceWatcher()
  {
  }


public:
  long size(Event& event)
  {
    LPARAM l = event.getLParam();
    
    statusbar.send(WM_SIZE, event.getWParam(), event.getLParam());
    int w, h;
    statusbar.getSize(w, h);
    
    listv.reshape(0, 0, w, HIWORD(l) - h);

    return 0;
  }

public:
  long close(Event& event)
  {
    if (watcherThreadId>0) {
      PostThreadMessage(watcherThreadId, WM_THREAD_CANCEL, 0, 0);
    }
    savePlacement();
    return defaultProc(event);
  }

public:
  /**
   * Clear Listview
   */
  void clear(Action& action)
  {
    listv.clear();
  }

public:
  /**
   *Create a thread of SolProcessCreationWatcher and run it.
   */
  void start(Action& action)
  {
    listv.clear();

    SolServiceWatcherThread* thread = new SolServiceWatcherThread(listv, imagelist);
    this->watcherThreadId = thread->getThreadId();
    thread->start();
    statusbar.setText(_T("Started a ServiceWatcher Thread."));
  }

public:
  /**
   *Stop a thread of SolProcessCreationWatcher.
   */
  void stop(Action& action)
  {
    if (watcherThreadId !=0) {
      PostThreadMessage(watcherThreadId, WM_THREAD_CANCEL, 0, 0);
      watcherThreadId = 0;
      statusbar.setText(_T("Stopped"));

    }
  }


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

};

}

//  Program entry point.
void  Main(int argc, TCHAR** argv)
{
  COMInitializer comInitializer(COINIT_MULTITHREADED);
  ModuleFileName module(argv[0]);
  const TCHAR* name = module.getFileName();

  try {
    //
    comInitializer.setDefaultSecurityLevels();

    Application applet(name, argc, argv);

    Args args;
  
    SolServiceWatcher watcher(applet, name, args);
    watcher.realize();

    applet.run();

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


Last modified: 1 Feb 2017

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