Console compiles on UNIX with UNICODE
[public/netxms.git] / src / client / console / plugins / AlarmBrowser / view.cpp
index 28741fb..83aeee3 100644 (file)
 // Event table
 //
 
-BEGIN_EVENT_TABLE(nxAlarmView, wxWindow)
+BEGIN_EVENT_TABLE(nxAlarmView, nxView)
        EVT_SIZE(nxAlarmView::OnSize)
-       EVT_LIST_ITEM_RIGHT_CLICK(wxID_LIST_CTRL, nxAlarmView::OnListItemRightClick)
+//     EVT_LIST_ITEM_RIGHT_CLICK(wxID_LIST_CTRL, nxAlarmView::OnListItemRightClick)
        EVT_LIST_COL_CLICK(wxID_LIST_CTRL, nxAlarmView::OnListColumnClick)
+       EVT_CONTEXT_MENU(nxAlarmView::OnContextMenu)
        EVT_MENU(XRCID("menuAlarmAck"), nxAlarmView::OnAlarmAck)
+       EVT_UPDATE_UI(XRCID("menuAlarmAck"), nxAlarmView::OnUpdateAlarmAck)
+       EVT_MENU(XRCID("menuAlarmTerminate"), nxAlarmView::OnAlarmTerminate)
+       EVT_UPDATE_UI(XRCID("menuAlarmTerminate"), nxAlarmView::OnUpdateAlarmTerminate)
+       EVT_MENU(XRCID("menuAlarmDelete"), nxAlarmView::OnAlarmDelete)
+       EVT_UPDATE_UI(XRCID("menuAlarmDelete"), nxAlarmView::OnUpdateAlarmDelete)
+       EVT_MENU(XRCID("menuAlarmIncreaseFont"), nxAlarmView::OnAlarmIncreaseFont)
+       EVT_MENU(XRCID("menuAlarmDecreaseFont"), nxAlarmView::OnAlarmDecreaseFont)
 END_EVENT_TABLE()
 
 
@@ -40,17 +48,21 @@ END_EVENT_TABLE()
 // Constructor
 //
 
-nxAlarmView::nxAlarmView(wxWindow *parent, const TCHAR *context)
-            : wxWindow(parent, wxID_ANY)
+nxAlarmView::nxAlarmView(wxWindow *parent, const TCHAR *context, NXC_OBJECT *object)
+            : nxView(parent)
 {
+       int fontSize;
+
        wxConfigBase *cfg = wxConfig::Get();
        wxString path = cfg->GetPath();
 
        m_context = context;
+       m_object = object;
 
        cfg->SetPath(m_context);
        cfg->Read(_T("AlarmView/SortMode"), &m_sortMode, 0);
        cfg->Read(_T("AlarmView/SortDir"), &m_sortDir, -1);
+       cfg->Read(_T("AlarmView/FontSize"), &fontSize, -1);
 
        m_wndListCtrl = new wxListView(this, wxID_LIST_CTRL, wxDefaultPosition, wxDefaultSize,
                                       wxLC_REPORT | wxLC_HRULES | wxLC_VRULES);
@@ -70,7 +82,18 @@ nxAlarmView::nxAlarmView(wxWindow *parent, const TCHAR *context)
        m_wndListCtrl->AssignImageList(imgList, wxIMAGE_LIST_SMALL);
        NXMCLoadListCtrlColumns(cfg, *m_wndListCtrl, _T("AlarmView"));
 
+       if (fontSize != -1)
+       {
+               wxFont font;
+
+               font = m_wndListCtrl->GetFont();
+               font.SetPointSize(fontSize);
+               m_wndListCtrl->SetFont(font);
+       }
+
        cfg->SetPath(path);
+       
+       SetIcon(wxXmlResource::Get()->LoadIcon(_T("icoAlarmView")));
 
        NXMCEvtConnect(nxEVT_NXC_ALARM_CHANGE, wxCommandEventHandler(nxAlarmView::OnAlarmChange), this);
 }
@@ -89,6 +112,7 @@ nxAlarmView::~nxAlarmView()
        cfg->SetPath(m_context);
        cfg->Write(_T("AlarmView/SortMode"), m_sortMode);
        cfg->Write(_T("AlarmView/SortDir"), m_sortDir);
+       cfg->Write(_T("AlarmView/FontSize"), m_wndListCtrl->GetFont().GetPointSize());
        NXMCSaveListCtrlColumns(cfg, *m_wndListCtrl, _T("AlarmView"));
        cfg->SetPath(path);
 }
@@ -119,7 +143,8 @@ void nxAlarmView::RefreshView()
        
        for(it = list->begin(); it != list->end(); it++)
        {
-               AddAlarm(it->second);
+               if (MatchAlarm(it->second))
+                       AddAlarm(it->second);
        }
 
        NXMCUnlockAlarmList();
@@ -128,6 +153,20 @@ void nxAlarmView::RefreshView()
 }
 
 
+//
+// Check if alarm matched to current filter settings
+//
+
+bool nxAlarmView::MatchAlarm(NXC_ALARM *alarm)
+{
+       if (m_object == NULL)
+               return true;
+       if (m_object->dwId == alarm->dwSourceObject)
+               return true;
+       return NXCIsParent(NXMCGetSession(), m_object->dwId, alarm->dwSourceObject) ? true : false;
+}
+
+
 //
 // Add alarm to view
 //
@@ -169,7 +208,7 @@ void nxAlarmView::UpdateAlarm(long item, NXC_ALARM *alarm)
 
        m_wndListCtrl->SetItem(item, 3, alarm->szMessage);
 
-       _stprintf(temp, _T("%d"), alarm->dwRepeatCount);
+       _sntprintf(temp, 64, _T("%d"), alarm->dwRepeatCount);
        m_wndListCtrl->SetItem(item, 4, temp);
 
        m_wndListCtrl->SetItem(item, 5, NXMCFormatTimeStamp(alarm->dwCreationTime, temp, TS_LONG_DATE_TIME));
@@ -185,11 +224,14 @@ void nxAlarmView::OnListItemRightClick(wxListEvent &event)
 {
        wxMenu *menu;
 
-       menu = wxXmlResource::Get()->LoadMenu(_T("menuCtxAlarm"));
-       if (menu != NULL)
+       if (!IsBusy())
        {
-               PopupMenu(menu);
-               delete menu;
+               menu = wxXmlResource::Get()->LoadMenu(_T("menuCtxAlarm"));
+               if (menu != NULL)
+               {
+                       PopupMenu(menu);
+                       delete menu;
+               }
        }
 }
 
@@ -250,7 +292,7 @@ static int wxCALLBACK CompareAlarms(long item1, long item2, long sortData)
 
 
 //
-// Sort aarm list
+// Sort alarm list
 //
 
 void nxAlarmView::SortAlarmList()
@@ -291,25 +333,31 @@ void nxAlarmView::OnListColumnClick(wxListEvent &event)
 void nxAlarmView::OnAlarmChange(wxCommandEvent &event)
 {
        long item;
+       NXC_ALARM *alarm;
        
+       alarm = (NXC_ALARM *)event.GetClientData();
        switch(event.GetInt())
        {
                case NX_NOTIFY_NEW_ALARM:
                case NX_NOTIFY_ALARM_CHANGED:
-                       item = m_wndListCtrl->FindItem(-1, ((NXC_ALARM *)event.GetClientData())->dwAlarmId);
+                       item = m_wndListCtrl->FindItem(-1, alarm->dwAlarmId);
                        if (item != -1)
                        {
-                               UpdateAlarm(item, (NXC_ALARM *)event.GetClientData());
+                               if (MatchAlarm(alarm))
+                                       UpdateAlarm(item, (NXC_ALARM *)event.GetClientData());
+                               else
+                                       m_wndListCtrl->DeleteItem(item);                // Alarm no longer match filter
                        }
                        else
                        {
-                               AddAlarm((NXC_ALARM *)event.GetClientData());
+                               if (MatchAlarm(alarm))
+                                       AddAlarm(alarm);
                        }
                        SortAlarmList();
                        break;
                case NX_NOTIFY_ALARM_TERMINATED:
                case NX_NOTIFY_ALARM_DELETED:
-                       item = m_wndListCtrl->FindItem(-1, ((NXC_ALARM *)event.GetClientData())->dwAlarmId);
+                       item = m_wndListCtrl->FindItem(-1, alarm->dwAlarmId);
                        if (item != -1)
                                m_wndListCtrl->DeleteItem(item);
                        break;
@@ -322,8 +370,180 @@ void nxAlarmView::OnAlarmChange(wxCommandEvent &event)
 // Handler for acknowledge alarm menu
 //
 
+static DWORD AckAlarms(int count, DWORD *list)
+{
+       int i;
+       DWORD rcc;
+       NXC_SESSION session = NXMCGetSession();
+       
+       for(i = 0; i < count; i++)
+       {
+               rcc = NXCAcknowledgeAlarm(session, list[i]);
+               if (rcc != RCC_SUCCESS)
+                       break;
+       }
+       delete list;
+       return rcc;
+}
+
 void nxAlarmView::OnAlarmAck(wxCommandEvent &event)
 {
-//     NXCAcknowledgeAlarm(NXMCGetSession(), m_wndListCtrl->GetItemData(m_wndListCtrl->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED)));
+       int i, count = m_wndListCtrl->GetSelectedItemCount();
+       DWORD *idList = new DWORD[count];
+       long item;
+
+       for(i = 0, item = -1; i < count; i++)
+       {
+               item = m_wndListCtrl->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
+               if (item == -1)
+                       break;
+               idList[i] = m_wndListCtrl->GetItemData(item);
+       }
+       if (count > 0)
+               DoRequestArg2((void *)AckAlarms, count, CAST_FROM_POINTER(idList, wxUIntPtr), _T("Cannot acknowledge alarm: %s"));
+}
+
+void nxAlarmView::OnUpdateAlarmAck(wxUpdateUIEvent &event)
+{
+       event.Enable(m_wndListCtrl->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED) != -1);
+}
+
+
+//
+// Handler for terminate alarm menu
+//
+
+static DWORD TerminateAlarms(int count, DWORD *list)
+{
+       int i;
+       DWORD rcc;
+       NXC_SESSION session = NXMCGetSession();
+       
+       for(i = 0; i < count; i++)
+       {
+               rcc = NXCTerminateAlarm(session, list[i]);
+               if (rcc != RCC_SUCCESS)
+                       break;
+       }
+       delete list;
+       return rcc;
+}
+
+void nxAlarmView::OnAlarmTerminate(wxCommandEvent &event)
+{
+       int i, count = m_wndListCtrl->GetSelectedItemCount();
+       DWORD *idList = new DWORD[count];
+       long item;
+
+       for(i = 0, item = -1; i < count; i++)
+       {
+               item = m_wndListCtrl->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
+               if (item == -1)
+                       break;
+               idList[i] = m_wndListCtrl->GetItemData(item);
+       }
+       if (count > 0)
+               DoRequestArg2((void *)TerminateAlarms, count, CAST_FROM_POINTER(idList, wxUIntPtr), _T("Cannot terminate alarm: %s"));
+}
+
+void nxAlarmView::OnUpdateAlarmTerminate(wxUpdateUIEvent &event)
+{
+       event.Enable(m_wndListCtrl->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED) != -1);
+}
+
+
+//
+// Handler for delete alarm menu
+//
+
+static DWORD DeleteAlarms(int count, DWORD *list)
+{
+       int i;
+       DWORD rcc;
+       NXC_SESSION session = NXMCGetSession();
+       
+       for(i = 0; i < count; i++)
+       {
+               rcc = NXCDeleteAlarm(session, list[i]);
+               if (rcc != RCC_SUCCESS)
+                       break;
+       }
+       delete list;
+       return rcc;
+}
+
+void nxAlarmView::OnAlarmDelete(wxCommandEvent &event)
+{
+       int i, count = m_wndListCtrl->GetSelectedItemCount();
+       DWORD *idList = new DWORD[count];
+       long item;
+
+       for(i = 0, item = -1; i < count; i++)
+       {
+               item = m_wndListCtrl->GetNextItem(item, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED);
+               if (item == -1)
+                       break;
+               idList[i] = m_wndListCtrl->GetItemData(item);
+       }
+       if (count > 0)
+               DoRequestArg2((void *)DeleteAlarms, count, CAST_FROM_POINTER(idList, wxUIntPtr), _T("Cannot delete alarm: %s"));
 }
 
+void nxAlarmView::OnUpdateAlarmDelete(wxUpdateUIEvent &event)
+{
+       event.Enable(m_wndListCtrl->GetNextItem(-1, wxLIST_NEXT_ALL, wxLIST_STATE_SELECTED) != -1);
+}
+
+
+//
+// Increase font
+//
+
+void nxAlarmView::OnAlarmIncreaseFont(wxCommandEvent &event)
+{
+       wxFont font;
+
+       font = m_wndListCtrl->GetFont();
+       if (font.GetPointSize() < 100)
+       {
+               font.SetPointSize(font.GetPointSize() + 1);
+               m_wndListCtrl->SetFont(font);
+       }
+}
+
+
+//
+// Decrease font
+//
+
+void nxAlarmView::OnAlarmDecreaseFont(wxCommandEvent &event)
+{
+       wxFont font;
+
+       font = m_wndListCtrl->GetFont();
+       if (font.GetPointSize() > 4)
+       {
+               font.SetPointSize(font.GetPointSize() - 1);
+               m_wndListCtrl->SetFont(font);
+       }
+}
+
+
+//
+// Context menu handler
+//
+
+void nxAlarmView::OnContextMenu(wxContextMenuEvent &event)
+{
+       wxMenu *menu;
+
+       if (!IsBusy())
+       {
+               menu = wxXmlResource::Get()->LoadMenu(_T("menuCtxAlarm"));
+               if (menu != NULL)
+               {
+                       PopupMenu(menu);
+                       delete menu;
+               }
+       }
+}