per-user-registry-20040320
[openafs.git] / src / WINNT / client_config / tab_general.cpp
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
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
8  */
9
10 extern "C" {
11 #include <afs/param.h>
12 #include <afs/stds.h>
13 #include <rx/rxkad.h>
14 }
15
16 #include "afs_config.h"
17 #include "tab_general.h"
18 #include "tab_hosts.h"
19 #include "tab_advanced.h"
20
21 #include "drivemap.h"
22 #include <adssts.h>
23
24 /*
25  * VARIABLES __________________________________________________________________
26  *
27  */
28
29 static struct l
30    {
31    BOOL fWarnIfStopped;
32    BOOL fWarnIfNotStopped;
33    BOOL fRestartIfStopped;
34    BOOL fServiceIsRunning;
35    BOOL fStarting;
36    HWND hStatus;
37    } l;
38
39
40 /*
41  * DEFINITIONS ________________________________________________________________
42  *
43  */
44
45 #define ID_TIMER                0
46
47 #define cmsecIDLE_REFRESH   10000
48 #define cmsecFAST_REFRESH    1000
49
50
51 /*
52  * PROTOTYPES _________________________________________________________________
53  *
54  */
55
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);
64
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);
70
71 BOOL fIsCellInCellServDB (LPCTSTR pszCell);
72
73 BOOL CALLBACK Status_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
74 void Status_OnRefresh (HWND hDlg);
75
76 /*
77  * ROUTINES ___________________________________________________________________
78  *
79  */
80
81 BOOL CALLBACK GeneralTab_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
82 {
83    switch (msg)
84       {
85       case WM_INITDIALOG:
86          Main_OnInitDialog (GetParent(hDlg));
87          GeneralTab_OnInitDialog (hDlg);
88          break;
89
90       case WM_DESTROY:
91          GeneralTab_FixRefreshTimer (hDlg, 0);
92          break;
93
94       case WM_TIMER:
95          GeneralTab_OnTimer (hDlg);
96          break;
97
98       case WM_COMMAND:
99          switch (LOWORD(wp))
100             {
101             case IDAPPLY:
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);
106                break;
107
108             case IDC_REFRESH:
109                GeneralTab_OnRefresh (hDlg, FALSE);
110                break;
111
112             case IDC_SERVICE_START:
113                GeneralTab_OnStartStop (hDlg, TRUE);
114                break;
115
116             case IDC_SERVICE_STOP:
117                GeneralTab_OnStartStop (hDlg, FALSE);
118                break;
119
120             case IDC_GATEWAY_CONN:
121                GeneralTab_OnConnect (hDlg);
122                break;
123
124             case IDC_GATEWAY:
125                GeneralTab_OnGateway (hDlg);
126                break;
127
128             case IDC_CELL:
129                GeneralTab_OnCell (hDlg);
130                break;
131
132             case IDHELP:
133                GeneralTab_DlgProc (hDlg, WM_HELP, 0, 0);
134                break;
135             }
136          break;
137
138       case WM_HELP:
139          if (g.fIsWinNT)
140             WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCONFIG_GENERAL_NT);
141          else
142             WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCONFIG_GENERAL_95);
143          break;
144       }
145
146    return FALSE;
147 }
148
149
150 void GeneralTab_OnInitDialog (HWND hDlg)
151 {
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);
157 }
158
159
160 BOOL GeneralTab_VerifyCell (HWND hDlg, BOOL fComplainIfInvalid, LPCTSTR pszCell)
161 {
162    TCHAR szNoCell[ cchRESOURCE ];
163    GetString (szNoCell, IDS_CELL_UNKNOWN);
164
165    TCHAR szCell[ cchRESOURCE ];
166    if (pszCell)
167       lstrcpy (szCell, pszCell);
168    else
169       GetDlgItemText (hDlg, IDC_CELL, szCell, cchRESOURCE);
170    if ((!szCell[0]) || (!lstrcmpi (szNoCell, szCell)))
171       {
172       if (fComplainIfInvalid)
173          {
174          if (g.fIsWinNT)
175             Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_NOCELL_DESC);
176          else
177             Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADGATEWAY_DESC);
178          }
179       return FALSE;
180       }
181
182    if (!fIsCellInCellServDB (szCell))
183       {
184       if (fComplainIfInvalid)
185          {
186          if (g.fIsWinNT)
187             Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADCELL_DESC);
188          else
189             Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADGWCELL_DESC, TEXT("%s"), szCell);
190          }
191       return FALSE;
192       }
193
194    return TRUE;
195 }
196
197
198 BOOL GeneralTab_VerifyOK (HWND hDlg, BOOL fComplainIfInvalid)
199 {
200    // If it's Windows 95, make sure there's a valid Gateway entry
201    //
202    if (!g.fIsWinNT)
203       {
204       TCHAR szGateway[ cchRESOURCE ];
205       GetDlgItemText (hDlg, IDC_GATEWAY, szGateway, cchRESOURCE);
206       if (!szGateway[0])
207          {
208          if (fComplainIfInvalid)
209             Message (MB_ICONASTERISK | MB_OK, IDS_NOGATEWAY_TITLE, IDS_NOGATEWAY_DESC);
210          return FALSE;
211          }
212       }
213
214    // Make sure the cell is in our CellServDB.
215    //
216    if (g.fIsWinNT)
217       {
218       if (!GeneralTab_VerifyCell (hDlg, fComplainIfInvalid, NULL))
219          return FALSE;
220       }
221
222    return TRUE;
223 }
224
225 BOOL GeneralTab_OnApply (HWND hDlg, BOOL fForce, BOOL fComplainIfInvalid)
226 {
227    if (!fForce)
228       {
229       // Don't try to do anything if we've already failed the apply
230       if (GetWindowLong (hDlg, DWL_MSGRESULT))
231          return FALSE;
232       }
233
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.
238    //
239    if (!HostsTab_CommitChanges (fForce))
240       return FALSE;
241
242    if (!AdvancedTab_CommitChanges (fForce))
243       return FALSE;
244
245    if (!GeneralTab_VerifyOK (hDlg, fComplainIfInvalid))
246       return FALSE;
247
248    TCHAR szText[ MAX_PATH ];
249
250    if (g.fIsWinNT)
251       {
252       GetDlgItemText (hDlg, IDC_CELL, szText, MAX_PATH);
253       if (lstrcmpi (szText, g.Configuration.szCell))
254          {
255          if (!Config_SetCellName (szText))
256             return FALSE;
257          lstrcpy (g.Configuration.szCell, szText);
258          }
259       }
260
261    BOOL fLogonAuthent = IsDlgButtonChecked (hDlg, IDC_LOGON);
262    if (fLogonAuthent != g.Configuration.fLogonAuthent)
263       {
264            SetBitLogonOption(fLogonAuthent,LOGON_OPTION_INTEGRATED);
265       g.Configuration.fLogonAuthent = fLogonAuthent;
266       }
267
268    Config_SetTrayIconFlag (IsDlgButtonChecked (hDlg, IDC_TRAYICON));
269
270    if (g.fIsWinNT)
271       {
272       BOOL fBeGateway = IsDlgButtonChecked (hDlg, IDC_GATEWAY);
273       if (fBeGateway != g.Configuration.fBeGateway)
274          {
275          if (!Config_SetGatewayFlag (fBeGateway))
276             return FALSE;
277          g.fNeedRestart = TRUE;
278          g.Configuration.fBeGateway = fBeGateway;
279          }
280       }
281    else // (!g.fIsWinNT)
282       {
283       GetDlgItemText (hDlg, IDC_GATEWAY, szText, MAX_PATH);
284       if (lstrcmpi (szText, g.Configuration.szGateway))
285          {
286          TCHAR szNewCell[ MAX_PATH ];
287          if (!Config_ContactGateway (szText, szNewCell))
288             {
289             Message (MB_ICONASTERISK | MB_OK, GetErrorTitle(), IDS_BADGATEWAY_DESC);
290             return FALSE;
291             }
292
293          if (!GeneralTab_VerifyCell (hDlg, fComplainIfInvalid, szNewCell))
294             return FALSE;
295
296          if (!Config_SetGatewayName (szText))
297             return FALSE;
298
299          if (!Config_SetCellName (szNewCell))
300             return FALSE;
301
302          Config_FixGatewayDrives();
303
304          SetDlgItemText (hDlg, IDC_CELL, szNewCell);
305          lstrcpy (g.Configuration.szGateway, szText);
306          lstrcpy (g.Configuration.szCell, szNewCell);
307
308          GeneralTab_OnGateway (hDlg);
309          }
310       }
311
312    return TRUE;
313 }
314
315
316 void GeneralTab_OnRefresh (HWND hDlg, BOOL fRequery)
317 {
318    // If necessary, update any fields in g.Configuration that we care about
319    //
320    if (fRequery)
321       {
322       if (g.fIsWinNT)
323          Config_GetGatewayFlag (&g.Configuration.fBeGateway);
324       else
325          Config_GetGatewayName (g.Configuration.szGateway);
326
327       Config_GetCellName (g.Configuration.szCell);
328       g.Configuration.fLogonAuthent=RWLogonOption(TRUE,LOGON_OPTION_INTEGRATED);
329       Config_GetTrayIconFlag (&g.Configuration.fShowTrayIcon);
330
331       if (!g.fIsWinNT)
332          SetDlgItemText (hDlg, IDC_GATEWAY, g.Configuration.szGateway);
333       else
334          CheckDlgButton (hDlg, IDC_GATEWAY, g.Configuration.fBeGateway);
335
336       SetDlgItemText (hDlg, IDC_CELL, g.Configuration.szCell);
337       CheckDlgButton (hDlg, IDC_LOGON, g.Configuration.fLogonAuthent);
338       CheckDlgButton (hDlg, IDC_TRAYICON, g.Configuration.fShowTrayIcon);
339       }
340
341    // Update our display of the service's status
342    //
343    DWORD CurrentState = Config_GetServiceState();
344    BOOL fIfServiceStopped = !(g.fIsWinNT && !g.fIsAdmin);
345    BOOL fIfServiceRunning = fIfServiceStopped && (CurrentState == SERVICE_RUNNING);
346
347    GeneralTab_ShowCurrentState (hDlg);
348
349    EnableWindow (GetDlgItem (hDlg, IDC_CELL), fIfServiceStopped && g.fIsWinNT);
350
351    EnableWindow (GetDlgItem (hDlg, IDC_LOGON), fIfServiceStopped);
352    EnableWindow (GetDlgItem (hDlg, IDC_GATEWAY), fIfServiceStopped);
353
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.
357    //
358    TCHAR szText[ cchRESOURCE ];
359    if ((!g.fIsWinNT) && (CurrentState != SERVICE_RUNNING))
360       {
361       GetString (szText, IDS_WARN_STOPPED);
362       SetDlgItemText (hDlg, IDC_WARN, szText);
363       ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_SHOW);
364       }
365    else if (g.fIsWinNT && !g.fIsAdmin)
366       {
367       GetString (szText, IDS_WARN_ADMIN);
368       SetDlgItemText (hDlg, IDC_WARN, szText);
369       ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_SHOW);
370       }
371    else // ((CurrentState == SERVICE_RUNNING) && (g.fIsAdmin))
372       {
373       ShowWindow (GetDlgItem (hDlg, IDC_WARN), SW_HIDE);
374       }
375
376    GeneralTab_OnGateway (hDlg);
377
378    // If the service isn't running/stopped, we may need to complain
379    //
380    if ((CurrentState == SERVICE_RUNNING) && (l.fWarnIfNotStopped))
381       {
382       Message (MB_ICONHAND, GetErrorTitle(), IDS_SERVICE_FAIL_STOP, TEXT("%08lX"), ERROR_SERVICE_SPECIFIC_ERROR);
383       }
384    else if ((CurrentState == SERVICE_STOPPED) && (l.fWarnIfStopped))
385       {
386       Message (MB_ICONHAND, GetErrorTitle(), IDS_SERVICE_FAIL_START, TEXT("%08lX"), ERROR_SERVICE_SPECIFIC_ERROR);
387       }
388
389    if ((CurrentState == SERVICE_RUNNING) || (CurrentState == SERVICE_STOPPED))
390       {
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);
396
397       if (fRestart)
398          {
399          GeneralTab_DoStartStop (hDlg, TRUE, FALSE);
400          }
401       }
402 }
403
404
405 void GeneralTab_OnTimer (HWND hDlg)
406 {
407    DWORD CurrentState = Config_GetServiceState();
408    DWORD DisplayState = GeneralTab_GetDisplayState(hDlg);
409    TestAndDoMapShare(CurrentState);             //Re map mounted drives if necessary
410
411    BOOL fInEndState = ((CurrentState == SERVICE_RUNNING) || (CurrentState == SERVICE_STOPPED));
412    if (fInEndState && l.hStatus)
413       {
414       if (IsWindow (l.hStatus))
415          DestroyWindow (l.hStatus);
416       l.hStatus = NULL;
417       }
418    else if (!fInEndState && !l.hStatus)
419       {
420       l.hStatus = ModelessDialog (IDD_STARTSTOP, GetParent (hDlg), (DLGPROC)Status_DlgProc);
421       }
422
423    if (CurrentState != DisplayState)
424       {
425       GeneralTab_OnRefresh (hDlg, FALSE);
426       Main_RefreshAllTabs();
427
428       if (l.hStatus && IsWindow (l.hStatus))
429          PostMessage (l.hStatus, WM_COMMAND, IDINIT, 0);
430       }
431
432    BOOL fNeedFastRefresh = ((CurrentState == SERVICE_STOPPED) || (CurrentState == SERVICE_RUNNING)) ? FALSE : TRUE;
433    BOOL fHaveFastRefresh = ((DisplayState == SERVICE_STOPPED) || (DisplayState == SERVICE_RUNNING)) ? FALSE : TRUE;
434
435    if (fNeedFastRefresh != fHaveFastRefresh)
436       {
437       GeneralTab_FixRefreshTimer (hDlg, ((fNeedFastRefresh) ? cmsecFAST_REFRESH : cmsecIDLE_REFRESH));
438       }
439 }
440
441
442 void GeneralTab_OnStartStop (HWND hDlg, BOOL fStart)
443 {
444    BOOL fSuccess = FALSE;
445    ULONG error = 0;
446
447    // Don't let the user stop the service on a whim; warn him first
448    //
449    if (!fStart)
450       {
451       if (Message (MB_ICONEXCLAMATION | MB_OKCANCEL, GetCautionTitle(), IDS_STOP_DESC) != IDOK)
452          return;
453       }
454
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.
458    //
459    if (!GeneralTab_OnApply (hDlg, TRUE, ((fStart) ? TRUE : FALSE)))
460       {
461       if (fStart)
462          return;
463       }
464
465    // Okay, start the service
466    //
467    GeneralTab_DoStartStop (hDlg, fStart, FALSE);
468 }
469
470
471 void GeneralTab_OnConnect (HWND hDlg)
472 {
473    if (!GeneralTab_OnApply (hDlg, TRUE, TRUE))
474       return;
475    GeneralTab_OnGateway (hDlg);
476    GeneralTab_OnApply (hDlg, TRUE, TRUE);
477 }
478
479
480 void GeneralTab_OnGateway (HWND hDlg)
481 {
482    if (!g.fIsWinNT)
483       {
484       TCHAR szGateway[ cchRESOURCE ];
485       GetDlgItemText (hDlg, IDC_GATEWAY, szGateway, cchRESOURCE);
486
487       BOOL fEnable = TRUE;
488       if (!szGateway[0])
489          fEnable = FALSE;
490       if (!lstrcmpi (szGateway, g.Configuration.szGateway))
491          fEnable = FALSE;
492       EnableWindow (GetDlgItem (hDlg, IDC_GATEWAY_CONN), fEnable);
493       }
494 }
495
496
497 void GeneralTab_OnCell (HWND hDlg)
498 {
499    if (g.fIsWinNT)
500       {
501       GeneralTab_ShowCurrentState (hDlg);
502       }
503 }
504
505
506 void GeneralTab_FixRefreshTimer (HWND hDlg, UINT cmsec)
507 {
508    static BOOL fTimerActive = FALSE;
509    if (fTimerActive)
510       {
511       KillTimer (hDlg, ID_TIMER);
512       fTimerActive = FALSE;
513       }
514
515    if (g.fIsWinNT && (cmsec != 0))
516       {
517       SetTimer (hDlg, ID_TIMER, cmsec, NULL);
518       }
519 }
520
521
522 DWORD GeneralTab_GetDisplayState (HWND hDlg)
523 {
524    TCHAR szText[ cchRESOURCE ];
525    TCHAR szTextNow[ cchRESOURCE ];
526    GetDlgItemText (hDlg, IDC_STATUS, szTextNow, cchRESOURCE);
527
528    GetString (szText, IDS_STATE_STOPPED);
529    if (!lstrcmpi (szTextNow, szText))
530       return SERVICE_STOPPED;
531
532    GetString (szText, IDS_STATE_RUNNING);
533    if (!lstrcmpi (szTextNow, szText))
534       return SERVICE_RUNNING;
535
536    GetString (szText, IDS_STATE_STARTING);
537    if (!lstrcmpi (szTextNow, szText))
538       return SERVICE_START_PENDING;
539
540    GetString (szText, IDS_STATE_STOPPING);
541    if (!lstrcmpi (szTextNow, szText))
542       return SERVICE_STOP_PENDING;
543
544    return 0;
545 }
546
547
548 void GeneralTab_ShowCurrentState (HWND hDlg)
549 {
550    TCHAR szNoCell[ cchRESOURCE ];
551    GetString (szNoCell, IDS_CELL_UNKNOWN);
552
553    TCHAR szCell[ cchRESOURCE ];
554    GetDlgItemText (hDlg, IDC_CELL, szCell, cchRESOURCE);
555
556    BOOL fValidCell = TRUE;
557    if (!szCell[0])
558       fValidCell = FALSE;
559    if (!lstrcmpi (szCell, szNoCell))
560       fValidCell = FALSE;
561
562    DWORD CurrentState = Config_GetServiceState();
563
564    TCHAR szText[ cchRESOURCE ];
565    switch (CurrentState)
566       {
567       case SERVICE_STOPPED:
568          GetString (szText, (fValidCell) ? IDS_STATE_STOPPED : IDS_STOPPED_NOCELL);
569          break;
570       case SERVICE_RUNNING:
571          GetString (szText, IDS_STATE_RUNNING);
572          break;
573       case SERVICE_START_PENDING:
574          GetString (szText, IDS_STATE_STARTING);
575          break;
576       case SERVICE_STOP_PENDING:
577          GetString (szText, IDS_STATE_STOPPING);
578          break;
579       default:
580          GetString (szText, IDS_STATE_UNKNOWN);
581          break;
582       }
583    SetDlgItemText (hDlg, IDC_STATUS, szText);
584
585    // Enable or disable controls as necessary
586    //
587    BOOL fIfServiceStopped = !(g.fIsWinNT && !g.fIsAdmin);
588    BOOL fIfServiceRunning = fIfServiceStopped && (CurrentState == SERVICE_RUNNING);
589
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)));
592 }
593
594
595 BOOL GeneralTab_AskIfStopped (HWND hDlg)
596 {
597    BOOL fStopService = FALSE;
598    BOOL fStartService = FALSE;
599
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.
602    //
603    DWORD CurrentState = Config_GetServiceState();
604    if (g.fIsAdmin)
605       {
606       if ((CurrentState == SERVICE_RUNNING) && (g.fNeedRestart))
607          {
608          if (Message (MB_YESNO | MB_ICONQUESTION, IDS_RESTART_TITLE, IDS_RESTART_DESC) == IDYES)
609             {
610             fStopService = TRUE;
611             fStartService = TRUE;
612             }
613          }
614       if (CurrentState == SERVICE_STOPPED)
615          {
616          if (Message (MB_YESNO | MB_ICONQUESTION, GetCautionTitle(), IDS_OKSTOP_DESC) == IDYES)
617             {
618             fStartService = TRUE;
619             }
620          }
621       }
622
623    // If we need to, start or stop-n-restart the service
624    //
625    if (fStartService && fStopService)
626       {
627       GeneralTab_DoStartStop (hDlg, FALSE, TRUE); // Stop and restart the thing
628       }
629    else if (fStartService && !fStopService)
630       {
631       GeneralTab_DoStartStop (hDlg, TRUE, FALSE); // Just start it
632       }
633
634    if (fStartService)
635       {
636       while ( (l.fRestartIfStopped) ||
637               (l.fWarnIfNotStopped) ||
638               (l.fWarnIfStopped) )
639          {
640          MSG msg;
641          if (!GetMessage (&msg, NULL, 0, 0))
642             break;
643          if (IsMemoryManagerMessage (&msg))
644             continue;
645          TranslateMessage (&msg);
646          DispatchMessage (&msg);
647          }
648       }
649
650    if (fStartService && !l.fServiceIsRunning)
651       return FALSE;
652
653    return TRUE;
654 }
655
656
657 BOOL fIsCellInCellServDB (LPCTSTR pszCell)
658 {
659    BOOL fFound = FALSE;
660
661    CELLSERVDB CellServDB;
662    if (CSDB_ReadFile (&CellServDB, NULL))
663       {
664       if (CSDB_FindCell (&CellServDB, pszCell))
665          fFound = TRUE;
666       CSDB_FreeFile (&CellServDB);
667       }
668
669    return fFound;
670 }
671
672
673 void GeneralTab_DoStartStop (HWND hDlg, BOOL fStart, BOOL fRestart)
674 {
675    BOOL fSuccess = FALSE;
676    ULONG error = 0;
677
678    SC_HANDLE hManager;
679    if ((hManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS)) != NULL)
680       {
681       SC_HANDLE hService;
682       if ((hService = OpenService (hManager, TEXT("TransarcAFSDaemon"), SERVICE_ALL_ACCESS)) != NULL)
683          {
684          if (fStart)
685             {
686             g.fNeedRestart = FALSE;
687             if (StartService (hService, 0, 0))
688                                 TestAndDoMapShare(SERVICE_START_PENDING);
689                fSuccess = TRUE;
690             }
691          else // (!fStart)
692             {
693             SERVICE_STATUS Status;
694             if (ControlService (hService, SERVICE_CONTROL_STOP, &Status))
695                fSuccess = TRUE;
696                            if (g.Configuration.fLogonAuthent)
697                                    DoUnMapShare(FALSE);
698             }
699
700          CloseServiceHandle (hService);
701          }
702
703       CloseServiceHandle (hManager);
704       }
705
706    if (fSuccess)
707       {
708       l.fWarnIfStopped = fStart;
709       l.fWarnIfNotStopped = !fStart;
710       l.fRestartIfStopped = fRestart && !fStart;
711       l.fStarting = fStart;
712       GeneralTab_OnTimer (hDlg);
713       }
714    else
715       {
716       l.fWarnIfStopped = FALSE;
717       l.fWarnIfNotStopped = FALSE;
718       l.fRestartIfStopped = FALSE;
719       GeneralTab_OnTimer (hDlg);
720
721       if (!error)
722          error = GetLastError();
723       Message (MB_OK | MB_ICONHAND, GetErrorTitle(), ((fStart) ? IDS_SERVICE_FAIL_START : IDS_SERVICE_FAIL_STOP), TEXT("%08lX"), error);
724       }
725 }
726
727
728 BOOL CALLBACK Status_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
729 {
730    switch (msg)
731       {
732       case WM_INITDIALOG:
733          l.hStatus = hDlg;
734          ShowWindow (l.hStatus, SW_SHOW);
735          Status_OnRefresh (hDlg);
736          break;
737
738       case WM_DESTROY:
739          l.hStatus = NULL;
740          break;
741
742       case WM_COMMAND:
743          switch (LOWORD(wp))
744             {
745             case IDCANCEL:
746                DestroyWindow (hDlg);
747                break;
748
749             case IDINIT:
750                Status_OnRefresh (hDlg);
751                break;
752             }
753          break;
754       }
755
756    return FALSE;
757 }
758
759
760 void Status_OnRefresh (HWND hDlg)
761 {
762    DWORD CurrentState = Config_GetServiceState();
763    if (CurrentState == SERVICE_START_PENDING)
764       l.fStarting = TRUE;
765    else if (CurrentState == SERVICE_STOP_PENDING)
766       l.fStarting = FALSE;
767
768    ShowWindow (GetDlgItem (l.hStatus, IDC_STARTING), l.fStarting);
769    ShowWindow (GetDlgItem (l.hStatus, IDC_STOPPING), !l.fStarting);
770 }
771