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