6 #include "afs_config.h"
7 #include "tab_general.h"
9 #include "tab_advanced.h"
13 * VARIABLES __________________________________________________________________
20 BOOL fWarnIfNotStopped;
21 BOOL fRestartIfStopped;
22 BOOL fServiceIsRunning;
29 * DEFINITIONS ________________________________________________________________
35 #define cmsecIDLE_REFRESH 10000
36 #define cmsecFAST_REFRESH 1000
40 * PROTOTYPES _________________________________________________________________
44 void GeneralTab_OnInitDialog (HWND hDlg);
45 void GeneralTab_OnTimer (HWND hDlg);
46 BOOL GeneralTab_OnApply (HWND hDlg, BOOL fForce, BOOL fComplainIfInvalid);
47 void GeneralTab_OnRefresh (HWND hDlg, BOOL fRequery);
48 void GeneralTab_OnStartStop (HWND hDlg, BOOL fStart);
49 void GeneralTab_OnConnect (HWND hDlg);
50 void GeneralTab_OnGateway (HWND hDlg);
51 void GeneralTab_OnCell (HWND hDlg);
53 void GeneralTab_DoStartStop (HWND hDlg, BOOL fStart, BOOL fRestart);
54 void GeneralTab_FixRefreshTimer (HWND hDlg, UINT cmsec = 0);
55 DWORD GeneralTab_GetDisplayState (HWND hDlg);
56 void GeneralTab_ShowCurrentState (HWND hDlg);
57 BOOL GeneralTab_AskIfStopped (HWND hDlg);
59 BOOL fIsCellInCellServDB (LPCTSTR pszCell);
61 BOOL CALLBACK Status_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
62 void Status_OnRefresh (HWND hDlg);
66 * ROUTINES ___________________________________________________________________
70 BOOL CALLBACK GeneralTab_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
75 Main_OnInitDialog (GetParent(hDlg));
76 GeneralTab_OnInitDialog (hDlg);
80 GeneralTab_FixRefreshTimer (hDlg, 0);
84 GeneralTab_OnTimer (hDlg);
91 if (!GeneralTab_OnApply (hDlg, FALSE, TRUE))
92 SetWindowLong (hDlg, DWL_MSGRESULT, TRUE);
93 else if (g.fIsWinNT && !GeneralTab_AskIfStopped (hDlg))
94 SetWindowLong (hDlg, DWL_MSGRESULT, TRUE);
98 GeneralTab_OnRefresh (hDlg, FALSE);
101 case IDC_SERVICE_START:
102 GeneralTab_OnStartStop (hDlg, TRUE);
105 case IDC_SERVICE_STOP:
106 GeneralTab_OnStartStop (hDlg, FALSE);
109 case IDC_GATEWAY_CONN:
110 GeneralTab_OnConnect (hDlg);
114 GeneralTab_OnGateway (hDlg);
118 GeneralTab_OnCell (hDlg);
122 GeneralTab_DlgProc (hDlg, WM_HELP, 0, 0);
129 WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCONFIG_GENERAL_NT);
131 WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCONFIG_GENERAL_95);
139 void GeneralTab_OnInitDialog (HWND hDlg)
141 DWORD CurrentState = Config_GetServiceState();
142 BOOL fNeedFastRefresh = ((CurrentState == SERVICE_STOPPED) || (CurrentState == SERVICE_RUNNING)) ? FALSE : TRUE;
143 GeneralTab_FixRefreshTimer (hDlg, ((fNeedFastRefresh) ? cmsecFAST_REFRESH : cmsecIDLE_REFRESH));
144 GeneralTab_OnTimer (hDlg);
145 GeneralTab_OnRefresh (hDlg, TRUE);
149 BOOL GeneralTab_VerifyCell (HWND hDlg, BOOL fComplainIfInvalid, LPCTSTR pszCell)
151 TCHAR szNoCell[ cchRESOURCE ];
152 GetString (szNoCell, IDS_CELL_UNKNOWN);
154 TCHAR szCell[ cchRESOURCE ];
156 lstrcpy (szCell, pszCell);
158 GetDlgItemText (hDlg, IDC_CELL, szCell, cchRESOURCE);
159 if ((!szCell[0]) || (!lstrcmpi (szNoCell, szCell)))
161 if (fComplainIfInvalid)
164 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_NOCELL_DESC);
166 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADGATEWAY_DESC);
171 if (!fIsCellInCellServDB (szCell))
173 if (fComplainIfInvalid)
176 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADCELL_DESC);
178 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADGWCELL_DESC, TEXT("%s"), szCell);
187 BOOL GeneralTab_VerifyOK (HWND hDlg, BOOL fComplainIfInvalid)
189 // If it's Windows 95, make sure there's a valid Gateway entry
193 TCHAR szGateway[ cchRESOURCE ];
194 GetDlgItemText (hDlg, IDC_GATEWAY, szGateway, cchRESOURCE);
197 if (fComplainIfInvalid)
198 Message (MB_ICONASTERISK | MB_OK, IDS_NOGATEWAY_TITLE, IDS_NOGATEWAY_DESC);
203 // Make sure the cell is in our CellServDB.
207 if (!GeneralTab_VerifyCell (hDlg, fComplainIfInvalid, NULL))
215 BOOL GeneralTab_OnApply (HWND hDlg, BOOL fForce, BOOL fComplainIfInvalid)
219 // Don't try to do anything if we've already failed the apply
220 if (GetWindowLong (hDlg, DWL_MSGRESULT))
224 // If the user has changed CellServDB, configuration parameters for
225 // the driver or anything else, we want to commit those changes first.
226 // We *won't* commit server prefs changes yet, because we haven't yet
227 // checked to see if the service is running.
229 if (!HostsTab_CommitChanges (fForce))
232 if (!AdvancedTab_CommitChanges (fForce))
235 if (!GeneralTab_VerifyOK (hDlg, fComplainIfInvalid))
238 TCHAR szText[ MAX_PATH ];
242 GetDlgItemText (hDlg, IDC_CELL, szText, MAX_PATH);
243 if (lstrcmpi (szText, g.Configuration.szCell))
245 if (!Config_SetCellName (szText))
247 lstrcpy (g.Configuration.szCell, szText);
251 BOOL fLogonAuthent = IsDlgButtonChecked (hDlg, IDC_LOGON);
252 if (fLogonAuthent != g.Configuration.fLogonAuthent)
254 if (!Config_SetAuthentFlag (fLogonAuthent))
256 g.Configuration.fLogonAuthent = fLogonAuthent;
259 Config_SetTrayIconFlag (IsDlgButtonChecked (hDlg, IDC_TRAYICON));
263 BOOL fBeGateway = IsDlgButtonChecked (hDlg, IDC_GATEWAY);
264 if (fBeGateway != g.Configuration.fBeGateway)
266 if (!Config_SetGatewayFlag (fBeGateway))
268 g.fNeedRestart = TRUE;
269 g.Configuration.fBeGateway = fBeGateway;
272 else // (!g.fIsWinNT)
274 GetDlgItemText (hDlg, IDC_GATEWAY, szText, MAX_PATH);
275 if (lstrcmpi (szText, g.Configuration.szGateway))
277 TCHAR szNewCell[ MAX_PATH ];
278 if (!Config_ContactGateway (szText, szNewCell))
280 Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADGATEWAY_DESC);
284 if (!GeneralTab_VerifyCell (hDlg, fComplainIfInvalid, szNewCell))
287 if (!Config_SetGatewayName (szText))
290 if (!Config_SetCellName (szNewCell))
293 Config_FixGatewayDrives();
295 SetDlgItemText (hDlg, IDC_CELL, szNewCell);
296 lstrcpy (g.Configuration.szGateway, szText);
297 lstrcpy (g.Configuration.szCell, szNewCell);
299 GeneralTab_OnGateway (hDlg);
307 void GeneralTab_OnRefresh (HWND hDlg, BOOL fRequery)
309 // If necessary, update any fields in g.Configuration that we care about
314 Config_GetGatewayFlag (&g.Configuration.fBeGateway);
316 Config_GetGatewayName (g.Configuration.szGateway);
318 Config_GetCellName (g.Configuration.szCell);
319 Config_GetAuthentFlag (&g.Configuration.fLogonAuthent);
320 Config_GetTrayIconFlag (&g.Configuration.fShowTrayIcon);
323 SetDlgItemText (hDlg, IDC_GATEWAY, g.Configuration.szGateway);
325 CheckDlgButton (hDlg, IDC_GATEWAY, g.Configuration.fBeGateway);
327 SetDlgItemText (hDlg, IDC_CELL, g.Configuration.szCell);
328 CheckDlgButton (hDlg, IDC_LOGON, g.Configuration.fLogonAuthent);
329 CheckDlgButton (hDlg, IDC_TRAYICON, g.Configuration.fShowTrayIcon);
332 // Update our display of the service's status
334 DWORD CurrentState = Config_GetServiceState();
335 BOOL fIfServiceStopped = !(g.fIsWinNT && !g.fIsAdmin);
336 BOOL fIfServiceRunning = fIfServiceStopped && (CurrentState == SERVICE_RUNNING);
338 GeneralTab_ShowCurrentState (hDlg);
340 EnableWindow (GetDlgItem (hDlg, IDC_CELL), fIfServiceStopped && g.fIsWinNT);
342 EnableWindow (GetDlgItem (hDlg, IDC_LOGON), fIfServiceStopped);
343 EnableWindow (GetDlgItem (hDlg, IDC_GATEWAY), fIfServiceStopped);
345 // Update our warning. Note that under WinNT, this tab doesn't have any
346 // controls (other than Start Service) which disable just because the
347 // service isn't running...so don't show that warning in that case.
349 TCHAR szText[ cchRESOURCE ];
350 if ((!g.fIsWinNT) && (CurrentState != SERVICE_RUNNING))
352 GetString (szText, IDS_WARN_STOPPED);
353 SetDlgItemText (hDlg, IDC_WARN, szText);
354 ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_SHOW);
356 else if (g.fIsWinNT && !g.fIsAdmin)
358 GetString (szText, IDS_WARN_ADMIN);
359 SetDlgItemText (hDlg, IDC_WARN, szText);
360 ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_SHOW);
362 else // ((CurrentState == SERVICE_RUNNING) && (g.fIsAdmin))
364 ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_HIDE);
367 GeneralTab_OnGateway (hDlg);
369 // If the service isn't running/stopped, we may need to complain
371 if ((CurrentState == SERVICE_RUNNING) && (l.fWarnIfNotStopped))
373 Message (MB_ICONHAND, GetErrorTitle(), IDS_SERVICE_FAIL_STOP, TEXT("%08lX"), ERROR_SERVICE_SPECIFIC_ERROR);
375 else if ((CurrentState == SERVICE_STOPPED) && (l.fWarnIfStopped))
377 Message (MB_ICONHAND, GetErrorTitle(), IDS_SERVICE_FAIL_START, TEXT("%08lX"), ERROR_SERVICE_SPECIFIC_ERROR);
380 if ((CurrentState == SERVICE_RUNNING) || (CurrentState == SERVICE_STOPPED))
382 BOOL fRestart = ((CurrentState == SERVICE_STOPPED) && (l.fRestartIfStopped));
383 l.fWarnIfStopped = FALSE;
384 l.fWarnIfNotStopped = FALSE;
385 l.fRestartIfStopped = FALSE;
386 l.fServiceIsRunning = (CurrentState == SERVICE_RUNNING);
390 GeneralTab_DoStartStop (hDlg, TRUE, FALSE);
396 void GeneralTab_OnTimer (HWND hDlg)
398 DWORD CurrentState = Config_GetServiceState();
399 DWORD DisplayState = GeneralTab_GetDisplayState(hDlg);
401 BOOL fInEndState = ((CurrentState == SERVICE_RUNNING) || (CurrentState == SERVICE_STOPPED));
402 if (fInEndState && l.hStatus)
404 if (IsWindow (l.hStatus))
405 DestroyWindow (l.hStatus);
408 else if (!fInEndState && !l.hStatus)
410 l.hStatus = ModelessDialog (IDD_STARTSTOP, GetParent (hDlg), (DLGPROC)Status_DlgProc);
413 if (CurrentState != DisplayState)
415 GeneralTab_OnRefresh (hDlg, FALSE);
416 Main_RefreshAllTabs();
418 if (l.hStatus && IsWindow (l.hStatus))
419 PostMessage (l.hStatus, WM_COMMAND, IDINIT, 0);
422 BOOL fNeedFastRefresh = ((CurrentState == SERVICE_STOPPED) || (CurrentState == SERVICE_RUNNING)) ? FALSE : TRUE;
423 BOOL fHaveFastRefresh = ((DisplayState == SERVICE_STOPPED) || (DisplayState == SERVICE_RUNNING)) ? FALSE : TRUE;
425 if (fNeedFastRefresh != fHaveFastRefresh)
427 GeneralTab_FixRefreshTimer (hDlg, ((fNeedFastRefresh) ? cmsecFAST_REFRESH : cmsecIDLE_REFRESH));
432 void GeneralTab_OnStartStop (HWND hDlg, BOOL fStart)
434 BOOL fSuccess = FALSE;
437 // Don't let the user stop the service on a whim; warn him first
441 if (Message (MB_ICONEXCLAMATION | MB_OKCANCEL, GetCautionTitle(), IDS_STOP_DESC) != IDOK)
445 // To start the service, we'll need to successfully commit our new
446 // configuration. To stop the service, we'll *try*, but it's not
447 // fatal if something goes wrong.
449 if (!GeneralTab_OnApply (hDlg, TRUE, ((fStart) ? TRUE : FALSE)))
455 // Okay, start the service
457 GeneralTab_DoStartStop (hDlg, fStart, FALSE);
461 void GeneralTab_OnConnect (HWND hDlg)
463 if (!GeneralTab_OnApply (hDlg, TRUE, TRUE))
465 GeneralTab_OnGateway (hDlg);
466 GeneralTab_OnApply (hDlg, TRUE, TRUE);
470 void GeneralTab_OnGateway (HWND hDlg)
474 TCHAR szGateway[ cchRESOURCE ];
475 GetDlgItemText (hDlg, IDC_GATEWAY, szGateway, cchRESOURCE);
480 if (!lstrcmpi (szGateway, g.Configuration.szGateway))
482 EnableWindow (GetDlgItem (hDlg, IDC_GATEWAY_CONN), fEnable);
487 void GeneralTab_OnCell (HWND hDlg)
491 GeneralTab_ShowCurrentState (hDlg);
496 void GeneralTab_FixRefreshTimer (HWND hDlg, UINT cmsec)
498 static BOOL fTimerActive = FALSE;
501 KillTimer (hDlg, ID_TIMER);
502 fTimerActive = FALSE;
505 if (g.fIsWinNT && (cmsec != 0))
507 SetTimer (hDlg, ID_TIMER, cmsec, NULL);
512 DWORD GeneralTab_GetDisplayState (HWND hDlg)
514 TCHAR szText[ cchRESOURCE ];
515 TCHAR szTextNow[ cchRESOURCE ];
516 GetDlgItemText (hDlg, IDC_STATUS, szTextNow, cchRESOURCE);
518 GetString (szText, IDS_STATE_STOPPED);
519 if (!lstrcmpi (szTextNow, szText))
520 return SERVICE_STOPPED;
522 GetString (szText, IDS_STATE_RUNNING);
523 if (!lstrcmpi (szTextNow, szText))
524 return SERVICE_RUNNING;
526 GetString (szText, IDS_STATE_STARTING);
527 if (!lstrcmpi (szTextNow, szText))
528 return SERVICE_START_PENDING;
530 GetString (szText, IDS_STATE_STOPPING);
531 if (!lstrcmpi (szTextNow, szText))
532 return SERVICE_STOP_PENDING;
538 void GeneralTab_ShowCurrentState (HWND hDlg)
540 TCHAR szNoCell[ cchRESOURCE ];
541 GetString (szNoCell, IDS_CELL_UNKNOWN);
543 TCHAR szCell[ cchRESOURCE ];
544 GetDlgItemText (hDlg, IDC_CELL, szCell, cchRESOURCE);
546 BOOL fValidCell = TRUE;
549 if (!lstrcmpi (szCell, szNoCell))
552 DWORD CurrentState = Config_GetServiceState();
554 TCHAR szText[ cchRESOURCE ];
555 switch (CurrentState)
557 case SERVICE_STOPPED:
558 GetString (szText, (fValidCell) ? IDS_STATE_STOPPED : IDS_STOPPED_NOCELL);
560 case SERVICE_RUNNING:
561 GetString (szText, IDS_STATE_RUNNING);
563 case SERVICE_START_PENDING:
564 GetString (szText, IDS_STATE_STARTING);
566 case SERVICE_STOP_PENDING:
567 GetString (szText, IDS_STATE_STOPPING);
570 GetString (szText, IDS_STATE_UNKNOWN);
573 SetDlgItemText (hDlg, IDC_STATUS, szText);
575 // Enable or disable controls as necessary
577 BOOL fIfServiceStopped = !(g.fIsWinNT && !g.fIsAdmin);
578 BOOL fIfServiceRunning = fIfServiceStopped && (CurrentState == SERVICE_RUNNING);
580 EnableWindow (GetDlgItem (hDlg, IDC_SERVICE_START), ((CurrentState == SERVICE_STOPPED) && (g.fIsAdmin) && (fValidCell)));
581 EnableWindow (GetDlgItem (hDlg, IDC_SERVICE_STOP), ((CurrentState == SERVICE_RUNNING) && (g.fIsAdmin)));
585 BOOL GeneralTab_AskIfStopped (HWND hDlg)
587 BOOL fStopService = FALSE;
588 BOOL fStartService = FALSE;
590 // If we changed things, ask if we should restart the service.
591 // Otherwise, if it's stopped, ask the user if we should start the service.
593 DWORD CurrentState = Config_GetServiceState();
596 if ((CurrentState == SERVICE_RUNNING) && (g.fNeedRestart))
598 if (Message (MB_YESNO | MB_ICONQUESTION, IDS_RESTART_TITLE, IDS_RESTART_DESC) == IDYES)
601 fStartService = TRUE;
604 if (CurrentState == SERVICE_STOPPED)
606 if (Message (MB_YESNO | MB_ICONQUESTION, GetCautionTitle(), IDS_OKSTOP_DESC) == IDYES)
608 fStartService = TRUE;
613 // If we need to, start or stop-n-restart the service
615 if (fStartService && fStopService)
617 GeneralTab_DoStartStop (hDlg, FALSE, TRUE); // Stop and restart the thing
619 else if (fStartService && !fStopService)
621 GeneralTab_DoStartStop (hDlg, TRUE, FALSE); // Just start it
626 while ( (l.fRestartIfStopped) ||
627 (l.fWarnIfNotStopped) ||
631 if (!GetMessage (&msg, NULL, 0, 0))
633 if (IsMemoryManagerMessage (&msg))
635 TranslateMessage (&msg);
636 DispatchMessage (&msg);
640 if (fStartService && !l.fServiceIsRunning)
647 BOOL fIsCellInCellServDB (LPCTSTR pszCell)
651 CELLSERVDB CellServDB;
652 if (CSDB_ReadFile (&CellServDB, NULL))
654 if (CSDB_FindCell (&CellServDB, pszCell))
656 CSDB_FreeFile (&CellServDB);
663 void GeneralTab_DoStartStop (HWND hDlg, BOOL fStart, BOOL fRestart)
665 BOOL fSuccess = FALSE;
669 if ((hManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS)) != NULL)
672 if ((hService = OpenService (hManager, TEXT("TransarcAFSDaemon"), SERVICE_ALL_ACCESS)) != NULL)
676 g.fNeedRestart = FALSE;
677 if (StartService (hService, 0, 0))
682 SERVICE_STATUS Status;
683 if (ControlService (hService, SERVICE_CONTROL_STOP, &Status))
687 CloseServiceHandle (hService);
690 CloseServiceHandle (hManager);
695 l.fWarnIfStopped = fStart;
696 l.fWarnIfNotStopped = !fStart;
697 l.fRestartIfStopped = fRestart && !fStart;
698 l.fStarting = fStart;
699 GeneralTab_OnTimer (hDlg);
703 l.fWarnIfStopped = FALSE;
704 l.fWarnIfNotStopped = FALSE;
705 l.fRestartIfStopped = FALSE;
706 GeneralTab_OnTimer (hDlg);
709 error = GetLastError();
710 Message (MB_OK | MB_ICONHAND, GetErrorTitle(), ((fStart) ? IDS_SERVICE_FAIL_START : IDS_SERVICE_FAIL_STOP), TEXT("%08lX"), error);
715 BOOL CALLBACK Status_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
721 ShowWindow (l.hStatus, SW_SHOW);
722 Status_OnRefresh (hDlg);
733 DestroyWindow (hDlg);
737 Status_OnRefresh (hDlg);
747 void Status_OnRefresh (HWND hDlg)
749 DWORD CurrentState = Config_GetServiceState();
750 if (CurrentState == SERVICE_START_PENDING)
752 else if (CurrentState == SERVICE_STOP_PENDING)
755 ShowWindow (GetDlgItem (l.hStatus, IDC_STARTING), l.fStarting);
756 ShowWindow (GetDlgItem (l.hStatus, IDC_STOPPING), !l.fStarting);