Update NEWS for 1.6.22
[pkg-k5-afs_openafs.git] / src / WINNT / afsapplib / al_misc.cpp
blob9bf13b63574333103e8bfece0260c123006fcc47
1 /*
2 * Copyright 2000, International Business Machines Corporation and others.
3 * All Rights Reserved.
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
8 */
10 #include <winsock2.h>
11 #include <ws2tcpip.h>
13 extern "C" {
14 #include <afs/param.h>
15 #include <afs/stds.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)
49 return g_hInst;
53 EXPORTED HINSTANCE AfsAppLib_GetAppInstance (void)
55 if (!g_hInstApp)
56 g_hInstApp = GetModuleHandle(NULL);
57 return g_hInstApp;
61 EXPORTED void AfsAppLib_SetAppInstance (HINSTANCE hInst)
63 g_hInstApp = hInst;
67 EXPORTED BOOLEAN _stdcall DllEntryPoint (HANDLE hInst, DWORD dwReason, PVOID pReserved)
69 switch (dwReason)
71 case DLL_PROCESS_ATTACH:
72 if (!g_hInst)
74 g_hInst = (HINSTANCE)hInst;
75 TaLocale_LoadCorrespondingModule (g_hInst);
76 SetErrorTranslationFunction ((LPERRORPROC)AfsAppLib_TranslateErrorFunc);
78 InitCommonControls();
79 RegisterSpinnerClass();
80 RegisterElapsedClass();
81 RegisterTimeClass();
82 RegisterDateClass();
83 RegisterSockAddrClass();
84 RegisterCheckListClass();
85 RegisterFastListClass();
87 break;
89 return TRUE;
94 * IMAGE LISTS ________________________________________________________________
98 HIMAGELIST AfsAppLib_CreateImageList (BOOL fLarge)
100 HIMAGELIST hil;
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);
120 return hil;
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)
138 static HICON hiStop;
139 static HICON hiFrame[8];
140 static BOOL fLoaded = FALSE;
142 if (!fLoaded)
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);
153 fLoaded = TRUE;
156 if (piFrameLast)
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);
169 switch (msg)
171 case WM_TIMER:
172 int iFrame;
173 iFrame = (int)GetWindowData (hIcon, GWD_ANIMATIONFRAME);
174 AfsAppLib_AnimateIcon (hIcon, &iFrame);
175 SetWindowData (hIcon, GWD_ANIMATIONFRAME, iFrame);
176 break;
178 case WM_DESTROY:
179 Subclass_RemoveHook (hIcon, AnimationHook);
180 break;
183 if (oldProc)
184 return CallWindowProc ((WNDPROC)oldProc, hIcon, msg, wp, lp);
185 else
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)
213 DWORD idClient;
214 if ((idClient = (DWORD)AfsAppLib_GetAdminServerClientID()) != 0)
216 ULONG status;
217 return asc_ErrorCodeTranslate (idClient, code, idLanguage, pszText, &status);
219 else
220 if (OpenUtilLibrary())
222 const char *pszTextA = NULL;
223 afs_status_t status;
224 if (util_AdminErrorCodeTranslate (code, idLanguage, &pszTextA, &status) && (pszTextA))
226 CopyAnsiToString (pszText, pszTextA);
227 return TRUE;
229 CloseUtilLibrary();
232 return FALSE;
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);
244 return rc;
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);
265 HKEY hk;
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);
276 RegCloseKey (hk);
279 else // Get cell list from AFS
281 // TODO
284 TCHAR szDefCell[ cchNAME ];
285 if (AfsAppLib_GetLocalCell (szDefCell))
287 size_t iclDef;
288 for (iclDef = 0; iclDef < lpcl->nCells; ++iclDef)
290 if (lpcl->aCells[ iclDef ] == NULL)
291 continue;
292 if (!lstrcmpi (lpcl->aCells[ iclDef ], szDefCell))
293 break;
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)
300 break;
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)
318 break;
321 return lpcl;
325 LPCELLLIST AfsAppLib_GetCellList (LPCELLLIST lpclCopy)
327 LPCELLLIST lpcl = New (CELLLIST);
328 memset (lpcl, 0x00, sizeof(CELLLIST));
330 if (lpclCopy)
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 ]);
344 return lpcl;
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);
355 HKEY hk;
356 if (RegCreateKey (lpcl->hkBase, szPath, &hk) == 0)
358 RegCloseKey (hk);
364 void AfsAppLib_FreeCellList (LPCELLLIST lpcl)
366 if (lpcl)
368 if (lpcl->aCells)
370 for (size_t icl = 0; icl < lpcl->nCells; ++icl)
372 if (lpcl->aCells[icl] != NULL)
373 FreeString (lpcl->aCells[icl]);
375 Free (lpcl->aCells);
377 Delete (lpcl);
383 * TRULY MISCELLANEOUS ________________________________________________________
387 BOOL AfsAppLib_IsTimeInFuture (LPSYSTEMTIME pstTest)
389 SYSTEMTIME stNow;
390 GetSystemTime (&stNow);
392 FILETIME ftNowGMT;
393 SystemTimeToFileTime (&stNow, &ftNowGMT);
395 FILETIME ftTest;
396 SystemTimeToFileTime (pstTest, &ftTest);
398 if (CompareFileTime (&ftTest, &ftNowGMT) >= 0)
399 return TRUE;
401 return FALSE;
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...
411 LARGE_INTEGER ldw;
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.
422 FILETIME ft;
423 ft.dwHighDateTime = (DWORD)ldw.HighPart;
424 ft.dwLowDateTime = (DWORD)ldw.LowPart;
425 FileTimeToSystemTime (&ft, pst);
427 if (fElapsed)
429 pst->wYear -= 1970;
430 pst->wMonth -= 1;
431 pst->wDayOfWeek -= 1;
432 pst->wDay -= 1;
437 void AfsAppLib_SplitCredentials (LPTSTR pszCreds, LPTSTR pszCell, LPTSTR pszID)
439 LPTSTR pszSlash = (LPTSTR)lstrrchr (pszCreds, TEXT('/'));
440 if (pszSlash == NULL)
442 if (pszCell)
443 AfsAppLib_GetLocalCell (pszCell);
444 if (pszID)
445 lstrcpy (pszID, pszCreds);
447 else // a cell was specified
449 if (pszCell)
450 lstrzcpy (pszCell, pszCreds, pszSlash -pszCreds);
451 if (pszID)
452 lstrcpy (pszID, 1+pszSlash);
457 BOOL AfsAppLib_GetLocalCell (LPTSTR pszCell, ULONG *pStatus)
459 static TCHAR szCell[ cchRESOURCE ] = TEXT("");
461 BOOL rc = TRUE;
462 ULONG status = 0;
464 if (szCell[0] == TEXT('\0'))
466 DWORD idClient;
467 if ((idClient = (DWORD)AfsAppLib_GetAdminServerClientID()) != 0)
469 rc = asc_LocalCellGet (idClient, szCell, &status);
471 else
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();
483 if (rc)
484 lstrcpy (pszCell, szCell);
485 else if (pStatus)
486 *pStatus = status;
487 return rc;
491 BOOL AfsAppLib_ReallocFunction (LPVOID *ppTarget, size_t cbElement, size_t *pcTarget, size_t cReq, size_t cInc)
493 LPVOID pNew;
494 size_t cNew;
496 if (cReq <= *pcTarget)
497 return TRUE;
499 if ((cNew = cInc * ((cReq + cInc-1) / cInc)) <= 0)
500 return FALSE;
502 if ((pNew = (LPVOID)Allocate (cbElement * cNew)) == NULL)
503 return FALSE;
504 memset (pNew, 0x00, cbElement * cNew);
506 if (*pcTarget != 0)
508 memcpy (pNew, *ppTarget, cbElement * (*pcTarget));
509 Free (*ppTarget);
512 *ppTarget = pNew;
513 *pcTarget = cNew;
514 return TRUE;
518 HFONT AfsAppLib_CreateFont (int idsFont)
520 TCHAR szFont[ cchRESOURCE ];
521 GetString (szFont, idsFont);
523 HFONT hf = (HFONT)GetStockObject (DEFAULT_GUI_FONT);
525 LOGFONT lf;
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)
534 return NULL;
536 lstrcpy (lf.lfFaceName, pszSection);
538 pszSection = pszNextSection;
539 if ((pszNextSection = (LPTSTR)lstrchr (pszSection, TEXT(','))) != NULL)
540 *pszNextSection++ = TEXT('\0');
541 if (!pszSection || !*pszSection)
542 return NULL;
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')))
556 lf.lfItalic = TRUE;
557 if (lstrchr (pszSection, TEXT('u')) || lstrchr (pszSection, TEXT('U')))
558 lf.lfUnderline = TRUE;
561 return CreateFontIndirect(&lf);