]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSWindows/ControlPanel/ThirdPage.cpp
mDNSResponder-107.tar.gz
[apple/mdnsresponder.git] / mDNSWindows / ControlPanel / ThirdPage.cpp
1 /*
2 * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22
23 Change History (most recent first):
24
25 $Log: ThirdPage.cpp,v $
26 Revision 1.3 2005/03/07 18:27:42 shersche
27 <rdar://problem/4037940> Fix problem when ControlPanel commits changes to the browse domain list
28
29 Revision 1.2 2005/03/03 19:55:22 shersche
30 <rdar://problem/4034481> ControlPanel source code isn't saving CVS log info
31
32
33 */
34
35 #include "ThirdPage.h"
36 #include "resource.h"
37
38 #include "ConfigPropertySheet.h"
39 #include "SharedSecret.h"
40
41 #include <WinServices.h>
42
43 #define MAX_KEY_LENGTH 255
44
45
46 IMPLEMENT_DYNCREATE(CThirdPage, CPropertyPage)
47
48
49 //---------------------------------------------------------------------------------------------------------------------------
50 // CThirdPage::CThirdPage
51 //---------------------------------------------------------------------------------------------------------------------------
52
53 CThirdPage::CThirdPage()
54 :
55 CPropertyPage(CThirdPage::IDD)
56 {
57 //{{AFX_DATA_INIT(CThirdPage)
58 //}}AFX_DATA_INIT
59
60 m_firstTime = true;
61 }
62
63
64 //---------------------------------------------------------------------------------------------------------------------------
65 // CThirdPage::~CThirdPage
66 //---------------------------------------------------------------------------------------------------------------------------
67
68 CThirdPage::~CThirdPage()
69 {
70 }
71
72
73 //---------------------------------------------------------------------------------------------------------------------------
74 // CThirdPage::DoDataExchange
75 //---------------------------------------------------------------------------------------------------------------------------
76
77 void CThirdPage::DoDataExchange(CDataExchange* pDX)
78 {
79 CPropertyPage::DoDataExchange(pDX);
80 //{{AFX_DATA_MAP(CThirdPage)
81 //}}AFX_DATA_MAP
82 DDX_Control(pDX, IDC_BROWSE_LIST, m_browseListCtrl);
83 DDX_Control(pDX, IDC_REMOVE_BROWSE_DOMAIN, m_removeButton);
84 }
85
86 BEGIN_MESSAGE_MAP(CThirdPage, CPropertyPage)
87 //{{AFX_MSG_MAP(CThirdPage)
88 //}}AFX_MSG_MAP
89 ON_BN_CLICKED(IDC_ADD_BROWSE_DOMAIN, OnBnClickedAddBrowseDomain)
90 ON_BN_CLICKED(IDC_REMOVE_BROWSE_DOMAIN, OnBnClickedRemoveBrowseDomain)
91 ON_NOTIFY(LVN_ITEMCHANGED, IDC_BROWSE_LIST, OnLvnItemchangedBrowseList)
92 END_MESSAGE_MAP()
93
94
95 //---------------------------------------------------------------------------------------------------------------------------
96 // CThirdPage::SetModified
97 //---------------------------------------------------------------------------------------------------------------------------
98
99 void CThirdPage::SetModified( BOOL bChanged )
100 {
101 m_modified = bChanged;
102
103 CPropertyPage::SetModified( bChanged );
104 }
105
106
107 //---------------------------------------------------------------------------------------------------------------------------
108 // CThirdPage::OnSetActive
109 //---------------------------------------------------------------------------------------------------------------------------
110
111 BOOL
112 CThirdPage::OnSetActive()
113 {
114 CConfigPropertySheet * psheet;
115 HKEY key = NULL;
116 HKEY subKey = NULL;
117 DWORD dwSize;
118 DWORD enabled;
119 DWORD err;
120 TCHAR subKeyName[MAX_KEY_LENGTH];
121 DWORD cSubKeys = 0;
122 DWORD cbMaxSubKey;
123 DWORD cchMaxClass;
124 int nIndex;
125 DWORD i;
126 BOOL b = CPropertyPage::OnSetActive();
127
128 psheet = reinterpret_cast<CConfigPropertySheet*>(GetParent());
129 require_quiet( psheet, exit );
130
131 m_modified = FALSE;
132
133 if ( m_firstTime )
134 {
135 m_browseListCtrl.SetExtendedStyle((m_browseListCtrl.GetStyle() & (~LVS_EX_GRIDLINES))|LVS_EX_CHECKBOXES | LVS_EX_FULLROWSELECT);
136
137 m_browseListCtrl.InsertColumn(0, L"", LVCFMT_LEFT, 20 );
138 m_browseListCtrl.InsertColumn(1, L"", LVCFMT_LEFT, 345);
139
140 m_firstTime = false;
141 }
142
143 m_initialized = false;
144
145 // Clear out what's there
146
147 m_browseListCtrl.DeleteAllItems();
148
149 // Now populate the browse domain box
150
151 err = RegCreateKey( HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Services\\" kServiceName L"\\Parameters\\DynDNS\\Setup\\" kServiceDynDNSBrowseDomains, &key );
152 require_noerr( err, exit );
153
154 // Get information about this node
155
156 err = RegQueryInfoKey( key, NULL, NULL, NULL, &cSubKeys, &cbMaxSubKey, &cchMaxClass, NULL, NULL, NULL, NULL, NULL );
157 require_noerr( err, exit );
158
159 for ( i = 0; i < cSubKeys; i++)
160 {
161 dwSize = MAX_KEY_LENGTH;
162
163 err = RegEnumKeyEx( key, i, subKeyName, &dwSize, NULL, NULL, NULL, NULL );
164 require_noerr( err, exit );
165
166 err = RegOpenKey( key, subKeyName, &subKey );
167 require_noerr( err, exit );
168
169 dwSize = sizeof( DWORD );
170 err = RegQueryValueEx( subKey, L"Enabled", NULL, NULL, (LPBYTE) &enabled, &dwSize );
171 require_noerr( err, exit );
172
173 nIndex = m_browseListCtrl.InsertItem( m_browseListCtrl.GetItemCount(), L"");
174 m_browseListCtrl.SetItemText( nIndex, 1, subKeyName );
175 m_browseListCtrl.SetCheck( nIndex, enabled );
176
177 RegCloseKey( subKey );
178 subKey = NULL;
179 }
180
181 m_browseListCtrl.SortItems( SortFunc, (DWORD_PTR) this );
182
183 m_removeButton.EnableWindow( FALSE );
184
185 exit:
186
187 if ( subKey )
188 {
189 RegCloseKey( subKey );
190 }
191
192 if ( key )
193 {
194 RegCloseKey( key );
195 }
196
197 m_initialized = true;
198
199 return b;
200 }
201
202
203
204 //---------------------------------------------------------------------------------------------------------------------------
205 // CThirdPage::OnOK
206 //---------------------------------------------------------------------------------------------------------------------------
207
208 void
209 CThirdPage::OnOK()
210 {
211 if ( m_modified )
212 {
213 Commit();
214 }
215 }
216
217
218
219 //---------------------------------------------------------------------------------------------------------------------------
220 // CThirdPage::Commit
221 //---------------------------------------------------------------------------------------------------------------------------
222
223 void
224 CThirdPage::Commit()
225 {
226 HKEY key = NULL;
227 HKEY subKey = NULL;
228 TCHAR subKeyName[MAX_KEY_LENGTH];
229 DWORD cSubKeys = 0;
230 DWORD cbMaxSubKey;
231 DWORD cchMaxClass;
232 DWORD dwSize;
233 int i;
234 DWORD err;
235
236 err = RegCreateKey( HKEY_LOCAL_MACHINE, L"SYSTEM\\CurrentControlSet\\Services\\" kServiceName L"\\Parameters\\DynDNS\\Setup\\" kServiceDynDNSBrowseDomains, &key );
237 require_noerr( err, exit );
238
239 // First, remove all the entries that are there
240
241 err = RegQueryInfoKey( key, NULL, NULL, NULL, &cSubKeys, &cbMaxSubKey, &cchMaxClass, NULL, NULL, NULL, NULL, NULL );
242 require_noerr( err, exit );
243
244 for ( i = 0; i < (int) cSubKeys; i++ )
245 {
246 dwSize = MAX_KEY_LENGTH;
247
248 err = RegEnumKeyEx( key, 0, subKeyName, &dwSize, NULL, NULL, NULL, NULL );
249 require_noerr( err, exit );
250
251 err = RegDeleteKey( key, subKeyName );
252 require_noerr( err, exit );
253 }
254
255 // Now re-populate
256
257 for ( i = 0; i < m_browseListCtrl.GetItemCount(); i++ )
258 {
259 DWORD enabled = (DWORD) m_browseListCtrl.GetCheck( i );
260
261 err = RegCreateKey( key, m_browseListCtrl.GetItemText( i, 1 ), &subKey );
262 require_noerr( err, exit );
263
264 err = RegSetValueEx( subKey, L"Enabled", NULL, REG_DWORD, (LPBYTE) &enabled, sizeof( enabled ) );
265 require_noerr( err, exit );
266
267 RegCloseKey( subKey );
268 subKey = NULL;
269 }
270
271 exit:
272
273 if ( subKey )
274 {
275 RegCloseKey( subKey );
276 }
277
278 if ( key )
279 {
280 RegCloseKey( key );
281 }
282 }
283
284
285
286 //---------------------------------------------------------------------------------------------------------------------------
287 // CThirdPage::OnBnClickedAddBrowseDomain
288 //---------------------------------------------------------------------------------------------------------------------------
289
290 void
291 CThirdPage::OnBnClickedAddBrowseDomain()
292 {
293 CAddBrowseDomain dlg( GetParent() );
294
295 if ( ( dlg.DoModal() == IDOK ) && ( dlg.m_text.GetLength() > 0 ) )
296 {
297 int nIndex;
298
299 nIndex = m_browseListCtrl.InsertItem( m_browseListCtrl.GetItemCount(), L"");
300 m_browseListCtrl.SetItemText( nIndex, 1, dlg.m_text );
301 m_browseListCtrl.SetCheck( nIndex, 1 );
302
303 m_browseListCtrl.SortItems( SortFunc, (DWORD_PTR) this );
304
305 m_browseListCtrl.Invalidate();
306
307 SetModified( TRUE );
308 }
309 }
310
311
312 //---------------------------------------------------------------------------------------------------------------------------
313 // CThirdPage::OnBnClickedRemoveBrowseDomain
314 //---------------------------------------------------------------------------------------------------------------------------
315
316 void
317 CThirdPage::OnBnClickedRemoveBrowseDomain()
318 {
319 UINT selectedCount = m_browseListCtrl.GetSelectedCount();
320 int nItem = -1;
321 UINT i;
322
323 // Update all of the selected items.
324
325 for ( i = 0; i < selectedCount; i++ )
326 {
327 nItem = m_browseListCtrl.GetNextItem( -1, LVNI_SELECTED );
328 check( nItem != -1 );
329
330 m_browseListCtrl.DeleteItem( nItem );
331
332 SetModified( TRUE );
333 }
334
335 m_removeButton.EnableWindow( FALSE );
336 }
337
338
339 void
340 CThirdPage::OnLvnItemchangedBrowseList(NMHDR *pNMHDR, LRESULT *pResult)
341 {
342 if ( m_browseListCtrl.GetSelectedCount() )
343 {
344 m_removeButton.EnableWindow( TRUE );
345 }
346
347 if ( m_initialized )
348 {
349 NM_LISTVIEW * pNMListView = (NM_LISTVIEW*)pNMHDR;
350
351 BOOL bPrevState = (BOOL) ( ( ( pNMListView->uOldState & LVIS_STATEIMAGEMASK ) >> 12 ) - 1 );
352
353 if ( bPrevState < 0 )
354 {
355 bPrevState = 0;
356 }
357
358
359 BOOL bChecked = ( BOOL ) ( ( ( pNMListView->uNewState & LVIS_STATEIMAGEMASK ) >> 12) - 1 );
360
361 if ( bChecked < 0 )
362 {
363 bChecked = 0;
364 }
365
366 if ( bPrevState != bChecked )
367 {
368 SetModified( TRUE );
369 }
370 }
371
372 *pResult = 0;
373 }
374
375
376
377 int CALLBACK
378 CThirdPage::SortFunc(LPARAM lParam1, LPARAM lParam2, LPARAM lParamSort)
379 {
380 CString str1;
381 CString str2;
382 int ret = 0;
383
384 CThirdPage * self = reinterpret_cast<CThirdPage*>( lParamSort );
385 require_quiet( self, exit );
386
387 str1 = self->m_browseListCtrl.GetItemText( (int) lParam1, 1 );
388 str2 = self->m_browseListCtrl.GetItemText( (int) lParam2, 1 );
389
390 ret = str1.Compare( str2 );
391
392 exit:
393
394 return ret;
395 }
396
397
398 // CAddBrowseDomain dialog
399
400 IMPLEMENT_DYNAMIC(CAddBrowseDomain, CDialog)
401 CAddBrowseDomain::CAddBrowseDomain(CWnd* pParent /*=NULL*/)
402 : CDialog(CAddBrowseDomain::IDD, pParent)
403 {
404 }
405
406 CAddBrowseDomain::~CAddBrowseDomain()
407 {
408 }
409
410 void CAddBrowseDomain::DoDataExchange(CDataExchange* pDX)
411 {
412 CDialog::DoDataExchange(pDX);
413 DDX_Control(pDX, IDC_COMBO1, m_comboBox);
414 }
415
416
417 BOOL
418 CAddBrowseDomain::OnInitDialog()
419 {
420 CConfigPropertySheet * psheet;
421 CConfigPropertySheet::StringList::iterator it;
422
423 BOOL b = CDialog::OnInitDialog();
424
425 psheet = reinterpret_cast<CConfigPropertySheet*>(GetParent());
426 require_quiet( psheet, exit );
427
428 for ( it = psheet->m_browseDomains.begin(); it != psheet->m_browseDomains.end(); it++ )
429 {
430 CString text = *it;
431
432 if ( m_comboBox.FindStringExact( -1, *it ) == CB_ERR )
433 {
434 m_comboBox.AddString( *it );
435 }
436 }
437
438 exit:
439
440 return b;
441 }
442
443
444 void
445 CAddBrowseDomain::OnOK()
446 {
447 m_comboBox.GetWindowText( m_text );
448
449 CDialog::OnOK();
450 }
451
452
453
454 BEGIN_MESSAGE_MAP(CAddBrowseDomain, CDialog)
455 END_MESSAGE_MAP()
456