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