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