Minor changes
[public/netxms.git] / src / client / console / plugins / AlarmBrowser / view.cpp
1 /*
2 ** NetXMS - Network Management System
3 ** Portable management console - Alarm Browser plugin
4 ** Copyright (C) 2007 Victor Kirhenshtein
5 **
6 ** This program is free software; you can redistribute it and/or modify
7 ** it under the terms of the GNU General Public License as published by
8 ** the Free Software Foundation; either version 2 of the License, or
9 ** (at your option) any later version.
10 **
11 ** This program is distributed in the hope that it will be useful,
12 ** but WITHOUT ANY WARRANTY; without even the implied warranty of
13 ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 ** GNU General Public License for more details.
15 **
16 ** You should have received a copy of the GNU General Public License
17 ** along with this program; if not, write to the Free Software
18 ** Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 **
20 ** File: view.cpp
21 **
22 **/
23
24 #include "alarm_browser.h"
25
26
27 //
28 // Event table
29 //
30
31 BEGIN_EVENT_TABLE(nxAlarmView, wxWindow)
32 EVT_SIZE(nxAlarmView::OnSize)
33 EVT_LIST_ITEM_RIGHT_CLICK(wxID_LIST_CTRL, nxAlarmView::OnListItemRightClick)
34 EVT_LIST_COL_CLICK(wxID_LIST_CTRL, nxAlarmView::OnListColumnClick)
35 EVT_MENU(XRCID("menuAlarmAck"), nxAlarmView::OnAlarmAck)
36 END_EVENT_TABLE()
37
38
39 //
40 // Constructor
41 //
42
43 nxAlarmView::nxAlarmView(wxWindow *parent, const TCHAR *context)
44 : wxWindow(parent, wxID_ANY)
45 {
46 wxConfigBase *cfg = wxConfig::Get();
47 wxString path = cfg->GetPath();
48
49 m_context = context;
50
51 cfg->SetPath(m_context);
52 cfg->Read(_T("AlarmView/SortMode"), &m_sortMode, 0);
53 cfg->Read(_T("AlarmView/SortDir"), &m_sortDir, -1);
54
55 m_wndListCtrl = new wxListView(this, wxID_LIST_CTRL, wxDefaultPosition, wxDefaultSize,
56 wxLC_REPORT | wxLC_HRULES | wxLC_VRULES);
57 m_wndListCtrl->InsertColumn(0, _T("Severity"), wxLIST_FORMAT_LEFT, 70);
58 m_wndListCtrl->InsertColumn(1, _T("State"), wxLIST_FORMAT_LEFT, 70);
59 m_wndListCtrl->InsertColumn(2, _T("Source"), wxLIST_FORMAT_LEFT, 120);
60 m_wndListCtrl->InsertColumn(3, _T("Message"), wxLIST_FORMAT_LEFT, 200);
61 m_wndListCtrl->InsertColumn(4, _T("Count"), wxLIST_FORMAT_LEFT, 40);
62 m_wndListCtrl->InsertColumn(5, _T("Created"), wxLIST_FORMAT_LEFT, 70);
63 m_wndListCtrl->InsertColumn(6, _T("Last Change"), wxLIST_FORMAT_LEFT, 70);
64 wxImageList *imgList = NXMCGetImageListCopy(IMAGE_LIST_STATUS_SMALL);
65 imgList->Add(wxXmlResource::Get()->LoadIcon(_T("icoSmallOutstanding")));
66 imgList->Add(wxXmlResource::Get()->LoadIcon(_T("icoSmallAcknowledged")));
67 imgList->Add(wxXmlResource::Get()->LoadIcon(_T("icoSmallTerminated")));
68 imgList->Add(wxXmlResource::Get()->LoadIcon(_T("icoSortUp")));
69 imgList->Add(wxXmlResource::Get()->LoadIcon(_T("icoSortDown")));
70 m_wndListCtrl->AssignImageList(imgList, wxIMAGE_LIST_SMALL);
71 NXMCLoadListCtrlColumns(cfg, *m_wndListCtrl, _T("AlarmView"));
72
73 cfg->SetPath(path);
74
75 NXMCEvtConnect(nxEVT_NXC_ALARM_CHANGE, wxCommandEventHandler(nxAlarmView::OnAlarmChange), this);
76 }
77
78
79 //
80 // Destructor
81 //
82
83 nxAlarmView::~nxAlarmView()
84 {
85 NXMCEvtDisconnect(nxEVT_NXC_ALARM_CHANGE, wxCommandEventHandler(nxAlarmView::OnAlarmChange), this);
86
87 wxConfigBase *cfg = wxConfig::Get();
88 wxString path = cfg->GetPath();
89 cfg->SetPath(m_context);
90 cfg->Write(_T("AlarmView/SortMode"), m_sortMode);
91 cfg->Write(_T("AlarmView/SortDir"), m_sortDir);
92 NXMCSaveListCtrlColumns(cfg, *m_wndListCtrl, _T("AlarmView"));
93 cfg->SetPath(path);
94 }
95
96
97 //
98 // Resize handler
99 //
100
101 void nxAlarmView::OnSize(wxSizeEvent &event)
102 {
103 wxSize size = GetClientSize();
104 m_wndListCtrl->SetSize(0, 0, size.x, size.y);
105 }
106
107
108 //
109 // Refresh view
110 //
111
112 void nxAlarmView::RefreshView()
113 {
114 nxAlarmList *list;
115 nxAlarmList::iterator it;
116
117 m_wndListCtrl->DeleteAllItems();
118 list = NXMCGetAlarmList();
119
120 for(it = list->begin(); it != list->end(); it++)
121 {
122 AddAlarm(it->second);
123 }
124
125 NXMCUnlockAlarmList();
126
127 SortAlarmList();
128 }
129
130
131 //
132 // Add alarm to view
133 //
134
135 void nxAlarmView::AddAlarm(NXC_ALARM *alarm)
136 {
137 long item;
138
139 item = m_wndListCtrl->InsertItem(0x7FFFFFFF, NXMCGetStatusTextSmall(alarm->nCurrentSeverity), alarm->nCurrentSeverity);
140 if (item != -1)
141 {
142 m_wndListCtrl->SetItemData(item, alarm->dwAlarmId);
143 UpdateAlarm(item, alarm);
144 }
145 }
146
147
148 //
149 // Update alarm in view
150 //
151
152 void nxAlarmView::UpdateAlarm(long item, NXC_ALARM *alarm)
153 {
154 NXC_OBJECT *object;
155 TCHAR temp[64];
156
157 m_wndListCtrl->SetItem(item, 0, NXMCGetStatusTextSmall(alarm->nCurrentSeverity), alarm->nCurrentSeverity);
158 m_wndListCtrl->SetItem(item, 1, NXMCGetAlarmStateName(alarm->nState), alarm->nState + STATUS_TESTING + 1);
159
160 object = NXCFindObjectById(NXMCGetSession(), alarm->dwSourceObject);
161 if (object != NULL)
162 {
163 m_wndListCtrl->SetItem(item, 2, object->szName);
164 }
165 else
166 {
167 m_wndListCtrl->SetItem(item, 2, _T("<unknown>"));
168 }
169
170 m_wndListCtrl->SetItem(item, 3, alarm->szMessage);
171
172 _stprintf(temp, _T("%d"), alarm->dwRepeatCount);
173 m_wndListCtrl->SetItem(item, 4, temp);
174
175 m_wndListCtrl->SetItem(item, 5, NXMCFormatTimeStamp(alarm->dwCreationTime, temp, TS_LONG_DATE_TIME));
176 m_wndListCtrl->SetItem(item, 6, NXMCFormatTimeStamp(alarm->dwLastChangeTime, temp, TS_LONG_DATE_TIME));
177 }
178
179
180 //
181 // Handler for right click on item
182 //
183
184 void nxAlarmView::OnListItemRightClick(wxListEvent &event)
185 {
186 wxMenu *menu;
187
188 menu = wxXmlResource::Get()->LoadMenu(_T("menuCtxAlarm"));
189 if (menu != NULL)
190 {
191 PopupMenu(menu);
192 delete menu;
193 }
194 }
195
196
197 //
198 // Alarm comparision callback
199 //
200
201 static nxAlarmList *s_alarmList;
202
203 static int wxCALLBACK CompareAlarms(long item1, long item2, long sortData)
204 {
205 long mode = sortData & 0x7FFF;
206 int rc;
207 nxAlarmList::iterator it1, it2;
208 NXC_OBJECT *obj1, *obj2;
209
210 it1 = s_alarmList->find(item1);
211 it2 = s_alarmList->find(item2);
212 if ((it1 == s_alarmList->end()) || (it2 == s_alarmList->end()))
213 {
214 wxLogDebug(_T("CompareAlarms: invalid iterator returned"));
215 return 0; // Shouldn't happen
216 }
217
218 switch(mode)
219 {
220 case 0: // Severity
221 rc = COMPARE_NUMBERS(it1->second->nCurrentSeverity, it2->second->nCurrentSeverity);
222 break;
223 case 1: // State
224 rc = COMPARE_NUMBERS(it1->second->nState, it2->second->nState);
225 break;
226 case 2: // Source
227 obj1 = NXCFindObjectById(NXMCGetSession(), it1->second->dwSourceObject);
228 obj2 = NXCFindObjectById(NXMCGetSession(), it2->second->dwSourceObject);
229 rc = _tcsicmp((obj1 != NULL) ? obj1->szName : _T("(null)"), (obj2 != NULL) ? obj2->szName : _T("(null)"));
230 break;
231 case 3: // Message
232 rc = _tcsicmp(it1->second->szMessage, it2->second->szMessage);
233 break;
234 case 4: // Count
235 rc = COMPARE_NUMBERS(it1->second->dwRepeatCount, it2->second->dwRepeatCount);
236 break;
237 case 5: // Created
238 rc = COMPARE_NUMBERS(it1->second->dwCreationTime, it2->second->dwCreationTime);
239 break;
240 case 6: // Last changed
241 rc = COMPARE_NUMBERS(it1->second->dwLastChangeTime, it2->second->dwLastChangeTime);
242 break;
243 default:
244 rc = 0;
245 break;
246 }
247
248 return (sortData & 0x8000) ? rc : -rc;
249 }
250
251
252 //
253 // Sort aarm list
254 //
255
256 void nxAlarmView::SortAlarmList()
257 {
258 s_alarmList = NXMCGetAlarmList();
259 m_wndListCtrl->SortItems(CompareAlarms, m_sortMode | ((m_sortDir == 1) ? 0x8000 : 0));
260 NXMCUnlockAlarmList();
261
262 // Mark sorting column
263 m_wndListCtrl->SetColumnImage(m_sortMode, STATUS_TESTING + ((m_sortDir == 1) ? 4 : 5));
264 }
265
266
267 //
268 // Handler for column click
269 //
270
271 void nxAlarmView::OnListColumnClick(wxListEvent &event)
272 {
273 m_wndListCtrl->ClearColumnImage(m_sortMode);
274
275 if (m_sortMode == event.GetColumn())
276 {
277 m_sortDir = -m_sortDir;
278 }
279 else
280 {
281 m_sortMode = event.GetColumn();
282 }
283 SortAlarmList();
284 }
285
286
287 //
288 // Handler for alarm change event
289 //
290
291 void nxAlarmView::OnAlarmChange(wxCommandEvent &event)
292 {
293 long item;
294
295 switch(event.GetInt())
296 {
297 case NX_NOTIFY_NEW_ALARM:
298 case NX_NOTIFY_ALARM_CHANGED:
299 item = m_wndListCtrl->FindItem(-1, ((NXC_ALARM *)event.GetClientData())->dwAlarmId);
300 if (item != -1)
301 {
302 UpdateAlarm(item, (NXC_ALARM *)event.GetClientData());
303 }
304 else
305 {
306 AddAlarm((NXC_ALARM *)event.GetClientData());
307 }
308 SortAlarmList();
309 break;
310 case NX_NOTIFY_ALARM_TERMINATED:
311 case NX_NOTIFY_ALARM_DELETED:
312 item = m_wndListCtrl->FindItem(-1, ((NXC_ALARM *)event.GetClientData())->dwAlarmId);
313 if (item != -1)
314 m_wndListCtrl->DeleteItem(item);
315 break;
316 }
317 event.Skip();
318 }
319
320
321 //
322 // Handler for acknowledge alarm menu
323 //
324
325 void nxAlarmView::OnAlarmAck(wxCommandEvent &event)
326 {
327 // NXCAcknowledgeAlarm(NXMCGetSession(), m_wndListCtrl->GetItemData(m_wndListCtrl->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)));
328 }
329