- Finished migration to new client library model
[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(g_hSession, &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 = DoRequestArg2(NXCSetEventInfo, g_hSession,
184 &evt, _T("Updating event configuration database..."));
185 if (dwResult == RCC_SUCCESS)
186 {
187 // Record was successfully updated on server, update local copy
188 safe_free(m_ppEventTemplates[dwIndex]->pszDescription);
189 safe_free(m_ppEventTemplates[dwIndex]->pszMessage);
190 memcpy(m_ppEventTemplates[dwIndex], &evt, sizeof(NXC_EVENT_TEMPLATE));
191 UpdateItem(iItem, m_ppEventTemplates[dwIndex]);
192 bResult = TRUE;
193 }
194 else
195 {
196 theApp.ErrorBox(dwResult, _T("Unable to update event configuration record: %s"));
197 safe_free(evt.pszDescription);
198 safe_free(evt.pszMessage);
199 }
200 }
201 }
202 return bResult;
203 }
204
205
206 //
207 // WM_SETFOCUS message handler
208 //
209
210 void CEventEditor::OnSetFocus(CWnd* pOldWnd)
211 {
212 CMDIChildWnd::OnSetFocus(pOldWnd);
213 m_wndListCtrl.SetFocus();
214 }
215
216
217 //
218 // WM_CLOSE message handler
219 //
220
221 void CEventEditor::OnClose()
222 {
223 DWORD dwResult;
224
225 dwResult = DoRequestArg1(NXCUnlockEventDB, g_hSession, _T("Unlocking event configuration database..."));
226 if (dwResult != RCC_SUCCESS)
227 theApp.ErrorBox(dwResult, _T("Unable to unlock event configuration database: %s"));
228 CMDIChildWnd::OnClose();
229 }
230
231
232 //
233 // PreCreateWindow()
234 //
235
236 BOOL CEventEditor::PreCreateWindow(CREATESTRUCT& cs)
237 {
238 if (cs.lpszClass == NULL)
239 cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW, NULL, NULL,
240 AfxGetApp()->LoadIcon(IDI_LOG));
241 return CMDIChildWnd::PreCreateWindow(cs);
242 }
243
244
245 //
246 // WM_CONTEXTMENU message handler
247 //
248
249 void CEventEditor::OnContextMenu(CWnd* pWnd, CPoint point)
250 {
251 CMenu *pMenu;
252
253 pMenu = theApp.GetContextMenu(10);
254 pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this, NULL);
255 }
256
257
258 //
259 // OnUpdate...() handlers
260 //
261
262 void CEventEditor::OnUpdateEventEdit(CCmdUI* pCmdUI)
263 {
264 pCmdUI->Enable(m_wndListCtrl.GetSelectedCount() == 1);
265 }
266
267 void CEventEditor::OnUpdateEventDelete(CCmdUI* pCmdUI)
268 {
269 pCmdUI->Enable(m_wndListCtrl.GetSelectedCount() > 0);
270 }
271
272
273 //
274 // WM_COMMAND::ID_EVENT_EDIT message handler
275 //
276
277 void CEventEditor::OnEventEdit()
278 {
279 int iItem;
280
281 if (m_wndListCtrl.GetSelectedCount() == 1)
282 {
283 iItem = m_wndListCtrl.GetNextItem(-1, LVIS_FOCUSED);
284 if (iItem != -1)
285 EditEvent(iItem);
286 }
287 }
288
289
290 //
291 // WM_COMMAND::ID_EVENT_NEW message handler
292 //
293
294 void CEventEditor::OnEventNew()
295 {
296 DWORD dwNewCode, dwResult;
297 NXC_EVENT_TEMPLATE *pData;
298 TCHAR szBuffer[32];
299 int iItem;
300
301 dwResult = DoRequestArg2(NXCGenerateEventCode, g_hSession, &dwNewCode,
302 _T("Generating code for new event..."));
303 if (dwResult == RCC_SUCCESS)
304 {
305 // Create new item in list view
306 _stprintf(szBuffer, _T("%ld"), dwNewCode);
307 iItem = m_wndListCtrl.InsertItem(0x7FFFFFFF, szBuffer, 0);
308 m_wndListCtrl.SetItemData(iItem, dwNewCode);
309
310 // Create empty record in template list
311 pData = (NXC_EVENT_TEMPLATE *)malloc(sizeof(NXC_EVENT_TEMPLATE));
312 memset(pData, 0, sizeof(NXC_EVENT_TEMPLATE));
313 pData->dwCode = dwNewCode;
314 NXCAddEventTemplate(g_hSession, pData);
315
316 // Pointers inside client library can change after adding new template, so we reget it
317 NXCGetEventDB(g_hSession, &m_ppEventTemplates, &m_dwNumTemplates);
318
319 // Edit new event
320 if (!EditEvent(iItem))
321 {
322 m_wndListCtrl.DeleteItem(iItem);
323 NXCDeleteEDBRecord(g_hSession, dwNewCode);
324 NXCGetEventDB(g_hSession, &m_ppEventTemplates, &m_dwNumTemplates);
325 }
326 }
327 else
328 {
329 theApp.ErrorBox(dwResult, _T("Unable to generate code for new event: %s"));
330 }
331 }
332
333
334 //
335 // WM_COMMAND::ID_EVENT_DELETE message handler
336 //
337
338 void CEventEditor::OnEventDelete()
339 {
340 }