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>
20 #include <WINNT/afsapplib.h>
21 #include "al_dynlink.h"
22 #include <WINNT/TaAfsAdmSvrClient.h>
26 * DEFINITIONS ________________________________________________________________
30 #define GWD_ANIMATIONFRAME TEXT("afsapplib/al_misc.cpp - next animation frame")
34 * PROTOTYPES _________________________________________________________________
38 HRESULT CALLBACK AfsAppLib_TranslateErrorFunc (LPTSTR pszText, ULONG code, LANGID idLanguage);
42 * STARTUP ____________________________________________________________________
46 static HINSTANCE g_hInst = NULL;
47 static HINSTANCE g_hInstApp = NULL;
49 HINSTANCE AfsAppLib_GetInstance (void)
55 EXPORTED HINSTANCE AfsAppLib_GetAppInstance (void)
58 g_hInstApp = GetModuleHandle(NULL);
63 EXPORTED void AfsAppLib_SetAppInstance (HINSTANCE hInst)
69 EXPORTED BOOLEAN _stdcall DllEntryPoint (HANDLE hInst, DWORD dwReason, PVOID pReserved)
73 case DLL_PROCESS_ATTACH:
76 g_hInst = (HINSTANCE)hInst;
77 TaLocale_LoadCorrespondingModule (g_hInst);
78 SetErrorTranslationFunction ((LPERRORPROC)AfsAppLib_TranslateErrorFunc);
81 RegisterSpinnerClass();
82 RegisterElapsedClass();
85 RegisterSockAddrClass();
86 RegisterCheckListClass();
87 RegisterFastListClass();
96 * IMAGE LISTS ________________________________________________________________
100 HIMAGELIST AfsAppLib_CreateImageList (BOOL fLarge)
103 hil = ImageList_Create ((fLarge)?32:16, (fLarge)?32:16, TRUE, 4, 0);
105 AfsAppLib_AddToImageList (hil, IDI_SERVER, fLarge);
106 AfsAppLib_AddToImageList (hil, IDI_SERVER_ALERT, fLarge);
107 AfsAppLib_AddToImageList (hil, IDI_SERVER_UNMON, fLarge);
108 AfsAppLib_AddToImageList (hil, IDI_SERVICE, fLarge);
109 AfsAppLib_AddToImageList (hil, IDI_SERVICE_ALERT, fLarge);
110 AfsAppLib_AddToImageList (hil, IDI_SERVICE_STOPPED, fLarge);
111 AfsAppLib_AddToImageList (hil, IDI_AGGREGATE, fLarge);
112 AfsAppLib_AddToImageList (hil, IDI_AGGREGATE_ALERT, fLarge);
113 AfsAppLib_AddToImageList (hil, IDI_FILESET, fLarge);
114 AfsAppLib_AddToImageList (hil, IDI_FILESET_ALERT, fLarge);
115 AfsAppLib_AddToImageList (hil, IDI_FILESET_LOCKED, fLarge);
116 AfsAppLib_AddToImageList (hil, IDI_BOSSERVICE, fLarge);
117 AfsAppLib_AddToImageList (hil, IDI_CELL, fLarge);
118 AfsAppLib_AddToImageList (hil, IDI_SERVER_KEY, fLarge);
119 AfsAppLib_AddToImageList (hil, IDI_USER, fLarge);
120 AfsAppLib_AddToImageList (hil, IDI_GROUP, fLarge);
126 void AfsAppLib_AddToImageList (HIMAGELIST hil, int idi, BOOL fLarge)
128 HICON hi = TaLocale_LoadIcon (idi);
129 ImageList_AddIcon (hil, hi);
134 * ANIMATION __________________________________________________________________
138 void AfsAppLib_AnimateIcon (HWND hIcon, int *piFrameLast)
141 static HICON hiFrame[8];
142 static BOOL fLoaded = FALSE;
146 hiStop = TaLocale_LoadIcon (IDI_SPINSTOP);
147 hiFrame[0] = TaLocale_LoadIcon (IDI_SPIN1);
148 hiFrame[1] = TaLocale_LoadIcon (IDI_SPIN2);
149 hiFrame[2] = TaLocale_LoadIcon (IDI_SPIN3);
150 hiFrame[3] = TaLocale_LoadIcon (IDI_SPIN4);
151 hiFrame[4] = TaLocale_LoadIcon (IDI_SPIN5);
152 hiFrame[5] = TaLocale_LoadIcon (IDI_SPIN6);
153 hiFrame[6] = TaLocale_LoadIcon (IDI_SPIN7);
154 hiFrame[7] = TaLocale_LoadIcon (IDI_SPIN8);
160 *piFrameLast = (*piFrameLast == 7) ? 0 : (1+*piFrameLast);
163 SendMessage (hIcon, STM_SETICON, (WPARAM)((piFrameLast) ? hiFrame[ *piFrameLast ] : hiStop), 0);
167 HRESULT CALLBACK AnimationHook (HWND hIcon, UINT msg, WPARAM wp, LPARAM lp)
169 PVOID oldProc = Subclass_FindNextHook (hIcon, AnimationHook);
175 iFrame = (int)GetWindowData (hIcon, GWD_ANIMATIONFRAME);
176 AfsAppLib_AnimateIcon (hIcon, &iFrame);
177 SetWindowData (hIcon, GWD_ANIMATIONFRAME, iFrame);
181 Subclass_RemoveHook (hIcon, AnimationHook);
186 return CallWindowProc ((WNDPROC)oldProc, hIcon, msg, wp, lp);
188 return DefWindowProc (hIcon, msg, wp, lp);
192 void AfsAppLib_StartAnimation (HWND hIcon, int fps)
194 Subclass_AddHook (hIcon, AnimationHook);
195 SetTimer (hIcon, 0, 1000/((fps) ? fps : 8), NULL);
196 AfsAppLib_AnimateIcon (hIcon);
200 void AfsAppLib_StopAnimation (HWND hIcon)
202 KillTimer (hIcon, 0);
203 AfsAppLib_AnimateIcon (hIcon);
204 Subclass_RemoveHook (hIcon, AnimationHook);
209 * ERROR TRANSLATION __________________________________________________________
213 HRESULT CALLBACK AfsAppLib_TranslateErrorFunc (LPTSTR pszText, ULONG code, LANGID idLanguage)
216 if ((idClient = (DWORD)AfsAppLib_GetAdminServerClientID()) != 0)
219 return asc_ErrorCodeTranslate (idClient, code, idLanguage, pszText, &status);
222 if (OpenUtilLibrary())
224 const char *pszTextA = NULL;
226 if (util_AdminErrorCodeTranslate (code, idLanguage, &pszTextA, &status) && (pszTextA))
228 CopyAnsiToString (pszText, pszTextA);
238 BOOL AfsAppLib_TranslateError (LPTSTR pszText, ULONG status, LANGID idLanguage)
240 LANGID idLangOld = TaLocale_GetLanguage();
241 TaLocale_SetLanguage (idLanguage);
243 BOOL rc = FormatError (pszText, TEXT("%s"), status);
245 TaLocale_SetLanguage (idLangOld);
251 * CELL LIST __________________________________________________________________
255 #define cREALLOC_CELLLIST 4
257 LPCELLLIST AfsAppLib_GetCellList (HKEY hkBase, LPTSTR pszRegPath)
259 LPCELLLIST lpcl = New (CELLLIST);
260 memset (lpcl, 0x00, sizeof(CELLLIST));
262 if (hkBase && pszRegPath)
264 lpcl->hkBase = hkBase;
265 lstrcpy (lpcl->szRegPath, pszRegPath);
268 if (RegOpenKey (hkBase, pszRegPath, &hk) == 0)
270 TCHAR szCell[ cchNAME ];
271 for (size_t ii = 0; RegEnumKey (hk, (DWORD)ii, szCell, cchNAME) == 0; ++ii)
273 if (REALLOC (lpcl->aCells, lpcl->nCells, 1+ii, cREALLOC_CELLLIST))
275 lpcl->aCells[ ii ] = CloneString (szCell);
281 else // Get cell list from AFS
286 TCHAR szDefCell[ cchNAME ];
287 if (AfsAppLib_GetLocalCell (szDefCell))
290 for (iclDef = 0; iclDef < lpcl->nCells; ++iclDef)
292 if (lpcl->aCells[ iclDef ] == NULL)
294 if (!lstrcmpi (lpcl->aCells[ iclDef ], szDefCell))
297 if (iclDef == lpcl->nCells) // default cell not currently in list?
299 for (iclDef = 0; iclDef < lpcl->nCells; ++iclDef)
301 if (lpcl->aCells[ iclDef ] == NULL)
304 if (REALLOC (lpcl->aCells, lpcl->nCells, 1+iclDef, cREALLOC_CELLLIST))
306 lpcl->aCells[iclDef] = CloneString (szDefCell);
309 if ((iclDef > 0) && (iclDef < lpcl->nCells) && (lpcl->aCells[ iclDef ]))
311 LPTSTR pszZero = lpcl->aCells[0];
312 lpcl->aCells[0] = lpcl->aCells[iclDef];
313 lpcl->aCells[iclDef] = pszZero;
317 for ( ; (lpcl->nCells != 0); (lpcl->nCells)--)
319 if (lpcl->aCells[ lpcl->nCells-1 ] != NULL)
327 LPCELLLIST AfsAppLib_GetCellList (LPCELLLIST lpclCopy)
329 LPCELLLIST lpcl = New (CELLLIST);
330 memset (lpcl, 0x00, sizeof(CELLLIST));
334 if (REALLOC (lpcl->aCells, lpcl->nCells, lpclCopy->nCells, cREALLOC_CELLLIST))
336 for (size_t icl = 0; icl < lpcl->nCells; ++icl)
338 if (lpclCopy->aCells[ icl ])
340 lpcl->aCells[ icl ] = CloneString (lpclCopy->aCells[ icl ]);
350 void AfsAppLib_AddToCellList (LPCELLLIST lpcl, LPTSTR pszCell)
352 if (lpcl && lpcl->hkBase && lpcl->szRegPath[0])
354 TCHAR szPath[ MAX_PATH ];
355 wsprintf (szPath, TEXT("%s\\%s"), lpcl->szRegPath, pszCell);
358 if (RegCreateKey (lpcl->hkBase, szPath, &hk) == 0)
366 void AfsAppLib_FreeCellList (LPCELLLIST lpcl)
372 for (size_t icl = 0; icl < lpcl->nCells; ++icl)
374 if (lpcl->aCells[icl] != NULL)
375 FreeString (lpcl->aCells[icl]);
385 * TRULY MISCELLANEOUS ________________________________________________________
389 BOOL AfsAppLib_IsTimeInFuture (LPSYSTEMTIME pstTest)
392 GetSystemTime (&stNow);
395 SystemTimeToFileTime (&stNow, &ftNowGMT);
398 SystemTimeToFileTime (pstTest, &ftTest);
400 if (CompareFileTime (&ftTest, &ftNowGMT) >= 0)
407 void AfsAppLib_UnixTimeToSystemTime (LPSYSTEMTIME pst, ULONG ut, BOOL fElapsed)
409 // A Unix time is the number of seconds since 1/1/1970.
410 // The first step in this conversion is to change that count-of-seconds
411 // into a count-of-100ns-intervals...
414 ldw.QuadPart = (LONGLONG)10000000 * (LONGLONG)ut;
416 // Then adjust the count to be a count-of-100ns-intervals since
417 // 1/1/1601, instead of 1/1/1970. That means adding a *big* number...
419 ldw.QuadPart += (LONGLONG)0x019db1ded53e8000;
421 // Now the count is effectively a FILETIME, which we can convert
422 // to a SYSTEMTIME with a Win32 API.
425 ft.dwHighDateTime = (DWORD)ldw.HighPart;
426 ft.dwLowDateTime = (DWORD)ldw.LowPart;
427 FileTimeToSystemTime (&ft, pst);
433 pst->wDayOfWeek -= 1;
439 void AfsAppLib_SplitCredentials (LPTSTR pszCreds, LPTSTR pszCell, LPTSTR pszID)
441 LPTSTR pszSlash = (LPTSTR)lstrrchr (pszCreds, TEXT('/'));
442 if (pszSlash == NULL)
445 AfsAppLib_GetLocalCell (pszCell);
447 lstrcpy (pszID, pszCreds);
449 else // a cell was specified
452 lstrzcpy (pszCell, pszCreds, pszSlash -pszCreds);
454 lstrcpy (pszID, 1+pszSlash);
459 BOOL AfsAppLib_GetLocalCell (LPTSTR pszCell, ULONG *pStatus)
461 static TCHAR szCell[ cchRESOURCE ] = TEXT("");
466 if (szCell[0] == TEXT('\0'))
469 if ((idClient = (DWORD)AfsAppLib_GetAdminServerClientID()) != 0)
471 rc = asc_LocalCellGet (idClient, szCell, &status);
474 if (OpenClientLibrary())
476 char szCellNameA[ MAX_PATH ];
477 if ((rc = afsclient_LocalCellGet (szCellNameA, (afs_status_p)&status)) == TRUE)
479 CopyAnsiToString (szCell, szCellNameA);
481 CloseClientLibrary();
486 lstrcpy (pszCell, szCell);
493 BOOL AfsAppLib_ReallocFunction (LPVOID *ppTarget, size_t cbElement, size_t *pcTarget, size_t cReq, size_t cInc)
498 if (cReq <= *pcTarget)
501 if ((cNew = cInc * ((cReq + cInc-1) / cInc)) <= 0)
504 if ((pNew = (LPVOID)Allocate (cbElement * cNew)) == NULL)
506 memset (pNew, 0x00, cbElement * cNew);
510 memcpy (pNew, *ppTarget, cbElement * (*pcTarget));
520 HFONT AfsAppLib_CreateFont (int idsFont)
522 TCHAR szFont[ cchRESOURCE ];
523 GetString (szFont, idsFont);
525 HFONT hf = (HFONT)GetStockObject (DEFAULT_GUI_FONT);
528 GetObject (hf, sizeof(lf), &lf);
529 lf.lfWeight = FW_NORMAL;
531 LPTSTR pszSection = szFont;
532 LPTSTR pszNextSection;
533 if ((pszNextSection = (LPTSTR)lstrchr (pszSection, TEXT(','))) != NULL)
534 *pszNextSection++ = TEXT('\0');
535 if (!pszSection || !*pszSection)
538 lstrcpy (lf.lfFaceName, pszSection);
540 pszSection = pszNextSection;
541 if ((pszNextSection = (LPTSTR)lstrchr (pszSection, TEXT(','))) != NULL)
542 *pszNextSection++ = TEXT('\0');
543 if (!pszSection || !*pszSection)
546 HDC hdc = GetDC (NULL);
547 lf.lfHeight = -MulDiv (_ttol(pszSection), GetDeviceCaps (hdc, LOGPIXELSY), 72);
548 ReleaseDC (NULL, hdc);
550 pszSection = pszNextSection;
551 if ((pszNextSection = (LPTSTR)lstrchr (pszSection, TEXT(','))) != NULL)
552 *pszNextSection++ = TEXT('\0');
553 if (pszSection && *pszSection)
555 if (lstrchr (pszSection, TEXT('b')) || lstrchr (pszSection, TEXT('B')))
556 lf.lfWeight = FW_BOLD;
557 if (lstrchr (pszSection, TEXT('i')) || lstrchr (pszSection, TEXT('I')))
559 if (lstrchr (pszSection, TEXT('u')) || lstrchr (pszSection, TEXT('U')))
560 lf.lfUnderline = TRUE;
563 return CreateFontIndirect(&lf);