2 * Copyright 2000, International Business Machines Corporation and others.
5 * This software has been released under the terms of the IBM Public
6 * License. For details, see the LICENSE file in the top-level source
7 * directory or online at http://www.openafs.org/dl/license10.html
14 #include <afsconfig.h>
15 #include <afs/param.h>
20 #include <afs/cm_config.h>
23 #include "afs_config.h"
24 #include "tab_general.h"
25 #include "tab_hosts.h"
26 #include "tab_advanced.h"
32 * VARIABLES __________________________________________________________________
39 BOOL fWarnIfNotStopped;
40 BOOL fRestartIfStopped;
41 BOOL fServiceIsRunning;
48 * DEFINITIONS ________________________________________________________________
54 #define cmsecIDLE_REFRESH 10000
55 #define cmsecFAST_REFRESH 1000
59 * PROTOTYPES _________________________________________________________________
63 void GeneralTab_OnInitDialog (HWND hDlg);
64 void GeneralTab_OnTimer (HWND hDlg);
65 BOOL GeneralTab_OnApply (HWND hDlg, BOOL fForce, BOOL fComplainIfInvalid);
66 void GeneralTab_OnRefresh (HWND hDlg, BOOL fRequery);
67 void GeneralTab_OnStartStop (HWND hDlg, BOOL fStart);
68 void GeneralTab_OnConnect (HWND hDlg);
69 void GeneralTab_OnGateway (HWND hDlg);
70 void GeneralTab_OnCell (HWND hDlg);
72 void GeneralTab_DoStartStop (HWND hDlg, BOOL fStart, BOOL fRestart);
73 void GeneralTab_FixRefreshTimer (HWND hDlg, UINT cmsec = 0);
74 DWORD GeneralTab_GetDisplayState (HWND hDlg);
75 void GeneralTab_ShowCurrentState (HWND hDlg);
76 BOOL GeneralTab_AskIfStopped (HWND hDlg);
78 BOOL fIsCellInCellServDB (LPCTSTR pszCell);
80 BOOL CALLBACK Status_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
81 void Status_OnRefresh (HWND hDlg);
84 * ROUTINES ___________________________________________________________________
88 BOOL CALLBACK GeneralTab_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
93 Main_OnInitDialog (GetParent(hDlg));
94 GeneralTab_OnInitDialog (hDlg);
98 GeneralTab_FixRefreshTimer (hDlg, 0);
102 GeneralTab_OnTimer (hDlg);
109 if (!GeneralTab_OnApply (hDlg, FALSE, TRUE))
110 SetWindowLongPtr (hDlg, DWLP_MSGRESULT, TRUE);
111 else if (g.fIsWinNT && !GeneralTab_AskIfStopped (hDlg))
112 SetWindowLongPtr (hDlg, DWLP_MSGRESULT, TRUE);
116 GeneralTab_OnRefresh (hDlg, FALSE);
119 case IDC_SERVICE_START:
120 GeneralTab_OnStartStop (hDlg, TRUE);
123 case IDC_SERVICE_STOP:
124 GeneralTab_OnStartStop (hDlg, FALSE);
127 case IDC_GATEWAY_CONN:
128 GeneralTab_OnConnect (hDlg);
132 GeneralTab_OnGateway (hDlg);
136 GeneralTab_OnCell (hDlg);
140 GeneralTab_DlgProc (hDlg, WM_HELP, 0, 0);
147 WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCONFIG_GENERAL_NT);
149 WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCONFIG_GENERAL_95);
157 void GeneralTab_OnInitDialog (HWND hDlg)
159 DWORD CurrentState = Config_GetServiceState();
160 BOOL fNeedFastRefresh = ((CurrentState == SERVICE_STOPPED) || (CurrentState == SERVICE_RUNNING)) ? FALSE : TRUE;
161 GeneralTab_FixRefreshTimer (hDlg, ((fNeedFastRefresh) ? cmsecFAST_REFRESH : cmsecIDLE_REFRESH));
162 GeneralTab_OnTimer (hDlg);
163 GeneralTab_OnRefresh (hDlg, TRUE);
167 BOOL GeneralTab_VerifyCell (HWND hDlg, BOOL fComplainIfInvalid, LPCTSTR pszCell)
169 TCHAR szNoCell[ cchRESOURCE ];
170 GetString (szNoCell, IDS_CELL_UNKNOWN);
172 TCHAR szCell[ cchRESOURCE ];
174 lstrcpy (szCell, pszCell);
176 GetDlgItemText (hDlg, IDC_CELL, szCell, cchRESOURCE);
177 if ((!szCell[0]) || (!lstrcmpi (szNoCell, szCell)))
179 if (fComplainIfInvalid)
182 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_NOCELL_DESC);
184 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADGATEWAY_DESC);
189 if (!fIsCellInCellServDB (szCell))
191 if (fComplainIfInvalid)
194 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADCELL_DESC);
196 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADGWCELL_DESC, TEXT("%s"), szCell);
205 BOOL GeneralTab_VerifyOK (HWND hDlg, BOOL fComplainIfInvalid)
207 // If it's Windows 95, make sure there's a valid Gateway entry
211 TCHAR szGateway[ cchRESOURCE ];
212 GetDlgItemText (hDlg, IDC_GATEWAY, szGateway, cchRESOURCE);
215 if (fComplainIfInvalid)
216 Message (MB_ICONASTERISK | MB_OK, IDS_NOGATEWAY_TITLE, IDS_NOGATEWAY_DESC);
221 // Make sure the cell is in our CellServDB.
225 if (!GeneralTab_VerifyCell (hDlg, fComplainIfInvalid, NULL))
232 BOOL GeneralTab_OnApply (HWND hDlg, BOOL fForce, BOOL fComplainIfInvalid)
236 // Don't try to do anything if we've already failed the apply
237 if (GetWindowLongPtr (hDlg, DWLP_MSGRESULT))
241 // If the user has changed CellServDB, configuration parameters for
242 // the driver or anything else, we want to commit those changes first.
243 // We *won't* commit server prefs changes yet, because we haven't yet
244 // checked to see if the service is running.
246 if (!HostsTab_CommitChanges (fForce))
249 if (!AdvancedTab_CommitChanges (fForce))
252 if (!GeneralTab_VerifyOK (hDlg, fComplainIfInvalid))
255 TCHAR szText[ MAX_PATH ];
259 GetDlgItemText (hDlg, IDC_CELL, szText, MAX_PATH);
260 if (lstrcmpi (szText, g.Configuration.szCell))
262 if (!Config_SetCellName (szText))
264 lstrcpy (g.Configuration.szCell, szText);
268 BOOL fLogonAuthent = IsDlgButtonChecked (hDlg, IDC_LOGON);
269 if (fLogonAuthent != g.Configuration.fLogonAuthent)
271 SetBitLogonOption(fLogonAuthent,LOGON_OPTION_INTEGRATED);
272 g.Configuration.fLogonAuthent = fLogonAuthent;
275 if (GetDlgItem(hDlg, IDC_TRAYICON) != NULL)
276 Config_SetTrayIconFlag (IsDlgButtonChecked(hDlg, IDC_TRAYICON));
280 BOOL fBeGateway = IsDlgButtonChecked (hDlg, IDC_GATEWAY);
281 if (fBeGateway != g.Configuration.fBeGateway)
283 if (!Config_SetGatewayFlag (fBeGateway))
285 g.fNeedRestart = TRUE;
286 g.Configuration.fBeGateway = fBeGateway;
289 else // (!g.fIsWinNT)
291 GetDlgItemText (hDlg, IDC_GATEWAY, szText, MAX_PATH);
292 if (lstrcmpi (szText, g.Configuration.szGateway))
294 TCHAR szNewCell[ MAX_PATH ];
295 if (!Config_ContactGateway (szText, szNewCell))
297 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADGATEWAY_DESC);
301 if (!GeneralTab_VerifyCell (hDlg, fComplainIfInvalid, szNewCell))
304 if (!Config_SetGatewayName (szText))
307 if (!Config_SetCellName (szNewCell))
310 Config_FixGatewayDrives();
312 SetDlgItemText (hDlg, IDC_CELL, szNewCell);
313 lstrcpy (g.Configuration.szGateway, szText);
314 lstrcpy (g.Configuration.szCell, szNewCell);
316 GeneralTab_OnGateway (hDlg);
324 void GeneralTab_OnRefresh (HWND hDlg, BOOL fRequery)
326 // If necessary, update any fields in g.Configuration that we care about
331 Config_GetGatewayFlag (&g.Configuration.fBeGateway);
333 Config_GetGatewayName (g.Configuration.szGateway);
335 Config_GetCellName (g.Configuration.szCell);
336 g.Configuration.fLogonAuthent=RWLogonOption(TRUE,LOGON_OPTION_INTEGRATED);
337 Config_GetTrayIconFlag (&g.Configuration.fShowTrayIcon);
340 SetDlgItemText (hDlg, IDC_GATEWAY, g.Configuration.szGateway);
342 CheckDlgButton (hDlg, IDC_GATEWAY, g.Configuration.fBeGateway);
344 SetDlgItemText (hDlg, IDC_CELL, g.Configuration.szCell);
345 CheckDlgButton (hDlg, IDC_LOGON, g.Configuration.fLogonAuthent);
346 CheckDlgButton (hDlg, IDC_TRAYICON, g.Configuration.fShowTrayIcon);
349 // Update our display of the service's status
351 DWORD CurrentState = Config_GetServiceState();
352 BOOL fIfServiceStopped = !(g.fIsWinNT && !g.fIsAdmin);
353 BOOL fIfServiceRunning = fIfServiceStopped && (CurrentState == SERVICE_RUNNING);
355 GeneralTab_ShowCurrentState (hDlg);
357 EnableWindow (GetDlgItem (hDlg, IDC_CELL), fIfServiceStopped && g.fIsWinNT);
359 EnableWindow (GetDlgItem (hDlg, IDC_LOGON), fIfServiceStopped);
360 EnableWindow (GetDlgItem (hDlg, IDC_GATEWAY), fIfServiceStopped);
362 // Update our warning. Note that under WinNT, this tab doesn't have any
363 // controls (other than Start Service) which disable just because the
364 // service isn't running...so don't show that warning in that case.
366 TCHAR szText[ cchRESOURCE ];
367 if ((!g.fIsWinNT) && (CurrentState != SERVICE_RUNNING))
369 GetString (szText, IDS_WARN_STOPPED);
370 SetDlgItemText (hDlg, IDC_WARN, szText);
371 ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_SHOW);
373 else if (g.fIsWinNT && !g.fIsAdmin)
375 GetString (szText, IDS_WARN_ADMIN);
376 SetDlgItemText (hDlg, IDC_WARN, szText);
377 ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_SHOW);
379 else // ((CurrentState == SERVICE_RUNNING) && (g.fIsAdmin))
381 ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_HIDE);
384 GeneralTab_OnGateway (hDlg);
386 // If the service isn't running/stopped, we may need to complain
388 if ((CurrentState == SERVICE_RUNNING) && (l.fWarnIfNotStopped))
390 Message (MB_ICONHAND, GetErrorTitle(), IDS_SERVICE_FAIL_STOP, TEXT("%08lX"), ERROR_SERVICE_SPECIFIC_ERROR);
392 else if ((CurrentState == SERVICE_STOPPED) && (l.fWarnIfStopped))
394 Message (MB_ICONHAND, GetErrorTitle(), IDS_SERVICE_FAIL_START, TEXT("%08lX"), ERROR_SERVICE_SPECIFIC_ERROR);
397 if ((CurrentState == SERVICE_RUNNING) || (CurrentState == SERVICE_STOPPED))
399 BOOL fRestart = ((CurrentState == SERVICE_STOPPED) && (l.fRestartIfStopped));
400 l.fWarnIfStopped = FALSE;
401 l.fWarnIfNotStopped = FALSE;
402 l.fRestartIfStopped = FALSE;
403 l.fServiceIsRunning = (CurrentState == SERVICE_RUNNING);
407 GeneralTab_DoStartStop (hDlg, TRUE, FALSE);
413 void GeneralTab_OnTimer (HWND hDlg)
415 DWORD CurrentState = Config_GetServiceState();
416 DWORD DisplayState = GeneralTab_GetDisplayState(hDlg);
417 TestAndDoMapShare(CurrentState); //Re map mounted drives if necessary
419 BOOL fInEndState = ((CurrentState == SERVICE_RUNNING) || (CurrentState == SERVICE_STOPPED));
420 if (fInEndState && l.hStatus)
422 if (IsWindow (l.hStatus))
423 DestroyWindow (l.hStatus);
426 else if (!fInEndState && !l.hStatus)
428 l.hStatus = ModelessDialog (IDD_STARTSTOP, GetParent (hDlg), (DLGPROC)Status_DlgProc);
431 if (CurrentState != DisplayState)
433 GeneralTab_OnRefresh (hDlg, FALSE);
434 Main_RefreshAllTabs();
436 if (l.hStatus && IsWindow (l.hStatus))
437 PostMessage (l.hStatus, WM_COMMAND, IDINIT, 0);
440 BOOL fNeedFastRefresh = ((CurrentState == SERVICE_STOPPED) || (CurrentState == SERVICE_RUNNING)) ? FALSE : TRUE;
441 BOOL fHaveFastRefresh = ((DisplayState == SERVICE_STOPPED) || (DisplayState == SERVICE_RUNNING)) ? FALSE : TRUE;
443 if (fNeedFastRefresh != fHaveFastRefresh)
445 GeneralTab_FixRefreshTimer (hDlg, ((fNeedFastRefresh) ? cmsecFAST_REFRESH : cmsecIDLE_REFRESH));
450 void GeneralTab_OnStartStop (HWND hDlg, BOOL fStart)
452 BOOL fSuccess = FALSE;
455 // Don't let the user stop the service on a whim; warn him first
459 if (Message (MB_ICONEXCLAMATION | MB_OKCANCEL, GetCautionTitle(), IDS_STOP_DESC) != IDOK)
463 // To start the service, we'll need to successfully commit our new
464 // configuration. To stop the service, we'll *try*, but it's not
465 // fatal if something goes wrong.
467 if (!GeneralTab_OnApply (hDlg, TRUE, ((fStart) ? TRUE : FALSE)))
473 // Okay, start the service
475 GeneralTab_DoStartStop (hDlg, fStart, FALSE);
479 void GeneralTab_OnConnect (HWND hDlg)
481 if (!GeneralTab_OnApply (hDlg, TRUE, TRUE))
483 GeneralTab_OnGateway (hDlg);
484 GeneralTab_OnApply (hDlg, TRUE, TRUE);
488 void GeneralTab_OnGateway (HWND hDlg)
492 TCHAR szGateway[ cchRESOURCE ];
493 GetDlgItemText (hDlg, IDC_GATEWAY, szGateway, cchRESOURCE);
498 if (!lstrcmpi (szGateway, g.Configuration.szGateway))
500 EnableWindow (GetDlgItem (hDlg, IDC_GATEWAY_CONN), fEnable);
505 void GeneralTab_OnCell (HWND hDlg)
509 GeneralTab_ShowCurrentState (hDlg);
514 void GeneralTab_FixRefreshTimer (HWND hDlg, UINT cmsec)
516 static BOOL fTimerActive = FALSE;
519 KillTimer (hDlg, ID_TIMER);
520 fTimerActive = FALSE;
523 if (g.fIsWinNT && (cmsec != 0))
525 SetTimer (hDlg, ID_TIMER, cmsec, NULL);
530 DWORD GeneralTab_GetDisplayState (HWND hDlg)
532 TCHAR szText[ cchRESOURCE ];
533 TCHAR szTextNow[ cchRESOURCE ];
534 GetDlgItemText (hDlg, IDC_STATUS, szTextNow, cchRESOURCE);
536 GetString (szText, IDS_STATE_STOPPED);
537 if (!lstrcmpi (szTextNow, szText))
538 return SERVICE_STOPPED;
540 GetString (szText, IDS_STATE_RUNNING);
541 if (!lstrcmpi (szTextNow, szText))
542 return SERVICE_RUNNING;
544 GetString (szText, IDS_STATE_STARTING);
545 if (!lstrcmpi (szTextNow, szText))
546 return SERVICE_START_PENDING;
548 GetString (szText, IDS_STATE_STOPPING);
549 if (!lstrcmpi (szTextNow, szText))
550 return SERVICE_STOP_PENDING;
556 void GeneralTab_ShowCurrentState (HWND hDlg)
558 TCHAR szNoCell[ cchRESOURCE ];
559 GetString (szNoCell, IDS_CELL_UNKNOWN);
561 TCHAR szCell[ cchRESOURCE ];
562 GetDlgItemText (hDlg, IDC_CELL, szCell, cchRESOURCE);
564 BOOL fValidCell = TRUE;
567 if (!lstrcmpi (szCell, szNoCell))
570 DWORD CurrentState = Config_GetServiceState();
572 TCHAR szText[ cchRESOURCE ];
573 switch (CurrentState)
575 case SERVICE_STOPPED:
576 GetString (szText, (fValidCell) ? IDS_STATE_STOPPED : IDS_STOPPED_NOCELL);
578 case SERVICE_RUNNING:
579 GetString (szText, IDS_STATE_RUNNING);
581 case SERVICE_START_PENDING:
582 GetString (szText, IDS_STATE_STARTING);
584 case SERVICE_STOP_PENDING:
585 GetString (szText, IDS_STATE_STOPPING);
588 GetString (szText, IDS_STATE_UNKNOWN);
591 SetDlgItemText (hDlg, IDC_STATUS, szText);
593 // Enable or disable controls as necessary
595 BOOL fIfServiceStopped = !(g.fIsWinNT && !g.fIsAdmin);
596 BOOL fIfServiceRunning = fIfServiceStopped && (CurrentState == SERVICE_RUNNING);
598 EnableWindow (GetDlgItem (hDlg, IDC_SERVICE_START), ((CurrentState == SERVICE_STOPPED) && (g.fIsAdmin) && (fValidCell)));
599 EnableWindow (GetDlgItem (hDlg, IDC_SERVICE_STOP), ((CurrentState == SERVICE_RUNNING) && (g.fIsAdmin)));
603 BOOL GeneralTab_AskIfStopped (HWND hDlg)
605 BOOL fStopService = FALSE;
606 BOOL fStartService = FALSE;
608 // If we changed things, ask if we should restart the service.
609 // Otherwise, if it's stopped, ask the user if we should start the service.
611 DWORD CurrentState = Config_GetServiceState();
614 if ((CurrentState == SERVICE_RUNNING) && (g.fNeedRestart))
616 if (Message (MB_YESNO | MB_ICONQUESTION, IDS_RESTART_TITLE, IDS_RESTART_DESC) == IDYES)
619 fStartService = TRUE;
622 if (CurrentState == SERVICE_STOPPED)
624 if (Message (MB_YESNO | MB_ICONQUESTION, GetCautionTitle(), IDS_OKSTOP_DESC) == IDYES)
626 fStartService = TRUE;
631 // If we need to, start or stop-n-restart the service
633 if (fStartService && fStopService)
635 GeneralTab_DoStartStop (hDlg, FALSE, TRUE); // Stop and restart the thing
637 else if (fStartService && !fStopService)
639 GeneralTab_DoStartStop (hDlg, TRUE, FALSE); // Just start it
644 while ( (l.fRestartIfStopped) ||
645 (l.fWarnIfNotStopped) ||
649 if (!GetMessage (&msg, NULL, 0, 0))
651 if (IsMemoryManagerMessage (&msg))
653 TranslateMessage (&msg);
654 DispatchMessage (&msg);
658 if (fStartService && !l.fServiceIsRunning)
665 BOOL fIsCellInCellServDB (LPCTSTR pszCell)
668 CELLSERVDB CellServDB;
669 char cellname[256], i;
671 /* we pray for all ascii cellnames */
672 for ( i=0 ; pszCell[i] && i < (sizeof(cellname)-1) ; i++ )
673 cellname[i] = pszCell[i];
676 ULONG code = cm_SearchCellRegistry(1, cellname, NULL, NULL, NULL, NULL);
680 (code != CM_ERROR_FORCE_DNS_LOOKUP) &&
681 CSDB_ReadFile (&CellServDB, NULL))
683 if (CSDB_FindCell (&CellServDB, pszCell))
685 CSDB_FreeFile (&CellServDB);
687 if ( fFound == FALSE ) {
689 fFound = !cm_SearchCellByDNS(cellname, NULL, &ttl, NULL, NULL);
697 void GeneralTab_DoStartStop (HWND hDlg, BOOL fStart, BOOL fRestart)
699 BOOL fSuccess = FALSE;
703 if ((hManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS)) != NULL)
706 if ((hService = OpenService (hManager, TEXT("TransarcAFSDaemon"), SERVICE_ALL_ACCESS)) != NULL)
710 g.fNeedRestart = FALSE;
711 if (StartService (hService, 0, 0))
712 TestAndDoMapShare(SERVICE_START_PENDING);
717 SERVICE_STATUS Status;
718 if (ControlService (hService, SERVICE_CONTROL_STOP, &Status))
720 if (g.Configuration.fLogonAuthent)
724 CloseServiceHandle (hService);
727 CloseServiceHandle (hManager);
732 l.fWarnIfStopped = fStart;
733 l.fWarnIfNotStopped = !fStart;
734 l.fRestartIfStopped = fRestart && !fStart;
735 l.fStarting = fStart;
736 GeneralTab_OnTimer (hDlg);
740 l.fWarnIfStopped = FALSE;
741 l.fWarnIfNotStopped = FALSE;
742 l.fRestartIfStopped = FALSE;
743 GeneralTab_OnTimer (hDlg);
746 error = GetLastError();
747 Message (MB_OK | MB_ICONHAND, GetErrorTitle(), ((fStart) ? IDS_SERVICE_FAIL_START : IDS_SERVICE_FAIL_STOP), TEXT("%08lX"), error);
752 BOOL CALLBACK Status_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
758 ShowWindow (l.hStatus, SW_SHOW);
759 Status_OnRefresh (hDlg);
770 DestroyWindow (hDlg);
774 Status_OnRefresh (hDlg);
784 void Status_OnRefresh (HWND hDlg)
786 DWORD CurrentState = Config_GetServiceState();
787 if (CurrentState == SERVICE_START_PENDING)
789 else if (CurrentState == SERVICE_STOP_PENDING)
792 ShowWindow (GetDlgItem (l.hStatus, IDC_STARTING), l.fStarting);
793 ShowWindow (GetDlgItem (l.hStatus, IDC_STOPPING), !l.fStarting);