2 * Copyright (c) 2002-2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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.
21 * @APPLE_LICENSE_HEADER_END@
23 Change History (most recent first):
25 $Log: SecondPage.cpp,v $
26 Revision 1.6 2005/10/05 20:46:50 herscher
27 <rdar://problem/4192011> Move Wide-Area preferences to another part of the registry so they don't removed during an update-install.
29 Revision 1.5 2005/04/05 04:15:46 shersche
30 RegQueryString was returning uninitialized strings if the registry key couldn't be found, so always initialize strings before checking the registry key.
32 Revision 1.4 2005/04/05 03:52:14 shersche
33 <rdar://problem/4066485> Registering with shared secret key doesn't work. Additionally, mDNSResponder wasn't dynamically re-reading it's DynDNS setup after setting a shared secret key.
35 Revision 1.3 2005/03/03 19:55:22 shersche
36 <rdar://problem/4034481> ControlPanel source code isn't saving CVS log info
41 #include "SecondPage.h"
44 #include "ConfigPropertySheet.h"
45 #include "SharedSecret.h"
47 #include <WinServices.h>
49 IMPLEMENT_DYNCREATE(CSecondPage
, CPropertyPage
)
52 //---------------------------------------------------------------------------------------------------------------------------
53 // CSecondPage::CSecondPage
54 //---------------------------------------------------------------------------------------------------------------------------
56 CSecondPage::CSecondPage()
58 CPropertyPage(CSecondPage::IDD
),
61 //{{AFX_DATA_INIT(CSecondPage)
66 err
= RegCreateKey( HKEY_LOCAL_MACHINE
, kServiceParametersNode L
"\\DynDNS\\Setup\\" kServiceDynDNSRegistrationDomains
, &m_setupKey
);
71 //---------------------------------------------------------------------------------------------------------------------------
72 // CSecondPage::~CSecondPage
73 //---------------------------------------------------------------------------------------------------------------------------
75 CSecondPage::~CSecondPage()
79 RegCloseKey( m_setupKey
);
85 //---------------------------------------------------------------------------------------------------------------------------
86 // CSecondPage::DoDataExchange
87 //---------------------------------------------------------------------------------------------------------------------------
89 void CSecondPage::DoDataExchange(CDataExchange
* pDX
)
91 CPropertyPage::DoDataExchange(pDX
);
92 //{{AFX_DATA_MAP(CSecondPage)
94 DDX_Control(pDX
, IDC_CHECK1
, m_advertiseServicesButton
);
95 DDX_Control(pDX
, IDC_BUTTON1
, m_sharedSecretButton
);
96 DDX_Control(pDX
, IDC_COMBO2
, m_regDomainsBox
);
99 BEGIN_MESSAGE_MAP(CSecondPage
, CPropertyPage
)
100 //{{AFX_MSG_MAP(CSecondPage)
102 ON_BN_CLICKED(IDC_BUTTON1
, OnBnClickedSharedSecret
)
103 ON_BN_CLICKED(IDC_CHECK1
, OnBnClickedAdvertise
)
104 ON_CBN_SELCHANGE(IDC_COMBO1
, OnCbnSelChange
)
105 ON_CBN_EDITCHANGE(IDC_COMBO1
, OnCbnEditChange
)
106 ON_CBN_EDITCHANGE(IDC_COMBO2
, OnCbnEditChange
)
107 ON_CBN_SELCHANGE(IDC_COMBO2
, OnCbnSelChange
)
112 //---------------------------------------------------------------------------------------------------------------------------
113 // CSecondPage::SetModified
114 //---------------------------------------------------------------------------------------------------------------------------
116 void CSecondPage::SetModified( BOOL bChanged
)
118 m_modified
= bChanged
;
120 CPropertyPage::SetModified( bChanged
);
124 //---------------------------------------------------------------------------------------------------------------------------
125 // CSecondPage::OnSetActive
126 //---------------------------------------------------------------------------------------------------------------------------
129 CSecondPage::OnSetActive()
131 CConfigPropertySheet
* psheet
;
135 BOOL b
= CPropertyPage::OnSetActive();
137 psheet
= reinterpret_cast<CConfigPropertySheet
*>(GetParent());
138 require_quiet( psheet
, exit
);
142 // Clear out what's there
144 EmptyComboBox( m_regDomainsBox
);
146 // Now populate the registration domain box
148 err
= Populate( m_regDomainsBox
, m_setupKey
, psheet
->m_regDomains
);
151 dwSize
= sizeof( DWORD
);
152 err
= RegQueryValueEx( m_setupKey
, L
"Enabled", NULL
, NULL
, (LPBYTE
) &enabled
, &dwSize
);
153 m_advertiseServicesButton
.SetCheck( ( !err
&& enabled
) ? BST_CHECKED
: BST_UNCHECKED
);
154 m_regDomainsBox
.EnableWindow( ( !err
&& enabled
) );
155 m_sharedSecretButton
.EnableWindow( (!err
&& enabled
) );
163 //---------------------------------------------------------------------------------------------------------------------------
165 //---------------------------------------------------------------------------------------------------------------------------
178 //---------------------------------------------------------------------------------------------------------------------------
179 // CSecondPage::Commit
180 //---------------------------------------------------------------------------------------------------------------------------
183 CSecondPage::Commit()
187 if ( m_setupKey
!= NULL
)
189 err
= Commit( m_regDomainsBox
, m_setupKey
, m_advertiseServicesButton
.GetCheck() == BST_CHECKED
);
195 //---------------------------------------------------------------------------------------------------------------------------
196 // CSecondPage::Commit
197 //---------------------------------------------------------------------------------------------------------------------------
200 CSecondPage::Commit( CComboBox
& box
, HKEY key
, DWORD enabled
)
203 OSStatus err
= kNoErr
;
207 box
.GetWindowText( selected
);
209 // If we haven't seen this string before, add the string to the box and
212 if ( ( selected
.GetLength() > 0 ) && ( box
.FindStringExact( -1, selected
) == CB_ERR
) )
216 box
.AddString( selected
);
218 err
= RegQueryString( key
, L
"UserDefined", string
);
221 if ( string
.GetLength() )
223 string
+= L
"," + selected
;
230 err
= RegSetValueEx( key
, L
"UserDefined", 0, REG_SZ
, (LPBYTE
) (LPCTSTR
) string
, ( string
.GetLength() + 1) * sizeof( TCHAR
) );
234 // Save selected text in registry. This will trigger mDNSResponder to setup
235 // DynDNS config again
237 err
= RegSetValueEx( key
, L
"", 0, REG_SZ
, (LPBYTE
) (LPCTSTR
) selected
, ( selected
.GetLength() + 1 ) * sizeof( TCHAR
) );
240 err
= RegSetValueEx( key
, L
"Enabled", 0, REG_DWORD
, (LPBYTE
) &enabled
, sizeof( DWORD
) );
247 //---------------------------------------------------------------------------------------------------------------------------
248 // CSecondPage::OnBnClickedSharedSecret
249 //---------------------------------------------------------------------------------------------------------------------------
251 void CSecondPage::OnBnClickedSharedSecret()
255 m_regDomainsBox
.GetWindowText( name
);
261 if ( dlg
.DoModal() == IDOK
)
264 DWORD dwSize
= sizeof( DWORD
);
269 // We have now updated the secret, however the system service
270 // doesn't know about it yet. So we're going to update the
271 // registry with a dummy value which will cause the system
272 // service to re-initialize it's DynDNS setup
275 RegQueryValueEx( m_setupKey
, L
"Wakeup", NULL
, NULL
, (LPBYTE
) &wakeup
, &dwSize
);
279 err
= RegSetValueEx( m_setupKey
, L
"Wakeup", 0, REG_DWORD
, (LPBYTE
) &wakeup
, sizeof( DWORD
) );
280 require_noerr( err
, exit
);
289 //---------------------------------------------------------------------------------------------------------------------------
290 // CSecondPage::OnBnClickedAdvertise
291 //---------------------------------------------------------------------------------------------------------------------------
293 void CSecondPage::OnBnClickedAdvertise()
297 state
= m_advertiseServicesButton
.GetCheck();
299 m_regDomainsBox
.EnableWindow( state
);
300 m_sharedSecretButton
.EnableWindow( state
);
306 //---------------------------------------------------------------------------------------------------------------------------
307 // CSecondPage::OnCbnSelChange
308 //---------------------------------------------------------------------------------------------------------------------------
310 void CSecondPage::OnCbnSelChange()
316 //---------------------------------------------------------------------------------------------------------------------------
317 // CSecondPage::OnCbnEditChange
318 //---------------------------------------------------------------------------------------------------------------------------
320 void CSecondPage::OnCbnEditChange()
327 //---------------------------------------------------------------------------------------------------------------------------
328 // CSecondPage::OnAddRegistrationDomain
329 //---------------------------------------------------------------------------------------------------------------------------
332 CSecondPage::OnAddRegistrationDomain( CString
& domain
)
334 int index
= m_regDomainsBox
.FindStringExact( -1, domain
);
336 if ( index
== CB_ERR
)
338 m_regDomainsBox
.AddString( domain
);
343 //---------------------------------------------------------------------------------------------------------------------------
344 // CSecondPage::OnRemoveRegistrationDomain
345 //---------------------------------------------------------------------------------------------------------------------------
348 CSecondPage::OnRemoveRegistrationDomain( CString
& domain
)
350 int index
= m_regDomainsBox
.FindStringExact( -1, domain
);
352 if ( index
!= CB_ERR
)
354 m_regDomainsBox
.DeleteString( index
);
359 //---------------------------------------------------------------------------------------------------------------------------
360 // CSecondPage::EmptyComboBox
361 //---------------------------------------------------------------------------------------------------------------------------
364 CSecondPage::EmptyComboBox( CComboBox
& box
)
366 while ( box
.GetCount() > 0 )
368 box
.DeleteString( 0 );
373 //---------------------------------------------------------------------------------------------------------------------------
374 // CSecondPage::Populate
375 //---------------------------------------------------------------------------------------------------------------------------
378 CSecondPage::Populate( CComboBox
& box
, HKEY key
, StringList
& l
)
380 TCHAR rawString
[kDNSServiceMaxDomainName
+ 1];
385 err
= RegQueryString( key
, L
"UserDefined", string
);
387 if ( !err
&& string
.GetLength() )
395 tok
= string
.SpanExcluding( L
"," );
397 box
.AddString( tok
);
401 // Get rid of that string and comma
403 string
= string
.Right( string
.GetLength() - tok
.GetLength() - 1 );
412 StringList::iterator it
;
414 for ( it
= l
.begin(); it
!= l
.end(); it
++ )
416 if ( box
.FindStringExact( -1, *it
) == CB_ERR
)
418 box
.AddString( *it
);
422 // Now look to see if there is a selected string, and if so,
427 rawStringLen
= sizeof( rawString
);
429 err
= RegQueryValueEx( key
, L
"", 0, NULL
, (LPBYTE
) rawString
, &rawStringLen
);
433 if ( !err
&& ( string
.GetLength() != 0 ) )
437 if ( box
.SelectString( -1, string
) == CB_ERR
)
441 box
.AddString( string
);
444 box
.SelectString( -1, string
);
451 //---------------------------------------------------------------------------------------------------------------------------
452 // CSecondPage::CreateKey
453 //---------------------------------------------------------------------------------------------------------------------------
456 CSecondPage::CreateKey( CString
& name
, DWORD enabled
)
461 err
= RegCreateKey( HKEY_LOCAL_MACHINE
, (LPCTSTR
) name
, &key
);
462 require_noerr( err
, exit
);
464 err
= RegSetValueEx( key
, L
"Enabled", 0, REG_DWORD
, (LPBYTE
) &enabled
, sizeof( DWORD
) );
478 //---------------------------------------------------------------------------------------------------------------------------
479 // CSecondPage::RegQueryString
480 //---------------------------------------------------------------------------------------------------------------------------
483 CSecondPage::RegQueryString( HKEY key
, CString valueName
, CString
& value
)
501 string
= (TCHAR
*) malloc( stringLen
);
502 require_action( string
, exit
, err
= kUnknownErr
);
505 err
= RegQueryValueEx( key
, valueName
, 0, NULL
, (LPBYTE
) string
, &stringLen
);
509 while ( ( err
== ERROR_MORE_DATA
) && ( i
< 100 ) );