SOL9 Sample: DiskSpaceViewer

SOL9 2.0 Samples

1 Screenshot


2 Source code

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



// SOL++2000
// 2015/12/13 Modifed to fix a bug.
// Note: The method DiskSpaceViewer::getOSName based on GetVersionEx Windows API will return a bad value
// on Windows 8.1.
 
//#define _NT_TARGET_VERSION $(_NT_TARGET_VERSION_LATEST)
 
#include <sol\ApplicationView.h>
#include <sol\ListView.h>
#include <sol\Profile.h>
#include <sol\MessageFont.h>
#include <sol\Stdio.h>
#include <sol\DiskSpace.h>
#include <sol\StringList.h>
#include "resource.h"

namespace SOL {


class DiskSpaceViewer :public ApplicationView {
private:
  Profile    profile;
  MessageFont    font;
  ListView  listView;

  DiskSpace  diskSpace;
/*
*/

public:
  DiskSpaceViewer(Application& applet, const TCHAR* name, Args& args)
  :ApplicationView(applet, name, args)
  {
    Args ar;

    font.create(9);

    ar.reset();
    ar.set(XmNexStyle, (ulong)WS_EX_CLIENTEDGE);
    ar.set(XmNstyle, (ulong)LVS_REPORT);
    listView.create(this, _T(""), ar);

    listView.setFont(&font);
    ListViewColumn items[] = {
      {_T("Drive"),LVCFMT_LEFT, 60},
      {_T("Free Space (MB)"), LVCFMT_RIGHT,140},    
      {_T("Total Space (MB)"), LVCFMT_RIGHT, 140},
      {_T("Used Ratio (%)"), LVCFMT_RIGHT, 140},

    };

    listView.setColumn(items, XtNumber(items));
    add(&listView);

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

    restorePlacement();
    showDiskSpace();
    TCHAR caption[256];
    _stprintf_s(caption, CountOf(caption), _T("DiskSpaceViewer on %s"), getOSName());
    setText(caption);
    
    addCallback(XmNmenuCallback, IDM_REFRESH, this,
      (Callback)&DiskSpaceViewer::refresh, NULL);

    addCallback(XmNmenuCallback, IDM_EXIT, this,
      (Callback)&DiskSpaceViewer::exit, NULL);

  }

private:
  const TCHAR* getOSName()
  {
    OSVERSIONINFO ver;
    memset(&ver, 0, sizeof(ver));
    ver.dwOSVersionInfoSize = sizeof(ver);

    GetVersionEx(&ver);
    TCHAR string[128];
    _stprintf_s(string, CountOf(string), _T("Major=%d Minor=%d"), ver.dwMajorVersion, ver.dwMinorVersion);
    MessageBox(NULL, string, _T("Version"), MB_OK);
    
    const TCHAR* name = _T("Unknown");

    if (ver.dwPlatformId ==VER_PLATFORM_WIN32_WINDOWS) {
      if (ver.dwMinorVersion== 0) {
        name = _T("Windows 95");
      } else if (ver.dwMinorVersion==10) {
        name = _T("Windows 98");
      }
    }

    if (ver.dwPlatformId == VER_PLATFORM_WIN32_NT) {
      if (ver.dwMajorVersion==4) {
        name = _T("Windows NT4.0");
      } else if (ver.dwMajorVersion==5) {
        if (ver.dwMinorVersion== 0) {
          name = _T("Windows 2000");
        } else if (ver.dwMinorVersion==1) {
          name = _T("Windows XP");
        }

      } else if (ver.dwMajorVersion==6) {
        if (ver.dwMinorVersion== 0) {
          name = _T("Windows Vista");
        } else if (ver.dwMinorVersion==1) {
          name = _T("Windows 7");
        } else if (ver.dwMinorVersion==2) {
          name = _T("Windows 8"); //2015/12/17
        }
  
      }
    }
    return name;
  }


private:
  void refresh(Action& action)
  {
    listView.deleteAllItems();
    showDiskSpace();
  }

private:
  void showDiskSpace()
  {
    DWORD drives = GetLogicalDrives();
    int n = 0;

    for (int x= 0; x <26; x++) {
      if ((drives & 1) != 0) {
        TCHAR d = 'A' + x;
        TCHAR buff[4];  //2015/12/13 Modified
        _stprintf_s(buff, CountOf(buff), _T("%c:\\"), d);
    
        int type = GetDriveType(buff);
      
        if (type == DRIVE_FIXED) {
          double free  =0.0;
          double total = 0.0;
          diskSpace.get(buff, free, total);
          TCHAR f[80];
          TCHAR t[80];
          TCHAR u[80];

          _stprintf_s(f,  CountOf(f), _T("%6.1f"), free);
          _stprintf_s(t,  CountOf(t), _T("%6.1f"), total);
          double used = total- free;
          _stprintf_s(u, CountOf(u), _T("%6.1f"), used*100.0/total);

          StringList list;
          list.add(new String(buff));
          list.add(new String(f));
          list.add(new String(t));
          list.add(new String(u));
          listView.insertLine(n, &list);
        
          n++;
        }
      }
      drives = drives >>1;
    }
  }

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

};

}

// DiskSpaceViewer 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(XmNwidth, 400);
    args.set(XmNheight, 300);

    DiskSpaceViewer viewer(applet, name, args);
    viewer.realize();
    applet.run();
    
  } catch (Exception& ex) {
    caught(ex);
  } catch (...) {
    caught(UnknownException());
  }
}


Last modified: 1 Feb 2017

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