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 <afs/param.h>
18 #include <afs/cm_config.h>
21 #include "afs_config.h"
22 #include "tab_general.h"
23 #include "tab_hosts.h"
24 #include "tab_advanced.h"
30 * VARIABLES __________________________________________________________________
37 BOOL fWarnIfNotStopped;
38 BOOL fRestartIfStopped;
39 BOOL fServiceIsRunning;
46 * DEFINITIONS ________________________________________________________________
52 #define cmsecIDLE_REFRESH 10000
53 #define cmsecFAST_REFRESH 1000
57 * PROTOTYPES _________________________________________________________________
61 void GeneralTab_OnInitDialog (HWND hDlg);
62 void GeneralTab_OnTimer (HWND hDlg);
63 BOOL GeneralTab_OnApply (HWND hDlg, BOOL fForce, BOOL fComplainIfInvalid);
64 void GeneralTab_OnRefresh (HWND hDlg, BOOL fRequery);
65 void GeneralTab_OnStartStop (HWND hDlg, BOOL fStart);
66 void GeneralTab_OnConnect (HWND hDlg);
67 void GeneralTab_OnGateway (HWND hDlg);
68 void GeneralTab_OnCell (HWND hDlg);
70 void GeneralTab_DoStartStop (HWND hDlg, BOOL fStart, BOOL fRestart);
71 void GeneralTab_FixRefreshTimer (HWND hDlg, UINT cmsec = 0);
72 DWORD GeneralTab_GetDisplayState (HWND hDlg);
73 void GeneralTab_ShowCurrentState (HWND hDlg);
74 BOOL GeneralTab_AskIfStopped (HWND hDlg);
76 BOOL fIsCellInCellServDB (LPCTSTR pszCell);
78 BOOL CALLBACK Status_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
79 void Status_OnRefresh (HWND hDlg);
82 * ROUTINES ___________________________________________________________________
86 BOOL CALLBACK GeneralTab_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
91 Main_OnInitDialog (GetParent(hDlg));
92 GeneralTab_OnInitDialog (hDlg);
96 GeneralTab_FixRefreshTimer (hDlg, 0);
100 GeneralTab_OnTimer (hDlg);
107 if (!GeneralTab_OnApply (hDlg, FALSE, TRUE))
108 SetWindowLongPtr (hDlg, DWLP_MSGRESULT, TRUE);
109 else if (g.fIsWinNT && !GeneralTab_AskIfStopped (hDlg))
110 SetWindowLongPtr (hDlg, DWLP_MSGRESULT, TRUE);
114 GeneralTab_OnRefresh (hDlg, FALSE);
117 case IDC_SERVICE_START:
118 GeneralTab_OnStartStop (hDlg, TRUE);
121 case IDC_SERVICE_STOP:
122 GeneralTab_OnStartStop (hDlg, FALSE);
125 case IDC_GATEWAY_CONN:
126 GeneralTab_OnConnect (hDlg);
130 GeneralTab_OnGateway (hDlg);
134 GeneralTab_OnCell (hDlg);
138 GeneralTab_DlgProc (hDlg, WM_HELP, 0, 0);
145 WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCONFIG_GENERAL_NT);
147 WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCONFIG_GENERAL_95);
155 void GeneralTab_OnInitDialog (HWND hDlg)
157 DWORD CurrentState = Config_GetServiceState();
158 BOOL fNeedFastRefresh = ((CurrentState == SERVICE_STOPPED) || (CurrentState == SERVICE_RUNNING)) ? FALSE : TRUE;
159 GeneralTab_FixRefreshTimer (hDlg, ((fNeedFastRefresh) ? cmsecFAST_REFRESH : cmsecIDLE_REFRESH));
160 GeneralTab_OnTimer (hDlg);
161 GeneralTab_OnRefresh (hDlg, TRUE);
165 BOOL GeneralTab_VerifyCell (HWND hDlg, BOOL fComplainIfInvalid, LPCTSTR pszCell)
167 TCHAR szNoCell[ cchRESOURCE ];
168 GetString (szNoCell, IDS_CELL_UNKNOWN);
170 TCHAR szCell[ cchRESOURCE ];
172 lstrcpy (szCell, pszCell);
174 GetDlgItemText (hDlg, IDC_CELL, szCell, cchRESOURCE);
175 if ((!szCell[0]) || (!lstrcmpi (szNoCell, szCell)))
177 if (fComplainIfInvalid)
180 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_NOCELL_DESC);
182 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADGATEWAY_DESC);
187 if (!fIsCellInCellServDB (szCell))
189 if (fComplainIfInvalid)
192 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADCELL_DESC);
194 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADGWCELL_DESC, TEXT("%s"), szCell);
203 BOOL GeneralTab_VerifyOK (HWND hDlg, BOOL fComplainIfInvalid)
205 // If it's Windows 95, make sure there's a valid Gateway entry
209 TCHAR szGateway[ cchRESOURCE ];
210 GetDlgItemText (hDlg, IDC_GATEWAY, szGateway, cchRESOURCE);
213 if (fComplainIfInvalid)
214 Message (MB_ICONASTERISK | MB_OK, IDS_NOGATEWAY_TITLE, IDS_NOGATEWAY_DESC);
219 // Make sure the cell is in our CellServDB.
223 if (!GeneralTab_VerifyCell (hDlg, fComplainIfInvalid, NULL))
230 BOOL GeneralTab_OnApply (HWND hDlg, BOOL fForce, BOOL fComplainIfInvalid)
234 // Don't try to do anything if we've already failed the apply
235 if (GetWindowLongPtr (hDlg, DWLP_MSGRESULT))
239 // If the user has changed CellServDB, configuration parameters for
240 // the driver or anything else, we want to commit those changes first.
241 // We *won't* commit server prefs changes yet, because we haven't yet
242 // checked to see if the service is running.
244 if (!HostsTab_CommitChanges (fForce))
247 if (!AdvancedTab_CommitChanges (fForce))
250 if (!GeneralTab_VerifyOK (hDlg, fComplainIfInvalid))
253 TCHAR szText[ MAX_PATH ];
257 GetDlgItemText (hDlg, IDC_CELL, szText, MAX_PATH);
258 if (lstrcmpi (szText, g.Configuration.szCell))
260 if (!Config_SetCellName (szText))
262 lstrcpy (g.Configuration.szCell, szText);
266 BOOL fLogonAuthent = IsDlgButtonChecked (hDlg, IDC_LOGON);
267 if (fLogonAuthent != g.Configuration.fLogonAuthent)
269 SetBitLogonOption(fLogonAuthent,LOGON_OPTION_INTEGRATED);
270 g.Configuration.fLogonAuthent = fLogonAuthent;
273 Config_SetTrayIconFlag (IsDlgButtonChecked (hDlg, IDC_TRAYICON));
277 BOOL fBeGateway = IsDlgButtonChecked (hDlg, IDC_GATEWAY);
278 if (fBeGateway != g.Configuration.fBeGateway)
280 if (!Config_SetGatewayFlag (fBeGateway))
282 g.fNeedRestart = TRUE;
283 g.Configuration.fBeGateway = fBeGateway;
286 else // (!g.fIsWinNT)
288 GetDlgItemText (hDlg, IDC_GATEWAY, szText, MAX_PATH);
289 if (lstrcmpi (szText, g.Configuration.szGateway))
291 TCHAR szNewCell[ MAX_PATH ];
292 if (!Config_ContactGateway (szText, szNewCell))
294 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADGATEWAY_DESC);
298 if (!GeneralTab_VerifyCell (hDlg, fComplainIfInvalid, szNewCell))
301 if (!Config_SetGatewayName (szText))
304 if (!Config_SetCellName (szNewCell))
307 Config_FixGatewayDrives();
309 SetDlgItemText (hDlg, IDC_CELL, szNewCell);
310 lstrcpy (g.Configuration.szGateway, szText);
311 lstrcpy (g.Configuration.szCell, szNewCell);
313 GeneralTab_OnGateway (hDlg);
321 void GeneralTab_OnRefresh (HWND hDlg, BOOL fRequery)
323 // If necessary, update any fields in g.Configuration that we care about
328 Config_GetGatewayFlag (&g.Configuration.fBeGateway);
330 Config_GetGatewayName (g.Configuration.szGateway);
332 Config_GetCellName (g.Configuration.szCell);
333 g.Configuration.fLogonAuthent=RWLogonOption(TRUE,LOGON_OPTION_INTEGRATED);
334 Config_GetTrayIconFlag (&g.Configuration.fShowTrayIcon);
337 SetDlgItemText (hDlg, IDC_GATEWAY, g.Configuration.szGateway);
339 CheckDlgButton (hDlg, IDC_GATEWAY, g.Configuration.fBeGateway);
341 SetDlgItemText (hDlg, IDC_CELL, g.Configuration.szCell);
342 CheckDlgButton (hDlg, IDC_LOGON, g.Configuration.fLogonAuthent);
343 CheckDlgButton (hDlg, IDC_TRAYICON, g.Configuration.fShowTrayIcon);
346 // Update our display of the service's status
348 DWORD CurrentState = Config_GetServiceState();
349 BOOL fIfServiceStopped = !(g.fIsWinNT && !g.fIsAdmin);
350 BOOL fIfServiceRunning = fIfServiceStopped && (CurrentState == SERVICE_RUNNING);
352 GeneralTab_ShowCurrentState (hDlg);
354 EnableWindow (GetDlgItem (hDlg, IDC_CELL), fIfServiceStopped && g.fIsWinNT);
356 EnableWindow (GetDlgItem (hDlg, IDC_LOGON), fIfServiceStopped);
357 EnableWindow (GetDlgItem (hDlg, IDC_GATEWAY), fIfServiceStopped);
359 // Update our warning. Note that under WinNT, this tab doesn't have any
360 // controls (other than Start Service) which disable just because the
361 // service isn't running...so don't show that warning in that case.
363 TCHAR szText[ cchRESOURCE ];
364 if ((!g.fIsWinNT) && (CurrentState != SERVICE_RUNNING))
366 GetString (szText, IDS_WARN_STOPPED);
367 SetDlgItemText (hDlg, IDC_WARN, szText);
368 ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_SHOW);
370 else if (g.fIsWinNT && !g.fIsAdmin)
372 GetString (szText, IDS_WARN_ADMIN);
373 SetDlgItemText (hDlg, IDC_WARN, szText);
374 ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_SHOW);
376 else // ((CurrentState == SERVICE_RUNNING) && (g.fIsAdmin))
378 ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_HIDE);
381 GeneralTab_OnGateway (hDlg);
383 // If the service isn't running/stopped, we may need to complain
385 if ((CurrentState == SERVICE_RUNNING) && (l.fWarnIfNotStopped))
387 Message (MB_ICONHAND, GetErrorTitle(), IDS_SERVICE_FAIL_STOP, TEXT("%08lX"), ERROR_SERVICE_SPECIFIC_ERROR);
389 else if ((CurrentState == SERVICE_STOPPED) && (l.fWarnIfStopped))
391 Message (MB_ICONHAND, GetErrorTitle(), IDS_SERVICE_FAIL_START, TEXT("%08lX"), ERROR_SERVICE_SPECIFIC_ERROR);
394 if ((CurrentState == SERVICE_RUNNING) || (CurrentState == SERVICE_STOPPED))
396 BOOL fRestart = ((CurrentState == SERVICE_STOPPED) && (l.fRestartIfStopped));
397 l.fWarnIfStopped = FALSE;
398 l.fWarnIfNotStopped = FALSE;
399 l.fRestartIfStopped = FALSE;
400 l.fServiceIsRunning = (CurrentState == SERVICE_RUNNING);
404 GeneralTab_DoStartStop (hDlg, TRUE, FALSE);
410 void GeneralTab_OnTimer (HWND hDlg)
412 DWORD CurrentState = Config_GetServiceState();
413 DWORD DisplayState = GeneralTab_GetDisplayState(hDlg);
414 TestAndDoMapShare(CurrentState); //Re map mounted drives if necessary
416 BOOL fInEndState = ((CurrentState == SERVICE_RUNNING) || (CurrentState == SERVICE_STOPPED));
417 if (fInEndState && l.hStatus)
419 if (IsWindow (l.hStatus))
420 DestroyWindow (l.hStatus);
423 else if (!fInEndState && !l.hStatus)
425 l.hStatus = ModelessDialog (IDD_STARTSTOP, GetParent (hDlg), (DLGPROC)Status_DlgProc);
428 if (CurrentState != DisplayState)
430 GeneralTab_OnRefresh (hDlg, FALSE);
431 Main_RefreshAllTabs();
433 if (l.hStatus && IsWindow (l.hStatus))
434 PostMessage (l.hStatus, WM_COMMAND, IDINIT, 0);
437 BOOL fNeedFastRefresh = ((CurrentState == SERVICE_STOPPED) || (CurrentState == SERVICE_RUNNING)) ? FALSE : TRUE;
438 BOOL fHaveFastRefresh = ((DisplayState == SERVICE_STOPPED) || (DisplayState == SERVICE_RUNNING)) ? FALSE : TRUE;
440 if (fNeedFastRefresh != fHaveFastRefresh)
442 GeneralTab_FixRefreshTimer (hDlg, ((fNeedFastRefresh) ? cmsecFAST_REFRESH : cmsecIDLE_REFRESH));
447 void GeneralTab_OnStartStop (HWND hDlg, BOOL fStart)
449 BOOL fSuccess = FALSE;
452 // Don't let the user stop the service on a whim; warn him first
456 if (Message (MB_ICONEXCLAMATION | MB_OKCANCEL, GetCautionTitle(), IDS_STOP_DESC) != IDOK)
460 // To start the service, we'll need to successfully commit our new
461 // configuration. To stop the service, we'll *try*, but it's not
462 // fatal if something goes wrong.
464 if (!GeneralTab_OnApply (hDlg, TRUE, ((fStart) ? TRUE : FALSE)))
470 // Okay, start the service
472 GeneralTab_DoStartStop (hDlg, fStart, FALSE);
476 void GeneralTab_OnConnect (HWND hDlg)
478 if (!GeneralTab_OnApply (hDlg, TRUE, TRUE))
480 GeneralTab_OnGateway (hDlg);
481 GeneralTab_OnApply (hDlg, TRUE, TRUE);
485 void GeneralTab_OnGateway (HWND hDlg)
489 TCHAR szGateway[ cchRESOURCE ];
490 GetDlgItemText (hDlg, IDC_GATEWAY, szGateway, cchRESOURCE);
495 if (!lstrcmpi (szGateway, g.Configuration.szGateway))
497 EnableWindow (GetDlgItem (hDlg, IDC_GATEWAY_CONN), fEnable);
502 void GeneralTab_OnCell (HWND hDlg)
506 GeneralTab_ShowCurrentState (hDlg);
511 void GeneralTab_FixRefreshTimer (HWND hDlg, UINT cmsec)
513 static BOOL fTimerActive = FALSE;
516 KillTimer (hDlg, ID_TIMER);
517 fTimerActive = FALSE;
520 if (g.fIsWinNT && (cmsec != 0))
522 SetTimer (hDlg, ID_TIMER, cmsec, NULL);
527 DWORD GeneralTab_GetDisplayState (HWND hDlg)
529 TCHAR szText[ cchRESOURCE ];
530 TCHAR szTextNow[ cchRESOURCE ];
531 GetDlgItemText (hDlg, IDC_STATUS, szTextNow, cchRESOURCE);
533 GetString (szText, IDS_STATE_STOPPED);
534 if (!lstrcmpi (szTextNow, szText))
535 return SERVICE_STOPPED;
537 GetString (szText, IDS_STATE_RUNNING);
538 if (!lstrcmpi (szTextNow, szText))
539 return SERVICE_RUNNING;
541 GetString (szText, IDS_STATE_STARTING);
542 if (!lstrcmpi (szTextNow, szText))
543 return SERVICE_START_PENDING;
545 GetString (szText, IDS_STATE_STOPPING);
546 if (!lstrcmpi (szTextNow, szText))
547 return SERVICE_STOP_PENDING;
553 void GeneralTab_ShowCurrentState (HWND hDlg)
555 TCHAR szNoCell[ cchRESOURCE ];
556 GetString (szNoCell, IDS_CELL_UNKNOWN);
558 TCHAR szCell[ cchRESOURCE ];
559 GetDlgItemText (hDlg, IDC_CELL, szCell, cchRESOURCE);
561 BOOL fValidCell = TRUE;
564 if (!lstrcmpi (szCell, szNoCell))
567 DWORD CurrentState = Config_GetServiceState();
569 TCHAR szText[ cchRESOURCE ];
570 switch (CurrentState)
572 case SERVICE_STOPPED:
573 GetString (szText, (fValidCell) ? IDS_STATE_STOPPED : IDS_STOPPED_NOCELL);
575 case SERVICE_RUNNING:
576 GetString (szText, IDS_STATE_RUNNING);
578 case SERVICE_START_PENDING:
579 GetString (szText, IDS_STATE_STARTING);
581 case SERVICE_STOP_PENDING:
582 GetString (szText, IDS_STATE_STOPPING);
585 GetString (szText, IDS_STATE_UNKNOWN);
588 SetDlgItemText (hDlg, IDC_STATUS, szText);
590 // Enable or disable controls as necessary
592 BOOL fIfServiceStopped = !(g.fIsWinNT && !g.fIsAdmin);
593 BOOL fIfServiceRunning = fIfServiceStopped && (CurrentState == SERVICE_RUNNING);
595 EnableWindow (GetDlgItem (hDlg, IDC_SERVICE_START), ((CurrentState == SERVICE_STOPPED) && (g.fIsAdmin) && (fValidCell)));
596 EnableWindow (GetDlgItem (hDlg, IDC_SERVICE_STOP), ((CurrentState == SERVICE_RUNNING) && (g.fIsAdmin)));
600 BOOL GeneralTab_AskIfStopped (HWND hDlg)
602 BOOL fStopService = FALSE;
603 BOOL fStartService = FALSE;
605 // If we changed things, ask if we should restart the service.
606 // Otherwise, if it's stopped, ask the user if we should start the service.
608 DWORD CurrentState = Config_GetServiceState();
611 if ((CurrentState == SERVICE_RUNNING) && (g.fNeedRestart))
613 if (Message (MB_YESNO | MB_ICONQUESTION, IDS_RESTART_TITLE, IDS_RESTART_DESC) == IDYES)
616 fStartService = TRUE;
619 if (CurrentState == SERVICE_STOPPED)
621 if (Message (MB_YESNO | MB_ICONQUESTION, GetCautionTitle(), IDS_OKSTOP_DESC) == IDYES)
623 fStartService = TRUE;
628 // If we need to, start or stop-n-restart the service
630 if (fStartService && fStopService)
632 GeneralTab_DoStartStop (hDlg, FALSE, TRUE); // Stop and restart the thing
634 else if (fStartService && !fStopService)
636 GeneralTab_DoStartStop (hDlg, TRUE, FALSE); // Just start it
641 while ( (l.fRestartIfStopped) ||
642 (l.fWarnIfNotStopped) ||
646 if (!GetMessage (&msg, NULL, 0, 0))
648 if (IsMemoryManagerMessage (&msg))
650 TranslateMessage (&msg);
651 DispatchMessage (&msg);
655 if (fStartService && !l.fServiceIsRunning)
662 BOOL fIsCellInCellServDB (LPCTSTR pszCell)
665 CELLSERVDB CellServDB;
666 char cellname[256], i;
668 /* we pray for all ascii cellnames */
669 for ( i=0 ; pszCell[i] && i < (sizeof(cellname)-1) ; i++ )
670 cellname[i] = pszCell[i];
673 ULONG code = cm_SearchCellRegistry(1, cellname, NULL, NULL, NULL, NULL);
677 (code != CM_ERROR_FORCE_DNS_LOOKUP) &&
678 CSDB_ReadFile (&CellServDB, NULL))
680 if (CSDB_FindCell (&CellServDB, pszCell))
682 CSDB_FreeFile (&CellServDB);
684 if ( fFound == FALSE ) {
686 fFound = !cm_SearchCellByDNS(cellname, NULL, &ttl, NULL, NULL);
694 void GeneralTab_DoStartStop (HWND hDlg, BOOL fStart, BOOL fRestart)
696 BOOL fSuccess = FALSE;
700 if ((hManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS)) != NULL)
703 if ((hService = OpenService (hManager, TEXT("TransarcAFSDaemon"), SERVICE_ALL_ACCESS)) != NULL)
707 g.fNeedRestart = FALSE;
708 if (StartService (hService, 0, 0))
709 TestAndDoMapShare(SERVICE_START_PENDING);
714 SERVICE_STATUS Status;
715 if (ControlService (hService, SERVICE_CONTROL_STOP, &Status))
717 if (g.Configuration.fLogonAuthent)
721 CloseServiceHandle (hService);
724 CloseServiceHandle (hManager);
729 l.fWarnIfStopped = fStart;
730 l.fWarnIfNotStopped = !fStart;
731 l.fRestartIfStopped = fRestart && !fStart;
732 l.fStarting = fStart;
733 GeneralTab_OnTimer (hDlg);
737 l.fWarnIfStopped = FALSE;
738 l.fWarnIfNotStopped = FALSE;
739 l.fRestartIfStopped = FALSE;
740 GeneralTab_OnTimer (hDlg);
743 error = GetLastError();
744 Message (MB_OK | MB_ICONHAND, GetErrorTitle(), ((fStart) ? IDS_SERVICE_FAIL_START : IDS_SERVICE_FAIL_STOP), TEXT("%08lX"), error);
749 BOOL CALLBACK Status_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
755 ShowWindow (l.hStatus, SW_SHOW);
756 Status_OnRefresh (hDlg);
767 DestroyWindow (hDlg);
771 Status_OnRefresh (hDlg);
781 void Status_OnRefresh (HWND hDlg)
783 DWORD CurrentState = Config_GetServiceState();
784 if (CurrentState == SERVICE_START_PENDING)
786 else if (CurrentState == SERVICE_STOP_PENDING)
789 ShowWindow (GetDlgItem (l.hStatus, IDC_STARTING), l.fStarting);
790 ShowWindow (GetDlgItem (l.hStatus, IDC_STOPPING), !l.fStarting);