-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathAppDlg.cpp
429 lines (316 loc) · 12 KB
/
AppDlg.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
////////////////////////////////////////////////////////////////////////////////
//! \file AppDlg.cpp
//! \brief The AppDlg class definition.
//! \author Chris Oldwood
#include "Common.hpp"
#include "AppDlg.hpp"
#include "Model.hpp"
#include "AppWnd.hpp"
#include <WMI/Connection.hpp>
#include <WMI/Exception.hpp>
#include <WMI/Win32_OperatingSystem.hpp>
#include <WMI/Win32_LogicalDisk.hpp>
#include <WCL/BusyCursor.hpp>
#include <WCL/ContextMenu.hpp>
#include <WCL/ICmdController.hpp>
#include <WCL/BusyCursor.hpp>
#include "ExecuteToolCmd.hpp"
#include "QueryRunner.hpp"
#include "LogonDialog.hpp"
#include <Core/Algorithm.hpp>
////////////////////////////////////////////////////////////////////////////////
//! Constants.
const tstring LastErrorName = TXT("Last Error");
////////////////////////////////////////////////////////////////////////////////
//! Constructor.
AppDlg::AppDlg(AppWnd& appWnd, WCL::ICmdController& appCmds, Model& model)
: CMainDlg(IDD_MAIN)
, m_appWnd(appWnd)
, m_appCmds(appCmds)
, m_model(model)
, m_finalWidths()
, m_logons()
{
DEFINE_CTRL_TABLE
CTRL(IDC_HOSTS, &m_hostView)
END_CTRL_TABLE
DEFINE_GRAVITY_TABLE
CTRLGRAV(IDC_HOSTS, LEFT_EDGE, TOP_EDGE, RIGHT_EDGE, BOTTOM_EDGE)
END_GRAVITY_TABLE
DEFINE_CTRLMSG_TABLE
NFY_CTRLMSG(IDC_HOSTS, LVN_ITEMCHANGED, &AppDlg::onHostSelectionChanged)
NFY_CTRLMSG(IDC_HOSTS, NM_RCLICK, &AppDlg::onRightClick)
END_CTRLMSG_TABLE
}
////////////////////////////////////////////////////////////////////////////////
//! Destructor.
AppDlg::~AppDlg()
{
}
////////////////////////////////////////////////////////////////////////////////
//! Is a host currently selected?
bool AppDlg::isHostSelected() const
{
return m_hostView.IsSelection();
}
////////////////////////////////////////////////////////////////////////////////
//! Get the index of the currently selected host, if available. If no host is
//! selected it returns npos;
size_t AppDlg::getSelectedHostIndex() const
{
return m_hostView.Selection();
}
////////////////////////////////////////////////////////////////////////////////
//! Get the currently selected host, if available. If no host is selected this
//! returns a null host ptr.
ConstHostPtr AppDlg::getSelectedHost() const
{
ConstHostPtr host;
size_t selection = m_hostView.Selection();
if (selection != Core::npos)
host = m_model.m_hosts.host(selection);
return host;
}
////////////////////////////////////////////////////////////////////////////////
//! Get the final widths of the main view columns.
const AppDlg::ColumnWidths& AppDlg::getFinalColumnWidths() const
{
ASSERT(m_hWnd == NULL);
return m_finalWidths;
}
////////////////////////////////////////////////////////////////////////////////
//! Set the widths of the main view columns.
void AppDlg::setColumnWidths(const ColumnWidths& widths)
{
const size_t columns = std::min(widths.size(), m_hostView.NumColumns());
for (size_t i = 0; i != columns; ++i)
m_hostView.ColumnWidth(i, widths[i]);
}
////////////////////////////////////////////////////////////////////////////////
//! Add a new host to be monitored.
void AppDlg::addHost(ConstHostPtr host)
{
const size_t index = m_model.m_hosts.add(host);
addHostToView(index);
if (!m_hostView.IsSelection())
m_hostView.Select(0);
}
////////////////////////////////////////////////////////////////////////////////
//! Replace the currently selected host.
void AppDlg::replaceHost(ConstHostPtr host)
{
ASSERT(m_hostView.IsSelection());
const size_t selection = m_hostView.Selection();
const bool renamed = (m_model.m_hosts.host(selection)->m_name != host->m_name);
m_model.m_hosts.replace(selection, host);
m_hostView.ItemText(selection, HOST_NAME, host->m_name);
m_hostView.ItemText(selection, ENVIRONMENT, host->m_environment);
m_hostView.ItemText(selection, DESCRIPTION, host->m_description);
if ( (renamed) || (!host->m_monitor) )
clearHost(selection);
}
////////////////////////////////////////////////////////////////////////////////
//! Swap two hosts in the view by index.
void AppDlg::swapHosts(size_t first, size_t second)
{
for (size_t column = 0; column != m_hostView.NumColumns(); ++column)
{
tstring temp = m_hostView.ItemText(first, column);
m_hostView.ItemText(first, column, m_hostView.ItemText(second, column));
m_hostView.ItemText(second, column, temp);
}
if (m_hostView.Selection() == first)
m_hostView.Select(second);
else if (m_hostView.Selection() == second)
m_hostView.Select(first);
}
////////////////////////////////////////////////////////////////////////////////
//! Remove the currently selected host.
void AppDlg::removeSelectedHost()
{
ASSERT(m_hostView.IsSelection());
const size_t selection = m_hostView.Selection();
m_model.m_hosts.remove(selection);
m_hostView.DeleteItem(selection);
if (m_hostView.ItemCount() != 0)
{
const size_t newSelection = (selection == 0) ? 0 : selection-1;
m_hostView.Select(newSelection);
}
}
////////////////////////////////////////////////////////////////////////////////
//! Handle dialog creation.
void AppDlg::OnInitDialog()
{
initialiseHostView();
}
////////////////////////////////////////////////////////////////////////////////
//! Handle the dialog being destroyed.
void AppDlg::OnDestroy()
{
ASSERT(m_finalWidths.size() == 0);
for (size_t i = 0; i != m_hostView.NumColumns(); ++i)
m_finalWidths.push_back(m_hostView.ColumnWidth(i));
CMainDlg::OnDestroy();
}
////////////////////////////////////////////////////////////////////////////////
//! Handle change in host selection.
LRESULT AppDlg::onHostSelectionChanged(NMHDR& header)
{
const NMLISTVIEW& message = reinterpret_cast<const NMLISTVIEW&>(header);
if (message.uChanged & LVIF_STATE)
m_appCmds.UpdateUI();
return 0;
}
////////////////////////////////////////////////////////////////////////////////
//! Handle a right-click on the hosts view.
LRESULT AppDlg::onRightClick(NMHDR& /*header*/)
{
const bool isSelection = m_hostView.IsSelection();
WCL::ContextMenu menu(IDR_CONTEXT);
const bool isMoveable = (m_hostView.ItemCount() > 1);
const bool isFirstSelected = isSelection && (m_hostView.Selection() == 0);
const bool isLastSelected = isSelection && (m_hostView.Selection() == (m_hostView.ItemCount()-1));
menu.EnableCmd(ID_HOST_EDITHOST, isSelection);
menu.EnableCmd(ID_HOST_REMOVEHOST, isSelection);
menu.EnableCmd(ID_HOST_UP, isMoveable && isSelection && !isFirstSelected);
menu.EnableCmd(ID_HOST_DOWN, isMoveable && isSelection && !isLastSelected);
menu.EnableCmd(ID_HOST_COPYHOST, isSelection);
menu.EnableCmd(ID_HOST_SERVICES, isSelection);
menu.EnableCmd(ID_HOST_PROCESSES, isSelection);
ExecuteToolCmd::buildToolsContextMenu(m_model.m_tools, menu, isSelection);
menu.display(m_appWnd);
return 0;
}
////////////////////////////////////////////////////////////////////////////////
//! Initialise the hosts view.
void AppDlg::initialiseHostView()
{
m_hostView.ImageList(LVSIL_SMALL, IDB_HOST_ICONS, 16, RGB(255, 0, 255));
m_hostView.FullRowSelect(true);
m_hostView.InsertColumn(HOST_NAME, TXT("Host"), m_hostView.StringWidth(20), LVCFMT_LEFT);
m_hostView.InsertColumn(ENVIRONMENT, TXT("Environment"), m_hostView.StringWidth(15), LVCFMT_LEFT);
m_hostView.InsertColumn(DESCRIPTION, TXT("Description"), m_hostView.StringWidth(15), LVCFMT_LEFT);
for (size_t column = 0; column != m_model.m_queries.size(); ++column)
m_hostView.InsertColumn(column+CUSTOM_BASE, m_model.m_queries.query(column)->m_title, m_hostView.StringWidth(10), LVCFMT_RIGHT);
const size_t LAST_ERROR = CUSTOM_BASE+m_model.m_queries.size();
m_hostView.InsertColumn(LAST_ERROR, LastErrorName, m_hostView.StringWidth(25), LVCFMT_LEFT);
for (size_t i = 0; i != m_model.m_hosts.size(); ++i)
addHostToView(i);
}
////////////////////////////////////////////////////////////////////////////////
//! Add a host to the view.
void AppDlg::addHostToView(size_t index)
{
m_hostView.InsertItem(index, m_model.m_hosts.host(index)->m_name, STATUS_UNKNOWN);
m_hostView.ItemText (index, ENVIRONMENT, m_model.m_hosts.host(index)->m_environment);
m_hostView.ItemText (index, DESCRIPTION, m_model.m_hosts.host(index)->m_description);
}
////////////////////////////////////////////////////////////////////////////////
//! Formats a memory size value. Memory sizes are KB based.
tstring FormatMemoryValue(uint64 valueInKb)
{
const double valueInGb = valueInKb / (1024.0 * 1024.0);
return Core::fmt(TXT("%.1f GB"), valueInGb);
}
////////////////////////////////////////////////////////////////////////////////
//! Formats the amount of disk space used.
tstring FormatDiskUsage(const WMI::Win32_LogicalDisk& disk)
{
const uint64 diskSize = disk.Size();
const uint64 freeSpace = disk.FreeSpace();
const uint64 spaceUsed = diskSize - freeSpace;
const double percentUsed = (spaceUsed * 100.0) / diskSize;
return Core::fmt(TXT("%.f %%"), percentUsed);
}
////////////////////////////////////////////////////////////////////////////////
//! Rebuild the view.
void AppDlg::rebuildView()
{
typedef std::map<tstring, size_t> NameWidthMap;
NameWidthMap columnWidths;
const size_t numColumns = m_hostView.NumColumns();
for (size_t column = CUSTOM_BASE; column != numColumns; ++column)
columnWidths[m_hostView.ColumnName(column)] = m_hostView.ColumnWidth(column);
NameWidthMap::const_iterator end = columnWidths.end();
for (size_t column = CUSTOM_BASE; column != numColumns; ++column)
m_hostView.DeleteColumn(CUSTOM_BASE);
const size_t defaultWidth = m_hostView.StringWidth(10);
for (size_t column = 0; column != m_model.m_queries.size(); ++column)
{
ConstQueryPtr query = m_model.m_queries.query(column);
const size_t width = Core::findOrDefault(columnWidths, query->m_title, defaultWidth);
m_hostView.InsertColumn(column+CUSTOM_BASE, query->m_title, width, LVCFMT_RIGHT);
}
const size_t LAST_ERROR = CUSTOM_BASE+m_model.m_queries.size();
m_hostView.InsertColumn(LAST_ERROR, LastErrorName, columnWidths[LastErrorName], LVCFMT_LEFT);
}
////////////////////////////////////////////////////////////////////////////////
//! Refresh the hosts view.
void AppDlg::refreshView()
{
CBusyCursor waitCursor;
bool abort = false;
for (size_t i = 0; (i != m_model.m_hosts.size()) && (!abort); ++i)
{
if (!refreshHost(i))
abort = true;
}
}
////////////////////////////////////////////////////////////////////////////////
//! Refresh the view for a single host. Returns true to continue or false if
//! the refreshing is to be aborted.
bool AppDlg::refreshHost(size_t index)
{
clearHost(index);
ConstHostPtr host = m_model.m_hosts.host(index);
if (!host->m_monitor)
return true;
ConstLogonPtr logon;
// bool newLogon = false;
if (!host->m_logon.empty())
{
logon = m_logons.find(host->m_logon);
if (logon.empty())
{
LogonDialog dialog;
dialog.m_logon.m_user = host->m_logon;
if (dialog.RunModal(m_appWnd) != IDOK)
return false;
// logon = makeLogon(dialog.m_logon);
// newLogon = true;
}
}
const size_t LAST_ERROR = CUSTOM_BASE+m_model.m_queries.size();
try
{
QueryRunner::const_iterator begin = m_model.m_queries.begin();
QueryRunner::const_iterator end = m_model.m_queries.end();
WMI::Connection connection;
if (logon.empty())
connection.open(host->m_name);
else
connection.open(host->m_name, logon->m_user, logon->m_password);
// if (newLogon)
// m_logons.set(logon);
QueryRunner::Results results = QueryRunner::run(connection, begin, end);
for (size_t column = 0; column != results.size(); ++column)
m_hostView.ItemText(index, column+CUSTOM_BASE, results[column]);
m_hostView.ItemImage(index, STATUS_GOOD);
}
catch (const WMI::Exception& e)
{
m_hostView.ItemText(index, LAST_ERROR, e.twhat());
m_hostView.ItemImage(index, STATUS_BAD);
}
return true;
}
////////////////////////////////////////////////////////////////////////////////
//! Clear the results for the host.
void AppDlg::clearHost(size_t index)
{
const size_t numColumns = m_hostView.NumColumns();
for (size_t column = CUSTOM_BASE; column != numColumns; ++column)
m_hostView.ItemText(index, column+CUSTOM_BASE, TXT(""));
m_hostView.ItemImage(index, STATUS_UNKNOWN);
}