SOL9 Sample: SolKnownFolderManager

SOL9 2.0 Samples

1 Screenshot


2 Source code

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


// SOL9
// 2012/11/05
// 2017/0/29 Updated listUp method to break when caught an exception.

#define COMMONCONTROLS_V6

#include <sol\ApplicationView.h>
#include <sol\TreeView.h>
#include <sol\Guid.h>
#include <shellapi.h>
#include <sol\shell\KnownFolderManager.h>
#include <sol\shell\KnownFolder.h>
#include <sol\shell\ShellItem.h>
#include <sol\SplitPane.h>
#include <sol\PropertyView.h>
#include <sol\ImageList.h>
#include <sol\ole\OleInitializer.h>

#include "resource.h"

namespace SOL {

#define WM_LISTUP  (WM_USER+2013)

// SolKnownFolderManager UI application.

class SolKnownFolderManager :public ApplicationView {

private:
  SplitPane panedw;
  TreeView  treev;
  PropertyView  listv;

  ImageList imageList;
  int  driveIconIndex;
  
  ImageList itemImageList;
  int itemIconIndex;

  SOL::KnownFolderManager  manager;
  KNOWNFOLDERID* pKFId;
  UINT         count;

public:
  SolKnownFolderManager(Application& applet, const TCHAR* name, Args& args)
  :ApplicationView(applet, name, args),
    pKFId(NULL),
    count(0)
  {
    Args ar;
    ar.set(XmNdirection, SplitPane::HORIZONTAL);
    panedw.create(this, NULL, ar);
    add(panedw);

    ar.reset();
    ar.set(XmNexStyle, (ulong)WS_EX_CLIENTEDGE);
    ar.set(XmNstyle, (ulong)TVS_HASBUTTONS//|TVS_HASLINES
      |TVS_LINESATROOT
      |TVS_SHOWSELALWAYS );
    treev.create(&panedw, NULL, ar);
    panedw.add(&treev);

    treev.addCallback(XmNselChangedCallback, this, 
      (Callback)&SolKnownFolderManager::selChanged, NULL);
    HINSTANCE hInst = getInstanceHandle();
    addIconToTreeView(hInst);

    ar.reset();
    listv.create(&panedw, NULL, ar);
    panedw.add(&listv);
    listv.addIcon(IDI_ITEM);

    addEventHandler(WM_LISTUP, this,
      (Handler)&SolKnownFolderManager::listUp, NULL);

    addCallback(XmNmenuCallback, ID_EXIT, this,
      (Callback)&SolKnownFolderManager::exit, NULL);
    
    addCallback(XmNmenuCallback, ID_VERSION, this,
      (Callback)&SolKnownFolderManager::version, NULL);

    pKFId = NULL;
    count = 0;
    try {
      manager.getFolderIds(&pKFId, &count);
    } catch (...) {
      caught(UnknownException());
    }

    panedw.setSashPosition(240);
    restorePlacement();

    post(WM_LISTUP);
  }


public:
  ~SolKnownFolderManager()
  {
    if (pKFId) {
      CoTaskMemFree(pKFId);
      pKFId = NULL;
    }
  }

private:
  void addIconToTreeView(HINSTANCE hInst)
  {
    this->imageList.create(16, 16, ILC_COLOR32|ILC_MASK, 10, 5);
    HICON hIcon1 = (HICON)LoadIcon(hInst, MAKEINTRESOURCE(IDI_FOLDER));
    this->driveIconIndex = imageList.addIcon(hIcon1);

    HIMAGELIST hImageList = imageList.getImageList();
    treev.setImageList(hImageList, TVSIL_STATE);
    treev.setImageList(hImageList, TVSIL_NORMAL);      
  }



private:
  void version(Action& action)
  {
    showMessageDialog(_T("Version"),
      _T("SolKnownFolderManager Version 1.0.0.0\r\nCopyright(C) 2012 Antillia.com"),
      MB_OK|MB_ICONINFORMATION);
  }


private:
  void selChanged(Action& action)
  {
    Event event = action.getEvent();
    NM_TREEVIEW* nmtr  = (NM_TREEVIEW*)event.getLParam();
    TV_ITEM tvItem     = nmtr -> itemNew;
    HTREEITEM hselItem = tvItem.hItem;
    if (treev.isLeafNode(hselItem)) {
      _bstr_t string;
      LONG index = 0;
      if (treev.getItemText(hselItem, string, index)) {
        if (index>=0 && index<(LONG)count) {
          listv.clear();
          SOL::KnownFolder folder = manager.getFolder(pKFId[index]);
          Guid guid;
          _bstr_t fid = guid.toString(pKFId[index]);
          KNOWNFOLDER_DEFINITION def;
          folder.getFolderDefinition(&def);
          listv.addLast(_T("Name"), (const TCHAR*)_bstr_t(def.pszName));
          listv.addLast(_T("Description"), (const TCHAR*)_bstr_t(def.pszDescription));
          listv.addLast(_T("KnownFId"), (const TCHAR*)fid);
          listv.addLast(_T("ParentKnownFId"), (const TCHAR*)guid.toString(def.fidParent));

          listv.addLast(_T("RelativePath"), (const TCHAR*)_bstr_t(def.pszRelativePath));
          listv.addLast(_T("ParsingName"),   (const TCHAR*)_bstr_t(def.pszParsingName));
          listv.addLast(_T("Tooltip"),  (const TCHAR*)_bstr_t(def.pszTooltip));
          listv.addLast(_T("LocalizedName"), (const TCHAR*)_bstr_t(def.pszLocalizedName));
          listv.addLast(_T("Icon"), (const TCHAR*)_bstr_t(def.pszIcon));
          listv.addLast(_T("Security"), (const TCHAR*)_bstr_t(def.pszSecurity));
          FileFlagsAndAttributes flagsAndAttributes;
          _bstr_t attrs = flagsAndAttributes.toString(def.dwAttributes);
          listv.addLast(_T("FileFlagsAndAttributes"), (const TCHAR*)attrs);
          _bstr_t dflags = folder.toString(def.kfdFlags);
          listv.addLast(_T("Flags"),  (const TCHAR*)dflags);
          listv.addLast(_T("FileTypeId"), (const TCHAR*)guid.toString(def.ftidType));
          FreeKnownFolderDefinitionFields(&def);

          TCHAR title[MAX_PATH*2];
          _stprintf_s(title, CountOf(title), _T("%s - SolKnownFolderManager"), 
                    (const TCHAR*)string);
        
          setText(title);
        }
      }
    } else {
      listv.clear();
      setText(_T("SolKnownFolderManager"));
    }
  }

private:
  long  listUp(Event& event)
  {
    treev.deleteAllItems();
    HTREEITEM root = treev.addItem(NULL, TVI_ROOT, _T("KnownFolder"), driveIconIndex, driveIconIndex);

    try {  
      for(UINT i = 0; i<count; i++) {
        Guid guid;
        _bstr_t fid = guid.toString(pKFId[i]);
        try {
          SOL::KnownFolder folder = manager.getFolder(pKFId[i]);
          
          KNOWNFOLDER_DEFINITION def;
          folder.getFolderDefinition(&def);
          _bstr_t category = folder.toString(def.category);
          HTREEITEM hParent = treev.findDirectChildItem(root, def.category);
          if (hParent == NULL) {
            hParent = treev.addItem(root, (TCHAR*)category, def.category, driveIconIndex, driveIconIndex);
          } 
          try {
            SOL::ShellItem shellItem = folder.getShellItem();
            _bstr_t displayName = shellItem.getDisplayName();

            TCHAR name[1024];
            _stprintf_s(name, CountOf(name), _T("%s  (%s)"), (const TCHAR*)displayName, (const TCHAR*)_bstr_t(def.pszName));
            treev.addItem(hParent, name, i, driveIconIndex, driveIconIndex);
          } catch (...) {
            ;//Ignore
          }
          FreeKnownFolderDefinitionFields(&def);

        } catch (HRESULT hr) {
          ; //Ignore this IException.
          caught(Exception(hr));
          break;
        }
      }
    } catch(...) {
    caught(UnknownException());
    }

    treev.sortChildren(root, TRUE);
    treev.expand(root, TVE_EXPAND);

    return 1;
  }
};

}

//////////////////////////////////////////
// Program Main
void Main(int argc, TCHAR** argv)
{
  OleInitializer intializer;
  ModuleFileName module(argv[0]);
  const TCHAR* name = module.getFileName();
  try {
    Application applet(name, argc, argv);
  
    Args args;
    args.set(XmNwidth, 700);
    args.set(XmNheight, 480);
    SolKnownFolderManager 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.