SOL9 Sample: SolWMPLibraryViewer

SOL9 2.0 Samples

1 Screenshot


2 Source code

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



// SOL9
// 2011/12/12 This is an elementary sample program to explore WMPLibrary for Windows Media
// by using SOL::WMPPlayer, SOL::WMPLibraryServices, SOL::WMPMedia.


#include <sol\ApplicationView.h>
#include <sol\StatusBar.h>
#include <sol\TreeView.h>
#include <sol\PropertyView.h>
#include <sol\SmartPtr.h>
#include <sol\DropTarget.h>
#include <sol\ImageList.h>
#include <sol\FileVersionDialog.h>

#include <sol/COMInitializer.h>
#include <sol/Locale.h>

#include <sol/wmp/WMPPlayer.h>
#include <sol/wmp/WMPLibrary.h>
#include <sol/wmp/WMPSettings.h>

#include <sol/wmp/WMPLibraryServices.h>
#include <sol/wmp/WMPMediaCollection.h>
#include <sol/wmp/WMPPlaylist.h>
#include <sol/wmp/WMPMedia.h>
#include <sol/wmp/WMPQuery.h>
#include <sol/wmp/WMPControls.h>

#include "SolWMPMedia.h"
#include "resource.h"

namespace SOL {

/**
 * SolWMPLibraryViewer class
 */
class SolWMPLibraryViewer :public ApplicationView {
private:
  TreeView  treev;
  PropertyView  propv;
  StatusBar  statusbar;
  FileVersionDialog fileVersion;
  
  WMPPlayer player;
  WMPLibraryServices services;
  WMPLibraryType     libraryType;
  const TCHAR*       libraryName;

  ImageList imageList;
  int  libIconIndex;
  int itemIconIndex;  

public:
  /**
   * Constructor
   */
  SolWMPLibraryViewer(Application& applet, const char* name, Args& args)
  :ApplicationView(applet, name, 
      args.set(XmNbackground, (ulong)(COLOR_BTNFACE+1))  ),
    libraryType(wmpltUnknown),
    libraryName(_T("")),
    libIconIndex(0),
    itemIconIndex(0)
  {
    Args ar;

    ar.reset();
    ar.set(XmNexStyle, (ulong)WS_EX_CLIENTEDGE);
    ar.set(XmNstyle, (ulong)(TVS_HASBUTTONS|TVS_SHOWSELALWAYS|
      TVS_HASLINES|TVS_LINESATROOT));
    treev.create(this, "", ar);
    
    ar.reset();
    ar.set(XmNexStyle, (ulong)WS_EX_CLIENTEDGE);
    ar.set(XmNstyle,  (ulong)WS_BORDER|LVS_REPORT);
    propv.create(this, "", ar);  
    propv.setBkColor(RGB(240,240,255));
    ar.reset();
    statusbar.create(this, "", ar);
    statusbar.setText("Please select a library type from [WMP Library] menu");
    treev.addCallback(XmNselChangedCallback, this, 
      (Callback)&SolWMPLibraryViewer::selChanged, NULL);

    fileVersion.create(this);

    addCallback(XmNmenuCallback, IDM_LOCAL_LIBRARY, this, 
        (Callback)&SolWMPLibraryViewer::localLibrary, NULL);
    addCallback(XmNmenuCallback, IDM_DISC_LIBRARY, this, 
        (Callback)&SolWMPLibraryViewer::discLibrary, NULL);
    addCallback(XmNmenuCallback, IDM_REMOTE_LIBRARY, this, 
        (Callback)&SolWMPLibraryViewer::remoteLibrary, NULL);
    addCallback(XmNmenuCallback, IDM_PORTABLE_DEVICE_LIBRARY, this, 
        (Callback)&SolWMPLibraryViewer::portableDeviceLibrary, NULL);

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

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

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

    addEventHandler(WM_CLOSE, this, 
        (Handler)&SolWMPLibraryViewer::close, NULL);
  
    HINSTANCE hInst = getInstanceHandle();
    HICON hIcon = LoadIcon(hInst, MAKEINTRESOURCE(IDI_SOL));
    send(WM_SETICON, ICON_SMALL, (LPARAM)hIcon);
    send(WM_SETICON, ICON_BIG, (LPARAM)hIcon);

    services =player.getLibraryServices();
    WMPSettings settings  = player.getSettings();
    //settings.requestMediaAccessRights(_bstr_t("read"));
    createImageList(hInst);

    restorePlacement();
  }

private:
  void createImageList(HINSTANCE hInst)
  {
    this->imageList.create(16, 16, ILC_COLOR32|ILC_MASK, 10, 5);
    //this->imageList.create(16, 16, ILC_COLORDDB |ILC_MASK , 10, 5);

    HICON hIcon1 = (HICON)LoadIcon(hInst, MAKEINTRESOURCE(IDI_LIBRARY));
    this->libIconIndex = imageList.addIcon(hIcon1);
    HICON hIcon2 = (HICON)LoadIcon(hInst, MAKEINTRESOURCE(IDI_ITEM));
    this->itemIconIndex = imageList.addIcon(hIcon2);
    HIMAGELIST hImageList = imageList.getImageList();
    //treev.setImageList(hImageList, TVSIL_STATE);
    treev.setImageList(hImageList, TVSIL_NORMAL);      
  }

public:
  /**
   * WM_SIZE eventhandler.
   */
  long size(Event& event)
  {
    LPARAM l = event.getLParam();
    
    statusbar.send(WM_SIZE, event.getWParam(), event.getLParam());
    int w, h;
    statusbar.getSize(w, h);
    treev.reshape(0, 0, w*1/3-1,  HIWORD(l) - h);
    propv.reshape(w/3, 0, w*2/3-2, HIWORD(l) - h);
    
    return 0;
  }

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

private:
  void showLibrary(WMPLibraryType type)
  {
    this->libraryType = type;

    treev.clear();
    propv.clear();

    TCHAR string[1024*2];
    _bstr_t mediaTypes[] = {_bstr_t("audio"), _bstr_t("video"), _bstr_t("photo"),
        _bstr_t("playlist"), _bstr_t("other") };

    long lcount = services.getCountByType(libraryType);

    for (long n = 0; n<lcount; n++) {
      _stprintf_s(string, CountOf(string), _T("%s(%d)"),libraryName, n);  
      HTREEITEM root = treev.addItem(NULL, TVI_ROOT, string, n,  libIconIndex, libIconIndex);
      
      WMPLibrary library =services.getLibraryByType(libraryType, n);
      WMPMediaCollection mediaCollection = library.getMediaCollection();
      for (int i = 0; i<CountOf(mediaTypes); i++) {
        showLibrary(type, root, n, mediaCollection, mediaTypes[i] );
      }
    }
    treev.expandAllChildren();
  }

private:
  void showLibrary(WMPLibraryType type, HTREEITEM top, int n,
    WMPMediaCollection& mediaCollection,
    const _bstr_t mediaType)
  {
    TCHAR caption[1024];
    _stprintf_s(caption, CountOf(caption), _T("%s - SolWMPLibraryViewer"),libraryName);
    setText(caption);

    long lcount = services.getCountByType(libraryType);
    
    TCHAR string[1024];

    HTREEITEM root = treev.addItem(top, (HTREEITEM)TVI_LAST, 
        (TCHAR*)(const TCHAR*)mediaType, n,  libIconIndex, libIconIndex);

    IWMPQueryPtr pQuery =mediaCollection.createQuery();
    WMPPlaylist playlist = mediaCollection.getPlaylistByQuery(
        pQuery, mediaType, _bstr_t("Name"), VARIANT_TRUE);

    long count = playlist.getCount();

    HTREEITEM item = (HTREEITEM)TVI_LAST;
    HTREEITEM next = NULL;
    for (long i = 0; i<count; i++) {
      WMPMedia media = playlist.getItem(i);

      _bstr_t name = media.getName();
      _stprintf_s(string, CountOf(string), _T("%s"), (const TCHAR*)name);
      treev.addItem(root, item, string, i, itemIconIndex, itemIconIndex);
    }
  }

public:
  /**
   * File menu [LocalLibrary] callback.
   */
  void localLibrary(Action& action)
  {
    libraryName = _T("Local Library");
    showLibrary(wmpltLocal);
  }

public:
  /**
   * File menu [Disc Library] callback.
   */
  void discLibrary(Action& action)
  {
    libraryName = _T("Disc Library");
    showLibrary(wmpltDisc);
  }

public:
  /**
   * File menu [Remote Library] callback.
   */
  void remoteLibrary(Action& action)
  {
    libraryName = _T("Remote Library");
    showLibrary(wmpltRemote);
  }

public:
  /**
   * File menu [Remote Library] callback.
   */
  void portableDeviceLibrary(Action& action)
  {
    libraryName = _T("Portable Device Library");
    showLibrary(wmpltPortableDevice);
  }


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

public:
  /**
   * Callback for a selection-change event in a treeView. 
   */
  void selChanged(Action& action)
  {
    try {
      propv.clear();

      Event& event = action.getEvent();
      NM_TREEVIEW* nmtr  = (NM_TREEVIEW*)event.getLParam();
  
      TV_ITEM tvItem     = nmtr -> itemNew;
      HTREEITEM hItem = tvItem.hItem; 

      if (treev.isLeafNode(hItem)) {
        //A leaf node has been selected.
        long mediaIndex = tvItem.lParam;
      
        TV_ITEM parentItem;
        if (treev.getParentItem(hItem, parentItem)) {
          long libIndex = parentItem.lParam;
          long param = 0;
          _bstr_t mediaType;
          treev.getItemText(parentItem.hItem, mediaType, param);

          WMPLibrary library = services.getLibraryByType(libraryType, libIndex);
          WMPMediaCollection mediaCollection = library.getMediaCollection();

          IWMPQueryPtr pQuery =mediaCollection.createQuery();
          //Sort by Name attribute
          WMPPlaylist playlist = mediaCollection.getPlaylistByQuery (
            pQuery, mediaType, _bstr_t("Name"), VARIANT_TRUE);
          if (playlist.getCount()>0) {
            SolWMPMedia media = playlist.getItem(mediaIndex);
            media.showProperties(propv);
          }
        }
      }
    } catch(...) {
    
    }
  }
};

}


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

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

    Args args;
  
    SolWMPLibraryViewer 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.