8bd38fac82228d92f6db1629d9b6522401083c78
[public/netxms.git] / src / console / win32 / ActionEditor.cpp
1 // ActionEditor.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "nxcon.h"
6 #include "ActionEditor.h"
7 #include "NewActionDlg.h"
8 #include "EditActionDlg.h"
9
10 #ifdef _DEBUG
11 #define new DEBUG_NEW
12 #undef THIS_FILE
13 static char THIS_FILE[] = __FILE__;
14 #endif
15
16 /////////////////////////////////////////////////////////////////////////////
17 // CActionEditor
18
19 IMPLEMENT_DYNCREATE(CActionEditor, CMDIChildWnd)
20
21 CActionEditor::CActionEditor()
22 {
23 }
24
25 CActionEditor::~CActionEditor()
26 {
27 }
28
29
30 BEGIN_MESSAGE_MAP(CActionEditor, CMDIChildWnd)
31 //{{AFX_MSG_MAP(CActionEditor)
32 ON_WM_CREATE()
33 ON_WM_DESTROY()
34 ON_WM_SETFOCUS()
35 ON_WM_SIZE()
36 ON_WM_CLOSE()
37 ON_COMMAND(ID_VIEW_REFRESH, OnViewRefresh)
38 ON_WM_CONTEXTMENU()
39 ON_UPDATE_COMMAND_UI(ID_ACTION_DELETE, OnUpdateActionDelete)
40 ON_UPDATE_COMMAND_UI(ID_ACTION_PROPERTIES, OnUpdateActionProperties)
41 ON_UPDATE_COMMAND_UI(ID_ACTION_RENAME, OnUpdateActionRename)
42 ON_COMMAND(ID_ACTION_NEW, OnActionNew)
43 ON_COMMAND(ID_ACTION_PROPERTIES, OnActionProperties)
44 ON_COMMAND(ID_ACTION_DELETE, OnActionDelete)
45 //}}AFX_MSG_MAP
46 ON_NOTIFY(NM_DBLCLK, IDC_LIST_VIEW, OnListViewDoubleClick)
47 END_MESSAGE_MAP()
48
49 /////////////////////////////////////////////////////////////////////////////
50 // CActionEditor message handlers
51
52 BOOL CActionEditor::PreCreateWindow(CREATESTRUCT& cs)
53 {
54 if (cs.lpszClass == NULL)
55 cs.lpszClass = AfxRegisterWndClass(CS_HREDRAW | CS_VREDRAW,
56 NULL,
57 GetSysColorBrush(COLOR_WINDOW),
58 AfxGetApp()->LoadIcon(IDI_EXEC));
59 return CMDIChildWnd::PreCreateWindow(cs);
60 }
61
62
63 //
64 // WM_CREATE message handler
65 //
66
67 int CActionEditor::OnCreate(LPCREATESTRUCT lpCreateStruct)
68 {
69 RECT rect;
70
71 if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
72 return -1;
73
74 theApp.OnViewCreate(VIEW_ACTION_EDITOR, this);
75
76 // Create list view control
77 GetClientRect(&rect);
78 m_wndListCtrl.Create(WS_CHILD | WS_VISIBLE | LVS_REPORT | LVS_SHOWSELALWAYS, rect, this, IDC_LIST_VIEW);
79 m_wndListCtrl.SetExtendedStyle(LVS_EX_TRACKSELECT | LVS_EX_UNDERLINEHOT |
80 LVS_EX_FULLROWSELECT | LVS_EX_GRIDLINES);
81 m_wndListCtrl.SetHoverTime(0x7FFFFFFF);
82
83 // Create image list
84 m_imageList.Create(16, 16, ILC_COLOR8 | ILC_MASK, 8, 8);
85 m_imageList.Add(AfxGetApp()->LoadIcon(IDI_EXEC));
86 m_imageList.Add(AfxGetApp()->LoadIcon(IDI_REXEC));
87 m_imageList.Add(AfxGetApp()->LoadIcon(IDI_EMAIL));
88 m_imageList.Add(AfxGetApp()->LoadIcon(IDI_SMS));
89 m_wndListCtrl.SetImageList(&m_imageList, LVSIL_SMALL);
90
91 // Setup columns
92 m_wndListCtrl.InsertColumn(0, _T("Name"), LVCFMT_LEFT, 100);
93 m_wndListCtrl.InsertColumn(1, _T("Type"), LVCFMT_LEFT, 70);
94 m_wndListCtrl.InsertColumn(2, _T("Recipient"), LVCFMT_LEFT, 150);
95 m_wndListCtrl.InsertColumn(3, _T("Data"), LVCFMT_LEFT, 350);
96
97 PostMessage(WM_COMMAND, ID_VIEW_REFRESH, 0);
98
99 return 0;
100 }
101
102
103 //
104 // WM_DESTROY message handler
105 //
106
107 void CActionEditor::OnDestroy()
108 {
109 theApp.OnViewDestroy(VIEW_ACTION_EDITOR, this);
110 CMDIChildWnd::OnDestroy();
111 }
112
113
114 //
115 // WM_SETFOCUS message handler
116 //
117
118 void CActionEditor::OnSetFocus(CWnd* pOldWnd)
119 {
120 CMDIChildWnd::OnSetFocus(pOldWnd);
121
122 m_wndListCtrl.SetFocus();
123 }
124
125
126 //
127 // WM_SIZE message handler
128 //
129
130 void CActionEditor::OnSize(UINT nType, int cx, int cy)
131 {
132 CMDIChildWnd::OnSize(nType, cx, cy);
133
134 m_wndListCtrl.SetWindowPos(NULL, 0, 0, cx, cy, SWP_NOZORDER);
135 }
136
137
138 //
139 // WM_CLOSE message handler
140 //
141
142 void CActionEditor::OnClose()
143 {
144 DoRequestArg1(NXCUnlockActionDB, g_hSession, _T("Unlocking action configuration database..."));
145 CMDIChildWnd::OnClose();
146 }
147
148
149 //
150 // Handler for double-clicks in list view
151 //
152
153 void CActionEditor::OnListViewDoubleClick(NMITEMACTIVATE *pInfo, LRESULT *pResult)
154 {
155 PostMessage(WM_COMMAND, ID_ACTION_PROPERTIES, 0);
156 }
157
158
159 //
160 // WM_COMMAND::ID_VIEW_REFRESH message handler
161 //
162
163 void CActionEditor::OnViewRefresh()
164 {
165 DWORD i;
166
167 m_wndListCtrl.DeleteAllItems();
168 LockActions();
169 for(i = 0; i < g_dwNumActions; i++)
170 AddItem(&g_pActionList[i]);
171 UnlockActions();
172 }
173
174
175 //
176 // WM_CONTEXTMENU message handler
177 //
178
179 void CActionEditor::OnContextMenu(CWnd* pWnd, CPoint point)
180 {
181 CMenu *pMenu;
182
183 pMenu = theApp.GetContextMenu(7);
184 pMenu->TrackPopupMenu(TPM_LEFTALIGN | TPM_RIGHTBUTTON, point.x, point.y, this, NULL);
185 }
186
187
188 //
189 // Replace data in existing item
190 //
191
192 void CActionEditor::ReplaceItem(int iItem, NXC_ACTION *pAction)
193 {
194 if (iItem != -1)
195 {
196 m_wndListCtrl.SetItem(iItem, 0, LVIF_TEXT | LVIF_IMAGE | LVIF_PARAM,
197 pAction->szName, pAction->iType, 0, 0, pAction->dwId);
198 m_wndListCtrl.SetItemText(iItem, 1, g_szActionType[pAction->iType]);
199 m_wndListCtrl.SetItemText(iItem, 2, pAction->szRcptAddr);
200 m_wndListCtrl.SetItemText(iItem, 3, pAction->pszData);
201 }
202
203 }
204
205
206 //
207 // Add new item to list
208 //
209
210 int CActionEditor::AddItem(NXC_ACTION *pAction)
211 {
212 int iIndex;
213
214 iIndex = m_wndListCtrl.InsertItem(0x7FFFFFFF, pAction->szName, pAction->iType);
215 if (iIndex != -1)
216 {
217 m_wndListCtrl.SetItemData(iIndex, pAction->dwId);
218 m_wndListCtrl.SetItemText(iIndex, 1, g_szActionType[pAction->iType]);
219 m_wndListCtrl.SetItemText(iIndex, 2, pAction->szRcptAddr);
220 m_wndListCtrl.SetItemText(iIndex, 3, pAction->pszData);
221 }
222 return iIndex;
223 }
224
225
226 //
227 // Update status for menu items
228 //
229
230 void CActionEditor::OnUpdateActionDelete(CCmdUI* pCmdUI)
231 {
232 pCmdUI->Enable(m_wndListCtrl.GetSelectedCount() > 0);
233 }
234
235 void CActionEditor::OnUpdateActionProperties(CCmdUI* pCmdUI)
236 {
237 pCmdUI->Enable(m_wndListCtrl.GetSelectedCount() == 1);
238 }
239
240 void CActionEditor::OnUpdateActionRename(CCmdUI* pCmdUI)
241 {
242 pCmdUI->Enable(m_wndListCtrl.GetSelectedCount() == 1);
243 }
244
245
246 //
247 // WM_COMMAND::ID_ACTION_NEW message handler
248 //
249
250 void CActionEditor::OnActionNew()
251 {
252 CNewActionDlg dlg;
253 DWORD i, dwActionId, dwResult;
254 int iItem;
255
256 if (dlg.DoModal() == IDOK)
257 {
258 dwResult = DoRequestArg3(NXCCreateAction, g_hSession, (void *)((LPCTSTR)dlg.m_strName),
259 &dwActionId, _T("Creating new action..."));
260 if (dwResult == RCC_SUCCESS)
261 {
262 LockActions();
263
264 // Check if we already received an update from server
265 for(i = 0; i < g_dwNumActions; i++)
266 if (g_pActionList[i].dwId == dwActionId)
267 break; // Already have this action in list
268 if (i == g_dwNumActions)
269 {
270 // Action still not in list, add it
271 g_dwNumActions++;
272 g_pActionList = (NXC_ACTION *)realloc(g_pActionList, sizeof(NXC_ACTION) * g_dwNumActions);
273 memset(&g_pActionList[i], 0, sizeof(NXC_ACTION));
274 g_pActionList[i].dwId = dwActionId;
275 g_pActionList[i].iType = ACTION_EXEC;
276 nx_strncpy(g_pActionList[i].szName, dlg.m_strName, MAX_OBJECT_NAME);
277 g_pActionList[i].pszData = _tcsdup(_T(""));
278 }
279
280 iItem = AddItem(&g_pActionList[i]);
281 SelectListViewItem(&m_wndListCtrl, iItem);
282 PostMessage(WM_COMMAND, ID_ACTION_PROPERTIES, 0);
283
284 UnlockActions();
285 }
286 else
287 {
288 theApp.ErrorBox(dwResult, _T("Error creating action: %s"));
289 }
290 }
291 }
292
293
294 //
295 // WM_COMMAND::ID_ACTION_PROPERTIES message handler
296 //
297
298 void CActionEditor::OnActionProperties()
299 {
300 int iItem;
301
302 if (m_wndListCtrl.GetSelectedCount() == 1)
303 {
304 iItem = m_wndListCtrl.GetSelectionMark();
305 if (iItem != -1)
306 {
307 NXC_ACTION *pAction;
308
309 LockActions();
310
311 pAction = FindActionById(m_wndListCtrl.GetItemData(iItem));
312 if (pAction != NULL)
313 {
314 CEditActionDlg dlg;
315
316 dlg.m_iType = pAction->iType;
317 dlg.m_strData = pAction->pszData;
318 dlg.m_strName = pAction->szName;
319 dlg.m_strRcpt = pAction->szRcptAddr;
320 dlg.m_strSubject = pAction->szEmailSubject;
321 UnlockActions();
322
323 if (dlg.DoModal() == IDOK)
324 {
325 NXC_ACTION action;
326 DWORD dwResult;
327
328 memset(&action, 0, sizeof(NXC_ACTION));
329 action.dwId = m_wndListCtrl.GetItemData(iItem);
330 action.iType = dlg.m_iType;
331 action.pszData = _tcsdup((LPCTSTR)dlg.m_strData);
332 nx_strncpy(action.szEmailSubject, (LPCTSTR)dlg.m_strSubject, MAX_EMAIL_SUBJECT_LEN);
333 nx_strncpy(action.szName, (LPCTSTR)dlg.m_strName, MAX_OBJECT_NAME);
334 nx_strncpy(action.szRcptAddr, (LPCTSTR)dlg.m_strRcpt, MAX_RCPT_ADDR_LEN);
335
336 dwResult = DoRequestArg2(NXCModifyAction, g_hSession, &action,
337 _T("Updating action configuration..."));
338 if (dwResult == RCC_SUCCESS)
339 {
340 ReplaceItem(iItem, &action);
341 }
342 else
343 {
344 theApp.ErrorBox(dwResult, _T("Error updating action configuration: %s"));
345 }
346 free(action.pszData);
347 }
348 }
349 else
350 {
351 UnlockActions();
352 MessageBox(_T("Internal error: cannot find requested action entry in list"), _T("Internal Error"), MB_OK | MB_ICONSTOP);
353 }
354 }
355 }
356 }
357
358
359 //
360 // Worker function for action deletion
361 //
362
363 static DWORD DeleteActions(DWORD dwNumActions, DWORD *pdwDeleteList, CListCtrl *pList)
364 {
365 DWORD i, dwResult = RCC_SUCCESS;
366 LVFINDINFO lvfi;
367 int iItem;
368
369 lvfi.flags = LVFI_PARAM;
370 for(i = 0; i < dwNumActions; i++)
371 {
372 dwResult = NXCDeleteAction(g_hSession, pdwDeleteList[i]);
373 if (dwResult == RCC_SUCCESS)
374 {
375 lvfi.lParam = pdwDeleteList[i];
376 iItem = pList->FindItem(&lvfi);
377 if (iItem != -1)
378 pList->DeleteItem(iItem);
379 }
380 else
381 {
382 break;
383 }
384 }
385 return dwResult;
386 }
387
388
389 //
390 // WM_COMMAND::ID_ACTION_DELETE message handler
391 //
392
393 void CActionEditor::OnActionDelete()
394 {
395 int iItem;
396 DWORD i, dwNumItems, *pdwDeleteList, dwResult;
397
398 dwNumItems = m_wndListCtrl.GetSelectedCount();
399 if (dwNumItems > 0)
400 {
401 pdwDeleteList = (DWORD *)malloc(sizeof(DWORD) * dwNumItems);
402 iItem = m_wndListCtrl.GetNextItem(-1, LVIS_SELECTED);
403 for(i = 0; (i < dwNumItems) && (iItem != -1); i++)
404 {
405 pdwDeleteList[i] = m_wndListCtrl.GetItemData(iItem);
406 iItem = m_wndListCtrl.GetNextItem(iItem, LVIS_SELECTED);
407 }
408
409 dwResult = DoRequestArg3(DeleteActions, (void *)dwNumItems, pdwDeleteList,
410 &m_wndListCtrl, _T("Deleting action(s)..."));
411 if (dwResult != RCC_SUCCESS)
412 theApp.ErrorBox(dwResult, _T("Error deleting action: %s"));
413 free(pdwDeleteList);
414 }
415 }