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 Config_SetTrayIconFlag (IsDlgButtonChecked (hDlg, IDC_TRAYICON));
279 BOOL fBeGateway = IsDlgButtonChecked (hDlg, IDC_GATEWAY);
280 if (fBeGateway != g.Configuration.fBeGateway)
282 if (!Config_SetGatewayFlag (fBeGateway))
284 g.fNeedRestart = TRUE;
285 g.Configuration.fBeGateway = fBeGateway;
288 else // (!g.fIsWinNT)
290 GetDlgItemText (hDlg, IDC_GATEWAY, szText, MAX_PATH);
291 if (lstrcmpi (szText, g.Configuration.szGateway))
293 TCHAR szNewCell[ MAX_PATH ];
294 if (!Config_ContactGateway (szText, szNewCell))
296 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADGATEWAY_DESC);
300 if (!GeneralTab_VerifyCell (hDlg, fComplainIfInvalid, szNewCell))
303 if (!Config_SetGatewayName (szText))
306 if (!Config_SetCellName (szNewCell))
309 Config_FixGatewayDrives();
311 SetDlgItemText (hDlg, IDC_CELL, szNewCell);
312 lstrcpy (g.Configuration.szGateway, szText);
313 lstrcpy (g.Configuration.szCell, szNewCell);
315 GeneralTab_OnGateway (hDlg);
323 void GeneralTab_OnRefresh (HWND hDlg, BOOL fRequery)
325 // If necessary, update any fields in g.Configuration that we care about
330 Config_GetGatewayFlag (&g.Configuration.fBeGateway);
332 Config_GetGatewayName (g.Configuration.szGateway);
334 Config_GetCellName (g.Configuration.szCell);
335 g.Configuration.fLogonAuthent=RWLogonOption(TRUE,LOGON_OPTION_INTEGRATED);
336 Config_GetTrayIconFlag (&g.Configuration.fShowTrayIcon);
339 SetDlgItemText (hDlg, IDC_GATEWAY, g.Configuration.szGateway);
341 CheckDlgButton (hDlg, IDC_GATEWAY, g.Configuration.fBeGateway);
343 SetDlgItemText (hDlg, IDC_CELL, g.Configuration.szCell);
344 CheckDlgButton (hDlg, IDC_LOGON, g.Configuration.fLogonAuthent);
345 CheckDlgButton (hDlg, IDC_TRAYICON, g.Configuration.fShowTrayIcon);
348 // Update our display of the service's status
350 DWORD CurrentState = Config_GetServiceState();
351 BOOL fIfServiceStopped = !(g.fIsWinNT && !g.fIsAdmin);
352 BOOL fIfServiceRunning = fIfServiceStopped && (CurrentState == SERVICE_RUNNING);
354 GeneralTab_ShowCurrentState (hDlg);
356 EnableWindow (GetDlgItem (hDlg, IDC_CELL), fIfServiceStopped && g.fIsWinNT);
358 EnableWindow (GetDlgItem (hDlg, IDC_LOGON), fIfServiceStopped);
359 EnableWindow (GetDlgItem (hDlg, IDC_GATEWAY), fIfServiceStopped);
361 // Update our warning. Note that under WinNT, this tab doesn't have any
362 // controls (other than Start Service) which disable just because the
363 // service isn't running...so don't show that warning in that case.
365 TCHAR szText[ cchRESOURCE ];
366 if ((!g.fIsWinNT) && (CurrentState != SERVICE_RUNNING))
368 GetString (szText, IDS_WARN_STOPPED);
369 SetDlgItemText (hDlg, IDC_WARN, szText);
370 ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_SHOW);
372 else if (g.fIsWinNT && !g.fIsAdmin)
374 GetString (szText, IDS_WARN_ADMIN);
375 SetDlgItemText (hDlg, IDC_WARN, szText);
376 ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_SHOW);
378 else // ((CurrentState == SERVICE_RUNNING) && (g.fIsAdmin))
380 ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_HIDE);
383 GeneralTab_OnGateway (hDlg);
385 // If the service isn't running/stopped, we may need to complain
387 if ((CurrentState == SERVICE_RUNNING) && (l.fWarnIfNotStopped))
389 Message (MB_ICONHAND, GetErrorTitle(), IDS_SERVICE_FAIL_STOP, TEXT("%08lX"), ERROR_SERVICE_SPECIFIC_ERROR);
391 else if ((CurrentState == SERVICE_STOPPED) && (l.fWarnIfStopped))
393 Message (MB_ICONHAND, GetErrorTitle(), IDS_SERVICE_FAIL_START, TEXT("%08lX"), ERROR_SERVICE_SPECIFIC_ERROR);
396 if ((CurrentState == SERVICE_RUNNING) || (CurrentState == SERVICE_STOPPED))
398 BOOL fRestart = ((CurrentState == SERVICE_STOPPED) && (l.fRestartIfStopped));
399 l.fWarnIfStopped = FALSE;
400 l.fWarnIfNotStopped = FALSE;
401 l.fRestartIfStopped = FALSE;
402 l.fServiceIsRunning = (CurrentState == SERVICE_RUNNING);
406 GeneralTab_DoStartStop (hDlg, TRUE, FALSE);
412 void GeneralTab_OnTimer (HWND hDlg)
414 DWORD CurrentState = Config_GetServiceState();
415 DWORD DisplayState = GeneralTab_GetDisplayState(hDlg);
416 TestAndDoMapShare(CurrentState); //Re map mounted drives if necessary
418 BOOL fInEndState = ((CurrentState == SERVICE_RUNNING) || (CurrentState == SERVICE_STOPPED));
419 if (fInEndState && l.hStatus)
421 if (IsWindow (l.hStatus))
422 DestroyWindow (l.hStatus);
425 else if (!fInEndState && !l.hStatus)
427 l.hStatus = ModelessDialog (IDD_STARTSTOP, GetParent (hDlg), (DLGPROC)Status_DlgProc);
430 if (CurrentState != DisplayState)
432 GeneralTab_OnRefresh (hDlg, FALSE);
433 Main_RefreshAllTabs();
435 if (l.hStatus && IsWindow (l.hStatus))
436 PostMessage (l.hStatus, WM_COMMAND, IDINIT, 0);
439 BOOL fNeedFastRefresh = ((CurrentState == SERVICE_STOPPED) || (CurrentState == SERVICE_RUNNING)) ? FALSE : TRUE;
440 BOOL fHaveFastRefresh = ((DisplayState == SERVICE_STOPPED) || (DisplayState == SERVICE_RUNNING)) ? FALSE : TRUE;
442 if (fNeedFastRefresh != fHaveFastRefresh)
444 GeneralTab_FixRefreshTimer (hDlg, ((fNeedFastRefresh) ? cmsecFAST_REFRESH : cmsecIDLE_REFRESH));
449 void GeneralTab_OnStartStop (HWND hDlg, BOOL fStart)
451 BOOL fSuccess = FALSE;
454 // Don't let the user stop the service on a whim; warn him first
458 if (Message (MB_ICONEXCLAMATION | MB_OKCANCEL, GetCautionTitle(), IDS_STOP_DESC) != IDOK)
462 // To start the service, we'll need to successfully commit our new
463 // configuration. To stop the service, we'll *try*, but it's not
464 // fatal if something goes wrong.
466 if (!GeneralTab_OnApply (hDlg, TRUE, ((fStart) ? TRUE : FALSE)))
472 // Okay, start the service
474 GeneralTab_DoStartStop (hDlg, fStart, FALSE);
478 void GeneralTab_OnConnect (HWND hDlg)
480 if (!GeneralTab_OnApply (hDlg, TRUE, TRUE))
482 GeneralTab_OnGateway (hDlg);
483 GeneralTab_OnApply (hDlg, TRUE, TRUE);
487 void GeneralTab_OnGateway (HWND hDlg)
491 TCHAR szGateway[ cchRESOURCE ];
492 GetDlgItemText (hDlg, IDC_GATEWAY, szGateway, cchRESOURCE);
497 if (!lstrcmpi (szGateway, g.Configuration.szGateway))
499 EnableWindow (GetDlgItem (hDlg, IDC_GATEWAY_CONN), fEnable);
504 void GeneralTab_OnCell (HWND hDlg)
508 GeneralTab_ShowCurrentState (hDlg);
513 void GeneralTab_FixRefreshTimer (HWND hDlg, UINT cmsec)
515 static BOOL fTimerActive = FALSE;
518 KillTimer (hDlg, ID_TIMER);
519 fTimerActive = FALSE;
522 if (g.fIsWinNT && (cmsec != 0))
524 SetTimer (hDlg, ID_TIMER, cmsec, NULL);
529 DWORD GeneralTab_GetDisplayState (HWND hDlg)
531 TCHAR szText[ cchRESOURCE ];
532 TCHAR szTextNow[ cchRESOURCE ];
533 GetDlgItemText (hDlg, IDC_STATUS, szTextNow, cchRESOURCE);
535 GetString (szText, IDS_STATE_STOPPED);
536 if (!lstrcmpi (szTextNow, szText))
537 return SERVICE_STOPPED;
539 GetString (szText, IDS_STATE_RUNNING);
540 if (!lstrcmpi (szTextNow, szText))
541 return SERVICE_RUNNING;
543 GetString (szText, IDS_STATE_STARTING);
544 if (!lstrcmpi (szTextNow, szText))
545 return SERVICE_START_PENDING;
547 GetString (szText, IDS_STATE_STOPPING);
548 if (!lstrcmpi (szTextNow, szText))
549 return SERVICE_STOP_PENDING;
555 void GeneralTab_ShowCurrentState (HWND hDlg)
557 TCHAR szNoCell[ cchRESOURCE ];
558 GetString (szNoCell, IDS_CELL_UNKNOWN);
560 TCHAR szCell[ cchRESOURCE ];
561 GetDlgItemText (hDlg, IDC_CELL, szCell, cchRESOURCE);
563 BOOL fValidCell = TRUE;
566 if (!lstrcmpi (szCell, szNoCell))
569 DWORD CurrentState = Config_GetServiceState();
571 TCHAR szText[ cchRESOURCE ];
572 switch (CurrentState)
574 case SERVICE_STOPPED:
575 GetString (szText, (fValidCell) ? IDS_STATE_STOPPED : IDS_STOPPED_NOCELL);
577 case SERVICE_RUNNING:
578 GetString (szText, IDS_STATE_RUNNING);
580 case SERVICE_START_PENDING:
581 GetString (szText, IDS_STATE_STARTING);
583 case SERVICE_STOP_PENDING:
584 GetString (szText, IDS_STATE_STOPPING);
587 GetString (szText, IDS_STATE_UNKNOWN);
590 SetDlgItemText (hDlg, IDC_STATUS, szText);
592 // Enable or disable controls as necessary
594 BOOL fIfServiceStopped = !(g.fIsWinNT && !g.fIsAdmin);
595 BOOL fIfServiceRunning = fIfServiceStopped && (CurrentState == SERVICE_RUNNING);
597 EnableWindow (GetDlgItem (hDlg, IDC_SERVICE_START), ((CurrentState == SERVICE_STOPPED) && (g.fIsAdmin) && (fValidCell)));
598 EnableWindow (GetDlgItem (hDlg, IDC_SERVICE_STOP), ((CurrentState == SERVICE_RUNNING) && (g.fIsAdmin)));
602 BOOL GeneralTab_AskIfStopped (HWND hDlg)
604 BOOL fStopService = FALSE;
605 BOOL fStartService = FALSE;
607 // If we changed things, ask if we should restart the service.
608 // Otherwise, if it's stopped, ask the user if we should start the service.
610 DWORD CurrentState = Config_GetServiceState();
613 if ((CurrentState == SERVICE_RUNNING) && (g.fNeedRestart))
615 if (Message (MB_YESNO | MB_ICONQUESTION, IDS_RESTART_TITLE, IDS_RESTART_DESC) == IDYES)
618 fStartService = TRUE;
621 if (CurrentState == SERVICE_STOPPED)
623 if (Message (MB_YESNO | MB_ICONQUESTION, GetCautionTitle(), IDS_OKSTOP_DESC) == IDYES)
625 fStartService = TRUE;
630 // If we need to, start or stop-n-restart the service
632 if (fStartService && fStopService)
634 GeneralTab_DoStartStop (hDlg, FALSE, TRUE); // Stop and restart the thing
636 else if (fStartService && !fStopService)
638 GeneralTab_DoStartStop (hDlg, TRUE, FALSE); // Just start it
643 while ( (l.fRestartIfStopped) ||
644 (l.fWarnIfNotStopped) ||
648 if (!GetMessage (&msg, NULL, 0, 0))
650 if (IsMemoryManagerMessage (&msg))
652 TranslateMessage (&msg);
653 DispatchMessage (&msg);
657 if (fStartService && !l.fServiceIsRunning)
664 BOOL fIsCellInCellServDB (LPCTSTR pszCell)
667 CELLSERVDB CellServDB;
668 char cellname[256], i;
670 /* we pray for all ascii cellnames */
671 for ( i=0 ; pszCell[i] && i < (sizeof(cellname)-1) ; i++ )
672 cellname[i] = pszCell[i];
675 ULONG code = cm_SearchCellRegistry(1, cellname, NULL, NULL, NULL, NULL);
679 (code != CM_ERROR_FORCE_DNS_LOOKUP) &&
680 CSDB_ReadFile (&CellServDB, NULL))
682 if (CSDB_FindCell (&CellServDB, pszCell))
684 CSDB_FreeFile (&CellServDB);
686 if ( fFound == FALSE ) {
688 fFound = !cm_SearchCellByDNS(cellname, NULL, &ttl, NULL, NULL);
696 void GeneralTab_DoStartStop (HWND hDlg, BOOL fStart, BOOL fRestart)
698 BOOL fSuccess = FALSE;
702 if ((hManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS)) != NULL)
705 if ((hService = OpenService (hManager, TEXT("TransarcAFSDaemon"), SERVICE_ALL_ACCESS)) != NULL)
709 g.fNeedRestart = FALSE;
710 if (StartService (hService, 0, 0))
711 TestAndDoMapShare(SERVICE_START_PENDING);
716 SERVICE_STATUS Status;
717 if (ControlService (hService, SERVICE_CONTROL_STOP, &Status))
719 if (g.Configuration.fLogonAuthent)
723 CloseServiceHandle (hService);
726 CloseServiceHandle (hManager);
731 l.fWarnIfStopped = fStart;
732 l.fWarnIfNotStopped = !fStart;
733 l.fRestartIfStopped = fRestart && !fStart;
734 l.fStarting = fStart;
735 GeneralTab_OnTimer (hDlg);
739 l.fWarnIfStopped = FALSE;
740 l.fWarnIfNotStopped = FALSE;
741 l.fRestartIfStopped = FALSE;
742 GeneralTab_OnTimer (hDlg);
745 error = GetLastError();
746 Message (MB_OK | MB_ICONHAND, GetErrorTitle(), ((fStart) ? IDS_SERVICE_FAIL_START : IDS_SERVICE_FAIL_STOP), TEXT("%08lX"), error);
751 BOOL CALLBACK Status_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
757 ShowWindow (l.hStatus, SW_SHOW);
758 Status_OnRefresh (hDlg);
769 DestroyWindow (hDlg);
773 Status_OnRefresh (hDlg);
783 void Status_OnRefresh (HWND hDlg)
785 DWORD CurrentState = Config_GetServiceState();
786 if (CurrentState == SERVICE_START_PENDING)
788 else if (CurrentState == SERVICE_STOP_PENDING)
791 ShowWindow (GetDlgItem (l.hStatus, IDC_STARTING), l.fStarting);
792 ShowWindow (GetDlgItem (l.hStatus, IDC_STOPPING), !l.fStarting);