// Addapt.cpp : Defines the class behaviors for the application.
//
#include "stdafx.h"
#include "Addaptapp.h"
#include "AddInChildFrm.h"
#include "FaultFIFOChildFrame.h"
#include "AddaptDoc.h"
#include "AddaptView.h"
#include "AddaptDocTemplate.h"
#include "AddInManager.h"
#include "TextDoc.h"
#include "TextView.h"
#include "DSDoc.h"
#include "DSGridFrm.h"
#include "PWDoc.h"
#include "PWReg.h"
#include "PWAppIO.h"
#include "PWUpdate.h"
#include "PWLogin.h"
#include "PWChgDlg.h"
#include "PWGridFrm.h"
#include "FaultFifoView.h"
#include "FaultFifoDoc.h"
#include "FileNewDlg.h"
#include "CfgDirDlg.h"
#include "ProductCode.h"
#include "Splash.h"
#include "AddaptMDIChildFrame.h"
#include "DriveFrame.h"
#include "Parameters.h"
#include "OptionsPropertySheet.h"
#include <winnetwk.h>
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
// the help file name
//TCHAR* g_szHelpFile = _T("hh \\avtron\\addapt2010\\add_zap\\addapt.chm");
//#define _CRTDBG_MAP_ALLOC
//#include <stdlib.h>
//#include <crtdbg.h>
/////////////////////////////////////////////////////////////////////////////
// CAddaptApp
BEGIN_MESSAGE_MAP(CAddaptApp, CWinApp)
//{{AFX_MSG_MAP(CAddaptApp)
ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
ON_COMMAND(ID_FILE_NEW, OnFileNew)
ON_COMMAND(ID_OPEN_DEFAULTCFG, OnOpenDefaultCfg)
ON_COMMAND(ID_FILE_OPEN, OnFileOpen)
ON_COMMAND(ID_HELP, OnHelp)
//}}AFX_MSG_MAP
ON_COMMAND(ID_FILE_PRINT_SETUP, CWinApp::OnFilePrintSetup)
ON_UPDATE_COMMAND_UI(ID_FILE_NEW, OnEnableFileNew)
ON_UPDATE_COMMAND_UI(ID_EDIT_CUT, OnEnableEditCut)
ON_UPDATE_COMMAND_UI(ID_OPEN_DEFAULTCFG, OnEnableDefaultConfig)
ON_COMMAND(ID_CONFIG_FILES, OnConfigFilesDlg)
ON_UPDATE_COMMAND_UI(ID_CONFIG_FILES, OnEnableConfigFilesDlg)
ON_UPDATE_COMMAND_UI(IDC_ADD_STTN, OnEnableAddSection)
ON_COMMAND(IDC_ADD_STTN, OnAddSection)
ON_UPDATE_COMMAND_UI(IDC_ADD_ROW, OnEnableAddRow)
ON_COMMAND(IDC_ADD_ROW, OnAddRow)
ON_COMMAND(ID_CAL_FILE_NEW, OnCalFileOpen)
ON_UPDATE_COMMAND_UI(ID_CAL_FILE_NEW, OnEnableCalFileOpen)
ON_UPDATE_COMMAND_UI(ID_ACTIVITY, OnCheckActivityIndicator)
ON_COMMAND(ID_ACTIVITY, OnActivityIndicator)
ON_UPDATE_COMMAND_UI(IDC_DRIVEGRID, OnEnableDSGrid)
ON_COMMAND(IDC_DRIVEGRID, OnDSGrid)
ON_UPDATE_COMMAND_UI(IDC_LOGIN, OnEnableLogin)
ON_COMMAND(IDC_LOGIN, OnLogin)
ON_UPDATE_COMMAND_UI(IDC_LOGOUT, OnEnableLogout)
ON_COMMAND(IDC_LOGOUT, OnLogout)
ON_UPDATE_COMMAND_UI(IDC_PRODUCT_CODE, OnEnableProductCode)
ON_COMMAND(IDC_PRODUCT_CODE, OnProductCode)
ON_UPDATE_COMMAND_UI(IDC_CHANGE_PASSWORD, OnEnableChangePassword)
ON_COMMAND(IDC_CHANGE_PASSWORD, OnChangePassword)
ON_UPDATE_COMMAND_UI(IDC_CONFIGURE, OnEnablePasswordConfig)
ON_COMMAND(IDC_CONFIGURE, OnPasswordConfig)
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CAddaptApp construction
CAddaptApp::CAddaptApp() : m_pFaultFIFOTemplate(NULL), m_bGXInitialized(FALSE),
m_bConfigOpen(FALSE), m_bEthernet(FALSE), m_b8024(FALSE),
m_bMonitorActivity(FALSE), m_bDSGridStatus(0),
m_bRestoreWindows(0), m_bCanAdd(0), m_bBlockCommTest(0),
m_bIsWindowsNT4orLater(0), m_bIsWindows98orLater(0),
m_bBeta(0)
{
m_hwndDialog=NULL;
}
/////////////////////////////////////////////////////////////////////////////
// The one and only CAddaptApp object
CAddaptApp theApp;
/////////////////////////////////////////////////////////////////////////////
// CAddaptApp initialization
BOOL CAddaptApp::InitInstance()
{
// _CrtSetBreakAlloc(); // find memory leak
bFirst = true;
OSVERSIONINFOA osvi;
osvi.dwOSVersionInfoSize = sizeof(osvi);
if (GetVersionExA(&osvi))
{
m_bIsWindowsNT4orLater = ( (osvi.dwPlatformId == VER_PLATFORM_WIN32_NT) &
(osvi.dwMajorVersion >= 4) );
m_bIsWindows98orLater = ( (osvi.dwPlatformId == VER_PLATFORM_WIN32_WINDOWS) &
( (osvi.dwMajorVersion > 4) ||
(osvi.dwMajorVersion == 4) & (osvi.dwMinorVersion > 0) ) );
}
//if (m_bIsWindowsNT4orLater)
// CParameters::DisableLAN();
int nOS = CParameters::GetOS();
switch(nOS)
{
case NoEth:
break;
case Win98:
case FieldServ:
m_bEthernet = TRUE;
m_b8024 = TRUE;
break;
case XP:
case Me:
case WinNT:
m_bEthernet = TRUE;
break;
default:
AfxMessageBox(IDS_COULD_NOT_RETRIEVE_REGISTRYVALUE);
}
CCommandLineInfo cmdInfo;
CSplashWnd::EnableSplashScreen(cmdInfo.m_bShowSplash);
AfxEnableControlContainer();
#ifdef _AFXDLL
Enable3dControls(); // Call this when using MFC in a shared DLL
#else
Enable3dControlsStatic(); // Call this when linking to MFC statically
#endif
// BOOL bOk = AfxOleInit(); // start up the ole libraries.
// Change the registry key under which our settings are stored.
// You should modify this string to be something appropriate
// such as the name of your company or organization.
//
LoadStdProfileSettings(); // Load standard INI file options (including MRU)
// Register the application's document templates. Document templates
// serve as the connection between documents, frame windows and views.
//
m_pAddInDocTemplate = new CAddaptDocTemplate( IDR_ADDIN,
RUNTIME_CLASS(CAddaptDoc),
RUNTIME_CLASS(CAddInChildFrame),
RUNTIME_CLASS(CAddaptView));
AddDocTemplate(m_pAddInDocTemplate);
m_pFaultFIFOTemplate = new CAddaptDocTemplate( IDR_FAULTFIFO,
RUNTIME_CLASS(CFaultFIFODoc),
RUNTIME_CLASS(CFaultFIFOChildFrame),
RUNTIME_CLASS(CFaultFIFOView));
AddDocTemplate(m_pFaultFIFOTemplate);
m_pFormTemplate = new CMultiDocTemplate(IDR_MAINFRAME,
RUNTIME_CLASS(CPasswordDoc),
RUNTIME_CLASS(CAddaptMDIChildFrame),
RUNTIME_CLASS(CSecurityFormGridWnd));
AddDocTemplate(m_pFormTemplate);
m_pDriveStatusTemplate = new CMultiDocTemplate( IDR_MAINFRAME,
RUNTIME_CLASS(CDriveStatusDoc),
RUNTIME_CLASS(CDriveFrame),
RUNTIME_CLASS(CDriveStatusFormGridWnd));
AddDocTemplate(m_pDriveStatusTemplate);
m_pCompareTemplate = new CAddaptDocTemplate( IDR_MAINFRAME,
RUNTIME_CLASS(CTextDoc),
RUNTIME_CLASS(CAddaptMDIChildFrame),
RUNTIME_CLASS(CTextView));
AddDocTemplate(m_pCompareTemplate);
// create password obj ptr
m_pPW = (CPassword *) new CPassword;
CPWReg *pReg = (CPWReg *) new CPWReg; // create register entry
m_pPW->SetUserType((_securitylevel)pReg->GetDefaultSecurityLevel());
SetAvailability();
AfxMessageBox("Please Call Field service at 216-642-1230 ext 1214 after 60 days from date of instll to purchase complete software package.");
// read installed add-ins file
HRESULT hr = GetDirectoryFromPath(m_pszHelpFilePath, m_strExeDirectory);
CString strAddInFile = m_strExeDirectory + "addins.bin";
CAddInManager AddInManager(strAddInFile);
AddInManager.ReadAll(m_ordAddIns); // mem 242 leak
// create main MDI Frame window pMainFrame = new CMainFrame;
pMainFrame = new CMainFrame;
if (!pMainFrame->LoadFrame(IDR_MAINFRAME))
{
return FALSE;
}
m_pMainWnd = pMainFrame;
// Parse command line for standard shell commands, DDE, file open
ParseCommandLine(cmdInfo);
// Dispatch commands specified on the command line
if (cmdInfo.m_nShellCommand != CCommandLineInfo::FileNew)
if (!ProcessShellCommand(cmdInfo))
return FALSE;
if( !m_dwSerialNum )
CreateSerialNum();
try
{
m_ADDAPT.Create();
m_ADDAPT.Initialize();
}
catch(CUserException* e)
{
e->Delete();
}
pMainFrame->UpdateLevel(m_pPW->GetUserLevel());
GXInit(); // initialize grid
m_bGXInitialized = TRUE;
CString strConfig;
try
{
strConfig = m_ADDAPT.DefaultCfgFile();
}
catch(CUserException* e)
{ // no message, there is not window yet
e->Delete();
}
if( !strConfig.IsEmpty() )
pMainFrame->UpdateConfig(strConfig);
// get drive status check from registry
if( ReadDSCheck() )
{ // trick msg handler into thinking
// a user selected drv status grid
m_bConfigOpen = FALSE;
OnDSGrid();
pMainFrame->OnSectionStatus();
}
// done with password reg ptr
delete pReg;
m_bMonitorActivity = CParameters::MonitorActivity();
ULONG lLength = MAX_PATH;
CString strValue, strTitle;
CRegKey Key;
LONG lResult = Key.Open(HKEY_LOCAL_MACHINE,"SOFTWARE\\Avtron Manufacturing\\ADDapt\\", KEY_ALL_ACCESS);
if ( lResult == ERROR_SUCCESS )
{
lResult = Key.QueryValue(strValue.GetBuffer(MAX_PATH), "Company", &lLength);
if( lResult == ERROR_SUCCESS )
{
strValue.ReleaseBuffer();
if( strValue == "Avtron" )
{
m_bBeta = TRUE;
strTitle = pMainFrame->GetTitle();
pMainFrame->SetTitle(strTitle + " Beta Release");
}
}
}
// The main window has been initialized, so show and update it.
pMainFrame->ShowWindow(m_nCmdShow);
pMainFrame->UpdateWindow();
return TRUE;
}
/////////////////////////////////////////////////////////////////////////////
// CAboutDlg dialog used for App About
class CAboutDlg : public CDialog
{
public:
CAboutDlg();
DWORD m_dwSerialNum;
DWORD m_dwCode;
// Dialog Data
//{{AFX_DATA(CAboutDlg)
enum { IDD = IDD_ABOUTBOX };
//}}AFX_DATA
// ClassWizard generated virtual function overrides
//{{AFX_VIRTUAL(CAboutDlg)
protected:
virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
virtual BOOL OnInitDialog();
//}}AFX_VIRTUAL
// Implementation
protected:
//{{AFX_MSG(CAboutDlg)
// No message handlers
//}}AFX_MSG
DECLARE_MESSAGE_MAP()
};
CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
{
//{{AFX_DATA_INIT(CAboutDlg)
m_dwSerialNum = CParameters::SerialNumber();
m_dwCode = CParameters::GetOS();
//}}AFX_DATA_INIT
}
void CAboutDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CAboutDlg)
DDX_Text(pDX, IDC_SN, m_dwSerialNum);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
//{{AFX_MSG_MAP(CAboutDlg)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
BOOL CAboutDlg::OnInitDialog()
{
CDialog::OnInitDialog();
if(m_dwCode == 9)
GetDlgItem(IDC_STATIC1)->SetWindowText("ADDapt 98 Version 17");
else
{
CString msg = "ADDapt 2010 ";
msg += CParameters::Version();
GetDlgItem(IDC_STATIC1)->SetWindowText(msg);
}
return TRUE;
}
// App command to run the dialog
void CAddaptApp::OnAppAbout()
{
CAboutDlg aboutDlg;
aboutDlg.DoModal();
}
/////////////////////////////////////////////////////////////////////////////
// CAddaptApp commands
void CAddaptApp::OnPasswordConfig()
{
if ( m_pPW->IsLoginRequired() )
SecurityMgr();
else
{ // only executed once
CAppIO file;
if ( file.Initialize() )
SecurityMgr();
}
}
void CAddaptApp::ExceptionErrorMessage(CString msg, int num)
{
CString s1;
CString s2;
s1.LoadString(IDS_COM_ERROR);
s2.Format(s1, msg, num);
AfxMessageBox(s2);
}
void CAddaptApp::SetAvailability()
{
m_bSA_Available = FALSE;
m_bProductCodeFound = m_bViewer_Available = m_bTuner_Available = FALSE;
m_bBlockViewer_Available = m_bDriveTuner_Available = FALSE;
m_dwSerialNum = CParameters::SerialNumber();
if( !(m_dwProductCode = CParameters::ProductCode()) )
return; // not available
DWORD num = (((m_dwSerialNum + 1001000) & 0xffc1f) + m_dwProductCode);
DWORD code = (num & 0x02e0);
if(code < 32 || code > 0x02e0)
return; // bogus product code
if(code & 0x020)
{
m_bProductCodeFound = TRUE;
m_bSA_Available = TRUE;
}
if(code & 0x0100)
m_bViewer_Available = TRUE;
if(code & 0x0200)
m_bTuner_Available = TRUE;
}
void CAddaptApp::CreateSerialNum()
{ // if we are here, all options are false
srand(time(NULL));// make random
m_dwSerialNum = rand(); // rand returns int from 0 to 32,767
//m_dwSerialNum &= 0x1111111000111111; // 65087
CParameters::SerialNumber(m_dwSerialNum);
}
void CAddaptApp::SecurityMgr()
{
m_pPW->SetPWGridFlag(TRUE);
((CMultiDocTemplate*)m_pFormTemplate)->OpenDocumentFile(NULL);
}
void CAddaptApp::OnEnableChangePassword(CCmdUI* pCmdUI)
{
TCHAR user[MAX_USERLEN];
m_pPW->GetBackdoorUser(user);
if ( m_pPW->IsUserLogged() & ( (m_pPW->GetCurrentUser().Compare(user)) != 0) )
pCmdUI->Enable(TRUE);
else
pCmdUI->Enable(FALSE);
}
void CAddaptApp::OnEnableFileNew(CCmdUI* pCmdUI)
{
if ( m_pPW->GetUserType() > sl_operator )
{
if( m_bConfigOpen )
pCmdUI->Enable(FALSE);
else
pCmdUI->Enable(TRUE);
}
else
pCmdUI->Enable(FALSE);
}
void CAddaptApp::OnEnableCalFileOpen(CCmdUI* pCmdUI)
{
if ( m_pPW->GetUserType() > sl_operator )
pCmdUI->Enable(TRUE);
else
pCmdUI->Enable(FALSE);
}
void CAddaptApp::OnChangePassword()
{
CChange passDlg;
passDlg.DoModal();
}
void CAddaptApp::OnEnableDefaultConfig(CCmdUI* pCmdUI)
{
CString strFilename;
CFileStatus status; // get file size
if( m_bGXInitialized ) // causes a delay
{
strFilename = m_ADDAPT.DefaultCfgFile();
if ( CFile::GetStatus(strFilename, status) )
{
if( m_bConfigOpen )
pCmdUI->Enable(FALSE);
else
pCmdUI->Enable(TRUE);
}
else
pCmdUI->Enable(FALSE);
}
}
void CAddaptApp::OnEnableConfigFilesDlg(CCmdUI* pCmdUI)
{
pCmdUI->Enable(TRUE);
}
void CAddaptApp::OnConfigFilesDlg()
{
if( ((CAddaptApp*)AfxGetApp())->ReadDSCheck() == ACM_XMIT )
{
CString msg;
msg.LoadString(IDS_DISABLE_COMM);
AfxMessageBox(msg);
return;
}
CString strTest = "Options";
COptionsPropertySheet dlg((LPCTSTR)strTest, AfxGetMainWnd());
if (dlg.DoModal() == IDOK)
{
if(dlg.m_bRestartADDAPT)
AfxMessageBox(IDS_RESTART_ADDAPT, MB_OK);
AfxGetMainWnd()->SendMessageToDescendants(AVM_PARAMETERS_CHANGED);
try
{
pMainFrame->UpdateConfig(m_ADDAPT.DefaultCfgFile());
}
catch(CUserException* e)
{
((CAddaptApp*)AfxGetApp())->ExceptionErrorMessage(THIS_FILE, __LINE__);
e->Delete();
}
}
}
void CAddaptApp::OnEnablePasswordConfig(CCmdUI* pCmdUI)
{
if ( m_pPW->GetUserType() > sl_observer & !m_pPW->IsGridOpen() )
pCmdUI->Enable(TRUE);
else
pCmdUI->Enable(FALSE);
}
void CAddaptApp::OnLogin()
{ // Launch the login dialog
CString s;
CAuthenticateUser dlg;
dlg.DoModal();
if ( m_pPW->IsUserLogged() )
{
pMainFrame->UpdateUser(m_pPW->GetCurrentUser());
pMainFrame->UpdateLevel(m_pPW->GetUserLevel());
// dont store backdoor user
TCHAR user[MAX_USERLEN];
m_pPW->GetBackdoorUser(user);
if ( (m_pPW->GetCurrentUser().Compare(user)) == 0 )
{ // overwrite backdoor user name
s.LoadString(IDS_DEFAULTUSER);
pMainFrame->UpdateUser(s);
return; // backdoor user, stop
}
CPWReg reg; // record user for next login
reg.WrtLastUser(m_pPW->GetCurrentUser());
}
}
void CAddaptApp::OnEnableLogin(CCmdUI* pCmdUI)
{
if ( m_pPW->IsLoginRequired() )
pCmdUI->Enable(TRUE);
else
pCmdUI->Enable(FALSE);
}
void CAddaptApp::OnLogout()
{
m_pPW->ClrUserLogged();
m_pPW->SetDefaultCurrentUser();
pMainFrame->UpdateUser(m_pPW->GetCurrentUser());
pMainFrame->UpdateLevel(m_pPW->GetUserLevel());
OnCloseDocs();
}
void CAddaptApp::OnCloseDocs()
{
if( ReadDSCheck() != ACM_OFFLINE)
{
int result = AfxMessageBox(IDS_LOGOUT, MB_YESNO);
if ( result == IDYES )
{
m_pPW->SetPWGridFlag(FALSE);
m_pFormTemplate->CloseAllDocuments(TRUE);
m_pDriveStatusTemplate->CloseAllDocuments(TRUE);
m_pFaultFIFOTemplate->CloseAllDocuments(TRUE);
}
}
}
void CAddaptApp::OnEnableProductCode(CCmdUI* pCmdUI)
{
if ( m_pPW->GetUserType() == sl_engineer )
pCmdUI->Enable(TRUE);
else
pCmdUI->Enable(FALSE);
}
void CAddaptApp::OnProductCode()
{
CProductCodeDlg dlg;
dlg.DoModal();
}
void CAddaptApp::OnEnableLogout(CCmdUI* pCmdUI)
{
if ( m_pPW->IsUserLogged() )
pCmdUI->Enable(TRUE);
else
pCmdUI->Enable(FALSE);
}
CString CAddaptApp::GetLastAddaptUser()
{
CPWReg reg;
CString s = reg.GetLastUser();
return s;
}
void CAddaptApp::OnCheckActivityIndicator(CCmdUI* pCmdUI)
{
pCmdUI->Enable(TRUE);
pCmdUI->SetCheck(m_bMonitorActivity);
}
void CAddaptApp::OnActivityIndicator()
{
m_bMonitorActivity = !m_bMonitorActivity;
CParameters::MonitorActivity(m_bMonitorActivity);
WPARAM wParam = MAKEWPARAM(m_bMonitorActivity, 0);
if( !m_bMonitorActivity )
WriteDSCheck(ACM_OFF);
else // start offline
WriteDSCheck(ACM_OFFLINE);
}
void CAddaptApp::OnEnableDSGrid(CCmdUI* pCmdUI)
{
m_bConfigOpen = ReadDSCheck();
pCmdUI->SetCheck(m_bConfigOpen);
}
void CAddaptApp::OnDSGrid()
{
}
void CAddaptApp::OnAddInNew(CAddInCollectable* pAddInCollectable)
{
if( pAddInCollectable->m_strLaunchName.isNull() )
{
CAddInCreate AddInCreate;
CLSID clsid;
IID iid;
UuidFromString((UCHAR*)(LPCSTR)pAddInCollectable->m_strClsid, &clsid);
UuidFromString((UCHAR*)(LPCSTR)pAddInCollectable->m_strIID, &iid);
if( AddInCreate.Create(clsid, iid) )
{
IAddIn* pUnknown = NULL;
try
{
pUnknown = AddInCreate.New(CDispatchWrapper((IDispatch*)NULL), m_pPW->GetUserType());
}
catch(CUserException* e)
{
// user cancelled
e->Delete();
return;
}
// cancel pressed - probably
if( pUnknown != NULL )
m_pAddInDocTemplate->OpenDocumentFile(pUnknown, pAddInCollectable);
}
else
{
CString strText, strLoadString;
strLoadString.LoadString(IDS_ERROR_CANT_CREATE_FILE);
strText.Format(strLoadString, pAddInCollectable->m_strFileType);
AfxMessageBox(strText);
}
}
else
{
STARTUPINFO startUpInfo;
PROCESS_INFORMATION processInfo;
::ZeroMemory(&startUpInfo, sizeof(STARTUPINFO));
::ZeroMemory(&processInfo, sizeof(PROCESS_INFORMATION));
startUpInfo.cb = sizeof(STARTUPINFO);
startUpInfo.lpTitle = strdup(pAddInCollectable->m_strFileType);
CString strPath;
HRESULT hr = GetDirectoryFromPath(m_pszHelpFilePath, strPath);
strPath += pAddInCollectable->m_strLaunchName;
BOOL bSuccess;
bSuccess = ::CreateProcess( NULL, (LPSTR)(LPCSTR)strPath, NULL,
NULL, FALSE, CREATE_NEW_CONSOLE,
NULL, NULL, &startUpInfo, &processInfo );
if( !bSuccess )
{
CString strLoadString, strMessage;
strLoadString.LoadString(IDS_CANT_LAUNCH);
strMessage.Format(strLoadString, pAddInCollectable->m_strLaunchName);
}
}
}
void CAddaptApp::OnFileNew()
{
CDocument* pDoc = m_pDriveStatusTemplate->OpenDocumentFile(NULL);
if( pDoc != NULL )
{ // when the user cancels all hell happens
// if when we try to open a no name file
// this quickly fixes that problem!
//m_bConfigOpen = TRUE;
OnAddRow();
}
}
CAddaptApp::~CAddaptApp()
{
m_ordAddIns.clearAndDestroy();
TRACE("CAddaptApp destructor\n");
if( m_pPW )
delete m_pPW;
}
int CAddaptApp::ExitInstance()
{
// persistent monitoring activity state
CParameters::MonitorActivity(m_bMonitorActivity);
if( ReadDSCheck() != ACM_OFFLINE )
{
m_pPW->SetPWGridFlag(FALSE);
m_pFormTemplate->CloseAllDocuments(TRUE);
m_pDriveStatusTemplate->CloseAllDocuments(TRUE);
}
return CWinApp::ExitInstance();
}
void CAddaptApp::OnCalFileOpen()
{
if ( m_pPW->GetUserType() == sl_observer )
return;
if( !m_bProductCodeFound )
{
AfxMessageBox(IDS_PRODUCTCODE_ERROR);
return;
}
AMCString fileName;
CFileDialog dlgFile(TRUE);
CString title;
VERIFY(title.LoadString(AFX_IDS_OPENFILE));
dlgFile.m_ofn.Flags |= OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_SHAREAWARE;
// Add configuration file
CString strDriveFile, strFilter, strNxt;
strDriveFile.LoadString(IDS_DRIVE_CONFIG_FILE);
strFilter += strDriveFile + " (*.add)";
strFilter += (TCHAR)'\0';
strFilter += "*.add";
strFilter += (TCHAR)'\0';
strFilter += (TCHAR)'\0'; // last string
dlgFile.m_ofn.lpstrFilter = strFilter;
dlgFile.m_ofn.lpstrTitle = title;
dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);
// display files if they exist
CString path = CParameters::DownloadableFiles();
if( !path.IsEmpty() )
dlgFile.m_ofn.lpstrInitialDir = path.GetBuffer(MAX_PATH);
BOOL bResult = dlgFile.DoModal() == IDOK ? TRUE : FALSE;
fileName.ReleaseBuffer();
if( bResult )
{
CDocument* pDoc = OpenDocumentFile(fileName);
}
}
void CAddaptApp::OnFileOpen()
{
if ( m_pPW->GetUserType() == sl_observer )
return;
AMCString fileName;
CFileDialog dlgFile(TRUE);
CString title;
VERIFY(title.LoadString(AFX_IDS_OPENFILE));
dlgFile.m_ofn.Flags |= OFN_HIDEREADONLY | OFN_FILEMUSTEXIST | OFN_SHAREAWARE;
// Add configuration file
CString strDriveFile, strFilter, strNxt;
strFilter = "ADDapt Files (*.cfg; *.add; *.alz; *.cmp)";
strFilter += (TCHAR)'\0';
strFilter += "*.cfg; *.add; *.alz; *.cmp";
strFilter += (TCHAR)'\0';
strDriveFile.LoadString(IDS_ADDAPT_CONFIG_FILE);
strFilter += strDriveFile + " (*.cfg)";
strFilter += (TCHAR)'\0';
strFilter += "*.cfg";
strFilter += (TCHAR)'\0';
strDriveFile.LoadString(IDS_ANALYZER_FILE);
strFilter += strDriveFile + " (*.alz)";
strFilter += (TCHAR)'\0';
strFilter += "*.alz";
strFilter += (TCHAR)'\0';
strDriveFile.LoadString(IDS_DRIVE_CONFIG_FILE);
strFilter += strDriveFile + " (*.add)";
strFilter += (TCHAR)'\0';
strFilter += "*.add";
strFilter += (TCHAR)'\0';
strDriveFile.LoadString(IDS_DRIVE_COMPARE_FILE);
strFilter += strDriveFile + " (*.cmp)";
strFilter += (TCHAR)'\0';
strFilter += "*.cmp";
strFilter += (TCHAR)'\0';
strFilter += (TCHAR)'\0'; // last string
dlgFile.m_ofn.lpstrFilter = strFilter;
dlgFile.m_ofn.lpstrTitle = title;
dlgFile.m_ofn.lpstrFile = fileName.GetBuffer(_MAX_PATH);
// display files if they exist
CString path = CParameters::DownloadableFiles();
if( !path.IsEmpty() )
dlgFile.m_ofn.lpstrInitialDir = path.GetBuffer(MAX_PATH);
BOOL bResult = dlgFile.DoModal() == IDOK ? TRUE : FALSE;
fileName.ReleaseBuffer();
if( bResult )
CDocument* pDoc = OpenDocumentFile(fileName);
}
CDocument* CAddaptApp::OpenDocumentFile(LPCTSTR lpszFileName)
{
BOOL bCfg, bDoc, bEmpty;
bCfg = bDoc = bEmpty = FALSE;
CString s(lpszFileName);
s.MakeLower();
if(s.Right(3) == "cfg")
{
if( m_bConfigOpen )
{
AfxMessageBox(ID_CANNOT_OPEN);
return NULL;
}
else
bCfg = TRUE;
}
if(s.Right(3) == "cmp")
bDoc = TRUE;
if(s.Right(3) == "alz")
{
OnLaunchAnalyzer((LPCTSTR)s);
return NULL;
}
// this could come from cwinapp::onopenrecentfile
if ( m_pPW->GetUserType() == sl_observer )
{
if( !bCfg )
return NULL;
}
if( bDoc )
{
CDocument* pDoc;
pDoc = (CDocument*) ((CAddaptApp*)AfxGetApp())->m_pCompareTemplate->OpenDocumentFile(lpszFileName);
pDoc->SetTitle(lpszFileName);
}
// the exception handling below looks shakey. jeh
IAddIn* pAddInInterface = NULL;
CLSID clsid;
IID iid;
CAddInFile* pAddInFile = NULL;
try
{
pAddInFile = new CAddInFile;
pAddInFile->Initialize((AMCString)lpszFileName, "rb+");
}
catch(...)
{
if( pAddInFile != NULL )
delete pAddInFile;
// Not an Addin file - try configuration file
// this used to be for something, use it to test for empty config file
CConfigurationFile TestFile;
TestFile.Create();
TestFile.Open(lpszFileName);
if( TestFile.GetEOF() )
bEmpty = TRUE;
CString s = lpszFileName;
CDocument *pDoc = m_pDriveStatusTemplate->OpenDocumentFile(lpszFileName);
if( pDoc != NULL & !bEmpty )
{
pMainFrame->UpdateConfig(s);
}
return pDoc;
}
// we are on the addin path
if( !pAddInFile->isValid() )
return NULL;
CAddInCollectable* pAddIn, FindAddIn;
if( SUCCEEDED(pAddInFile->Open()) )
{
FindAddIn.m_dwAddInIdentifier = pAddInFile->m_AddInIdentifier.m_dwAddInIdentifier;
pAddIn = static_cast<CAddInCollectable*>(((CAddaptApp*)AfxGetApp())->m_ordAddIns.find(&FindAddIn));
if( pAddIn == NULL )
{
delete pAddInFile;
CString strTemp, strError;
strTemp.LoadString(IDS_ERROR_FILE_NOT_VALID);
strError.Format(strTemp, (LPCSTR)lpszFileName);
AfxMessageBox(strError);
return NULL;
}
UuidFromString((UCHAR*)(LPCSTR)pAddIn->m_strClsid, &clsid);
UuidFromString((UCHAR*)(LPCSTR)pAddIn->m_strIID, &iid);
CAddInCreate AddInCreate;
if( !AddInCreate.Create(clsid, iid) )
{
AfxMessageBox(IDS_ERROR_CANT_CREATE_ADDIN_CREATE);
return NULL;
}
delete pAddInFile;
pAddInInterface = AddInCreate.Open(lpszFileName, m_pPW->GetUserType());
if( pAddInInterface == NULL )
return NULL;
}
else
{
delete pAddInFile;
AfxMessageBox(g_strErrorMessage);
return NULL;
}
AddToRecentFileList(lpszFileName);
return m_pAddInDocTemplate->OpenDocumentFile(pAddInInterface, pAddIn);
}
void CAddaptApp::AddToRecentFileList(LPCTSTR lpszPathName)
{
CString s(lpszPathName);
if( s.Right(4) != ".cfg" )
CWinApp::AddToRecentFileList(lpszPathName);
}
BOOL CAddaptApp::ProcessMessageFilter(int code, LPMSG lpMsg)
{
/* if( m_hwndDialog != NULL )
{
if( lpMsg->hwnd == m_hwndDialog || ::IsChild(m_hwndDialog, lpMsg->hwnd) )
{
AfxMessageBox("Here for debugging dialog message bugs");
}
}*/
return CWinApp::ProcessMessageFilter(code, lpMsg);
}
BOOL CAddaptApp::PreTranslateMessage(MSG* pMsg)
{
// CG: The following lines were added by the Splash Screen component.
if (CSplashWnd::PreTranslateAppMessage(pMsg))
return TRUE;
return CWinApp::PreTranslateMessage(pMsg);
}
BOOL CAddaptApp::ReadDSCheck()
{
long state = CParameters::DisplayStatusGrid();
if( state < ACM_RESET || state > ACM_OFF )
state = ACM_OFF;
return state;
}
void CAddaptApp::WriteDSCheck(UINT bChk)
{ // write only changes
if( bChk != m_bDSGridStatus )
{
m_bDSGridStatus = bChk;
CParameters::DisplayStatusGrid(bChk);
((CMainFrame*)AfxGetApp()->m_pMainWnd)->PostMessage(AVM_COMMUNICATION_EVENT,0,0);
}
}
void CAddaptApp::OnOpenDefaultCfg()
{
CString strFilename;
try
{
strFilename = m_ADDAPT.DefaultCfgFile();
OpenDocumentFile(strFilename);
}
catch(CUserException* e)
{
((CAddaptApp*)AfxGetApp())->ExceptionErrorMessage(THIS_FILE, __LINE__);
e->Delete();
}
}
void CAddaptApp::RestoreWindows()
{
CRegKey Key;
LONG lResult = Key.Create(HKEY_LOCAL_MACHINE, "Software\\Avtron Manufacturing\\Addapt\\Save Window State");
FILETIME time;
TCHAR szBuffer[256];
DWORD dwSize = 256;
DWORD dwIndex = 0;
CWnd* pActiveWnd = NULL;
CRegKey MainWndKey;
if (MainWndKey.Open(Key, "Main Window") == ERROR_SUCCESS)
{
DWORD dwTemp;
CRect rcWnd;
MainWndKey.QueryValue(dwTemp, "Left");
rcWnd.left = dwTemp;
MainWndKey.QueryValue(dwTemp, "Top");
rcWnd.top = dwTemp;
MainWndKey.QueryValue(dwTemp, "Right");
rcWnd.right = dwTemp;
MainWndKey.QueryValue(dwTemp, "Bottom");
rcWnd.bottom = dwTemp;
AfxGetMainWnd()->SetWindowPos(NULL, rcWnd.left, rcWnd.top, rcWnd.Width(), rcWnd.Height(), SWP_NOZORDER);
}
while (RegEnumKeyEx(Key.m_hKey, dwIndex++, szBuffer, &dwSize, NULL, NULL, NULL, &time) == ERROR_SUCCESS)
{
CRegKey FileKey;
FileKey.Open(Key, szBuffer);
CString strFilename;
DWORD dwLength = MAX_PATH;
if (FileKey.QueryValue(strFilename.GetBuffer(MAX_PATH), "Filename", &dwLength) == ERROR_SUCCESS)
{
strFilename.ReleaseBuffer();
DWORD dwTemp;
CRect rcWnd;
FileKey.QueryValue(dwTemp, "Left");
rcWnd.left = dwTemp;
FileKey.QueryValue(dwTemp, "Top");
rcWnd.top = dwTemp;
FileKey.QueryValue(dwTemp, "Right");
rcWnd.right = dwTemp;
FileKey.QueryValue(dwTemp, "Bottom");
rcWnd.bottom = dwTemp;
BOOL bMinimize = FALSE, bMaximize = FALSE, bActive = FALSE;
if (FileKey.QueryValue(dwTemp, "Minimized") == ERROR_SUCCESS)
bMinimize = dwTemp;
if (FileKey.QueryValue(dwTemp, "Maximized") == ERROR_SUCCESS)
bMaximize = dwTemp;
if (FileKey.QueryValue(dwTemp, "Active") == ERROR_SUCCESS)
bActive = dwTemp;
CDocument* pDoc = OpenDocumentFile(strFilename);
POSITION pos = pDoc->GetFirstViewPosition();
CView* pView = pDoc->GetNextView(pos);
if (!bActive)
pView->GetParent()->SetWindowPos(NULL, rcWnd.left, rcWnd.top, rcWnd.Width(), rcWnd.Height(), SWP_NOZORDER);
else
pView->GetParent()->SetWindowPos(NULL, rcWnd.left, rcWnd.top, rcWnd.Width(), rcWnd.Height(), SWP_NOZORDER);
if (bMinimize)
pView->GetParent()->PostMessage(WM_SYSCOMMAND, SC_MINIMIZE);
else if (bMaximize)
pView->GetParent()->PostMessage(WM_SYSCOMMAND, SC_MAXIMIZE);
if (bActive)
pActiveWnd = pView->GetParent();
}
dwSize = 256;
}
if (pActiveWnd != NULL)
pActiveWnd->SetWindowPos(NULL, 0, 0, 0, 0, SWP_NOMOVE | SWP_NOSIZE | SWP_NOZORDER);
}
void CAddaptApp::OnHelp()
{
CString sHlp;
sHlp = (CParameters::DefaultDir() + "\\addapt.hlp");
::WinHelp(*AfxGetMainWnd(), sHlp, HELP_CONTENTS, 0);
}
void CAddaptApp::OnAddInCopy(CAddIn* pAddIn)
{
IAddIn* pUnknown;
pAddIn->m_pUnknown->QueryInterface(IID_IAddIn, (void**)&pUnknown);
m_pAddInDocTemplate->OpenDocumentFile(pUnknown, NULL);
}
void CAddaptApp::OnEnableEditCut(CCmdUI* pCmdUI)
{
//if( m_bConfigOpen )
pCmdUI->Enable(FALSE);
}
void CAddaptApp::OnEnableAddSection(CCmdUI* pCmdUI)
{
if( m_pPW->GetUserType() > sl_operator &
!m_pPW->IsGridOpen() & m_bCanAdd )
pCmdUI->Enable(TRUE);
else
pCmdUI->Enable(FALSE);
}
void CAddaptApp::OnAddSection()
{
AfxGetMainWnd()->SendMessageToDescendants(AVM_NEWROW, 0, 0, TRUE);
}
void CAddaptApp::OnEnableAddRow(CCmdUI* pCmdUI)
{
if( m_pPW->GetUserType() > sl_operator & m_bConfigOpen )
pCmdUI->Enable(TRUE);
else
pCmdUI->Enable(FALSE);
}
void CAddaptApp::OnAddRow()
{
AfxGetMainWnd()->SendMessageToDescendants(AVM_GUESS, 0, 0, TRUE);
}
void CAddaptApp::OnLaunchAnalyzer(LPCTSTR filename)
{
CString strValue;
CRegKey Key;
if( !((CAddaptApp*)AfxGetApp())->m_bSA_Available )
return;
LONG lResult = Key.Open(HKEY_LOCAL_MACHINE,"Software\\Avtron Manufacturing\\Addapt\\", KEY_ALL_ACCESS);
if ( lResult != ERROR_SUCCESS )
{ // The key does not exist in the registry, create it .
lResult = Key.Create(HKEY_LOCAL_MACHINE, "Software\\Avtron Manufacturing\\Addapt\\alz");
}
if( lResult != ERROR_SUCCESS )
AfxMessageBox(IDS_COULD_NOT_CREATE_REGISTRYKEY);
else // we can write to the registry
{ // start security with greatest access
lResult = Key.SetValue(filename, "alz");
if( lResult != ERROR_SUCCESS )
AfxMessageBox(IDS_COULD_NOT_CREATE_REGISTRYKEY);
}
STARTUPINFO startUpInfo;
PROCESS_INFORMATION processInfo;
::ZeroMemory(&startUpInfo, sizeof(STARTUPINFO));
::ZeroMemory(&processInfo, sizeof(PROCESS_INFORMATION));
startUpInfo.cb = sizeof(STARTUPINFO);
startUpInfo.lpTitle = "Signal Analyzer";
BOOL bSuccess = ::CreateProcess( NULL, "C:\\Avtron\\ADDapt2010\\68220113.exe",
NULL, NULL, FALSE, CREATE_NEW_CONSOLE,
NULL, NULL, &startUpInfo, &processInfo);
if (!bSuccess)
{
DWORD dwError = GetLastError();
CString strLoadString, strMessage;
strLoadString.LoadString(IDS_CANT_LAUNCH);
strMessage.Format(strLoadString, "Analyzer", dwError);
AfxMessageBox(strMessage);
}
}
BOOL CAddaptApp::TemplateOK(CSection Section, CString path)
{
CFileStatus stat;
CString sFilename, sLoadString, sText;
BOOL bFound=FALSE;
sFilename.Format("%s\\%0.6s%0.2s.tem", (LPCTSTR)path,
(LPCTSTR)Section.PartNumber(), (LPCTSTR)Section.VersionNumber());
if( CFile::GetStatus(sFilename, stat) )
bFound = TRUE;
if( !bFound )
AfxMessageBox(IDS_CANNOT_LOCATE_TEMPLATE_FILE);
return bFound;
}