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: BrowserDialog.cpp,v $
26 Revision 1.2 2004/07/13 21:24:27 rpantos
27 Fix for <rdar://problem/3701120>.
29 Revision 1.1 2004/06/18 04:04:37 rpantos
32 Revision 1.5 2004/01/30 02:56:33 bradley
33 Updated to support full Unicode display. Added support for all services on www.dns-sd.org.
35 Revision 1.4 2003/10/16 09:21:56 bradley
36 Ignore non-IPv4 resolves until mDNS on Windows supports IPv6.
38 Revision 1.3 2003/10/14 03:28:50 bradley
39 Insert services in sorted order to make them easier to find. Defer service adds/removes to the main
40 thread to avoid potential problems with multi-threaded MFC message map access. Added some asserts.
42 Revision 1.2 2003/10/10 03:43:34 bradley
43 Added support for launching a web browser to go to the browsed web site on a single-tap.
45 Revision 1.1 2003/08/21 02:16:10 bradley
46 DNSServiceBrowser for HTTP services for Windows CE/PocketPC.
52 #include "Application.h"
54 #include "DNSServices.h"
56 #include "BrowserDialog.h"
61 static char THIS_FILE
[] = __FILE__
;
64 //===========================================================================================================================
66 //===========================================================================================================================
68 #define WM_USER_SERVICE_ADD ( WM_USER + 0x100 )
69 #define WM_USER_SERVICE_REMOVE ( WM_USER + 0x101 )
71 //===========================================================================================================================
73 //===========================================================================================================================
75 BEGIN_MESSAGE_MAP(BrowserDialog
, CDialog
)
76 //{{AFX_MSG_MAP(BrowserDialog)
77 ON_NOTIFY(NM_CLICK
, IDC_BROWSE_LIST
, OnBrowserListDoubleClick
)
78 ON_MESSAGE( WM_USER_SERVICE_ADD
, OnServiceAdd
)
79 ON_MESSAGE( WM_USER_SERVICE_REMOVE
, OnServiceRemove
)
83 static DWORD
UTF8StringToStringObject( const char *inUTF8
, CString
&inObject
);
85 //===========================================================================================================================
87 //===========================================================================================================================
89 BrowserDialog::BrowserDialog( CWnd
*inParent
)
90 : CDialog( BrowserDialog::IDD
, inParent
)
92 //{{AFX_DATA_INIT(BrowserDialog)
93 // NOTE: the ClassWizard will add member initialization here
96 // Note that LoadIcon does not require a subsequent DestroyIcon in Win32.
98 mIcon
= AfxGetApp()->LoadIcon( IDR_MAINFRAME
);
102 //===========================================================================================================================
104 //===========================================================================================================================
106 void BrowserDialog::DoDataExchange( CDataExchange
*pDX
)
108 CDialog::DoDataExchange(pDX
);
109 //{{AFX_DATA_MAP(BrowserDialog)
110 DDX_Control(pDX
, IDC_BROWSE_LIST
, mBrowserList
);
114 //===========================================================================================================================
116 //===========================================================================================================================
118 BOOL
BrowserDialog::OnInitDialog()
122 CDialog::OnInitDialog();
124 // Set the icon for this dialog. The framework does this automatically when the application's main window is not a dialog.
126 SetIcon( mIcon
, TRUE
); // Set big icon
127 SetIcon( mIcon
, FALSE
); // Set small icon
129 CenterWindow( GetDesktopWindow() );
135 s
.LoadString( IDS_BROWSER_LIST_COLUMN_NAME
);
136 mBrowserList
.GetWindowRect( rect
);
137 mBrowserList
.InsertColumn( 0, s
, LVCFMT_LEFT
, rect
.Width() - 8 );
139 // Start browsing for services.
143 err
= DNSBrowserCreate( 0, OnBrowserCallBack
, this, &mBrowser
);
146 AfxMessageBox( IDP_SOCKETS_INIT_FAILED
);
150 err
= DNSBrowserStartServiceSearch( mBrowser
, kDNSBrowserFlagAutoResolve
, "_http._tcp", NULL
);
153 AfxMessageBox( IDP_SOCKETS_INIT_FAILED
);
162 //===========================================================================================================================
163 // OnBrowserListDoubleClick
164 //===========================================================================================================================
166 void BrowserDialog::OnBrowserListDoubleClick( NMHDR
*pNMHDR
, LRESULT
*pResult
)
170 (void) pNMHDR
; // Unused
172 selectedItem
= mBrowserList
.GetNextItem( -1, LVNI_SELECTED
);
173 if( selectedItem
>= 0 )
175 BrowserEntry
* entry
;
179 // Build the URL from the IP and optional TXT record.
181 entry
= &mBrowserEntries
[ selectedItem
];
182 url
+= "http://" + entry
->ip
;
184 if( temp
.Find( TEXT( "path=" ) ) == 0 )
188 if( temp
.Find( '/' ) != 0 )
194 // Let the system open the URL in the correct app.
196 SHELLEXECUTEINFO info
;
198 info
.cbSize
= sizeof( info
);
203 info
.lpParameters
= NULL
;
204 info
.lpDirectory
= NULL
;
205 info
.nShow
= SW_SHOWNORMAL
;
206 info
.hInstApp
= NULL
;
208 ShellExecuteEx( &info
);
213 //===========================================================================================================================
214 // OnBrowserCallBack [static]
215 //===========================================================================================================================
218 BrowserDialog::OnBrowserCallBack(
221 DNSStatus inStatusCode
,
222 const DNSBrowserEvent
* inEvent
)
224 BrowserDialog
* dialog
;
225 BrowserEntry
* entry
;
228 DNS_UNUSED( inStatusCode
);
229 dialog
= reinterpret_cast < BrowserDialog
* > ( inContext
);
232 switch( inEvent
->type
)
234 case kDNSBrowserEventTypeResolved
:
235 if( inEvent
->data
.resolved
->address
.addressType
== kDNSNetworkAddressTypeIPv4
)
239 sprintf( ip
, "%u.%u.%u.%u:%u",
240 inEvent
->data
.resolved
->address
.u
.ipv4
.addr
.v8
[ 0 ],
241 inEvent
->data
.resolved
->address
.u
.ipv4
.addr
.v8
[ 1 ],
242 inEvent
->data
.resolved
->address
.u
.ipv4
.addr
.v8
[ 2 ],
243 inEvent
->data
.resolved
->address
.u
.ipv4
.addr
.v8
[ 3 ],
244 ( inEvent
->data
.resolved
->address
.u
.ipv4
.port
.v8
[ 0 ] << 8 ) |
245 inEvent
->data
.resolved
->address
.u
.ipv4
.port
.v8
[ 1 ] );
247 entry
= new BrowserEntry
;
251 UTF8StringToStringObject( inEvent
->data
.resolved
->name
, entry
->name
);
252 UTF8StringToStringObject( ip
, entry
->ip
);
253 UTF8StringToStringObject( inEvent
->data
.resolved
->textRecord
, entry
->text
);
255 posted
= ::PostMessage( dialog
->GetSafeHwnd(), WM_USER_SERVICE_ADD
, 0, (LPARAM
) entry
);
265 case kDNSBrowserEventTypeRemoveService
:
266 entry
= new BrowserEntry
;
270 UTF8StringToStringObject( inEvent
->data
.removeService
.name
, entry
->name
);
272 posted
= ::PostMessage( dialog
->GetSafeHwnd(), WM_USER_SERVICE_REMOVE
, 0, (LPARAM
) entry
);
286 //===========================================================================================================================
288 //===========================================================================================================================
290 LONG
BrowserDialog::OnServiceAdd( WPARAM inWParam
, LPARAM inLParam
)
292 BrowserEntry
* entry
;
298 (void) inWParam
; // Unused
300 entry
= reinterpret_cast < BrowserEntry
* > ( inLParam
);
306 hi
= mBrowserEntries
.GetSize() - 1;
309 mid
= ( lo
+ hi
) / 2;
310 result
= entry
->name
.CompareNoCase( mBrowserEntries
[ mid
].name
);
315 else if( result
< 0 )
326 mBrowserEntries
[ mid
].ip
= entry
->ip
;
327 mBrowserEntries
[ mid
].text
= entry
->text
;
335 mBrowserEntries
.InsertAt( mid
, *entry
);
336 mBrowserList
.InsertItem( mid
, entry
->name
);
342 //===========================================================================================================================
344 //===========================================================================================================================
346 LONG
BrowserDialog::OnServiceRemove( WPARAM inWParam
, LPARAM inLParam
)
348 BrowserEntry
* entry
;
354 (void) inWParam
; // Unused
356 entry
= reinterpret_cast < BrowserEntry
* > ( inLParam
);
362 hi
= mBrowserEntries
.GetSize() - 1;
365 mid
= ( lo
+ hi
) / 2;
366 result
= entry
->name
.CompareNoCase( mBrowserEntries
[ mid
].name
);
371 else if( result
< 0 )
382 mBrowserList
.DeleteItem( mid
);
383 mBrowserEntries
.RemoveAt( mid
);
393 //===========================================================================================================================
394 // UTF8StringToStringObject
395 //===========================================================================================================================
397 static DWORD
UTF8StringToStringObject( const char *inUTF8
, CString
&inObject
)
405 n
= MultiByteToWideChar( CP_UTF8
, 0, inUTF8
, -1, NULL
, 0 );
408 unicode
= (wchar_t *) malloc( (size_t)( n
* sizeof( wchar_t ) ) );
409 if( !unicode
) { err
= ERROR_INSUFFICIENT_BUFFER
; goto exit
; };
411 n
= MultiByteToWideChar( CP_UTF8
, 0, inUTF8
, -1, unicode
, n
);