28821e8e9e094a844b638cb1b7ca38d90ae42d86
[public/netxms.git] / src / console / win32 / nxcon.cpp
1 // nxcon.cpp : Defines the class behaviors for the application.
2 //
3
4 #include "stdafx.h"
5 #include "nxcon.h"
6
7 #include "MainFrm.h"
8 #include "LoginDialog.h"
9 #include "CreateContainerDlg.h"
10 #include "CreateNodeDlg.h"
11 #include "CreateTemplateDlg.h"
12 #include "CreateTGDlg.h"
13 #include "NodePoller.h"
14 #include "MIBBrowserDlg.h"
15
16 #ifdef _DEBUG
17 #define new DEBUG_NEW
18 #undef THIS_FILE
19 static char THIS_FILE[] = __FILE__;
20 #endif
21
22
23 //
24 // Constants
25 //
26
27 #define LAST_APP_MENU 3
28
29
30 //
31 // Wrapper for client library event handler
32 //
33
34 static void ClientEventHandler(DWORD dwEvent, DWORD dwCode, void *pArg)
35 {
36 theApp.EventHandler(dwEvent, dwCode, pArg);
37 }
38
39
40 //
41 // Wrapper for client library debug callback
42 //
43
44 static void ClientDebugCallback(char *pszMsg)
45 {
46 theApp.DebugCallback(pszMsg);
47 }
48
49
50 //
51 // CConsoleApp
52 //
53
54 BEGIN_MESSAGE_MAP(CConsoleApp, CWinApp)
55 //{{AFX_MSG_MAP(CConsoleApp)
56 ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
57 ON_COMMAND(ID_VIEW_CONTROLPANEL, OnViewControlpanel)
58 ON_COMMAND(ID_VIEW_EVENTS, OnViewEvents)
59 ON_COMMAND(ID_VIEW_MAP, OnViewMap)
60 ON_COMMAND(ID_CONNECT_TO_SERVER, OnConnectToServer)
61 ON_COMMAND(ID_VIEW_OBJECTBROWSER, OnViewObjectbrowser)
62 ON_COMMAND(ID_CONTROLPANEL_EVENTS, OnControlpanelEvents)
63 ON_COMMAND(ID_VIEW_DEBUG, OnViewDebug)
64 ON_COMMAND(ID_CONTROLPANEL_USERS, OnControlpanelUsers)
65 ON_COMMAND(ID_VIEW_NETWORKSUMMARY, OnViewNetworksummary)
66 ON_COMMAND(ID_TOOLS_MIBBROWSER, OnToolsMibbrowser)
67 ON_COMMAND(ID_CONTROLPANEL_EVENTPOLICY, OnControlpanelEventpolicy)
68 ON_COMMAND(ID_VIEW_ALARMS, OnViewAlarms)
69 ON_COMMAND(ID_FILE_SETTINGS, OnFileSettings)
70 ON_COMMAND(ID_CONTROLPANEL_ACTIONS, OnControlpanelActions)
71 ON_COMMAND(ID_TOOLS_ADDNODE, OnToolsAddnode)
72 ON_COMMAND(ID_CONTROLPANEL_SNMPTRAPS, OnControlpanelSnmptraps)
73 //}}AFX_MSG_MAP
74 END_MESSAGE_MAP()
75
76
77 //
78 // CConsoleApp construction
79 //
80
81 CConsoleApp::CConsoleApp()
82 {
83 m_bActionEditorActive = FALSE;
84 m_bTrapEditorActive = FALSE;
85 m_bCtrlPanelActive = FALSE;
86 m_bAlarmBrowserActive = FALSE;
87 m_bEventBrowserActive = FALSE;
88 m_bEventEditorActive = FALSE;
89 m_bUserEditorActive = FALSE;
90 m_bObjectBrowserActive = FALSE;
91 m_bDebugWindowActive = FALSE;
92 m_bNetSummaryActive = FALSE;
93 m_bEventPolicyEditorActive = FALSE;
94 m_dwClientState = STATE_DISCONNECTED;
95 memset(m_openDCEditors, 0, sizeof(DC_EDITOR) * MAX_DC_EDITORS);
96 }
97
98
99 //
100 // CConsoleApp destruction
101 //
102
103 CConsoleApp::~CConsoleApp()
104 {
105 }
106
107
108 //
109 // The one and only CConsoleApp object
110 //
111
112 CConsoleApp theApp;
113
114
115 //
116 // Setup working directory
117 //
118
119 BOOL CConsoleApp::SetupWorkDir()
120 {
121 int iLastChar;
122
123 // Get path to user's "Application Data" folder
124 if (SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, g_szWorkDir) != S_OK)
125 {
126 AfxMessageBox(IDS_GETFOLDERPATH_FAILED, MB_OK | MB_ICONSTOP);
127 return FALSE;
128 }
129
130 // Create working directory root
131 strcat(g_szWorkDir, "\\NetXMS Console");
132 if (!CreateDirectory(g_szWorkDir, NULL))
133 if (GetLastError() != ERROR_ALREADY_EXISTS)
134 {
135 AfxMessageBox(IDS_WORKDIR_CREATION_FAILED, MB_OK | MB_ICONSTOP);
136 return FALSE;
137 }
138 iLastChar = strlen(g_szWorkDir);
139
140 // Create MIB cache directory
141 strcpy(&g_szWorkDir[iLastChar], WORKDIR_MIBCACHE);
142 if (!CreateDirectory(g_szWorkDir, NULL))
143 if (GetLastError() != ERROR_ALREADY_EXISTS)
144 {
145 AfxMessageBox(IDS_WORKDIR_CREATION_FAILED, MB_OK | MB_ICONSTOP);
146 return FALSE;
147 }
148
149 // Create image cache directory
150 strcpy(&g_szWorkDir[iLastChar], WORKDIR_IMAGECACHE);
151 if (!CreateDirectory(g_szWorkDir, NULL))
152 if (GetLastError() != ERROR_ALREADY_EXISTS)
153 {
154 AfxMessageBox(IDS_WORKDIR_CREATION_FAILED, MB_OK | MB_ICONSTOP);
155 return FALSE;
156 }
157
158 // Success
159 g_szWorkDir[iLastChar] = 0;
160 return TRUE;
161 }
162
163
164 //
165 // CConsoleApp initialization
166 //
167
168 BOOL CConsoleApp::InitInstance()
169 {
170 BOOL bSetWindowPos;
171 DWORD dwBytes;
172 BYTE *pData;
173 HMENU hMenu;
174
175 // Setup our working directory
176 if (!SetupWorkDir())
177 return FALSE;
178
179 if (!AfxSocketInit())
180 {
181 AfxMessageBox(IDP_SOCKETS_INIT_FAILED, MB_OK | MB_ICONSTOP);
182 return FALSE;
183 }
184
185 if (!NXCInitialize())
186 {
187 AfxMessageBox(IDS_NXC_INIT_FAILED, MB_OK | MB_ICONSTOP);
188 return FALSE;
189 }
190 NXCSetEventHandler(ClientEventHandler);
191
192 AfxEnableControlContainer();
193
194 // Standard initialization
195 // If you are not using these features and wish to reduce the size
196 // of your final executable, you should remove from the following
197 // the specific initialization routines you do not need.
198
199 #ifdef _AFXDLL
200 Enable3dControls(); // Call this when using MFC in a shared DLL
201 #else
202 Enable3dControlsStatic(); // Call this when linking to MFC statically
203 #endif
204
205 // Change the registry key under which our settings are stored.
206 SetRegistryKey(_T("NetXMS"));
207
208 // Load configuration from registry
209 dwBytes = sizeof(WINDOWPLACEMENT);
210 bSetWindowPos = GetProfileBinary(_T("General"), _T("WindowPlacement"),
211 &pData, (UINT *)&dwBytes);
212 g_dwOptions = GetProfileInt(_T("General"), _T("Options"), 0);
213 strcpy(g_szServer, (LPCTSTR)GetProfileString(_T("Connection"), _T("Server"), _T("localhost")));
214 strcpy(g_szLogin, (LPCTSTR)GetProfileString(_T("Connection"), _T("Login"), NULL));
215 g_dwEncryptionMethod = GetProfileInt(_T("Connection"), _T("Encryption"), CSCP_ENCRYPTION_NONE);
216
217 // Create mutex for action list access
218 g_mutexActionListAccess = CreateMutex(NULL, FALSE, NULL);
219
220 // To create the main window, this code creates a new frame window
221 // object and then sets it as the application's main window object.
222 CMDIFrameWnd* pFrame = new CMainFrame;
223 m_pMainWnd = pFrame;
224
225 // create main MDI frame window
226 if (!pFrame->LoadFrame(IDR_MAINFRAME))
227 return FALSE;
228
229 // Load context menus
230 if (!m_ctxMenu.LoadMenu(IDM_CONTEXT))
231 MessageBox(NULL, "FAILED", "", 0);
232
233 // Load shared MDI menus and accelerator table
234 HINSTANCE hInstance = AfxGetResourceHandle();
235
236 hMenu = ::LoadMenu(hInstance, MAKEINTRESOURCE(IDM_VIEW_SPECIFIC));
237
238 // Modify application menu as needed
239 if (g_dwOptions & UI_OPT_EXPAND_CTRLPANEL)
240 {
241 pFrame->GetMenu()->RemoveMenu(ID_VIEW_CONTROLPANEL, MF_BYCOMMAND);
242 pFrame->GetMenu()->InsertMenu(ID_VIEW_DEBUG, MF_BYCOMMAND | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 8), "&Control panel");
243 }
244
245 m_hMDIMenu = LoadAppMenu(hMenu);
246 InsertMenu(m_hMDIMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
247
248 m_hEventBrowserMenu = LoadAppMenu(hMenu);
249 InsertMenu(m_hEventBrowserMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
250 InsertMenu(m_hEventBrowserMenu, LAST_APP_MENU - 1, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 1), "&Event");
251
252 m_hObjectBrowserMenu = LoadAppMenu(hMenu);
253 InsertMenu(m_hObjectBrowserMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
254 InsertMenu(m_hObjectBrowserMenu, LAST_APP_MENU - 1, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 2), "&Object");
255
256 m_hUserEditorMenu = LoadAppMenu(hMenu);
257 InsertMenu(m_hUserEditorMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
258 InsertMenu(m_hUserEditorMenu, LAST_APP_MENU - 1, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 3), "&User");
259
260 m_hDCEditorMenu = LoadAppMenu(hMenu);
261 InsertMenu(m_hDCEditorMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
262 InsertMenu(m_hDCEditorMenu, LAST_APP_MENU - 1, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 4), "&Item");
263
264 m_hPolicyEditorMenu = LoadAppMenu(hMenu);
265 InsertMenu(m_hPolicyEditorMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
266 InsertMenu(m_hPolicyEditorMenu, LAST_APP_MENU - 1, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 5), "&Policy");
267
268 m_hAlarmBrowserMenu = LoadAppMenu(hMenu);
269 InsertMenu(m_hAlarmBrowserMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
270 InsertMenu(m_hAlarmBrowserMenu, LAST_APP_MENU - 1, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 6), "&Alarm");
271
272 m_hMapMenu = LoadAppMenu(hMenu);
273 InsertMenu(m_hMapMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
274 InsertMenu(m_hMapMenu, LAST_APP_MENU - 1, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 9), "&Object");
275
276 m_hEventEditorMenu = LoadAppMenu(hMenu);
277 InsertMenu(m_hEventEditorMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
278 InsertMenu(m_hEventEditorMenu, LAST_APP_MENU - 1, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 10), "&Event");
279
280 m_hActionEditorMenu = LoadAppMenu(hMenu);
281 InsertMenu(m_hActionEditorMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
282 InsertMenu(m_hActionEditorMenu, LAST_APP_MENU - 1, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 11), "&Action");
283
284 m_hTrapEditorMenu = LoadAppMenu(hMenu);
285 InsertMenu(m_hTrapEditorMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
286 InsertMenu(m_hTrapEditorMenu, LAST_APP_MENU - 1, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 12), "T&rap");
287
288 m_hGraphMenu = LoadAppMenu(hMenu);
289 InsertMenu(m_hGraphMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
290 InsertMenu(m_hGraphMenu, LAST_APP_MENU - 1, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 13), "&Graph");
291
292 m_hMDIAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_MDI_DEFAULT));
293 m_hAlarmBrowserAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_ALARM_BROWSER));
294 m_hEventBrowserAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_MDI_DEFAULT));
295 m_hObjectBrowserAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_OBJECT_BROWSER));
296 m_hEventEditorAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_EVENT_EDITOR));
297 m_hUserEditorAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_MDI_DEFAULT));
298 m_hDCEditorAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_MDI_DEFAULT));
299 m_hPolicyEditorAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_EPP));
300 m_hMapAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_NETMAP));
301 m_hActionEditorAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_ACTION_EDITOR));
302 m_hTrapEditorAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_TRAP_EDITOR));
303 m_hGraphAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_GRAPH));
304
305 // The main window has been initialized, so show and update it.
306 if (bSetWindowPos)
307 {
308 pFrame->SetWindowPlacement((WINDOWPLACEMENT *)pData);
309 if (pFrame->IsIconic())
310 pFrame->ShowWindow(SW_RESTORE);
311 }
312 else
313 {
314 pFrame->ShowWindow(m_nCmdShow);
315 }
316 pFrame->UpdateWindow();
317 //pFrame->PostMessage(WM_COMMAND, ID_VIEW_DEBUG, 0);
318 pFrame->PostMessage(WM_COMMAND, ID_CONNECT_TO_SERVER, 0);
319
320 return TRUE;
321 }
322
323 /////////////////////////////////////////////////////////////////////////////
324 // CConsoleApp message handlers
325
326 int CConsoleApp::ExitInstance()
327 {
328 NXCSetDebugCallback(NULL);
329 NXCDisconnect();
330 NXCShutdown();
331 NXCDestroyCCList(g_pCCList);
332
333 // Save configuration
334 WriteProfileInt(_T("General"), _T("Options"), g_dwOptions);
335
336 // Free resources
337 SafeFreeResource(m_hMDIMenu);
338 SafeFreeResource(m_hMDIAccel);
339 SafeFreeResource(m_hAlarmBrowserMenu);
340 SafeFreeResource(m_hAlarmBrowserAccel);
341 SafeFreeResource(m_hEventBrowserMenu);
342 SafeFreeResource(m_hEventBrowserAccel);
343 SafeFreeResource(m_hUserEditorMenu);
344 SafeFreeResource(m_hUserEditorAccel);
345 SafeFreeResource(m_hDCEditorMenu);
346 SafeFreeResource(m_hDCEditorAccel);
347 SafeFreeResource(m_hEventEditorMenu);
348 SafeFreeResource(m_hEventEditorAccel);
349 SafeFreeResource(m_hActionEditorMenu);
350 SafeFreeResource(m_hActionEditorAccel);
351 SafeFreeResource(m_hTrapEditorMenu);
352 SafeFreeResource(m_hTrapEditorAccel);
353 SafeFreeResource(m_hGraphMenu);
354 SafeFreeResource(m_hGraphAccel);
355
356 CloseHandle(g_mutexActionListAccess);
357
358 return CWinApp::ExitInstance();
359 }
360
361
362 /////////////////////////////////////////////////////////////////////////////
363 // CAboutDlg dialog used for App About
364
365 class CAboutDlg : public CDialog
366 {
367 public:
368 CAboutDlg();
369
370 // Dialog Data
371 //{{AFX_DATA(CAboutDlg)
372 enum { IDD = IDD_ABOUTBOX };
373 CStatic m_wndVersion;
374 //}}AFX_DATA
375
376 // ClassWizard generated virtual function overrides
377 //{{AFX_VIRTUAL(CAboutDlg)
378 protected:
379 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
380 //}}AFX_VIRTUAL
381
382 // Implementation
383 protected:
384 //{{AFX_MSG(CAboutDlg)
385 virtual BOOL OnInitDialog();
386 //}}AFX_MSG
387 DECLARE_MESSAGE_MAP()
388 };
389
390 CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
391 {
392 //{{AFX_DATA_INIT(CAboutDlg)
393 //}}AFX_DATA_INIT
394 }
395
396 void CAboutDlg::DoDataExchange(CDataExchange* pDX)
397 {
398 CDialog::DoDataExchange(pDX);
399 //{{AFX_DATA_MAP(CAboutDlg)
400 DDX_Control(pDX, IDC_VERSION, m_wndVersion);
401 //}}AFX_DATA_MAP
402 }
403
404 BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
405 //{{AFX_MSG_MAP(CAboutDlg)
406 //}}AFX_MSG_MAP
407 END_MESSAGE_MAP()
408
409 BOOL CAboutDlg::OnInitDialog()
410 {
411 CDialog::OnInitDialog();
412
413 m_wndVersion.SetWindowText(_T("NetXMS Console Version ") NETXMS_VERSION_STRING);
414 return TRUE;
415 }
416
417 // App command to run the dialog
418 void CConsoleApp::OnAppAbout()
419 {
420 CAboutDlg aboutDlg;
421 aboutDlg.DoModal();
422 }
423
424 /////////////////////////////////////////////////////////////////////////////
425 // CConsoleApp message handlers
426
427
428 void CConsoleApp::OnViewControlpanel()
429 {
430 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
431
432 // create a new MDI child window or open existing
433 if (m_bCtrlPanelActive)
434 m_pwndCtrlPanel->BringWindowToTop();
435 else
436 pFrame->CreateNewChild(
437 RUNTIME_CLASS(CControlPanel), IDR_CTRLPANEL, m_hMDIMenu, m_hMDIAccel);
438 }
439
440
441 //
442 // This method called when new view is created
443 //
444
445 void CConsoleApp::OnViewCreate(DWORD dwView, CWnd *pWnd, DWORD dwArg)
446 {
447 DWORD i;
448
449 switch(dwView)
450 {
451 case IDR_CTRLPANEL:
452 m_bCtrlPanelActive = TRUE;
453 m_pwndCtrlPanel = pWnd;
454 break;
455 case IDR_ALARMS:
456 m_bAlarmBrowserActive = TRUE;
457 m_pwndAlarmBrowser = (CAlarmBrowser *)pWnd;
458 break;
459 case IDR_EVENTS:
460 m_bEventBrowserActive = TRUE;
461 m_pwndEventBrowser = (CEventBrowser *)pWnd;
462 break;
463 case IDR_OBJECTS:
464 m_bObjectBrowserActive = TRUE;
465 m_pwndObjectBrowser = pWnd;
466 break;
467 case IDR_EVENT_EDITOR:
468 m_bEventEditorActive = TRUE;
469 m_pwndEventEditor = (CEventEditor *)pWnd;
470 break;
471 case IDR_ACTION_EDITOR:
472 m_bActionEditorActive = TRUE;
473 m_pwndActionEditor = (CActionEditor *)pWnd;
474 break;
475 case IDR_TRAP_EDITOR:
476 m_bTrapEditorActive = TRUE;
477 m_pwndTrapEditor = (CTrapEditor *)pWnd;
478 break;
479 case IDR_USER_EDITOR:
480 m_bUserEditorActive = TRUE;
481 m_pwndUserEditor = (CUserEditor *)pWnd;
482 break;
483 case IDR_DEBUG_WINDOW:
484 m_bDebugWindowActive = TRUE;
485 m_pwndDebugWindow = (CDebugFrame *)pWnd;
486 NXCSetDebugCallback(ClientDebugCallback);
487 break;
488 case IDR_NETWORK_SUMMARY:
489 m_bNetSummaryActive = TRUE;
490 m_pwndNetSummary = (CNetSummaryFrame *)pWnd;
491 break;
492 case IDR_DC_EDITOR:
493 // Register new DC editor
494 for(i = 0; i < MAX_DC_EDITORS; i++)
495 if (m_openDCEditors[i].pWnd == NULL)
496 {
497 m_openDCEditors[i].pWnd = pWnd;
498 m_openDCEditors[i].dwNodeId = dwArg;
499 break;
500 }
501 break;
502 case IDR_EPP_EDITOR:
503 m_bEventPolicyEditorActive = TRUE;
504 m_pwndEventPolicyEditor = (CEventPolicyEditor *)pWnd;
505 break;
506 default:
507 break;
508 }
509 }
510
511
512 //
513 // This method called when view is destroyed
514 //
515
516 void CConsoleApp::OnViewDestroy(DWORD dwView, CWnd *pWnd, DWORD dwArg)
517 {
518 DWORD i;
519
520 switch(dwView)
521 {
522 case IDR_CTRLPANEL:
523 m_bCtrlPanelActive = FALSE;
524 break;
525 case IDR_ALARMS:
526 m_bAlarmBrowserActive = FALSE;
527 break;
528 case IDR_EVENTS:
529 m_bEventBrowserActive = FALSE;
530 break;
531 case IDR_OBJECTS:
532 m_bObjectBrowserActive = FALSE;
533 break;
534 case IDR_EVENT_EDITOR:
535 m_bEventEditorActive = FALSE;
536 break;
537 case IDR_ACTION_EDITOR:
538 m_bActionEditorActive = FALSE;
539 break;
540 case IDR_TRAP_EDITOR:
541 m_bTrapEditorActive = FALSE;
542 break;
543 case IDR_USER_EDITOR:
544 m_bUserEditorActive = FALSE;
545 break;
546 case IDR_DEBUG_WINDOW:
547 m_bDebugWindowActive = FALSE;
548 NXCSetDebugCallback(NULL);
549 break;
550 case IDR_NETWORK_SUMMARY:
551 m_bNetSummaryActive = FALSE;
552 break;
553 case IDR_DC_EDITOR:
554 // Unregister DC editor
555 for(i = 0; i < MAX_DC_EDITORS; i++)
556 if (m_openDCEditors[i].pWnd == pWnd)
557 {
558 m_openDCEditors[i].pWnd = NULL;
559 m_openDCEditors[i].dwNodeId = 0;
560 break;
561 }
562 break;
563 case IDR_EPP_EDITOR:
564 m_bEventPolicyEditorActive = FALSE;
565 break;
566 default:
567 break;
568 }
569 }
570
571
572 //
573 // WM_COMMAND::ID_VIEW_EVENTS message handler
574 //
575
576 void CConsoleApp::OnViewEvents()
577 {
578 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
579
580 // create a new MDI child window or open existing
581 if (m_bEventBrowserActive)
582 m_pwndEventBrowser->BringWindowToTop();
583 else
584 pFrame->CreateNewChild(
585 RUNTIME_CLASS(CEventBrowser), IDR_EVENTS, m_hEventBrowserMenu, m_hEventBrowserAccel);
586 }
587
588
589 //
590 // WM_COMMAND::ID_VIEW_MAP message handler
591 //
592
593 void CConsoleApp::OnViewMap()
594 {
595 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
596
597 // create a new MDI child window
598 pFrame->CreateNewChild(RUNTIME_CLASS(CMapFrame), IDR_MAPFRAME, m_hMapMenu, m_hMapAccel);
599 }
600
601
602 //
603 // WM_COMMAND::ID_CONNECT_TO_SERVER message handler
604 //
605
606 void CConsoleApp::OnConnectToServer()
607 {
608 CLoginDialog dlgLogin;
609 DWORD dwResult;
610
611 dlgLogin.m_szServer = g_szServer;
612 dlgLogin.m_szLogin = g_szLogin;
613 dlgLogin.m_iEncryption = g_dwEncryptionMethod;
614 do
615 {
616 if (dlgLogin.DoModal() != IDOK)
617 {
618 PostQuitMessage(1);
619 break;
620 }
621 strcpy(g_szServer, (LPCTSTR)dlgLogin.m_szServer);
622 strcpy(g_szLogin, (LPCTSTR)dlgLogin.m_szLogin);
623 strcpy(g_szPassword, (LPCTSTR)dlgLogin.m_szPassword);
624 g_dwEncryptionMethod = dlgLogin.m_iEncryption;
625
626 // Save last connection parameters
627 WriteProfileString(_T("Connection"), _T("Server"), g_szServer);
628 WriteProfileString(_T("Connection"), _T("Login"), g_szLogin);
629 WriteProfileInt(_T("Connection"), _T("Encryption"), g_dwEncryptionMethod);
630
631 // Initiate connection
632 dwResult = DoLogin();
633 if (dwResult != RCC_SUCCESS)
634 ErrorBox(dwResult, "Unable to connect: %s", "Connection error");
635 }
636 while(dwResult != RCC_SUCCESS);
637 }
638
639
640 //
641 // WM_COMMAND::ID_VIEW_OBJECTBROWSER message handler
642 //
643
644 void CConsoleApp::OnViewObjectbrowser()
645 {
646 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
647
648 // create a new MDI child window or open existing
649 if (m_bObjectBrowserActive)
650 m_pwndObjectBrowser->BringWindowToTop();
651 else
652 pFrame->CreateNewChild(
653 RUNTIME_CLASS(CObjectBrowser), IDR_OBJECTS, m_hObjectBrowserMenu, m_hObjectBrowserAccel);
654 }
655
656
657 //
658 // Event handler for client libray events
659 //
660
661 void CConsoleApp::EventHandler(DWORD dwEvent, DWORD dwCode, void *pArg)
662 {
663 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
664
665 switch(dwEvent)
666 {
667 case NXC_EVENT_STATE_CHANGED:
668 m_dwClientState = dwCode;
669 ((CMainFrame *)m_pMainWnd)->PostMessage(WM_STATE_CHANGE, dwCode, (LPARAM)pArg);
670 break;
671 case NXC_EVENT_NEW_ELOG_RECORD:
672 if (m_bEventBrowserActive)
673 m_pwndEventBrowser->AddEvent((NXC_EVENT *)pArg);
674 free(pArg);
675 break;
676 case NXC_EVENT_OBJECT_CHANGED:
677 ((CMainFrame *)m_pMainWnd)->PostMessage(WM_OBJECT_CHANGE, dwCode, (LPARAM)pArg);
678 break;
679 case NXC_EVENT_USER_DB_CHANGED:
680 ((CMainFrame *)m_pMainWnd)->PostMessage(WM_USERDB_CHANGE, dwCode, (LPARAM)pArg);
681 break;
682 case NXC_EVENT_NOTIFICATION:
683 switch(dwCode)
684 {
685 case NX_NOTIFY_SHUTDOWN:
686 m_pMainWnd->MessageBox("Server was shutdown", "Warning", MB_OK | MB_ICONSTOP);
687 PostQuitMessage(0);
688 break;
689 case NX_NOTIFY_EVENTDB_CHANGED:
690 m_pMainWnd->PostMessage(WM_COMMAND, ID_UPDATE_EVENT_LIST, 0);
691 break;
692 case NX_NOTIFY_NEW_ALARM:
693 case NX_NOTIFY_ALARM_DELETED:
694 case NX_NOTIFY_ALARM_ACKNOWLEGED:
695 m_pMainWnd->PostMessage(WM_ALARM_UPDATE, dwCode,
696 (LPARAM)nx_memdup(pArg, sizeof(NXC_ALARM)));
697 break;
698 case NX_NOTIFY_ACTION_CREATED:
699 case NX_NOTIFY_ACTION_MODIFIED:
700 case NX_NOTIFY_ACTION_DELETED:
701 UpdateActions(dwCode, (NXC_ACTION *)pArg);
702 break;
703 default:
704 break;
705 }
706 break;
707 default:
708 break;
709 }
710 }
711
712
713 //
714 // Handler for WM_COMMAND(ID_CONTROLPANEL_EVENTS) message
715 //
716
717 void CConsoleApp::OnControlpanelEvents()
718 {
719 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
720
721 // create a new MDI child window or open existing
722 if (m_bEventEditorActive)
723 m_pwndEventEditor->BringWindowToTop();
724 else
725 {
726 DWORD dwResult;
727
728 dwResult = DoRequest(NXCLockEventDB, "Locking event configuration database...");
729 if (dwResult == RCC_SUCCESS)
730 {
731 pFrame->CreateNewChild(
732 RUNTIME_CLASS(CEventEditor), IDR_EVENT_EDITOR, m_hEventEditorMenu, m_hEventEditorAccel);
733 }
734 else
735 {
736 ErrorBox(dwResult, "Unable to open event configuration database:\n%s");
737 }
738 }
739 }
740
741
742 //
743 // Handler for WM_COMMAND::ID_CONTROLPANEL_USERS message
744 //
745
746 void CConsoleApp::OnControlpanelUsers()
747 {
748 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
749
750 // create a new MDI child window or open existing
751 if (m_bUserEditorActive)
752 m_pwndUserEditor->BringWindowToTop();
753 else
754 {
755 DWORD dwResult;
756
757 dwResult = DoRequest(NXCLockUserDB, "Locking user database...");
758 if (dwResult == RCC_SUCCESS)
759 {
760 pFrame->CreateNewChild(
761 RUNTIME_CLASS(CUserEditor), IDR_USER_EDITOR, m_hUserEditorMenu, m_hUserEditorAccel);
762 }
763 else
764 {
765 ErrorBox(dwResult, "Unable to lock user database:\n%s");
766 }
767 }
768 }
769
770
771 //
772 // Handler for WM_COMMAND::ID_VIEW_DEBUG message
773 //
774
775 void CConsoleApp::OnViewDebug()
776 {
777 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
778
779 // create a new MDI child window or open existing
780 if (m_bDebugWindowActive)
781 m_pwndDebugWindow->BringWindowToTop();
782 else
783 pFrame->CreateNewChild(
784 RUNTIME_CLASS(CDebugFrame), IDR_DEBUG_WINDOW, m_hMDIMenu, m_hMDIAccel);
785 }
786
787
788 //
789 // Print debug information
790 //
791
792 void CConsoleApp::DebugPrintf(char *szFormat, ...)
793 {
794 if (m_bDebugWindowActive)
795 {
796 char szBuffer[1024];
797 va_list args;
798
799 va_start(args, szFormat);
800 vsprintf(szBuffer, szFormat, args);
801 va_end(args);
802
803 m_pwndDebugWindow->AddMessage(szBuffer);
804 }
805 }
806
807
808 //
809 // Edit properties of specific object
810 //
811
812 void CConsoleApp::ObjectProperties(DWORD dwObjectId)
813 {
814 CObjectPropSheet wndPropSheet("Object Properties", GetMainWnd(), 0);
815 CNodePropsGeneral wndNodeGeneral;
816 CObjectPropsGeneral wndObjectGeneral;
817 CObjectPropCaps wndObjectCaps;
818 CObjectPropsSecurity wndObjectSecurity;
819 CObjectPropsPresentation wndObjectPresentation;
820 NXC_OBJECT *pObject;
821 char szBuffer[32];
822
823 pObject = NXCFindObjectById(dwObjectId);
824 if (pObject != NULL)
825 {
826 // Create "General" tab
827 if (pObject->iClass == OBJECT_NODE)
828 {
829 wndNodeGeneral.m_dwObjectId = dwObjectId;
830 wndNodeGeneral.m_strName = pObject->szName;
831 wndNodeGeneral.m_strOID = pObject->node.szObjectId;
832 wndNodeGeneral.m_strPrimaryIp = IpToStr(pObject->dwIpAddr, szBuffer);
833 wndNodeGeneral.m_iAgentPort = (int)pObject->node.wAgentPort;
834 wndNodeGeneral.m_strCommunity = pObject->node.szCommunityString;
835 wndNodeGeneral.m_iAuthType = pObject->node.wAuthMethod;
836 wndNodeGeneral.m_strSecret = pObject->node.szSharedSecret;
837 wndNodeGeneral.m_iSNMPVersion = (pObject->node.wSNMPVersion == SNMP_VERSION_1) ? 0 : 1;
838 wndPropSheet.AddPage(&wndNodeGeneral);
839 }
840 else
841 {
842 wndObjectGeneral.m_dwObjectId = dwObjectId;
843 wndObjectGeneral.m_strClass = g_szObjectClass[pObject->iClass];
844 wndObjectGeneral.m_strName = pObject->szName;
845 wndPropSheet.AddPage(&wndObjectGeneral);
846 }
847
848 // Create tabs specific for node objects
849 if (pObject->iClass == OBJECT_NODE)
850 {
851 // Create "Capabilities" tab
852 wndObjectCaps.m_pObject = pObject;
853 wndPropSheet.AddPage(&wndObjectCaps);
854 }
855
856 // Create "Security" tab
857 wndObjectSecurity.m_pObject = pObject;
858 wndObjectSecurity.m_bInheritRights = pObject->bInheritRights;
859 wndPropSheet.AddPage(&wndObjectSecurity);
860
861 // Create "Presentation" tab
862 wndObjectPresentation.m_dwImageId = pObject->dwImage;
863 wndObjectPresentation.m_bUseDefaultImage = (pObject->dwImage == IMG_DEFAULT);
864 wndPropSheet.AddPage(&wndObjectPresentation);
865
866 wndPropSheet.SetObject(pObject);
867 if (wndPropSheet.DoModal() == IDOK)
868 wndPropSheet.SaveObjectChanges();
869 }
870 }
871
872
873 //
874 // Get context menu
875 //
876
877 CMenu *CConsoleApp::GetContextMenu(int iIndex)
878 {
879 return m_ctxMenu.GetSubMenu(iIndex);
880 }
881
882
883 //
884 // Start data collection editor for specific object
885 //
886
887 void CConsoleApp::StartObjectDCEditor(NXC_OBJECT *pObject)
888 {
889 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
890 NXC_DCI_LIST *pItemList;
891 DWORD dwResult;
892 CDataCollectionEditor *pWnd;
893
894 pWnd = (CDataCollectionEditor *)FindOpenDCEditor(pObject->dwId);
895 if (pWnd == NULL)
896 {
897 dwResult = DoRequestArg2(NXCOpenNodeDCIList, (void *)pObject->dwId,
898 &pItemList, "Loading node's data collection information...");
899 if (dwResult == RCC_SUCCESS)
900 {
901 CCreateContext context;
902
903 pWnd = new CDataCollectionEditor(pItemList);
904
905 // load the frame
906 context.m_pCurrentFrame = pFrame;
907
908 if (pWnd->LoadFrame(IDR_DC_EDITOR, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, &context))
909 {
910 CString strFullString, strTitle;
911
912 if (strFullString.LoadString(IDR_DC_EDITOR))
913 AfxExtractSubString(strTitle, strFullString, CDocTemplate::docName);
914
915 // add node name to title
916 strTitle += " - [";
917 strTitle += pObject->szName;
918 strTitle += "]";
919
920 // set the handles and redraw the frame and parent
921 pWnd->SetHandles(m_hDCEditorMenu, m_hDCEditorAccel);
922 pWnd->SetTitle(strTitle);
923 pWnd->InitialUpdateFrame(NULL, TRUE);
924 }
925 else
926 {
927 delete pWnd;
928 }
929 }
930 else
931 {
932 ErrorBox(dwResult, "Unable to load data collection information for node:\n%s");
933 }
934 }
935 else
936 {
937 // Data collection editor already open, activate it
938 pWnd->BringWindowToTop();
939 }
940 }
941
942
943 //
944 // Show network summary view
945 //
946
947 void CConsoleApp::OnViewNetworksummary()
948 {
949 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
950
951 // create a new MDI child window or open existing
952 if (m_bNetSummaryActive)
953 m_pwndNetSummary->BringWindowToTop();
954 else
955 pFrame->CreateNewChild(
956 RUNTIME_CLASS(CNetSummaryFrame), IDR_NETWORK_SUMMARY, m_hMDIMenu, m_hMDIAccel);
957 }
958
959
960 //
961 // Set object's management status
962 //
963
964 void CConsoleApp::SetObjectMgmtStatus(NXC_OBJECT *pObject, BOOL bIsManaged)
965 {
966 DWORD dwResult;
967
968 dwResult = DoRequestArg2(NXCSetObjectMgmtStatus, (void *)pObject->dwId,
969 (void *)bIsManaged, "Changing object status...");
970 if (dwResult != RCC_SUCCESS)
971 {
972 char szBuffer[256];
973
974 sprintf(szBuffer, "Unable to change management status for object %s:\n%s",
975 pObject->szName, NXCGetErrorText(dwResult));
976 GetMainWnd()->MessageBox(szBuffer, "Error", MB_ICONSTOP);
977 }
978 }
979
980
981 //
982 // Find open data collection editor for given node, if any
983 //
984
985 CWnd *CConsoleApp::FindOpenDCEditor(DWORD dwNodeId)
986 {
987 DWORD i;
988
989 for(i = 0; i < MAX_DC_EDITORS; i++)
990 if (m_openDCEditors[i].dwNodeId == dwNodeId)
991 return m_openDCEditors[i].pWnd;
992 return NULL;
993 }
994
995
996 //
997 // Display message box with error text from client library
998 //
999
1000 void CConsoleApp::ErrorBox(DWORD dwError, TCHAR *pszMessage, TCHAR *pszTitle)
1001 {
1002 TCHAR szBuffer[512];
1003
1004 _sntprintf(szBuffer, 512, (pszMessage != NULL) ? pszMessage : _T("Error: %s"),
1005 NXCGetErrorText(dwError));
1006 m_pMainWnd->MessageBox(szBuffer, (pszTitle != NULL) ? pszTitle : _T("Error"), MB_ICONSTOP);
1007 }
1008
1009
1010 //
1011 // Show window with DCI's data
1012 //
1013
1014 void CConsoleApp::ShowDCIData(DWORD dwNodeId, DWORD dwItemId, char *pszItemName)
1015 {
1016 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
1017 CCreateContext context;
1018 CDCIDataView *pWnd;
1019
1020 pWnd = new CDCIDataView(dwNodeId, dwItemId);
1021
1022 // load the frame
1023 context.m_pCurrentFrame = pFrame;
1024
1025 if (pWnd->LoadFrame(IDR_DCI_DATA_VIEW, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, &context))
1026 {
1027 CString strFullString, strTitle;
1028
1029 if (strFullString.LoadString(IDR_DCI_DATA_VIEW))
1030 AfxExtractSubString(strTitle, strFullString, CDocTemplate::docName);
1031
1032 // add node name to title
1033 strTitle += " - [";
1034 strTitle += pszItemName;
1035 strTitle += "]";
1036
1037 // set the handles and redraw the frame and parent
1038 pWnd->SetHandles(m_hMDIMenu, m_hMDIAccel);
1039 pWnd->SetTitle(strTitle);
1040 pWnd->InitialUpdateFrame(NULL, TRUE);
1041 }
1042 else
1043 {
1044 delete pWnd;
1045 }
1046 }
1047
1048
1049 //
1050 // Show graph for collected data
1051 //
1052
1053 void CConsoleApp::ShowDCIGraph(DWORD dwNodeId, DWORD dwItemId, char *pszItemName)
1054 {
1055 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
1056 CCreateContext context;
1057 CGraphFrame *pWnd;
1058 DWORD dwCurrTime;
1059
1060 pWnd = new CGraphFrame;
1061 pWnd->AddItem(dwNodeId, dwItemId);
1062 dwCurrTime = time(NULL);
1063 pWnd->SetTimeFrame(dwCurrTime - 3600, dwCurrTime); // Last hour
1064
1065 // load the frame
1066 context.m_pCurrentFrame = pFrame;
1067
1068 if (pWnd->LoadFrame(IDR_DCI_HISTORY_GRAPH, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, &context))
1069 {
1070 CString strFullString, strTitle;
1071
1072 if (strFullString.LoadString(IDR_DCI_HISTORY_GRAPH))
1073 AfxExtractSubString(strTitle, strFullString, CDocTemplate::docName);
1074
1075 // add item name to title
1076 strTitle += " - [";
1077 strTitle += pszItemName;
1078 strTitle += "]";
1079
1080 // set the handles and redraw the frame and parent
1081 pWnd->SetHandles(m_hGraphMenu, m_hGraphAccel);
1082 pWnd->SetTitle(strTitle);
1083 pWnd->InitialUpdateFrame(NULL, TRUE);
1084 }
1085 else
1086 {
1087 delete pWnd;
1088 }
1089 }
1090
1091
1092 //
1093 // WM_COMMAND::ID_TOOLS_MIBBROWSER message handler
1094 //
1095
1096 void CConsoleApp::OnToolsMibbrowser()
1097 {
1098 CMIBBrowserDlg dlg;
1099 dlg.DoModal();
1100 }
1101
1102
1103 //
1104 // WM_COMMAND::ID_CONTROLPANEL_EVENTPOLICY message handler
1105 //
1106
1107 void CConsoleApp::OnControlpanelEventpolicy()
1108 {
1109 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
1110
1111 // create a new MDI child window or open existing
1112 if (m_bEventPolicyEditorActive)
1113 {
1114 m_pwndEventPolicyEditor->BringWindowToTop();
1115 }
1116 else
1117 {
1118 DWORD dwResult;
1119
1120 dwResult = DoRequestArg1(NXCOpenEventPolicy, &m_pEventPolicy, "Loading event processing policy...");
1121 if (dwResult == RCC_SUCCESS)
1122 {
1123 pFrame->CreateNewChild(
1124 RUNTIME_CLASS(CEventPolicyEditor), IDR_EPP_EDITOR, m_hPolicyEditorMenu, m_hPolicyEditorAccel);
1125 }
1126 else
1127 {
1128 ErrorBox(dwResult, "Unable to load event processing policy:\n%s");
1129 }
1130 }
1131 }
1132
1133
1134 //
1135 // WM_COMMAND::ID_VIEW_ALARMS message handler
1136 //
1137
1138 void CConsoleApp::OnViewAlarms()
1139 {
1140 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
1141
1142 // create a new MDI child window or open existing
1143 if (m_bAlarmBrowserActive)
1144 m_pwndAlarmBrowser->BringWindowToTop();
1145 else
1146 pFrame->CreateNewChild(
1147 RUNTIME_CLASS(CAlarmBrowser), IDR_ALARMS, m_hAlarmBrowserMenu, m_hAlarmBrowserAccel);
1148 }
1149
1150
1151 //
1152 // WM_COMMAND::ID_FILE_SETTINGS message handler
1153 //
1154
1155 void CConsoleApp::OnFileSettings()
1156 {
1157 CPropertySheet wndPropSheet("Settings", GetMainWnd(), 0);
1158 CConsolePropsGeneral wndGeneral;
1159
1160 // "General" page
1161 wndGeneral.m_bExpandCtrlPanel = (g_dwOptions & UI_OPT_EXPAND_CTRLPANEL) ? TRUE : FALSE;
1162 wndGeneral.m_bShowGrid = (g_dwOptions & UI_OPT_SHOW_GRID) ? TRUE : FALSE;
1163 wndPropSheet.AddPage(&wndGeneral);
1164
1165 if (wndPropSheet.DoModal() == IDOK)
1166 {
1167 if (wndGeneral.m_bExpandCtrlPanel)
1168 g_dwOptions |= UI_OPT_EXPAND_CTRLPANEL;
1169 else
1170 g_dwOptions &= ~UI_OPT_EXPAND_CTRLPANEL;
1171
1172 if (wndGeneral.m_bShowGrid)
1173 g_dwOptions |= UI_OPT_SHOW_GRID;
1174 else
1175 g_dwOptions &= ~UI_OPT_SHOW_GRID;
1176 }
1177 }
1178
1179
1180 //
1181 // WM_COMMAND::ID_CONTROLPANEL_ACTIONS message handler
1182 //
1183
1184 void CConsoleApp::OnControlpanelActions()
1185 {
1186 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
1187
1188 // create a new MDI child window or open existing
1189 if (m_bActionEditorActive)
1190 {
1191 m_pwndActionEditor->BringWindowToTop();
1192 }
1193 else
1194 {
1195 DWORD dwResult;
1196
1197 dwResult = DoRequest(NXCLockActionDB, "Locking action configuration database...");
1198 if (dwResult == RCC_SUCCESS)
1199 {
1200 pFrame->CreateNewChild(
1201 RUNTIME_CLASS(CActionEditor), IDR_ACTION_EDITOR, m_hActionEditorMenu, m_hActionEditorAccel);
1202 }
1203 else
1204 {
1205 ErrorBox(dwResult, "Unable to lock action configuration database:\n%s");
1206 }
1207 }
1208 }
1209
1210
1211 //
1212 // WM_COMMAND::ID_CONTROLPANEL_SNMPTRAPS message handler
1213 //
1214
1215 void CConsoleApp::OnControlpanelSnmptraps()
1216 {
1217 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
1218
1219 // create a new MDI child window or open existing
1220 if (m_bTrapEditorActive)
1221 {
1222 m_pwndTrapEditor->BringWindowToTop();
1223 }
1224 else
1225 {
1226 DWORD dwResult;
1227
1228 dwResult = DoRequest(NXCLockTrapCfg, "Locking SNMP trap configuration database...");
1229 if (dwResult == RCC_SUCCESS)
1230 {
1231 pFrame->CreateNewChild(
1232 RUNTIME_CLASS(CTrapEditor), IDR_TRAP_EDITOR, m_hTrapEditorMenu, m_hTrapEditorAccel);
1233 }
1234 else
1235 {
1236 ErrorBox(dwResult, "Unable to lock SNMP trap configuration database:\n%s");
1237 }
1238 }
1239 }
1240
1241
1242 //
1243 // Load application menu from resources and modify it as needed
1244 //
1245
1246 HMENU CConsoleApp::LoadAppMenu(HMENU hViewMenu)
1247 {
1248 HMENU hMenu;
1249
1250 hMenu = ::LoadMenu(m_hInstance, MAKEINTRESOURCE(IDR_MAINFRAME));
1251
1252 if (g_dwOptions & UI_OPT_EXPAND_CTRLPANEL)
1253 {
1254 RemoveMenu(hMenu, ID_VIEW_CONTROLPANEL, MF_BYCOMMAND);
1255 InsertMenu(hMenu, ID_VIEW_DEBUG, MF_BYCOMMAND | MF_POPUP, (UINT_PTR)GetSubMenu(hViewMenu, 8), "&Control panel");
1256 }
1257
1258 return hMenu;
1259 }
1260
1261
1262 //
1263 // Create new object
1264 //
1265
1266 void CConsoleApp::CreateObject(NXC_OBJECT_CREATE_INFO *pInfo)
1267 {
1268 DWORD dwResult, dwObjectId;
1269
1270 dwResult = DoRequestArg2(NXCCreateObject, pInfo, &dwObjectId, "Creating object...");
1271 if (dwResult != RCC_SUCCESS)
1272 {
1273 ErrorBox(dwResult, "Error creating object: %s");
1274 }
1275 }
1276
1277
1278 //
1279 // Create container object
1280 //
1281
1282 void CConsoleApp::CreateContainer(DWORD dwParent)
1283 {
1284 NXC_OBJECT_CREATE_INFO ci;
1285 CCreateContainerDlg dlg;
1286
1287 dlg.m_pParentObject = NXCFindObjectById(dwParent);
1288 if (dlg.m_pParentObject != NULL)
1289 if ((dlg.m_pParentObject->iClass != OBJECT_CONTAINER) &&
1290 (dlg.m_pParentObject->iClass != OBJECT_SERVICEROOT))
1291 dlg.m_pParentObject = NULL;
1292 if (dlg.DoModal() == IDOK)
1293 {
1294 ci.dwParentId = (dlg.m_pParentObject != NULL) ? dlg.m_pParentObject->dwId : 0;
1295 ci.iClass = OBJECT_CONTAINER;
1296 ci.pszName = (char *)((LPCTSTR)dlg.m_strObjectName);
1297 ci.cs.container.dwCategory = 1;
1298 ci.cs.container.pszDescription = (char *)((LPCTSTR)dlg.m_strDescription);
1299 CreateObject(&ci);
1300 }
1301 }
1302
1303
1304 //
1305 // Create node object
1306 //
1307
1308 void CConsoleApp::CreateNode(DWORD dwParent)
1309 {
1310 NXC_OBJECT_CREATE_INFO ci;
1311 CCreateNodeDlg dlg;
1312
1313 dlg.m_pParentObject = NXCFindObjectById(dwParent);
1314 if (dlg.m_pParentObject != NULL)
1315 if ((dlg.m_pParentObject->iClass != OBJECT_CONTAINER) &&
1316 (dlg.m_pParentObject->iClass != OBJECT_SERVICEROOT))
1317 dlg.m_pParentObject = NULL;
1318 if (dlg.DoModal() == IDOK)
1319 {
1320 ci.dwParentId = (dlg.m_pParentObject != NULL) ? dlg.m_pParentObject->dwId : 0;
1321 ci.iClass = OBJECT_NODE;
1322 ci.pszName = (char *)((LPCTSTR)dlg.m_strObjectName);
1323 ci.cs.node.dwIpAddr = dlg.m_dwIpAddr;
1324 ci.cs.node.dwNetMask = 0;
1325 CreateObject(&ci);
1326 }
1327 }
1328
1329
1330 //
1331 // Create template object
1332 //
1333
1334 void CConsoleApp::CreateTemplate(DWORD dwParent)
1335 {
1336 NXC_OBJECT_CREATE_INFO ci;
1337 CCreateTemplateDlg dlg;
1338
1339 dlg.m_pParentObject = NXCFindObjectById(dwParent);
1340 if (dlg.m_pParentObject != NULL)
1341 if ((dlg.m_pParentObject->iClass != OBJECT_TEMPLATEGROUP) &&
1342 (dlg.m_pParentObject->iClass != OBJECT_TEMPLATEROOT))
1343 dlg.m_pParentObject = NULL;
1344 if (dlg.DoModal() == IDOK)
1345 {
1346 ci.dwParentId = (dlg.m_pParentObject != NULL) ? dlg.m_pParentObject->dwId : 0;
1347 ci.iClass = OBJECT_TEMPLATE;
1348 ci.pszName = (char *)((LPCTSTR)dlg.m_strObjectName);
1349 CreateObject(&ci);
1350 }
1351 }
1352
1353
1354 //
1355 // Create template group object
1356 //
1357
1358 void CConsoleApp::CreateTemplateGroup(DWORD dwParent)
1359 {
1360 NXC_OBJECT_CREATE_INFO ci;
1361 CCreateTGDlg dlg;
1362
1363 dlg.m_pParentObject = NXCFindObjectById(dwParent);
1364 if (dlg.m_pParentObject != NULL)
1365 if ((dlg.m_pParentObject->iClass != OBJECT_TEMPLATEGROUP) &&
1366 (dlg.m_pParentObject->iClass != OBJECT_TEMPLATEROOT))
1367 dlg.m_pParentObject = NULL;
1368 if (dlg.DoModal() == IDOK)
1369 {
1370 ci.dwParentId = (dlg.m_pParentObject != NULL) ? dlg.m_pParentObject->dwId : 0;
1371 ci.iClass = OBJECT_TEMPLATEGROUP;
1372 ci.pszName = (char *)((LPCTSTR)dlg.m_strObjectName);
1373 ci.cs.templateGroup.pszDescription = (char *)((LPCTSTR)dlg.m_strDescription);
1374 CreateObject(&ci);
1375 }
1376 }
1377
1378
1379 //
1380 // Delete object on server
1381 //
1382
1383 void CConsoleApp::DeleteNetXMSObject(NXC_OBJECT *pObject)
1384 {
1385 DWORD dwResult;
1386
1387 dwResult = DoRequestArg1(NXCDeleteObject, (void *)pObject->dwId, "Deleting object...");
1388 if (dwResult != RCC_SUCCESS)
1389 ErrorBox(dwResult, "Unable to delete object: %s");
1390 }
1391
1392
1393 //
1394 // Perform forced poll on a node
1395 //
1396
1397 void CConsoleApp::PollNode(DWORD dwObjectId, int iPollType)
1398 {
1399 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
1400 CNodePoller *pWnd;
1401
1402 pWnd = (CNodePoller *)pFrame->CreateNewChild(
1403 RUNTIME_CLASS(CNodePoller), IDR_NODE_POLLER, m_hMDIMenu, m_hMDIAccel);
1404 if (pWnd != NULL)
1405 {
1406 pWnd->m_dwObjectId = dwObjectId;
1407 pWnd->m_iPollType = iPollType;
1408 pWnd->PostMessage(WM_COMMAND, ID_POLL_RESTART, 0);
1409 }
1410 }
1411
1412
1413 //
1414 // WM_COMMAND::ID_TOOLS_ADDNODE message handler
1415 //
1416
1417 void CConsoleApp::OnToolsAddnode()
1418 {
1419 CreateNode(0);
1420 }
1421
1422
1423 //
1424 // Wake up node
1425 //
1426
1427 void CConsoleApp::WakeUpNode(DWORD dwObjectId)
1428 {
1429 DWORD dwResult;
1430
1431 dwResult = DoRequestArg1(NXCWakeUpNode, (void *)dwObjectId, _T("Sending Wake-On-LAN magic packet to node..."));
1432 if (dwResult != RCC_SUCCESS)
1433 ErrorBox(dwResult, _T("Unable to send WOL magic packet: %s"));
1434 else
1435 m_pMainWnd->MessageBox(_T("Wake-On-LAN magic packet was successfully sent to node"),
1436 _T("Information"), MB_ICONINFORMATION | MB_OK);
1437 }