]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSWindows/Applications/DNSServiceBrowser/WindowsCE/Sources/BrowserDialog.cpp
mDNSResponder-58.8.1.tar.gz
[apple/mdnsresponder.git] / mDNSWindows / Applications / DNSServiceBrowser / WindowsCE / Sources / BrowserDialog.cpp
1 /*
2 * Copyright (c) 2002-2003 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: BrowserDialog.cpp,v $
26 Revision 1.1 2003/08/21 02:16:10 bradley
27 Rendezvous Browser for HTTP services for Windows CE/PocketPC.
28
29 */
30
31 #include "stdafx.h"
32
33 #include "Application.h"
34
35 #include "DNSServices.h"
36
37 #include "BrowserDialog.h"
38
39 #ifdef _DEBUG
40 #define new DEBUG_NEW
41 #undef THIS_FILE
42 static char THIS_FILE[] = __FILE__;
43 #endif
44
45 //===========================================================================================================================
46 // Message Map
47 //===========================================================================================================================
48
49 BEGIN_MESSAGE_MAP(BrowserDialog, CDialog)
50 //{{AFX_MSG_MAP(BrowserDialog)
51 //}}AFX_MSG_MAP
52 END_MESSAGE_MAP()
53
54 static DWORD UTF8StringToStringObject( const char *inUTF8, CString &inObject );
55
56 //===========================================================================================================================
57 // BrowserDialog
58 //===========================================================================================================================
59
60 BrowserDialog::BrowserDialog( CWnd *inParent )
61 : CDialog( BrowserDialog::IDD, inParent )
62 {
63 //{{AFX_DATA_INIT(BrowserDialog)
64 // NOTE: the ClassWizard will add member initialization here
65 //}}AFX_DATA_INIT
66
67 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32.
68
69 mIcon = AfxGetApp()->LoadIcon( IDR_MAINFRAME );
70 }
71
72 //===========================================================================================================================
73 // DoDataExchange
74 //===========================================================================================================================
75
76 void BrowserDialog::DoDataExchange( CDataExchange *pDX )
77 {
78 CDialog::DoDataExchange(pDX);
79 //{{AFX_DATA_MAP(BrowserDialog)
80 DDX_Control(pDX, IDC_BROWSE_LIST, mBrowserList);
81 //}}AFX_DATA_MAP
82 }
83
84 //===========================================================================================================================
85 // OnInitDialog
86 //===========================================================================================================================
87
88 BOOL BrowserDialog::OnInitDialog()
89 {
90 CString s;
91
92 CDialog::OnInitDialog();
93
94 // Set the icon for this dialog. The framework does this automatically when the application's main window is not a dialog.
95
96 SetIcon( mIcon, TRUE ); // Set big icon
97 SetIcon( mIcon, FALSE ); // Set small icon
98
99 CenterWindow( GetDesktopWindow() );
100
101 // Set up the list.
102
103 CRect rect;
104
105 s.LoadString( IDS_BROWSER_LIST_COLUMN_NAME );
106 mBrowserList.GetWindowRect( rect );
107 mBrowserList.InsertColumn( 0, s, LVCFMT_LEFT, rect.Width() - 8 );
108
109 // Start browsing for services.
110
111 DNSStatus err;
112
113 err = DNSBrowserCreate( 0, BrowserCallBack, this, &mBrowser );
114 if( err )
115 {
116 AfxMessageBox( IDP_SOCKETS_INIT_FAILED );
117 goto exit;
118 }
119
120 err = DNSBrowserStartServiceSearch( mBrowser, 0, "_http._tcp", NULL );
121 if( err )
122 {
123 AfxMessageBox( IDP_SOCKETS_INIT_FAILED );
124 goto exit;
125 }
126
127 exit:
128 return( TRUE );
129 }
130
131 //===========================================================================================================================
132 // BrowserCallBack [static]
133 //===========================================================================================================================
134
135 void
136 BrowserDialog::BrowserCallBack(
137 void * inContext,
138 DNSBrowserRef inRef,
139 DNSStatus inStatusCode,
140 const DNSBrowserEvent * inEvent )
141 {
142 BrowserDialog * dialog;
143
144 DNS_UNUSED( inStatusCode );
145 dialog = reinterpret_cast < BrowserDialog * > ( inContext );
146
147 switch( inEvent->type )
148 {
149 case kDNSBrowserEventTypeAddService:
150 dialog->BrowserAddService( inEvent->data.addService.name );
151 break;
152
153 case kDNSBrowserEventTypeRemoveService:
154 dialog->BrowserRemoveService( inEvent->data.removeService.name );
155 break;
156
157 default:
158 break;
159 }
160 }
161
162 //===========================================================================================================================
163 // BrowserAddService
164 //===========================================================================================================================
165
166 void BrowserDialog::BrowserAddService( const char *inName )
167 {
168 BrowserEntry newEntry;
169 INT_PTR n;
170 INT_PTR i;
171
172 UTF8StringToStringObject( inName, newEntry.name );
173
174 n = mBrowserEntries.GetSize();
175 for( i = 0; i < n; ++i )
176 {
177 BrowserEntry & entry = mBrowserEntries.ElementAt( i );
178
179 if( entry.name.CompareNoCase( newEntry.name ) == 0 )
180 {
181 break;
182 }
183 }
184 if( i >= n )
185 {
186 mBrowserEntries.Add( newEntry );
187 mBrowserList.InsertItem( i, newEntry.name );
188 }
189 }
190
191 //===========================================================================================================================
192 // BrowserRemoveService
193 //===========================================================================================================================
194
195 void BrowserDialog::BrowserRemoveService( const char *inName )
196 {
197 BrowserEntry newEntry;
198 INT_PTR n;
199 INT_PTR i;
200
201 UTF8StringToStringObject( inName, newEntry.name );
202
203 n = mBrowserEntries.GetSize();
204 for( i = 0; i < n; ++i )
205 {
206 BrowserEntry & entry = mBrowserEntries.ElementAt( i );
207
208 if( entry.name.CompareNoCase( newEntry.name ) == 0 )
209 {
210 break;
211 }
212 }
213 if( i < n )
214 {
215 mBrowserEntries.RemoveAt( i );
216 mBrowserList.DeleteItem( i );
217 }
218 }
219
220 #if 0
221 #pragma mark -
222 #endif
223
224 //===========================================================================================================================
225 // UTF8StringToStringObject
226 //===========================================================================================================================
227
228 static DWORD UTF8StringToStringObject( const char *inUTF8, CString &inObject )
229 {
230 DWORD err;
231 int n;
232 wchar_t * unicode;
233
234 unicode = NULL;
235
236 n = MultiByteToWideChar( CP_UTF8, 0, inUTF8, -1, NULL, 0 );
237 if( n > 0 )
238 {
239 unicode = (wchar_t *) malloc( (size_t)( n * sizeof( wchar_t ) ) );
240 if( !unicode ) { err = ERROR_INSUFFICIENT_BUFFER; goto exit; };
241
242 n = MultiByteToWideChar( CP_UTF8, 0, inUTF8, -1, unicode, n );
243 inObject = unicode;
244 }
245 else
246 {
247 inObject = "";
248 }
249 err = 0;
250
251 exit:
252 if( unicode )
253 {
254 free( unicode );
255 }
256 return( err );
257 }