- Fixed bugs number 195 and 203
[public/netxms.git] / src / console / win32 / EventPolicyEditor.cpp
1 // EventPolicyEditor.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "nxcon.h"
6 #include "EventPolicyEditor.h"
7 #include "RuleSeverityDlg.h"
8 #include "RuleAlarmDlg.h"
9 #include "RuleScriptDlg.h"
10 #include "RuleOptionsDlg.h"
11 #include "ActionSelDlg.h"
12 #include "RuleSituationDlg.h"
13
14 #ifdef _DEBUG
15 #define new DEBUG_NEW
16 #undef THIS_FILE
17 static char THIS_FILE[] = __FILE__;
18 #endif
19
20
21 //
22 // Mask for any severity match
23 //
24
25 #define ANY_SEVERITY (RF_SEVERITY_INFO | RF_SEVERITY_WARNING | RF_SEVERITY_MINOR | \
26 RF_SEVERITY_MAJOR | RF_SEVERITY_CRITICAL)
27
28
29 //
30 // Column numbers
31 //
32
33 #define COL_RULE 0
34 #define COL_SOURCE 1
35 #define COL_EVENT 2
36 #define COL_SEVERITY 3
37 #define COL_SCRIPT 4
38 #define COL_ALARM 5
39 #define COL_SITUATION 6
40 #define COL_ACTION 7
41 #define COL_OPTIONS 8
42 #define COL_COMMENT 9
43
44
45 /////////////////////////////////////////////////////////////////////////////
46 // CEventPolicyEditor
47
48 IMPLEMENT_DYNCREATE(CEventPolicyEditor, CMDIChildWnd)
49
50 CEventPolicyEditor::CEventPolicyEditor()
51 {
52 m_pEventPolicy = theApp.m_pEventPolicy;
53 m_pImageList = NULL;
54 m_bIsModified = FALSE;
55 }
56
57 CEventPolicyEditor::~CEventPolicyEditor()
58 {
59 delete m_pImageList;
60 }
61
62
63 BEGIN_MESSAGE_MAP(CEventPolicyEditor, CMDIChildWnd)
64 //{{AFX_MSG_MAP(CEventPolicyEditor)
65 ON_WM_CREATE()
66 ON_WM_DESTROY()
67 ON_WM_CLOSE()
68 ON_WM_SIZE()
69 ON_WM_CONTEXTMENU()
70 ON_WM_SETFOCUS()
71 ON_COMMAND(ID_POLICY_INSERTRULE_TOP, OnPolicyInsertruleTop)
72 ON_COMMAND(ID_POLICY_INSERTRULE_BOTTOM, OnPolicyInsertruleBottom)
73 ON_COMMAND(ID_POLICY_INSERTRULE_ABOVE, OnPolicyInsertruleAbove)
74 ON_COMMAND(ID_POLICY_INSERTRULE_BELOW, OnPolicyInsertruleBelow)
75 ON_UPDATE_COMMAND_UI(ID_POLICY_INSERTRULE_BELOW, OnUpdatePolicyInsertruleBelow)
76 ON_UPDATE_COMMAND_UI(ID_POLICY_INSERTRULE_ABOVE, OnUpdatePolicyInsertruleAbove)
77 ON_UPDATE_COMMAND_UI(ID_POLICY_NEGATECELL, OnUpdatePolicyNegatecell)
78 ON_UPDATE_COMMAND_UI(ID_POLICY_DISABLERULE, OnUpdatePolicyDisablerule)
79 ON_UPDATE_COMMAND_UI(ID_POLICY_DELETERULE, OnUpdatePolicyDeleterule)
80 ON_COMMAND(ID_POLICY_DISABLERULE, OnPolicyDisablerule)
81 ON_COMMAND(ID_POLICY_ENABLERULE, OnPolicyEnablerule)
82 ON_UPDATE_COMMAND_UI(ID_POLICY_ENABLERULE, OnUpdatePolicyEnablerule)
83 ON_COMMAND(ID_POLICY_ADD, OnPolicyAdd)
84 ON_UPDATE_COMMAND_UI(ID_POLICY_ADD, OnUpdatePolicyAdd)
85 ON_COMMAND(ID_POLICY_DELETE, OnPolicyDelete)
86 ON_UPDATE_COMMAND_UI(ID_POLICY_DELETE, OnUpdatePolicyDelete)
87 ON_COMMAND(ID_POLICY_DELETERULE, OnPolicyDeleterule)
88 ON_COMMAND(ID_POLICY_EDIT, OnPolicyEdit)
89 ON_COMMAND(ID_POLICY_SAVE, OnPolicySave)
90 ON_UPDATE_COMMAND_UI(ID_POLICY_SAVE, OnUpdatePolicySave)
91 ON_COMMAND(ID_POLICY_NEGATECELL, OnPolicyNegatecell)
92 //}}AFX_MSG_MAP
93 ON_NOTIFY(NM_DBLCLK, ID_RULE_LIST, OnRuleListDblClk)
94 END_MESSAGE_MAP()
95
96 /////////////////////////////////////////////////////////////////////////////
97 // CEventPolicyEditor message handlers
98
99
100 //
101 // Redefined PreCreateWindow()
102 //
103
104 BOOL CEventPolicyEditor::PreCreateWindow(CREATESTRUCT& cs)
105 {
106 if (cs.lpszClass == NULL)
107 cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW, NULL, NULL,
108 AfxGetApp()->LoadIcon(IDI_RULEMGR));
109 return CMDIChildWnd::PreCreateWindow(cs);
110 }
111
112
113 //
114 // WM_CREATE message handler
115 //
116
117 int CEventPolicyEditor::OnCreate(LPCREATESTRUCT lpCreateStruct)
118 {
119 RECT rect;
120 CBitmap bmp;
121 DWORD i;
122
123 if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
124 return -1;
125
126 // Create image list for rule list control
127 m_pImageList = new CImageList;
128 m_pImageList->Create(g_pObjectSmallImageList);
129 m_iImageAny = m_pImageList->GetImageCount();
130 LoadBitmapIntoList(m_pImageList, IDB_ANY, PSYM_MASK_COLOR);
131 LoadBitmapIntoList(m_pImageList, IDB_NONE, PSYM_MASK_COLOR);
132 m_pImageList->Add(theApp.LoadIcon(IDI_SITUATION));
133 m_pImageList->Add(theApp.LoadIcon(IDI_INSTANCE));
134 m_iImageSeverityBase = m_pImageList->GetImageCount();
135 m_pImageList->Add(theApp.LoadIcon(IDI_SEVERITY_NORMAL));
136 m_pImageList->Add(theApp.LoadIcon(IDI_SEVERITY_WARNING));
137 m_pImageList->Add(theApp.LoadIcon(IDI_SEVERITY_MINOR));
138 m_pImageList->Add(theApp.LoadIcon(IDI_SEVERITY_MAJOR));
139 m_pImageList->Add(theApp.LoadIcon(IDI_SEVERITY_CRITICAL));
140 m_pImageList->Add(theApp.LoadIcon(IDI_UNKNOWN));
141 m_pImageList->Add(theApp.LoadIcon(IDI_ACK));
142 m_iImageActionsBase = m_pImageList->GetImageCount();
143 m_pImageList->Add(theApp.LoadIcon(IDI_EXEC));
144 m_pImageList->Add(theApp.LoadIcon(IDI_REXEC));
145 m_pImageList->Add(theApp.LoadIcon(IDI_EMAIL));
146 m_pImageList->Add(theApp.LoadIcon(IDI_SMS));
147 m_iImageOptionsBase = m_pImageList->GetImageCount();
148 m_pImageList->Add(theApp.LoadIcon(IDI_STOP));
149
150 // Create rule list control
151 GetClientRect(&rect);
152 m_wndRuleList.Create(WS_CHILD | WS_VISIBLE, rect, this, ID_RULE_LIST);
153 m_wndRuleList.SetImageList(m_pImageList);
154
155 // Setup columns
156 m_wndRuleList.InsertColumn(0, _T("No."), 35, CF_CENTER | CF_TITLE_COLOR | CF_NON_SELECTABLE);
157 m_wndRuleList.InsertColumn(1, _T("Source"), 150);
158 m_wndRuleList.InsertColumn(2, _T("Event"), 150);
159 m_wndRuleList.InsertColumn(3, _T("Severity"), 90, CF_NON_SELECTABLE);
160 m_wndRuleList.InsertColumn(4, _T("Script"), 150, CF_TEXTBOX | CF_NON_SELECTABLE);
161 m_wndRuleList.InsertColumn(5, _T("Alarm"), 150, CF_NON_SELECTABLE);
162 m_wndRuleList.InsertColumn(6, _T("Situation"), 150, CF_NON_SELECTABLE);
163 m_wndRuleList.InsertColumn(7, _T("Action"), 150);
164 m_wndRuleList.InsertColumn(8, _T("Options"), 150, CF_NON_SELECTABLE);
165 m_wndRuleList.InsertColumn(9, _T("Comments"), 200, CF_TEXTBOX | CF_NON_SELECTABLE);
166 m_wndRuleList.RestoreColumns(_T("EventPolicyEditor"), _T("RuleList"));
167
168 // Fill rule list with existing rules
169 for(i = 0; i < m_pEventPolicy->dwNumRules; i++)
170 {
171 m_wndRuleList.InsertRow(i);
172 UpdateRow(i);
173 if (m_pEventPolicy->pRuleList[i].dwFlags & RF_DISABLED)
174 m_wndRuleList.EnableRow(i, FALSE);
175 }
176
177 theApp.OnViewCreate(VIEW_EPP_EDITOR, this);
178 return 0;
179 }
180
181
182 //
183 // WM_DESTROY message handler
184 //
185
186 void CEventPolicyEditor::OnDestroy()
187 {
188 NXCDestroyEventPolicy(theApp.m_pEventPolicy);
189 theApp.OnViewDestroy(VIEW_EPP_EDITOR, this);
190 CMDIChildWnd::OnDestroy();
191 }
192
193
194 //
195 // WM_CLOSE message handler
196 //
197
198 void CEventPolicyEditor::OnClose()
199 {
200 DWORD dwResult;
201 int iAnswer = IDNO;
202
203 m_wndRuleList.SaveColumns(_T("EventPolicyEditor"), _T("RuleList"));
204
205 if (m_bIsModified)
206 {
207 iAnswer = MessageBox(_T("Event processing policy was modified. Do you want to save changes?"),
208 _T("Confirmation"), MB_YESNOCANCEL | MB_ICONQUESTION);
209
210 if (iAnswer == IDYES)
211 {
212 dwResult = DoRequestArg2(NXCSaveEventPolicy, g_hSession,
213 m_pEventPolicy, _T("Saving event processing policy..."));
214 if (dwResult != RCC_SUCCESS)
215 {
216 theApp.ErrorBox(dwResult, _T("Error saving event processing policy: %s"));
217 iAnswer = IDCANCEL; // Will not close window if there are errors
218 }
219 }
220 }
221
222 if (iAnswer != IDCANCEL)
223 {
224 dwResult = DoRequestArg1(NXCCloseEventPolicy, g_hSession,
225 _T("Unlocking event processing policy..."));
226 if (dwResult != RCC_SUCCESS)
227 theApp.ErrorBox(dwResult, _T("Error unlocking event processing policy: %s"));
228 CMDIChildWnd::OnClose();
229 }
230 }
231
232
233 //
234 // WM_SIZE message handler
235 //
236
237 void CEventPolicyEditor::OnSize(UINT nType, int cx, int cy)
238 {
239 CMDIChildWnd::OnSize(nType, cx, cy);
240
241 m_wndRuleList.SetWindowPos(NULL, 0, 0, cx, cy, SWP_NOZORDER);
242 }
243
244
245 //
246 // WM_CONTEXTMENU message handler
247 //
248
249 void CEventPolicyEditor::OnContextMenu(CWnd* pWnd, CPoint point)
250 {
251 CMenu *pMenu;
252 int iMenu;
253
254 switch(m_wndRuleList.GetCurrentColumn())
255 {
256 case COL_SOURCE:
257 case COL_EVENT:
258 case COL_ACTION:
259 iMenu = 4;
260 break;
261 case COL_SEVERITY:
262 case COL_SCRIPT:
263 case COL_ALARM:
264 case COL_SITUATION:
265 case COL_OPTIONS:
266 case COL_COMMENT:
267 iMenu = 5;
268 break;
269 default:
270 iMenu = 3; // Row operations only
271 break;
272 }
273 pMenu = theApp.GetContextMenu(iMenu);
274 pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this, NULL);
275 }
276
277
278 //
279 // WM_SETFOCUS message handler
280 //
281
282 void CEventPolicyEditor::OnSetFocus(CWnd* pOldWnd)
283 {
284 CMDIChildWnd::OnSetFocus(pOldWnd);
285
286 m_wndRuleList.SetFocus();
287 }
288
289
290 //
291 // Insert new rule into policy
292 //
293
294 void CEventPolicyEditor::InsertNewRule(int iInsertBefore)
295 {
296 int iPos;
297 TCHAR szBuffer[32];
298
299 // Position for new rule
300 iPos = (iInsertBefore > (int)m_pEventPolicy->dwNumRules) ?
301 (int)m_pEventPolicy->dwNumRules : iInsertBefore;
302
303 // Extend rule list
304 m_pEventPolicy->dwNumRules++;
305 m_pEventPolicy->pRuleList = (NXC_EPP_RULE *)realloc(m_pEventPolicy->pRuleList,
306 sizeof(NXC_EPP_RULE) * m_pEventPolicy->dwNumRules);
307 if (iPos < (int)m_pEventPolicy->dwNumRules - 1)
308 memmove(&m_pEventPolicy->pRuleList[iPos + 1], &m_pEventPolicy->pRuleList[iPos],
309 sizeof(NXC_EPP_RULE) * ((int)m_pEventPolicy->dwNumRules - iPos - 1));
310
311 // Setup empty rule
312 memset(&m_pEventPolicy->pRuleList[iPos], 0, sizeof(NXC_EPP_RULE));
313 m_pEventPolicy->pRuleList[iPos].dwId = (DWORD)iPos;
314 m_pEventPolicy->pRuleList[iPos].dwFlags = ANY_SEVERITY;
315 m_pEventPolicy->pRuleList[iPos].pSituationAttrList = new StringMap;
316
317 // Insert new row into rule list view
318 m_wndRuleList.InsertRow(iPos);
319 UpdateRow(iPos);
320
321 // Renumber all rows below new
322 for(iPos++; iPos < (int)m_pEventPolicy->dwNumRules; iPos++)
323 {
324 _stprintf(szBuffer, _T("%d"), iPos + 1);
325 m_wndRuleList.ReplaceItem(iPos, 0, 0, szBuffer);
326 }
327
328 Modify();
329 }
330
331
332 //
333 // Update display row with data from in-memory policy
334 //
335
336 void CEventPolicyEditor::UpdateRow(int iRow)
337 {
338 TCHAR szBuffer[MAX_DB_STRING];
339 DWORD i;
340
341 // Rule number
342 _stprintf(szBuffer, _T("%d"), iRow + 1);
343 if (m_wndRuleList.GetNumItems(iRow, COL_RULE) == 0)
344 m_wndRuleList.AddItem(iRow, COL_RULE, szBuffer);
345 else
346 m_wndRuleList.ReplaceItem(iRow, COL_RULE, 0, szBuffer);
347
348 // Source list
349 m_wndRuleList.ClearCell(iRow, COL_SOURCE);
350 if (m_pEventPolicy->pRuleList[iRow].dwNumSources == 0)
351 {
352 m_wndRuleList.AddItem(iRow, COL_SOURCE, _T("Any"), m_iImageAny);
353 }
354 else
355 {
356 NXC_OBJECT *pObject;
357
358 for(i = 0; i < m_pEventPolicy->pRuleList[iRow].dwNumSources; i++)
359 {
360 pObject = NXCFindObjectById(g_hSession, m_pEventPolicy->pRuleList[iRow].pdwSourceList[i]);
361 if (pObject != NULL)
362 m_wndRuleList.AddItem(iRow, COL_SOURCE, pObject->szName,
363 GetObjectImageIndex(pObject));
364 }
365 }
366 m_wndRuleList.SetNegationFlag(iRow, COL_SOURCE, (m_pEventPolicy->pRuleList[iRow].dwFlags & RF_NEGATED_SOURCE) ? TRUE : FALSE);
367
368 // Event list
369 m_wndRuleList.ClearCell(iRow, COL_EVENT);
370 if (m_pEventPolicy->pRuleList[iRow].dwNumEvents == 0)
371 {
372 m_wndRuleList.AddItem(iRow, COL_EVENT, _T("Any"), m_iImageAny);
373 }
374 else
375 {
376 for(i = 0; i < m_pEventPolicy->pRuleList[iRow].dwNumEvents; i++)
377 m_wndRuleList.AddItem(iRow, COL_EVENT,
378 (TCHAR *)NXCGetEventName(g_hSession, m_pEventPolicy->pRuleList[iRow].pdwEventList[i]),
379 m_iImageSeverityBase + NXCGetEventSeverity(g_hSession, m_pEventPolicy->pRuleList[iRow].pdwEventList[i]));
380 }
381 m_wndRuleList.SetNegationFlag(iRow, COL_EVENT, (m_pEventPolicy->pRuleList[iRow].dwFlags & RF_NEGATED_EVENTS) ? TRUE : FALSE);
382
383 // Severity
384 m_wndRuleList.ClearCell(iRow, COL_SEVERITY);
385 if ((m_pEventPolicy->pRuleList[iRow].dwFlags & ANY_SEVERITY) == ANY_SEVERITY)
386 {
387 m_wndRuleList.AddItem(iRow, COL_SEVERITY, _T("Any"), m_iImageAny);
388 }
389 else
390 {
391 DWORD dwMask;
392
393 for(i = 0, dwMask = RF_SEVERITY_INFO; i < 5; i++, dwMask <<= 1)
394 if (m_pEventPolicy->pRuleList[iRow].dwFlags & dwMask)
395 m_wndRuleList.AddItem(iRow, COL_SEVERITY, g_szStatusTextSmall[i],
396 m_iImageSeverityBase + i);
397 }
398
399 // Script
400 m_wndRuleList.SetCellText(iRow, COL_SCRIPT, m_pEventPolicy->pRuleList[iRow].pszScript);
401
402 // Alarm
403 if (m_pEventPolicy->pRuleList[iRow].dwFlags & RF_GENERATE_ALARM)
404 {
405 if (m_wndRuleList.GetNumItems(iRow, COL_ALARM) == 0)
406 m_wndRuleList.AddItem(iRow, COL_ALARM,
407 (m_pEventPolicy->pRuleList[iRow].wAlarmSeverity == SEVERITY_TERMINATE) ? m_pEventPolicy->pRuleList[iRow].szAlarmKey : m_pEventPolicy->pRuleList[iRow].szAlarmMessage,
408 m_iImageSeverityBase + m_pEventPolicy->pRuleList[iRow].wAlarmSeverity);
409 else
410 m_wndRuleList.ReplaceItem(iRow, COL_ALARM, 0,
411 (m_pEventPolicy->pRuleList[iRow].wAlarmSeverity == SEVERITY_TERMINATE) ? m_pEventPolicy->pRuleList[iRow].szAlarmKey : m_pEventPolicy->pRuleList[iRow].szAlarmMessage,
412 m_iImageSeverityBase + m_pEventPolicy->pRuleList[iRow].wAlarmSeverity);
413 }
414 else
415 {
416 if (m_wndRuleList.GetNumItems(iRow, COL_ALARM) == 0)
417 m_wndRuleList.AddItem(iRow, COL_ALARM, _T("None"), m_iImageAny + 1);
418 else
419 m_wndRuleList.ReplaceItem(iRow, COL_ALARM, 0, _T("None"), m_iImageAny + 1);
420 }
421
422 // Action
423 m_wndRuleList.ClearCell(iRow, COL_ACTION);
424 if (m_pEventPolicy->pRuleList[iRow].dwNumActions > 0)
425 {
426 NXC_ACTION *pAction;
427
428 LockActions();
429 for(i = 0; i < m_pEventPolicy->pRuleList[iRow].dwNumActions; i++)
430 {
431 pAction = FindActionById(m_pEventPolicy->pRuleList[iRow].pdwActionList[i]);
432 if (pAction != NULL)
433 m_wndRuleList.AddItem(iRow, COL_ACTION, pAction->szName,
434 m_iImageActionsBase + pAction->iType);
435 }
436 UnlockActions();
437 }
438 else
439 {
440 m_wndRuleList.AddItem(iRow, COL_ACTION, _T("None"), m_iImageAny + 1);
441 }
442
443 // Options
444 m_wndRuleList.ClearCell(iRow, COL_OPTIONS);
445 if (m_pEventPolicy->pRuleList[iRow].dwFlags & RF_STOP_PROCESSING)
446 {
447 m_wndRuleList.AddItem(iRow, COL_OPTIONS, _T("Stop processing"), m_iImageOptionsBase);
448 }
449 if (m_wndRuleList.GetNumItems(iRow, COL_OPTIONS) == 0)
450 {
451 m_wndRuleList.AddItem(iRow, COL_OPTIONS, _T("None"), m_iImageAny + 1);
452 }
453
454 // Situation
455 m_wndRuleList.ClearCell(iRow, COL_SITUATION);
456 if (m_pEventPolicy->pRuleList[iRow].dwSituationId != 0)
457 {
458 m_wndRuleList.AddItem(iRow, COL_SITUATION,
459 theApp.GetSituationName(m_pEventPolicy->pRuleList[iRow].dwSituationId, szBuffer),
460 m_iImageAny + 2);
461 m_wndRuleList.AddItem(iRow, COL_SITUATION,
462 m_pEventPolicy->pRuleList[iRow].szSituationInstance,
463 m_iImageAny + 3);
464 }
465 else
466 {
467 m_wndRuleList.AddItem(iRow, COL_SITUATION, _T("None"), m_iImageAny + 1);
468 }
469
470 // Comment
471 m_wndRuleList.SetCellText(iRow, COL_COMMENT, m_pEventPolicy->pRuleList[iRow].pszComment);
472
473 // Enable/disable selection
474 m_wndRuleList.EnableCellSelection(iRow, COL_SOURCE, m_pEventPolicy->pRuleList[iRow].dwNumSources != 0);
475 m_wndRuleList.EnableCellSelection(iRow, COL_EVENT, m_pEventPolicy->pRuleList[iRow].dwNumEvents != 0);
476 m_wndRuleList.EnableCellSelection(iRow, COL_ACTION, m_pEventPolicy->pRuleList[iRow].dwNumActions != 0);
477 }
478
479
480 //
481 // WM_COMMAND::ID_POLICY_INSERTRULE_TOP message handler
482 //
483
484 void CEventPolicyEditor::OnPolicyInsertruleTop()
485 {
486 InsertNewRule(0);
487 }
488
489
490 //
491 // WM_COMMAND::ID_POLICY_INSERTRULE_BOTTOM message handler
492 //
493
494 void CEventPolicyEditor::OnPolicyInsertruleBottom()
495 {
496 InsertNewRule(0x7FFFFFFF);
497 }
498
499
500 //
501 // WM_COMMAND::ID_POLICY_INSERTRULE_ABOVE message handler
502 //
503
504 void CEventPolicyEditor::OnPolicyInsertruleAbove()
505 {
506 InsertNewRule(m_wndRuleList.GetCurrentRow());
507 }
508
509
510 //
511 // WM_COMMAND::ID_POLICY_INSERTRULE_BELOW message handler
512 //
513
514 void CEventPolicyEditor::OnPolicyInsertruleBelow()
515 {
516 InsertNewRule(m_wndRuleList.GetCurrentRow() + 1);
517 }
518
519
520 //
521 // Enable or disable selected rows
522 //
523
524 void CEventPolicyEditor::EnableSelectedRows(BOOL bEnable)
525 {
526 int iRow;
527
528 iRow = m_wndRuleList.GetNextRow(-1, RLF_SELECTED);
529 if (iRow != -1)
530 {
531 while(iRow != -1)
532 {
533 if (bEnable)
534 m_pEventPolicy->pRuleList[iRow].dwFlags &= ~RF_DISABLED;
535 else
536 m_pEventPolicy->pRuleList[iRow].dwFlags |= RF_DISABLED;
537 m_wndRuleList.EnableRow(iRow, bEnable);
538 iRow = m_wndRuleList.GetNextRow(iRow, RLF_SELECTED);
539 }
540 Modify();
541 }
542 }
543
544
545 //
546 // WM_COMMAND::ID_POLICY_DISABLERULE message handler
547 //
548
549 void CEventPolicyEditor::OnPolicyDisablerule()
550 {
551 EnableSelectedRows(FALSE);
552 }
553
554
555 //
556 // WM_COMMAND::ID_POLICY_ENABLERULE message handler
557 //
558
559 void CEventPolicyEditor::OnPolicyEnablerule()
560 {
561 EnableSelectedRows(TRUE);
562 }
563
564
565 //
566 // WM_COMMAND::ID_POLICY_ADD message handler
567 //
568
569 void CEventPolicyEditor::OnPolicyAdd()
570 {
571 switch(m_wndRuleList.GetCurrentColumn())
572 {
573 case COL_SOURCE:
574 AddSource();
575 break;
576 case COL_EVENT:
577 AddEvent();
578 break;
579 case COL_ACTION:
580 AddAction();
581 break;
582 }
583 }
584
585
586 //
587 // ON_COMMAND_UPDATE_UI handlers
588 //
589
590 void CEventPolicyEditor::OnUpdatePolicyInsertruleBelow(CCmdUI* pCmdUI)
591 {
592 pCmdUI->Enable(m_wndRuleList.GetCurrentRow() != -1);
593 }
594
595 void CEventPolicyEditor::OnUpdatePolicyInsertruleAbove(CCmdUI* pCmdUI)
596 {
597 pCmdUI->Enable(m_wndRuleList.GetCurrentRow() != -1);
598 }
599
600 void CEventPolicyEditor::OnUpdatePolicyNegatecell(CCmdUI* pCmdUI)
601 {
602 int nRow, nCol;
603
604 // Negation available only for source, event and severity columns
605 nCol = m_wndRuleList.GetCurrentColumn();
606 if ((nCol == COL_SOURCE) || (nCol == COL_EVENT))
607 {
608 nRow = m_wndRuleList.GetCurrentRow();
609 pCmdUI->Enable(TRUE);
610 if ((nRow >= 0) && (nRow < (int)m_pEventPolicy->dwNumRules))
611 {
612 pCmdUI->SetCheck((m_pEventPolicy->pRuleList[nRow].dwFlags &
613 ((nCol == COL_SOURCE) ? RF_NEGATED_SOURCE : RF_NEGATED_EVENTS)) ? TRUE : FALSE);
614 }
615 else
616 {
617 pCmdUI->SetCheck(FALSE);
618 }
619 }
620 else
621 {
622 pCmdUI->Enable(FALSE);
623 pCmdUI->SetCheck(FALSE);
624 }
625 }
626
627 void CEventPolicyEditor::OnUpdatePolicyDisablerule(CCmdUI* pCmdUI)
628 {
629 pCmdUI->Enable(m_wndRuleList.GetCurrentRow() != -1);
630 }
631
632 void CEventPolicyEditor::OnUpdatePolicyDeleterule(CCmdUI* pCmdUI)
633 {
634 pCmdUI->Enable(m_wndRuleList.GetCurrentRow() != -1);
635 }
636
637 void CEventPolicyEditor::OnUpdatePolicyEnablerule(CCmdUI* pCmdUI)
638 {
639 pCmdUI->Enable(m_wndRuleList.GetCurrentRow() != -1);
640 }
641
642 void CEventPolicyEditor::OnUpdatePolicyAdd(CCmdUI* pCmdUI)
643 {
644 int iColumn;
645
646 iColumn = m_wndRuleList.GetCurrentColumn();
647 pCmdUI->Enable((m_wndRuleList.GetCurrentRow() != -1) &&
648 ((iColumn == COL_SOURCE) || (iColumn == COL_EVENT) ||
649 (iColumn == COL_SEVERITY) || (iColumn == COL_ACTION)));
650 }
651
652 void CEventPolicyEditor::OnUpdatePolicyDelete(CCmdUI* pCmdUI)
653 {
654 pCmdUI->Enable(m_wndRuleList.GetCurrentItem() != -1);
655 }
656
657 void CEventPolicyEditor::OnUpdatePolicySave(CCmdUI* pCmdUI)
658 {
659 pCmdUI->Enable(m_bIsModified);
660 }
661
662
663 //
664 // Add new source object to current row
665 //
666
667 void CEventPolicyEditor::AddSource(void)
668 {
669 CObjectSelDlg dlg;
670 DWORD i, j;
671 int iRow;
672
673 dlg.m_dwAllowedClasses = SCL_NODE | SCL_CLUSTER | SCL_CONTAINER | SCL_SUBNET | SCL_NETWORK | SCL_SERVICEROOT;
674 if (dlg.DoModal() == IDOK)
675 {
676 iRow = m_wndRuleList.GetCurrentRow();
677 for(i = 0; i < dlg.m_dwNumObjects; i++)
678 {
679 // Check if object already in the list
680 for(j = 0; j < m_pEventPolicy->pRuleList[iRow].dwNumSources; j++)
681 if (m_pEventPolicy->pRuleList[iRow].pdwSourceList[j] == dlg.m_pdwObjectList[i])
682 break;
683 if (j == m_pEventPolicy->pRuleList[iRow].dwNumSources)
684 {
685 // New object, add it to source list
686 m_pEventPolicy->pRuleList[iRow].dwNumSources++;
687 m_pEventPolicy->pRuleList[iRow].pdwSourceList =
688 (DWORD *)realloc(m_pEventPolicy->pRuleList[iRow].pdwSourceList,
689 sizeof(DWORD) * m_pEventPolicy->pRuleList[iRow].dwNumSources);
690 m_pEventPolicy->pRuleList[iRow].pdwSourceList[j] = dlg.m_pdwObjectList[i];
691 Modify();
692 }
693 }
694 UpdateRow(iRow);
695 Modify();
696 }
697 }
698
699
700 //
701 // Add new event to current rule
702 //
703
704 void CEventPolicyEditor::AddEvent(void)
705 {
706 CEventSelDlg dlg;
707 DWORD i, j;
708 int iRow;
709
710 if (dlg.DoModal() == IDOK)
711 {
712 iRow = m_wndRuleList.GetCurrentRow();
713 for(i = 0; i < dlg.m_dwNumEvents; i++)
714 {
715 // Check if object already in the list
716 for(j = 0; j < m_pEventPolicy->pRuleList[iRow].dwNumEvents; j++)
717 if (m_pEventPolicy->pRuleList[iRow].pdwEventList[j] == dlg.m_pdwEventList[i])
718 break;
719 if (j == m_pEventPolicy->pRuleList[iRow].dwNumEvents)
720 {
721 // New object, add it to source list
722 m_pEventPolicy->pRuleList[iRow].dwNumEvents++;
723 m_pEventPolicy->pRuleList[iRow].pdwEventList =
724 (DWORD *)realloc(m_pEventPolicy->pRuleList[iRow].pdwEventList,
725 sizeof(DWORD) * m_pEventPolicy->pRuleList[iRow].dwNumEvents);
726 m_pEventPolicy->pRuleList[iRow].pdwEventList[j] = dlg.m_pdwEventList[i];
727 Modify();
728 }
729 }
730 UpdateRow(iRow);
731 Modify();
732 }
733 }
734
735
736 //
737 // Delete element
738 //
739
740 void CEventPolicyEditor::OnPolicyDelete(void)
741 {
742 int iRow, iCol, iItem;
743
744 iRow = m_wndRuleList.GetCurrentRow();
745 iCol = m_wndRuleList.GetCurrentColumn();
746 iItem = m_wndRuleList.GetCurrentItem();
747 if (iItem != -1)
748 {
749 switch(iCol)
750 {
751 case COL_SOURCE: // Source
752 if (m_pEventPolicy->pRuleList[iRow].dwNumSources > 0)
753 {
754 m_pEventPolicy->pRuleList[iRow].dwNumSources--;
755 memmove(&m_pEventPolicy->pRuleList[iRow].pdwSourceList[iItem],
756 &m_pEventPolicy->pRuleList[iRow].pdwSourceList[iItem + 1],
757 sizeof(DWORD) * (m_pEventPolicy->pRuleList[iRow].dwNumSources - iItem));
758 if (m_pEventPolicy->pRuleList[iRow].dwNumSources == 0)
759 {
760 m_wndRuleList.ReplaceItem(iRow, iCol, 0, _T("Any"), m_iImageAny);
761 m_wndRuleList.EnableCellSelection(iRow, iCol, FALSE);
762 }
763 else
764 {
765 m_wndRuleList.DeleteItem(iRow, iCol, iItem);
766 }
767 Modify();
768 }
769 break;
770 case COL_EVENT: // Event
771 if (m_pEventPolicy->pRuleList[iRow].dwNumEvents > 0)
772 {
773 m_pEventPolicy->pRuleList[iRow].dwNumEvents--;
774 memmove(&m_pEventPolicy->pRuleList[iRow].pdwEventList[iItem],
775 &m_pEventPolicy->pRuleList[iRow].pdwEventList[iItem + 1],
776 sizeof(DWORD) * (m_pEventPolicy->pRuleList[iRow].dwNumEvents - iItem));
777 if (m_pEventPolicy->pRuleList[iRow].dwNumEvents == 0)
778 {
779 m_wndRuleList.ReplaceItem(iRow, iCol, 0, _T("Any"), m_iImageAny);
780 m_wndRuleList.EnableCellSelection(iRow, iCol, FALSE);
781 }
782 else
783 {
784 m_wndRuleList.DeleteItem(iRow, iCol, iItem);
785 }
786 Modify();
787 }
788 break;
789 case COL_ACTION:
790 if (m_pEventPolicy->pRuleList[iRow].dwNumActions > 0)
791 {
792 m_pEventPolicy->pRuleList[iRow].dwNumActions--;
793 memmove(&m_pEventPolicy->pRuleList[iRow].pdwActionList[iItem],
794 &m_pEventPolicy->pRuleList[iRow].pdwActionList[iItem + 1],
795 sizeof(DWORD) * (m_pEventPolicy->pRuleList[iRow].dwNumActions - iItem));
796 if (m_pEventPolicy->pRuleList[iRow].dwNumActions == 0)
797 {
798 m_wndRuleList.ReplaceItem(iRow, iCol, 0, _T("None"), m_iImageAny + 1);
799 m_wndRuleList.EnableCellSelection(iRow, iCol, FALSE);
800 }
801 else
802 {
803 m_wndRuleList.DeleteItem(iRow, iCol, iItem);
804 }
805 Modify();
806 }
807 break;
808 }
809 }
810 }
811
812
813 //
814 // Delete selected rule(s)
815 //
816
817 void CEventPolicyEditor::OnPolicyDeleterule(void)
818 {
819 int i, iRow;
820 TCHAR szBuffer[32];
821
822 iRow = m_wndRuleList.GetNextRow(-1, RLF_SELECTED);
823 if (iRow != -1)
824 {
825 while(iRow != -1)
826 {
827 m_wndRuleList.DeleteRow(iRow);
828 NXCDeletePolicyRule(m_pEventPolicy, iRow);
829 for(i = iRow; i < (int)m_pEventPolicy->dwNumRules; i++)
830 {
831 _stprintf(szBuffer, _T("%d"), i + 1);
832 m_wndRuleList.ReplaceItem(i, 0, 0, szBuffer);
833 }
834 iRow = m_wndRuleList.GetNextRow(iRow - 1, RLF_SELECTED);
835 }
836 Modify();
837 }
838 }
839
840
841 //
842 // Edit current cell
843 //
844
845 void CEventPolicyEditor::OnPolicyEdit()
846 {
847 int iRow, iColumn;
848
849 iRow = m_wndRuleList.GetCurrentRow();
850 iColumn = m_wndRuleList.GetCurrentColumn();
851 if (iRow != -1)
852 switch(iColumn)
853 {
854 case COL_RULE:
855 EnableSelectedRows((m_pEventPolicy->pRuleList[iRow].dwFlags & RF_DISABLED) ? TRUE : FALSE);
856 break;
857 case COL_SEVERITY:
858 EditSeverity(iRow);
859 break;
860 case COL_SCRIPT:
861 EditScript(iRow);
862 break;
863 case COL_ALARM:
864 EditAlarm(iRow);
865 break;
866 case COL_SITUATION:
867 EditSituation(iRow);
868 break;
869 case COL_OPTIONS:
870 EditOptions(iRow);
871 break;
872 case COL_COMMENT:
873 EditComment(iRow);
874 break;
875 default:
876 break;
877 }
878 }
879
880
881 //
882 // Edit severity cell
883 //
884
885 void CEventPolicyEditor::EditSeverity(int iRow)
886 {
887 CRuleSeverityDlg dlg;
888
889 dlg.m_bNormal = (m_pEventPolicy->pRuleList[iRow].dwFlags & RF_SEVERITY_INFO) ? TRUE : FALSE;
890 dlg.m_bWarning = (m_pEventPolicy->pRuleList[iRow].dwFlags & RF_SEVERITY_WARNING) ? TRUE : FALSE;
891 dlg.m_bMinor = (m_pEventPolicy->pRuleList[iRow].dwFlags & RF_SEVERITY_MINOR) ? TRUE : FALSE;
892 dlg.m_bMajor = (m_pEventPolicy->pRuleList[iRow].dwFlags & RF_SEVERITY_MAJOR) ? TRUE : FALSE;
893 dlg.m_bCritical = (m_pEventPolicy->pRuleList[iRow].dwFlags & RF_SEVERITY_CRITICAL) ? TRUE : FALSE;
894 if (dlg.DoModal() == IDOK)
895 {
896 DWORD dwSeverity = 0;
897
898 if (dlg.m_bNormal)
899 dwSeverity |= RF_SEVERITY_INFO;
900 if (dlg.m_bWarning)
901 dwSeverity |= RF_SEVERITY_WARNING;
902 if (dlg.m_bMinor)
903 dwSeverity |= RF_SEVERITY_MINOR;
904 if (dlg.m_bMajor)
905 dwSeverity |= RF_SEVERITY_MAJOR;
906 if (dlg.m_bCritical)
907 dwSeverity |= RF_SEVERITY_CRITICAL;
908
909 m_pEventPolicy->pRuleList[iRow].dwFlags &= ~(ANY_SEVERITY);
910 m_pEventPolicy->pRuleList[iRow].dwFlags |= dwSeverity;
911 Modify();
912 UpdateRow(iRow);
913 }
914 }
915
916
917 //
918 // Edit alarm cell
919 //
920
921 void CEventPolicyEditor::EditAlarm(int iRow)
922 {
923 CRuleAlarmDlg dlg;
924
925 dlg.m_nMode = (m_pEventPolicy->pRuleList[iRow].dwFlags & RF_GENERATE_ALARM) ? ((m_pEventPolicy->pRuleList[iRow].wAlarmSeverity == SEVERITY_TERMINATE) ? 1 : 0) : -1;
926 if (dlg.m_nMode == 0)
927 {
928 dlg.m_strMessage = m_pEventPolicy->pRuleList[iRow].szAlarmMessage;
929 dlg.m_strKey = m_pEventPolicy->pRuleList[iRow].szAlarmKey;
930 dlg.m_iSeverity = m_pEventPolicy->pRuleList[iRow].wAlarmSeverity;
931 dlg.m_dwTimeout = m_pEventPolicy->pRuleList[iRow].dwAlarmTimeout;
932 dlg.m_dwTimeoutEvent = m_pEventPolicy->pRuleList[iRow].dwAlarmTimeoutEvent;
933 }
934 else if (dlg.m_nMode == 1)
935 {
936 dlg.m_strAckKey = m_pEventPolicy->pRuleList[iRow].szAlarmKey;
937 }
938 if (dlg.DoModal() == IDOK)
939 {
940 if (dlg.m_nMode != -1)
941 {
942 m_pEventPolicy->pRuleList[iRow].dwFlags |= RF_GENERATE_ALARM;
943 if (dlg.m_nMode == 0)
944 {
945 m_pEventPolicy->pRuleList[iRow].wAlarmSeverity = dlg.m_iSeverity;
946 nx_strncpy(m_pEventPolicy->pRuleList[iRow].szAlarmMessage, (LPCTSTR)dlg.m_strMessage, MAX_DB_STRING);
947 nx_strncpy(m_pEventPolicy->pRuleList[iRow].szAlarmKey, (LPCTSTR)dlg.m_strKey, MAX_DB_STRING);
948 m_pEventPolicy->pRuleList[iRow].dwAlarmTimeout = dlg.m_dwTimeout;
949 m_pEventPolicy->pRuleList[iRow].dwAlarmTimeoutEvent = dlg.m_dwTimeoutEvent;
950 }
951 else
952 {
953 m_pEventPolicy->pRuleList[iRow].wAlarmSeverity = SEVERITY_TERMINATE;
954 nx_strncpy(m_pEventPolicy->pRuleList[iRow].szAlarmKey, (LPCTSTR)dlg.m_strAckKey, MAX_DB_STRING);
955 }
956 }
957 else
958 {
959 m_pEventPolicy->pRuleList[iRow].dwFlags &= ~RF_GENERATE_ALARM;
960 }
961 Modify();
962 UpdateRow(iRow);
963 }
964 }
965
966
967 //
968 // Edit situation cell
969 //
970
971 void CEventPolicyEditor::EditSituation(int row)
972 {
973 CRuleSituationDlg dlg;
974
975 dlg.m_dwSituation = m_pEventPolicy->pRuleList[row].dwSituationId;
976 dlg.m_strInstance = m_pEventPolicy->pRuleList[row].szSituationInstance;
977 dlg.m_attrList = *m_pEventPolicy->pRuleList[row].pSituationAttrList;
978 if (dlg.DoModal() == IDOK)
979 {
980 m_pEventPolicy->pRuleList[row].dwSituationId = dlg.m_dwSituation;
981 nx_strncpy(m_pEventPolicy->pRuleList[row].szSituationInstance, (LPCTSTR)dlg.m_strInstance, MAX_DB_STRING);
982 *m_pEventPolicy->pRuleList[row].pSituationAttrList = dlg.m_attrList;
983 Modify();
984 UpdateRow(row);
985 }
986 }
987
988
989 //
990 // Edit comment cell
991 //
992
993 void CEventPolicyEditor::EditComment(int iRow)
994 {
995 CRuleCommentDlg dlg;
996
997 if (m_pEventPolicy->pRuleList[iRow].pszComment != NULL)
998 dlg.m_strText = m_pEventPolicy->pRuleList[iRow].pszComment;
999 if (dlg.DoModal() == IDOK)
1000 {
1001 safe_free(m_pEventPolicy->pRuleList[iRow].pszComment);
1002 m_pEventPolicy->pRuleList[iRow].pszComment = _tcsdup((LPCTSTR)dlg.m_strText);
1003 Modify();
1004 UpdateRow(iRow);
1005 }
1006 }
1007
1008
1009 //
1010 // Edit script cell
1011 //
1012
1013 void CEventPolicyEditor::EditScript(int iRow)
1014 {
1015 CRuleScriptDlg dlg;
1016
1017 if (m_pEventPolicy->pRuleList[iRow].pszScript != NULL)
1018 dlg.m_strText = m_pEventPolicy->pRuleList[iRow].pszScript;
1019 if (dlg.DoModal() == IDOK)
1020 {
1021 safe_free(m_pEventPolicy->pRuleList[iRow].pszScript);
1022 m_pEventPolicy->pRuleList[iRow].pszScript = _tcsdup((LPCTSTR)dlg.m_strText);
1023 Modify();
1024 UpdateRow(iRow);
1025 }
1026 }
1027
1028
1029 //
1030 // Edit options cell
1031 //
1032
1033 void CEventPolicyEditor::EditOptions(int row)
1034 {
1035 CRuleOptionsDlg dlg;
1036
1037 dlg.m_bStopProcessing = (m_pEventPolicy->pRuleList[row].dwFlags & RF_STOP_PROCESSING) ? TRUE : FALSE;
1038 if (dlg.DoModal() == IDOK)
1039 {
1040 if (dlg.m_bStopProcessing)
1041 m_pEventPolicy->pRuleList[row].dwFlags |= RF_STOP_PROCESSING;
1042 else
1043 m_pEventPolicy->pRuleList[row].dwFlags &= ~RF_STOP_PROCESSING;
1044 Modify();
1045 UpdateRow(row);
1046 }
1047 }
1048
1049
1050 //
1051 // Add new action to rule
1052 //
1053
1054 void CEventPolicyEditor::AddAction()
1055 {
1056 CActionSelDlg dlg;
1057 DWORD i, j;
1058 int iRow;
1059
1060 if (dlg.DoModal() == IDOK)
1061 {
1062 iRow = m_wndRuleList.GetCurrentRow();
1063 for(i = 0; i < dlg.m_dwNumActions; i++)
1064 {
1065 // Check if object already in the list
1066 for(j = 0; j < m_pEventPolicy->pRuleList[iRow].dwNumActions; j++)
1067 if (m_pEventPolicy->pRuleList[iRow].pdwActionList[j] == dlg.m_pdwActionList[i])
1068 break;
1069 if (j == m_pEventPolicy->pRuleList[iRow].dwNumActions)
1070 {
1071 // New object, add it to source list
1072 m_pEventPolicy->pRuleList[iRow].dwNumActions++;
1073 m_pEventPolicy->pRuleList[iRow].pdwActionList =
1074 (DWORD *)realloc(m_pEventPolicy->pRuleList[iRow].pdwActionList,
1075 sizeof(DWORD) * m_pEventPolicy->pRuleList[iRow].dwNumActions);
1076 m_pEventPolicy->pRuleList[iRow].pdwActionList[j] = dlg.m_pdwActionList[i];
1077 Modify();
1078 }
1079 }
1080 UpdateRow(iRow);
1081 Modify();
1082 }
1083 }
1084
1085
1086 //
1087 // Modify frame title when policy was modified
1088 //
1089
1090 void CEventPolicyEditor::ModifyTitle()
1091 {
1092 SetTitle(GetTitle() + _T(" (Modified)"));
1093 OnUpdateFrameTitle(TRUE);
1094 }
1095
1096
1097 //
1098 // WM_COMMAND::ID_POLICY_SAVE message handler
1099 //
1100
1101 void CEventPolicyEditor::OnPolicySave()
1102 {
1103 DWORD dwResult;
1104
1105 dwResult = DoRequestArg2(NXCSaveEventPolicy, g_hSession, m_pEventPolicy, _T("Saving event processing policy..."));
1106 if (dwResult != RCC_SUCCESS)
1107 {
1108 theApp.ErrorBox(dwResult, _T("Error saving event processing policy: %s"));
1109 }
1110 else
1111 {
1112 TCHAR szBuffer[256], *ptr;
1113
1114 m_bIsModified = FALSE;
1115 ::LoadString(theApp.m_hInstance, IDR_EPP_EDITOR, szBuffer, 256);
1116 ptr = _tcschr(&szBuffer[1], _T('\n'));
1117 if (ptr != NULL)
1118 *ptr = 0;
1119 SetTitle(&szBuffer[1]);
1120 OnUpdateFrameTitle(TRUE);
1121 }
1122 }
1123
1124
1125 //
1126 // Handler for double click inside rule list
1127 //
1128
1129 void CEventPolicyEditor::OnRuleListDblClk(LPNMHDR pNMHDR, LRESULT *pResult)
1130 {
1131 PostMessage(WM_COMMAND, ID_POLICY_EDIT, 0);
1132 }
1133
1134
1135 //
1136 // WM_COMMAND::ID_POLICY_NEGATECELL message handler
1137 //
1138
1139 void CEventPolicyEditor::OnPolicyNegatecell()
1140 {
1141 int nRow;
1142
1143 nRow = m_wndRuleList.GetCurrentRow();
1144 switch(m_wndRuleList.GetCurrentColumn())
1145 {
1146 case COL_SOURCE:
1147 if (m_pEventPolicy->pRuleList[nRow].dwFlags & RF_NEGATED_SOURCE)
1148 {
1149 m_pEventPolicy->pRuleList[nRow].dwFlags &= ~RF_NEGATED_SOURCE;
1150 m_wndRuleList.SetNegationFlag(nRow, COL_SOURCE, FALSE);
1151 }
1152 else
1153 {
1154 m_pEventPolicy->pRuleList[nRow].dwFlags |= RF_NEGATED_SOURCE;
1155 m_wndRuleList.SetNegationFlag(nRow, COL_SOURCE, TRUE);
1156 }
1157 Modify();
1158 break;
1159 case COL_EVENT:
1160 if (m_pEventPolicy->pRuleList[nRow].dwFlags & RF_NEGATED_EVENTS)
1161 {
1162 m_pEventPolicy->pRuleList[nRow].dwFlags &= ~RF_NEGATED_EVENTS;
1163 m_wndRuleList.SetNegationFlag(nRow, COL_EVENT, FALSE);
1164 }
1165 else
1166 {
1167 m_pEventPolicy->pRuleList[nRow].dwFlags |= RF_NEGATED_EVENTS;
1168 m_wndRuleList.SetNegationFlag(nRow, COL_EVENT, TRUE);
1169 }
1170 Modify();
1171 break;
1172 default:
1173 break;
1174 }
1175 }