1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
BOOL CMagicDoc::OnOpenDocument(LPCTSTR lpszPathName)
{
LPBYTE pData = NULL;
BOOL bOK = TRUE;
CWaitCursor wait;
try
{
CFile file;
CFileException fe;
if (!file.Open(lpszPathName, CFile::modeRead, &fe))
{
ReportSaveLoadException(lpszPathName, &fe,
FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
return FALSE;
}
DeleteContents();
SetModifiedFlag(); // dirty during de-serialize
DWORD dwLen = file.GetLength();
pData = new BYTE[dwLen+5];
DWORD dwRead = file.Read(pData, dwLen);
// check signiture - encrypted?
if (IsEncrypted(pData, dwRead)) // decrypt data
{
// prompt for password
do
{
DPassword dlg(FALSE);
if (dlg.DoModal() != IDOK)
{
delete pData;
SetModifiedFlag(FALSE);
// can not return FALSE, due to bugs in reinit
if (AfxGetMainWnd())
AfxGetMainWnd()->PostMessage(WM_COMMAND, ID_FILE_NEW);
else
((CMagicApp*)AfxGetApp())->m_bReset = TRUE;
return TRUE;
}
m_sPassword = dlg.m_sPassword;
}
while (!DecryptData(pData, dwRead, m_sPassword));
}
else
m_sPassword.Empty();
CMemFile mf;
mf.Attach(pData, dwRead);
CArchive ar(&mf, CArchive::load);
ar.m_pDocument = this;
Serialize(ar); // load me
ar.Close();
mf.Detach();
}
catch (CException* e)
{
ReportSaveLoadException(lpszPathName, e,
FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
e->Delete();
bOK = FALSE;
}
catch(...)
{
ReportSaveLoadException(lpszPathName, NULL,
FALSE, AFX_IDP_FAILED_TO_OPEN_DOC);
bOK = FALSE;
}
delete pData;
if (!bOK)
{
DeleteContents();
return FALSE;
}
SetModifiedFlag(FALSE);
CMagicFrame *wnd = (CMagicFrame*) AfxGetMainWnd();
if( wnd )
{
wnd->UpdateMailCount( 0, 0 );
}
return TRUE;
}
|