56b6b8dc1daa1084430039b7bcbd3b75ce86e22a
[public/netxms.git] / src / console / win32 / EventEditor.cpp
1 // EventEditor.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "nxcon.h"
6 #include "EventEditor.h"
7 #include "EditEventDlg.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 // CEventEditor
17
18 IMPLEMENT_DYNCREATE(CEventEditor, CMDIChildWnd)
19
20 CEventEditor::CEventEditor()
21 {
22 m_pImageList = NULL;
23 }
24
25 CEventEditor::~CEventEditor()
26 {
27 delete m_pImageList;
28 }
29
30
31 BEGIN_MESSAGE_MAP(CEventEditor, CMDIChildWnd)
32 //{{AFX_MSG_MAP(CEventEditor)
33 ON_WM_CREATE()
34 ON_WM_DESTROY()
35 ON_WM_SIZE()
36 ON_WM_SETFOCUS()
37 ON_WM_CLOSE()
38 ON_WM_CONTEXTMENU()
39 ON_UPDATE_COMMAND_UI(ID_EVENT_EDIT, OnUpdateEventEdit)
40 ON_COMMAND(ID_EVENT_EDIT, OnEventEdit)
41 ON_UPDATE_COMMAND_UI(ID_EVENT_DELETE, OnUpdateEventDelete)
42 ON_COMMAND(ID_EVENT_NEW, OnEventNew)
43 ON_COMMAND(ID_EVENT_DELETE, OnEventDelete)
44 //}}AFX_MSG_MAP
45 ON_NOTIFY(NM_DBLCLK, IDC_LIST_VIEW, OnListViewDoubleClick)
46 END_MESSAGE_MAP()
47
48 //
49 // CEventEditor message handlers
50 //
51
52 int CEventEditor::OnCreate(LPCREATESTRUCT lpCreateStruct)
53 {
54 RECT rect;
55 TCHAR szBuffer[32];
56 DWORD i;
57
58 if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
59 return -1;
60
61 theApp.OnViewCreate(IDR_EVENT_EDITOR, this);
62
63 // Create list view control
64 GetClientRect(&rect);
65 m_wndListCtrl.Create(WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SHOWSELALWAYS, rect, this, IDC_LIST_VIEW);
66 m_wndListCtrl.SetExtendedStyle(LVS_EX_TRACKSELECT | LVS_EX_UNDERLINEHOT |
67 LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
68 m_wndListCtrl.SetHoverTime(0x7FFFFFFF);
69
70 // Setup columns
71 m_wndListCtrl.InsertColumn(0, _T("ID"), LVCFMT_LEFT, 60);
72 m_wndListCtrl.InsertColumn(1, _T("Name"), LVCFMT_LEFT, 190);
73 m_wndListCtrl.InsertColumn(2, _T("Severity"), LVCFMT_LEFT, 70);
74 m_wndListCtrl.InsertColumn(3, _T("Flags"), LVCFMT_LEFT, 40);
75 m_wndListCtrl.InsertColumn(4, _T("Message"), LVCFMT_LEFT, 300);
76 m_wndListCtrl.InsertColumn(5, _T("Description"), LVCFMT_LEFT, 300);
77
78 // Create image list
79 m_pImageList = CreateEventImageList();
80 m_wndListCtrl.SetImageList(m_pImageList, LVSIL_SMALL);
81
82 // Load event templates
83 NXCGetEventDB(&m_ppEventTemplates, &m_dwNumTemplates);
84 for(i = 0; i < m_dwNumTemplates; i++)
85 {
86 _stprintf(szBuffer, _T("%ld"), m_ppEventTemplates[i]->dwCode);
87 m_wndListCtrl.InsertItem(i, szBuffer, m_ppEventTemplates[i]->dwSeverity);
88 m_wndListCtrl.SetItemData(i, m_ppEventTemplates[i]->dwCode);
89 UpdateItem(i, m_ppEventTemplates[i]);
90 }
91
92 return 0;
93 }
94
95
96 //
97 // Update list view item
98 //
99
100 void CEventEditor::UpdateItem(int iItem, NXC_EVENT_TEMPLATE *pData)
101 {
102 TCHAR szBuffer[32];
103
104 m_wndListCtrl.SetItem(iItem, 0, LVIF_IMAGE, NULL, pData->dwSeverity, 0, 0, 0);
105 m_wndListCtrl.SetItemText(iItem, 1, pData->szName);
106 m_wndListCtrl.SetItemText(iItem, 2, g_szStatusTextSmall[pData->dwSeverity]);
107 _stprintf(szBuffer, _T("%ld"), pData->dwFlags);
108 m_wndListCtrl.SetItemText(iItem, 3, szBuffer);
109 m_wndListCtrl.SetItemText(iItem, 4, pData->pszMessage);
110 m_wndListCtrl.SetItemText(iItem, 5, pData->pszDescription);
111 }
112
113
114 //
115 // WM_DESTROY message handler
116 //
117
118 void CEventEditor::OnDestroy()
119 {
120 theApp.OnViewDestroy(IDR_EVENT_EDITOR, this);
121 CMDIChildWnd::OnDestroy();
122 }
123
124
125 //
126 // WM_SIZE message handler
127 //
128
129 void CEventEditor::OnSize(UINT nType, int cx, int cy)
130 {
131 CMDIChildWnd::OnSize(nType, cx, cy);
132
133 m_wndListCtrl.SetWindowPos(NULL, 0, 0, cx, cy, SWP_NOZORDER);
134 }
135
136
137 //
138 // Handler for double-clicks in list view
139 //
140
141 void CEventEditor::OnListViewDoubleClick(NMITEMACTIVATE *pInfo, LRESULT *pResult)
142 {
143 if (pInfo->iItem != -1)
144 EditEvent(pInfo->iItem);
145 }
146
147
148 //
149 // Edit currently selected event
150 //
151
152 BOOL CEventEditor::EditEvent(int iItem)
153 {
154 DWORD dwIndex, dwResult, dwId;
155 CEditEventDlg dlgEditEvent;
156 BOOL bResult = FALSE;
157
158 dwId = m_wndListCtrl.GetItemData(iItem);
159 for(dwIndex = 0; dwIndex < m_dwNumTemplates; dwIndex++)
160 if (m_ppEventTemplates[dwIndex]->dwCode == dwId)
161 break;
162
163 if (dwIndex < m_dwNumTemplates)
164 {
165 dlgEditEvent.m_bWriteLog = m_ppEventTemplates[dwIndex]->dwFlags & EF_LOG;
166 dlgEditEvent.m_dwEventId = m_ppEventTemplates[dwIndex]->dwCode;
167 dlgEditEvent.m_strName = m_ppEventTemplates[dwIndex]->szName;
168 dlgEditEvent.m_strMessage = m_ppEventTemplates[dwIndex]->pszMessage;
169 dlgEditEvent.m_strDescription = m_ppEventTemplates[dwIndex]->pszDescription;
170 dlgEditEvent.m_dwSeverity = m_ppEventTemplates[dwIndex]->dwSeverity;
171
172 if (dlgEditEvent.DoModal() == IDOK)
173 {
174 NXC_EVENT_TEMPLATE evt;
175
176 evt.dwCode = m_ppEventTemplates[dwIndex]->dwCode;
177 evt.dwFlags = dlgEditEvent.m_bWriteLog ? EF_LOG : 0;
178 evt.dwSeverity = dlgEditEvent.m_dwSeverity;
179 evt.pszDescription = _tcsdup((LPCTSTR)dlgEditEvent.m_strDescription);
180 evt.pszMessage = _tcsdup((LPCTSTR)dlgEditEvent.m_strMessage);
181 _tcsncpy(evt.szName, (LPCTSTR)dlgEditEvent.m_strName, MAX_EVENT_NAME);
182
183 dwResult = DoRequestArg1(NXCSetEventInfo, &evt, _T("Updating event configuration database..."));
184 if (dwResult == RCC_SUCCESS)
185 {
186 // Record was successfully updated on server, update local copy
187 safe_free(m_ppEventTemplates[dwIndex]->pszDescription);
188 safe_free(m_ppEventTemplates[dwIndex]->pszMessage);
189 memcpy(m_ppEventTemplates[dwIndex], &evt, sizeof(NXC_EVENT_TEMPLATE));
190 UpdateItem(iItem, m_ppEventTemplates[dwIndex]);
191 bResult = TRUE;
192 }
193 else
194 {
195 theApp.ErrorBox(dwResult, _T("Unable to update event configuration record: %s"));
196 safe_free(evt.pszDescription);
197 safe_free(evt.pszMessage);
198 }
199 }
200 }
201 return bResult;
202 }
203
204
205 //
206 // WM_SETFOCUS message handler
207 //
208
209 void CEventEditor::OnSetFocus(CWnd* pOldWnd)
210 {
211 CMDIChildWnd::OnSetFocus(pOldWnd);
212 m_wndListCtrl.SetFocus();
213 }
214
215
216 //
217 // WM_CLOSE message handler
218 //
219
220 void CEventEditor::OnClose()
221 {
222 DWORD dwResult;
223
224 dwResult = DoRequest(NXCUnlockEventDB, _T("Unlocking event configuration database..."));
225 if (dwResult != RCC_SUCCESS)
226 theApp.ErrorBox(dwResult, _T("Unable to unlock event configuration database: %s"));
227 CMDIChildWnd::OnClose();
228 }
229
230
231 //
232 // PreCreateWindow()
233 //
234
235 BOOL CEventEditor::PreCreateWindow(CREATESTRUCT& cs)
236 {
237 if (cs.lpszClass == NULL)
238 cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW, NULL, NULL,
239 AfxGetApp()->LoadIcon(IDI_LOG));
240 return CMDIChildWnd::PreCreateWindow(cs);
241 }
242
243
244 //
245 // WM_CONTEXTMENU message handler
246 //
247
248 void CEventEditor::OnContextMenu(CWnd* pWnd, CPoint point)
249 {
250 CMenu *pMenu;
251
252 pMenu = theApp.GetContextMenu(10);
253 pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this, NULL);
254 }
255
256
257 //
258 // OnUpdate...() handlers
259 //
260
261 void CEventEditor::OnUpdateEventEdit(CCmdUI* pCmdUI)
262 {
263 pCmdUI->Enable(m_wndListCtrl.GetSelectedCount() == 1);
264 }
265
266 void CEventEditor::OnUpdateEventDelete(CCmdUI* pCmdUI)
267 {
268 pCmdUI->Enable(m_wndListCtrl.GetSelectedCount() > 0);
269 }
270
271
272 //
273 // WM_COMMAND::ID_EVENT_EDIT message handler
274 //
275
276 void CEventEditor::OnEventEdit()
277 {
278 int iItem;
279
280 if (m_wndListCtrl.GetSelectedCount() == 1)
281 {
282 iItem = m_wndListCtrl.GetNextItem(-1, LVIS_FOCUSED);
283 if (iItem != -1)
284 EditEvent(iItem);
285 }
286 }
287
288
289 //
290 // WM_COMMAND::ID_EVENT_NEW message handler
291 //
292
293 void CEventEditor::OnEventNew()
294 {
295 DWORD dwNewCode, dwResult;
296 NXC_EVENT_TEMPLATE *pData;
297 TCHAR szBuffer[32];
298 int iItem;
299
300 dwResult = DoRequestArg1(NXCGenerateEventCode, &dwNewCode, _T("Generating code for new event..."));
301 if (dwResult == RCC_SUCCESS)
302 {
303 // Create new item in list view
304 _stprintf(szBuffer, _T("%ld"), dwNewCode);
305 iItem = m_wndListCtrl.InsertItem(0x7FFFFFFF, szBuffer, 0);
306 m_wndListCtrl.SetItemData(iItem, dwNewCode);
307
308 // Create empty record in template list
309 pData = (NXC_EVENT_TEMPLATE *)malloc(sizeof(NXC_EVENT_TEMPLATE));
310 memset(pData, 0, sizeof(NXC_EVENT_TEMPLATE));
311 pData->dwCode = dwNewCode;
312 NXCAddEventTemplate(pData);
313
314 // Pointers inside client library can change after adding new template, so we reget it
315 NXCGetEventDB(&m_ppEventTemplates, &m_dwNumTemplates);
316
317 // Edit new event
318 if (!EditEvent(iItem))
319 {
320 m_wndListCtrl.DeleteItem(iItem);
321 NXCDeleteEDBRecord(dwNewCode);
322 NXCGetEventDB(&m_ppEventTemplates, &m_dwNumTemplates);
323 }
324 }
325 else
326 {
327 theApp.ErrorBox(dwResult, _T("Unable to generate code for new event: %s"));
328 }
329 }
330
331
332 //
333 // WM_COMMAND::ID_EVENT_DELETE message handler
334 //
335
336 void CEventEditor::OnEventDelete()
337 {
338 }