6 #include <WINNT/afsapplib.h>
7 #include "al_dynlink.h"
8 #include <WINNT/TaAfsAdmSvrClient.h>
12 * DEFINITIONS ________________________________________________________________
16 #define GWD_ANIMATIONFRAME TEXT("afsapplib/al_misc.cpp - next animation frame")
20 * PROTOTYPES _________________________________________________________________
24 BOOL CALLBACK AfsAppLib_TranslateErrorFunc (LPTSTR pszText, ULONG code, LANGID idLanguage);
28 * STARTUP ____________________________________________________________________
32 static HINSTANCE g_hInst = NULL;
33 static HINSTANCE g_hInstApp = NULL;
35 HINSTANCE AfsAppLib_GetInstance (void)
41 EXPORTED HINSTANCE AfsAppLib_GetAppInstance (void)
44 g_hInstApp = GetModuleHandle(NULL);
49 EXPORTED void AfsAppLib_SetAppInstance (HINSTANCE hInst)
55 extern "C" BOOLEAN _stdcall DllEntryPoint (HANDLE hInst, DWORD dwReason, PVOID pReserved)
59 case DLL_PROCESS_ATTACH:
62 g_hInst = (HINSTANCE)hInst;
63 TaLocale_LoadCorrespondingModule (g_hInst);
64 SetErrorTranslationFunction ((LPERRORPROC)AfsAppLib_TranslateErrorFunc);
67 RegisterSpinnerClass();
68 RegisterElapsedClass();
71 RegisterSockAddrClass();
72 RegisterCheckListClass();
73 RegisterFastListClass();
82 * IMAGE LISTS ________________________________________________________________
86 HIMAGELIST AfsAppLib_CreateImageList (BOOL fLarge)
89 hil = ImageList_Create ((fLarge)?32:16, (fLarge)?32:16, TRUE, 4, 0);
91 AfsAppLib_AddToImageList (hil, IDI_SERVER, fLarge);
92 AfsAppLib_AddToImageList (hil, IDI_SERVER_ALERT, fLarge);
93 AfsAppLib_AddToImageList (hil, IDI_SERVER_UNMON, fLarge);
94 AfsAppLib_AddToImageList (hil, IDI_SERVICE, fLarge);
95 AfsAppLib_AddToImageList (hil, IDI_SERVICE_ALERT, fLarge);
96 AfsAppLib_AddToImageList (hil, IDI_SERVICE_STOPPED, fLarge);
97 AfsAppLib_AddToImageList (hil, IDI_AGGREGATE, fLarge);
98 AfsAppLib_AddToImageList (hil, IDI_AGGREGATE_ALERT, fLarge);
99 AfsAppLib_AddToImageList (hil, IDI_FILESET, fLarge);
100 AfsAppLib_AddToImageList (hil, IDI_FILESET_ALERT, fLarge);
101 AfsAppLib_AddToImageList (hil, IDI_FILESET_LOCKED, fLarge);
102 AfsAppLib_AddToImageList (hil, IDI_BOSSERVICE, fLarge);
103 AfsAppLib_AddToImageList (hil, IDI_CELL, fLarge);
104 AfsAppLib_AddToImageList (hil, IDI_SERVER_KEY, fLarge);
105 AfsAppLib_AddToImageList (hil, IDI_USER, fLarge);
106 AfsAppLib_AddToImageList (hil, IDI_GROUP, fLarge);
112 void AfsAppLib_AddToImageList (HIMAGELIST hil, int idi, BOOL fLarge)
114 HICON hi = TaLocale_LoadIcon (idi);
115 ImageList_AddIcon (hil, hi);
120 * ANIMATION __________________________________________________________________
124 void AfsAppLib_AnimateIcon (HWND hIcon, int *piFrameLast)
127 static HICON hiFrame[8];
128 static BOOL fLoaded = FALSE;
132 hiStop = TaLocale_LoadIcon (IDI_SPINSTOP);
133 hiFrame[0] = TaLocale_LoadIcon (IDI_SPIN1);
134 hiFrame[1] = TaLocale_LoadIcon (IDI_SPIN2);
135 hiFrame[2] = TaLocale_LoadIcon (IDI_SPIN3);
136 hiFrame[3] = TaLocale_LoadIcon (IDI_SPIN4);
137 hiFrame[4] = TaLocale_LoadIcon (IDI_SPIN5);
138 hiFrame[5] = TaLocale_LoadIcon (IDI_SPIN6);
139 hiFrame[6] = TaLocale_LoadIcon (IDI_SPIN7);
140 hiFrame[7] = TaLocale_LoadIcon (IDI_SPIN8);
146 *piFrameLast = (*piFrameLast == 7) ? 0 : (1+*piFrameLast);
149 SendMessage (hIcon, STM_SETICON, (WPARAM)((piFrameLast) ? hiFrame[ *piFrameLast ] : hiStop), 0);
153 BOOL CALLBACK AnimationHook (HWND hIcon, UINT msg, WPARAM wp, LPARAM lp)
155 PVOID oldProc = Subclass_FindNextHook (hIcon, AnimationHook);
161 iFrame = GetWindowData (hIcon, GWD_ANIMATIONFRAME);
162 AfsAppLib_AnimateIcon (hIcon, &iFrame);
163 SetWindowData (hIcon, GWD_ANIMATIONFRAME, iFrame);
167 Subclass_RemoveHook (hIcon, AnimationHook);
172 return CallWindowProc ((WNDPROC)oldProc, hIcon, msg, wp, lp);
174 return DefWindowProc (hIcon, msg, wp, lp);
178 void AfsAppLib_StartAnimation (HWND hIcon, int fps)
180 Subclass_AddHook (hIcon, AnimationHook);
181 SetTimer (hIcon, 0, 1000/((fps) ? fps : 8), NULL);
182 AfsAppLib_AnimateIcon (hIcon);
186 void AfsAppLib_StopAnimation (HWND hIcon)
188 KillTimer (hIcon, 0);
189 AfsAppLib_AnimateIcon (hIcon);
190 Subclass_RemoveHook (hIcon, AnimationHook);
195 * ERROR TRANSLATION __________________________________________________________
199 BOOL CALLBACK AfsAppLib_TranslateErrorFunc (LPTSTR pszText, ULONG code, LANGID idLanguage)
202 if ((idClient = AfsAppLib_GetAdminServerClientID()) != 0)
205 return asc_ErrorCodeTranslate (idClient, code, idLanguage, pszText, &status);
207 else if (OpenUtilLibrary())
209 const char *pszTextA = NULL;
211 if (util_AdminErrorCodeTranslate (code, idLanguage, &pszTextA, &status) && (pszTextA))
213 CopyAnsiToString (pszText, pszTextA);
223 BOOL AfsAppLib_TranslateError (LPTSTR pszText, ULONG status, LANGID idLanguage)
225 LANGID idLangOld = TaLocale_GetLanguage();
226 TaLocale_SetLanguage (idLanguage);
228 BOOL rc = FormatError (pszText, TEXT("%s"), status);
230 TaLocale_SetLanguage (idLangOld);
236 * CELL LIST __________________________________________________________________
240 #define cREALLOC_CELLLIST 4
242 LPCELLLIST AfsAppLib_GetCellList (HKEY hkBase, LPTSTR pszRegPath)
244 LPCELLLIST lpcl = New (CELLLIST);
245 memset (lpcl, 0x00, sizeof(CELLLIST));
247 if (hkBase && pszRegPath)
249 lpcl->hkBase = hkBase;
250 lstrcpy (lpcl->szRegPath, pszRegPath);
253 if (RegOpenKey (hkBase, pszRegPath, &hk) == 0)
255 TCHAR szCell[ cchNAME ];
256 for (size_t ii = 0; RegEnumKey (hk, ii, szCell, cchNAME) == 0; ++ii)
258 if (REALLOC (lpcl->aCells, lpcl->nCells, 1+ii, cREALLOC_CELLLIST))
260 lpcl->aCells[ ii ] = CloneString (szCell);
266 else // Get cell list from AFS
271 TCHAR szDefCell[ cchNAME ];
272 if (AfsAppLib_GetLocalCell (szDefCell))
274 for (size_t iclDef = 0; iclDef < lpcl->nCells; ++iclDef)
276 if (lpcl->aCells[ iclDef ] == NULL)
278 if (!lstrcmpi (lpcl->aCells[ iclDef ], szDefCell))
281 if (iclDef == lpcl->nCells) // default cell not currently in list?
283 for (iclDef = 0; iclDef < lpcl->nCells; ++iclDef)
285 if (lpcl->aCells[ iclDef ] == NULL)
288 if (REALLOC (lpcl->aCells, lpcl->nCells, 1+iclDef, cREALLOC_CELLLIST))
290 lpcl->aCells[iclDef] = CloneString (szDefCell);
293 if ((iclDef > 0) && (iclDef < lpcl->nCells) && (lpcl->aCells[ iclDef ]))
295 LPTSTR pszZero = lpcl->aCells[0];
296 lpcl->aCells[0] = lpcl->aCells[iclDef];
297 lpcl->aCells[iclDef] = pszZero;
301 for ( ; (lpcl->nCells != 0); (lpcl->nCells)--)
303 if (lpcl->aCells[ lpcl->nCells-1 ] != NULL)
311 LPCELLLIST AfsAppLib_GetCellList (LPCELLLIST lpclCopy)
313 LPCELLLIST lpcl = New (CELLLIST);
314 memset (lpcl, 0x00, sizeof(CELLLIST));
318 if (REALLOC (lpcl->aCells, lpcl->nCells, lpclCopy->nCells, cREALLOC_CELLLIST))
320 for (size_t icl = 0; icl < lpcl->nCells; ++icl)
322 if (lpclCopy->aCells[ icl ])
324 lpcl->aCells[ icl ] = CloneString (lpclCopy->aCells[ icl ]);
334 void AfsAppLib_AddToCellList (LPCELLLIST lpcl, LPTSTR pszCell)
336 if (lpcl && lpcl->hkBase && lpcl->szRegPath[0])
338 TCHAR szPath[ MAX_PATH ];
339 wsprintf (szPath, TEXT("%s\\%s"), lpcl->szRegPath, pszCell);
342 if (RegCreateKey (lpcl->hkBase, szPath, &hk) == 0)
350 void AfsAppLib_FreeCellList (LPCELLLIST lpcl)
356 for (size_t icl = 0; icl < lpcl->nCells; ++icl)
358 if (lpcl->aCells[icl] != NULL)
359 FreeString (lpcl->aCells[icl]);
369 * TRULY MISCELLANEOUS ________________________________________________________
373 BOOL AfsAppLib_IsTimeInFuture (LPSYSTEMTIME pstTest)
376 GetSystemTime (&stNow);
379 SystemTimeToFileTime (&stNow, &ftNowGMT);
382 SystemTimeToFileTime (pstTest, &ftTest);
384 if (CompareFileTime (&ftTest, &ftNowGMT) >= 0)
391 void AfsAppLib_UnixTimeToSystemTime (LPSYSTEMTIME pst, ULONG ut, BOOL fElapsed)
393 // A Unix time is the number of seconds since 1/1/1970.
394 // The first step in this conversion is to change that count-of-seconds
395 // into a count-of-100ns-intervals...
398 ldw.QuadPart = (LONGLONG)10000000 * (LONGLONG)ut;
400 // Then adjust the count to be a count-of-100ns-intervals since
401 // 1/1/1601, instead of 1/1/1970. That means adding a *big* number...
403 ldw.QuadPart += (LONGLONG)0x019db1ded53e8000;
405 // Now the count is effectively a FILETIME, which we can convert
406 // to a SYSTEMTIME with a Win32 API.
409 ft.dwHighDateTime = (DWORD)ldw.HighPart;
410 ft.dwLowDateTime = (DWORD)ldw.LowPart;
411 FileTimeToSystemTime (&ft, pst);
417 pst->wDayOfWeek -= 1;
423 void AfsAppLib_SplitCredentials (LPTSTR pszCreds, LPTSTR pszCell, LPTSTR pszID)
425 LPTSTR pszSlash = (LPTSTR)lstrrchr (pszCreds, TEXT('/'));
426 if (pszSlash == NULL)
429 AfsAppLib_GetLocalCell (pszCell);
431 lstrcpy (pszID, pszCreds);
433 else // a cell was specified
436 lstrzcpy (pszCell, pszCreds, pszSlash -pszCreds);
438 lstrcpy (pszID, 1+pszSlash);
443 BOOL AfsAppLib_GetLocalCell (LPTSTR pszCell, ULONG *pStatus)
445 static TCHAR szCell[ cchRESOURCE ] = TEXT("");
450 if (szCell[0] == TEXT('\0'))
453 if ((idClient = AfsAppLib_GetAdminServerClientID()) != 0)
455 rc = asc_LocalCellGet (idClient, szCell, &status);
457 else if (OpenClientLibrary())
459 char szCellNameA[ MAX_PATH ];
460 if ((rc = afsclient_LocalCellGet (szCellNameA, (afs_status_p)&status)) == TRUE)
462 CopyAnsiToString (szCell, szCellNameA);
464 CloseClientLibrary();
469 lstrcpy (pszCell, szCell);
476 BOOL AfsAppLib_ReallocFunction (LPVOID *ppTarget, size_t cbElement, size_t *pcTarget, size_t cReq, size_t cInc)
481 if (cReq <= *pcTarget)
484 if ((cNew = cInc * ((cReq + cInc-1) / cInc)) <= 0)
487 if ((pNew = (LPVOID)Allocate (cbElement * cNew)) == NULL)
489 memset (pNew, 0x00, cbElement * cNew);
493 memcpy (pNew, *ppTarget, cbElement * (*pcTarget));
503 HFONT AfsAppLib_CreateFont (int idsFont)
505 TCHAR szFont[ cchRESOURCE ];
506 GetString (szFont, idsFont);
508 HFONT hf = (HFONT)GetStockObject (DEFAULT_GUI_FONT);
511 GetObject (hf, sizeof(lf), &lf);
512 lf.lfWeight = FW_NORMAL;
514 LPTSTR pszSection = szFont;
515 LPTSTR pszNextSection;
516 if ((pszNextSection = (LPTSTR)lstrchr (pszSection, TEXT(','))) != NULL)
517 *pszNextSection++ = TEXT('\0');
518 if (!pszSection || !*pszSection)
521 lstrcpy (lf.lfFaceName, pszSection);
523 pszSection = pszNextSection;
524 if ((pszNextSection = (LPTSTR)lstrchr (pszSection, TEXT(','))) != NULL)
525 *pszNextSection++ = TEXT('\0');
526 if (!pszSection || !*pszSection)
529 HDC hdc = GetDC (NULL);
530 lf.lfHeight = -MulDiv (_ttol(pszSection), GetDeviceCaps (hdc, LOGPIXELSY), 72);
531 ReleaseDC (NULL, hdc);
533 pszSection = pszNextSection;
534 if ((pszNextSection = (LPTSTR)lstrchr (pszSection, TEXT(','))) != NULL)
535 *pszNextSection++ = TEXT('\0');
536 if (pszSection && *pszSection)
538 if (lstrchr (pszSection, TEXT('b')) || lstrchr (pszSection, TEXT('B')))
539 lf.lfWeight = FW_BOLD;
540 if (lstrchr (pszSection, TEXT('i')) || lstrchr (pszSection, TEXT('I')))
542 if (lstrchr (pszSection, TEXT('u')) || lstrchr (pszSection, TEXT('U')))
543 lf.lfUnderline = TRUE;
546 return CreateFontIndirect(&lf);