7355a99156ff738b62973bb102887feae79e9468
[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"), 0);
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 // WM_COMMAND::ID_VIEW_REFRESH message handler
161 //
162
163 void CAgentConfigMgr::OnViewRefresh()
164 {
165 DWORD i, dwResult, dwNumCfg;
166 NXC_AGENT_CONFIG_INFO *pList;
167 TCHAR szBuffer[32];
168 int iItem;
169
170 m_wndListCtrl.DeleteAllItems();
171 dwResult = DoRequestArg3(NXCGetAgentConfigList, g_hSession, &dwNumCfg, &pList,
172 _T("Loading list of agent configurations..."));
173 if (dwResult == RCC_SUCCESS)
174 {
175 for(i = 0; i < dwNumCfg; i++)
176 {
177 _stprintf(szBuffer, _T("%d"), pList[i].dwId);
178 iItem = m_wndListCtrl.InsertItem(0x7FFFFFFF, szBuffer, -1);
179 if (iItem != -1)
180 {
181 m_wndListCtrl.SetItemData(iItem, pList[i].dwId);
182 _stprintf(szBuffer, _T("%d"), pList[i].dwSequence);
183 m_wndListCtrl.SetItemText(iItem, 1, szBuffer);
184 m_wndListCtrl.SetItemText(iItem, 2, pList[i].szName);
185 }
186 }
187 safe_free(pList);
188 }
189 else
190 {
191 theApp.ErrorBox(dwResult, _T("Cannot load list of agent configurations: %s"));
192 }
193 }
194
195
196 //
197 // UI update handlers
198 //
199
200 void CAgentConfigMgr::OnUpdateConfigEdit(CCmdUI* pCmdUI)
201 {
202 pCmdUI->Enable(m_wndListCtrl.GetSelectedCount() == 1);
203 }
204
205 void CAgentConfigMgr::OnUpdateConfigDelete(CCmdUI* pCmdUI)
206 {
207 pCmdUI->Enable(m_wndListCtrl.GetSelectedCount() > 0);
208 }
209
210 void CAgentConfigMgr::OnUpdateConfigMoveup(CCmdUI* pCmdUI)
211 {
212 pCmdUI->Enable(m_wndListCtrl.GetSelectedCount() == 1);
213 }
214
215 void CAgentConfigMgr::OnUpdateConfigMovedown(CCmdUI* pCmdUI)
216 {
217 pCmdUI->Enable(m_wndListCtrl.GetSelectedCount() == 1);
218 }
219
220
221 //
222 // Menu item "Config->New..." handler
223 //
224
225 void CAgentConfigMgr::OnConfigNew()
226 {
227 NXC_AGENT_CONFIG cfg;
228
229 memset(&cfg, 0, sizeof(NXC_AGENT_CONFIG));
230 EditConfig(&cfg);
231 }
232
233
234 //
235 // Menu item "Config->Edit..." handler
236 //
237
238 void CAgentConfigMgr::OnConfigEdit()
239 {
240 int nItem;
241 NXC_AGENT_CONFIG cfg;
242 DWORD dwResult;
243
244 nItem = m_wndListCtrl.GetSelectionMark();
245 if (nItem == -1)
246 return;
247
248 dwResult = DoRequestArg3(NXCOpenAgentConfig, g_hSession,
249 (void *)m_wndListCtrl.GetItemData(nItem),
250 &cfg, _T("Loading agent configuration..."));
251 if (dwResult == RCC_SUCCESS)
252 {
253 EditConfig(&cfg);
254 }
255 else
256 {
257 theApp.ErrorBox(dwResult, _T("Cannot load agent config: %s"));
258 }
259 }
260
261
262 //
263 // Edit config
264 //
265
266 void CAgentConfigMgr::EditConfig(NXC_AGENT_CONFIG *pConfig)
267 {
268 CAgentCfgDlg dlg;
269 DWORD dwResult;
270 TCHAR szBuffer[32];
271 BOOL bNew = (pConfig->dwId == 0);
272 int nItem;
273
274 dlg.m_strName = pConfig->szName;
275 dlg.m_strText = CHECK_NULL_EX(pConfig->pszText);
276 if ((pConfig->dwId == 0) && (pConfig->pszFilter == NULL))
277 {
278 dlg.m_strFilter = _T("// Filtering script will receive the following arguments:\n")
279 _T("//\n// $1 - agent's IP address\n")
280 _T("// $2 - platform name (like \"windows-i386\")\n")
281 _T("// $3 - agent's major version number\n")
282 _T("// $4 - agent's minor version number\n")
283 _T("// $5 - agent's release number\n\n");
284 }
285 else
286 {
287 dlg.m_strFilter = CHECK_NULL_EX(pConfig->pszFilter);
288 }
289 if (dlg.DoModal() == IDOK)
290 {
291 safe_free(pConfig->pszFilter);
292 safe_free(pConfig->pszText);
293
294 nx_strncpy(pConfig->szName, (LPCTSTR)dlg.m_strName, MAX_DB_STRING);
295 pConfig->pszFilter = (TCHAR *)((LPCTSTR)dlg.m_strFilter);
296 pConfig->pszText = (TCHAR *)((LPCTSTR)dlg.m_strText);
297
298 dwResult = DoRequestArg2(NXCSaveAgentConfig, g_hSession, pConfig,
299 _T("Saving changes in agent configs..."));
300 if (dwResult == RCC_SUCCESS)
301 {
302 if (bNew)
303 {
304 _stprintf(szBuffer, _T("%d"), pConfig->dwId);
305 nItem = m_wndListCtrl.InsertItem(0x7FFFFFFF, szBuffer, -1);
306 _stprintf(szBuffer, _T("%d"), pConfig->dwSequence);
307 m_wndListCtrl.SetItemText(nItem, 1, szBuffer);
308 m_wndListCtrl.SetItemData(nItem, pConfig->dwId);
309 SelectListViewItem(&m_wndListCtrl, nItem);
310 }
311 else
312 {
313 nItem = m_wndListCtrl.GetSelectionMark();
314 }
315 m_wndListCtrl.SetItemText(nItem, 2, pConfig->szName);
316 }
317 else
318 {
319 theApp.ErrorBox(dwResult, _T("Cannot save changes in agent configs: %s"));
320 }
321 }
322 else
323 {
324 safe_free(pConfig->pszFilter);
325 safe_free(pConfig->pszText);
326 }
327 }
328
329
330 //
331 // Delete config records
332 //
333
334 static DWORD DeleteConfigRecords(CListCtrl *pListCtrl)
335 {
336 int nItem;
337 DWORD dwResult = RCC_SUCCESS;
338
339 nItem = pListCtrl->GetNextItem(-1, LVIS_SELECTED);
340 while(nItem != -1)
341 {
342 dwResult = NXCDeleteAgentConfig(g_hSession, pListCtrl->GetItemData(nItem));
343 if (dwResult != RCC_SUCCESS)
344 break;
345 pListCtrl->DeleteItem(nItem);
346 nItem = pListCtrl->GetNextItem(-1, LVIS_SELECTED);
347 }
348 return dwResult;
349 }
350
351
352 //
353 // Menu item "Config->Delete" handler
354 //
355
356 void CAgentConfigMgr::OnConfigDelete()
357 {
358 DWORD dwResult;
359
360 dwResult = DoRequestArg1(DeleteConfigRecords, &m_wndListCtrl, _T("Deleting configs..."));
361 if (dwResult != RCC_SUCCESS)
362 theApp.ErrorBox(dwResult, _T("Cannot delete agent config record: %s"));
363 }
364
365
366 //
367 // Handler for WM_NOTIFY::NM_DBLCLK from ID_LIST_VIEW
368 //
369
370 void CAgentConfigMgr::OnListViewDblClk(LPNMITEMACTIVATE pNMHDR, LRESULT *pResult)
371 {
372 PostMessage(WM_COMMAND, ID_CONFIG_EDIT, 0);
373 }
374
375
376 //
377 // WM_NOTIFY::LVN_COLUMNCLICK message handler
378 //
379
380 void CAgentConfigMgr::OnListViewColumnClick(LPNMLISTVIEW pNMHDR, LRESULT *pResult)
381 {
382 LVCOLUMN lvCol;
383
384 // Unmark old sorting column
385 lvCol.mask = LVCF_FMT;
386 lvCol.fmt = LVCFMT_LEFT;
387 m_wndListCtrl.SetColumn(m_iSortMode, &lvCol);
388
389 // Change current sort mode and resort list
390 if (m_iSortMode == pNMHDR->iSubItem)
391 {
392 // Same column, change sort direction
393 m_iSortDir = 1 - m_iSortDir;
394 }
395 else
396 {
397 // Another sorting column
398 m_iSortMode = pNMHDR->iSubItem;
399 }
400 //m_wndListCtrl.SortItems(ItemCompareProc, (LPARAM)this);
401
402 // Mark new sorting column
403 lvCol.mask = LVCF_IMAGE | LVCF_FMT;
404 lvCol.fmt = LVCFMT_BITMAP_ON_RIGHT | LVCFMT_IMAGE | LVCFMT_LEFT;
405 lvCol.iImage = m_iSortDir;
406 m_wndListCtrl.SetColumn(pNMHDR->iSubItem, &lvCol);
407
408 *pResult = 0;
409 }
410
411
412 //
413 // Handler for "Config->Move up" menu
414 //
415
416 void CAgentConfigMgr::OnConfigMoveup()
417 {
418 int nItem;
419
420 nItem = m_wndListCtrl.GetSelectionMark();
421 if (nItem > 0)
422 SwapItems(nItem, nItem - 1);
423 }
424
425
426 //
427 // Handler for "Config->Move down" menu
428 //
429
430 void CAgentConfigMgr::OnConfigMovedown()
431 {
432 int nItem;
433
434 nItem = m_wndListCtrl.GetSelectionMark();
435 if (nItem < m_wndListCtrl.GetItemCount() - 1)
436 SwapItems(nItem, nItem + 1);
437 }
438
439
440 //
441 // Swap sequence numbers for two items
442 //
443
444 void CAgentConfigMgr::SwapItems(int nItem1, int nItem2)
445 {
446 DWORD dwResult, dwId1, dwId2;
447 TCHAR szBuffer[32], szBuf1[256], szBuf2[256];
448 int nItem;
449
450 dwId1 = m_wndListCtrl.GetItemData(nItem1);
451 dwId2 = m_wndListCtrl.GetItemData(nItem2);
452 dwResult = DoRequestArg3(NXCSwapAgentConfigs, g_hSession, (void *)dwId1,
453 (void *)dwId2, _T("Changing sequence of agent config..."));
454 if (dwResult == RCC_SUCCESS)
455 {
456 m_wndListCtrl.GetItemText(nItem1, 1, szBuf1, 256);
457 m_wndListCtrl.GetItemText(nItem2, 1, szBuf2, 256);
458 m_wndListCtrl.SetItemText(nItem1, 1, szBuf2);
459 if (m_iSortMode == 1)
460 {
461 m_wndListCtrl.GetItemText(nItem1, 2, szBuf2, 256);
462 m_wndListCtrl.DeleteItem(nItem2);
463
464 _stprintf(szBuffer, _T("%d"), dwId2);
465 nItem = m_wndListCtrl.InsertItem(nItem1, szBuffer, -1);
466 m_wndListCtrl.SetItemText(nItem, 1, szBuf1);
467 m_wndListCtrl.SetItemText(nItem, 2, szBuf2);
468 m_wndListCtrl.SetItemData(nItem, dwId2);
469 }
470 else
471 {
472 m_wndListCtrl.SetItemText(nItem2, 1, szBuf1);
473 }
474 }
475 else
476 {
477 theApp.ErrorBox(dwResult, _T("Cannot change sequence number of agent config: %s"));
478 }
479 }