760fc1d32738c96d77cddfbfefffd8848308a06d
[openafs.git] / src / WINNT / client_creds / advtab.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 }
14
15 #include "afscreds.h"
16
17
18 /*
19  * PROTOTYPES _________________________________________________________________
20  *
21  */
22
23 void Advanced_OnInitDialog (HWND hDlg);
24 void Advanced_StartTimer (HWND hDlg);
25 void Advanced_OnServiceTimer (HWND hDlg);
26 void Advanced_OnOpenCPL (HWND hDlg);
27 void Advanced_OnChangeService (HWND hDlg, WORD wCmd);
28 void Advanced_OnStartup (HWND hDlg);
29
30
31 /*
32  * ROUTINES ___________________________________________________________________
33  *
34  */
35
36 BOOL CALLBACK Advanced_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
37 {
38    switch (msg)
39       {
40       case WM_INITDIALOG:
41          RECT rTab;
42          GetClientRect (GetParent(hDlg), &rTab);
43          TabCtrl_AdjustRect (GetParent (hDlg), FALSE, &rTab); 
44          SetWindowPos (hDlg, NULL, rTab.left, rTab.top, 0, 0, SWP_NOSIZE | SWP_NOACTIVATE | SWP_NOZORDER);
45
46          Advanced_OnInitDialog (hDlg);
47          break;
48
49       case WM_TIMER:
50          Advanced_OnServiceTimer (hDlg);
51          break;
52
53       case WM_COMMAND:
54          switch (LOWORD(wp))
55             {
56             case IDC_SERVICE_STOP:
57             case IDC_SERVICE_START:
58             case IDC_SERVICE_AUTO:
59                Advanced_OnChangeService (hDlg, LOWORD(wp));
60                break;
61
62             case IDC_OPEN_CPL:
63                Advanced_OnOpenCPL (hDlg);
64                break;
65
66             case IDC_STARTUP:
67                Advanced_OnStartup (hDlg);
68                break;
69
70             case IDHELP:
71                Advanced_DlgProc (hDlg, WM_HELP, 0, 0);
72                break;
73             }
74          break;
75
76       case WM_HELP:
77          WinHelp (hDlg, g.szHelpFile, HELP_CONTEXT, IDH_AFSCREDS_TAB_ADVANCED);
78          break;
79       }
80    return FALSE;
81 }
82
83
84 void Advanced_OnInitDialog (HWND hDlg)
85 {
86    CheckDlgButton (hDlg, IDC_STARTUP, g.fStartup);
87    Advanced_OnServiceTimer (hDlg);
88    Advanced_StartTimer (hDlg);
89 }
90
91
92 void Advanced_StartTimer (HWND hDlg)
93 {
94    KillTimer (hDlg, ID_SERVICE_TIMER);
95    SetTimer (hDlg, ID_SERVICE_TIMER, (ULONG)cmsecSERVICE, NULL);
96 }
97
98
99 void Advanced_OnServiceTimer (HWND hDlg)
100 {
101    BOOL fFinal = TRUE;
102    BOOL fFound = FALSE;
103
104    struct {
105       QUERY_SERVICE_CONFIG Config;
106       BYTE buf[1024];
107    } Config;
108    memset (&Config, 0x00, sizeof(Config));
109    Config.Config.dwStartType = SERVICE_DEMAND_START;
110
111    SERVICE_STATUS Status;
112    memset (&Status, 0x00, sizeof(Status));
113    Status.dwCurrentState = SERVICE_STOPPED;
114
115    SC_HANDLE hManager;
116    if ((hManager = OpenSCManager (NULL, NULL, GENERIC_READ)) != NULL)
117       {
118       SC_HANDLE hService;
119       if ((hService = OpenService (hManager, TEXT("TransarcAFSDaemon"), GENERIC_READ)) != NULL)
120          {
121          fFound = TRUE;
122          DWORD dwSize = sizeof(Config);
123          QueryServiceConfig (hService, (QUERY_SERVICE_CONFIG*)&Config, sizeof(Config), &dwSize);
124          QueryServiceStatus (hService, &Status);
125                  TestAndDoMapShare(Status.dwCurrentState);
126
127          CloseServiceHandle (hService);
128          }
129
130       CloseServiceHandle (hManager);
131       }
132
133    if (fFound)
134       {
135       if ((Status.dwCurrentState == SERVICE_STOP_PENDING) ||
136           (Status.dwCurrentState == SERVICE_START_PENDING))
137          {
138          fFinal = FALSE;
139          }
140       }
141
142    EnableWindow (GetDlgItem (hDlg, IDC_SERVICE_START), fFound && (Status.dwCurrentState == SERVICE_STOPPED));
143    EnableWindow (GetDlgItem (hDlg, IDC_SERVICE_STOP), fFound && (Status.dwCurrentState == SERVICE_RUNNING));
144    EnableWindow (GetDlgItem (hDlg, IDC_SERVICE_AUTO), fFound);
145    CheckDlgButton (hDlg, IDC_SERVICE_AUTO, fFound && (Config.Config.dwStartType == SERVICE_AUTO_START));
146
147    TCHAR szStatus[cchRESOURCE];
148    if (!fFound)
149       GetString (szStatus, IDS_SERVICE_BROKEN);
150    else if (Status.dwCurrentState == SERVICE_RUNNING)
151       GetString (szStatus, IDS_SERVICE_RUNNING);
152    else if (Status.dwCurrentState == SERVICE_STOPPED)
153       GetString (szStatus, IDS_SERVICE_STOPPED);
154    else if (Status.dwCurrentState == SERVICE_STOP_PENDING)
155       GetString (szStatus, IDS_SERVICE_STOPPING);
156    else if (Status.dwCurrentState == SERVICE_START_PENDING)
157       GetString (szStatus, IDS_SERVICE_STARTING);
158    else
159       GetString (szStatus, IDS_SERVICE_UNKNOWN);
160    TestAndDoMapShare(Status.dwCurrentState);
161    SetDlgItemText (hDlg, IDC_SERVICE_STATUS, szStatus);
162
163    if (fFinal && GetWindowLong (hDlg, DWL_USER))
164       {
165       SetWindowLong (hDlg, DWL_USER, 0);
166       Main_RepopulateTabs (FALSE);
167       }
168
169    if (fFinal)
170       {
171       KillTimer (hDlg, ID_SERVICE_TIMER);
172       }
173 }
174
175
176 void Advanced_OnChangeService (HWND hDlg, WORD wCmd)
177 {
178    BOOL fSuccess = FALSE;
179    ULONG error = 0;
180
181    SC_HANDLE hManager;
182    if ((hManager = OpenSCManager (NULL, NULL, SC_MANAGER_ALL_ACCESS)) != NULL)
183       {
184       SC_HANDLE hService;
185       if ((hService = OpenService (hManager, TEXT("TransarcAFSDaemon"), SERVICE_ALL_ACCESS)) != NULL)
186          {
187          switch (wCmd)
188             {
189             case IDC_SERVICE_AUTO:
190                DWORD StartType;
191                StartType = (IsDlgButtonChecked (hDlg, wCmd)) ? SERVICE_AUTO_START : SERVICE_DEMAND_START;
192
193                if (ChangeServiceConfig (hService, SERVICE_NO_CHANGE, StartType, SERVICE_NO_CHANGE, 0, 0, 0, 0, 0, 0, 0))
194                   fSuccess = TRUE;
195                break;
196
197             case IDC_SERVICE_START:
198                if (StartService (hService, 0, 0))
199                            {
200                                   TestAndDoMapShare(SERVICE_START_PENDING);
201                   fSuccess = TRUE;
202                            }
203                break;
204
205             case IDC_SERVICE_STOP:
206                SERVICE_STATUS Status;
207                            TestAndDoUnMapShare();
208                ControlService (hService, SERVICE_CONTROL_STOP, &Status);
209                fSuccess = TRUE;
210                break;
211             }
212
213          CloseServiceHandle (hService);
214          }
215
216       CloseServiceHandle (hManager);
217       }
218
219    if (fSuccess)
220       {
221       if (wCmd == IDC_SERVICE_STOP)
222          SetWindowLong (hDlg, DWL_USER, TRUE);
223       Advanced_OnServiceTimer (hDlg);
224       Advanced_StartTimer (hDlg);
225       }
226    else
227       {
228       if (!error)
229          error = GetLastError();
230
231       int ids;
232       switch (wCmd)
233          {
234          case IDC_SERVICE_START:
235             ids = IDS_SERVICE_FAIL_START;
236             break;
237          case IDC_SERVICE_STOP:
238             ids = IDS_SERVICE_FAIL_STOP;
239             break;
240          default:
241             ids = IDS_SERVICE_FAIL_CONFIG;
242             break;
243          }
244
245       Message (MB_OK | MB_ICONHAND, IDS_SERVICE_ERROR, ids, TEXT("%08lX"), error);
246       }
247 }
248
249
250 void Advanced_OnOpenCPL (HWND hDlg)
251 {
252    WinExec ("afs_config.exe", SW_SHOW);
253 }
254
255
256 void Advanced_OnStartup (HWND hDlg)
257 {
258    g.fStartup = IsDlgButtonChecked (hDlg, IDC_STARTUP);
259
260    HKEY hk;
261    if (RegCreateKey (HKEY_LOCAL_MACHINE, TEXT("System\\CurrentControlSet\\Services\\TransarcAFSDaemon\\Parameters"), &hk) == 0)
262       {
263       DWORD dwSize = sizeof(g.fStartup);
264       DWORD dwType = REG_DWORD;
265       RegSetValueEx (hk, TEXT("ShowTrayIcon"), NULL, dwType, (PBYTE)&g.fStartup, dwSize);
266       RegCloseKey (hk);
267       }
268
269    Shortcut_FixStartup (cszSHORTCUT_NAME, g.fStartup);
270 }
271