Update NEWS for 1.6.22
[pkg-k5-afs_openafs.git] / src / WINNT / afssvrmgr / svc_viewlog.cpp
blob3f8ba08d9dd1f4a921285ee460d185050ae48618
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 "svrmgr.h"
19 #include <shellapi.h>
20 #include "svc_viewlog.h"
21 #include "svc_general.h"
24 #define cchMAXLOG 20480 // show at most 20k worth of log file
26 #define cxMIN_VIEWLOG 250 // minimum size of View Log File dialog
27 #define cyMIN_VIEWLOG 200 // minimum size of View Log File dialog
31 * RESIZING WINDOWS ___________________________________________________________
35 rwWindowData awdShowLog[] = {
36 { IDC_SVC_VIEWLOG_DESC, raSizeX | raRepaint, 0, 0 },
37 { IDC_SVC_VIEWLOG_FILENAME, raSizeX | raRepaint, 0, 0 },
38 { IDC_VIEWLOG_TEXT, raSizeX | raSizeY | raRepaint, MAKELONG(cxMIN_VIEWLOG,cyMIN_VIEWLOG), 0 },
39 { IDC_SVC_VIEWLOG_CONTENTS, raSizeX, 0, 0 },
40 { IDOK, raMoveX | raMoveY, 0, 0 },
41 { IDC_VIEWLOG_SAVEAS, raMoveX | raMoveY, 0, 0 },
42 { idENDLIST, 0, 0, 0 }
47 * PROTOTYPES _________________________________________________________________
51 BOOL CALLBACK Services_ShowLog_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
52 void Services_ShowLog_TakeNextStep (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp);
53 void Services_ShowLog_OnEndTask (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp, LPTASKPACKET ptp);
54 void Services_ShowLog_OnInitDialog (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp);
55 void Services_ShowLog_OnSaveAs (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp);
56 BOOL Services_ShowLog_Pick (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp);
58 BOOL CALLBACK Services_PickLog_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp);
59 void Services_PickLog_OnInitDialog (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp);
63 * ROUTINES ___________________________________________________________________
67 void Services_ShowServiceLog (LPIDENT lpi)
69 LPSVC_VIEWLOG_PACKET lpp = New (SVC_VIEWLOG_PACKET);
70 lpp->lpiServer = lpi->GetServer();
71 lpp->lpiService = (lpi && lpi->fIsService()) ? lpi : NULL;
72 lpp->szLocal[0] = TEXT('\0');
73 lpp->szRemote[0] = TEXT('\0');
74 lpp->nDownloadAttempts = 0;
76 HWND hDlg = ModelessDialogParam (IDD_SVC_VIEWLOG, NULL, (DLGPROC)Services_ShowLog_DlgProc, (LPARAM)lpp);
77 AfsAppLib_RegisterModelessDialog (hDlg);
81 void Services_ShowServerLog (LPIDENT lpiServer, LPTSTR pszRemote)
83 LPSVC_VIEWLOG_PACKET lpp = New (SVC_VIEWLOG_PACKET);
84 lpp->lpiService = NULL;
85 lpp->lpiServer = lpiServer;
86 lpp->szLocal[0] = TEXT('\0');
87 lpp->nDownloadAttempts = 0;
89 if (pszRemote)
90 lstrcpy (lpp->szRemote, pszRemote);
91 else
92 lpp->szRemote[0] = TEXT('\0');
94 HWND hDlg = ModelessDialogParam (IDD_SVC_VIEWLOG, NULL, (DLGPROC)Services_ShowLog_DlgProc, (LPARAM)lpp);
95 AfsAppLib_RegisterModelessDialog (hDlg);
99 BOOL CALLBACK Services_ShowLog_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
101 if (AfsAppLib_HandleHelp (IDD_SVC_VIEWLOG, hDlg, msg, wp, lp))
102 return TRUE;
104 if (msg == WM_INITDIALOG)
105 SetWindowLongPtr (hDlg, DWLP_USER, lp);
107 LPSVC_VIEWLOG_PACKET lpp;
108 if ((lpp = (LPSVC_VIEWLOG_PACKET)GetWindowLongPtr (hDlg, DWLP_USER)) != NULL)
110 switch (msg)
112 case WM_INITDIALOG:
113 if (gr.rViewLog.right == 0)
114 GetWindowRect (hDlg, &gr.rViewLog);
115 ResizeWindow (hDlg, awdShowLog, rwaMoveToHere, &gr.rViewLog);
117 Services_ShowLog_TakeNextStep (hDlg, lpp);
118 break;
120 case WM_DESTROY:
121 if (lpp->szLocal[0] != TEXT('\0'))
122 DeleteFile (lpp->szLocal);
123 GetWindowRect (hDlg, &gr.rViewLog);
124 SetWindowLongPtr (hDlg, DWLP_USER, 0);
125 Delete (lpp);
126 break;
128 case WM_SIZE:
129 // if (lp==0), we're minimizing--don't call ResizeWindow().
131 if (lp != 0)
132 ResizeWindow (hDlg, awdShowLog, rwaFixupGuts);
133 break;
135 case WM_ENDTASK:
136 LPTASKPACKET ptp;
137 if ((ptp = (LPTASKPACKET)lp) == NULL)
138 Services_ShowLog_TakeNextStep (hDlg, lpp);
139 else
141 Services_ShowLog_OnEndTask (hDlg, lpp, ptp);
142 FreeTaskPacket (ptp);
144 break;
146 case WM_COMMAND:
147 switch (LOWORD(wp))
149 case IDOK:
150 case IDCANCEL:
151 DestroyWindow (hDlg);
152 break;
154 case IDC_VIEWLOG_SAVEAS:
155 Services_ShowLog_OnSaveAs (hDlg, lpp);
156 break;
158 break;
160 case WM_CTLCOLOREDIT:
161 if ((HWND)lp == GetDlgItem (hDlg, IDC_VIEWLOG_TEXT))
163 SetBkColor ((HDC)wp, GetSysColor (COLOR_WINDOW));
164 return CreateSolidBrush (GetSysColor (COLOR_WINDOW))?TRUE:FALSE;
166 break;
170 return FALSE;
174 void Services_ShowLog_TakeNextStep (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp)
176 if (lpp->szLocal[0] != TEXT('\0'))
178 Services_ShowLog_OnInitDialog (hDlg, lpp);
179 ShowWindow (hDlg, SW_SHOW);
181 else if (lpp->szRemote[0] != TEXT('\0'))
183 LPSVC_VIEWLOG_PACKET lppNew = New (SVC_VIEWLOG_PACKET);
184 *lppNew = *lpp;
185 lpp->nDownloadAttempts++;
186 StartTask (taskSVC_VIEWLOG, hDlg, lppNew); // downloads log file
188 else if (lpp->lpiService)
190 StartTask (taskSVC_FINDLOG, hDlg, lpp->lpiService); // guesses log name
192 else
194 if (!Services_ShowLog_Pick (hDlg, lpp))
195 DestroyWindow (hDlg);
196 else
197 PostMessage (hDlg, WM_ENDTASK, 0, 0); // Services_ShowLog_TakeNextStep
202 void Services_ShowLog_OnEndTask (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp, LPTASKPACKET ptp)
204 if (ptp->idTask == taskSVC_FINDLOG) // tried to guess log file name?
206 if (ptp->rc)
208 lstrcpy (lpp->szRemote, TASKDATA(ptp)->pszText1);
209 PostMessage (hDlg, WM_ENDTASK, 0, 0); // Services_ShowLog_TakeNextStep
211 else
213 if (!Services_ShowLog_Pick (hDlg, lpp))
214 DestroyWindow (hDlg);
215 else
216 PostMessage (hDlg, WM_ENDTASK, 0, 0); // Services_ShowLog_TakeNextStep
219 else if (ptp->idTask == taskSVC_VIEWLOG) // tried to download log file?
221 if (ptp->rc)
223 lstrcpy (lpp->szLocal, TASKDATA(ptp)->pszText1);
224 PostMessage (hDlg, WM_ENDTASK, 0, 0); // Services_ShowLog_TakeNextStep
226 else
228 if (lpp->lpiService && lpp->nDownloadAttempts==1)
230 if (!Services_ShowLog_Pick (hDlg, lpp))
231 DestroyWindow (hDlg);
232 else
233 PostMessage (hDlg, WM_ENDTASK, 0, 0); // Services_ShowLog_TakeNextStep
235 else
237 TCHAR szServer[ cchNAME ];
238 lpp->lpiServer->GetServerName (szServer);
239 ErrorDialog (ptp->status, IDS_ERROR_VIEW_LOGFILE, TEXT("%s%s"), szServer, lpp->szRemote);
240 DestroyWindow (hDlg);
247 void Services_ShowLog_OnInitDialog (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp)
249 // If we're viewing the log file for a service, record the remote
250 // filename so the user won't ever have to enter it again.
252 if (lpp->lpiService)
254 LPSERVICE_PREF lpcp;
255 if ((lpcp = (LPSERVICE_PREF)lpp->lpiService->GetUserParam()) != NULL)
257 lstrcpy (lpcp->szLogFile, lpp->szRemote);
258 Services_SavePreferences (lpp->lpiService);
262 // Prepare the ShowLog dialog's static text controls
264 LPTSTR psz;
265 if (lpp->lpiService)
267 TCHAR szServer[ cchNAME ];
268 TCHAR szService[ cchNAME ];
269 lpp->lpiService->GetServerName (szServer);
270 lpp->lpiService->GetServiceName (szService);
271 psz = FormatString (IDS_VIEWLOG_FROMSERVICE, TEXT("%s%s"), szServer, szService);
273 else
275 TCHAR szServer[ cchNAME ];
276 lpp->lpiServer->GetServerName (szServer);
277 psz = FormatString (IDS_VIEWLOG_FROMSERVER, TEXT("%s"), szServer);
279 SetDlgItemText (hDlg, IDC_SVC_VIEWLOG_DESC, psz);
280 FreeString (psz);
282 TCHAR szText[ cchRESOURCE ];
283 GetDlgItemText (hDlg, IDC_SVC_VIEWLOG_FILENAME, szText, cchRESOURCE);
284 psz = FormatString (szText, TEXT("%s"), lpp->szRemote);
285 SetDlgItemText (hDlg, IDC_SVC_VIEWLOG_FILENAME, psz);
286 FreeString (psz);
288 // Read the log file into memory, and chunk its text into the
289 // edit control on the dialog. If the file is over cchMAXLOG chars
290 // long, only read cchMAXLOG bytes from the end (make sure we start
291 // the read after a carriage return).
293 HANDLE hFile = CreateFile (lpp->szLocal, GENERIC_READ, 0, NULL,
294 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
295 if (hFile != INVALID_HANDLE_VALUE)
297 BOOL fTruncated = FALSE;
298 DWORD dwSize = GetFileSize (hFile, NULL);
299 if (dwSize > cchMAXLOG)
301 // find the first \r\n within the final cchMAXLOG chars of the file
303 SetFilePointer (hFile, 0-cchMAXLOG, NULL, FILE_END);
305 TCHAR ch;
306 DWORD cbRead;
307 while (ReadFile (hFile, &ch, 1, &cbRead, NULL) && cbRead)
309 if (ch == TEXT('\n'))
310 break;
313 dwSize -= SetFilePointer (hFile, 0, 0, FILE_CURRENT);
314 fTruncated = TRUE;
317 LPTSTR pszLog;
318 if ((pszLog = AllocateString (dwSize)) != NULL)
320 DWORD cbRead;
321 (void)ReadFile (hFile, pszLog, dwSize, &cbRead, NULL);
322 pszLog[ cbRead ] = TEXT('\0');
324 size_t cch = cbRead;
325 while ( cch &&
326 (pszLog[ cch-1 ] == TEXT('\r')) ||
327 (pszLog[ cch-1 ] == TEXT('\n')) )
329 pszLog[ --cch ] = TEXT('\0');
332 SetFocus (GetDlgItem (hDlg, IDC_VIEWLOG_TEXT));
333 SetDlgItemText (hDlg, IDC_VIEWLOG_TEXT, pszLog);
334 SendDlgItemMessage (hDlg, IDC_VIEWLOG_TEXT, EM_LINESCROLL, 0, 0xFFFF);
335 SendDlgItemMessage (hDlg, IDC_VIEWLOG_TEXT, EM_SETSEL, (WPARAM)-1, 0);
337 if (fTruncated)
339 size_t nLines = 1;
340 for (size_t ich = 0; ich < dwSize; ++ich)
342 if (pszLog[ich] == TEXT('\n'))
343 ++nLines;
346 psz = FormatString (IDS_VIEWLOG_TRUNCATED, TEXT("%lu"), nLines);
347 SetDlgItemText (hDlg, IDC_SVC_VIEWLOG_CONTENTS, psz);
348 FreeString (psz);
351 FreeString (pszLog);
354 CloseHandle (hFile);
359 void Services_ShowLog_OnSaveAs (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp)
361 TCHAR szFilter[ cchRESOURCE ];
362 GetString (szFilter, IDS_SAVELOG_FILTER);
363 TCHAR chFilter = szFilter[ lstrlen(szFilter)-1 ];
364 for (LPTSTR pszFilter = szFilter;
365 (*pszFilter) && ((pszFilter = (LPTSTR)lstrchr (pszFilter, chFilter)) != NULL);
366 ++pszFilter)
368 *pszFilter = TEXT('\0');
371 TCHAR szSaveAs[ MAX_PATH ] = TEXT("");
373 OPENFILENAME sfn;
374 memset (&sfn, 0x00, sizeof(sfn));
375 sfn.lStructSize = sizeof(sfn);
376 sfn.hwndOwner = hDlg;
377 sfn.hInstance = THIS_HINST;
378 sfn.lpstrFilter = szFilter;
379 sfn.nFilterIndex = 1;
380 sfn.lpstrFile = szSaveAs;
381 sfn.nMaxFile = MAX_PATH;
382 sfn.Flags = OFN_HIDEREADONLY | OFN_NOREADONLYRETURN |
383 OFN_OVERWRITEPROMPT | OFN_PATHMUSTEXIST;
384 sfn.lpstrDefExt = TEXT("txt");
386 if (GetSaveFileName (&sfn))
388 TCHAR szzSource[ MAX_PATH+1 ];
389 lstrcpy (szzSource, lpp->szLocal);
390 szzSource[ lstrlen(szzSource)+1 ] = TEXT('\0');
392 TCHAR szzTarget[ MAX_PATH+1 ];
393 lstrcpy (szzTarget, szSaveAs);
394 szzTarget[ lstrlen(szzTarget)+1 ] = TEXT('\0');
396 SHFILEOPSTRUCT op;
397 memset (&op, 0x00, sizeof(op));
398 op.hwnd = hDlg;
399 op.wFunc = FO_COPY;
400 op.pFrom = szzSource;
401 op.pTo = szzTarget;
402 op.fFlags = FOF_ALLOWUNDO | FOF_NOCONFIRMATION;
403 SHFileOperation (&op);
408 BOOL Services_ShowLog_Pick (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp)
410 if (ModalDialogParam (IDD_SVC_LOGNAME, NULL, (DLGPROC)Services_PickLog_DlgProc, (LPARAM)lpp) != IDOK)
412 return FALSE;
415 if (lpp->szRemote[0] == TEXT('\0'))
417 return FALSE;
420 return TRUE;
424 BOOL CALLBACK Services_PickLog_DlgProc (HWND hDlg, UINT msg, WPARAM wp, LPARAM lp)
426 if (AfsAppLib_HandleHelp (IDD_SVC_LOGNAME, hDlg, msg, wp, lp))
427 return TRUE;
429 if (msg == WM_INITDIALOG)
430 SetWindowLongPtr (hDlg, DWLP_USER, lp);
432 LPSVC_VIEWLOG_PACKET lpp;
433 if ((lpp = (LPSVC_VIEWLOG_PACKET)GetWindowLongPtr (hDlg, DWLP_USER)) != NULL)
435 switch (msg)
437 case WM_INITDIALOG:
438 Services_PickLog_OnInitDialog (hDlg, lpp);
439 break;
441 case WM_COMMAND:
442 switch (LOWORD(wp))
444 case IDOK:
445 GetDlgItemText (hDlg, IDC_VIEWLOG_FILENAME, lpp->szRemote, MAX_PATH);
446 case IDCANCEL:
447 EndDialog (hDlg, LOWORD(wp));
448 break;
450 break;
452 case WM_ENDTASK:
453 LPTASKPACKET ptp;
454 if ((ptp = (LPTASKPACKET)lp) != NULL)
456 if (ptp->idTask == taskSVR_ENUM_TO_COMBOBOX)
457 EnableWindow (GetDlgItem (hDlg, IDC_VIEWLOG_SERVER), TRUE);
458 FreeTaskPacket (ptp);
460 break;
462 case WM_NOTIFY:
463 switch (((LPNMHDR)lp)->code)
465 case FLN_ITEMSELECT:
466 if (IsWindowEnabled (GetDlgItem (hDlg, IDC_VIEWLOG_SERVER)))
468 LPIDENT lpi;
469 if ((lpi = (LPIDENT)FL_GetSelectedData (GetDlgItem (hDlg, IDC_VIEWLOG_SERVER))) != NULL)
470 lpp->lpiServer = lpi;
472 break;
474 break;
478 return FALSE;
482 void Services_PickLog_OnInitDialog (HWND hDlg, LPSVC_VIEWLOG_PACKET lpp)
484 SetDlgItemText (hDlg, IDC_VIEWLOG_FILENAME, lpp->szRemote);
485 EnableWindow (GetDlgItem (hDlg, IDC_VIEWLOG_SERVER), FALSE);
487 if (lpp->lpiService)
489 TCHAR szServer[ cchNAME ];
490 TCHAR szService[ cchNAME ];
491 lpp->lpiService->GetServerName (szServer);
492 lpp->lpiService->GetServiceName (szService);
494 LPTSTR pszText = FormatString (IDS_VIEWLOG_DESC_NOFILE, TEXT("%s%s"), szServer, szService);
495 SetDlgItemText (hDlg, IDC_VIEWLOG_DESC, pszText);
496 FreeString (pszText);
498 CB_StartChange (GetDlgItem (hDlg, IDC_VIEWLOG_SERVER), TRUE);
499 CB_AddItem (GetDlgItem (hDlg, IDC_VIEWLOG_SERVER), szServer, (LPARAM)(lpp->lpiServer));
500 CB_EndChange (GetDlgItem (hDlg, IDC_VIEWLOG_SERVER), (LPARAM)(lpp->lpiServer));
502 else
504 LPSVR_ENUM_TO_COMBOBOX_PACKET lpEnum = New (SVR_ENUM_TO_COMBOBOX_PACKET);
505 lpEnum->hCombo = GetDlgItem (hDlg, IDC_VIEWLOG_SERVER);
506 lpEnum->lpiSelect = lpp->lpiServer;
507 StartTask (taskSVR_ENUM_TO_COMBOBOX, hDlg, lpEnum);
510 PostMessage (hDlg, WM_NEXTDLGCTL, (WPARAM)GetDlgItem (hDlg, IDC_VIEWLOG_FILENAME), TRUE);