- Fixed bugs number 195 and 203
[public/netxms.git] / src / console / win32 / AgentConfigMgr.cpp
1 // AgentConfigMgr.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "nxcon.h"
6 #include "AgentConfigMgr.h"
7 #include "AgentCfgDlg.h"
8
9 #ifdef _DEBUG
10 #define new DEBUG_NEW
11 #undef THIS_FILE
12 static char THIS_FILE[] = __FILE__;
13 #endif
14
15 /////////////////////////////////////////////////////////////////////////////
16 // CAgentConfigMgr
17
18 IMPLEMENT_DYNCREATE(CAgentConfigMgr, CMDIChildWnd)
19
20 CAgentConfigMgr::CAgentConfigMgr()
21 {
22 m_iSortMode = theApp.GetProfileInt(_T("AgentConfigMgr"), _T("SortMode"), 1);
23 m_iSortDir = theApp.GetProfileInt(_T("AgentConfigMgr"), _T("SortDir"), 1);
24 }
25
26 CAgentConfigMgr::~CAgentConfigMgr()
27 {
28 theApp.WriteProfileInt(_T("AgentConfigMgr"), _T("SortMode"), m_iSortMode);
29 theApp.WriteProfileInt(_T("AgentConfigMgr"), _T("SortDir"), m_iSortDir);
30 }
31
32
33 BEGIN_MESSAGE_MAP(CAgentConfigMgr, CMDIChildWnd)
34 //{{AFX_MSG_MAP(CAgentConfigMgr)
35 ON_WM_CREATE()
36 ON_WM_DESTROY()
37 ON_WM_SETFOCUS()
38 ON_WM_SIZE()
39 ON_WM_CONTEXTMENU()
40 ON_COMMAND(ID_VIEW_REFRESH, OnViewRefresh)
41 ON_UPDATE_COMMAND_UI(ID_CONFIG_DELETE, OnUpdateConfigDelete)
42 ON_UPDATE_COMMAND_UI(ID_CONFIG_MOVEUP, OnUpdateConfigMoveup)
43 ON_UPDATE_COMMAND_UI(ID_CONFIG_MOVEDOWN, OnUpdateConfigMovedown)
44 ON_UPDATE_COMMAND_UI(ID_CONFIG_EDIT, OnUpdateConfigEdit)
45 ON_COMMAND(ID_CONFIG_NEW, OnConfigNew)
46 ON_COMMAND(ID_CONFIG_EDIT, OnConfigEdit)
47 ON_COMMAND(ID_CONFIG_DELETE, OnConfigDelete)
48 ON_COMMAND(ID_CONFIG_MOVEUP, OnConfigMoveup)
49 ON_COMMAND(ID_CONFIG_MOVEDOWN, OnConfigMovedown)
50 //}}AFX_MSG_MAP
51 ON_NOTIFY(NM_DBLCLK, ID_LIST_VIEW, OnListViewDblClk)
52 ON_NOTIFY(LVN_COLUMNCLICK, ID_LIST_VIEW, OnListViewColumnClick)
53 END_MESSAGE_MAP()
54
55 /////////////////////////////////////////////////////////////////////////////
56 // CAgentConfigMgr message handlers
57
58 BOOL CAgentConfigMgr::PreCreateWindow(CREATESTRUCT& cs)
59 {
60 if (cs.lpszClass == NULL)
61 cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW,
62 NULL, GetSysColorBrush(COLOR_WINDOW),
63 theApp.LoadIcon(IDI_CONFIGS));
64 return CMDIChildWnd::PreCreateWindow(cs);
65 }
66
67
68 //
69 // WM_CREATE message handler
70 //
71
72 int CAgentConfigMgr::OnCreate(LPCREATESTRUCT lpCreateStruct)
73 {
74 RECT rect;
75 LVCOLUMN lvCol;
76
77 if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
78 return -1;
79
80 GetClientRect(&rect);
81
82 // Create list view control
83 m_wndListCtrl.Create(WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SHAREIMAGELISTS,
84 rect, this, ID_LIST_VIEW);
85 m_wndListCtrl.SetExtendedStyle(LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
86
87 // Create image list
88 m_imageList.Create(16, 16, ILC_COLOR24 | ILC_MASK, 2, 1);
89 m_imageList.Add(theApp.LoadIcon(IDI_SORT_UP));
90 m_imageList.Add(theApp.LoadIcon(IDI_SORT_DOWN));
91 m_wndListCtrl.SetImageList(&m_imageList, LVSIL_SMALL);
92
93 // Setup columns
94 m_wndListCtrl.InsertColumn(0, _T("ID"), LVCFMT_LEFT, 55);
95 m_wndListCtrl.InsertColumn(1, _T("Seq."), LVCFMT_LEFT, 55);
96 m_wndListCtrl.InsertColumn(2, _T("Name"), LVCFMT_LEFT, 400);
97 LoadListCtrlColumns(m_wndListCtrl, _T("AgentConfigMgr"), _T("ListCtrl"));
98
99 // Mark sorting column
100 lvCol.mask = LVCF_IMAGE | LVCF_FMT;
101 lvCol.fmt = LVCFMT_BITMAP_ON_RIGHT | LVCFMT_IMAGE | LVCFMT_LEFT;
102 lvCol.iImage = m_iSortDir;
103 m_wndListCtrl.SetColumn(m_iSortMode, &lvCol);
104
105 theApp.OnViewCreate(VIEW_AGENT_CONFIG_MANAGER, this);
106
107 PostMessage(WM_COMMAND, ID_VIEW_REFRESH, 0);
108 return 0;
109 }
110
111
112 //
113 // WM_DESTROY message handler
114 //
115
116 void CAgentConfigMgr::OnDestroy()
117 {
118 SaveListCtrlColumns(m_wndListCtrl, _T("AgentConfigMgr"), _T("ListCtrl"));
119 theApp.OnViewDestroy(VIEW_AGENT_CONFIG_MANAGER, this);
120 CMDIChildWnd::OnDestroy();
121 }
122
123
124 //
125 // WM_SETFOCUS message handler
126 //
127
128 void CAgentConfigMgr::OnSetFocus(CWnd* pOldWnd)
129 {
130 CMDIChildWnd::OnSetFocus(pOldWnd);
131 m_wndListCtrl.SetFocus();
132 }
133
134
135 //
136 // WM_SIZE message handler
137 //
138
139 void CAgentConfigMgr::OnSize(UINT nType, int cx, int cy)
140 {
141 CMDIChildWnd::OnSize(nType, cx, cy);
142 m_wndListCtrl.SetWindowPos(NULL, 0, 0, cx, cy, SWP_NOZORDER);
143 }
144
145
146 //
147 // WM_CONTEXTMENU message handler
148 //
149
150 void CAgentConfigMgr::OnContextMenu(CWnd* pWnd, CPoint point)
151 {
152 CMenu *pMenu;
153
154 pMenu = theApp.GetContextMenu(22);
155 pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this, NULL);
156 }
157
158
159 //
160 // UI update handlers
161 //
162
163 void CAgentConfigMgr::OnUpdateConfigEdit(CCmdUI* pCmdUI)
164 {
165 pCmdUI->Enable(m_wndListCtrl.GetSelectedCount() == 1);
166 }
167
168 void CAgentConfigMgr::OnUpdateConfigDelete(CCmdUI* pCmdUI)
169 {
170 pCmdUI->Enable(m_wndListCtrl.GetSelectedCount() > 0);
171 }
172
173 void CAgentConfigMgr::OnUpdateConfigMoveup(CCmdUI* pCmdUI)
174 {
175 pCmdUI->Enable((m_wndListCtrl.GetSelectedCount() == 1) && (m_iSortMode == 1));
176 }
177
178 void CAgentConfigMgr::OnUpdateConfigMovedown(CCmdUI* pCmdUI)
179 {
180 pCmdUI->Enable((m_wndListCtrl.GetSelectedCount() == 1) && (m_iSortMode == 1));
181 }
182
183
184 //
185 // Menu item "Config->New..." handler
186 //
187
188 void CAgentConfigMgr::OnConfigNew()
189 {
190 NXC_AGENT_CONFIG cfg;
191
192 memset(&cfg, 0, sizeof(NXC_AGENT_CONFIG));
193 EditConfig(&cfg);
194 }
195
196
197 //
198 // Menu item "Config->Edit..." handler
199 //
200
201 void CAgentConfigMgr::OnConfigEdit()
202 {
203 int nItem;
204 NXC_AGENT_CONFIG cfg;
205 DWORD dwResult;
206
207 nItem = m_wndListCtrl.GetSelectionMark();
208 if (nItem == -1)
209 return;
210
211 dwResult = DoRequestArg3(NXCOpenAgentConfig, g_hSession,
212 (void *)m_wndListCtrl.GetItemData(nItem),
213 &cfg, _T("Loading agent configuration..."));
214 if (dwResult == RCC_SUCCESS)
215 {
216 EditConfig(&cfg);
217 }
218 else
219 {
220 theApp.ErrorBox(dwResult, _T("Cannot load agent config: %s"));
221 }
222 }
223
224
225 //
226 // Edit config
227 //
228
229 void CAgentConfigMgr::EditConfig(NXC_AGENT_CONFIG *pConfig)
230 {
231 CAgentCfgDlg dlg;
232 DWORD dwResult;
233 TCHAR szBuffer[32];
234 BOOL bNew = (pConfig->dwId == 0);
235 int nItem;
236
237 dlg.m_strName = pConfig->szName;
238 dlg.m_strText = CHECK_NULL_EX(pConfig->pszText);
239 if ((pConfig->dwId == 0) && (pConfig->pszFilter == NULL))
240 {
241 dlg.m_strFilter = _T("// Filtering script will receive the following arguments:\n")
242 _T("//\n// $1 - agent's IP address\n")
243 _T("// $2 - platform name (like \"windows-i386\")\n")
244 _T("// $3 - agent's major version number\n")
245 _T("// $4 - agent's minor version number\n")
246 _T("// $5 - agent's release number\n\n");
247 }
248 else
249 {
250 dlg.m_strFilter = CHECK_NULL_EX(pConfig->pszFilter);
251 }
252 if (dlg.DoModal() == IDOK)
253 {
254 safe_free(pConfig->pszFilter);
255 safe_free(pConfig->pszText);
256
257 nx_strncpy(pConfig->szName, (LPCTSTR)dlg.m_strName, MAX_DB_STRING);
258 pConfig->pszFilter = (TCHAR *)((LPCTSTR)dlg.m_strFilter);
259 pConfig->pszText = (TCHAR *)((LPCTSTR)dlg.m_strText);
260
261 dwResult = DoRequestArg2(NXCSaveAgentConfig, g_hSession, pConfig,
262 _T("Saving changes in agent configs..."));
263 if (dwResult == RCC_SUCCESS)
264 {
265 if (bNew)
266 {
267 _stprintf(szBuffer, _T("%d"), pConfig->dwId);
268 nItem = m_wndListCtrl.InsertItem(0x7FFFFFFF, szBuffer, -1);
269 _stprintf(szBuffer, _T("%d"), pConfig->dwSequence);
270 m_wndListCtrl.SetItemText(nItem, 1, szBuffer);
271 m_wndListCtrl.SetItemData(nItem, pConfig->dwId);
272 SelectListViewItem(&m_wndListCtrl, nItem);
273 }
274 else
275 {
276 nItem = m_wndListCtrl.GetSelectionMark();
277 }
278 m_wndListCtrl.SetItemText(nItem, 2, pConfig->szName);
279 }
280 else
281 {
282 theApp.ErrorBox(dwResult, _T("Cannot save changes in agent configs: %s"));
283 }
284 }
285 else
286 {
287 safe_free(pConfig->pszFilter);
288 safe_free(pConfig->pszText);
289 }
290 }
291
292
293 //
294 // Delete config records
295 //
296
297 static DWORD DeleteConfigRecords(CListCtrl *pListCtrl)
298 {
299 int nItem;
300 DWORD dwResult = RCC_SUCCESS;
301
302 nItem = pListCtrl->GetNextItem(-1, LVIS_SELECTED);
303 while(nItem != -1)
304 {
305 dwResult = NXCDeleteAgentConfig(g_hSession, pListCtrl->GetItemData(nItem));
306 if (dwResult != RCC_SUCCESS)
307 break;
308 pListCtrl->DeleteItem(nItem);
309 nItem = pListCtrl->GetNextItem(-1, LVIS_SELECTED);
310 }
311 return dwResult;
312 }
313
314
315 //
316 // Menu item "Config->Delete" handler
317 //
318
319 void CAgentConfigMgr::OnConfigDelete()
320 {
321 DWORD dwResult;
322
323 dwResult = DoRequestArg1(DeleteConfigRecords, &m_wndListCtrl, _T("Deleting configs..."));
324 if (dwResult != RCC_SUCCESS)
325 theApp.ErrorBox(dwResult, _T("Cannot delete agent config record: %s"));
326 }
327
328
329 //
330 // Handler for WM_NOTIFY::NM_DBLCLK from ID_LIST_VIEW
331 //
332
333 void CAgentConfigMgr::OnListViewDblClk(LPNMITEMACTIVATE pNMHDR, LRESULT *pResult)
334 {
335 PostMessage(WM_COMMAND, ID_CONFIG_EDIT, 0);
336 }
337
338
339 //
340 // Item comparision procedure for sorting
341 //
342
343 static int CALLBACK ItemCompareProc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
344 {
345 CAgentConfigMgr *pWnd = (CAgentConfigMgr *)lParamSort;
346 TCHAR item1[256], item2[256];
347 int iResult;
348
349 pWnd->GetItemText(lParam1, pWnd->GetSortMode(), item1);
350 pWnd->GetItemText(lParam2, pWnd->GetSortMode(), item2);
351
352 switch(pWnd->GetSortMode())
353 {
354 case 0: // ID
355 iResult = COMPARE_NUMBERS(_tcstol(item1, NULL, 10), _tcstol(item2, NULL, 10));
356 break;
357 case 1: // Sequence
358 iResult = COMPARE_NUMBERS(_tcstol(item1, NULL, 10), _tcstol(item2, NULL, 10));
359 break;
360 case 2: // Name
361 iResult = _tcsicmp(item1, item2);
362 break;
363 default:
364 iResult = 0;
365 break;
366 }
367
368 return iResult * pWnd->GetSortDir();
369 }
370
371
372 //
373 // WM_NOTIFY::LVN_COLUMNCLICK message handler
374 //
375
376 void CAgentConfigMgr::OnListViewColumnClick(LPNMLISTVIEW pNMHDR, LRESULT *pResult)
377 {
378 LVCOLUMN lvCol;
379
380 // Unmark old sorting column
381 lvCol.mask = LVCF_FMT;
382 lvCol.fmt = LVCFMT_LEFT;
383 m_wndListCtrl.SetColumn(m_iSortMode, &lvCol);
384
385 // Change current sort mode and resort list
386 if (m_iSortMode == pNMHDR->iSubItem)
387 {
388 // Same column, change sort direction
389 m_iSortDir = -m_iSortDir;
390 }
391 else
392 {
393 // Another sorting column
394 m_iSortMode = pNMHDR->iSubItem;
395 }
396 m_wndListCtrl.SortItems(ItemCompareProc, (LPARAM)this);
397
398 // Mark new sorting column
399 lvCol.mask = LVCF_IMAGE | LVCF_FMT;
400 lvCol.fmt = LVCFMT_BITMAP_ON_RIGHT | LVCFMT_IMAGE | LVCFMT_LEFT;
401 lvCol.iImage = (m_iSortDir == 1) ? 0 : 1;
402 m_wndListCtrl.SetColumn(pNMHDR->iSubItem, &lvCol);
403
404 *pResult = 0;
405 }
406
407
408 //
409 // Handler for "Config->Move up" menu
410 //
411
412 void CAgentConfigMgr::OnConfigMoveup()
413 {
414 int nItem;
415
416 nItem = m_wndListCtrl.GetSelectionMark();
417 if (nItem > 0)
418 SwapItems(nItem, nItem - 1);
419 }
420
421
422 //
423 // Handler for "Config->Move down" menu
424 //
425
426 void CAgentConfigMgr::OnConfigMovedown()
427 {
428 int nItem;
429
430 nItem = m_wndListCtrl.GetSelectionMark();
431 if (nItem < m_wndListCtrl.GetItemCount() - 1)
432 SwapItems(nItem, nItem + 1);
433 }
434
435
436 //
437 // Swap sequence numbers for two items
438 //
439
440 void CAgentConfigMgr::SwapItems(int nItem1, int nItem2)
441 {
442 DWORD dwResult, dwId1, dwId2;
443 TCHAR szBuffer[32], szBuf1[256], szBuf2[256];
444 int nItem;
445
446 dwId1 = m_wndListCtrl.GetItemData(nItem1);
447 dwId2 = m_wndListCtrl.GetItemData(nItem2);
448 dwResult = DoRequestArg3(NXCSwapAgentConfigs, g_hSession, (void *)dwId1,
449 (void *)dwId2, _T("Changing sequence of agent config..."));
450 if (dwResult == RCC_SUCCESS)
451 {
452 m_wndListCtrl.GetItemText(nItem1, 1, szBuf1, 256);
453 m_wndListCtrl.GetItemText(nItem2, 1, szBuf2, 256);
454 m_wndListCtrl.SetItemText(nItem1, 1, szBuf2);
455 if (m_iSortMode == 1)
456 {
457 m_wndListCtrl.GetItemText(nItem2, 2, szBuf2, 256);
458 m_wndListCtrl.DeleteItem(nItem2);
459
460 _stprintf(szBuffer, _T("%d"), dwId2);
461 nItem = m_wndListCtrl.InsertItem(nItem1, szBuffer, -1);
462 m_wndListCtrl.SetItemText(nItem, 1, szBuf1);
463 m_wndListCtrl.SetItemText(nItem, 2, szBuf2);
464 m_wndListCtrl.SetItemData(nItem, dwId2);
465 }
466 else
467 {
468 m_wndListCtrl.SetItemText(nItem2, 1, szBuf1);
469 }
470 }
471 else
472 {
473 theApp.ErrorBox(dwResult, _T("Cannot change sequence number of agent config: %s"));
474 }
475 }
476
477
478 //
479 // Get list item text
480 //
481
482 void CAgentConfigMgr::GetItemText(int item, int col, TCHAR *buffer)
483 {
484 int index;
485 LVFINDINFO lvfi;
486
487 lvfi.flags = LVFI_PARAM;
488 lvfi.lParam = item;
489 index = m_wndListCtrl.FindItem(&lvfi);
490 if (index != -1)
491 {
492 m_wndListCtrl.GetItemText(index, col, buffer, 256);
493 }
494 else
495 {
496 *buffer = 0;
497 }
498 }
499
500
501 //
502 // WM_COMMAND::ID_VIEW_REFRESH message handler
503 //
504
505 void CAgentConfigMgr::OnViewRefresh()
506 {
507 DWORD i, dwResult, dwNumCfg;
508 NXC_AGENT_CONFIG_INFO *pList;
509 TCHAR szBuffer[32];
510 int iItem;
511
512 m_wndListCtrl.DeleteAllItems();
513 dwResult = DoRequestArg3(NXCGetAgentConfigList, g_hSession, &dwNumCfg, &pList,
514 _T("Loading list of agent configurations..."));
515 if (dwResult == RCC_SUCCESS)
516 {
517 for(i = 0; i < dwNumCfg; i++)
518 {
519 _stprintf(szBuffer, _T("%d"), pList[i].dwId);
520 iItem = m_wndListCtrl.InsertItem(0x7FFFFFFF, szBuffer, -1);
521 if (iItem != -1)
522 {
523 m_wndListCtrl.SetItemData(iItem, pList[i].dwId);
524 _stprintf(szBuffer, _T("%d"), pList[i].dwSequence);
525 m_wndListCtrl.SetItemText(iItem, 1, szBuffer);
526 m_wndListCtrl.SetItemText(iItem, 2, pList[i].szName);
527 }
528 }
529 safe_free(pList);
530 m_wndListCtrl.SortItems(ItemCompareProc, (LPARAM)this);
531 }
532 else
533 {
534 theApp.ErrorBox(dwResult, _T("Cannot load list of agent configurations: %s"));
535 }
536 }