]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSWindows/Applications/RendezvousBrowser/WindowsCE/Sources/BrowserDialog.cpp
de86ab868acdb5eca8f9a63bdcdf879dbc579953
[apple/mdnsresponder.git] / mDNSWindows / Applications / RendezvousBrowser / WindowsCE / Sources / BrowserDialog.cpp
1 /*
2 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
7 *
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * file.
14 *
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
22 *
23 * @APPLE_LICENSE_HEADER_END@
24
25 Change History (most recent first):
26
27 $Log: BrowserDialog.cpp,v $
28 Revision 1.1 2003/08/21 02:16:10 bradley
29 Rendezvous Browser for HTTP services for Windows CE/PocketPC.
30
31 */
32
33 #include "stdafx.h"
34
35 #include "Application.h"
36
37 #include "DNSServices.h"
38
39 #include "BrowserDialog.h"
40
41 #ifdef _DEBUG
42 #define new DEBUG_NEW
43 #undef THIS_FILE
44 static char THIS_FILE[] = __FILE__;
45 #endif
46
47 //===========================================================================================================================
48 // Message Map
49 //===========================================================================================================================
50
51 BEGIN_MESSAGE_MAP(BrowserDialog, CDialog)
52 //{{AFX_MSG_MAP(BrowserDialog)
53 //}}AFX_MSG_MAP
54 END_MESSAGE_MAP()
55
56 static DWORD UTF8StringToStringObject( const char *inUTF8, CString &inObject );
57
58 //===========================================================================================================================
59 // BrowserDialog
60 //===========================================================================================================================
61
62 BrowserDialog::BrowserDialog( CWnd *inParent )
63 : CDialog( BrowserDialog::IDD, inParent )
64 {
65 //{{AFX_DATA_INIT(BrowserDialog)
66 // NOTE: the ClassWizard will add member initialization here
67 //}}AFX_DATA_INIT
68
69 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32.
70
71 mIcon = AfxGetApp()->LoadIcon( IDR_MAINFRAME );
72 }
73
74 //===========================================================================================================================
75 // DoDataExchange
76 //===========================================================================================================================
77
78 void BrowserDialog::DoDataExchange( CDataExchange *pDX )
79 {
80 CDialog::DoDataExchange(pDX);
81 //{{AFX_DATA_MAP(BrowserDialog)
82 DDX_Control(pDX, IDC_BROWSE_LIST, mBrowserList);
83 //}}AFX_DATA_MAP
84 }
85
86 //===========================================================================================================================
87 // OnInitDialog
88 //===========================================================================================================================
89
90 BOOL BrowserDialog::OnInitDialog()
91 {
92 CString s;
93
94 CDialog::OnInitDialog();
95
96 // Set the icon for this dialog. The framework does this automatically when the application's main window is not a dialog.
97
98 SetIcon( mIcon, TRUE ); // Set big icon
99 SetIcon( mIcon, FALSE ); // Set small icon
100
101 CenterWindow( GetDesktopWindow() );
102
103 // Set up the list.
104
105 CRect rect;
106
107 s.LoadString( IDS_BROWSER_LIST_COLUMN_NAME );
108 mBrowserList.GetWindowRect( rect );
109 mBrowserList.InsertColumn( 0, s, LVCFMT_LEFT, rect.Width() - 8 );
110
111 // Start browsing for services.
112
113 DNSStatus err;
114
115 err = DNSBrowserCreate( 0, BrowserCallBack, this, &mBrowser );
116 if( err )
117 {
118 AfxMessageBox( IDP_SOCKETS_INIT_FAILED );
119 goto exit;
120 }
121
122 err = DNSBrowserStartServiceSearch( mBrowser, 0, "_http._tcp", NULL );
123 if( err )
124 {
125 AfxMessageBox( IDP_SOCKETS_INIT_FAILED );
126 goto exit;
127 }
128
129 exit:
130 return( TRUE );
131 }
132
133 //===========================================================================================================================
134 // BrowserCallBack [static]
135 //===========================================================================================================================
136
137 void
138 BrowserDialog::BrowserCallBack(
139 void * inContext,
140 DNSBrowserRef inRef,
141 DNSStatus inStatusCode,
142 const DNSBrowserEvent * inEvent )
143 {
144 BrowserDialog * dialog;
145
146 DNS_UNUSED( inStatusCode );
147 dialog = reinterpret_cast < BrowserDialog * > ( inContext );
148
149 switch( inEvent->type )
150 {
151 case kDNSBrowserEventTypeAddService:
152 dialog->BrowserAddService( inEvent->data.addService.name );
153 break;
154
155 case kDNSBrowserEventTypeRemoveService:
156 dialog->BrowserRemoveService( inEvent->data.removeService.name );
157 break;
158
159 default:
160 break;
161 }
162 }
163
164 //===========================================================================================================================
165 // BrowserAddService
166 //===========================================================================================================================
167
168 void BrowserDialog::BrowserAddService( const char *inName )
169 {
170 BrowserEntry newEntry;
171 INT_PTR n;
172 INT_PTR i;
173
174 UTF8StringToStringObject( inName, newEntry.name );
175
176 n = mBrowserEntries.GetSize();
177 for( i = 0; i < n; ++i )
178 {
179 BrowserEntry & entry = mBrowserEntries.ElementAt( i );
180
181 if( entry.name.CompareNoCase( newEntry.name ) == 0 )
182 {
183 break;
184 }
185 }
186 if( i >= n )
187 {
188 mBrowserEntries.Add( newEntry );
189 mBrowserList.InsertItem( i, newEntry.name );
190 }
191 }
192
193 //===========================================================================================================================
194 // BrowserRemoveService
195 //===========================================================================================================================
196
197 void BrowserDialog::BrowserRemoveService( const char *inName )
198 {
199 BrowserEntry newEntry;
200 INT_PTR n;
201 INT_PTR i;
202
203 UTF8StringToStringObject( inName, newEntry.name );
204
205 n = mBrowserEntries.GetSize();
206 for( i = 0; i < n; ++i )
207 {
208 BrowserEntry & entry = mBrowserEntries.ElementAt( i );
209
210 if( entry.name.CompareNoCase( newEntry.name ) == 0 )
211 {
212 break;
213 }
214 }
215 if( i < n )
216 {
217 mBrowserEntries.RemoveAt( i );
218 mBrowserList.DeleteItem( i );
219 }
220 }
221
222 #if 0
223 #pragma mark -
224 #endif
225
226 //===========================================================================================================================
227 // UTF8StringToStringObject
228 //===========================================================================================================================
229
230 static DWORD UTF8StringToStringObject( const char *inUTF8, CString &inObject )
231 {
232 DWORD err;
233 int n;
234 wchar_t * unicode;
235
236 unicode = NULL;
237
238 n = MultiByteToWideChar( CP_UTF8, 0, inUTF8, -1, NULL, 0 );
239 if( n > 0 )
240 {
241 unicode = (wchar_t *) malloc( (size_t)( n * sizeof( wchar_t ) ) );
242 if( !unicode ) { err = ERROR_INSUFFICIENT_BUFFER; goto exit; };
243
244 n = MultiByteToWideChar( CP_UTF8, 0, inUTF8, -1, unicode, n );
245 inObject = unicode;
246 }
247 else
248 {
249 inObject = "";
250 }
251 err = 0;
252
253 exit:
254 if( unicode )
255 {
256 free( unicode );
257 }
258 return( err );
259 }