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
11 #include <afs/param.h>
15 #include <WINNT/afsapplib.h>
16 #include "al_dynlink.h"
17 #include <WINNT/TaAfsAdmSvrClient.h>
21 * DEFINITIONS ________________________________________________________________
25 #define GWD_ANIMATIONFRAME TEXT("afsapplib/al_misc.cpp - next animation frame")
29 * PROTOTYPES _________________________________________________________________
33 BOOL CALLBACK AfsAppLib_TranslateErrorFunc (LPTSTR pszText, ULONG code, LANGID idLanguage);
37 * STARTUP ____________________________________________________________________
41 static HINSTANCE g_hInst = NULL;
42 static HINSTANCE g_hInstApp = NULL;
44 HINSTANCE AfsAppLib_GetInstance (void)
50 EXPORTED HINSTANCE AfsAppLib_GetAppInstance (void)
53 g_hInstApp = GetModuleHandle(NULL);
58 EXPORTED void AfsAppLib_SetAppInstance (HINSTANCE hInst)
64 extern "C" BOOLEAN _stdcall DllEntryPoint (HANDLE hInst, DWORD dwReason, PVOID pReserved)
68 case DLL_PROCESS_ATTACH:
71 g_hInst = (HINSTANCE)hInst;
72 TaLocale_LoadCorrespondingModule (g_hInst);
73 SetErrorTranslationFunction ((LPERRORPROC)AfsAppLib_TranslateErrorFunc);
76 RegisterSpinnerClass();
77 RegisterElapsedClass();
80 RegisterSockAddrClass();
81 RegisterCheckListClass();
82 RegisterFastListClass();
91 * IMAGE LISTS ________________________________________________________________
95 HIMAGELIST AfsAppLib_CreateImageList (BOOL fLarge)
98 hil = ImageList_Create ((fLarge)?32:16, (fLarge)?32:16, TRUE, 4, 0);
100 AfsAppLib_AddToImageList (hil, IDI_SERVER, fLarge);
101 AfsAppLib_AddToImageList (hil, IDI_SERVER_ALERT, fLarge);
102 AfsAppLib_AddToImageList (hil, IDI_SERVER_UNMON, fLarge);
103 AfsAppLib_AddToImageList (hil, IDI_SERVICE, fLarge);
104 AfsAppLib_AddToImageList (hil, IDI_SERVICE_ALERT, fLarge);
105 AfsAppLib_AddToImageList (hil, IDI_SERVICE_STOPPED, fLarge);
106 AfsAppLib_AddToImageList (hil, IDI_AGGREGATE, fLarge);
107 AfsAppLib_AddToImageList (hil, IDI_AGGREGATE_ALERT, fLarge);
108 AfsAppLib_AddToImageList (hil, IDI_FILESET, fLarge);
109 AfsAppLib_AddToImageList (hil, IDI_FILESET_ALERT, fLarge);
110 AfsAppLib_AddToImageList (hil, IDI_FILESET_LOCKED, fLarge);
111 AfsAppLib_AddToImageList (hil, IDI_BOSSERVICE, fLarge);
112 AfsAppLib_AddToImageList (hil, IDI_CELL, fLarge);
113 AfsAppLib_AddToImageList (hil, IDI_SERVER_KEY, fLarge);
114 AfsAppLib_AddToImageList (hil, IDI_USER, fLarge);
115 AfsAppLib_AddToImageList (hil, IDI_GROUP, fLarge);
121 void AfsAppLib_AddToImageList (HIMAGELIST hil, int idi, BOOL fLarge)
123 HICON hi = TaLocale_LoadIcon (idi);
124 ImageList_AddIcon (hil, hi);
129 * ANIMATION __________________________________________________________________
133 void AfsAppLib_AnimateIcon (HWND hIcon, int *piFrameLast)
136 static HICON hiFrame[8];
137 static BOOL fLoaded = FALSE;
141 hiStop = TaLocale_LoadIcon (IDI_SPINSTOP);
142 hiFrame[0] = TaLocale_LoadIcon (IDI_SPIN1);
143 hiFrame[1] = TaLocale_LoadIcon (IDI_SPIN2);
144 hiFrame[2] = TaLocale_LoadIcon (IDI_SPIN3);
145 hiFrame[3] = TaLocale_LoadIcon (IDI_SPIN4);
146 hiFrame[4] = TaLocale_LoadIcon (IDI_SPIN5);
147 hiFrame[5] = TaLocale_LoadIcon (IDI_SPIN6);
148 hiFrame[6] = TaLocale_LoadIcon (IDI_SPIN7);
149 hiFrame[7] = TaLocale_LoadIcon (IDI_SPIN8);
155 *piFrameLast = (*piFrameLast == 7) ? 0 : (1+*piFrameLast);
158 SendMessage (hIcon, STM_SETICON, (WPARAM)((piFrameLast) ? hiFrame[ *piFrameLast ] : hiStop), 0);
162 BOOL CALLBACK AnimationHook (HWND hIcon, UINT msg, WPARAM wp, LPARAM lp)
164 PVOID oldProc = Subclass_FindNextHook (hIcon, AnimationHook);
170 iFrame = GetWindowData (hIcon, GWD_ANIMATIONFRAME);
171 AfsAppLib_AnimateIcon (hIcon, &iFrame);
172 SetWindowData (hIcon, GWD_ANIMATIONFRAME, iFrame);
176 Subclass_RemoveHook (hIcon, AnimationHook);
181 return CallWindowProc ((WNDPROC)oldProc, hIcon, msg, wp, lp);
183 return DefWindowProc (hIcon, msg, wp, lp);
187 void AfsAppLib_StartAnimation (HWND hIcon, int fps)
189 Subclass_AddHook (hIcon, AnimationHook);
190 SetTimer (hIcon, 0, 1000/((fps) ? fps : 8), NULL);
191 AfsAppLib_AnimateIcon (hIcon);
195 void AfsAppLib_StopAnimation (HWND hIcon)
197 KillTimer (hIcon, 0);
198 AfsAppLib_AnimateIcon (hIcon);
199 Subclass_RemoveHook (hIcon, AnimationHook);
204 * ERROR TRANSLATION __________________________________________________________
208 BOOL CALLBACK AfsAppLib_TranslateErrorFunc (LPTSTR pszText, ULONG code, LANGID idLanguage)
211 if ((idClient = AfsAppLib_GetAdminServerClientID()) != 0)
214 return asc_ErrorCodeTranslate (idClient, code, idLanguage, pszText, &status);
217 if (OpenUtilLibrary())
219 const char *pszTextA = NULL;
221 if (util_AdminErrorCodeTranslate (code, idLanguage, &pszTextA, &status) && (pszTextA))
223 CopyAnsiToString (pszText, pszTextA);
233 BOOL AfsAppLib_TranslateError (LPTSTR pszText, ULONG status, LANGID idLanguage)
235 LANGID idLangOld = TaLocale_GetLanguage();
236 TaLocale_SetLanguage (idLanguage);
238 BOOL rc = FormatError (pszText, TEXT("%s"), status);
240 TaLocale_SetLanguage (idLangOld);
246 * CELL LIST __________________________________________________________________
250 #define cREALLOC_CELLLIST 4
252 LPCELLLIST AfsAppLib_GetCellList (HKEY hkBase, LPTSTR pszRegPath)
254 LPCELLLIST lpcl = New (CELLLIST);
255 memset (lpcl, 0x00, sizeof(CELLLIST));
257 if (hkBase && pszRegPath)
259 lpcl->hkBase = hkBase;
260 lstrcpy (lpcl->szRegPath, pszRegPath);
263 if (RegOpenKey (hkBase, pszRegPath, &hk) == 0)
265 TCHAR szCell[ cchNAME ];
266 for (size_t ii = 0; RegEnumKey (hk, ii, szCell, cchNAME) == 0; ++ii)
268 if (REALLOC (lpcl->aCells, lpcl->nCells, 1+ii, cREALLOC_CELLLIST))
270 lpcl->aCells[ ii ] = CloneString (szCell);
276 else // Get cell list from AFS
281 TCHAR szDefCell[ cchNAME ];
282 if (AfsAppLib_GetLocalCell (szDefCell))
284 for (size_t iclDef = 0; iclDef < lpcl->nCells; ++iclDef)
286 if (lpcl->aCells[ iclDef ] == NULL)
288 if (!lstrcmpi (lpcl->aCells[ iclDef ], szDefCell))
291 if (iclDef == lpcl->nCells) // default cell not currently in list?
293 for (iclDef = 0; iclDef < lpcl->nCells; ++iclDef)
295 if (lpcl->aCells[ iclDef ] == NULL)
298 if (REALLOC (lpcl->aCells, lpcl->nCells, 1+iclDef, cREALLOC_CELLLIST))
300 lpcl->aCells[iclDef] = CloneString (szDefCell);
303 if ((iclDef > 0) && (iclDef < lpcl->nCells) && (lpcl->aCells[ iclDef ]))
305 LPTSTR pszZero = lpcl->aCells[0];
306 lpcl->aCells[0] = lpcl->aCells[iclDef];
307 lpcl->aCells[iclDef] = pszZero;
311 for ( ; (lpcl->nCells != 0); (lpcl->nCells)--)
313 if (lpcl->aCells[ lpcl->nCells-1 ] != NULL)
321 LPCELLLIST AfsAppLib_GetCellList (LPCELLLIST lpclCopy)
323 LPCELLLIST lpcl = New (CELLLIST);
324 memset (lpcl, 0x00, sizeof(CELLLIST));
328 if (REALLOC (lpcl->aCells, lpcl->nCells, lpclCopy->nCells, cREALLOC_CELLLIST))
330 for (size_t icl = 0; icl < lpcl->nCells; ++icl)
332 if (lpclCopy->aCells[ icl ])
334 lpcl->aCells[ icl ] = CloneString (lpclCopy->aCells[ icl ]);
344 void AfsAppLib_AddToCellList (LPCELLLIST lpcl, LPTSTR pszCell)
346 if (lpcl && lpcl->hkBase && lpcl->szRegPath[0])
348 TCHAR szPath[ MAX_PATH ];
349 wsprintf (szPath, TEXT("%s\\%s"), lpcl->szRegPath, pszCell);
352 if (RegCreateKey (lpcl->hkBase, szPath, &hk) == 0)
360 void AfsAppLib_FreeCellList (LPCELLLIST lpcl)
366 for (size_t icl = 0; icl < lpcl->nCells; ++icl)
368 if (lpcl->aCells[icl] != NULL)
369 FreeString (lpcl->aCells[icl]);
379 * TRULY MISCELLANEOUS ________________________________________________________
383 BOOL AfsAppLib_IsTimeInFuture (LPSYSTEMTIME pstTest)
386 GetSystemTime (&stNow);
389 SystemTimeToFileTime (&stNow, &ftNowGMT);
392 SystemTimeToFileTime (pstTest, &ftTest);
394 if (CompareFileTime (&ftTest, &ftNowGMT) >= 0)
401 void AfsAppLib_UnixTimeToSystemTime (LPSYSTEMTIME pst, ULONG ut, BOOL fElapsed)
403 // A Unix time is the number of seconds since 1/1/1970.
404 // The first step in this conversion is to change that count-of-seconds
405 // into a count-of-100ns-intervals...
408 ldw.QuadPart = (LONGLONG)10000000 * (LONGLONG)ut;
410 // Then adjust the count to be a count-of-100ns-intervals since
411 // 1/1/1601, instead of 1/1/1970. That means adding a *big* number...
413 ldw.QuadPart += (LONGLONG)0x019db1ded53e8000;
415 // Now the count is effectively a FILETIME, which we can convert
416 // to a SYSTEMTIME with a Win32 API.
419 ft.dwHighDateTime = (DWORD)ldw.HighPart;
420 ft.dwLowDateTime = (DWORD)ldw.LowPart;
421 FileTimeToSystemTime (&ft, pst);
427 pst->wDayOfWeek -= 1;
433 void AfsAppLib_SplitCredentials (LPTSTR pszCreds, LPTSTR pszCell, LPTSTR pszID)
435 LPTSTR pszSlash = (LPTSTR)lstrrchr (pszCreds, TEXT('/'));
436 if (pszSlash == NULL)
439 AfsAppLib_GetLocalCell (pszCell);
441 lstrcpy (pszID, pszCreds);
443 else // a cell was specified
446 lstrzcpy (pszCell, pszCreds, pszSlash -pszCreds);
448 lstrcpy (pszID, 1+pszSlash);
453 BOOL AfsAppLib_GetLocalCell (LPTSTR pszCell, ULONG *pStatus)
455 static TCHAR szCell[ cchRESOURCE ] = TEXT("");
460 if (szCell[0] == TEXT('\0'))
463 if ((idClient = AfsAppLib_GetAdminServerClientID()) != 0)
465 rc = asc_LocalCellGet (idClient, szCell, &status);
468 if (OpenClientLibrary())
470 char szCellNameA[ MAX_PATH ];
471 if ((rc = afsclient_LocalCellGet (szCellNameA, (afs_status_p)&status)) == TRUE)
473 CopyAnsiToString (szCell, szCellNameA);
475 CloseClientLibrary();
480 lstrcpy (pszCell, szCell);
487 BOOL AfsAppLib_ReallocFunction (LPVOID *ppTarget, size_t cbElement, size_t *pcTarget, size_t cReq, size_t cInc)
492 if (cReq <= *pcTarget)
495 if ((cNew = cInc * ((cReq + cInc-1) / cInc)) <= 0)
498 if ((pNew = (LPVOID)Allocate (cbElement * cNew)) == NULL)
500 memset (pNew, 0x00, cbElement * cNew);
504 memcpy (pNew, *ppTarget, cbElement * (*pcTarget));
514 HFONT AfsAppLib_CreateFont (int idsFont)
516 TCHAR szFont[ cchRESOURCE ];
517 GetString (szFont, idsFont);
519 HFONT hf = (HFONT)GetStockObject (DEFAULT_GUI_FONT);
522 GetObject (hf, sizeof(lf), &lf);
523 lf.lfWeight = FW_NORMAL;
525 LPTSTR pszSection = szFont;
526 LPTSTR pszNextSection;
527 if ((pszNextSection = (LPTSTR)lstrchr (pszSection, TEXT(','))) != NULL)
528 *pszNextSection++ = TEXT('\0');
529 if (!pszSection || !*pszSection)
532 lstrcpy (lf.lfFaceName, pszSection);
534 pszSection = pszNextSection;
535 if ((pszNextSection = (LPTSTR)lstrchr (pszSection, TEXT(','))) != NULL)
536 *pszNextSection++ = TEXT('\0');
537 if (!pszSection || !*pszSection)
540 HDC hdc = GetDC (NULL);
541 lf.lfHeight = -MulDiv (_ttol(pszSection), GetDeviceCaps (hdc, LOGPIXELSY), 72);
542 ReleaseDC (NULL, hdc);
544 pszSection = pszNextSection;
545 if ((pszNextSection = (LPTSTR)lstrchr (pszSection, TEXT(','))) != NULL)
546 *pszNextSection++ = TEXT('\0');
547 if (pszSection && *pszSection)
549 if (lstrchr (pszSection, TEXT('b')) || lstrchr (pszSection, TEXT('B')))
550 lf.lfWeight = FW_BOLD;
551 if (lstrchr (pszSection, TEXT('i')) || lstrchr (pszSection, TEXT('I')))
553 if (lstrchr (pszSection, TEXT('u')) || lstrchr (pszSection, TEXT('U')))
554 lf.lfUnderline = TRUE;
557 return CreateFontIndirect(&lf);