-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathUnixSetupDlg.cpp
486 lines (449 loc) · 13.8 KB
/
UnixSetupDlg.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
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
//====================================================================================
// Open Computer and Software Inventory Next Generation
// Copyright (C) 2010 OCS Inventory NG Team. All rights reserved.
// Web: http://www.ocsinventory-ng.org
// This code is open source and may be copied and modified as long as the source
// code is always made freely available.
// Please refer to the General Public Licence V2 http://www.gnu.org/ or Licence.txt
//====================================================================================
// UnixSetupDlg.cpp : implementation file
//
#include "stdafx.h"
#include "ocs_deploy_tool.h"
#include "AgentSettings.h"
#include "UnixAgentSettings.h"
#include "UnixSetupDlg.h"
#include "FileVersion.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif
/////////////////////////////////////////////////////////////////////////////
// CUnixSetupDlg dialog
CUnixSetupDlg::CUnixSetupDlg(CWnd* pParent /*=NULL*/)
: CDialog(CUnixSetupDlg::IDD, pParent)
{
//{{AFX_DATA_INIT(CUnixSetupDlg)
// NOTE: the ClassWizard will add member initialization here
//}}AFX_DATA_INIT
}
void CUnixSetupDlg::DoDataExchange(CDataExchange* pDX)
{
CDialog::DoDataExchange(pDX);
//{{AFX_DATA_MAP(CUnixSetupDlg)
DDX_Control(pDX, IDC_COMBO_ETC_DIRECTORY, m_EtcCombo);
DDX_Control(pDX, IDC_LIST_FILES, m_ListFiles);
//}}AFX_DATA_MAP
}
BEGIN_MESSAGE_MAP(CUnixSetupDlg, CDialog)
//{{AFX_MSG_MAP(CUnixSetupDlg)
ON_BN_CLICKED(IDC_BUTTON_ADD, OnButtonAdd)
ON_BN_CLICKED(IDC_BUTTON_EXE, OnButtonExe)
ON_BN_CLICKED(IDC_BUTTON_REMOVE, OnButtonRemove)
ON_BN_CLICKED(ID_WIZNEXT, OnWiznext)
ON_BN_CLICKED(ID_WIZBACK, OnWizback)
ON_LBN_DBLCLK(IDC_LIST_FILES, OnDblclkListFiles)
ON_BN_CLICKED(IDC_BUTTON_CHANGE_DIRECTORY, OnButtonChangeDirectory)
ON_BN_CLICKED(IDC_CHECK_ETC_DIRECTORY, OnCheckEtcDirectory)
//}}AFX_MSG_MAP
END_MESSAGE_MAP()
/////////////////////////////////////////////////////////////////////////////
// CUnixSetupDlg message handlers
BOOL CUnixSetupDlg::OnInitDialog()
{
CDialog::OnInitDialog();
// TODO: Add extra initialization here
try
{
CString csMessage, csSection, csNumber;
BOOL bNoMoreValue = FALSE;
int nIndex = 0;
UINT uValue;
csSection.Format( _T("%s\\%s"), AGENT_SECTION, OPTION_UNIX_AGENT_OTHER_FILES);
// Get tool version
CFileVersion fileVer;
CString csVersion;
// Get application path
if (GetModuleFileName( AfxGetInstanceHandle(), csVersion.GetBuffer( 4*_MAX_PATH+1), 4*_MAX_PATH) == 0)
{
csVersion.Empty();
}
else
{
csVersion.ReleaseBuffer();
// Open application file to get version from file
if (fileVer.Open( csVersion))
{
csVersion = fileVer.GetProductVersion();
csVersion.Remove( ' ');
csVersion.Replace( ',', '.');
fileVer.Close();
}
else
csVersion.Empty();
}
csMessage.FormatMessage( IDS_OCS_DEPLOY_TOOL, csVersion);
SetDlgItemText( IDC_STATUS, csMessage);
// Set Agent setup file
csMessage = AfxGetApp()->GetProfileString( AGENT_SECTION, OPTION_UNIX_AGENT_SETUP_FILE, _T( ""));
SetDlgItemText( IDC_EDIT_EXE, csMessage);
// Set list of other files to copy
while (!bNoMoreValue)
{
csNumber.Format( _T( "%d"), nIndex);
csMessage = AfxGetApp()->GetProfileString( csSection, csNumber, _T( ""));
if (!csMessage.IsEmpty())
m_ListFiles.AddString( csMessage);
else
bNoMoreValue = TRUE;
nIndex ++;
}
// Set OCS server address
csMessage = AfxGetApp()->GetProfileString( AGENT_SECTION, OPTION_SERVER_ADDRESS, DEFAULT_SERVER_ADDRESS);
SetDlgItemText( IDC_EDIT_SERVER_IP, csMessage);
// Set TAG value
/* csMessage = AfxGetApp()->GetProfileString( AGENT_SECTION, OPTION_UNIX_AGENT_TAG_VALUE, _T( ""));
SetDlgItemText( IDC_EDIT_TAG_VALUE, csMessage);
// Set if immediate launch of agent required
uValue = AfxGetApp()->GetProfileInt( AGENT_SECTION, OPTION_UNIX_AGENT_LAUNCH_NOW, 0);
CheckDlgButton( IDC_CHECK_LAUNCH_INVENTORY, uValue);
*/ // Set agent's var directory
csMessage = AfxGetApp()->GetProfileString( AGENT_SECTION, OPTION_UNIX_AGENT_VAR_DIRECTORY, DEFAULT_UNIX_AGENT_VAR_DIRECTORY);
if (csMessage.Compare( DEFAULT_UNIX_AGENT_VAR_DIRECTORY) != 0)
{
// Not default setup folder selected, so enable checkbox
CheckDlgButton( IDC_BUTTON_CHANGE_DIRECTORY, TRUE);
GetDlgItem( IDC_EDIT_INSTALL)->EnableWindow( TRUE);
}
SetDlgItemText( IDC_EDIT_INSTALL, m_pSettings->GetAgentSetupDirectory());
// Set agent etc directory to use
csMessage = AfxGetApp()->GetProfileString( AGENT_SECTION, OPTION_UNIX_AGENT_ETC_DIRECTORY, DEFAULT_UNIX_AGENT_ETC_DIRECTORY);
if (csMessage.Compare( DEFAULT_UNIX_AGENT_ETC_DIRECTORY) != 0)
{
CheckDlgButton( IDC_CHECK_ETC_DIRECTORY, 1);
m_EtcCombo.SelectString( 0, csMessage);
m_EtcCombo.EnableWindow( TRUE);
}
else
{
CheckDlgButton( IDC_CHECK_ETC_DIRECTORY, 0);
m_EtcCombo.SelectString( 0, csMessage);
m_EtcCombo.EnableWindow( FALSE);
}
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
}
return TRUE; // return TRUE unless you set the focus to a control
// EXCEPTION: OCX Property Pages should return FALSE
}
void CUnixSetupDlg::OnButtonExe()
{
// TODO: Add your control notification handler code here
LPITEMIDLIST pMyIdList = NULL;
LPMALLOC pIMalloc;
try
{
CFileDialog dlgOpenFile( TRUE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, _T( "OCS Inventory NG Agent for Unix Archive|Ocsinventory-Agent*.tar.gz|TAR GZ files|*.tar.gz||"));
TCHAR szInitialFolder[4*_MAX_PATH+1];
CString csMessage;
// Get User Desktop path
if (SHGetMalloc( &pIMalloc) != NOERROR)
{
AfxMessageBox( IDS_ERROR_DISPLAY_FOLDER, MB_ICONSTOP);
return;
}
if (SHGetSpecialFolderLocation( m_hWnd, CSIDL_DESKTOP, &pMyIdList) != NOERROR)
{
AfxMessageBox( IDS_ERROR_DISPLAY_FOLDER, MB_ICONSTOP);
return;
}
if (!SHGetPathFromIDList( pMyIdList, szInitialFolder))
{
AfxMessageBox( IDS_ERROR_DISPLAY_FOLDER, MB_ICONSTOP);
pIMalloc->Free( pMyIdList);
return;
}
pIMalloc->Free( pMyIdList);
// Fill in the OPENFILENAME structure to support a template and hook.
dlgOpenFile.m_ofn.lpstrInitialDir = szInitialFolder;
if (!csMessage.LoadString( IDS_SELECT_AGENT_FILE))
AfxThrowMemoryException();
dlgOpenFile.m_ofn.lpstrTitle = csMessage.GetBuffer( csMessage.GetLength());
if (dlgOpenFile.DoModal() != IDOK)
return;
SetDlgItemText( IDC_EDIT_EXE, dlgOpenFile.GetPathName());
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
if (pMyIdList != NULL)
pIMalloc->Free( pMyIdList);
return;
}
}
void CUnixSetupDlg::OnButtonAdd()
{
// TODO: Add your control notification handler code here
LPITEMIDLIST pMyIdList = NULL;
LPMALLOC pIMalloc;
try
{
CFileDialog dlgOpenFile( TRUE, NULL, NULL, OFN_FILEMUSTEXIST | OFN_HIDEREADONLY, _T( "All files|*.*||"));
TCHAR szInitialFolder[4*_MAX_PATH+1];
CString csMessage;
// Get User Desktop path
if (SHGetMalloc( &pIMalloc) != NOERROR)
{
AfxMessageBox( IDS_ERROR_DISPLAY_FOLDER, MB_ICONSTOP);
return;
}
if (SHGetSpecialFolderLocation( m_hWnd, CSIDL_DESKTOP, &pMyIdList) != NOERROR)
{
AfxMessageBox( IDS_ERROR_DISPLAY_FOLDER, MB_ICONSTOP);
return;
}
if (!SHGetPathFromIDList( pMyIdList, szInitialFolder))
{
AfxMessageBox( IDS_ERROR_DISPLAY_FOLDER, MB_ICONSTOP);
pIMalloc->Free( pMyIdList);
return;
}
pIMalloc->Free( pMyIdList);
pMyIdList = NULL;
// Fill in the OPENFILENAME structure to support a template and hook.
dlgOpenFile.m_ofn.lpstrInitialDir = szInitialFolder;
if (!csMessage.LoadString( IDS_SELECT_OTHER_FILE))
AfxThrowMemoryException();
dlgOpenFile.m_ofn.lpstrTitle = csMessage.GetBuffer( csMessage.GetLength());
if (dlgOpenFile.DoModal() != IDOK)
return;
m_ListFiles.AddString( dlgOpenFile.GetPathName());
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
if (pMyIdList != NULL)
pIMalloc->Free( pMyIdList);
return;
}
}
void CUnixSetupDlg::OnButtonRemove()
{
// TODO: Add your control notification handler code here
try
{
int nCurSel;
if ((nCurSel = m_ListFiles.GetCurSel()) == LB_ERR )
// No item selected
return;
m_ListFiles.DeleteString( nCurSel);
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
return;
}
}
void CUnixSetupDlg::OnDblclkListFiles()
{
// TODO: Add your control notification handler code here
OnButtonRemove();
}
void CUnixSetupDlg::OnWiznext()
{
// TODO: Add your control notification handler code here
try
{
if (!Save( TRUE))
return;
EndDialog( ID_WIZNEXT);
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
return;
}
}
void CUnixSetupDlg::OnWizback()
{
// TODO: Add your control notification handler code here
try
{
Save();
EndDialog( ID_WIZBACK);
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
return;
}
}
void CUnixSetupDlg::OnCancel()
{
// TODO: Add extra cleanup here
CDialog::OnCancel();
}
BOOL CUnixSetupDlg::Save( BOOL bDisplayError)
{
int nIndex, nCount = 0;
CString csMessage, csSection, csNumber;
CStringList *pList;
UINT uValue;
csSection.Format( _T("%s\\%s"), AGENT_SECTION, OPTION_UNIX_AGENT_OTHER_FILES);
// Get Agent setup file
if (GetDlgItemText( IDC_EDIT_EXE, csMessage) == 0)
{
// No data
bDisplayError && AfxMessageBox( IDS_ERROR_INVALID_VALUE, MB_ICONINFORMATION);
GetDlgItem( IDC_EDIT_EXE)->SetFocus();
return FALSE;
}
AfxGetApp()->WriteProfileString( AGENT_SECTION, OPTION_UNIX_AGENT_SETUP_FILE, csMessage);
m_pSettings->SetAgentSetupFile( csMessage);
// Remove registry stored files, before saving new ones
RemoveOtherFilesFromRegistry();
// Get list of other files to copy
pList = m_pSettings->GetAgentOtherFiles();
if (((nCount = m_ListFiles.GetCount()) == LB_ERR) || (nCount == 0))
{
// No items in List => NO FILES TO COPY
pList->RemoveAll();
}
else
{
// Empty current list
pList->RemoveAll();
// Fill in file list with each file from listbox
for (nIndex = 0; nIndex < nCount; nIndex++)
{
m_ListFiles.GetText( nIndex, csMessage);
pList->AddTail( csMessage);
csNumber.Format( _T( "%d"), nIndex);
AfxGetApp()->WriteProfileString( csSection, csNumber, csMessage);
}
}
// Get OCS server address
if (GetDlgItemText( IDC_EDIT_SERVER_IP, csMessage) == 0)
{
// No data
bDisplayError && AfxMessageBox( IDS_ERROR_INVALID_VALUE, MB_ICONINFORMATION);
GetDlgItem( IDC_EDIT_SERVER_IP)->SetFocus();
return FALSE;
}
csMessage.MakeLower();
if ((csMessage.Find( _T( "http://"), 0) != 0) && (csMessage.Find( _T( "https://"), 0) != 0))
{
// Server URI does not start with http:// or https://
bDisplayError && AfxMessageBox( IDS_ERROR_INVALID_VALUE, MB_ICONINFORMATION);
GetDlgItem( IDC_EDIT_SERVER_IP)->SetFocus();
return FALSE;
}
AfxGetApp()->WriteProfileString( AGENT_SECTION, OPTION_SERVER_ADDRESS, csMessage);
m_pSettings->SetServerAddress( csMessage);
// Get TAG value
GetDlgItemText( IDC_EDIT_TAG_VALUE, csMessage);
/* AfxGetApp()->WriteProfileString( AGENT_SECTION, OPTION_UNIX_AGENT_TAG_VALUE, csMessage);
m_pSettings->SetTagValue( csMessage);
// Get if immediate launch of agent required
uValue = IsDlgButtonChecked( IDC_CHECK_LAUNCH_INVENTORY);
AfxGetApp()->WriteProfileInt( AGENT_SECTION, OPTION_UNIX_AGENT_LAUNCH_NOW, uValue);
m_pSettings->SetLaunchNowRequired( uValue);
*/ // Get agent's var directory
if (GetDlgItemText( IDC_EDIT_INSTALL, csMessage) == 0)
{
// No data
bDisplayError && AfxMessageBox( IDS_ERROR_INVALID_VALUE, MB_ICONINFORMATION);
GetDlgItem( IDC_EDIT_INSTALL)->SetFocus();
return FALSE;
}
AfxGetApp()->WriteProfileString( AGENT_SECTION, OPTION_UNIX_AGENT_VAR_DIRECTORY, csMessage);
m_pSettings->SetAgentSetupDirectory( csMessage);
// Get agent's etc directory
if (GetDlgItemText( IDC_COMBO_ETC_DIRECTORY, csMessage) == 0)
{
// No data
bDisplayError && AfxMessageBox( IDS_ERROR_INVALID_VALUE, MB_ICONINFORMATION);
GetDlgItem( IDC_COMBO_ETC_DIRECTORY)->SetFocus();
return FALSE;
}
AfxGetApp()->WriteProfileString( AGENT_SECTION, OPTION_UNIX_AGENT_ETC_DIRECTORY, csMessage);
m_pSettings->SetAgentEtcDirectory( csMessage);
return TRUE;
}
void CUnixSetupDlg::SetAgentSetting( CAgentSettings *pSettings)
{
m_pSettings = pSettings;
}
void CUnixSetupDlg::OnButtonChangeDirectory()
{
// TODO: Add your control notification handler code here
try
{
if (IsDlgButtonChecked( IDC_BUTTON_CHANGE_DIRECTORY))
GetDlgItem( IDC_EDIT_INSTALL)->EnableWindow( TRUE);
else
{
GetDlgItem( IDC_EDIT_INSTALL)->EnableWindow( FALSE);
SetDlgItemText( IDC_EDIT_INSTALL, DEFAULT_UNIX_AGENT_VAR_DIRECTORY);
}
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
return;
}
}
BOOL CUnixSetupDlg::RemoveOtherFilesFromRegistry()
{
HKEY hKey;
CString csKey, csValue;
DWORD dwErr, dwLength;
UINT uIndex = 0;
csKey.Format( _T( "SOFTWARE\\OCS_DEPLOY_TOOL\\%s\\%s"), AGENT_SECTION, OPTION_UNIX_AGENT_OTHER_FILES);
if ((dwErr = RegOpenKeyEx( HKEY_CURRENT_USER, csKey, 0, KEY_READ|KEY_WRITE, &hKey)) != ERROR_SUCCESS)
return FALSE;
while (dwErr == ERROR_SUCCESS)
{
csValue.Format( _T( "%u"), uIndex);
// Check if value exists
dwErr = REG_SZ;
dwLength = 0;
if ((dwErr = RegQueryValueEx( hKey, csValue, NULL, &dwErr, NULL, &dwLength)) == ERROR_SUCCESS)
// Key exists, delete it
RegDeleteValue( hKey, csValue);
// Continue
uIndex ++;
}
RegCloseKey( hKey);
return TRUE;
}
void CUnixSetupDlg::OnCheckEtcDirectory()
{
// TODO: Add your control notification handler code here
try
{
if (IsDlgButtonChecked( IDC_CHECK_ETC_DIRECTORY))
m_EtcCombo.EnableWindow( TRUE);
else
{
m_EtcCombo.SelectString( 0, DEFAULT_UNIX_AGENT_ETC_DIRECTORY);
m_EtcCombo.EnableWindow( FALSE);
}
}
catch( CException *pEx)
{
pEx->ReportError( MB_OK|MB_ICONSTOP);
pEx->Delete();
return;
}
}