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 <afs/param.h>
18 #include <WINNT/afsapplib.h>
19 #include "al_dynlink.h"
20 #include <WINNT/TaAfsAdmSvrClient.h>
24 * DEFINITIONS ________________________________________________________________
28 #define GWD_ANIMATIONFRAME TEXT("afsapplib/al_misc.cpp - next animation frame")
32 * PROTOTYPES _________________________________________________________________
36 HRESULT CALLBACK
AfsAppLib_TranslateErrorFunc (LPTSTR pszText
, ULONG code
, LANGID idLanguage
);
40 * STARTUP ____________________________________________________________________
44 static HINSTANCE g_hInst
= NULL
;
45 static HINSTANCE g_hInstApp
= NULL
;
47 HINSTANCE
AfsAppLib_GetInstance (void)
53 EXPORTED HINSTANCE
AfsAppLib_GetAppInstance (void)
56 g_hInstApp
= GetModuleHandle(NULL
);
61 EXPORTED
void AfsAppLib_SetAppInstance (HINSTANCE hInst
)
67 EXPORTED BOOLEAN _stdcall
DllEntryPoint (HANDLE hInst
, DWORD dwReason
, PVOID pReserved
)
71 case DLL_PROCESS_ATTACH
:
74 g_hInst
= (HINSTANCE
)hInst
;
75 TaLocale_LoadCorrespondingModule (g_hInst
);
76 SetErrorTranslationFunction ((LPERRORPROC
)AfsAppLib_TranslateErrorFunc
);
79 RegisterSpinnerClass();
80 RegisterElapsedClass();
83 RegisterSockAddrClass();
84 RegisterCheckListClass();
85 RegisterFastListClass();
94 * IMAGE LISTS ________________________________________________________________
98 HIMAGELIST
AfsAppLib_CreateImageList (BOOL fLarge
)
101 hil
= ImageList_Create ((fLarge
)?32:16, (fLarge
)?32:16, TRUE
, 4, 0);
103 AfsAppLib_AddToImageList (hil
, IDI_SERVER
, fLarge
);
104 AfsAppLib_AddToImageList (hil
, IDI_SERVER_ALERT
, fLarge
);
105 AfsAppLib_AddToImageList (hil
, IDI_SERVER_UNMON
, fLarge
);
106 AfsAppLib_AddToImageList (hil
, IDI_SERVICE
, fLarge
);
107 AfsAppLib_AddToImageList (hil
, IDI_SERVICE_ALERT
, fLarge
);
108 AfsAppLib_AddToImageList (hil
, IDI_SERVICE_STOPPED
, fLarge
);
109 AfsAppLib_AddToImageList (hil
, IDI_AGGREGATE
, fLarge
);
110 AfsAppLib_AddToImageList (hil
, IDI_AGGREGATE_ALERT
, fLarge
);
111 AfsAppLib_AddToImageList (hil
, IDI_FILESET
, fLarge
);
112 AfsAppLib_AddToImageList (hil
, IDI_FILESET_ALERT
, fLarge
);
113 AfsAppLib_AddToImageList (hil
, IDI_FILESET_LOCKED
, fLarge
);
114 AfsAppLib_AddToImageList (hil
, IDI_BOSSERVICE
, fLarge
);
115 AfsAppLib_AddToImageList (hil
, IDI_CELL
, fLarge
);
116 AfsAppLib_AddToImageList (hil
, IDI_SERVER_KEY
, fLarge
);
117 AfsAppLib_AddToImageList (hil
, IDI_USER
, fLarge
);
118 AfsAppLib_AddToImageList (hil
, IDI_GROUP
, fLarge
);
124 void AfsAppLib_AddToImageList (HIMAGELIST hil
, int idi
, BOOL fLarge
)
126 HICON hi
= TaLocale_LoadIcon (idi
);
127 ImageList_AddIcon (hil
, hi
);
132 * ANIMATION __________________________________________________________________
136 void AfsAppLib_AnimateIcon (HWND hIcon
, int *piFrameLast
)
139 static HICON hiFrame
[8];
140 static BOOL fLoaded
= FALSE
;
144 hiStop
= TaLocale_LoadIcon (IDI_SPINSTOP
);
145 hiFrame
[0] = TaLocale_LoadIcon (IDI_SPIN1
);
146 hiFrame
[1] = TaLocale_LoadIcon (IDI_SPIN2
);
147 hiFrame
[2] = TaLocale_LoadIcon (IDI_SPIN3
);
148 hiFrame
[3] = TaLocale_LoadIcon (IDI_SPIN4
);
149 hiFrame
[4] = TaLocale_LoadIcon (IDI_SPIN5
);
150 hiFrame
[5] = TaLocale_LoadIcon (IDI_SPIN6
);
151 hiFrame
[6] = TaLocale_LoadIcon (IDI_SPIN7
);
152 hiFrame
[7] = TaLocale_LoadIcon (IDI_SPIN8
);
158 *piFrameLast
= (*piFrameLast
== 7) ? 0 : (1+*piFrameLast
);
161 SendMessage (hIcon
, STM_SETICON
, (WPARAM
)((piFrameLast
) ? hiFrame
[ *piFrameLast
] : hiStop
), 0);
165 HRESULT CALLBACK
AnimationHook (HWND hIcon
, UINT msg
, WPARAM wp
, LPARAM lp
)
167 PVOID oldProc
= Subclass_FindNextHook (hIcon
, AnimationHook
);
173 iFrame
= (int)GetWindowData (hIcon
, GWD_ANIMATIONFRAME
);
174 AfsAppLib_AnimateIcon (hIcon
, &iFrame
);
175 SetWindowData (hIcon
, GWD_ANIMATIONFRAME
, iFrame
);
179 Subclass_RemoveHook (hIcon
, AnimationHook
);
184 return CallWindowProc ((WNDPROC
)oldProc
, hIcon
, msg
, wp
, lp
);
186 return DefWindowProc (hIcon
, msg
, wp
, lp
);
190 void AfsAppLib_StartAnimation (HWND hIcon
, int fps
)
192 Subclass_AddHook (hIcon
, AnimationHook
);
193 SetTimer (hIcon
, 0, 1000/((fps
) ? fps
: 8), NULL
);
194 AfsAppLib_AnimateIcon (hIcon
);
198 void AfsAppLib_StopAnimation (HWND hIcon
)
200 KillTimer (hIcon
, 0);
201 AfsAppLib_AnimateIcon (hIcon
);
202 Subclass_RemoveHook (hIcon
, AnimationHook
);
207 * ERROR TRANSLATION __________________________________________________________
211 HRESULT CALLBACK
AfsAppLib_TranslateErrorFunc (LPTSTR pszText
, ULONG code
, LANGID idLanguage
)
214 if ((idClient
= (DWORD
)AfsAppLib_GetAdminServerClientID()) != 0)
217 return asc_ErrorCodeTranslate (idClient
, code
, idLanguage
, pszText
, &status
);
220 if (OpenUtilLibrary())
222 const char *pszTextA
= NULL
;
224 if (util_AdminErrorCodeTranslate (code
, idLanguage
, &pszTextA
, &status
) && (pszTextA
))
226 CopyAnsiToString (pszText
, pszTextA
);
236 BOOL
AfsAppLib_TranslateError (LPTSTR pszText
, ULONG status
, LANGID idLanguage
)
238 LANGID idLangOld
= TaLocale_GetLanguage();
239 TaLocale_SetLanguage (idLanguage
);
241 BOOL rc
= FormatError (pszText
, TEXT("%s"), status
);
243 TaLocale_SetLanguage (idLangOld
);
249 * CELL LIST __________________________________________________________________
253 #define cREALLOC_CELLLIST 4
255 LPCELLLIST
AfsAppLib_GetCellList (HKEY hkBase
, LPTSTR pszRegPath
)
257 LPCELLLIST lpcl
= New (CELLLIST
);
258 memset (lpcl
, 0x00, sizeof(CELLLIST
));
260 if (hkBase
&& pszRegPath
)
262 lpcl
->hkBase
= hkBase
;
263 lstrcpy (lpcl
->szRegPath
, pszRegPath
);
266 if (RegOpenKey (hkBase
, pszRegPath
, &hk
) == 0)
268 TCHAR szCell
[ cchNAME
];
269 for (size_t ii
= 0; RegEnumKey (hk
, (DWORD
)ii
, szCell
, cchNAME
) == 0; ++ii
)
271 if (REALLOC (lpcl
->aCells
, lpcl
->nCells
, 1+ii
, cREALLOC_CELLLIST
))
273 lpcl
->aCells
[ ii
] = CloneString (szCell
);
279 else // Get cell list from AFS
284 TCHAR szDefCell
[ cchNAME
];
285 if (AfsAppLib_GetLocalCell (szDefCell
))
288 for (iclDef
= 0; iclDef
< lpcl
->nCells
; ++iclDef
)
290 if (lpcl
->aCells
[ iclDef
] == NULL
)
292 if (!lstrcmpi (lpcl
->aCells
[ iclDef
], szDefCell
))
295 if (iclDef
== lpcl
->nCells
) // default cell not currently in list?
297 for (iclDef
= 0; iclDef
< lpcl
->nCells
; ++iclDef
)
299 if (lpcl
->aCells
[ iclDef
] == NULL
)
302 if (REALLOC (lpcl
->aCells
, lpcl
->nCells
, 1+iclDef
, cREALLOC_CELLLIST
))
304 lpcl
->aCells
[iclDef
] = CloneString (szDefCell
);
307 if ((iclDef
> 0) && (iclDef
< lpcl
->nCells
) && (lpcl
->aCells
[ iclDef
]))
309 LPTSTR pszZero
= lpcl
->aCells
[0];
310 lpcl
->aCells
[0] = lpcl
->aCells
[iclDef
];
311 lpcl
->aCells
[iclDef
] = pszZero
;
315 for ( ; (lpcl
->nCells
!= 0); (lpcl
->nCells
)--)
317 if (lpcl
->aCells
[ lpcl
->nCells
-1 ] != NULL
)
325 LPCELLLIST
AfsAppLib_GetCellList (LPCELLLIST lpclCopy
)
327 LPCELLLIST lpcl
= New (CELLLIST
);
328 memset (lpcl
, 0x00, sizeof(CELLLIST
));
332 if (REALLOC (lpcl
->aCells
, lpcl
->nCells
, lpclCopy
->nCells
, cREALLOC_CELLLIST
))
334 for (size_t icl
= 0; icl
< lpcl
->nCells
; ++icl
)
336 if (lpclCopy
->aCells
[ icl
])
338 lpcl
->aCells
[ icl
] = CloneString (lpclCopy
->aCells
[ icl
]);
348 void AfsAppLib_AddToCellList (LPCELLLIST lpcl
, LPTSTR pszCell
)
350 if (lpcl
&& lpcl
->hkBase
&& lpcl
->szRegPath
[0])
352 TCHAR szPath
[ MAX_PATH
];
353 wsprintf (szPath
, TEXT("%s\\%s"), lpcl
->szRegPath
, pszCell
);
356 if (RegCreateKey (lpcl
->hkBase
, szPath
, &hk
) == 0)
364 void AfsAppLib_FreeCellList (LPCELLLIST lpcl
)
370 for (size_t icl
= 0; icl
< lpcl
->nCells
; ++icl
)
372 if (lpcl
->aCells
[icl
] != NULL
)
373 FreeString (lpcl
->aCells
[icl
]);
383 * TRULY MISCELLANEOUS ________________________________________________________
387 BOOL
AfsAppLib_IsTimeInFuture (LPSYSTEMTIME pstTest
)
390 GetSystemTime (&stNow
);
393 SystemTimeToFileTime (&stNow
, &ftNowGMT
);
396 SystemTimeToFileTime (pstTest
, &ftTest
);
398 if (CompareFileTime (&ftTest
, &ftNowGMT
) >= 0)
405 void AfsAppLib_UnixTimeToSystemTime (LPSYSTEMTIME pst
, ULONG ut
, BOOL fElapsed
)
407 // A Unix time is the number of seconds since 1/1/1970.
408 // The first step in this conversion is to change that count-of-seconds
409 // into a count-of-100ns-intervals...
412 ldw
.QuadPart
= (LONGLONG
)10000000 * (LONGLONG
)ut
;
414 // Then adjust the count to be a count-of-100ns-intervals since
415 // 1/1/1601, instead of 1/1/1970. That means adding a *big* number...
417 ldw
.QuadPart
+= (LONGLONG
)0x019db1ded53e8000;
419 // Now the count is effectively a FILETIME, which we can convert
420 // to a SYSTEMTIME with a Win32 API.
423 ft
.dwHighDateTime
= (DWORD
)ldw
.HighPart
;
424 ft
.dwLowDateTime
= (DWORD
)ldw
.LowPart
;
425 FileTimeToSystemTime (&ft
, pst
);
431 pst
->wDayOfWeek
-= 1;
437 void AfsAppLib_SplitCredentials (LPTSTR pszCreds
, LPTSTR pszCell
, LPTSTR pszID
)
439 LPTSTR pszSlash
= (LPTSTR
)lstrrchr (pszCreds
, TEXT('/'));
440 if (pszSlash
== NULL
)
443 AfsAppLib_GetLocalCell (pszCell
);
445 lstrcpy (pszID
, pszCreds
);
447 else // a cell was specified
450 lstrzcpy (pszCell
, pszCreds
, pszSlash
-pszCreds
);
452 lstrcpy (pszID
, 1+pszSlash
);
457 BOOL
AfsAppLib_GetLocalCell (LPTSTR pszCell
, ULONG
*pStatus
)
459 static TCHAR szCell
[ cchRESOURCE
] = TEXT("");
464 if (szCell
[0] == TEXT('\0'))
467 if ((idClient
= (DWORD
)AfsAppLib_GetAdminServerClientID()) != 0)
469 rc
= asc_LocalCellGet (idClient
, szCell
, &status
);
472 if (OpenClientLibrary())
474 char szCellNameA
[ MAX_PATH
];
475 if ((rc
= afsclient_LocalCellGet (szCellNameA
, (afs_status_p
)&status
)) == TRUE
)
477 CopyAnsiToString (szCell
, szCellNameA
);
479 CloseClientLibrary();
484 lstrcpy (pszCell
, szCell
);
491 BOOL
AfsAppLib_ReallocFunction (LPVOID
*ppTarget
, size_t cbElement
, size_t *pcTarget
, size_t cReq
, size_t cInc
)
496 if (cReq
<= *pcTarget
)
499 if ((cNew
= cInc
* ((cReq
+ cInc
-1) / cInc
)) <= 0)
502 if ((pNew
= (LPVOID
)Allocate (cbElement
* cNew
)) == NULL
)
504 memset (pNew
, 0x00, cbElement
* cNew
);
508 memcpy (pNew
, *ppTarget
, cbElement
* (*pcTarget
));
518 HFONT
AfsAppLib_CreateFont (int idsFont
)
520 TCHAR szFont
[ cchRESOURCE
];
521 GetString (szFont
, idsFont
);
523 HFONT hf
= (HFONT
)GetStockObject (DEFAULT_GUI_FONT
);
526 GetObject (hf
, sizeof(lf
), &lf
);
527 lf
.lfWeight
= FW_NORMAL
;
529 LPTSTR pszSection
= szFont
;
530 LPTSTR pszNextSection
;
531 if ((pszNextSection
= (LPTSTR
)lstrchr (pszSection
, TEXT(','))) != NULL
)
532 *pszNextSection
++ = TEXT('\0');
533 if (!pszSection
|| !*pszSection
)
536 lstrcpy (lf
.lfFaceName
, pszSection
);
538 pszSection
= pszNextSection
;
539 if ((pszNextSection
= (LPTSTR
)lstrchr (pszSection
, TEXT(','))) != NULL
)
540 *pszNextSection
++ = TEXT('\0');
541 if (!pszSection
|| !*pszSection
)
544 HDC hdc
= GetDC (NULL
);
545 lf
.lfHeight
= -MulDiv (_ttol(pszSection
), GetDeviceCaps (hdc
, LOGPIXELSY
), 72);
546 ReleaseDC (NULL
, hdc
);
548 pszSection
= pszNextSection
;
549 if ((pszNextSection
= (LPTSTR
)lstrchr (pszSection
, TEXT(','))) != NULL
)
550 *pszNextSection
++ = TEXT('\0');
551 if (pszSection
&& *pszSection
)
553 if (lstrchr (pszSection
, TEXT('b')) || lstrchr (pszSection
, TEXT('B')))
554 lf
.lfWeight
= FW_BOLD
;
555 if (lstrchr (pszSection
, TEXT('i')) || lstrchr (pszSection
, TEXT('I')))
557 if (lstrchr (pszSection
, TEXT('u')) || lstrchr (pszSection
, TEXT('U')))
558 lf
.lfUnderline
= TRUE
;
561 return CreateFontIndirect(&lf
);