win95-initial-port-20010430
[openafs.git] / src / WINNT / win9xpanel / ProgBarDlg.cpp
1 /* Copyright 2000, International Business Machines Corporation and others.
2         All Rights Reserved.
3  
4         This software has been released under the terms of the IBM Public
5         License.  For details, see the LICENSE file in the top-level source
6         directory or online at http://www.openafs.org/dl/license10.html
7 */
8 // ProgBarDlg.cpp : implementation file
9 //
10
11 #include "stdafx.h"
12 #include "MyFrame.h"
13 #include "WinAfsLoad.h"
14 #include "ProgBarDlg.h"
15 #include "share.h"
16 //#include <process.h>
17
18 #ifdef _DEBUG
19 #define new DEBUG_NEW
20 #undef THIS_FILE
21 static char THIS_FILE[] = __FILE__;
22 #endif
23
24 /////////////////////////////////////////////////////////////////////////////
25 // CProgBarDlg dialog
26
27
28 CProgBarDlg::CProgBarDlg(CWnd* pParent /*=NULL*/)
29         : CDialog(CProgBarDlg::IDD, pParent)
30 {
31         //{{AFX_DATA_INIT(CProgBarDlg)
32         m_sAuthenicateTime = _T("");
33         m_sBackgroundTime = _T("");
34         m_sMountTime = _T("");
35         //}}AFX_DATA_INIT
36         m_pParent=pParent;
37 }
38
39 CProgBarDlg::~CProgBarDlg()
40 {
41         // nitwit do not expect any local variables to hang around
42 }
43
44 void CProgBarDlg::DoDataExchange(CDataExchange* pDX)
45 {
46         CDialog::DoDataExchange(pDX);
47         //{{AFX_DATA_MAP(CProgBarDlg)
48         DDX_Control(pDX, IDC_CHECKAUTHENICATION, m_cAuthenicate);
49         DDX_Control(pDX, IDC_CHECKMOUNT, m_cMount);
50         DDX_Control(pDX, IDC_CHECKBACKGROUND, m_cBackground);
51         DDX_Control(pDX, IDC_STATUSREGION, m_cStatusRegion);
52         DDX_Control(pDX, IDC_COMPLETE_MOUNT, m_cCompleteMount);
53         DDX_Control(pDX, IDC_COMPLETE_ENABLE, m_cCompleteEnable);
54         DDX_Control(pDX, IDC_COMPLETE_AUTH, m_cCompleteAuth);
55         DDX_Text(pDX, IDC_TIMEAUTHENICATION, m_sAuthenicateTime);
56         DDX_Text(pDX, IDC_TIMEBACKGROUND, m_sBackgroundTime);
57         DDX_Text(pDX, IDC_TIMEMOUNT, m_sMountTime);
58         //}}AFX_DATA_MAP
59 }
60
61
62 BEGIN_MESSAGE_MAP(CProgBarDlg, CDialog)
63         //{{AFX_MSG_MAP(CProgBarDlg)
64         ON_WM_QUERYDRAGICON()
65         ON_WM_TIMER()
66         ON_MESSAGE( WM_PROGRESSPARM,OnParm)
67         ON_WM_DESTROY()
68         ON_WM_PAINT()
69         //}}AFX_MSG_MAP
70         ON_MESSAGE(WSA_EVENT, OnWSAEvent)
71 END_MESSAGE_MAP()
72
73 /////////////////////////////////////////////////////////////////////////////
74 // CProgBarDlg message handlers
75
76 BOOL CProgBarDlg::OnInitDialog()
77 {
78         CDialog::OnInitDialog();
79
80         // Set the icon for this dialog.  The framework does this automatically
81         //  when the application's main window is not a dialog
82         SetIcon(m_hIcon, TRUE);                 // Set big icon
83         SetIcon(m_hIcon, FALSE);                // Set small icon
84         
85         // TODO: Add extra initialization here
86         
87         m_Option=0;
88         SetFocus();
89         m_uStatus=0;
90         m_Socket = INVALID_SOCKET;
91 //      m_sLog="";
92         m_cBackground.GetWindowText(m_sDefaultBackground.GetBuffer(32),31);
93         m_cAuthenicate.GetWindowText(m_sDefaultAuthenicate.GetBuffer(32),32);
94         m_cMount.GetWindowText(m_sDefaultMount.GetBuffer(32),32);
95         return TRUE;  // return TRUE  unless you set the focus to a control
96 }
97
98 // If you add a minimize button to your dialog, you will need the code below
99 //  to draw the icon.  For MFC applications using the document/view model,
100 //  this is automatically done for you by the framework.
101
102
103 HCURSOR CProgBarDlg::OnQueryDragIcon()
104 {
105         return (HCURSOR) m_hIcon;
106 }
107
108 void CProgBarDlg::OnTimer(UINT nIDEvent) 
109 {
110         // TODO: Add your message handler code here and/or call default
111
112         CString msg;
113         m_iOrientation=++m_iOrientation % 2;
114         CRect s1;
115         m_cStatusRegion.GetWindowRect(&s1);
116         ScreenToClient(&s1);
117         InvalidateRect(&s1,TRUE);
118         switch (nIDEvent)
119         {
120         case WM_PROGRESSTIMER:
121                 {
122                 CTimeSpan elapsed=::CTime::GetCurrentTime()-m_StartTime;
123                 switch (m_Current)
124                 {
125                 case 1:
126                         m_sBackgroundTime.Format("%02d:%02d",elapsed.GetTotalMinutes()
127                                 ,elapsed.GetTotalSeconds()-60*elapsed.GetTotalMinutes());
128                         UpdateData(FALSE);
129                         break;
130                 case 2:
131                         m_sAuthenicateTime.Format("%02d:%02d",elapsed.GetTotalMinutes()
132                                 ,elapsed.GetTotalSeconds()-60*elapsed.GetTotalMinutes());
133                         UpdateData(FALSE);
134                         break;
135                 case 4:
136                         m_sMountTime.Format("%02d:%02d",elapsed.GetTotalMinutes()
137                                 ,elapsed.GetTotalSeconds()-60*elapsed.GetTotalMinutes());
138                         UpdateData(FALSE);
139                         break;
140                 default:
141                         break;
142                 }
143                 }
144                 break;
145         default:
146                 break;
147         }
148         CDialog::OnTimer(nIDEvent);
149 }
150
151 LRESULT CProgBarDlg::OnParm(WPARAM wp, LPARAM lp)
152 {
153         switch (wp)
154         {
155         case ProgFSetTitle:
156                 if (lp==0) break;
157                 {
158                 char *msg=(char *)lp;
159                 if (strlen(msg))
160                         m_cBackground.SetWindowText(msg);
161                 msg+=strlen(msg)+1;
162                 if (strlen(msg))
163                         m_cAuthenicate.SetWindowText(msg);
164                 msg+=strlen(msg)+1;
165                 if (strlen(msg))
166                         m_cMount.SetWindowText(msg);
167                 free((char *)lp);
168                 }
169                 break;
170         case ProgFShow:
171                 ShowWindow(SW_SHOW);
172                 m_nTimer=SetTimer(WM_PROGRESSTIMER,1000,0);
173                 break;
174         case ProgFHide:
175                 ShowWindow(SW_HIDE);
176                 KillTimer(m_nTimer);
177                 m_cBackground.SetWindowText(m_sDefaultBackground);
178                 m_cAuthenicate.SetWindowText(m_sDefaultAuthenicate);
179                 m_cMount.SetWindowText(m_sDefaultMount);
180                 break;
181         case ProgFOpt:
182                 ShowWindow(SW_SHOW);
183                 m_Option=(UINT) lp;
184                 m_sBackgroundTime="";
185                 m_sAuthenicateTime="";
186                 m_sMountTime="";
187                 UpdateData(FALSE);
188                 m_iOrientation=0;
189                 if (m_Option & 1)
190                 {
191                         m_cBackground.ModifyStyle(WS_DISABLED,0,0);
192                 } else {
193                         m_cBackground.ModifyStyle(0,WS_DISABLED,0);
194                 }
195                 if (m_Option & 2)
196                         m_cAuthenicate.ModifyStyle(WS_DISABLED,0,0);
197                 else
198                         m_cAuthenicate.ModifyStyle(0,WS_DISABLED,0);
199                 if (m_Option & 4)
200                         m_cMount.ModifyStyle(WS_DISABLED,0,0);
201                 else
202                         m_cMount.ModifyStyle(0,WS_DISABLED,0);
203                 m_Current=0;
204                 m_nTimer=SetTimer(WM_PROGRESSTIMER,1000,0);
205                 ShowWindow(SW_SHOW);
206                 break;
207         case ProgFNext:
208                 m_iOrientation=0;
209                 switch(m_Current)
210                 {
211                 case 0:
212                         m_Current=1;
213                         if (m_Option & m_Current)
214                         {
215                                 CWnd* pWnd=GetDlgItem(IDC_TIMEBACKGROUND);
216                                 if (pWnd)
217                                         pWnd->SetFocus();
218                                 m_StartTime=CTime::GetCurrentTime();
219                                 OnTimer(WM_PROGRESSTIMER);
220                                 break;
221                         }
222                 case 1:
223                         m_Current=2;
224                         if (m_Option & m_Current)
225                         {
226                                 OnTimer(WM_PROGRESSTIMER);
227                                 CWnd* pWnd=GetDlgItem(IDC_TIMEAUTHENICATION);
228                                 if (pWnd)
229                                         pWnd->SetFocus();
230                                 m_StartTime=CTime::GetCurrentTime();
231                                 break;
232                         }
233                 case 2:
234                         m_Current=4;
235                         if (m_Option & m_Current)
236                         {
237                                 OnTimer(WM_PROGRESSTIMER);
238                                 CWnd* pWnd=GetDlgItem(IDC_TIMEMOUNT);
239                                 if (pWnd)
240                                         pWnd->SetFocus();
241                                 m_StartTime=CTime::GetCurrentTime();
242                                 break;
243                         }
244                 default:
245                         m_Current=0;
246                         break;
247                 }
248                 break;
249         default:
250                 break;
251         }
252         CRect s1;
253         m_cStatusRegion.GetWindowRect(&s1);
254         ScreenToClient(&s1);
255         InvalidateRect(&s1,TRUE);
256         return 0;
257 }
258
259 BOOL CProgBarDlg::Create()
260 {
261         return CDialog::Create(IDD, m_pParent);
262 }
263
264 void CProgBarDlg::PostNcDestroy() 
265 {
266         // TODO: Add your specialized code here and/or call the base class
267         if (m_Socket != INVALID_SOCKET)
268                 WSAAsyncSelect(m_Socket, m_hWnd, 0, 0); //cancel out messages
269         if (m_Socket != INVALID_SOCKET)
270                 closesocket(m_Socket);
271         delete this;
272 }
273
274 void CProgBarDlg::OnDestroy() 
275 {
276         KillTimer(m_nTimer);
277         Sleep(50);
278         CDialog::OnDestroy();
279         // TODO: Add your message handler code here
280 }
281
282 VOID CProgBarDlg::DisConnect()
283 {
284         if (m_Socket != INVALID_SOCKET)
285         {
286                 shutdown(m_Socket,3);
287                 char cBuffer[AFS_MAX_MSG_LEN];
288                 DWORD uStatus=1;
289                 int loop=SOCKETIO/250;
290                 while ((uStatus !=SOCKET_ERROR)&&(uStatus!=0) &&(loop-- >0))
291                 {
292                         Sleep(250);
293                         uStatus = recv(m_Socket, (char FAR *)&cBuffer, AFS_MAX_MSG_LEN, 0 );
294                 }
295                 closesocket(m_Socket);
296                 m_Socket = INVALID_SOCKET;
297         }
298         WSAAsyncSelect(m_Socket, m_hWnd, 0, 0); //cancel out messages
299         LOG("Socket Disconnect");
300 }
301
302 typedef struct SoCkErR {
303         int                     soc_err;
304         LPSTR           szSockErr;
305 } SOC_ERR;
306
307 SOC_ERR socket_error[] = {
308         { WSANOTINITIALISED     , "A successful WSAStartup must occur before using this function."},
309         { WSAENETDOWN           ,       "The Windows Sockets implementation has detected that the network "\
310                                                         "subsystem has failed."},
311         { WSAENOTCONN           ,       "The socket is not connected."},
312         { WSAEINTR                      ,       "The (blocking) call was canceled using WSACancelBlockingCall."},
313         { WSAEINPROGRESS        ,       "A blocking Windows Sockets operation is in progress."},
314         { WSAENOTSOCK           ,       "The descriptor is not a socket."},
315         { WSAEOPNOTSUPP         ,       "MSG_OOB was specified, but the socket is not of type SOCK_STREAM."},
316         { WSAESHUTDOWN          ,       "The socket has been shut down; it is not possible to recv on a socket "\
317                                                         "after shutdown has been invoked with how set to 0 or 2."},
318         { WSAEWOULDBLOCK        ,       "The socket is marked as nonblocking and the receive operation would block."},
319         { WSAEMSGSIZE           ,       "The datagram was too large to fit into the specified buffer and was truncated."},
320         { WSAEINVAL                     ,       "The socket has not been bound with bind."},
321         { WSAECONNABORTED       ,       "The virtual circuit was aborted due to timeout or other failure."},
322         { WSAECONNRESET         ,       "The virtual circuit was reset by the remote side."},
323         { WSAEACCES                     ,       "The requested address is a broadcast address, but the appropriate flag was not set."},
324         { WSAEFAULT                     ,       "The buf argument is not in a valid part of the user address space."},
325         { WSAENETRESET          ,       "The connection must be reset because the Windows Sockets implementation dropped it."},
326         { WSAENOBUFS            ,       "The Windows Sockets implementation reports a buffer deadlock."},
327         { WSAENOTCONN           ,       "The socket is not connected."},
328         { WSAEWOULDBLOCK        ,       " The socket is marked as nonblocking and the requested operation would block."},
329         { -1                            ,       "Unknown Socket Error"}
330 };
331
332 LPCSTR show_socket_error()
333 {
334         INT soc_err=WSAGetLastError();
335         SOC_ERR *lperr = &socket_error[0];
336         while (1 && lperr->soc_err != -1)
337         {
338                 if (lperr->soc_err == soc_err)
339                 {
340                         return lperr->szSockErr;
341                 }
342                 lperr++;
343         }
344         return "Unknown Socket Error";
345 }
346
347
348 BOOL CProgBarDlg::Connect(CString &sStatus)
349 {
350         CHAR szBuff[80];
351         UINT dwSize = sizeof(szBuff);
352         SOCKADDR_IN socketAddress;
353         m_Socket = socket( AF_INET, SOCK_DGRAM, 0);
354         if (m_Socket == INVALID_SOCKET) {
355                 sStatus="Socket() failed";
356                 return FALSE;
357         }
358         if (gethostname(szBuff, dwSize)!=0)
359         {
360                 sStatus.Format("Get Host Name - %s", show_socket_error());
361                 return FALSE;
362         }
363         socketAddress.sin_family = AF_INET;
364     socketAddress.sin_addr.s_addr = INADDR_ANY;
365         socketAddress.sin_port = htons(AFS_MSG_PORT);        /* Convert to network ordering */
366         if (bind(m_Socket, (struct sockaddr FAR *) &socketAddress, sizeof(socketAddress)) == SOCKET_ERROR) {
367                 sStatus.Format("Bind :%s", show_socket_error());
368                 return FALSE;
369         }
370         if ((m_uStatus = WSAAsyncSelect( m_Socket,GetSafeHwnd(), WSA_EVENT, FD_READ | FD_CLOSE )) > 0) {
371                 WSAAsyncSelect( m_Socket, GetSafeHwnd(), 0, 0);
372                 sStatus.Format("Get Host Name: ", show_socket_error());
373                 return FALSE;
374         }
375         m_bReadyToSend=TRUE;
376         sStatus="Success Connect";
377         LOG("Socket Connect");
378         return TRUE;
379 }
380
381 LRESULT CProgBarDlg::OnWSAEvent(WPARAM wParam, LPARAM lParam)
382 {
383         CString sLog;
384         if (WSAGETSELECTERROR( lParam ) != 0) { 
385 //              m_sLog.Format("Read Failure status=%d\r\n", WSAGETSELECTERROR(lParam));
386                 WSAAsyncSelect( m_Socket,GetSafeHwnd(), 0, 0);
387                 m_uStatus=WSAGETSELECTERROR( lParam );
388                 LOG("WSA Select error");
389                 return 0;
390         }
391         switch (WSAGETSELECTEVENT(lParam))
392         {
393         case FD_READ:
394                 wParam=Decode(wParam,sLog);
395                 LOG("OnWSAEvent FD_READ(%d)",wParam);
396                 CWINAFSLOADAPP->WSANotifyFromUI(wParam,(const char *)sLog);
397                 break;
398         case FD_CLOSE:
399                 LOG("OnWSAEvent FD_CLOSE(%d)",wParam);
400                 break;
401         case FD_WRITE:
402                 LOG("OnWSAEvent FD_WRITE(%d)",wParam);
403                 break;
404         default:
405                 LOG("OnWSAEvent Unknown Response");
406                 break;
407         }
408         return 0;
409 }
410
411 // this routine will set the notfication return (wParam) for WinAfsLoadDlg to receive
412 // if problem with decoding result return AFS_EXITCODE_GENERAL_FAILURE
413 // Other returns are:
414 //              AFS_EXITCODE_NORMAL(0) ,AFS_EXITCODE_PANIC(2) ,AFS_EXITCODE_NETWORK_FAILURE(3)
415 UINT CProgBarDlg::Decode(WPARAM wParam,CString &sLog)
416 {
417         m_bReadyToSend=FALSE;
418         m_uStatus = recv((SOCKET)wParam, (char FAR *)&m_buffer, AFS_MAX_MSG_LEN, 0 );
419         if (m_uStatus==0){
420                 sLog.Format("Receive Error: %s", show_socket_error());
421                 return AFS_EXITCODE_GENERAL_FAILURE;
422         }
423         SOCKETTYPE *p=(SOCKETTYPE *)m_buffer;
424         switch (p->header.msgtype)
425         {
426         case AFS_MSG_STATUS_CHANGE:
427                 {
428                 _afsMsg_statChange *sp=(_afsMsg_statChange *)p;
429                 switch (sp->exitCode)
430                 {
431                 case AFS_EXITCODE_NORMAL:
432                         {
433                         if ((m_uStatus = WSAAsyncSelect( m_Socket,GetSafeHwnd(),WSA_EVENT, FD_READ | FD_CLOSE )) > 0) {
434                                 WSAAsyncSelect( m_Socket, GetSafeHwnd(), 0, 0);
435                                 sLog.Format("WSAAsyncSelect: %s", show_socket_error());
436                                 return AFS_EXITCODE_GENERAL_FAILURE;
437                         }
438                         int len=sp->hdr.length-sizeof(*sp);
439                         if (len>0)
440                         {
441                                 char *x=new char[len+1];
442                                 strncpy(x,&(sp->message),len);
443                                 *(x+len)=0;
444                                 sLog.Format("AFSD operation normal Message#%s#",x);
445                                 delete x;
446                         } else {
447                                 sLog="AFSD operation normal";
448                         }
449                         }
450                         break;
451                 case AFS_EXITCODE_PANIC:
452                         sLog="AFS Client Console Panic Exit";
453                         break;
454                 case AFS_EXITCODE_NETWORK_FAILURE:
455                         sLog="AFS Client Network Failure Exit";
456                         break;
457                 default:
458                         sLog.Format("Receive Error: Messsage format(exit code)=%0x",p->change.exitCode);
459                         p->change.exitCode=AFS_EXITCODE_GENERAL_FAILURE;
460                         break;
461                 }
462                 return (p->change.exitCode);
463                 }
464         case AFS_MSG_PRINT:
465                 if ((m_uStatus = WSAAsyncSelect( m_Socket, GetSafeHwnd(),WSA_EVENT, FD_READ | FD_CLOSE )) > 0) {
466                         WSAAsyncSelect( m_Socket, GetSafeHwnd(), 0, 0);
467                         sLog.Format("WSAAsyncSelect: %s", show_socket_error());
468                         return AFS_EXITCODE_GENERAL_FAILURE;
469                 }
470                 return AFS_EXITCODE_GENERAL_FAILURE;
471         default:
472                 sLog.Format("Receive Error: Messsage format=%0x",p->header.msgtype);
473                 return AFS_EXITCODE_GENERAL_FAILURE;
474         }
475 }
476
477
478
479 void CProgBarDlg::OnPaint() 
480 {
481         CPaintDC dc(this); // device context for painting
482         
483         // TODO: Add your message handler code here
484         //m_iCurrent is the Current item (1,2,4)
485         //m_iOption is which are active
486         
487         CBrush brBack,brGreen,brYellow;
488         brBack.CreateSolidBrush(GetSysColor(COLOR_MENU));
489         brGreen.CreateSolidBrush(RGB(0,255,0));
490         brYellow.CreateSolidBrush(RGB(255,255,0));
491         CRect shape;
492         switch (m_Current)
493         {
494         case 1: //progress
495                 SetShape(shape,m_Current,m_iOrientation);
496                 dc.SelectObject(&brYellow);
497                 dc.Ellipse(shape);
498                 SetShape(shape,m_Option & 2,0);
499                 if (shape.Width())
500                 {
501                         dc.SelectObject(&brBack);
502                         dc.Ellipse(shape);
503                 }
504                 SetShape(shape,m_Option & 4,0);
505                 if (shape.Width())
506                 {
507                         dc.SelectObject(&brBack);
508                         dc.Ellipse(shape);
509                 }
510                 break;
511         case 2:
512                 SetShape(shape,m_Option & 1,0);
513                 if (shape.Width())
514                 {
515                         dc.SelectObject(&brGreen);
516                         dc.Ellipse(shape);
517                 }
518                 SetShape(shape,m_Current,m_iOrientation);
519                 if (shape.Width())
520                 {
521                         dc.SelectObject(&brYellow);
522                         dc.Ellipse(shape);
523                 }
524                 SetShape(shape,m_Option & 4,0);
525                 if (shape.Width())
526                 {
527                         dc.SelectObject(&brBack);
528                         dc.Ellipse(shape);
529                 }
530                 break;
531         case 4:
532                 SetShape(shape,m_Option & 1,0);
533                 if (shape.Width())
534                 {
535                         dc.SelectObject(&brGreen);
536                         dc.Ellipse(shape);
537                 }
538                 SetShape(shape,m_Option & 2,0);
539                 if (shape.Width())
540                 {
541                         dc.SelectObject(&brGreen);
542                         dc.Ellipse(shape);
543                 }
544                 SetShape(shape,m_Current,m_iOrientation);
545                 if (shape.Width())
546                 {
547                         dc.SelectObject(&brYellow);
548                         dc.Ellipse(shape);
549                 }
550                 break;
551         default:
552                 break;
553         }
554         // Do not call CDialog::OnPaint() for painting messages
555 }
556
557 void CProgBarDlg::SetShape(CRect &shape,int option,int orientation)
558 {
559         CRect s1,s2,rect;
560         switch(option)
561         {
562         case 1:
563                 m_cCompleteEnable.GetWindowRect(&s1);
564                 break;
565         case 2:
566                 m_cCompleteAuth.GetWindowRect(&s1);
567                 break;
568         case 4:
569                 m_cCompleteMount.GetWindowRect(&s1);
570                 break;
571         default:
572                 s1=CRect(0,0,0,0);
573                 break;
574         }
575         ScreenToClient(&s1);
576         int delta=(s1.Width()-s1.Height())/2;
577         s2.top=s1.top-delta;
578         s2.bottom=s1.bottom+delta;
579         s2.left=s1.left+delta;
580         s2.right=s1.right-delta;
581         rect.top=s2.top;
582         rect.bottom=s2.bottom;
583         rect.left=s1.left;
584         rect.right=s1.right;
585         switch (orientation)
586         {
587         case 1:
588                 shape=s2;
589                 break;
590         default:
591                 shape=s1;
592                 break;
593         }
594 }
595