d8371e96c08ebae155260eb145145fa66c345529
[public/netxms.git] / src / console / win32 / DeploymentView.cpp
1 // DeploymentView.cpp : implementation file
2 //
3
4 #include "stdafx.h"
5 #include "nxcon.h"
6 #include "DeploymentView.h"
7
8 #ifdef _DEBUG
9 #define new DEBUG_NEW
10 #undef THIS_FILE
11 static char THIS_FILE[] = __FILE__;
12 #endif
13
14
15 //
16 // Constants
17 //
18
19 #define STATUS_HEIGHT 34
20 #define PROGRESS_CTRL_TOP 20
21 #define PROGRESS_CTRL_HEIGHT 9
22
23
24 /////////////////////////////////////////////////////////////////////////////
25 // CDeploymentView
26
27 IMPLEMENT_DYNCREATE(CDeploymentView, CMDIChildWnd)
28
29 CDeploymentView::CDeploymentView()
30 {
31 m_dwRqId = 0;
32 m_bFinished = FALSE;
33 }
34
35 CDeploymentView::~CDeploymentView()
36 {
37 }
38
39
40 BEGIN_MESSAGE_MAP(CDeploymentView, CMDIChildWnd)
41 //{{AFX_MSG_MAP(CDeploymentView)
42 ON_WM_CREATE()
43 ON_WM_PAINT()
44 ON_WM_SIZE()
45 //}}AFX_MSG_MAP
46 ON_MESSAGE(WM_START_DEPLOYMENT, OnStartDeployment)
47 ON_MESSAGE(WM_DEPLOYMENT_INFO, OnDeploymentInfo)
48 ON_MESSAGE(WM_DEPLOYMENT_FINISHED, OnDeploymentFinished)
49 END_MESSAGE_MAP()
50
51 /////////////////////////////////////////////////////////////////////////////
52 // CDeploymentView message handlers
53
54 BOOL CDeploymentView::PreCreateWindow(CREATESTRUCT& cs)
55 {
56 // TODO: Add your specialized code here and/or call the base class
57
58 return CMDIChildWnd::PreCreateWindow(cs);
59 }
60
61
62 //
63 // WM_CREATE message handler
64 //
65
66 int CDeploymentView::OnCreate(LPCREATESTRUCT lpCreateStruct)
67 {
68 RECT rect;
69
70 if (CMDIChildWnd::OnCreate(lpCreateStruct) == -1)
71 return -1;
72
73 // Create font
74 m_font.CreateFont(-MulDiv(8, GetDeviceCaps(GetDC()->m_hDC, LOGPIXELSY), 72),
75 0, 0, 0, FW_BOLD, FALSE, FALSE, FALSE, ANSI_CHARSET,
76 OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, PROOF_QUALITY,
77 VARIABLE_PITCH | FF_DONTCARE, "Verdana");
78
79 // Create progress bar
80 GetClientRect(&rect);
81 rect.left += 4;
82 rect.right -= 4;
83 rect.top = PROGRESS_CTRL_TOP;
84 rect.bottom = rect.top + PROGRESS_CTRL_HEIGHT;
85 m_wndProgressCtrl.Create(WS_CHILD | WS_VISIBLE, rect, this, ID_PROGRESS_CTRL);
86
87 // Create list view inside window
88 GetClientRect(&rect);
89 rect.top += STATUS_HEIGHT + 1;
90 m_wndListCtrl.Create(WS_CHILD | WS_VISIBLE | LVS_REPORT, rect, this, ID_LIST_VIEW);
91 m_wndListCtrl.InsertColumn(0, _T("Node"), LVCFMT_LEFT, 120);
92 m_wndListCtrl.InsertColumn(1, _T("Status"), LVCFMT_LEFT, 100);
93 m_wndListCtrl.InsertColumn(2, _T("Message"), LVCFMT_LEFT, 250);
94 m_wndListCtrl.SetExtendedStyle(LVS_EX_GRIDLINES | LVS_EX_FULLROWSELECT);
95
96 m_imageList.Create(g_pObjectSmallImageList);
97 m_wndListCtrl.SetImageList(&m_imageList, LVSIL_SMALL);
98
99 return 0;
100 }
101
102
103 //
104 // Deployment worker thread
105 //
106
107 static DWORD WINAPI DeploymentThread(DEPLOYMENT_JOB *pJob)
108 {
109 DWORD dwResult;
110
111 dwResult = NXCDeployPackage(g_hSession, pJob->dwPkgId, pJob->dwNumObjects,
112 pJob->pdwObjectList, pJob->pdwRqId);
113 PostMessage(pJob->hWnd, WM_DEPLOYMENT_FINISHED, 0, dwResult);
114 safe_free(pJob->pdwObjectList);
115 free(pJob);
116 return 0;
117 }
118
119
120 //
121 // WM_START_DEPLOYMENT message handler
122 //
123
124 void CDeploymentView::OnStartDeployment(WPARAM wParam, DEPLOYMENT_JOB *pJob)
125 {
126 DWORD dwThreadId;
127 HANDLE hThread;
128
129 m_wndProgressCtrl.SetRange32(0, pJob->dwNumObjects);
130 m_wndProgressCtrl.SetStep(1);
131 m_wndProgressCtrl.SetPos(0);
132
133 pJob->hWnd = m_hWnd;
134 pJob->pdwRqId = &m_dwRqId;
135 hThread = CreateThread(NULL, 0, (LPTHREAD_START_ROUTINE)DeploymentThread,
136 pJob, 0, &dwThreadId);
137 if (hThread != NULL)
138 {
139 CloseHandle(hThread);
140 }
141 }
142
143
144 //
145 // WM_DEPLOYMENT_INFO message handler
146 //
147
148 void CDeploymentView::OnDeploymentInfo(DWORD dwRqId, NXC_DEPLOYMENT_STATUS *pInfo)
149 {
150 if (dwRqId == m_dwRqId)
151 {
152 LVFINDINFO lvfi;
153 int iItem;
154
155 // Find node's record
156 lvfi.flags = LVFI_PARAM;
157 lvfi.lParam = pInfo->dwNodeId;
158 iItem = m_wndListCtrl.FindItem(&lvfi, -1);
159 if (iItem == -1)
160 {
161 NXC_OBJECT *pObject;
162
163 // Create new record
164 pObject = NXCFindObjectById(g_hSession, pInfo->dwNodeId);
165 if (pObject != NULL)
166 {
167 iItem = m_wndListCtrl.InsertItem(0x7FFFFFFF, pObject->szName,
168 GetObjectImageIndex(pObject));
169 }
170 else
171 {
172 iItem = m_wndListCtrl.InsertItem(0x7FFFFFFF, _T("<invalid>"), -1);
173 }
174 m_wndListCtrl.SetItemData(iItem, pInfo->dwNodeId);
175 m_wndProgressCtrl.SetRange32(0, m_wndListCtrl.GetItemCount());
176 }
177 m_wndListCtrl.SetItemText(iItem, 1, g_szDeploymentStatus[pInfo->dwStatus]);
178 m_wndListCtrl.SetItemText(iItem, 2,
179 (pInfo->dwStatus == DEPLOYMENT_STATUS_FAILED) ? pInfo->pszErrorMessage : _T(""));
180
181 if ((pInfo->dwStatus == DEPLOYMENT_STATUS_FAILED) ||
182 (pInfo->dwStatus == DEPLOYMENT_STATUS_COMPLETED))
183 m_wndProgressCtrl.StepIt();
184 }
185 }
186
187
188 //
189 // WM_PAINT message handler
190 //
191
192 void CDeploymentView::OnPaint()
193 {
194 CPaintDC dc(this); // device context for painting
195 RECT rect;
196 CPen pen, *pOldPen;
197 CFont *pOldFont;
198
199 // Draw separator between status box and node list
200 pen.CreatePen(PS_SOLID, 0, RGB(127, 127, 127));
201 pOldPen = dc.SelectObject(&pen);
202
203 GetClientRect(&rect);
204 dc.MoveTo(0, STATUS_HEIGHT);
205 dc.LineTo(rect.right, STATUS_HEIGHT);
206
207 dc.SelectObject(pOldPen);
208
209 // Show current status
210 pOldFont = dc.SelectObject(&m_font);
211 dc.TextOut(4, 4, m_bFinished ? _T("Job finished") : _T("Job running "), 12);
212 dc.SelectObject(pOldFont);
213 }
214
215
216 //
217 // WM_SIZE messahe handler
218 //
219
220 void CDeploymentView::OnSize(UINT nType, int cx, int cy)
221 {
222 CMDIChildWnd::OnSize(nType, cx, cy);
223
224 m_wndProgressCtrl.SetWindowPos(NULL, 4, PROGRESS_CTRL_TOP, cx - 8, PROGRESS_CTRL_HEIGHT, SWP_NOZORDER);
225 m_wndListCtrl.SetWindowPos(NULL, 0, STATUS_HEIGHT + 1, cx, cy - STATUS_HEIGHT, SWP_NOZORDER);
226 }
227
228
229 //
230 // WM_DEPLOYMENT_FINISHED message handler
231 //
232
233 void CDeploymentView::OnDeploymentFinished(WPARAM wParam, LPARAM lParam)
234 {
235 m_bFinished = TRUE;
236 InvalidateRect(NULL);
237 if (lParam == RCC_SUCCESS)
238 MessageBox(_T("Deployment job finished successfully"), _T("Information"), MB_OK | MB_ICONINFORMATION);
239 else
240 theApp.ErrorBox(lParam, _T("Deployment job failed: %s"));
241 }