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