1 // WinAfsLoad.cpp : Defines the class behaviors for the application.
3 /* Copyright 2000, International Business Machines Corporation and others.
6 This software has been released under the terms of the IBM Public
7 License. For details, see the LICENSE file in the top-level source
8 directory or online at http://www.openafs.org/dl/license10.html
12 #include "WinAfsLoad.h"
13 #include "WinAfsLoadDlg.h"
24 static char THIS_FILE[] = __FILE__;
27 /////////////////////////////////////////////////////////////////////////////
30 BEGIN_MESSAGE_MAP(CWinAfsLoadApp, CWinApp)
31 //{{AFX_MSG_MAP(CWinAfsLoadApp)
33 ON_COMMAND(ID_HELP, CWinApp::OnHelp)
34 ON_THREAD_MESSAGE(WM_UIONPARM,OnNotifyReturn)
35 ON_THREAD_MESSAGE(WSA_EVENT, OnAfsEvent)
38 /////////////////////////////////////////////////////////////////////////////
39 // CWinAfsLoadApp construction
41 CWinAfsLoadApp::CWinAfsLoadApp()
43 // TODO: add construction code here,
44 // Place all significant initialization in InitInstance
47 /////////////////////////////////////////////////////////////////////////////
48 // The one and only CWinAfsLoadApp object
50 CWinAfsLoadApp theApp;
52 /////////////////////////////////////////////////////////////////////////////
53 // CWinAfsLoadApp initialization
55 HWND CWinAfsLoadApp::m_hAfsLoadFinish=0;
56 HWND CWinAfsLoadApp::m_hAfsLoad=0;
57 CString CWinAfsLoadApp::m_sDosAppName;
60 ::GetWindowThreadProcessId (hWnd, &dwProcessID);
62 BOOL CALLBACK CWinAfsLoadApp::EnumWindowsProc(HWND hWnd, LPARAM lParam)
64 CWnd *wnd=CWnd::FromHandle(hWnd);
67 wnd->GetWindowText(msg);
68 if ( (!m_sDosAppName.IsEmpty())
69 && (m_sDosAppName!=""))
71 if (msg==m_sDosAppName)
73 sFinish.Format("Finished - %s",m_sDosAppName);
75 m_hAfsLoadFinish=hWnd;
79 if ((msg==DOSTITLEFINISH)||(msg==APPTITLEFINISH))
80 m_hAfsLoadFinish=hWnd;
81 return ((m_hAfsLoad==0) && (m_hAfsLoadFinish==0));
84 int CWinAfsLoadApp::ExitInstance()
86 // TODO: Add your specialized code here and/or call the base class
89 SetNotifyEventSuspend();
90 m_pCMyUIThread->PostThreadMessage(WM_UIONPARM,ONPARMCLOSE,0);
91 WaitForSingleObject(CMyUIThread::m_hEventThreadKilled, INFINITE);
92 CloseHandle(CMyUIThread::m_hEventThreadKilled);
95 return CWinApp::ExitInstance();
98 BOOL CWinAfsLoadApp::InitInstance()
100 HWND hwnd=FindWindow(NULL,"AFS Activity Log");
102 msg.LoadString(IDD_ACTIVITYLOG);
105 ::PostMessage(hwnd,WM_SYSCOMMAND,IDM_VISIBLE,0);
108 EnumWindows (CWinAfsLoadApp::EnumWindowsProc, (LPARAM) 0); //lets grab other open windows and close them down
109 if (!AfxSocketInit())
111 AfxMessageBox(IDP_SOCKETS_INIT_FAILED);
115 // Standard initialization
116 // If you are not using these features and wish to reduce the size
117 // of your final executable, you should remove from the following
118 // the specific initialization routines you do not need.
121 Enable3dControls(); // Call this when using MFC in a shared DLL
123 Enable3dControlsStatic(); // Call this when linking to MFC statically
126 PCHAR pPath=getenv("AFSCONF");
127 m_sTargetDir.Empty();
129 AfxMessageBox("AFS Error - Improper installation, Enviornemnt variable AFSCONF missing",MB_ICONWARNING | MB_OK);
131 m_sTargetDir.Format("%s\\",pPath);
133 // CString t(m_sTargetDir);
134 // t+="afs-light.hlp";
136 m_pszHelpFilePath=_tcsdup(_T("afswin9x.hlp"));
137 // initialize variables
138 CRegkey regkey("AFS\\Window");
139 if (regkey.Getkey()!=0)
142 regkey.GetString("Title",m_sDosAppName,len);
146 if ((m_pCMyUIThread=(CMyUIThread *)AfxBeginThread(RUNTIME_CLASS(CMyUIThread)),THREAD_PRIORITY_IDLE)==NULL)
147 {// need to create a very low priority else it will interfere with other windows
148 AfxMessageBox("Error creating background thread for progress bar! /nApplicaton still operational.");
151 m_pCMyUIThread->m_hEventThreadKilled //used to test when thread is done
152 = CreateEvent(NULL, FALSE, FALSE, NULL); // auto reset, initially reset
153 // scan for command line options
154 // show userid: password: connect
155 CharLower(m_lpCmdLine);
156 m_bShowAfs=(strstr(m_lpCmdLine,"show")!=NULL);
163 strlwr(m_lpCmdLine); //convert to lower case
164 if ((str=strstr(m_lpCmdLine,"userid:"))!=NULL) //
166 cUserName=(strstr(str,":"));
169 if ((str=strstr(m_lpCmdLine,"password:"))!=NULL)
171 cPassWord=(strstr(str,":"));
173 // BOOL bConnect=(strstr(m_lpCmdLine,"connect")!=NULL);
175 if (m_hAfsLoadFinish) // SOME REASON THE WINDOW WAS NOT SHUT DOWN, SO LETES KILL IT
177 ::SendMessage(m_hAfsLoadFinish,WM_CLOSE,0,0);
180 if (m_hAfsLoad) // Something wrong, BACKGROUND is running without dialog
182 CTermWarn *pModeless = new CTermWarn(NULL);
184 pModeless->SetActiveWindow();
186 CTime startTime = CTime::GetCurrentTime();
187 CTimeSpan elapsedTime;
192 EnumWindows (EnumWindowsProc, (LPARAM) 0); //lets prevent multiple instances!
193 elapsedTime= CTime::GetCurrentTime() - startTime;
194 if (elapsedTime.GetTotalSeconds()>10)
197 pModeless->OnCancel();
198 ::ShowWindow(m_hAfsLoad,SW_SHOWNORMAL);
199 ::AfxMessageBox("AFS Error - Warning AFS Client Console still running.\n Locate the console and use Control C to manually terminate it!");
202 } while (m_hAfsLoad);
203 if (m_hAfsLoadFinish) // SOME REASON THE WINDOW WAS NOT SHUT DOWN, SO LETES KILL IT
205 ::SendMessage(m_hAfsLoadFinish,WM_CLOSE,0,0);
209 pModeless->OnCancel();
211 CWinAfsLoadDlg dlg(cUserName,cPassWord);
213 int nResponse = dlg.DoModal();
214 if (nResponse == IDOK)
216 // TODO: Place code here to handle when the dialog is
219 else if (nResponse == IDCANCEL)
221 // TODO: Place code here to handle when the dialog is
222 // dismissed with Cancel
225 // Since the dialog has been closed, return FALSE so that we exit the
226 // application, rather than start the application's message pump.
230 BOOL CWinAfsLoadApp::RegOptions(BOOL fetch)
232 CRegkey regkey("AFS\\Window");
233 if (regkey.Getkey()==0) return FALSE;
238 regkey.GetBinary("CommandSettings",(LPBYTE)(&w),len);
239 m_bLogWindow=((w & 2)!=0);
241 m_bConnect=((w & 1)!=0);
244 w=((m_bLogWindow)?2:0) | ((m_bLog)?4:0) | ((m_bConnect)?1:0);
245 regkey.PutBinary("CommandSettings",(LPBYTE)(&w),sizeof(DWORD));
250 void CWinAfsLoadApp::Log(const char *s)
253 if ((!m_bLogWindow) && (!m_bLog)) return;
257 GetSystemTime(&timeDest);
258 CString *mt=new CString; //release is handled by CMyUIThread
259 mt->Format("(%02d:%02d:%2d-%03d)%s\r\n",
263 timeDest.wMilliseconds,s);
264 m_pCMyUIThread->PostThreadMessage(WM_LOG,0,(LPARAM)mt);
267 m_pCMyUIThread->PostThreadMessage(WM_LOG,0,0);
270 void CWinAfsLoadApp::Log(const char *f,const DWORD d)
277 void CWinAfsLoadApp::Log(const char *f,const DWORD d,const DWORD d2)
284 void CWinAfsLoadApp::Log(const char *f,const char*s)
291 void CWinAfsLoadApp::Log(const char *f,const char*s,const char*s2)
298 void CWinAfsLoadApp::Log(const char *f,const char*s,const char*s2,const char*s3)
301 msg.Format(f,s,s2,s3);
305 void CWinAfsLoadApp::ShowLog(BOOL yes)
307 m_pCMyUIThread->PostThreadMessage(WM_LOG,LOGSHOWWINDOW,yes);
310 void CWinAfsLoadApp::ShowPrint(BOOL yes)
312 CString *mp=new CString();
313 mp->Format("%s\\afscli.log",getenv("AFSCONF"));
314 m_pCMyUIThread->PostThreadMessage(WM_LOG,LOGSHOWPRINT,(yes)?(LPARAM)mp:NULL);
317 void CWinAfsLoadApp::Progress(VOID *ptr, UINT mode)
319 // you cannot obtain the handle to the window int these routines so you have enough time to create the window
320 // It is ok to postmessage to a window in a different thread!
321 if (m_pCMyUIThread==NULL) return;
325 ::PostMessage(((CDialog *)m_pCMyUIThread->GetMainWnd())->GetSafeHwnd()
326 ,WM_PROGRESSPARM,ProgFSetTitle,(LPARAM)ptr);
329 ::PostMessage(((CDialog *)m_pCMyUIThread->GetMainWnd())->GetSafeHwnd()
330 ,WM_PROGRESSPARM,ProgFHide,0);
333 ::PostMessage(((CDialog *)m_pCMyUIThread->GetMainWnd())->GetSafeHwnd(),WM_PROGRESSPARM,ProgFNext,0);
334 BringWindowToTop(((CDialog *)m_pCMyUIThread->GetMainWnd())->GetSafeHwnd());
337 ::PostMessage(((CDialog *)m_pCMyUIThread->GetMainWnd())->GetSafeHwnd(),WM_PROGRESSPARM,ProgFOpt,mode);
338 BringWindowToTop(((CDialog *)m_pCMyUIThread->GetMainWnd())->GetSafeHwnd());
339 ((CDialog *)m_pCMyUIThread->GetMainWnd())->CenterWindow((CWnd *)ptr);
345 void CWinAfsLoadApp::RequestUISuspendEvent(UINT mode)
347 SetNotifyEventSuspend();
348 m_pCMyUIThread->PostThreadMessage(WM_UIONPARM,mode,0);
351 void CWinAfsLoadApp::RequestUIPostEvent(UINT mode,WPARAM wp,LPARAM lp)
353 SetNotifyEventPostMessage();
354 m_pCMyUIThread->PostThreadMessage(mode,wp,lp);
359 There is no queue for messages (only one event) therefore messages passed to us
360 are not queued and stored in m_sMsg & m_wParam;
363 void CWinAfsLoadApp::SetNotifyEventSuspend()
365 ResetEvent(CMyUIThread::m_hEventThreadKilled);
369 void CWinAfsLoadApp::WaitForEvent(UINT logintime,WPARAM *wp,CString *msg)
371 switch (WaitForSingleObject(CMyUIThread::m_hEventThreadKilled, logintime))
374 LOG("OnAfsEvent - Receive Good Status ");
378 *wp=AFS_EXITCODE_GENERAL_FAILURE;
379 *msg="AFS Client timed out, failure to respond\r\nCheck Network Connection.";
380 LOG("OnAfsEvent %s",(const char *)*msg);
387 Tthis funciton is called from UIThread (different thread)
388 and should not contain and Windows stuff assocaited with CWinAFSLoad.
389 It is used to display messages from a backgound process or during PowerSuspend denial
390 NOTICE: PostThreadMessage leaves deletion of message up to caller
391 WaitEvent does not have to delete message because Wait is not queued like PostMessage
393 void CWinAfsLoadApp::WSANotifyFromUI(WPARAM wp,const char *msg)
395 TRACE("CWinAfsLoadApp::Notify wp=%d,msg=[%s]\n",wp,msg);
396 switch (m_uNotifyMessage)
399 {// for post message use allocated memory to hold results and pass back to main thread
400 CString *mp=new CString; //memory is released by destination routine!
402 PostThreadMessage(WSA_EVENT,wp,(LPARAM)mp); //this goes to WSA_EVENT
406 // For setting the Event completion, pass information back to calling thread
409 SetEvent(CMyUIThread::m_hEventThreadKilled);
417 void CWinAfsLoadApp::NotifyFromUI(WPARAM wp,const char *msg)
418 {// called from UIThread to notify return status
422 mp=new CString; //memory is released by destination routine!
424 TRACE("CWinAfsLoadApp::Notify wp=%d,msg=[%s]\n",wp,msg);
426 TRACE("CWinAfsLoadApp::Notify wp=%d,msg=NULL\n",wp);
428 PostThreadMessage(WM_UIONPARM,wp,(LPARAM)mp);
431 LRESULT CWinAfsLoadApp::OnNotifyReturn(WPARAM wParam, LPARAM lParam)
434 return ((CWinAfsLoadDlg *)m_pMainWnd)->OnNotifyReturn(wParam,lParam);
438 LRESULT CWinAfsLoadApp::OnAfsEvent(WPARAM wParam, LPARAM lParam)
441 return ((CWinAfsLoadDlg *)m_pMainWnd)->OnAfsEvent(wParam,lParam);