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
11 #include <afs/param.h>
16 #include "afs_config.h"
17 #include "tab_general.h"
18 #include "tab_hosts.h"
19 #include "tab_advanced.h"
25 * VARIABLES __________________________________________________________________
32 BOOL fWarnIfNotStopped;
33 BOOL fRestartIfStopped;
34 BOOL fServiceIsRunning;
41 * DEFINITIONS ________________________________________________________________
47 #define cmsecIDLE_REFRESH 10000
48 #define cmsecFAST_REFRESH 1000
52 * PROTOTYPES _________________________________________________________________
56 void GeneralTab_OnInitDialog (HWND hDlg);
57 void GeneralTab_OnTimer (HWND hDlg);
58 BOOL GeneralTab_OnApply (HWND hDlg, BOOL fForce, BOOL fComplainIfInvalid);
59 void GeneralTab_OnRefresh (HWND hDlg, BOOL fRequery);
60 void GeneralTab_OnStartStop (HWND hDlg, BOOL fStart);
61 void GeneralTab_OnConnect (HWND hDlg);
62 void GeneralTab_OnGateway (HWND hDlg);
63 void GeneralTab_OnCell (HWND hDlg);
65 void GeneralTab_DoStartStop (HWND hDlg, BOOL fStart, BOOL fRestart);
66 void GeneralTab_FixRefreshTimer (HWND hDlg, UINT cmsec = 0);
67 DWORD GeneralTab_GetDisplayState (HWND hDlg);
68 void GeneralTab_ShowCurrentState (HWND hDlg);
69 BOOL GeneralTab_AskIfStopped (HWND hDlg);
71 BOOL fIsCellInCellServDB (LPCTSTR pszCell);
73 BOOL CALLBACK Status_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
74 void Status_OnRefresh (HWND hDlg);
77 * ROUTINES ___________________________________________________________________
81 BOOL CALLBACK GeneralTab_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
86 Main_OnInitDialog (GetParent(hDlg));
87 GeneralTab_OnInitDialog (hDlg);
91 GeneralTab_FixRefreshTimer (hDlg, 0);
95 GeneralTab_OnTimer (hDlg);
102 if (!GeneralTab_OnApply (hDlg, FALSE, TRUE))
103 SetWindowLong (hDlg, DWL_MSGRESULT, TRUE);
104 else if (g.fIsWinNT && !GeneralTab_AskIfStopped (hDlg))
105 SetWindowLong (hDlg, DWL_MSGRESULT, TRUE);
109 GeneralTab_OnRefresh (hDlg, FALSE);
112 case IDC_SERVICE_START:
113 GeneralTab_OnStartStop (hDlg, TRUE);
116 case IDC_SERVICE_STOP:
117 GeneralTab_OnStartStop (hDlg, FALSE);
120 case IDC_GATEWAY_CONN:
121 GeneralTab_OnConnect (hDlg);
125 GeneralTab_OnGateway (hDlg);
129 GeneralTab_OnCell (hDlg);
133 GeneralTab_DlgProc (hDlg, WM_HELP, 0, 0);
140 WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCONFIG_GENERAL_NT);
142 WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCONFIG_GENERAL_95);
150 void GeneralTab_OnInitDialog (HWND hDlg)
152 DWORD CurrentState = Config_GetServiceState();
153 BOOL fNeedFastRefresh = ((CurrentState == SERVICE_STOPPED) || (CurrentState == SERVICE_RUNNING)) ? FALSE : TRUE;
154 GeneralTab_FixRefreshTimer (hDlg, ((fNeedFastRefresh) ? cmsecFAST_REFRESH : cmsecIDLE_REFRESH));
155 GeneralTab_OnTimer (hDlg);
156 GeneralTab_OnRefresh (hDlg, TRUE);
160 BOOL GeneralTab_VerifyCell (HWND hDlg, BOOL fComplainIfInvalid, LPCTSTR pszCell)
162 TCHAR szNoCell[ cchRESOURCE ];
163 GetString (szNoCell, IDS_CELL_UNKNOWN);
165 TCHAR szCell[ cchRESOURCE ];
167 lstrcpy (szCell, pszCell);
169 GetDlgItemText (hDlg, IDC_CELL, szCell, cchRESOURCE);
170 if ((!szCell[0]) || (!lstrcmpi (szNoCell, szCell)))
172 if (fComplainIfInvalid)
175 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_NOCELL_DESC);
177 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADGATEWAY_DESC);
182 if (!fIsCellInCellServDB (szCell))
184 if (fComplainIfInvalid)
187 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADCELL_DESC);
189 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADGWCELL_DESC, TEXT("%s"), szCell);
198 BOOL GeneralTab_VerifyOK (HWND hDlg, BOOL fComplainIfInvalid)
200 // If it's Windows 95, make sure there's a valid Gateway entry
204 TCHAR szGateway[ cchRESOURCE ];
205 GetDlgItemText (hDlg, IDC_GATEWAY, szGateway, cchRESOURCE);
208 if (fComplainIfInvalid)
209 Message (MB_ICONASTERISK | MB_OK, IDS_NOGATEWAY_TITLE, IDS_NOGATEWAY_DESC);
214 // Make sure the cell is in our CellServDB.
218 if (!GeneralTab_VerifyCell (hDlg, fComplainIfInvalid, NULL))
225 BOOL GeneralTab_OnApply (HWND hDlg, BOOL fForce, BOOL fComplainIfInvalid)
229 // Don't try to do anything if we've already failed the apply
230 if (GetWindowLong (hDlg, DWL_MSGRESULT))
234 // If the user has changed CellServDB, configuration parameters for
235 // the driver or anything else, we want to commit those changes first.
236 // We *won't* commit server prefs changes yet, because we haven't yet
237 // checked to see if the service is running.
239 if (!HostsTab_CommitChanges (fForce))
242 if (!AdvancedTab_CommitChanges (fForce))
245 if (!GeneralTab_VerifyOK (hDlg, fComplainIfInvalid))
248 TCHAR szText[ MAX_PATH ];
252 GetDlgItemText (hDlg, IDC_CELL, szText, MAX_PATH);
253 if (lstrcmpi (szText, g.Configuration.szCell))
255 if (!Config_SetCellName (szText))
257 lstrcpy (g.Configuration.szCell, szText);
261 BOOL fLogonAuthent = IsDlgButtonChecked (hDlg, IDC_LOGON);
262 if (fLogonAuthent != g.Configuration.fLogonAuthent)
264 SetBitLogonOption(fLogonAuthent,LOGON_OPTION_INTEGRATED);
265 g.Configuration.fLogonAuthent = fLogonAuthent;
268 Config_SetTrayIconFlag (IsDlgButtonChecked (hDlg, IDC_TRAYICON));
272 BOOL fBeGateway = IsDlgButtonChecked (hDlg, IDC_GATEWAY);
273 if (fBeGateway != g.Configuration.fBeGateway)
275 if (!Config_SetGatewayFlag (fBeGateway))
277 g.fNeedRestart = TRUE;
278 g.Configuration.fBeGateway = fBeGateway;
281 else // (!g.fIsWinNT)
283 GetDlgItemText (hDlg, IDC_GATEWAY, szText, MAX_PATH);
284 if (lstrcmpi (szText, g.Configuration.szGateway))
286 TCHAR szNewCell[ MAX_PATH ];
287 if (!Config_ContactGateway (szText, szNewCell))
289 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADGATEWAY_DESC);
293 if (!GeneralTab_VerifyCell (hDlg, fComplainIfInvalid, szNewCell))
296 if (!Config_SetGatewayName (szText))
299 if (!Config_SetCellName (szNewCell))
302 Config_FixGatewayDrives();
304 SetDlgItemText (hDlg, IDC_CELL, szNewCell);
305 lstrcpy (g.Configuration.szGateway, szText);
306 lstrcpy (g.Configuration.szCell, szNewCell);
308 GeneralTab_OnGateway (hDlg);
316 void GeneralTab_OnRefresh (HWND hDlg, BOOL fRequery)
318 // If necessary, update any fields in g.Configuration that we care about
323 Config_GetGatewayFlag (&g.Configuration.fBeGateway);
325 Config_GetGatewayName (g.Configuration.szGateway);
327 Config_GetCellName (g.Configuration.szCell);
328 g.Configuration.fLogonAuthent=RWLogonOption(TRUE,LOGON_OPTION_INTEGRATED);
329 Config_GetTrayIconFlag (&g.Configuration.fShowTrayIcon);
332 SetDlgItemText (hDlg, IDC_GATEWAY, g.Configuration.szGateway);
334 CheckDlgButton (hDlg, IDC_GATEWAY, g.Configuration.fBeGateway);
336 SetDlgItemText (hDlg, IDC_CELL, g.Configuration.szCell);
337 CheckDlgButton (hDlg, IDC_LOGON, g.Configuration.fLogonAuthent);
338 CheckDlgButton (hDlg, IDC_TRAYICON, g.Configuration.fShowTrayIcon);
341 // Update our display of the service's status
343 DWORD CurrentState = Config_GetServiceState();
344 BOOL fIfServiceStopped = !(g.fIsWinNT && !g.fIsAdmin);
345 BOOL fIfServiceRunning = fIfServiceStopped && (CurrentState == SERVICE_RUNNING);
347 GeneralTab_ShowCurrentState (hDlg);
349 EnableWindow (GetDlgItem (hDlg, IDC_CELL), fIfServiceStopped && g.fIsWinNT);
351 EnableWindow (GetDlgItem (hDlg, IDC_LOGON), fIfServiceStopped);
352 EnableWindow (GetDlgItem (hDlg, IDC_GATEWAY), fIfServiceStopped);
354 // Update our warning. Note that under WinNT, this tab doesn't have any
355 // controls (other than Start Service) which disable just because the
356 // service isn't running...so don't show that warning in that case.
358 TCHAR szText[ cchRESOURCE ];
359 if ((!g.fIsWinNT) && (CurrentState != SERVICE_RUNNING))
361 GetString (szText, IDS_WARN_STOPPED);
362 SetDlgItemText (hDlg, IDC_WARN, szText);
363 ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_SHOW);
365 else if (g.fIsWinNT && !g.fIsAdmin)
367 GetString (szText, IDS_WARN_ADMIN);
368 SetDlgItemText (hDlg, IDC_WARN, szText);
369 ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_SHOW);
371 else // ((CurrentState == SERVICE_RUNNING) && (g.fIsAdmin))
373 ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_HIDE);
376 GeneralTab_OnGateway (hDlg);
378 // If the service isn't running/stopped, we may need to complain
380 if ((CurrentState == SERVICE_RUNNING) && (l.fWarnIfNotStopped))
382 Message (MB_ICONHAND, GetErrorTitle(), IDS_SERVICE_FAIL_STOP, TEXT("%08lX"), ERROR_SERVICE_SPECIFIC_ERROR);
384 else if ((CurrentState == SERVICE_STOPPED) && (l.fWarnIfStopped))
386 Message (MB_ICONHAND, GetErrorTitle(), IDS_SERVICE_FAIL_START, TEXT("%08lX"), ERROR_SERVICE_SPECIFIC_ERROR);
389 if ((CurrentState == SERVICE_RUNNING) || (CurrentState == SERVICE_STOPPED))
391 BOOL fRestart = ((CurrentState == SERVICE_STOPPED) && (l.fRestartIfStopped));
392 l.fWarnIfStopped = FALSE;
393 l.fWarnIfNotStopped = FALSE;
394 l.fRestartIfStopped = FALSE;
395 l.fServiceIsRunning = (CurrentState == SERVICE_RUNNING);
399 GeneralTab_DoStartStop (hDlg, TRUE, FALSE);
405 void GeneralTab_OnTimer (HWND hDlg)
407 DWORD CurrentState = Config_GetServiceState();
408 DWORD DisplayState = GeneralTab_GetDisplayState(hDlg);
409 TestAndDoMapShare(CurrentState); //Re map mounted drives if necessary
411 BOOL fInEndState = ((CurrentState == SERVICE_RUNNING) || (CurrentState == SERVICE_STOPPED));
412 if (fInEndState && l.hStatus)
414 if (IsWindow (l.hStatus))
415 DestroyWindow (l.hStatus);
418 else if (!fInEndState && !l.hStatus)
420 l.hStatus = ModelessDialog (IDD_STARTSTOP, GetParent (hDlg), (DLGPROC)Status_DlgProc);
423 if (CurrentState != DisplayState)
425 GeneralTab_OnRefresh (hDlg, FALSE);
426 Main_RefreshAllTabs();
428 if (l.hStatus && IsWindow (l.hStatus))
429 PostMessage (l.hStatus, WM_COMMAND, IDINIT, 0);
432 BOOL fNeedFastRefresh = ((CurrentState == SERVICE_STOPPED) || (CurrentState == SERVICE_RUNNING)) ? FALSE : TRUE;
433 BOOL fHaveFastRefresh = ((DisplayState == SERVICE_STOPPED) || (DisplayState == SERVICE_RUNNING)) ? FALSE : TRUE;
435 if (fNeedFastRefresh != fHaveFastRefresh)
437 GeneralTab_FixRefreshTimer (hDlg, ((fNeedFastRefresh) ? cmsecFAST_REFRESH : cmsecIDLE_REFRESH));
442 void GeneralTab_OnStartStop (HWND hDlg, BOOL fStart)
444 BOOL fSuccess = FALSE;
447 // Don't let the user stop the service on a whim; warn him first
451 if (Message (MB_ICONEXCLAMATION | MB_OKCANCEL, GetCautionTitle(), IDS_STOP_DESC) != IDOK)
455 // To start the service, we'll need to successfully commit our new
456 // configuration. To stop the service, we'll *try*, but it's not
457 // fatal if something goes wrong.
459 if (!GeneralTab_OnApply (hDlg, TRUE, ((fStart) ? TRUE : FALSE)))
465 // Okay, start the service
467 GeneralTab_DoStartStop (hDlg, fStart, FALSE);
471 void GeneralTab_OnConnect (HWND hDlg)
473 if (!GeneralTab_OnApply (hDlg, TRUE, TRUE))
475 GeneralTab_OnGateway (hDlg);
476 GeneralTab_OnApply (hDlg, TRUE, TRUE);
480 void GeneralTab_OnGateway (HWND hDlg)
484 TCHAR szGateway[ cchRESOURCE ];
485 GetDlgItemText (hDlg, IDC_GATEWAY, szGateway, cchRESOURCE);
490 if (!lstrcmpi (szGateway, g.Configuration.szGateway))
492 EnableWindow (GetDlgItem (hDlg, IDC_GATEWAY_CONN), fEnable);
497 void GeneralTab_OnCell (HWND hDlg)
501 GeneralTab_ShowCurrentState (hDlg);
506 void GeneralTab_FixRefreshTimer (HWND hDlg, UINT cmsec)
508 static BOOL fTimerActive = FALSE;
511 KillTimer (hDlg, ID_TIMER);
512 fTimerActive = FALSE;
515 if (g.fIsWinNT && (cmsec != 0))
517 SetTimer (hDlg, ID_TIMER, cmsec, NULL);
522 DWORD GeneralTab_GetDisplayState (HWND hDlg)
524 TCHAR szText[ cchRESOURCE ];
525 TCHAR szTextNow[ cchRESOURCE ];
526 GetDlgItemText (hDlg, IDC_STATUS, szTextNow, cchRESOURCE);
528 GetString (szText, IDS_STATE_STOPPED);
529 if (!lstrcmpi (szTextNow, szText))
530 return SERVICE_STOPPED;
532 GetString (szText, IDS_STATE_RUNNING);
533 if (!lstrcmpi (szTextNow, szText))
534 return SERVICE_RUNNING;
536 GetString (szText, IDS_STATE_STARTING);
537 if (!lstrcmpi (szTextNow, szText))
538 return SERVICE_START_PENDING;
540 GetString (szText, IDS_STATE_STOPPING);
541 if (!lstrcmpi (szTextNow, szText))
542 return SERVICE_STOP_PENDING;
548 void GeneralTab_ShowCurrentState (HWND hDlg)
550 TCHAR szNoCell[ cchRESOURCE ];
551 GetString (szNoCell, IDS_CELL_UNKNOWN);
553 TCHAR szCell[ cchRESOURCE ];
554 GetDlgItemText (hDlg, IDC_CELL, szCell, cchRESOURCE);
556 BOOL fValidCell = TRUE;
559 if (!lstrcmpi (szCell, szNoCell))
562 DWORD CurrentState = Config_GetServiceState();
564 TCHAR szText[ cchRESOURCE ];
565 switch (CurrentState)
567 case SERVICE_STOPPED:
568 GetString (szText, (fValidCell) ? IDS_STATE_STOPPED : IDS_STOPPED_NOCELL);
570 case SERVICE_RUNNING:
571 GetString (szText, IDS_STATE_RUNNING);
573 case SERVICE_START_PENDING:
574 GetString (szText, IDS_STATE_STARTING);
576 case SERVICE_STOP_PENDING:
577 GetString (szText, IDS_STATE_STOPPING);
580 GetString (szText, IDS_STATE_UNKNOWN);
583 SetDlgItemText (hDlg, IDC_STATUS, szText);
585 // Enable or disable controls as necessary
587 BOOL fIfServiceStopped = !(g.fIsWinNT && !g.fIsAdmin);
588 BOOL fIfServiceRunning = fIfServiceStopped && (CurrentState == SERVICE_RUNNING);
590 EnableWindow (GetDlgItem (hDlg, IDC_SERVICE_START), ((CurrentState == SERVICE_STOPPED) && (g.fIsAdmin) && (fValidCell)));
591 EnableWindow (GetDlgItem (hDlg, IDC_SERVICE_STOP), ((CurrentState == SERVICE_RUNNING) && (g.fIsAdmin)));
595 BOOL GeneralTab_AskIfStopped (HWND hDlg)
597 BOOL fStopService = FALSE;
598 BOOL fStartService = FALSE;
600 // If we changed things, ask if we should restart the service.
601 // Otherwise, if it's stopped, ask the user if we should start the service.
603 DWORD CurrentState = Config_GetServiceState();
606 if ((CurrentState == SERVICE_RUNNING) && (g.fNeedRestart))
608 if (Message (MB_YESNO | MB_ICONQUESTION, IDS_RESTART_TITLE, IDS_RESTART_DESC) == IDYES)
611 fStartService = TRUE;
614 if (CurrentState == SERVICE_STOPPED)
616 if (Message (MB_YESNO | MB_ICONQUESTION, GetCautionTitle(), IDS_OKSTOP_DESC) == IDYES)
618 fStartService = TRUE;
623 // If we need to, start or stop-n-restart the service
625 if (fStartService && fStopService)
627 GeneralTab_DoStartStop (hDlg, FALSE, TRUE); // Stop and restart the thing
629 else if (fStartService && !fStopService)
631 GeneralTab_DoStartStop (hDlg, TRUE, FALSE); // Just start it
636 while ( (l.fRestartIfStopped) ||
637 (l.fWarnIfNotStopped) ||
641 if (!GetMessage (&msg, NULL, 0, 0))
643 if (IsMemoryManagerMessage (&msg))
645 TranslateMessage (&msg);
646 DispatchMessage (&msg);
650 if (fStartService && !l.fServiceIsRunning)
657 BOOL fIsCellInCellServDB (LPCTSTR pszCell)
661 CELLSERVDB CellServDB;
662 if (CSDB_ReadFile (&CellServDB, NULL))
664 if (CSDB_FindCell (&CellServDB, pszCell))
666 CSDB_FreeFile (&CellServDB);
673 void GeneralTab_DoStartStop (HWND hDlg, BOOL fStart, BOOL fRestart)
675 BOOL fSuccess = FALSE;
679 if ((hManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS)) != NULL)
682 if ((hService = OpenService (hManager, TEXT("TransarcAFSDaemon"), SERVICE_ALL_ACCESS)) != NULL)
686 g.fNeedRestart = FALSE;
687 if (StartService (hService, 0, 0))
688 TestAndDoMapShare(SERVICE_START_PENDING);
693 SERVICE_STATUS Status;
694 if (ControlService (hService, SERVICE_CONTROL_STOP, &Status))
696 if (g.Configuration.fLogonAuthent)
700 CloseServiceHandle (hService);
703 CloseServiceHandle (hManager);
708 l.fWarnIfStopped = fStart;
709 l.fWarnIfNotStopped = !fStart;
710 l.fRestartIfStopped = fRestart && !fStart;
711 l.fStarting = fStart;
712 GeneralTab_OnTimer (hDlg);
716 l.fWarnIfStopped = FALSE;
717 l.fWarnIfNotStopped = FALSE;
718 l.fRestartIfStopped = FALSE;
719 GeneralTab_OnTimer (hDlg);
722 error = GetLastError();
723 Message (MB_OK | MB_ICONHAND, GetErrorTitle(), ((fStart) ? IDS_SERVICE_FAIL_START : IDS_SERVICE_FAIL_STOP), TEXT("%08lX"), error);
728 BOOL CALLBACK Status_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
734 ShowWindow (l.hStatus, SW_SHOW);
735 Status_OnRefresh (hDlg);
746 DestroyWindow (hDlg);
750 Status_OnRefresh (hDlg);
760 void Status_OnRefresh (HWND hDlg)
762 DWORD CurrentState = Config_GetServiceState();
763 if (CurrentState == SERVICE_START_PENDING)
765 else if (CurrentState == SERVICE_STOP_PENDING)
768 ShowWindow (GetDlgItem (l.hStatus, IDC_STARTING), l.fStarting);
769 ShowWindow (GetDlgItem (l.hStatus, IDC_STOPPING), !l.fStarting);