Implemented mechanism which allow controls to subscribe to client library events
[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 END_EVENT_TABLE()
36
37
38 //
39 // Constructor
40 //
41
42 nxAlarmView::nxAlarmView(wxWindow *parent, const TCHAR *context)
43 : wxWindow(parent, wxID_ANY)
44 {
45 wxConfigBase *cfg = wxConfig::Get();
46 wxString path = cfg->GetPath();
47
48 m_context = context;
49
50 cfg->SetPath(m_context);
51 cfg->Read(_T("AlarmView/SortMode"), &m_sortMode, 0);
52 cfg->Read(_T("AlarmView/SortDir"), &m_sortDir, -1);
53
54 m_wndListCtrl = new wxListView(this, wxID_LIST_CTRL, wxDefaultPosition, wxDefaultSize,
55 wxLC_REPORT | wxLC_HRULES | wxLC_VRULES);
56 m_wndListCtrl->InsertColumn(0, _T("Severity"), wxLIST_FORMAT_LEFT, 70);
57 m_wndListCtrl->InsertColumn(1, _T("State"), wxLIST_FORMAT_LEFT, 70);
58 m_wndListCtrl->InsertColumn(2, _T("Source"), wxLIST_FORMAT_LEFT, 120);
59 m_wndListCtrl->InsertColumn(3, _T("Message"), wxLIST_FORMAT_LEFT, 200);
60 m_wndListCtrl->InsertColumn(4, _T("Count"), wxLIST_FORMAT_LEFT, 40);
61 m_wndListCtrl->InsertColumn(5, _T("Created"), wxLIST_FORMAT_LEFT, 70);
62 m_wndListCtrl->InsertColumn(6, _T("Last Change"), wxLIST_FORMAT_LEFT, 70);
63 wxImageList *imgList = NXMCGetImageListCopy(IMAGE_LIST_STATUS_SMALL);
64 imgList->Add(wxXmlResource::Get()->LoadIcon(_T("icoSmallOutstanding")));
65 imgList->Add(wxXmlResource::Get()->LoadIcon(_T("icoSmallAcknowledged")));
66 imgList->Add(wxXmlResource::Get()->LoadIcon(_T("icoSmallTerminated")));
67 imgList->Add(wxXmlResource::Get()->LoadIcon(_T("icoSortUp")));
68 imgList->Add(wxXmlResource::Get()->LoadIcon(_T("icoSortDown")));
69 m_wndListCtrl->AssignImageList(imgList, wxIMAGE_LIST_SMALL);
70 NXMCLoadListCtrlColumns(cfg, *m_wndListCtrl, _T("AlarmView"));
71
72 cfg->SetPath(path);
73
74 NXMCEvtConnect(nxEVT_NXC_ALARM_CHANGE, wxCommandEventHandler(nxAlarmView::OnAlarmChange), this);
75 }
76
77
78 //
79 // Destructor
80 //
81
82 nxAlarmView::~nxAlarmView()
83 {
84 NXMCEvtDisconnect(nxEVT_NXC_ALARM_CHANGE, wxCommandEventHandler(nxAlarmView::OnAlarmChange), this);
85
86 wxConfigBase *cfg = wxConfig::Get();
87 wxString path = cfg->GetPath();
88 cfg->SetPath(m_context);
89 cfg->Write(_T("AlarmView/SortMode"), m_sortMode);
90 cfg->Write(_T("AlarmView/SortDir"), m_sortDir);
91 NXMCSaveListCtrlColumns(cfg, *m_wndListCtrl, _T("AlarmView"));
92 cfg->SetPath(path);
93 }
94
95
96 //
97 // Resize handler
98 //
99
100 void nxAlarmView::OnSize(wxSizeEvent &event)
101 {
102 wxSize size = GetClientSize();
103 m_wndListCtrl->SetSize(0, 0, size.x, size.y);
104 }
105
106
107 //
108 // Refresh view
109 //
110
111 void nxAlarmView::RefreshView()
112 {
113 nxAlarmList *list;
114 nxAlarmList::iterator it;
115
116 m_wndListCtrl->DeleteAllItems();
117 list = NXMCGetAlarmList();
118
119 for(it = list->begin(); it != list->end(); it++)
120 {
121 AddAlarm(it->second);
122 }
123
124 NXMCUnlockAlarmList();
125
126 SortAlarmList();
127 }
128
129
130 //
131 // Add alarm to view
132 //
133
134 void nxAlarmView::AddAlarm(NXC_ALARM *alarm)
135 {
136 long item;
137
138 item = m_wndListCtrl->InsertItem(0x7FFFFFFF, NXMCGetStatusTextSmall(alarm->nCurrentSeverity), alarm->nCurrentSeverity);
139 if (item != -1)
140 {
141 m_wndListCtrl->SetItemData(item, alarm->dwAlarmId);
142 UpdateAlarm(item, alarm);
143 }
144 }
145
146
147 //
148 // Update alarm in view
149 //
150
151 void nxAlarmView::UpdateAlarm(long item, NXC_ALARM *alarm)
152 {
153 NXC_OBJECT *object;
154 TCHAR temp[64];
155
156 m_wndListCtrl->SetItem(item, 0, NXMCGetStatusTextSmall(alarm->nCurrentSeverity), alarm->nCurrentSeverity);
157 m_wndListCtrl->SetItem(item, 1, NXMCGetAlarmStateName(alarm->nState), alarm->nState + STATUS_TESTING + 1);
158
159 object = NXCFindObjectById(NXMCGetSession(), alarm->dwSourceObject);
160 if (object != NULL)
161 {
162 m_wndListCtrl->SetItem(item, 2, object->szName);
163 }
164 else
165 {
166 m_wndListCtrl->SetItem(item, 2, _T("<unknown>"));
167 }
168
169 m_wndListCtrl->SetItem(item, 3, alarm->szMessage);
170
171 _stprintf(temp, _T("%d"), alarm->dwRepeatCount);
172 m_wndListCtrl->SetItem(item, 4, temp);
173
174 m_wndListCtrl->SetItem(item, 5, NXMCFormatTimeStamp(alarm->dwCreationTime, temp, TS_LONG_DATE_TIME));
175 m_wndListCtrl->SetItem(item, 6, NXMCFormatTimeStamp(alarm->dwLastChangeTime, temp, TS_LONG_DATE_TIME));
176 }
177
178
179 //
180 // Handler for right click on item
181 //
182
183 void nxAlarmView::OnListItemRightClick(wxListEvent &event)
184 {
185 wxMenu *menu;
186
187 menu = wxXmlResource::Get()->LoadMenu(_T("menuCtxAlarm"));
188 if (menu != NULL)
189 {
190 PopupMenu(menu);
191 delete menu;
192 }
193 }
194
195
196 //
197 // Alarm comparision callback
198 //
199
200 static nxAlarmList *s_alarmList;
201
202 static int wxCALLBACK CompareAlarms(long item1, long item2, long sortData)
203 {
204 long mode = sortData & 0x7FFF;
205 int rc;
206 nxAlarmList::iterator it1, it2;
207 NXC_OBJECT *obj1, *obj2;
208
209 it1 = s_alarmList->find(item1);
210 it2 = s_alarmList->find(item2);
211 if ((it1 == s_alarmList->end()) || (it2 == s_alarmList->end()))
212 {
213 wxLogWarning(_T("CompareAlarms: invalid iterator returned"));
214 return 0; // Shouldn't happen
215 }
216
217 switch(mode)
218 {
219 case 0: // Severity
220 rc = COMPARE_NUMBERS(it1->second->nCurrentSeverity, it2->second->nCurrentSeverity);
221 break;
222 case 1: // State
223 rc = COMPARE_NUMBERS(it1->second->nState, it2->second->nState);
224 break;
225 case 2: // Source
226 obj1 = NXCFindObjectById(NXMCGetSession(), it1->second->dwSourceObject);
227 obj2 = NXCFindObjectById(NXMCGetSession(), it2->second->dwSourceObject);
228 rc = _tcsicmp((obj1 != NULL) ? obj1->szName : _T("(null)"), (obj2 != NULL) ? obj2->szName : _T("(null)"));
229 break;
230 case 3: // Message
231 rc = _tcsicmp(it1->second->szMessage, it2->second->szMessage);
232 break;
233 case 4: // Count
234 rc = COMPARE_NUMBERS(it1->second->dwRepeatCount, it2->second->dwRepeatCount);
235 break;
236 case 5: // Created
237 rc = COMPARE_NUMBERS(it1->second->dwCreationTime, it2->second->dwCreationTime);
238 break;
239 case 6: // Last changed
240 rc = COMPARE_NUMBERS(it1->second->dwLastChangeTime, it2->second->dwLastChangeTime);
241 break;
242 default:
243 rc = 0;
244 break;
245 }
246
247 return (sortData & 0x8000) ? rc : -rc;
248 }
249
250
251 //
252 // Sort aarm list
253 //
254
255 void nxAlarmView::SortAlarmList()
256 {
257 s_alarmList = NXMCGetAlarmList();
258 m_wndListCtrl->SortItems(CompareAlarms, m_sortMode | ((m_sortDir == 1) ? 0x8000 : 0));
259 NXMCUnlockAlarmList();
260
261 // Mark sorting column
262 m_wndListCtrl->SetColumnImage(m_sortMode, STATUS_TESTING + ((m_sortDir == 1) ? 4 : 5));
263 }
264
265
266 //
267 // Handler for column click
268 //
269
270 void nxAlarmView::OnListColumnClick(wxListEvent &event)
271 {
272 m_wndListCtrl->ClearColumnImage(m_sortMode);
273
274 if (m_sortMode == event.GetColumn())
275 {
276 m_sortDir = -m_sortDir;
277 }
278 else
279 {
280 m_sortMode = event.GetColumn();
281 }
282 SortAlarmList();
283 }
284
285
286 //
287 // Handler for alarm change event
288 //
289
290 void nxAlarmView::OnAlarmChange(wxCommandEvent &event)
291 {
292 wxLogDebug(_T("AlarmView: onAlarmChange %d"), event.GetInt());
293 event.Skip();
294 }