New object classes OBJECT_LOCATION and OBJECT_ZONE added (for future use)
[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
17//
18// Wrapper for client library event handler
19//
20
21static void ClientEventHandler(DWORD dwEvent, DWORD dwCode, void *pArg)
22{
23 theApp.EventHandler(dwEvent, dwCode, pArg);
24}
25
26
8e748b3e
VK
27//
28// Wrapper for client library debug callback
29//
30
31static void ClientDebugCallback(char *pszMsg)
32{
33 theApp.DebugCallback(pszMsg);
34}
35
36
3bf7e95e
VK
37//
38// CConsoleApp
39//
40
41BEGIN_MESSAGE_MAP(CConsoleApp, CWinApp)
42 //{{AFX_MSG_MAP(CConsoleApp)
43 ON_COMMAND(ID_APP_ABOUT, OnAppAbout)
44 ON_COMMAND(ID_VIEW_CONTROLPANEL, OnViewControlpanel)
45 ON_COMMAND(ID_VIEW_EVENTS, OnViewEvents)
46 ON_COMMAND(ID_VIEW_MAP, OnViewMap)
47 ON_COMMAND(ID_CONNECT_TO_SERVER, OnConnectToServer)
48 ON_COMMAND(ID_VIEW_OBJECTBROWSER, OnViewObjectbrowser)
a5f8f9b0 49 ON_COMMAND(ID_CONTROLPANEL_EVENTS, OnControlpanelEvents)
8e748b3e 50 ON_COMMAND(ID_VIEW_DEBUG, OnViewDebug)
3bf7e95e
VK
51 //}}AFX_MSG_MAP
52END_MESSAGE_MAP()
53
54
55//
56// CConsoleApp construction
57//
58
59CConsoleApp::CConsoleApp()
60{
61 m_bCtrlPanelActive = FALSE;
62 m_bEventBrowserActive = FALSE;
a5f8f9b0 63 m_bEventEditorActive = FALSE;
3bf7e95e 64 m_bObjectBrowserActive = FALSE;
8e748b3e 65 m_bDebugWindowActive = FALSE;
3bf7e95e 66 m_dwClientState = STATE_DISCONNECTED;
a5f8f9b0 67 m_pRqWaitList = NULL;
999bd324
VK
68 m_dwRqWaitListSize = 0;
69 m_mutexRqWaitList = MutexCreate();
3bf7e95e
VK
70}
71
999bd324
VK
72
73//
74// CConsoleApp destruction
75//
76
77CConsoleApp::~CConsoleApp()
78{
79 if (m_pRqWaitList != NULL)
80 MemFree(m_pRqWaitList);
81 MutexDestroy(m_mutexRqWaitList);
82}
83
84
3bf7e95e
VK
85//
86// The one and only CConsoleApp object
87//
88
89CConsoleApp theApp;
90
91
92//
93// CConsoleApp initialization
94//
95
96BOOL CConsoleApp::InitInstance()
97{
98 BOOL bSetWindowPos;
99 DWORD dwBytes;
100 BYTE *pData;
8e748b3e 101 HMENU hMenu;
3bf7e95e
VK
102
103 if (!AfxSocketInit())
104 {
105 AfxMessageBox(IDP_SOCKETS_INIT_FAILED, MB_OK | MB_ICONSTOP);
106 return FALSE;
107 }
108
109 if (!NXCInitialize())
110 {
111 AfxMessageBox(IDS_NXC_INIT_FAILED, MB_OK | MB_ICONSTOP);
112 return FALSE;
113 }
114 NXCSetEventHandler(ClientEventHandler);
115
116 AfxEnableControlContainer();
117
118 // Standard initialization
119 // If you are not using these features and wish to reduce the size
120 // of your final executable, you should remove from the following
121 // the specific initialization routines you do not need.
122
123#ifdef _AFXDLL
124 Enable3dControls(); // Call this when using MFC in a shared DLL
125#else
126 Enable3dControlsStatic(); // Call this when linking to MFC statically
127#endif
128
129 // Change the registry key under which our settings are stored.
130 SetRegistryKey(_T("NetXMS"));
131
132 // To create the main window, this code creates a new frame window
133 // object and then sets it as the application's main window object.
134
135 CMDIFrameWnd* pFrame = new CMainFrame;
136 m_pMainWnd = pFrame;
137
138 // create main MDI frame window
139 if (!pFrame->LoadFrame(IDR_MAINFRAME))
140 return FALSE;
141
8e748b3e
VK
142 // Load shared MDI menus and accelerator table
143 HINSTANCE hInstance = AfxGetResourceHandle();
144
145 hMenu = ::LoadMenu(hInstance, MAKEINTRESOURCE(IDM_VIEW_SPECIFIC));
146
147 m_hMDIMenu = ::LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MAINFRAME));
148 InsertMenu(m_hMDIMenu, 3, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
149
150 m_hEventBrowserMenu = ::LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MAINFRAME));
151 InsertMenu(m_hEventBrowserMenu, 3, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
152 InsertMenu(m_hEventBrowserMenu, 3, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 1), "&Event");
153
2060ba91
VK
154 m_hObjectBrowserMenu = ::LoadMenu(hInstance, MAKEINTRESOURCE(IDR_MAINFRAME));
155 InsertMenu(m_hObjectBrowserMenu, 3, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 0), "&Window");
156 InsertMenu(m_hObjectBrowserMenu, 3, MF_BYPOSITION | MF_POPUP, (UINT_PTR)GetSubMenu(hMenu, 2), "&Object");
157
8e748b3e
VK
158 m_hMDIAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_MDI_DEFAULT));
159 m_hEventBrowserAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_MDI_DEFAULT));
2060ba91 160 m_hObjectBrowserAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_OBJECT_BROWSER));
3bf7e95e
VK
161
162 // Load configuration from registry
163 dwBytes = sizeof(WINDOWPLACEMENT);
164 bSetWindowPos = GetProfileBinary(_T("General"), _T("WindowPlacement"),
165 &pData, (UINT *)&dwBytes);
166 strcpy(g_szServer, (LPCTSTR)GetProfileString(_T("Connection"), _T("Server"), _T("localhost")));
167 strcpy(g_szLogin, (LPCTSTR)GetProfileString(_T("Connection"), _T("Login"), NULL));
168
169 // The main window has been initialized, so show and update it.
170 if (bSetWindowPos)
171 {
172 pFrame->SetWindowPlacement((WINDOWPLACEMENT *)pData);
173 if (pFrame->IsIconic())
174 pFrame->ShowWindow(SW_RESTORE);
175 }
176 else
177 {
178 pFrame->ShowWindow(m_nCmdShow);
179 }
180 pFrame->UpdateWindow();
181 pFrame->PostMessage(WM_COMMAND, ID_CONNECT_TO_SERVER, 0);
182
183 return TRUE;
184}
185
186/////////////////////////////////////////////////////////////////////////////
187// CConsoleApp message handlers
188
189int CConsoleApp::ExitInstance()
190{
8e748b3e
VK
191 NXCSetDebugCallback(NULL);
192 NXCDisconnect();
193
3bf7e95e 194 // Free resources
8e748b3e
VK
195 SafeFreeResource(m_hMDIMenu);
196 SafeFreeResource(m_hMDIAccel);
197 SafeFreeResource(m_hEventBrowserMenu);
198 SafeFreeResource(m_hEventBrowserAccel);
3bf7e95e
VK
199
200 return CWinApp::ExitInstance();
201}
202
203
204/////////////////////////////////////////////////////////////////////////////
205// CAboutDlg dialog used for App About
206
207class CAboutDlg : public CDialog
208{
209public:
210 CAboutDlg();
211
212// Dialog Data
213 //{{AFX_DATA(CAboutDlg)
214 enum { IDD = IDD_ABOUTBOX };
215 //}}AFX_DATA
216
217 // ClassWizard generated virtual function overrides
218 //{{AFX_VIRTUAL(CAboutDlg)
219 protected:
220 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
221 //}}AFX_VIRTUAL
222
223// Implementation
224protected:
225 //{{AFX_MSG(CAboutDlg)
226 // No message handlers
227 //}}AFX_MSG
228 DECLARE_MESSAGE_MAP()
229};
230
231CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
232{
233 //{{AFX_DATA_INIT(CAboutDlg)
234 //}}AFX_DATA_INIT
235}
236
237void CAboutDlg::DoDataExchange(CDataExchange* pDX)
238{
239 CDialog::DoDataExchange(pDX);
240 //{{AFX_DATA_MAP(CAboutDlg)
241 //}}AFX_DATA_MAP
242}
243
244BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
245 //{{AFX_MSG_MAP(CAboutDlg)
246 // No message handlers
247 //}}AFX_MSG_MAP
248END_MESSAGE_MAP()
249
250// App command to run the dialog
251void CConsoleApp::OnAppAbout()
252{
253 CAboutDlg aboutDlg;
254 aboutDlg.DoModal();
255}
256
257/////////////////////////////////////////////////////////////////////////////
258// CConsoleApp message handlers
259
260
261void CConsoleApp::OnViewControlpanel()
262{
263 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
264
265 // create a new MDI child window or open existing
266 if (m_bCtrlPanelActive)
267 m_pwndCtrlPanel->BringWindowToTop();
268 else
269 pFrame->CreateNewChild(
8e748b3e 270 RUNTIME_CLASS(CControlPanel), IDR_CTRLPANEL, m_hMDIMenu, m_hMDIAccel);
3bf7e95e
VK
271}
272
273
274//
275// This method called when new view is created
276//
277
278void CConsoleApp::OnViewCreate(DWORD dwView, CWnd *pWnd)
279{
280 switch(dwView)
281 {
282 case IDR_CTRLPANEL:
283 m_bCtrlPanelActive = TRUE;
284 m_pwndCtrlPanel = pWnd;
285 break;
286 case IDR_EVENTS:
287 m_bEventBrowserActive = TRUE;
288 m_pwndEventBrowser = (CEventBrowser *)pWnd;
289 break;
290 case IDR_OBJECTS:
291 m_bObjectBrowserActive = TRUE;
292 m_pwndObjectBrowser = pWnd;
293 break;
a5f8f9b0
VK
294 case IDR_EVENT_EDITOR:
295 m_bEventEditorActive = TRUE;
296 m_pwndEventEditor = (CEventEditor *)pWnd;
297 break;
8e748b3e
VK
298 case IDR_DEBUG_WINDOW:
299 m_bDebugWindowActive = TRUE;
300 m_pwndDebugWindow = (CDebugFrame *)pWnd;
301 NXCSetDebugCallback(ClientDebugCallback);
302 break;
3bf7e95e
VK
303 default:
304 break;
305 }
306}
307
308
309//
310// This method called when view is destroyed
311//
312
313void CConsoleApp::OnViewDestroy(DWORD dwView, CWnd *pWnd)
314{
315 switch(dwView)
316 {
317 case IDR_CTRLPANEL:
318 m_bCtrlPanelActive = FALSE;
319 break;
320 case IDR_EVENTS:
321 m_bEventBrowserActive = FALSE;
322 break;
323 case IDR_OBJECTS:
324 m_bObjectBrowserActive = FALSE;
325 break;
a5f8f9b0
VK
326 case IDR_EVENT_EDITOR:
327 m_bEventEditorActive = FALSE;
328 break;
8e748b3e
VK
329 case IDR_DEBUG_WINDOW:
330 m_bDebugWindowActive = FALSE;
331 NXCSetDebugCallback(NULL);
332 break;
3bf7e95e
VK
333 default:
334 break;
335 }
336}
337
338void CConsoleApp::OnViewEvents()
339{
340 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
341
342 // create a new MDI child window or open existing
343 if (m_bEventBrowserActive)
344 m_pwndEventBrowser->BringWindowToTop();
345 else
346 pFrame->CreateNewChild(
8e748b3e 347 RUNTIME_CLASS(CEventBrowser), IDR_EVENTS, m_hEventBrowserMenu, m_hEventBrowserAccel);
3bf7e95e
VK
348}
349
350void CConsoleApp::OnViewMap()
351{
352 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
353
354 // create a new MDI child window
355 pFrame->CreateNewChild(RUNTIME_CLASS(CMapFrame), IDR_MAPFRAME, m_hMDIMenu, m_hMDIAccel);
356}
357
358void CConsoleApp::OnConnectToServer()
359{
360 CLoginDialog dlgLogin;
361
362 dlgLogin.m_szServer = g_szServer;
363 dlgLogin.m_szLogin = g_szLogin;
364 while(1)
365 {
366 if (dlgLogin.DoModal() != IDOK)
367 {
368 PostQuitMessage(1);
369 break;
370 }
371 strcpy(g_szServer, (LPCTSTR)dlgLogin.m_szServer);
372 strcpy(g_szLogin, (LPCTSTR)dlgLogin.m_szLogin);
373 strcpy(g_szPassword, (LPCTSTR)dlgLogin.m_szPassword);
374
375 // Save last connection parameters
376 WriteProfileString(_T("Connection"), _T("Server"), g_szServer);
377 WriteProfileString(_T("Connection"), _T("Login"), g_szLogin);
378
379 // Initiate connection
380 m_bAuthFailed = FALSE;
381 if (NXCConnect(g_szServer, g_szLogin, g_szPassword))
382 {
383 m_dlgProgress.m_szStatusText = "Connecting to server...";
384 if (m_dlgProgress.DoModal() == IDOK)
385 {
386 // Now we are connected, request data sync
387 NXCSyncObjects();
388 break;
389 }
390 else
391 m_pMainWnd->MessageBox(m_bAuthFailed ? "Client authentication failed (invalid login name or password)" :
392 "Unable to establish connection with the server",
393 "Connection error", MB_OK | MB_ICONSTOP);
394 }
395 else
396 {
397 m_pMainWnd->MessageBox("Unable to initiate connection to server",
398 "Connection error", MB_OK | MB_ICONSTOP);
399 }
400 }
401}
402
403void CConsoleApp::OnViewObjectbrowser()
404{
405 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
406
407 // create a new MDI child window or open existing
408 if (m_bObjectBrowserActive)
409 m_pwndObjectBrowser->BringWindowToTop();
410 else
411 pFrame->CreateNewChild(
2060ba91 412 RUNTIME_CLASS(CObjectBrowser), IDR_OBJECTS, m_hObjectBrowserMenu, m_hObjectBrowserAccel);
3bf7e95e
VK
413}
414
415
416//
417// Event handler for client libray events
418//
419
420void CConsoleApp::EventHandler(DWORD dwEvent, DWORD dwCode, void *pArg)
421{
422 static BOOL bNowConnecting = FALSE;
423 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
424
425 switch(dwEvent)
426 {
427 case NXC_EVENT_STATE_CHANGED:
428 switch(dwCode)
429 {
430 case STATE_CONNECTING:
431 bNowConnecting = TRUE;
432 break;
433 case STATE_IDLE:
434 if (bNowConnecting)
435 {
6aaf0c07 436 m_dlgProgress.Terminate(IDOK);
3bf7e95e
VK
437 bNowConnecting = FALSE;
438 }
439 if ((m_bEventBrowserActive) && (m_dwClientState == STATE_SYNC_EVENTS))
440 m_pwndEventBrowser->EnableDisplay(TRUE);
441 break;
442 case STATE_DISCONNECTED:
443 if (bNowConnecting)
444 {
6aaf0c07 445 m_dlgProgress.Terminate(IDCANCEL);
3bf7e95e
VK
446 bNowConnecting = FALSE;
447 }
448 break;
449 case STATE_SYNC_EVENTS:
450 if (m_bEventBrowserActive)
451 m_pwndEventBrowser->EnableDisplay(FALSE);
452 break;
453 default:
454 break;
455 }
456 m_dwClientState = dwCode;
457 break;
458 case NXC_EVENT_LOGIN_RESULT:
459 if (dwCode == 0)
460 m_bAuthFailed = TRUE;
461 break;
462 case NXC_EVENT_NEW_ELOG_RECORD:
463 if (m_bEventBrowserActive)
464 m_pwndEventBrowser->AddEvent((NXC_EVENT *)pArg);
465 MemFree(pArg);
466// Sleep(0);
467 break;
a5f8f9b0 468 case NXC_EVENT_REQUEST_COMPLETED:
999bd324 469 OnRequestComplete(dwCode, (DWORD)pArg);
a5f8f9b0 470 break;
3bf7e95e
VK
471 default:
472 break;
473 }
474}
a5f8f9b0 475
999bd324
VK
476
477//
478// Handler for WM_COMMAND(ID_CONTROLPANEL_EVENTS) message
479//
480
a5f8f9b0
VK
481void CConsoleApp::OnControlpanelEvents()
482{
483 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
484
485 // create a new MDI child window or open existing
486 if (m_bEventEditorActive)
487 m_pwndEventEditor->BringWindowToTop();
488 else
489 pFrame->CreateNewChild(
8e748b3e 490 RUNTIME_CLASS(CEventEditor), IDR_OBJECTS, m_hMDIMenu, m_hMDIAccel);
a5f8f9b0
VK
491}
492
493
494//
495// Register callback for request completion
496//
497
498void CConsoleApp::RegisterRequest(HREQUEST hRequest, CWnd *pWnd)
499{
999bd324
VK
500 m_pRqWaitList = (RQ_WAIT_INFO *)MemReAlloc(m_pRqWaitList, sizeof(RQ_WAIT_INFO) * (m_dwRqWaitListSize + 1));
501 m_pRqWaitList[m_dwRqWaitListSize].hRequest = hRequest;
502 m_pRqWaitList[m_dwRqWaitListSize].pWnd = pWnd;
503 m_dwRqWaitListSize++;
504}
505
506
507//
508// This method called by client library event handler on NXC_EVENT_REQUEST_COMPLETE
509//
510
511void CConsoleApp::OnRequestComplete(HREQUEST hRequest, DWORD dwRetCode)
512{
513 DWORD i;
a5f8f9b0 514
999bd324
VK
515 for(i = 0; i < m_dwRqWaitListSize; i++)
516 if (m_pRqWaitList[i].hRequest == hRequest)
517 {
518 m_pRqWaitList[i].pWnd->SendMessage(WM_REQUEST_COMPLETED, hRequest, dwRetCode);
519 m_dwRqWaitListSize--;
520 memmove(&m_pRqWaitList[i], &m_pRqWaitList[i + 1], sizeof(RQ_WAIT_INFO) * (m_dwRqWaitListSize - i));
521 i--;
522 }
a5f8f9b0 523}
8e748b3e
VK
524
525
526//
527// Handler for WM_COMMAND::ID_VIEW_DEBUG message
528//
529
530void CConsoleApp::OnViewDebug()
531{
532 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
533
534 // create a new MDI child window or open existing
535 if (m_bDebugWindowActive)
536 m_pwndDebugWindow->BringWindowToTop();
537 else
538 pFrame->CreateNewChild(
539 RUNTIME_CLASS(CDebugFrame), IDR_DEBUG_WINDOW, m_hMDIMenu, m_hMDIAccel);
540}