SOL9 Sample: SolZipFileViewer

SOL9 2.0 Samples

1 Screenshot


2 Source code

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



// SOL9
// 2008/09/05
// 2008/09/16 Modified to use restoreFileFolder/saveFileFolder
// 2008/12/18 Modified to show FileVersion.

#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/DropTarget.h>

#include "SolZipFileParser.h"

#include "resource.h"

namespace SOL {

/**
 * SolZipFileViewer class is a sample Appplication program to display files data of a zip file 
 * (*.zip, *.jar, *.docx, *.xlsx, *.pptx) by using SOL::ListView class
 */
class SolZipFileViewer :public ApplicationView {

private:
  ListView   listv;
  SystemImageList  imglist;

  StatusBar  statusbar;

  FileDialog fileDialog;

  DWORD parserThreadId;

  FileVersionDialog fileVersion;
  CDropTarget  dropTarget;

public:
  /**
   * Constructor
   */
  SolZipFileViewer(Application& applet, const TCHAR* name, Args& args)
  :ApplicationView(applet, name, 
      args.set(XmNbackground, (ulong)(COLOR_BTNFACE+1)))
    
  {
    //Create a window of SOL::HexView.
    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, NULL, ar);  
    listv.setImageList(&imglist, LVSIL_SMALL);

    ListViewColumn items[] = {
      {_T("Filename"),   LVCFMT_LEFT, 440},
      {_T("Enc"),      LVCFMT_CENTER,  50},
      {_T("LastModifed"), LVCFMT_LEFT, 110},
      {_T("Type"),    LVCFMT_LEFT, 120},
      {_T("Size"),    LVCFMT_RIGHT, 90},
      {_T("Packed"),    LVCFMT_RIGHT, 90},

    };
    listv.setColumn(items, sizeof(items)/sizeof(items[0]));

    //2009/11/01
    dropTarget.setTarget(listv.getWindow());
    dropTarget.setEventReceiver(getWindow());
    //

    ar.reset();
    statusbar.create(this, NULL, ar);

    ar.reset();
    ar.set(XmNaccessMode, FileDialog::OPEN);
    fileDialog.create(this, NULL, ar);

    //2008/12/18
    fileVersion.create(this);

    addCallback(XmNmenuCallback, IDM_OPEN, this, 
      (Callback)&SolZipFileViewer::open, NULL);
    addCallback(XmNmenuCallback, IDM_EXIT, this, 
      (Callback)&SolZipFileViewer::exit, NULL);
    //2008/12/18
    addCallback(XmNmenuCallback, IDM_VERSION, this, 
      (Callback)&SolZipFileViewer::version, NULL);

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

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

    //2009/11/01
    addEventHandler(WM_SOL_DROPFILES, this, 
      (Handler)&SolZipFileViewer::dropFiles, NULL);
    restorePlacement();
  }



public:
  ~SolZipFileViewer()
  {
  }


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 (parserThreadId>0) {
      PostThreadMessage(parserThreadId, WM_THREAD_CANCEL, 0, 0);
    }
    savePlacement();
    return defaultProc(event);
  }

public:
  /**
   * WM_SOL_DROPFILES eventhandler.
   */
  long dropFiles(Event& event)
  {
    WPARAM wParam = event.getWParam();
    HDROP hDrop = (HDROP)wParam;
    if (hDrop) {
      int n = DragQueryFile(hDrop, 0xffffffff, NULL, 0);
      if (n >0) {

        //Get the first file only
        int i = 0;
        TCHAR path[_MAX_PATH];
        DragQueryFile(hDrop, i, path, CountOf(path));
      
        DWORD attr = GetFileAttributes(path);
        if (!(attr & FILE_ATTRIBUTE_DIRECTORY)) {

          parse(path);
        }
      }
    }
    return 0;
  }

private:
  void parse(const TCHAR* fileName)
  {
    this->listv.clear();

    SolZipFileParser* parser = new SolZipFileParser(this->listv, this->imglist);
      this->parserThreadId = 0; 
      try {
        if (parser->open(fileName) ==0) {
          TCHAR caption[1024];
          _stprintf_s(caption, CountOf(caption), _T("%s - SolZipFileViewer"), fileName);
          this->setText(caption);

          this->statusbar.setText(fileName);
          this->parserThreadId = parser->getThreadId();

          parser->start();
        // Deleted automatically;
        } else {
          delete parser;
          parser = NULL;
        }
      } catch (int ex) {
        TCHAR message[1024];
        TCHAR* error = _T("Unknown");
        if (ex == ERR_ZS_SIGNATURE) {
          error = _T("No Zip Signature");
        }
        if (ex == ERR_ZS_OPEN) {
          error = _T("Zip Open Error");
        }

        _stprintf_s(message, CountOf(message), _T("Failed to open a file=%s\r\nException:%s"),
          fileName, error);
        showMessageDialog(_T("SolZipFileViewer"),  message,  MB_OK|MB_ICONHAND);
        delete parser;
      }
  }

public:
  /**
   *Show a dialgo of FileDialog, select a file.
   *Create a thread of SolZipFileParser and run it.
   */
  void open(Action& action)
  {
    TCHAR dir[_MAX_PATH];
    if (restoreFileFolder(dir, CountOf(dir))) {
      Args ar;
      ar.set(XmNdirectory, dir);
      fileDialog.setValues(ar);
    }

    int rc = fileDialog.open();
    if (rc == IDOK) {

      const TCHAR* fileName = fileDialog.getFileName();
      saveFileFolder(fileName);

      //Create a thread of SolZipFileParser with a referense oflistv to display files.
      parse(fileName);
      /*

      SolZipFileParser* parser = new SolZipFileParser(this->listv, this->imglist);
      this->parserThreadId = 0; 
      try {
        if (parser->open(fileName) ==0) {
          TCHAR caption[1024];
          _stprintf_s(caption, CountOf(caption), _T("%s - SolZipFileViewer"), fileName);
          this->setText(caption);

          this->statusbar.setText(fileName);
          this->parserThreadId = parser->getThreadId();

          parser->start();
        // Deleted automatically;
        } else {
          delete parser;
          parser = NULL;
        }
      } catch (int ex) {
        TCHAR message[1024];
        TCHAR* error = _T("Unknown");
        if (ex == ERR_ZS_SIGNATURE) {
          error = _T("No Zip Signature");
        }
        if (ex == ERR_ZS_OPEN) {
          error = _T("Zip Open Error");
        }

        _stprintf_s(message, CountOf(message), _T("Failed to open a file=%s\r\nException:%s"),
          fileName, error);
        showMessageDialog(_T("SolZipFileViewer"),  message,  MB_OK|MB_ICONHAND);
        delete parser;
      }
      */
    }
  }

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

};

}

//  Program entry point.
void  Main(int argc, TCHAR** argv)
{
  OleInitialize(NULL);

  ModuleFileName module(argv[0]);
  const TCHAR* name = module.getFileName();

  try {
    Application applet(name, argc, argv);

    Args args;
  
    SolZipFileViewer viewer(applet, name, args);
    viewer.realize();

    applet.run();


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

}


Last modified: 1 Feb 2017

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