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