2 * Copyright 2000, International Business Machines Corporation and others.
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
14 #include <afsconfig.h>
15 #include <afs/param.h>
23 * PROTOTYPES _________________________________________________________________
27 void Creds_OnUpdate (HWND hDlg
);
28 void Creds_OnCheckRemind (HWND hDlg
);
29 void Creds_OnClickObtain (HWND hDlg
);
30 void Creds_OnClickDestroy (HWND hDlg
);
32 BOOL CALLBACK
NewCreds_DlgProc (HWND hDlg
, UINT msg
, WPARAM wp
, LPARAM lp
);
33 void NewCreds_OnInitDialog (HWND hDlg
);
34 void NewCreds_OnEnable (HWND hDlg
);
35 BOOL
NewCreds_OnOK (HWND hDlg
);
36 void NewCreds_OnCancel (HWND hDlg
);
40 * ROUTINES ___________________________________________________________________
44 BOOL CALLBACK
Creds_DlgProc (HWND hDlg
, UINT msg
, WPARAM wp
, LPARAM lp
)
50 GetClientRect (GetParent(hDlg
), &rTab
);
51 TabCtrl_AdjustRect (GetParent (hDlg
), FALSE
, &rTab
);
52 SetWindowPos (hDlg
, NULL
, rTab
.left
, rTab
.top
, 0, 0, SWP_NOSIZE
| SWP_NOACTIVATE
| SWP_NOZORDER
);
54 SetWindowLongPtr (hDlg
, DWLP_USER
, lp
);
55 Creds_OnUpdate (hDlg
);
61 case IDC_CREDS_REMIND
:
62 Creds_OnCheckRemind (hDlg
);
65 case IDC_CREDS_OBTAIN
:
66 Creds_OnClickObtain (hDlg
);
69 case IDC_CREDS_DESTROY
:
70 Creds_OnClickDestroy (hDlg
);
74 Creds_DlgProc (hDlg
, WM_HELP
, 0, 0);
84 void Creds_OnCheckRemind (HWND hDlg
)
86 LPTSTR pszCell
= (LPTSTR
)GetWindowLongPtr (hDlg
, DWLP_USER
);
87 lock_ObtainMutex(&g
.credsLock
);
89 for (iCreds
= 0; iCreds
< g
.cCreds
; ++iCreds
)
91 if (!lstrcmpi (g
.aCreds
[ iCreds
].szCell
, pszCell
))
95 if (iCreds
!= g
.cCreds
)
97 g
.aCreds
[ iCreds
].fRemind
= IsDlgButtonChecked (hDlg
, IDC_CREDS_REMIND
);
100 lock_ReleaseMutex(&g
.credsLock
);
104 void Creds_OnUpdate (HWND hDlg
)
106 LPTSTR pszCell
= (LPTSTR
)GetWindowLongPtr (hDlg
, DWLP_USER
);
107 if (!pszCell
|| !*pszCell
)
109 BOOL fRunning
= IsServiceRunning();
110 ShowWindow (GetDlgItem (hDlg
, IDC_RUNNING
), fRunning
);
111 ShowWindow (GetDlgItem (hDlg
, IDC_STOPPED
), !fRunning
);
112 ShowWindow (GetDlgItem (hDlg
, IDC_CREDS_OBTAIN
), fRunning
);
116 lock_ObtainMutex(&g
.credsLock
);
118 for (iCreds
= 0; iCreds
< g
.cCreds
; ++iCreds
)
120 if (!lstrcmpi (g
.aCreds
[ iCreds
].szCell
, pszCell
))
124 TCHAR szGateway
[cchRESOURCE
] = TEXT("");
126 GetGatewayName (szGateway
);
130 SetDlgItemText (hDlg
, IDC_CREDS_CELL
, pszCell
);
134 TCHAR szCell
[ cchRESOURCE
];
135 TCHAR szFormat
[ cchRESOURCE
];
136 GetString (szFormat
, IDS_CELL_GATEWAY
);
137 wsprintf (szCell
, szFormat
, pszCell
, szGateway
);
138 SetDlgItemText (hDlg
, IDC_CREDS_CELL
, szCell
);
141 if (iCreds
== g
.cCreds
)
143 TCHAR szText
[cchRESOURCE
];
144 GetString (szText
, IDS_NO_CREDS
);
145 SetDlgItemText (hDlg
, IDC_CREDS_INFO
, szText
);
149 // FormatString(%t) expects a date in GMT, not the local time zone...
152 SystemTimeToFileTime (&g
.aCreds
[ iCreds
].stExpires
, &ftLocal
);
155 LocalFileTimeToFileTime (&ftLocal
, &ftGMT
);
158 FileTimeToSystemTime (&ftGMT
, &stGMT
);
161 GetLocalTime (&stNow
);
164 SystemTimeToFileTime (&stNow
, &ftNow
);
166 LONGLONG llNow
= (((LONGLONG
)ftNow
.dwHighDateTime
) << 32) + (LONGLONG
)(ftNow
.dwLowDateTime
);
167 LONGLONG llExpires
= (((LONGLONG
)ftLocal
.dwHighDateTime
) << 32) + (LONGLONG
)(ftLocal
.dwLowDateTime
);
169 llNow
/= c100ns1SECOND
;
170 llExpires
/= c100ns1SECOND
;
172 LPTSTR pszCreds
= NULL
;
173 if (llExpires
<= (llNow
+ (LONGLONG
)cminREMIND_WARN
* csec1MINUTE
))
174 pszCreds
= FormatString (IDS_CREDS_EXPIRED
, TEXT("%s"), g
.aCreds
[ iCreds
].szUser
);
176 if (!pszCreds
|| !pszCreds
[0])
177 pszCreds
= FormatString (IDS_CREDS
, TEXT("%s%t"), g
.aCreds
[ iCreds
].szUser
, &stGMT
);
178 SetDlgItemText (hDlg
, IDC_CREDS_INFO
, pszCreds
);
179 FreeString (pszCreds
);
182 lock_ReleaseMutex(&g
.credsLock
);
183 CheckDlgButton (hDlg
, IDC_CREDS_REMIND
, (iCreds
== g
.cCreds
) ? FALSE
: g
.aCreds
[iCreds
].fRemind
);
185 EnableWindow (GetDlgItem (hDlg
, IDC_CREDS_OBTAIN
), IsServiceRunning());
186 EnableWindow (GetDlgItem (hDlg
, IDC_CREDS_REMIND
), (iCreds
!= g
.cCreds
));
187 EnableWindow (GetDlgItem (hDlg
, IDC_CREDS_DESTROY
), (iCreds
!= g
.cCreds
));
191 void Creds_OnClickObtain (HWND hDlg
)
193 LPTSTR pszCell
= (LPTSTR
)GetWindowLongPtr (hDlg
, DWLP_USER
);
195 InterlockedIncrement (&g
.fShowingMessage
);
196 ShowObtainCreds (FALSE
, pszCell
);
200 void Creds_OnClickDestroy (HWND hDlg
)
202 LPTSTR pszCell
= (LPTSTR
)GetWindowLongPtr (hDlg
, DWLP_USER
);
203 if (pszCell
&& *pszCell
)
205 DestroyCurrentCredentials (pszCell
);
206 Main_RepopulateTabs (FALSE
);
207 Creds_OnUpdate (hDlg
);
212 struct _obtaincreds
{
218 void ObtainCredsThread(void * data
)
220 struct _obtaincreds
* oc
= (struct _obtaincreds
*)data
;
222 ModalDialogParam (oc
->type
, oc
->parent
, (DLGPROC
)NewCreds_DlgProc
, (LPARAM
)oc
->cell
);
227 void ShowObtainCreds (BOOL fExpiring
, LPTSTR pszCell
)
229 struct _obtaincreds
* oc
= (struct _obtaincreds
*)malloc(sizeof(struct _obtaincreds
));
232 oc
->parent
= (IsWindowVisible (g
.hMain
)) ? g
.hMain
: NULL
;
233 oc
->type
= fExpiring
? IDD_NEWCREDS_EXPIRE
: IDD_NEWCREDS
;
234 oc
->cell
= strdup(pszCell
);
237 ULONG threadID
= 123;
239 thread
= CreateThread(NULL
, 0, (PTHREAD_START_ROUTINE
)ObtainCredsThread
,
250 BOOL CALLBACK
NewCreds_DlgProc (HWND hDlg
, UINT msg
, WPARAM wp
, LPARAM lp
)
255 SetWindowLongPtr (hDlg
, DWLP_USER
, lp
);
256 NewCreds_OnInitDialog (hDlg
);
260 InterlockedDecrement (&g
.fShowingMessage
);
267 if (NewCreds_OnOK (hDlg
))
268 EndDialog (hDlg
, IDOK
);
272 NewCreds_OnCancel (hDlg
);
273 EndDialog (hDlg
, IDCANCEL
);
276 case IDC_NEWCREDS_USER
:
277 case IDC_NEWCREDS_PASSWORD
:
278 NewCreds_OnEnable (hDlg
);
282 NewCreds_DlgProc (hDlg
, WM_HELP
, 0, 0);
291 void NewCreds_OnInitDialog (HWND hDlg
)
293 LPTSTR pszCell
= (LPTSTR
)GetWindowLongPtr (hDlg
, DWLP_USER
);
297 if (GetDlgItem (hDlg
, IDC_NEWCREDS_TITLE
))
299 TCHAR szText
[ cchRESOURCE
];
300 GetDlgItemText (hDlg
, IDC_NEWCREDS_TITLE
, szText
, cchRESOURCE
);
301 LPTSTR pszText
= FormatString (szText
, TEXT("%s"), pszCell
);
302 SetDlgItemText (hDlg
, IDC_NEWCREDS_TITLE
, pszText
);
303 FreeString (pszText
);
306 if (pszCell
&& *pszCell
)
308 SetDlgItemText (hDlg
, IDC_NEWCREDS_CELL
, pszCell
);
312 TCHAR szCell
[ cchRESOURCE
] = TEXT("");
313 (void)GetDefaultCell (szCell
);
314 SetDlgItemText (hDlg
, IDC_NEWCREDS_CELL
, szCell
);
317 lock_ObtainMutex(&g
.credsLock
);
319 for (iCreds
= 0; iCreds
< g
.cCreds
; ++iCreds
)
321 if (*pszCell
&& !lstrcmpi (g
.aCreds
[ iCreds
].szCell
, pszCell
))
324 if ((iCreds
== g
.cCreds
) || (!g
.aCreds
[ iCreds
].szUser
[0]))
326 PostMessage (hDlg
, WM_NEXTDLGCTL
, (WPARAM
)GetDlgItem (hDlg
, IDC_NEWCREDS_USER
), TRUE
);
328 else // (we have a valid username already)
330 SetDlgItemText (hDlg
, IDC_NEWCREDS_USER
, g
.aCreds
[ iCreds
].szUser
);
331 PostMessage (hDlg
, WM_NEXTDLGCTL
, (WPARAM
)GetDlgItem (hDlg
, IDC_NEWCREDS_PASSWORD
), TRUE
);
333 lock_ReleaseMutex(&g
.credsLock
);
335 NewCreds_OnEnable (hDlg
);
336 SetForegroundWindow(hDlg
);
337 KillTimer (g
.hMain
, ID_SERVICE_TIMER
);
341 void NewCreds_OnEnable (HWND hDlg
)
345 TCHAR szUser
[ cchRESOURCE
];
346 GetDlgItemText (hDlg
, IDC_NEWCREDS_USER
, szUser
, cchRESOURCE
);
350 if ( !strchr(szUser
, '@') ) {
351 TCHAR szPassword
[ cchRESOURCE
];
352 GetDlgItemText (hDlg
, IDC_NEWCREDS_PASSWORD
, szPassword
, cchRESOURCE
);
356 EnableWindow (GetDlgItem (hDlg
, IDOK
), fEnable
);
360 BOOL
NewCreds_OnOK (HWND hDlg
)
362 TCHAR szCell
[ cchRESOURCE
];
363 GetDlgItemText (hDlg
, IDC_NEWCREDS_CELL
, szCell
, cchRESOURCE
);
365 TCHAR szUser
[ cchRESOURCE
];
366 GetDlgItemText (hDlg
, IDC_NEWCREDS_USER
, szUser
, cchRESOURCE
);
368 TCHAR szPassword
[ cchRESOURCE
];
369 GetDlgItemText (hDlg
, IDC_NEWCREDS_PASSWORD
, szPassword
, cchRESOURCE
);
372 if ((rc
= ObtainNewCredentials (szCell
, szUser
, szPassword
, FALSE
)) != 0)
374 EnableWindow (GetDlgItem (hDlg
, IDOK
), TRUE
);
375 EnableWindow (GetDlgItem (hDlg
, IDCANCEL
), TRUE
);
376 EnableWindow (GetDlgItem (hDlg
, IDC_NEWCREDS_CELL
), TRUE
);
377 EnableWindow (GetDlgItem (hDlg
, IDC_NEWCREDS_USER
), TRUE
);
378 EnableWindow (GetDlgItem (hDlg
, IDC_NEWCREDS_PASSWORD
), TRUE
);
382 Main_RepopulateTabs (FALSE
);
387 void NewCreds_OnCancel (HWND hDlg
)
389 TCHAR szText
[ cchRESOURCE
] = "";
390 LPTSTR pszCell
= NULL
;
392 if (GetDlgItem (hDlg
, IDC_NEWCREDS_CELL
))
394 GetDlgItemText (hDlg
, IDC_NEWCREDS_CELL
, szText
, cchRESOURCE
);
400 pszCell
= (LPTSTR
)GetWindowLongPtr (hDlg
, DWLP_USER
);
403 HWND hTab
= GetDlgItem (g
.hMain
, IDC_TABS
);
404 LPTSTR pszTab
= (LPTSTR
)GetTabParam (hTab
, TabCtrl_GetCurSel(hTab
));
405 HWND hChildDlg
= NULL
;
407 lock_ObtainMutex(&g
.credsLock
);
408 for (size_t iCreds
= 0; iCreds
< g
.cCreds
; ++iCreds
)
410 if (!lstrcmpi (g
.aCreds
[ iCreds
].szCell
, pszCell
))
412 g
.aCreds
[ iCreds
].fRemind
= FALSE
;
415 // Check the active tab, and fix its checkbox if necessary
417 if (pszTab
&& HIWORD(pszTab
) && (!lstrcmpi (pszTab
, pszCell
)))
419 hChildDlg
= GetTabChild (hTab
);
423 lock_ReleaseMutex(&g
.credsLock
);
426 CheckDlgButton (hChildDlg
, IDC_CREDS_REMIND
, FALSE
);