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);
157 m_bConnect=(strstr(m_lpCmdLine,"connect")!=NULL);
164 strlwr(m_lpCmdLine); //convert to lower case
165 if ((str=strstr(m_lpCmdLine,"userid:"))!=NULL) //
167 cUserName=(strstr(str,":"));
170 if ((str=strstr(m_lpCmdLine,"password:"))!=NULL)
172 cPassWord=(strstr(str,":"));
174 m_bNoID=(strstr(m_lpCmdLine,"noid")!=NULL);
176 if (m_hAfsLoadFinish) // SOME REASON THE WINDOW WAS NOT SHUT DOWN, SO LETES KILL IT
178 ::SendMessage(m_hAfsLoadFinish,WM_CLOSE,0,0);
181 if (m_hAfsLoad) // Something wrong, BACKGROUND is running without dialog
183 CTermWarn *pModeless = new CTermWarn(NULL);
185 pModeless->SetActiveWindow();
187 CTime startTime = CTime::GetCurrentTime();
188 CTimeSpan elapsedTime;
193 EnumWindows (EnumWindowsProc, (LPARAM) 0); //lets prevent multiple instances!
194 elapsedTime= CTime::GetCurrentTime() - startTime;
195 if (elapsedTime.GetTotalSeconds()>10)
198 pModeless->OnCancel();
199 ::ShowWindow(m_hAfsLoad,SW_SHOWNORMAL);
200 ::AfxMessageBox("AFS Error - Warning AFS Client Console still running.\n Locate the console and use Control C to manually terminate it!");
203 } while (m_hAfsLoad);
204 if (m_hAfsLoadFinish) // SOME REASON THE WINDOW WAS NOT SHUT DOWN, SO LETES KILL IT
206 ::SendMessage(m_hAfsLoadFinish,WM_CLOSE,0,0);
210 pModeless->OnCancel();
212 CWinAfsLoadDlg dlg(cUserName,cPassWord);
214 int nResponse = dlg.DoModal();
215 if (nResponse == IDOK)
217 // TODO: Place code here to handle when the dialog is
220 else if (nResponse == IDCANCEL)
222 // TODO: Place code here to handle when the dialog is
223 // dismissed with Cancel
226 // Since the dialog has been closed, return FALSE so that we exit the
227 // application, rather than start the application's message pump.
231 BOOL CWinAfsLoadApp::RegOptions(BOOL fetch)
233 CRegkey regkey("AFS\\Window");
234 if (regkey.Getkey()==0) return FALSE;
239 regkey.GetBinary("CommandSettings",(LPBYTE)(&w),len);
240 m_bLogWindow=((w & 2)!=0);
242 m_bConnect=((w & 1)!=0);
245 w=((m_bLogWindow)?2:0) | ((m_bLog)?4:0) | ((m_bConnect)?1:0);
246 regkey.PutBinary("CommandSettings",(LPBYTE)(&w),sizeof(DWORD));
251 void CWinAfsLoadApp::Log(const char *s)
254 if ((!m_bLogWindow) && (!m_bLog)) return;
258 GetSystemTime(&timeDest);
259 CString *mt=new CString; //release is handled by CMyUIThread
260 mt->Format("(%02d:%02d:%2d-%03d)%s\r\n",
264 timeDest.wMilliseconds,s);
265 m_pCMyUIThread->PostThreadMessage(WM_LOG,0,(LPARAM)mt);
268 m_pCMyUIThread->PostThreadMessage(WM_LOG,0,0);
271 void CWinAfsLoadApp::Log(const char *f,const DWORD d)
278 void CWinAfsLoadApp::Log(const char *f,const DWORD d,const DWORD d2)
285 void CWinAfsLoadApp::Log(const char *f,const char*s)
292 void CWinAfsLoadApp::Log(const char *f,const char*s,const char*s2)
299 void CWinAfsLoadApp::Log(const char *f,const char*s,const char*s2,const char*s3)
302 msg.Format(f,s,s2,s3);
306 void CWinAfsLoadApp::ShowLog(BOOL yes)
308 m_pCMyUIThread->PostThreadMessage(WM_LOG,LOGSHOWWINDOW,yes);
311 void CWinAfsLoadApp::ShowPrint(BOOL yes)
313 CString *mp=new CString();
314 mp->Format("%s\\afscli.log",getenv("AFSCONF"));
315 m_pCMyUIThread->PostThreadMessage(WM_LOG,LOGSHOWPRINT,(yes)?(LPARAM)mp:NULL);
318 void CWinAfsLoadApp::Progress(VOID *ptr, UINT mode)
320 // you cannot obtain the handle to the window int these routines so you have enough time to create the window
321 // It is ok to postmessage to a window in a different thread!
322 if (m_pCMyUIThread==NULL) return;
326 ::PostMessage(((CDialog *)m_pCMyUIThread->GetMainWnd())->GetSafeHwnd()
327 ,WM_PROGRESSPARM,ProgFSetTitle,(LPARAM)ptr);
330 ::PostMessage(((CDialog *)m_pCMyUIThread->GetMainWnd())->GetSafeHwnd()
331 ,WM_PROGRESSPARM,ProgFHide,0);
334 ::PostMessage(((CDialog *)m_pCMyUIThread->GetMainWnd())->GetSafeHwnd(),WM_PROGRESSPARM,ProgFNext,0);
335 BringWindowToTop(((CDialog *)m_pCMyUIThread->GetMainWnd())->GetSafeHwnd());
338 ::PostMessage(((CDialog *)m_pCMyUIThread->GetMainWnd())->GetSafeHwnd(),WM_PROGRESSPARM,ProgFOpt,mode);
339 BringWindowToTop(((CDialog *)m_pCMyUIThread->GetMainWnd())->GetSafeHwnd());
340 ((CDialog *)m_pCMyUIThread->GetMainWnd())->CenterWindow((CWnd *)ptr);
346 void CWinAfsLoadApp::RequestUISuspendEvent(UINT mode)
348 SetNotifyEventSuspend();
349 m_pCMyUIThread->PostThreadMessage(WM_UIONPARM,mode,0);
352 void CWinAfsLoadApp::RequestUIPostEvent(UINT mode,WPARAM wp,LPARAM lp)
354 SetNotifyEventPostMessage();
355 m_pCMyUIThread->PostThreadMessage(mode,wp,lp);
360 There is no queue for messages (only one event) therefore messages passed to us
361 are not queued and stored in m_sMsg & m_wParam;
364 void CWinAfsLoadApp::SetNotifyEventSuspend()
366 ResetEvent(CMyUIThread::m_hEventThreadKilled);
370 void CWinAfsLoadApp::WaitForEvent(UINT logintime,WPARAM *wp,CString *msg)
372 switch (WaitForSingleObject(CMyUIThread::m_hEventThreadKilled, logintime))
375 LOG("OnAfsEvent - Receive Good Status ");
379 *wp=AFS_EXITCODE_GENERAL_FAILURE;
380 *msg="AFS Client timed out, failure to respond\r\nCheck Network Connection.";
381 LOG("OnAfsEvent %s",(const char *)*msg);
388 Tthis funciton is called from UIThread (different thread)
389 and should not contain and Windows stuff assocaited with CWinAFSLoad.
390 It is used to display messages from a backgound process or during PowerSuspend denial
391 NOTICE: PostThreadMessage leaves deletion of message up to caller
392 WaitEvent does not have to delete message because Wait is not queued like PostMessage
394 void CWinAfsLoadApp::WSANotifyFromUI(WPARAM wp,const char *msg)
396 TRACE("CWinAfsLoadApp::Notify wp=%d,msg=[%s]\n",wp,msg);
397 switch (m_uNotifyMessage)
400 {// for post message use allocated memory to hold results and pass back to main thread
401 CString *mp=new CString; //memory is released by destination routine!
403 PostThreadMessage(WSA_EVENT,wp,(LPARAM)mp); //this goes to WSA_EVENT
407 // For setting the Event completion, pass information back to calling thread
410 SetEvent(CMyUIThread::m_hEventThreadKilled);
418 void CWinAfsLoadApp::NotifyFromUI(WPARAM wp,const char *msg)
419 {// called from UIThread to notify return status
423 mp=new CString; //memory is released by destination routine!
425 TRACE("CWinAfsLoadApp::Notify wp=%d,msg=[%s]\n",wp,msg);
427 TRACE("CWinAfsLoadApp::Notify wp=%d,msg=NULL\n",wp);
429 PostThreadMessage(WM_UIONPARM,wp,(LPARAM)mp);
432 LRESULT CWinAfsLoadApp::OnNotifyReturn(WPARAM wParam, LPARAM lParam)
435 return ((CWinAfsLoadDlg *)m_pMainWnd)->OnNotifyReturn(wParam,lParam);
439 LRESULT CWinAfsLoadApp::OnAfsEvent(WPARAM wParam, LPARAM lParam)
442 return ((CWinAfsLoadDlg *)m_pMainWnd)->OnAfsEvent(wParam,lParam);