// ViewersDoc.cpp : implementation file
//

#include "stdafx.h"
#include "xpeluc01.h"
#include "ViewersDoc.h"
#include "..\\global\\iviewer.h"


// ViewersDoc

IMPLEMENT_DYNCREATE(CViewersDoc, CDocument)

CViewersDoc::CViewersDoc()
{
}

BOOL CViewersDoc::OnNewDocument()
{
	if (!CDocument::OnNewDocument())
		return FALSE;

	HANDLE hFile;
	WIN32_FIND_DATA findData;
	hFile = FindFirstFile(L"v*.dll", &findData);
	if ( hFile )
	{
		do
		{
			HMODULE hModule = LoadLibrary(findData.cFileName);
			if ( hModule )
			{	// ano - podarilo se nahrat knihovnu
				CREATEPROC cp = (CREATEPROC)GetProcAddress(hModule, "CreateViewerInstance");
				if ( cp )
				{
					IViewer *ifcViewer;
					if ( S_OK == cp(IID_IViewer, (void**)&ifcViewer) )
					{
						VIEWERITEM vi;
						ifcViewer->GetName(vi.strName);
						ifcViewer->GetDescription(vi.strDescription);
						vi.cp = cp;
						vi.hModule = hModule;
						ifcViewer->Release();
						m_mapViewers.insert(std::pair<CString, VIEWERITEM>(vi.strName, vi));
					}
				}
				else
				{
					FreeLibrary(hModule);
				}
			}
		}while (FindNextFile(hFile, &findData));
	}

	return TRUE;
}

CViewersDoc::~CViewersDoc()
{
	std::map<CString, VIEWERITEM>::iterator it;
	for ( it = m_mapViewers.begin(); it != m_mapViewers.end(); it++ )
	{
		FreeLibrary(it->second.hModule);
	}
}


BEGIN_MESSAGE_MAP(CViewersDoc, CDocument)
//	ON_COMMAND(ID_PROCESS_START, &CViewersDoc::OnProcessStart)
//	ON_UPDATE_COMMAND_UI(ID_PROCESS_START, &CViewersDoc::OnUpdateProcessStart)
END_MESSAGE_MAP()


// ViewersDoc diagnostics

#ifdef _DEBUG
void CViewersDoc::AssertValid() const
{
	CDocument::AssertValid();
}

#ifndef _WIN32_WCE
void CViewersDoc::Dump(CDumpContext& dc) const
{
	CDocument::Dump(dc);
}
#endif
#endif //_DEBUG

#ifndef _WIN32_WCE
// ViewersDoc serialization

void CViewersDoc::Serialize(CArchive& ar)
{
	if (ar.IsStoring())
	{
		// TODO: add storing code here
	}
	else
	{
		// TODO: add loading code here
	}
}
#endif


// ViewersDoc commands

//void CViewersDoc::OnProcessStart()
//{
//	// TODO: Add your command handler code here
//}

//void CViewersDoc::OnUpdateProcessStart(CCmdUI *pCmdUI)
//{
//	// TODO: Add your command update UI handler code here
//}
