1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
18 #include "SecondPage.h"
21 #include "ConfigPropertySheet.h"
22 #include "SharedSecret.h"
24 #include <WinServices.h>
26 #define MAX_KEY_LENGTH 255
28 IMPLEMENT_DYNCREATE(CSecondPage, CPropertyPage)
31 //---------------------------------------------------------------------------------------------------------------------------
32 // CSecondPage::CSecondPage
33 //---------------------------------------------------------------------------------------------------------------------------
35 CSecondPage::CSecondPage()
37 CPropertyPage(CSecondPage::IDD),
40 //{{AFX_DATA_INIT(CSecondPage)
45 err = RegCreateKeyEx( HKEY_LOCAL_MACHINE, kServiceParametersNode L"\\DynDNS\\Setup\\" kServiceDynDNSRegistrationDomains, 0,
46 NULL, REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE, NULL, &m_setupKey, NULL );
51 //---------------------------------------------------------------------------------------------------------------------------
52 // CSecondPage::~CSecondPage
53 //---------------------------------------------------------------------------------------------------------------------------
55 CSecondPage::~CSecondPage()
59 RegCloseKey( m_setupKey );
65 //---------------------------------------------------------------------------------------------------------------------------
66 // CSecondPage::DoDataExchange
67 //---------------------------------------------------------------------------------------------------------------------------
69 void CSecondPage::DoDataExchange(CDataExchange* pDX)
71 CPropertyPage::DoDataExchange(pDX);
72 //{{AFX_DATA_MAP(CSecondPage)
74 DDX_Control(pDX, IDC_CHECK1, m_advertiseServicesButton);
75 DDX_Control(pDX, IDC_BUTTON1, m_sharedSecretButton);
76 DDX_Control(pDX, IDC_COMBO2, m_regDomainsBox);
79 BEGIN_MESSAGE_MAP(CSecondPage, CPropertyPage)
80 //{{AFX_MSG_MAP(CSecondPage)
82 ON_BN_CLICKED(IDC_BUTTON1, OnBnClickedSharedSecret)
83 ON_BN_CLICKED(IDC_CHECK1, OnBnClickedAdvertise)
84 ON_CBN_SELCHANGE(IDC_COMBO1, OnCbnSelChange)
85 ON_CBN_EDITCHANGE(IDC_COMBO1, OnCbnEditChange)
86 ON_CBN_EDITCHANGE(IDC_COMBO2, OnCbnEditChange)
87 ON_CBN_SELCHANGE(IDC_COMBO2, OnCbnSelChange)
92 //---------------------------------------------------------------------------------------------------------------------------
93 // CSecondPage::SetModified
94 //---------------------------------------------------------------------------------------------------------------------------
96 void CSecondPage::SetModified( BOOL bChanged )
98 m_modified = bChanged;
100 CPropertyPage::SetModified( bChanged );
104 //---------------------------------------------------------------------------------------------------------------------------
105 // CSecondPage::OnSetActive
106 //---------------------------------------------------------------------------------------------------------------------------
109 CSecondPage::OnSetActive()
111 CConfigPropertySheet * psheet;
113 BOOL b = CPropertyPage::OnSetActive();
115 psheet = reinterpret_cast<CConfigPropertySheet*>(GetParent());
116 require_quiet( psheet, exit );
120 // Clear out what's there
122 EmptyComboBox( m_regDomainsBox );
124 // Now populate the registration domain box
126 err = Populate( m_regDomainsBox, m_setupKey, psheet->m_regDomains );
135 //---------------------------------------------------------------------------------------------------------------------------
137 //---------------------------------------------------------------------------------------------------------------------------
150 //---------------------------------------------------------------------------------------------------------------------------
151 // CSecondPage::Commit
152 //---------------------------------------------------------------------------------------------------------------------------
155 CSecondPage::Commit()
159 if ( m_setupKey != NULL )
161 err = Commit( m_regDomainsBox, m_setupKey, m_advertiseServicesButton.GetCheck() == BST_CHECKED );
167 //---------------------------------------------------------------------------------------------------------------------------
168 // CSecondPage::Commit
169 //---------------------------------------------------------------------------------------------------------------------------
172 CSecondPage::Commit( CComboBox & box, HKEY key, DWORD enabled )
176 TCHAR subKeyName[MAX_KEY_LENGTH];
182 OSStatus err = kNoErr;
184 // First, remove all the entries that are there
186 err = RegQueryInfoKey( key, NULL, NULL, NULL, &cSubKeys, &cbMaxSubKey, &cchMaxClass, NULL, NULL, NULL, NULL, NULL );
187 require_noerr( err, exit );
189 for ( i = 0; i < (int) cSubKeys; i++ )
191 dwSize = MAX_KEY_LENGTH;
193 err = RegEnumKeyEx( key, 0, subKeyName, &dwSize, NULL, NULL, NULL, NULL );
194 require_noerr( err, exit );
196 err = RegDeleteKey( key, subKeyName );
197 require_noerr( err, exit );
202 box.GetWindowText( selected );
204 // If we haven't seen this string before, add the string to the box and
207 if ( ( selected.GetLength() > 0 ) && ( box.FindStringExact( -1, selected ) == CB_ERR ) )
211 box.AddString( selected );
213 err = RegQueryString( key, L"UserDefined", string );
216 if ( string.GetLength() )
218 string += L"," + selected;
225 err = RegSetValueEx( key, L"UserDefined", 0, REG_SZ, (LPBYTE) (LPCTSTR) string, ( string.GetLength() + 1) * sizeof( TCHAR ) );
229 // Save selected text in registry. This will trigger mDNSResponder to setup
230 // DynDNS config again
232 err = RegCreateKeyEx( key, selected, 0,
233 NULL, REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE, NULL, &subKey, NULL );
234 require_noerr( err, exit );
236 err = RegSetValueEx( subKey, L"Enabled", 0, REG_DWORD, (LPBYTE) &enabled, sizeof( DWORD ) );
243 RegCloseKey( subKey );
251 //---------------------------------------------------------------------------------------------------------------------------
252 // CSecondPage::OnBnClickedSharedSecret
253 //---------------------------------------------------------------------------------------------------------------------------
255 void CSecondPage::OnBnClickedSharedSecret()
259 m_regDomainsBox.GetWindowText( name );
265 if ( dlg.DoModal() == IDOK )
268 DWORD dwSize = sizeof( DWORD );
273 // We have now updated the secret, however the system service
274 // doesn't know about it yet. So we're going to update the
275 // registry with a dummy value which will cause the system
276 // service to re-initialize it's DynDNS setup
279 RegQueryValueEx( m_setupKey, L"Wakeup", NULL, NULL, (LPBYTE) &wakeup, &dwSize );
283 err = RegSetValueEx( m_setupKey, L"Wakeup", 0, REG_DWORD, (LPBYTE) &wakeup, sizeof( DWORD ) );
284 require_noerr( err, exit );
293 //---------------------------------------------------------------------------------------------------------------------------
294 // CSecondPage::OnBnClickedAdvertise
295 //---------------------------------------------------------------------------------------------------------------------------
297 void CSecondPage::OnBnClickedAdvertise()
301 state = m_advertiseServicesButton.GetCheck();
303 m_regDomainsBox.EnableWindow( state );
304 m_sharedSecretButton.EnableWindow( state );
310 //---------------------------------------------------------------------------------------------------------------------------
311 // CSecondPage::OnCbnSelChange
312 //---------------------------------------------------------------------------------------------------------------------------
314 void CSecondPage::OnCbnSelChange()
320 //---------------------------------------------------------------------------------------------------------------------------
321 // CSecondPage::OnCbnEditChange
322 //---------------------------------------------------------------------------------------------------------------------------
324 void CSecondPage::OnCbnEditChange()
331 //---------------------------------------------------------------------------------------------------------------------------
332 // CSecondPage::OnAddRegistrationDomain
333 //---------------------------------------------------------------------------------------------------------------------------
336 CSecondPage::OnAddRegistrationDomain( CString & domain )
338 int index = m_regDomainsBox.FindStringExact( -1, domain );
340 if ( index == CB_ERR )
342 m_regDomainsBox.AddString( domain );
347 //---------------------------------------------------------------------------------------------------------------------------
348 // CSecondPage::OnRemoveRegistrationDomain
349 //---------------------------------------------------------------------------------------------------------------------------
352 CSecondPage::OnRemoveRegistrationDomain( CString & domain )
354 int index = m_regDomainsBox.FindStringExact( -1, domain );
356 if ( index != CB_ERR )
358 m_regDomainsBox.DeleteString( index );
363 //---------------------------------------------------------------------------------------------------------------------------
364 // CSecondPage::EmptyComboBox
365 //---------------------------------------------------------------------------------------------------------------------------
368 CSecondPage::EmptyComboBox( CComboBox & box )
370 while ( box.GetCount() > 0 )
372 box.DeleteString( 0 );
377 //---------------------------------------------------------------------------------------------------------------------------
378 // CSecondPage::Populate
379 //---------------------------------------------------------------------------------------------------------------------------
382 CSecondPage::Populate( CComboBox & box, HKEY key, StringList & l )
388 TCHAR subKeyName[MAX_KEY_LENGTH];
394 err = RegQueryString( key, L"UserDefined", string );
396 if ( !err && string.GetLength() )
404 tok = string.SpanExcluding( L"," );
406 box.AddString( tok );
410 // Get rid of that string and comma
412 string = string.Right( string.GetLength() - tok.GetLength() - 1 );
421 StringList::iterator it;
423 for ( it = l.begin(); it != l.end(); it++ )
425 if ( box.FindStringExact( -1, *it ) == CB_ERR )
427 box.AddString( *it );
431 err = RegQueryInfoKey( key, NULL, NULL, NULL, &cSubKeys, &cbMaxSubKey, &cchMaxClass, NULL, NULL, NULL, NULL, NULL );
432 require_noerr( err, exit );
436 dwSize = MAX_KEY_LENGTH;
438 err = RegEnumKeyEx( key, 0, subKeyName, &dwSize, NULL, NULL, NULL, NULL );
439 require_noerr( err, exit );
441 err = RegOpenKey( key, subKeyName, &subKey );
442 require_noerr( err, exit );
444 dwSize = sizeof( DWORD );
445 err = RegQueryValueEx( subKey, L"Enabled", NULL, NULL, (LPBYTE) &enabled, &dwSize );
446 require_noerr( err, exit );
450 if ( box.SelectString( -1, subKeyName ) == CB_ERR )
454 box.AddString( subKeyName );
457 box.SelectString( -1, subKeyName );
459 RegCloseKey( subKey );
465 m_advertiseServicesButton.SetCheck( ( !err && enabled ) ? BST_CHECKED : BST_UNCHECKED );
466 m_regDomainsBox.EnableWindow( ( !err && enabled ) );
467 m_sharedSecretButton.EnableWindow( (!err && enabled ) );
473 //---------------------------------------------------------------------------------------------------------------------------
474 // CSecondPage::CreateKey
475 //---------------------------------------------------------------------------------------------------------------------------
478 CSecondPage::CreateKey( CString & name, DWORD enabled )
483 err = RegCreateKeyEx( HKEY_LOCAL_MACHINE, (LPCTSTR) name, 0,
484 NULL, REG_OPTION_NON_VOLATILE, KEY_READ|KEY_WRITE, NULL, &key, NULL );
485 require_noerr( err, exit );
487 err = RegSetValueEx( key, L"Enabled", 0, REG_DWORD, (LPBYTE) &enabled, sizeof( DWORD ) );
501 //---------------------------------------------------------------------------------------------------------------------------
502 // CSecondPage::RegQueryString
503 //---------------------------------------------------------------------------------------------------------------------------
506 CSecondPage::RegQueryString( HKEY key, CString valueName, CString & value )
524 string = (TCHAR*) malloc( stringLen );
525 require_action( string, exit, err = kUnknownErr );
528 err = RegQueryValueEx( key, valueName, 0, NULL, (LPBYTE) string, &stringLen );
532 while ( ( err == ERROR_MORE_DATA ) && ( i < 100 ) );