Minor changes
[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
154 m_hMDIAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_MDI_DEFAULT));
155 m_hEventBrowserAccel = ::LoadAccelerators(hInstance, MAKEINTRESOURCE(IDA_MDI_DEFAULT));
3bf7e95e
VK
156
157 // Load configuration from registry
158 dwBytes = sizeof(WINDOWPLACEMENT);
159 bSetWindowPos = GetProfileBinary(_T("General"), _T("WindowPlacement"),
160 &pData, (UINT *)&dwBytes);
161 strcpy(g_szServer, (LPCTSTR)GetProfileString(_T("Connection"), _T("Server"), _T("localhost")));
162 strcpy(g_szLogin, (LPCTSTR)GetProfileString(_T("Connection"), _T("Login"), NULL));
163
164 // The main window has been initialized, so show and update it.
165 if (bSetWindowPos)
166 {
167 pFrame->SetWindowPlacement((WINDOWPLACEMENT *)pData);
168 if (pFrame->IsIconic())
169 pFrame->ShowWindow(SW_RESTORE);
170 }
171 else
172 {
173 pFrame->ShowWindow(m_nCmdShow);
174 }
175 pFrame->UpdateWindow();
176 pFrame->PostMessage(WM_COMMAND, ID_CONNECT_TO_SERVER, 0);
177
178 return TRUE;
179}
180
181/////////////////////////////////////////////////////////////////////////////
182// CConsoleApp message handlers
183
184int CConsoleApp::ExitInstance()
185{
8e748b3e
VK
186 NXCSetDebugCallback(NULL);
187 NXCDisconnect();
188
3bf7e95e 189 // Free resources
8e748b3e
VK
190 SafeFreeResource(m_hMDIMenu);
191 SafeFreeResource(m_hMDIAccel);
192 SafeFreeResource(m_hEventBrowserMenu);
193 SafeFreeResource(m_hEventBrowserAccel);
3bf7e95e
VK
194
195 return CWinApp::ExitInstance();
196}
197
198
199/////////////////////////////////////////////////////////////////////////////
200// CAboutDlg dialog used for App About
201
202class CAboutDlg : public CDialog
203{
204public:
205 CAboutDlg();
206
207// Dialog Data
208 //{{AFX_DATA(CAboutDlg)
209 enum { IDD = IDD_ABOUTBOX };
210 //}}AFX_DATA
211
212 // ClassWizard generated virtual function overrides
213 //{{AFX_VIRTUAL(CAboutDlg)
214 protected:
215 virtual void DoDataExchange(CDataExchange* pDX); // DDX/DDV support
216 //}}AFX_VIRTUAL
217
218// Implementation
219protected:
220 //{{AFX_MSG(CAboutDlg)
221 // No message handlers
222 //}}AFX_MSG
223 DECLARE_MESSAGE_MAP()
224};
225
226CAboutDlg::CAboutDlg() : CDialog(CAboutDlg::IDD)
227{
228 //{{AFX_DATA_INIT(CAboutDlg)
229 //}}AFX_DATA_INIT
230}
231
232void CAboutDlg::DoDataExchange(CDataExchange* pDX)
233{
234 CDialog::DoDataExchange(pDX);
235 //{{AFX_DATA_MAP(CAboutDlg)
236 //}}AFX_DATA_MAP
237}
238
239BEGIN_MESSAGE_MAP(CAboutDlg, CDialog)
240 //{{AFX_MSG_MAP(CAboutDlg)
241 // No message handlers
242 //}}AFX_MSG_MAP
243END_MESSAGE_MAP()
244
245// App command to run the dialog
246void CConsoleApp::OnAppAbout()
247{
248 CAboutDlg aboutDlg;
249 aboutDlg.DoModal();
250}
251
252/////////////////////////////////////////////////////////////////////////////
253// CConsoleApp message handlers
254
255
256void CConsoleApp::OnViewControlpanel()
257{
258 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
259
260 // create a new MDI child window or open existing
261 if (m_bCtrlPanelActive)
262 m_pwndCtrlPanel->BringWindowToTop();
263 else
264 pFrame->CreateNewChild(
8e748b3e 265 RUNTIME_CLASS(CControlPanel), IDR_CTRLPANEL, m_hMDIMenu, m_hMDIAccel);
3bf7e95e
VK
266}
267
268
269//
270// This method called when new view is created
271//
272
273void CConsoleApp::OnViewCreate(DWORD dwView, CWnd *pWnd)
274{
275 switch(dwView)
276 {
277 case IDR_CTRLPANEL:
278 m_bCtrlPanelActive = TRUE;
279 m_pwndCtrlPanel = pWnd;
280 break;
281 case IDR_EVENTS:
282 m_bEventBrowserActive = TRUE;
283 m_pwndEventBrowser = (CEventBrowser *)pWnd;
284 break;
285 case IDR_OBJECTS:
286 m_bObjectBrowserActive = TRUE;
287 m_pwndObjectBrowser = pWnd;
288 break;
a5f8f9b0
VK
289 case IDR_EVENT_EDITOR:
290 m_bEventEditorActive = TRUE;
291 m_pwndEventEditor = (CEventEditor *)pWnd;
292 break;
8e748b3e
VK
293 case IDR_DEBUG_WINDOW:
294 m_bDebugWindowActive = TRUE;
295 m_pwndDebugWindow = (CDebugFrame *)pWnd;
296 NXCSetDebugCallback(ClientDebugCallback);
297 break;
3bf7e95e
VK
298 default:
299 break;
300 }
301}
302
303
304//
305// This method called when view is destroyed
306//
307
308void CConsoleApp::OnViewDestroy(DWORD dwView, CWnd *pWnd)
309{
310 switch(dwView)
311 {
312 case IDR_CTRLPANEL:
313 m_bCtrlPanelActive = FALSE;
314 break;
315 case IDR_EVENTS:
316 m_bEventBrowserActive = FALSE;
317 break;
318 case IDR_OBJECTS:
319 m_bObjectBrowserActive = FALSE;
320 break;
a5f8f9b0
VK
321 case IDR_EVENT_EDITOR:
322 m_bEventEditorActive = FALSE;
323 break;
8e748b3e
VK
324 case IDR_DEBUG_WINDOW:
325 m_bDebugWindowActive = FALSE;
326 NXCSetDebugCallback(NULL);
327 break;
3bf7e95e
VK
328 default:
329 break;
330 }
331}
332
333void CConsoleApp::OnViewEvents()
334{
335 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
336
337 // create a new MDI child window or open existing
338 if (m_bEventBrowserActive)
339 m_pwndEventBrowser->BringWindowToTop();
340 else
341 pFrame->CreateNewChild(
8e748b3e 342 RUNTIME_CLASS(CEventBrowser), IDR_EVENTS, m_hEventBrowserMenu, m_hEventBrowserAccel);
3bf7e95e
VK
343}
344
345void CConsoleApp::OnViewMap()
346{
347 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
348
349 // create a new MDI child window
350 pFrame->CreateNewChild(RUNTIME_CLASS(CMapFrame), IDR_MAPFRAME, m_hMDIMenu, m_hMDIAccel);
351}
352
353void CConsoleApp::OnConnectToServer()
354{
355 CLoginDialog dlgLogin;
356
357 dlgLogin.m_szServer = g_szServer;
358 dlgLogin.m_szLogin = g_szLogin;
359 while(1)
360 {
361 if (dlgLogin.DoModal() != IDOK)
362 {
363 PostQuitMessage(1);
364 break;
365 }
366 strcpy(g_szServer, (LPCTSTR)dlgLogin.m_szServer);
367 strcpy(g_szLogin, (LPCTSTR)dlgLogin.m_szLogin);
368 strcpy(g_szPassword, (LPCTSTR)dlgLogin.m_szPassword);
369
370 // Save last connection parameters
371 WriteProfileString(_T("Connection"), _T("Server"), g_szServer);
372 WriteProfileString(_T("Connection"), _T("Login"), g_szLogin);
373
374 // Initiate connection
375 m_bAuthFailed = FALSE;
376 if (NXCConnect(g_szServer, g_szLogin, g_szPassword))
377 {
378 m_dlgProgress.m_szStatusText = "Connecting to server...";
379 if (m_dlgProgress.DoModal() == IDOK)
380 {
381 // Now we are connected, request data sync
382 NXCSyncObjects();
383 break;
384 }
385 else
386 m_pMainWnd->MessageBox(m_bAuthFailed ? "Client authentication failed (invalid login name or password)" :
387 "Unable to establish connection with the server",
388 "Connection error", MB_OK | MB_ICONSTOP);
389 }
390 else
391 {
392 m_pMainWnd->MessageBox("Unable to initiate connection to server",
393 "Connection error", MB_OK | MB_ICONSTOP);
394 }
395 }
396}
397
398void CConsoleApp::OnViewObjectbrowser()
399{
400 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
401
402 // create a new MDI child window or open existing
403 if (m_bObjectBrowserActive)
404 m_pwndObjectBrowser->BringWindowToTop();
405 else
406 pFrame->CreateNewChild(
8e748b3e 407 RUNTIME_CLASS(CObjectBrowser), IDR_OBJECTS, m_hMDIMenu, m_hMDIAccel);
3bf7e95e
VK
408}
409
410
411//
412// Event handler for client libray events
413//
414
415void CConsoleApp::EventHandler(DWORD dwEvent, DWORD dwCode, void *pArg)
416{
417 static BOOL bNowConnecting = FALSE;
418 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
419
420 switch(dwEvent)
421 {
422 case NXC_EVENT_STATE_CHANGED:
423 switch(dwCode)
424 {
425 case STATE_CONNECTING:
426 bNowConnecting = TRUE;
427 break;
428 case STATE_IDLE:
429 if (bNowConnecting)
430 {
431 m_dlgProgress.EndDialog(IDOK);
432 bNowConnecting = FALSE;
433 }
434 if ((m_bEventBrowserActive) && (m_dwClientState == STATE_SYNC_EVENTS))
435 m_pwndEventBrowser->EnableDisplay(TRUE);
436 break;
437 case STATE_DISCONNECTED:
438 if (bNowConnecting)
439 {
440 m_dlgProgress.EndDialog(IDCANCEL);
441 bNowConnecting = FALSE;
442 }
443 break;
444 case STATE_SYNC_EVENTS:
445 if (m_bEventBrowserActive)
446 m_pwndEventBrowser->EnableDisplay(FALSE);
447 break;
448 default:
449 break;
450 }
451 m_dwClientState = dwCode;
452 break;
453 case NXC_EVENT_LOGIN_RESULT:
454 if (dwCode == 0)
455 m_bAuthFailed = TRUE;
456 break;
457 case NXC_EVENT_NEW_ELOG_RECORD:
458 if (m_bEventBrowserActive)
459 m_pwndEventBrowser->AddEvent((NXC_EVENT *)pArg);
460 MemFree(pArg);
461// Sleep(0);
462 break;
a5f8f9b0 463 case NXC_EVENT_REQUEST_COMPLETED:
999bd324 464 OnRequestComplete(dwCode, (DWORD)pArg);
a5f8f9b0 465 break;
3bf7e95e
VK
466 default:
467 break;
468 }
469}
a5f8f9b0 470
999bd324
VK
471
472//
473// Handler for WM_COMMAND(ID_CONTROLPANEL_EVENTS) message
474//
475
a5f8f9b0
VK
476void CConsoleApp::OnControlpanelEvents()
477{
478 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
479
480 // create a new MDI child window or open existing
481 if (m_bEventEditorActive)
482 m_pwndEventEditor->BringWindowToTop();
483 else
484 pFrame->CreateNewChild(
8e748b3e 485 RUNTIME_CLASS(CEventEditor), IDR_OBJECTS, m_hMDIMenu, m_hMDIAccel);
a5f8f9b0
VK
486}
487
488
489//
490// Register callback for request completion
491//
492
493void CConsoleApp::RegisterRequest(HREQUEST hRequest, CWnd *pWnd)
494{
999bd324
VK
495 m_pRqWaitList = (RQ_WAIT_INFO *)MemReAlloc(m_pRqWaitList, sizeof(RQ_WAIT_INFO) * (m_dwRqWaitListSize + 1));
496 m_pRqWaitList[m_dwRqWaitListSize].hRequest = hRequest;
497 m_pRqWaitList[m_dwRqWaitListSize].pWnd = pWnd;
498 m_dwRqWaitListSize++;
499}
500
501
502//
503// This method called by client library event handler on NXC_EVENT_REQUEST_COMPLETE
504//
505
506void CConsoleApp::OnRequestComplete(HREQUEST hRequest, DWORD dwRetCode)
507{
508 DWORD i;
a5f8f9b0 509
999bd324
VK
510 for(i = 0; i < m_dwRqWaitListSize; i++)
511 if (m_pRqWaitList[i].hRequest == hRequest)
512 {
513 m_pRqWaitList[i].pWnd->SendMessage(WM_REQUEST_COMPLETED, hRequest, dwRetCode);
514 m_dwRqWaitListSize--;
515 memmove(&m_pRqWaitList[i], &m_pRqWaitList[i + 1], sizeof(RQ_WAIT_INFO) * (m_dwRqWaitListSize - i));
516 i--;
517 }
a5f8f9b0 518}
8e748b3e
VK
519
520
521//
522// Handler for WM_COMMAND::ID_VIEW_DEBUG message
523//
524
525void CConsoleApp::OnViewDebug()
526{
527 CMainFrame* pFrame = STATIC_DOWNCAST(CMainFrame, m_pMainWnd);
528
529 // create a new MDI child window or open existing
530 if (m_bDebugWindowActive)
531 m_pwndDebugWindow->BringWindowToTop();
532 else
533 pFrame->CreateNewChild(
534 RUNTIME_CLASS(CDebugFrame), IDR_DEBUG_WINDOW, m_hMDIMenu, m_hMDIAccel);
535}