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.
17 Change History (most recent first):
19 $Log: SecondPage.cpp,v $
20 Revision 1.7 2006/08/14 23:25:28 cheshire
21 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
23 Revision 1.6 2005/10/05 20:46:50 herscher
24 <rdar://problem/4192011> Move Wide-Area preferences to another part of the registry so they don't removed during an update-install.
26 Revision 1.5 2005/04/05 04:15:46 shersche
27 RegQueryString was returning uninitialized strings if the registry key couldn't be found, so always initialize strings before checking the registry key.
29 Revision 1.4 2005/04/05 03:52:14 shersche
30 <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.
32 Revision 1.3 2005/03/03 19:55:22 shersche
33 <rdar://problem/4034481> ControlPanel source code isn't saving CVS log info
38 #include "SecondPage.h"
41 #include "ConfigPropertySheet.h"
42 #include "SharedSecret.h"
44 #include <WinServices.h>
46 IMPLEMENT_DYNCREATE(CSecondPage
, CPropertyPage
)
49 //---------------------------------------------------------------------------------------------------------------------------
50 // CSecondPage::CSecondPage
51 //---------------------------------------------------------------------------------------------------------------------------
53 CSecondPage::CSecondPage()
55 CPropertyPage(CSecondPage::IDD
),
58 //{{AFX_DATA_INIT(CSecondPage)
63 err
= RegCreateKey( HKEY_LOCAL_MACHINE
, kServiceParametersNode L
"\\DynDNS\\Setup\\" kServiceDynDNSRegistrationDomains
, &m_setupKey
);
68 //---------------------------------------------------------------------------------------------------------------------------
69 // CSecondPage::~CSecondPage
70 //---------------------------------------------------------------------------------------------------------------------------
72 CSecondPage::~CSecondPage()
76 RegCloseKey( m_setupKey
);
82 //---------------------------------------------------------------------------------------------------------------------------
83 // CSecondPage::DoDataExchange
84 //---------------------------------------------------------------------------------------------------------------------------
86 void CSecondPage::DoDataExchange(CDataExchange
* pDX
)
88 CPropertyPage::DoDataExchange(pDX
);
89 //{{AFX_DATA_MAP(CSecondPage)
91 DDX_Control(pDX
, IDC_CHECK1
, m_advertiseServicesButton
);
92 DDX_Control(pDX
, IDC_BUTTON1
, m_sharedSecretButton
);
93 DDX_Control(pDX
, IDC_COMBO2
, m_regDomainsBox
);
96 BEGIN_MESSAGE_MAP(CSecondPage
, CPropertyPage
)
97 //{{AFX_MSG_MAP(CSecondPage)
99 ON_BN_CLICKED(IDC_BUTTON1
, OnBnClickedSharedSecret
)
100 ON_BN_CLICKED(IDC_CHECK1
, OnBnClickedAdvertise
)
101 ON_CBN_SELCHANGE(IDC_COMBO1
, OnCbnSelChange
)
102 ON_CBN_EDITCHANGE(IDC_COMBO1
, OnCbnEditChange
)
103 ON_CBN_EDITCHANGE(IDC_COMBO2
, OnCbnEditChange
)
104 ON_CBN_SELCHANGE(IDC_COMBO2
, OnCbnSelChange
)
109 //---------------------------------------------------------------------------------------------------------------------------
110 // CSecondPage::SetModified
111 //---------------------------------------------------------------------------------------------------------------------------
113 void CSecondPage::SetModified( BOOL bChanged
)
115 m_modified
= bChanged
;
117 CPropertyPage::SetModified( bChanged
);
121 //---------------------------------------------------------------------------------------------------------------------------
122 // CSecondPage::OnSetActive
123 //---------------------------------------------------------------------------------------------------------------------------
126 CSecondPage::OnSetActive()
128 CConfigPropertySheet
* psheet
;
132 BOOL b
= CPropertyPage::OnSetActive();
134 psheet
= reinterpret_cast<CConfigPropertySheet
*>(GetParent());
135 require_quiet( psheet
, exit
);
139 // Clear out what's there
141 EmptyComboBox( m_regDomainsBox
);
143 // Now populate the registration domain box
145 err
= Populate( m_regDomainsBox
, m_setupKey
, psheet
->m_regDomains
);
148 dwSize
= sizeof( DWORD
);
149 err
= RegQueryValueEx( m_setupKey
, L
"Enabled", NULL
, NULL
, (LPBYTE
) &enabled
, &dwSize
);
150 m_advertiseServicesButton
.SetCheck( ( !err
&& enabled
) ? BST_CHECKED
: BST_UNCHECKED
);
151 m_regDomainsBox
.EnableWindow( ( !err
&& enabled
) );
152 m_sharedSecretButton
.EnableWindow( (!err
&& enabled
) );
160 //---------------------------------------------------------------------------------------------------------------------------
162 //---------------------------------------------------------------------------------------------------------------------------
175 //---------------------------------------------------------------------------------------------------------------------------
176 // CSecondPage::Commit
177 //---------------------------------------------------------------------------------------------------------------------------
180 CSecondPage::Commit()
184 if ( m_setupKey
!= NULL
)
186 err
= Commit( m_regDomainsBox
, m_setupKey
, m_advertiseServicesButton
.GetCheck() == BST_CHECKED
);
192 //---------------------------------------------------------------------------------------------------------------------------
193 // CSecondPage::Commit
194 //---------------------------------------------------------------------------------------------------------------------------
197 CSecondPage::Commit( CComboBox
& box
, HKEY key
, DWORD enabled
)
200 OSStatus err
= kNoErr
;
204 box
.GetWindowText( selected
);
206 // If we haven't seen this string before, add the string to the box and
209 if ( ( selected
.GetLength() > 0 ) && ( box
.FindStringExact( -1, selected
) == CB_ERR
) )
213 box
.AddString( selected
);
215 err
= RegQueryString( key
, L
"UserDefined", string
);
218 if ( string
.GetLength() )
220 string
+= L
"," + selected
;
227 err
= RegSetValueEx( key
, L
"UserDefined", 0, REG_SZ
, (LPBYTE
) (LPCTSTR
) string
, ( string
.GetLength() + 1) * sizeof( TCHAR
) );
231 // Save selected text in registry. This will trigger mDNSResponder to setup
232 // DynDNS config again
234 err
= RegSetValueEx( key
, L
"", 0, REG_SZ
, (LPBYTE
) (LPCTSTR
) selected
, ( selected
.GetLength() + 1 ) * sizeof( TCHAR
) );
237 err
= RegSetValueEx( key
, L
"Enabled", 0, REG_DWORD
, (LPBYTE
) &enabled
, sizeof( DWORD
) );
244 //---------------------------------------------------------------------------------------------------------------------------
245 // CSecondPage::OnBnClickedSharedSecret
246 //---------------------------------------------------------------------------------------------------------------------------
248 void CSecondPage::OnBnClickedSharedSecret()
252 m_regDomainsBox
.GetWindowText( name
);
258 if ( dlg
.DoModal() == IDOK
)
261 DWORD dwSize
= sizeof( DWORD
);
266 // We have now updated the secret, however the system service
267 // doesn't know about it yet. So we're going to update the
268 // registry with a dummy value which will cause the system
269 // service to re-initialize it's DynDNS setup
272 RegQueryValueEx( m_setupKey
, L
"Wakeup", NULL
, NULL
, (LPBYTE
) &wakeup
, &dwSize
);
276 err
= RegSetValueEx( m_setupKey
, L
"Wakeup", 0, REG_DWORD
, (LPBYTE
) &wakeup
, sizeof( DWORD
) );
277 require_noerr( err
, exit
);
286 //---------------------------------------------------------------------------------------------------------------------------
287 // CSecondPage::OnBnClickedAdvertise
288 //---------------------------------------------------------------------------------------------------------------------------
290 void CSecondPage::OnBnClickedAdvertise()
294 state
= m_advertiseServicesButton
.GetCheck();
296 m_regDomainsBox
.EnableWindow( state
);
297 m_sharedSecretButton
.EnableWindow( state
);
303 //---------------------------------------------------------------------------------------------------------------------------
304 // CSecondPage::OnCbnSelChange
305 //---------------------------------------------------------------------------------------------------------------------------
307 void CSecondPage::OnCbnSelChange()
313 //---------------------------------------------------------------------------------------------------------------------------
314 // CSecondPage::OnCbnEditChange
315 //---------------------------------------------------------------------------------------------------------------------------
317 void CSecondPage::OnCbnEditChange()
324 //---------------------------------------------------------------------------------------------------------------------------
325 // CSecondPage::OnAddRegistrationDomain
326 //---------------------------------------------------------------------------------------------------------------------------
329 CSecondPage::OnAddRegistrationDomain( CString
& domain
)
331 int index
= m_regDomainsBox
.FindStringExact( -1, domain
);
333 if ( index
== CB_ERR
)
335 m_regDomainsBox
.AddString( domain
);
340 //---------------------------------------------------------------------------------------------------------------------------
341 // CSecondPage::OnRemoveRegistrationDomain
342 //---------------------------------------------------------------------------------------------------------------------------
345 CSecondPage::OnRemoveRegistrationDomain( CString
& domain
)
347 int index
= m_regDomainsBox
.FindStringExact( -1, domain
);
349 if ( index
!= CB_ERR
)
351 m_regDomainsBox
.DeleteString( index
);
356 //---------------------------------------------------------------------------------------------------------------------------
357 // CSecondPage::EmptyComboBox
358 //---------------------------------------------------------------------------------------------------------------------------
361 CSecondPage::EmptyComboBox( CComboBox
& box
)
363 while ( box
.GetCount() > 0 )
365 box
.DeleteString( 0 );
370 //---------------------------------------------------------------------------------------------------------------------------
371 // CSecondPage::Populate
372 //---------------------------------------------------------------------------------------------------------------------------
375 CSecondPage::Populate( CComboBox
& box
, HKEY key
, StringList
& l
)
377 TCHAR rawString
[kDNSServiceMaxDomainName
+ 1];
382 err
= RegQueryString( key
, L
"UserDefined", string
);
384 if ( !err
&& string
.GetLength() )
392 tok
= string
.SpanExcluding( L
"," );
394 box
.AddString( tok
);
398 // Get rid of that string and comma
400 string
= string
.Right( string
.GetLength() - tok
.GetLength() - 1 );
409 StringList::iterator it
;
411 for ( it
= l
.begin(); it
!= l
.end(); it
++ )
413 if ( box
.FindStringExact( -1, *it
) == CB_ERR
)
415 box
.AddString( *it
);
419 // Now look to see if there is a selected string, and if so,
424 rawStringLen
= sizeof( rawString
);
426 err
= RegQueryValueEx( key
, L
"", 0, NULL
, (LPBYTE
) rawString
, &rawStringLen
);
430 if ( !err
&& ( string
.GetLength() != 0 ) )
434 if ( box
.SelectString( -1, string
) == CB_ERR
)
438 box
.AddString( string
);
441 box
.SelectString( -1, string
);
448 //---------------------------------------------------------------------------------------------------------------------------
449 // CSecondPage::CreateKey
450 //---------------------------------------------------------------------------------------------------------------------------
453 CSecondPage::CreateKey( CString
& name
, DWORD enabled
)
458 err
= RegCreateKey( HKEY_LOCAL_MACHINE
, (LPCTSTR
) name
, &key
);
459 require_noerr( err
, exit
);
461 err
= RegSetValueEx( key
, L
"Enabled", 0, REG_DWORD
, (LPBYTE
) &enabled
, sizeof( DWORD
) );
475 //---------------------------------------------------------------------------------------------------------------------------
476 // CSecondPage::RegQueryString
477 //---------------------------------------------------------------------------------------------------------------------------
480 CSecondPage::RegQueryString( HKEY key
, CString valueName
, CString
& value
)
498 string
= (TCHAR
*) malloc( stringLen
);
499 require_action( string
, exit
, err
= kUnknownErr
);
502 err
= RegQueryValueEx( key
, valueName
, 0, NULL
, (LPBYTE
) string
, &stringLen
);
506 while ( ( err
== ERROR_MORE_DATA
) && ( i
< 100 ) );