Started work on action management GUI
[public/netxms.git] / src / console / win32 / nxcon.cpp
CommitLineData
3bf7e95e
VK
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
10#ifdef _DEBUG
11#define new DEBUG_NEW
12#undef THIS_FILE
13static char THIS_FILE[] = __FILE__;
14#endif
15
16
34870fc0
VK
17//
18// Constants
19//
20
21#define LAST_APP_MENU 4
22
23
3bf7e95e
VK
24//
25// Wrapper for client library event handler
26//
27
28static void ClientEventHandler(DWORD dwEvent, DWORD dwCode, void *pArg)
29{
30 theApp.EventHandler(dwEvent, dwCode, pArg);
31}
32
33
8e748b3e
VK
34//
35// Wrapper for client library debug callback
36//
37
38static void ClientDebugCallback(char *pszMsg)
39{
40 theApp.DebugCallback(pszMsg);
41}
42
43
3bf7e95e
VK
44//
45// CConsoleApp
46//
47
48BEGIN_MESSAGE_MAP(CConsoleApp, CWinApp)
49 //{{AFX_MSG_MAP(CConsoleApp)
50 ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
51 ON_COMMAND(ID_VIEW_CONTROLPANEL, OnViewControlpanel)
52 ON_COMMAND(ID_VIEW_EVENTS, OnViewEvents)
53 ON_COMMAND(ID_VIEW_MAP, OnViewMap)
54 ON_COMMAND(ID_CONNECT_TO_SERVER, OnConnectToServer)
55 ON_COMMAND(ID_VIEW_OBJECTBROWSER, OnViewObjectbrowser)
a5f8f9b0 56 ON_COMMAND(ID_CONTROLPANEL_EVENTS, OnControlpanelEvents)
8e748b3e 57 ON_COMMAND(ID_VIEW_DEBUG, OnViewDebug)
417924d6 58 ON_COMMAND(ID_CONTROLPANEL_USERS, OnControlpanelUsers)
946d48b3 59 ON_COMMAND(ID_VIEW_NETWORKSUMMARY, OnViewNetworksummary)
34870fc0
VK
60 ON_COMMAND(ID_TOOLS_MIBBROWSER, OnToolsMibbrowser)
61 ON_COMMAND(ID_CONTROLPANEL_EVENTPOLICY, OnControlpanelEventpolicy)
ece89362 62 ON_COMMAND(ID_VIEW_ALARMS, OnViewAlarms)
3bf7e95e
VK
63 //}}AFX_MSG_MAP
64END_MESSAGE_MAP()
65
66
67//
68// CConsoleApp construction
69//
70
71CConsoleApp::CConsoleApp()
72{
73 m_bCtrlPanelActive = FALSE;
ece89362 74 m_bAlarmBrowserActive = FALSE;
3bf7e95e 75 m_bEventBrowserActive = FALSE;
a5f8f9b0 76 m_bEventEditorActive = FALSE;
417924d6 77 m_bUserEditorActive = FALSE;
3bf7e95e 78 m_bObjectBrowserActive = FALSE;
8e748b3e 79 m_bDebugWindowActive = FALSE;
946d48b3 80 m_bNetSummaryActive = FALSE;
34870fc0 81 m_bEventPolicyEditorActive = FALSE;
3bf7e95e 82 m_dwClientState = STATE_DISCONNECTED;
f6915462 83 memset(m_openDCEditors, 0, sizeof(DC_EDITOR) * MAX_DC_EDITORS);
3bf7e95e
VK
84}
85
999bd324
VK
86
87//
88// CConsoleApp destruction
89//
90
91CConsoleApp::~CConsoleApp()
92{
999bd324
VK
93}
94
95
3bf7e95e
VK
96//
97// The one and only CConsoleApp object
98//
99
100CConsoleApp theApp;
101
102
34870fc0
VK
103//
104// Setup working directory
105//
106
107BOOL CConsoleApp::SetupWorkDir()
108{
109 int iLastChar;
110
111 // Get path to user's "Application Data" folder
112 if (SHGetFolderPath(NULL, CSIDL_APPDATA, NULL, SHGFP_TYPE_CURRENT, g_szWorkDir) != S_OK)
113 {
114 AfxMessageBox(IDS_GETFOLDERPATH_FAILED, MB_OK | MB_ICONSTOP);
115 return FALSE;
116 }
117
118 // Create working directory root
119 strcat(g_szWorkDir, "\\NetXMS Console");
120 if (!CreateDirectory(g_szWorkDir, NULL))
121 if (GetLastError() != ERROR_ALREADY_EXISTS)
122 {
123 AfxMessageBox(IDS_WORKDIR_CREATION_FAILED, MB_OK | MB_ICONSTOP);
124 return FALSE;
125 }
126 iLastChar = strlen(g_szWorkDir);
127
128 // Create MIB cache directory
129 strcpy(&g_szWorkDir[iLastChar], WORKDIR_MIBCACHE);
130 if (!CreateDirectory(g_szWorkDir, NULL))
131 if (GetLastError() != ERROR_ALREADY_EXISTS)
132 {
133 AfxMessageBox(IDS_WORKDIR_CREATION_FAILED, MB_OK | MB_ICONSTOP);
134 return FALSE;
135 }
136
f70fc197
VK
137 // Create image cache directory
138 strcpy(&g_szWorkDir[iLastChar], WORKDIR_IMAGECACHE);
139 if (!CreateDirectory(g_szWorkDir, NULL))
140 if (GetLastError() != ERROR_ALREADY_EXISTS)
141 {
142 AfxMessageBox(IDS_WORKDIR_CREATION_FAILED, MB_OK | MB_ICONSTOP);
143 return FALSE;
144 }
145
34870fc0
VK
146 // Success
147 g_szWorkDir[iLastChar] = 0;
148 return TRUE;
149}
150
151
3bf7e95e
VK
152//
153// CConsoleApp initialization
154//
155
156BOOL CConsoleApp::InitInstance()
157{
158 BOOL bSetWindowPos;
159 DWORD dwBytes;
160 BYTE *pData;
8e748b3e 161 HMENU hMenu;
3bf7e95e 162
34870fc0
VK
163 // Setup our working directory
164 if (!SetupWorkDir())
165 return FALSE;
166
3bf7e95e
VK
167 if (!AfxSocketInit())
168 {
169 AfxMessageBox(IDP_SOCKETS_INIT_FAILED, MB_OK | MB_ICONSTOP);
170 return FALSE;
171 }
172
173 if (!NXCInitialize())
174 {
175 AfxMessageBox(IDS_NXC_INIT_FAILED, MB_OK | MB_ICONSTOP);
176 return FALSE;
177 }
178 NXCSetEventHandler(ClientEventHandler);
179
180 AfxEnableControlContainer();
181
182 // Standard initialization
183 // If you are not using these features and wish to reduce the size
184 // of your final executable, you should remove from the following
185 // the specific initialization routines you do not need.
186
187#ifdef _AFXDLL
188 Enable3dControls(); // Call this when using MFC in a shared DLL
189#else
190 Enable3dControlsStatic(); // Call this when linking to MFC statically
191#endif
192
193 // Change the registry key under which our settings are stored.
194 SetRegistryKey(_T("NetXMS"));
195
c7863da1
VK
196 // Create mutex for action list access
197 g_mutexActionListAccess = CreateMutex(NULL, FALSE, NULL);
198
3bf7e95e
VK
199 // To create the main window, this code creates a new frame window
200 // object and then sets it as the application's main window object.
3bf7e95e
VK
201 CMDIFrameWnd* pFrame = new CMainFrame;
202 m_pMainWnd = pFrame;
203
204 // create main MDI frame window
205 if (!pFrame->LoadFrame(IDR_MAINFRAME))
206 return FALSE;
207
790cd256
VK
208 // Load context menus
209 if (!m_ctxMenu.LoadMenu(IDM_CONTEXT))
210 MessageBox(NULL, "FAILED", "", 0);
211
8e748b3e
VK
212 // Load shared MDI menus and accelerator table
213 HINSTANCE hInstance = AfxGetResourceHandle();
214
215 hMenu = ::LoadMenu(hInstance, MAKEINTRESOURCE(IDM_VIEW_SPECIFIC));
216
217 m_hMDIMenu = ::LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MAINFRAME));
34870fc0 218 InsertMenu(m_hMDIMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
8e748b3e
VK
219
220 m_hEventBrowserMenu = ::LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MAINFRAME));
34870fc0
VK
221 InsertMenu(m_hEventBrowserMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
222 InsertMenu(m_hEventBrowserMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 1), "&Event");
8e748b3e 223
2060ba91 224 m_hObjectBrowserMenu = ::LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MAINFRAME));
34870fc0
VK
225 InsertMenu(m_hObjectBrowserMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
226 InsertMenu(m_hObjectBrowserMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 2), "&Object");
2060ba91 227
cad81e33 228 m_hUserEditorMenu = ::LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MAINFRAME));
34870fc0
VK
229 InsertMenu(m_hUserEditorMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
230 InsertMenu(m_hUserEditorMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 3), "&User");
cad81e33 231
1c0c8dde 232 m_hDCEditorMenu = ::LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MAINFRAME));
34870fc0
VK
233 InsertMenu(m_hDCEditorMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
234 InsertMenu(m_hDCEditorMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 4), "&Item");
1c0c8dde 235
ef44d5ea
VK
236 m_hPolicyEditorMenu = ::LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MAINFRAME));
237 InsertMenu(m_hPolicyEditorMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
238 InsertMenu(m_hPolicyEditorMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 5), "&Policy");
239
5cac98d8
VK
240 m_hAlarmBrowserMenu = ::LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MAINFRAME));
241 InsertMenu(m_hAlarmBrowserMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
242 InsertMenu(m_hAlarmBrowserMenu, LAST_APP_MENU, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 6), "&Alarm");
243
8e748b3e 244 m_hMDIAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_MDI_DEFAULT));
5cac98d8 245 m_hAlarmBrowserAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_ALARM_BROWSER));
8e748b3e 246 m_hEventBrowserAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_MDI_DEFAULT));
2060ba91 247 m_hObjectBrowserAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_OBJECT_BROWSER));
cad81e33 248 m_hUserEditorAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_MDI_DEFAULT));
1c0c8dde 249 m_hDCEditorAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_MDI_DEFAULT));
ef44d5ea 250 m_hPolicyEditorAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_EPP));
3bf7e95e
VK
251
252 // Load configuration from registry
253 dwBytes = sizeof(WINDOWPLACEMENT);
254 bSetWindowPos = GetProfileBinary(_T("General"), _T("WindowPlacement"),
255 &pData, (UINT *)&dwBytes);
256 strcpy(g_szServer, (LPCTSTR)GetProfileString(_T("Connection"), _T("Server"), _T("localhost")));
257 strcpy(g_szLogin, (LPCTSTR)GetProfileString(_T("Connection"), _T("Login"), NULL));
258
259 // The main window has been initialized, so show and update it.
260 if (bSetWindowPos)
261 {
262 pFrame->SetWindowPlacement((WINDOWPLACEMENT *)pData);
263 if (pFrame->IsIconic())
264 pFrame->ShowWindow(SW_RESTORE);
265 }
266 else
267 {
268 pFrame->ShowWindow(m_nCmdShow);
269 }
270 pFrame->UpdateWindow();
34870fc0 271 //pFrame->PostMessage(WM_COMMAND, ID_VIEW_DEBUG, 0);
3bf7e95e
VK
272 pFrame->PostMessage(WM_COMMAND, ID_CONNECT_TO_SERVER, 0);
273
274 return TRUE;
275}
276
277/////////////////////////////////////////////////////////////////////////////
278// CConsoleApp message handlers
279
280int CConsoleApp::ExitInstance()
281{
8e748b3e
VK
282 NXCSetDebugCallback(NULL);
283 NXCDisconnect();
9cf2ab6f 284 NXCShutdown();
8e748b3e 285
3bf7e95e 286 // Free resources
8e748b3e
VK
287 SafeFreeResource(m_hMDIMenu);
288 SafeFreeResource(m_hMDIAccel);
5cac98d8
VK
289 SafeFreeResource(m_hAlarmBrowserMenu);
290 SafeFreeResource(m_hAlarmBrowserAccel);
8e748b3e
VK
291 SafeFreeResource(m_hEventBrowserMenu);
292 SafeFreeResource(m_hEventBrowserAccel);
1c0c8dde
VK
293 SafeFreeResource(m_hUserEditorMenu);
294 SafeFreeResource(m_hUserEditorAccel);
295 SafeFreeResource(m_hDCEditorMenu);
296 SafeFreeResource(m_hDCEditorAccel);
3bf7e95e 297
c7863da1
VK
298 CloseHandle(g_mutexActionListAccess);
299
cc068873 300 return CWinApp::ExitInstance();
3bf7e95e
VK
301}
302
303
304/////////////////////////////////////////////////////////////////////////////
305// CAboutDlg dialog used for App About
306
307class CAboutDlg : public CDialog
308{
309public:
310 CAboutDlg();
311
312// Dialog Data
313 //{{AFX_DATA(CAboutDlg)
314 enum { IDD = IDD_ABOUTBOX };
315 //}}AFX_DATA
316
317 // ClassWizard generated virtual function overrides
318 //{{AFX_VIRTUAL(CAboutDlg)
319 protected:
320 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
321 //}}AFX_VIRTUAL
322
323// Implementation
324protected:
325 //{{AFX_MSG(CAboutDlg)
326 // No message handlers
327 //}}AFX_MSG
328 DECLARE_MESSAGE_MAP()
329};
330
331CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
332{
333 //{{AFX_DATA_INIT(CAboutDlg)
334 //}}AFX_DATA_INIT
335}
336
337void CAboutDlg::DoDataExchange(CDataExchange* pDX)
338{
339 CDialog::DoDataExchange(pDX);
340 //{{AFX_DATA_MAP(CAboutDlg)
341 //}}AFX_DATA_MAP
342}
343
344BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
345 //{{AFX_MSG_MAP(CAboutDlg)
346 // No message handlers
347 //}}AFX_MSG_MAP
348END_MESSAGE_MAP()
349
350// App command to run the dialog
351void CConsoleApp::OnAppAbout()
352{
353 CAboutDlg aboutDlg;
354 aboutDlg.DoModal();
355}
356
357/////////////////////////////////////////////////////////////////////////////
358// CConsoleApp message handlers
359
360
361void CConsoleApp::OnViewControlpanel()
362{
363 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
364
365 // create a new MDI child window or open existing
366 if (m_bCtrlPanelActive)
367 m_pwndCtrlPanel->BringWindowToTop();
368 else
369 pFrame->CreateNewChild(
8e748b3e 370 RUNTIME_CLASS(CControlPanel), IDR_CTRLPANEL, m_hMDIMenu, m_hMDIAccel);
3bf7e95e
VK
371}
372
373
374//
375// This method called when new view is created
376//
377
f6915462 378void CConsoleApp::OnViewCreate(DWORD dwView, CWnd *pWnd, DWORD dwArg)
3bf7e95e 379{
f6915462
VK
380 DWORD i;
381
3bf7e95e
VK
382 switch(dwView)
383 {
384 case IDR_CTRLPANEL:
385 m_bCtrlPanelActive = TRUE;
386 m_pwndCtrlPanel = pWnd;
387 break;
ece89362
VK
388 case IDR_ALARMS:
389 m_bAlarmBrowserActive = TRUE;
390 m_pwndAlarmBrowser = (CAlarmBrowser *)pWnd;
391 break;
3bf7e95e
VK
392 case IDR_EVENTS:
393 m_bEventBrowserActive = TRUE;
394 m_pwndEventBrowser = (CEventBrowser *)pWnd;
395 break;
396 case IDR_OBJECTS:
397 m_bObjectBrowserActive = TRUE;
398 m_pwndObjectBrowser = pWnd;
399 break;
a5f8f9b0
VK
400 case IDR_EVENT_EDITOR:
401 m_bEventEditorActive = TRUE;
402 m_pwndEventEditor = (CEventEditor *)pWnd;
403 break;
417924d6
VK
404 case IDR_USER_EDITOR:
405 m_bUserEditorActive = TRUE;
406 m_pwndUserEditor = (CUserEditor *)pWnd;
407 break;
8e748b3e
VK
408 case IDR_DEBUG_WINDOW:
409 m_bDebugWindowActive = TRUE;
410 m_pwndDebugWindow = (CDebugFrame *)pWnd;
411 NXCSetDebugCallback(ClientDebugCallback);
412 break;
946d48b3
VK
413 case IDR_NETWORK_SUMMARY:
414 m_bNetSummaryActive = TRUE;
415 m_pwndNetSummary = (CNetSummaryFrame *)pWnd;
416 break;
f6915462
VK
417 case IDR_DC_EDITOR:
418 // Register new DC editor
419 for(i = 0; i < MAX_DC_EDITORS; i++)
420 if (m_openDCEditors[i].pWnd == NULL)
421 {
422 m_openDCEditors[i].pWnd = pWnd;
423 m_openDCEditors[i].dwNodeId = dwArg;
424 break;
425 }
426 break;
34870fc0
VK
427 case IDR_EPP_EDITOR:
428 m_bEventPolicyEditorActive = TRUE;
429 m_pwndEventPolicyEditor = (CEventPolicyEditor *)pWnd;
430 break;
3bf7e95e
VK
431 default:
432 break;
433 }
434}
435
436
437//
438// This method called when view is destroyed
439//
440
f6915462 441void CConsoleApp::OnViewDestroy(DWORD dwView, CWnd *pWnd, DWORD dwArg)
3bf7e95e 442{
f6915462
VK
443 DWORD i;
444
3bf7e95e
VK
445 switch(dwView)
446 {
447 case IDR_CTRLPANEL:
448 m_bCtrlPanelActive = FALSE;
449 break;
ece89362
VK
450 case IDR_ALARMS:
451 m_bAlarmBrowserActive = FALSE;
452 break;
3bf7e95e
VK
453 case IDR_EVENTS:
454 m_bEventBrowserActive = FALSE;
455 break;
456 case IDR_OBJECTS:
457 m_bObjectBrowserActive = FALSE;
458 break;
a5f8f9b0
VK
459 case IDR_EVENT_EDITOR:
460 m_bEventEditorActive = FALSE;
461 break;
417924d6
VK
462 case IDR_USER_EDITOR:
463 m_bUserEditorActive = FALSE;
464 break;
8e748b3e
VK
465 case IDR_DEBUG_WINDOW:
466 m_bDebugWindowActive = FALSE;
467 NXCSetDebugCallback(NULL);
468 break;
946d48b3
VK
469 case IDR_NETWORK_SUMMARY:
470 m_bNetSummaryActive = FALSE;
471 break;
f6915462
VK
472 case IDR_DC_EDITOR:
473 // Unregister DC editor
474 for(i = 0; i < MAX_DC_EDITORS; i++)
475 if (m_openDCEditors[i].pWnd == pWnd)
476 {
477 m_openDCEditors[i].pWnd = NULL;
478 m_openDCEditors[i].dwNodeId = 0;
479 break;
480 }
481 break;
34870fc0
VK
482 case IDR_EPP_EDITOR:
483 m_bEventPolicyEditorActive = FALSE;
484 break;
3bf7e95e
VK
485 default:
486 break;
487 }
488}
489
7ac0c0a8
VK
490
491//
492// WM_COMMAND::ID_VIEW_EVENTS message handler
493//
494
3bf7e95e
VK
495void CConsoleApp::OnViewEvents()
496{
497 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
498
499 // create a new MDI child window or open existing
500 if (m_bEventBrowserActive)
501 m_pwndEventBrowser->BringWindowToTop();
502 else
503 pFrame->CreateNewChild(
8e748b3e 504 RUNTIME_CLASS(CEventBrowser), IDR_EVENTS, m_hEventBrowserMenu, m_hEventBrowserAccel);
3bf7e95e
VK
505}
506
7ac0c0a8
VK
507
508//
509// WM_COMMAND::ID_VIEW_MAP message handler
510//
511
3bf7e95e
VK
512void CConsoleApp::OnViewMap()
513{
514 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
515
516 // create a new MDI child window
f7a7f8e4 517 pFrame->CreateNewChild(RUNTIME_CLASS(CMapFrame), IDR_MAPFRAME, m_hMDIMenu, m_hMDIAccel);
3bf7e95e
VK
518}
519
7ac0c0a8
VK
520
521//
522// WM_COMMAND::ID_CONNECT_TO_SERVER message handler
523//
524
3bf7e95e
VK
525void CConsoleApp::OnConnectToServer()
526{
527 CLoginDialog dlgLogin;
f7a7f8e4 528 DWORD dwResult;
3bf7e95e
VK
529
530 dlgLogin.m_szServer = g_szServer;
531 dlgLogin.m_szLogin = g_szLogin;
f7a7f8e4 532 do
3bf7e95e
VK
533 {
534 if (dlgLogin.DoModal() != IDOK)
535 {
536 PostQuitMessage(1);
537 break;
538 }
539 strcpy(g_szServer, (LPCTSTR)dlgLogin.m_szServer);
540 strcpy(g_szLogin, (LPCTSTR)dlgLogin.m_szLogin);
541 strcpy(g_szPassword, (LPCTSTR)dlgLogin.m_szPassword);
542
543 // Save last connection parameters
544 WriteProfileString(_T("Connection"), _T("Server"), g_szServer);
545 WriteProfileString(_T("Connection"), _T("Login"), g_szLogin);
546
547 // Initiate connection
f7a7f8e4
VK
548 dwResult = DoLogin();
549 if (dwResult != RCC_SUCCESS)
7ac0c0a8 550 ErrorBox(dwResult, "Unable to connect: %s", "Connection error");
3bf7e95e 551 }
f7a7f8e4 552 while(dwResult != RCC_SUCCESS);
3bf7e95e
VK
553}
554
7ac0c0a8
VK
555
556//
557// WM_COMMAND::ID_VIEW_OBJECTBROWSER message handler
558//
559
3bf7e95e
VK
560void CConsoleApp::OnViewObjectbrowser()
561{
562 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
563
564 // create a new MDI child window or open existing
565 if (m_bObjectBrowserActive)
566 m_pwndObjectBrowser->BringWindowToTop();
567 else
568 pFrame->CreateNewChild(
2060ba91 569 RUNTIME_CLASS(CObjectBrowser), IDR_OBJECTS, m_hObjectBrowserMenu, m_hObjectBrowserAccel);
3bf7e95e
VK
570}
571
572
573//
574// Event handler for client libray events
575//
576
577void CConsoleApp::EventHandler(DWORD dwEvent, DWORD dwCode, void *pArg)
578{
3bf7e95e
VK
579 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
580
581 switch(dwEvent)
582 {
583 case NXC_EVENT_STATE_CHANGED:
3bf7e95e 584 m_dwClientState = dwCode;
9cf2ab6f 585 ((CMainFrame *)m_pMainWnd)->PostMessage(WM_STATE_CHANGE, dwCode, (LPARAM)pArg);
3bf7e95e 586 break;
3bf7e95e
VK
587 case NXC_EVENT_NEW_ELOG_RECORD:
588 if (m_bEventBrowserActive)
589 m_pwndEventBrowser->AddEvent((NXC_EVENT *)pArg);
9d72bde1 590 free(pArg);
3bf7e95e 591 break;
89d4d2a2
VK
592 case NXC_EVENT_OBJECT_CHANGED:
593 ((CMainFrame *)m_pMainWnd)->PostMessage(WM_OBJECT_CHANGE, dwCode, (LPARAM)pArg);
594 break;
fadbdf37
VK
595 case NXC_EVENT_USER_DB_CHANGED:
596 ((CMainFrame *)m_pMainWnd)->PostMessage(WM_USERDB_CHANGE, dwCode, (LPARAM)pArg);
597 break;
3421c063
VK
598 case NXC_EVENT_NOTIFICATION:
599 switch(dwCode)
600 {
601 case NX_NOTIFY_SHUTDOWN:
602 m_pMainWnd->MessageBox("Server was shutdown", "Warning", MB_OK | MB_ICONSTOP);
603 PostQuitMessage(0);
604 break;
605 case NX_NOTIFY_EVENTDB_CHANGED:
606 m_pMainWnd->PostMessage(WM_COMMAND, ID_UPDATE_EVENT_LIST, 0);
607 break;
a10722f0
VK
608 case NX_NOTIFY_NEW_ALARM:
609 case NX_NOTIFY_ALARM_DELETED:
610 case NX_NOTIFY_ALARM_ACKNOWLEGED:
611 m_pMainWnd->PostMessage(WM_ALARM_UPDATE, dwCode,
612 (LPARAM)nx_memdup(pArg, sizeof(NXC_ALARM)));
613 break;
3421c063
VK
614 default:
615 break;
616 }
617 break;
3bf7e95e
VK
618 default:
619 break;
620 }
621}
a5f8f9b0 622
999bd324
VK
623
624//
625// Handler for WM_COMMAND(ID_CONTROLPANEL_EVENTS) message
626//
627
a5f8f9b0
VK
628void CConsoleApp::OnControlpanelEvents()
629{
630 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
631
632 // create a new MDI child window or open existing
633 if (m_bEventEditorActive)
634 m_pwndEventEditor->BringWindowToTop();
635 else
417924d6
VK
636 {
637 DWORD dwResult;
638
f7a7f8e4 639 dwResult = DoRequest(NXCOpenEventDB, "Opening event configuration database...");
417924d6
VK
640 if (dwResult == RCC_SUCCESS)
641 {
642 pFrame->CreateNewChild(
643 RUNTIME_CLASS(CEventEditor), IDR_EVENT_EDITOR, m_hMDIMenu, m_hMDIAccel);
644 }
645 else
646 {
7ac0c0a8 647 ErrorBox(dwResult, "Unable to open event configuration database:\n%s");
417924d6
VK
648 }
649 }
650}
651
652
653//
7ac0c0a8 654// Handler for WM_COMMAND::ID_CONTROLPANEL_USERS message
417924d6
VK
655//
656
657void CConsoleApp::OnControlpanelUsers()
658{
659 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
660
661 // create a new MDI child window or open existing
662 if (m_bUserEditorActive)
663 m_pwndUserEditor->BringWindowToTop();
664 else
665 {
666 DWORD dwResult;
667
f7a7f8e4 668 dwResult = DoRequest(NXCLockUserDB, "Locking user database...");
417924d6
VK
669 if (dwResult == RCC_SUCCESS)
670 {
671 pFrame->CreateNewChild(
cad81e33 672 RUNTIME_CLASS(CUserEditor), IDR_USER_EDITOR, m_hUserEditorMenu, m_hUserEditorAccel);
417924d6
VK
673 }
674 else
675 {
7ac0c0a8 676 ErrorBox(dwResult, "Unable to lock user database:\n%s");
417924d6
VK
677 }
678 }
a5f8f9b0
VK
679}
680
681
8e748b3e
VK
682//
683// Handler for WM_COMMAND::ID_VIEW_DEBUG message
684//
685
686void CConsoleApp::OnViewDebug()
687{
688 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
689
690 // create a new MDI child window or open existing
691 if (m_bDebugWindowActive)
692 m_pwndDebugWindow->BringWindowToTop();
693 else
694 pFrame->CreateNewChild(
695 RUNTIME_CLASS(CDebugFrame), IDR_DEBUG_WINDOW, m_hMDIMenu, m_hMDIAccel);
696}
89d4d2a2
VK
697
698
699//
700// Print debug information
701//
702
703void CConsoleApp::DebugPrintf(char *szFormat, ...)
704{
705 if (m_bDebugWindowActive)
706 {
707 char szBuffer[1024];
708 va_list args;
709
710 va_start(args, szFormat);
711 vsprintf(szBuffer, szFormat, args);
712 va_end(args);
713
714 m_pwndDebugWindow->AddMessage(szBuffer);
715 }
716}
2057008d
VK
717
718
719//
720// Edit properties of specific object
721//
722
723void CConsoleApp::ObjectProperties(DWORD dwObjectId)
724{
725 CObjectPropSheet wndPropSheet("Object Properties", GetMainWnd(), 0);
726 CNodePropsGeneral wndNodeGeneral;
8e2809bb 727 CObjectPropsGeneral wndObjectGeneral;
2057008d 728 CObjectPropCaps wndObjectCaps;
8e2809bb 729 CObjectPropsSecurity wndObjectSecurity;
2a41a4b5 730 CObjectPropsPresentation wndObjectPresentation;
2057008d
VK
731 NXC_OBJECT *pObject;
732 char szBuffer[32];
733
734 pObject = NXCFindObjectById(dwObjectId);
735 if (pObject != NULL)
736 {
737 // Create "General" tab
8e2809bb 738 if (pObject->iClass == OBJECT_NODE)
2057008d 739 {
8e2809bb
VK
740 wndNodeGeneral.m_dwObjectId = dwObjectId;
741 wndNodeGeneral.m_strName = pObject->szName;
742 wndNodeGeneral.m_strOID = pObject->node.szObjectId;
743 wndNodeGeneral.m_strPrimaryIp = IpToStr(pObject->dwIpAddr, szBuffer);
744 wndNodeGeneral.m_iAgentPort = (int)pObject->node.wAgentPort;
745 wndNodeGeneral.m_strCommunity = pObject->node.szCommunityString;
746 wndNodeGeneral.m_iAuthType = pObject->node.wAuthMethod;
747 wndNodeGeneral.m_strSecret = pObject->node.szSharedSecret;
748 wndPropSheet.AddPage(&wndNodeGeneral);
749 }
750 else
751 {
752 wndObjectGeneral.m_dwObjectId = dwObjectId;
753 wndObjectGeneral.m_strClass = g_szObjectClass[pObject->iClass];
754 wndObjectGeneral.m_strName = pObject->szName;
755 wndPropSheet.AddPage(&wndObjectGeneral);
2057008d
VK
756 }
757
758 // Create tabs specific for node objects
759 if (pObject->iClass == OBJECT_NODE)
760 {
761 // Create "Capabilities" tab
762 wndObjectCaps.m_pObject = pObject;
763 wndPropSheet.AddPage(&wndObjectCaps);
764 }
765
f6d9527a 766 // Create "Security" tab
8e2809bb
VK
767 wndObjectSecurity.m_pObject = pObject;
768 wndObjectSecurity.m_bInheritRights = pObject->bInheritRights;
769 wndPropSheet.AddPage(&wndObjectSecurity);
f6d9527a 770
2a41a4b5
VK
771 // Create "Presentation" tab
772 wndObjectPresentation.m_dwImageId = pObject->dwImage;
773 wndObjectPresentation.m_bUseDefaultImage = (pObject->dwImage == IMG_DEFAULT);
774 wndPropSheet.AddPage(&wndObjectPresentation);
775
f6d9527a
VK
776 wndPropSheet.SetObject(pObject);
777 if (wndPropSheet.DoModal() == IDOK)
778 wndPropSheet.SaveObjectChanges();
2057008d
VK
779 }
780}
790cd256
VK
781
782
783//
784// Get context menu
785//
786
787CMenu *CConsoleApp::GetContextMenu(int iIndex)
788{
789 return m_ctxMenu.GetSubMenu(iIndex);
790}
946d48b3
VK
791
792
793//
794// Start data collection editor for specific object
795//
796
797void CConsoleApp::StartObjectDCEditor(NXC_OBJECT *pObject)
798{
223ffcfa
VK
799 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
800 NXC_DCI_LIST *pItemList;
801 DWORD dwResult;
f6915462 802 CDataCollectionEditor *pWnd;
946d48b3 803
f6915462
VK
804 pWnd = (CDataCollectionEditor *)FindOpenDCEditor(pObject->dwId);
805 if (pWnd == NULL)
223ffcfa 806 {
f6915462
VK
807 dwResult = DoRequestArg2(NXCOpenNodeDCIList, (void *)pObject->dwId,
808 &pItemList, "Loading node's data collection information...");
809 if (dwResult == RCC_SUCCESS)
810 {
811 CCreateContext context;
812
813 pWnd = new CDataCollectionEditor(pItemList);
814
815 // load the frame
816 context.m_pCurrentFrame = pFrame;
817
818 if (pWnd->LoadFrame(IDR_DC_EDITOR, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, &context))
819 {
820 CString strFullString, strTitle;
821
822 if (strFullString.LoadString(IDR_DC_EDITOR))
823 AfxExtractSubString(strTitle, strFullString, CDocTemplate::docName);
824
825 // add node name to title
826 strTitle += " - [";
827 strTitle += pObject->szName;
828 strTitle += "]";
829
830 // set the handles and redraw the frame and parent
1c0c8dde 831 pWnd->SetHandles(m_hDCEditorMenu, m_hDCEditorAccel);
f6915462
VK
832 pWnd->SetTitle(strTitle);
833 pWnd->InitialUpdateFrame(NULL, TRUE);
834 }
835 else
836 {
837 delete pWnd;
838 }
839 }
840 else
841 {
7ac0c0a8 842 ErrorBox(dwResult, "Unable to load data collection information for node:\n%s");
f6915462 843 }
223ffcfa
VK
844 }
845 else
846 {
f6915462
VK
847 // Data collection editor already open, activate it
848 pWnd->BringWindowToTop();
223ffcfa 849 }
946d48b3
VK
850}
851
852
853//
854// Show network summary view
855//
856
857void CConsoleApp::OnViewNetworksummary()
858{
859 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
860
861 // create a new MDI child window or open existing
862 if (m_bNetSummaryActive)
863 m_pwndNetSummary->BringWindowToTop();
864 else
865 pFrame->CreateNewChild(
866 RUNTIME_CLASS(CNetSummaryFrame), IDR_NETWORK_SUMMARY, m_hMDIMenu, m_hMDIAccel);
867}
223ffcfa
VK
868
869
870//
871// Set object's management status
872//
873
874void CConsoleApp::SetObjectMgmtStatus(NXC_OBJECT *pObject, BOOL bIsManaged)
875{
876 DWORD dwResult;
877
878 dwResult = DoRequestArg2(NXCSetObjectMgmtStatus, (void *)pObject->dwId,
879 (void *)bIsManaged, "Changing object status...");
880 if (dwResult != RCC_SUCCESS)
881 {
882 char szBuffer[256];
883
884 sprintf(szBuffer, "Unable to change management status for object %s:\n%s",
885 pObject->szName, NXCGetErrorText(dwResult));
886 GetMainWnd()->MessageBox(szBuffer, "Error", MB_ICONSTOP);
887 }
888}
f6915462
VK
889
890
891//
892// Find open data collection editor for given node, if any
893//
894
895CWnd *CConsoleApp::FindOpenDCEditor(DWORD dwNodeId)
896{
897 DWORD i;
898
899 for(i = 0; i < MAX_DC_EDITORS; i++)
900 if (m_openDCEditors[i].dwNodeId == dwNodeId)
901 return m_openDCEditors[i].pWnd;
902 return NULL;
903}
7ac0c0a8
VK
904
905
906//
907// Display message box with error text from client library
908//
909
910void CConsoleApp::ErrorBox(DWORD dwError, char *pszMessage, char *pszTitle)
911{
912 char szBuffer[512];
913
914 sprintf(szBuffer, (pszMessage != NULL) ? pszMessage : "Error: %s",
915 NXCGetErrorText(dwError));
916 m_pMainWnd->MessageBox(szBuffer, (pszTitle != NULL) ? pszTitle : "Error", MB_ICONSTOP);
917}
9f20696e
VK
918
919
920//
921// Show window with DCI's data
922//
923
924void CConsoleApp::ShowDCIData(DWORD dwNodeId, DWORD dwItemId, char *pszItemName)
925{
926 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
927 CCreateContext context;
928 CDCIDataView *pWnd;
929
930 pWnd = new CDCIDataView(dwNodeId, dwItemId);
931
932 // load the frame
933 context.m_pCurrentFrame = pFrame;
934
935 if (pWnd->LoadFrame(IDR_DCI_DATA_VIEW, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, &context))
936 {
937 CString strFullString, strTitle;
938
939 if (strFullString.LoadString(IDR_DCI_DATA_VIEW))
940 AfxExtractSubString(strTitle, strFullString, CDocTemplate::docName);
941
942 // add node name to title
943 strTitle += " - [";
944 strTitle += pszItemName;
945 strTitle += "]";
946
947 // set the handles and redraw the frame and parent
948 pWnd->SetHandles(m_hMDIMenu, m_hMDIAccel);
949 pWnd->SetTitle(strTitle);
950 pWnd->InitialUpdateFrame(NULL, TRUE);
951 }
952 else
953 {
954 delete pWnd;
955 }
956}
957
958
959//
23e62c09 960// Show graph for collected data
9f20696e
VK
961//
962
23e62c09 963void CConsoleApp::ShowDCIGraph(DWORD dwNodeId, DWORD dwItemId, char *pszItemName)
9f20696e 964{
23e62c09
VK
965 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
966 CCreateContext context;
967 CGraphFrame *pWnd;
968 DWORD dwCurrTime;
969
970 pWnd = new CGraphFrame;
971 pWnd->AddItem(dwNodeId, dwItemId);
972 dwCurrTime = time(NULL);
973 pWnd->SetTimeFrame(dwCurrTime - 3600, dwCurrTime); // Last hour
9f20696e 974
23e62c09
VK
975 // load the frame
976 context.m_pCurrentFrame = pFrame;
977
978 if (pWnd->LoadFrame(IDR_DCI_HISTORY_GRAPH, WS_OVERLAPPEDWINDOW | FWS_ADDTOTITLE, NULL, &context))
9f20696e 979 {
23e62c09
VK
980 CString strFullString, strTitle;
981
982 if (strFullString.LoadString(IDR_DCI_HISTORY_GRAPH))
983 AfxExtractSubString(strTitle, strFullString, CDocTemplate::docName);
984
985 // add item name to title
986 strTitle += " - [";
987 strTitle += pszItemName;
988 strTitle += "]";
989
990 // set the handles and redraw the frame and parent
991 pWnd->SetHandles(m_hMDIMenu, m_hMDIAccel);
992 pWnd->SetTitle(strTitle);
993 pWnd->InitialUpdateFrame(NULL, TRUE);
9f20696e 994 }
23e62c09
VK
995 else
996 {
997 delete pWnd;
998 }
9f20696e 999}
34870fc0
VK
1000
1001
1002//
1003// WM_COMMAND::ID_TOOLS_MIBBROWSER message handler
1004//
1005
1006void CConsoleApp::OnToolsMibbrowser()
1007{
1008 CMIBBrowserDlg dlg;
1009 dlg.DoModal();
1010}
1011
1012
1013//
1014// WM_COMMAND::ID_CONTROLPANEL_EVENTPOLICY message handler
1015//
1016
1017void CConsoleApp::OnControlpanelEventpolicy()
1018{
1019 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
1020
1021 // create a new MDI child window or open existing
1022 if (m_bEventPolicyEditorActive)
618d6256 1023 {
34870fc0 1024 m_pwndEventPolicyEditor->BringWindowToTop();
618d6256 1025 }
34870fc0
VK
1026 else
1027 {
1028 DWORD dwResult;
1029
1030 dwResult = DoRequestArg1(NXCOpenEventPolicy, &m_pEventPolicy, "Loading event processing policy...");
1031 if (dwResult == RCC_SUCCESS)
1032 {
1033 pFrame->CreateNewChild(
ef44d5ea 1034 RUNTIME_CLASS(CEventPolicyEditor), IDR_EPP_EDITOR, m_hPolicyEditorMenu, m_hPolicyEditorAccel);
34870fc0
VK
1035 }
1036 else
1037 {
7b053de2 1038 ErrorBox(dwResult, "Unable to load event processing policy:\n%s");
34870fc0
VK
1039 }
1040 }
1041}
ece89362
VK
1042
1043
1044//
1045// WM_COMMAND::ID_VIEW_ALARMS message handler
1046//
1047
1048void CConsoleApp::OnViewAlarms()
1049{
1050 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
1051
1052 // create a new MDI child window or open existing
1053 if (m_bAlarmBrowserActive)
1054 m_pwndAlarmBrowser->BringWindowToTop();
1055 else
1056 pFrame->CreateNewChild(
5cac98d8 1057 RUNTIME_CLASS(CAlarmBrowser), IDR_ALARMS, m_hAlarmBrowserMenu, m_hAlarmBrowserAccel);
ece89362 1058}