mshtml: Implement IHTMLLocation::get_host.
[wine/multimedia.git] / dlls / mshtml / htmllocation.c
blobbadf1116d5fea9b19f5898a25ad4323a335a737b
1 /*
2 * Copyright 2008 Jacek Caban for CodeWeavers
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 #include <stdarg.h>
21 #define COBJMACROS
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "winreg.h"
27 #include "ole2.h"
28 #include "wininet.h"
29 #include "shlwapi.h"
31 #include "wine/debug.h"
33 #include "mshtml_private.h"
34 #include "resource.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(mshtml);
38 static HRESULT get_url(HTMLLocation *This, const WCHAR **ret)
40 if(!This->window || !This->window->doc_obj || !This->window->doc_obj->url) {
41 FIXME("No current URL\n");
42 return E_NOTIMPL;
45 *ret = This->window->doc_obj->url;
46 return S_OK;
49 static HRESULT get_url_components(HTMLLocation *This, URL_COMPONENTSW *url)
51 const WCHAR *doc_url;
52 HRESULT hres;
54 hres = get_url(This, &doc_url);
55 if(FAILED(hres))
56 return hres;
58 if(!InternetCrackUrlW(doc_url, 0, 0, url)) {
59 FIXME("InternetCrackUrlW failed: 0x%08x\n", GetLastError());
60 SetLastError(0);
61 return E_FAIL;
64 return S_OK;
67 #define HTMLLOCATION_THIS(iface) DEFINE_THIS(HTMLLocation, HTMLLocation, iface)
69 static HRESULT WINAPI HTMLLocation_QueryInterface(IHTMLLocation *iface, REFIID riid, void **ppv)
71 HTMLLocation *This = HTMLLOCATION_THIS(iface);
73 *ppv = NULL;
75 if(IsEqualGUID(&IID_IUnknown, riid)) {
76 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
77 *ppv = HTMLLOCATION(This);
78 }else if(IsEqualGUID(&IID_IHTMLLocation, riid)) {
79 TRACE("(%p)->(IID_IHTMLLocation %p)\n", This, ppv);
80 *ppv = HTMLLOCATION(This);
81 }else if(dispex_query_interface(&This->dispex, riid, ppv)) {
82 return *ppv ? S_OK : E_NOINTERFACE;
85 if(*ppv) {
86 IUnknown_AddRef((IUnknown*)*ppv);
87 return S_OK;
90 WARN("(%p)->(%s %p)\n", This, debugstr_guid(riid), ppv);
91 return E_NOINTERFACE;
94 static ULONG WINAPI HTMLLocation_AddRef(IHTMLLocation *iface)
96 HTMLLocation *This = HTMLLOCATION_THIS(iface);
97 LONG ref = InterlockedIncrement(&This->ref);
99 TRACE("(%p) ref=%d\n", This, ref);
101 return ref;
104 static ULONG WINAPI HTMLLocation_Release(IHTMLLocation *iface)
106 HTMLLocation *This = HTMLLOCATION_THIS(iface);
107 LONG ref = InterlockedDecrement(&This->ref);
109 TRACE("(%p) ref=%d\n", This, ref);
111 if(!ref) {
112 if(This->window)
113 This->window->location = NULL;
114 release_dispex(&This->dispex);
115 heap_free(This);
118 return ref;
121 static HRESULT WINAPI HTMLLocation_GetTypeInfoCount(IHTMLLocation *iface, UINT *pctinfo)
123 HTMLLocation *This = HTMLLOCATION_THIS(iface);
124 return IDispatchEx_GetTypeInfoCount(DISPATCHEX(&This->dispex), pctinfo);
127 static HRESULT WINAPI HTMLLocation_GetTypeInfo(IHTMLLocation *iface, UINT iTInfo,
128 LCID lcid, ITypeInfo **ppTInfo)
130 HTMLLocation *This = HTMLLOCATION_THIS(iface);
131 return IDispatchEx_GetTypeInfo(DISPATCHEX(&This->dispex), iTInfo, lcid, ppTInfo);
134 static HRESULT WINAPI HTMLLocation_GetIDsOfNames(IHTMLLocation *iface, REFIID riid,
135 LPOLESTR *rgszNames, UINT cNames,
136 LCID lcid, DISPID *rgDispId)
138 HTMLLocation *This = HTMLLOCATION_THIS(iface);
139 return IDispatchEx_GetIDsOfNames(DISPATCHEX(&This->dispex), riid, rgszNames, cNames, lcid, rgDispId);
142 static HRESULT WINAPI HTMLLocation_Invoke(IHTMLLocation *iface, DISPID dispIdMember,
143 REFIID riid, LCID lcid, WORD wFlags, DISPPARAMS *pDispParams,
144 VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
146 HTMLLocation *This = HTMLLOCATION_THIS(iface);
147 return IDispatchEx_Invoke(DISPATCHEX(&This->dispex), dispIdMember, riid, lcid,
148 wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
151 static HRESULT WINAPI HTMLLocation_put_href(IHTMLLocation *iface, BSTR v)
153 HTMLLocation *This = HTMLLOCATION_THIS(iface);
155 TRACE("(%p)->(%s)\n", This, debugstr_w(v));
157 if(!This->window || !This->window->doc) {
158 FIXME("No document available\n");
159 return E_FAIL;
162 return navigate_url(This->window->doc, v);
165 static HRESULT WINAPI HTMLLocation_get_href(IHTMLLocation *iface, BSTR *p)
167 HTMLLocation *This = HTMLLOCATION_THIS(iface);
168 const WCHAR *url;
169 HRESULT hres;
171 TRACE("(%p)->(%p)\n", This, p);
173 if(!p)
174 return E_POINTER;
176 hres = get_url(This, &url);
177 if(FAILED(hres))
178 return hres;
180 *p = SysAllocString(url);
181 return *p ? S_OK : E_OUTOFMEMORY;
184 static HRESULT WINAPI HTMLLocation_put_protocol(IHTMLLocation *iface, BSTR v)
186 HTMLLocation *This = HTMLLOCATION_THIS(iface);
187 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
188 return E_NOTIMPL;
191 static HRESULT WINAPI HTMLLocation_get_protocol(IHTMLLocation *iface, BSTR *p)
193 HTMLLocation *This = HTMLLOCATION_THIS(iface);
194 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
195 HRESULT hres;
197 TRACE("(%p)->(%p)\n", This, p);
199 if(!p)
200 return E_POINTER;
202 url.dwSchemeLength = 1;
203 hres = get_url_components(This, &url);
204 if(FAILED(hres))
205 return hres;
207 if(!url.dwSchemeLength) {
208 FIXME("Unexpected blank protocol\n");
209 return E_NOTIMPL;
210 }else {
211 WCHAR buf[url.dwSchemeLength + 1];
212 memcpy(buf, url.lpszScheme, url.dwSchemeLength * sizeof(WCHAR));
213 buf[url.dwSchemeLength] = ':';
214 *p = SysAllocStringLen(buf, url.dwSchemeLength + 1);
216 if(!*p)
217 return E_OUTOFMEMORY;
218 return S_OK;
221 static HRESULT WINAPI HTMLLocation_put_host(IHTMLLocation *iface, BSTR v)
223 HTMLLocation *This = HTMLLOCATION_THIS(iface);
224 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
225 return E_NOTIMPL;
228 static HRESULT WINAPI HTMLLocation_get_host(IHTMLLocation *iface, BSTR *p)
230 HTMLLocation *This = HTMLLOCATION_THIS(iface);
231 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
232 HRESULT hres;
234 TRACE("(%p)->(%p)\n", This, p);
236 if(!p)
237 return E_POINTER;
239 url.dwHostNameLength = 1;
240 hres = get_url_components(This, &url);
241 if(FAILED(hres))
242 return hres;
244 if(!url.dwHostNameLength){
245 *p = NULL;
246 return S_OK;
249 if(url.nPort) {
250 /* <hostname>:<port> */
251 const WCHAR format[] = {'%','d',0};
252 DWORD len = url.dwHostNameLength + 1 + 5 + 1;
253 WCHAR buf[len];
255 memcpy(buf, url.lpszHostName, url.dwHostNameLength * sizeof(WCHAR));
256 buf[url.dwHostNameLength] = ':';
257 snprintfW(buf + url.dwHostNameLength + 1, 6, format, url.nPort);
258 *p = SysAllocString(buf);
259 }else
260 *p = SysAllocStringLen(url.lpszHostName, url.dwHostNameLength);
262 if(!*p)
263 return E_OUTOFMEMORY;
264 return S_OK;
267 static HRESULT WINAPI HTMLLocation_put_hostname(IHTMLLocation *iface, BSTR v)
269 HTMLLocation *This = HTMLLOCATION_THIS(iface);
270 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
271 return E_NOTIMPL;
274 static HRESULT WINAPI HTMLLocation_get_hostname(IHTMLLocation *iface, BSTR *p)
276 HTMLLocation *This = HTMLLOCATION_THIS(iface);
277 FIXME("(%p)->(%p)\n", This, p);
279 if(!p)
280 return E_POINTER;
282 return E_NOTIMPL;
285 static HRESULT WINAPI HTMLLocation_put_port(IHTMLLocation *iface, BSTR v)
287 HTMLLocation *This = HTMLLOCATION_THIS(iface);
288 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
289 return E_NOTIMPL;
292 static HRESULT WINAPI HTMLLocation_get_port(IHTMLLocation *iface, BSTR *p)
294 HTMLLocation *This = HTMLLOCATION_THIS(iface);
295 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
296 HRESULT hres;
298 TRACE("(%p)->(%p)\n", This, p);
300 if(!p)
301 return E_POINTER;
303 hres = get_url_components(This, &url);
304 if(FAILED(hres))
305 return hres;
307 if(url.nPort) {
308 const WCHAR format[] = {'%','d',0};
309 WCHAR buf[6];
310 snprintfW(buf, 6, format, url.nPort);
311 *p = SysAllocString(buf);
312 }else {
313 const WCHAR empty[] = {0};
314 *p = SysAllocString(empty);
317 if(!*p)
318 return E_OUTOFMEMORY;
319 return S_OK;
322 static HRESULT WINAPI HTMLLocation_put_pathname(IHTMLLocation *iface, BSTR v)
324 HTMLLocation *This = HTMLLOCATION_THIS(iface);
325 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
326 return E_NOTIMPL;
329 static HRESULT WINAPI HTMLLocation_get_pathname(IHTMLLocation *iface, BSTR *p)
331 HTMLLocation *This = HTMLLOCATION_THIS(iface);
332 WCHAR buf[INTERNET_MAX_PATH_LENGTH];
333 URL_COMPONENTSW url = {sizeof(url)};
334 const WCHAR *doc_url;
335 DWORD size = 0;
336 HRESULT hres;
338 TRACE("(%p)->(%p)\n", This, p);
340 if(!p)
341 return E_POINTER;
343 hres = get_url(This, &doc_url);
344 if(FAILED(hres))
345 return hres;
347 hres = CoInternetParseUrl(doc_url, PARSE_PATH_FROM_URL, 0, buf, sizeof(buf), &size, 0);
348 if(SUCCEEDED(hres)) {
349 *p = SysAllocString(buf);
350 if(!*p)
351 return E_OUTOFMEMORY;
352 return S_OK;
355 url.dwUrlPathLength = 1;
356 hres = get_url_components(This, &url);
357 if(FAILED(hres))
358 return hres;
360 if(!url.dwUrlPathLength) {
361 *p = NULL;
362 return S_OK;
365 *p = SysAllocStringLen(url.lpszUrlPath, url.dwUrlPathLength);
366 if(!*p)
367 return E_OUTOFMEMORY;
368 return S_OK;
371 static HRESULT WINAPI HTMLLocation_put_search(IHTMLLocation *iface, BSTR v)
373 HTMLLocation *This = HTMLLOCATION_THIS(iface);
374 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
375 return E_NOTIMPL;
378 static HRESULT WINAPI HTMLLocation_get_search(IHTMLLocation *iface, BSTR *p)
380 HTMLLocation *This = HTMLLOCATION_THIS(iface);
381 FIXME("(%p)->(%p)\n", This, p);
383 if(!p)
384 return E_POINTER;
386 return E_NOTIMPL;
389 static HRESULT WINAPI HTMLLocation_put_hash(IHTMLLocation *iface, BSTR v)
391 HTMLLocation *This = HTMLLOCATION_THIS(iface);
392 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
393 return E_NOTIMPL;
396 static HRESULT WINAPI HTMLLocation_get_hash(IHTMLLocation *iface, BSTR *p)
398 HTMLLocation *This = HTMLLOCATION_THIS(iface);
399 FIXME("(%p)->(%p)\n", This, p);
401 if(!p)
402 return E_POINTER;
404 return E_NOTIMPL;
407 static HRESULT WINAPI HTMLLocation_reload(IHTMLLocation *iface, VARIANT_BOOL flag)
409 HTMLLocation *This = HTMLLOCATION_THIS(iface);
410 FIXME("(%p)->(%x)\n", This, flag);
411 return E_NOTIMPL;
414 static HRESULT WINAPI HTMLLocation_replace(IHTMLLocation *iface, BSTR bstr)
416 HTMLLocation *This = HTMLLOCATION_THIS(iface);
417 FIXME("(%p)->(%s)\n", This, debugstr_w(bstr));
418 return E_NOTIMPL;
421 static HRESULT WINAPI HTMLLocation_assign(IHTMLLocation *iface, BSTR bstr)
423 HTMLLocation *This = HTMLLOCATION_THIS(iface);
424 FIXME("(%p)->(%s)\n", This, debugstr_w(bstr));
425 return E_NOTIMPL;
428 static HRESULT WINAPI HTMLLocation_toString(IHTMLLocation *iface, BSTR *String)
430 HTMLLocation *This = HTMLLOCATION_THIS(iface);
431 FIXME("(%p)->(%p)\n", This, String);
432 return E_NOTIMPL;
435 #undef HTMLLOCATION_THIS
437 static const IHTMLLocationVtbl HTMLLocationVtbl = {
438 HTMLLocation_QueryInterface,
439 HTMLLocation_AddRef,
440 HTMLLocation_Release,
441 HTMLLocation_GetTypeInfoCount,
442 HTMLLocation_GetTypeInfo,
443 HTMLLocation_GetIDsOfNames,
444 HTMLLocation_Invoke,
445 HTMLLocation_put_href,
446 HTMLLocation_get_href,
447 HTMLLocation_put_protocol,
448 HTMLLocation_get_protocol,
449 HTMLLocation_put_host,
450 HTMLLocation_get_host,
451 HTMLLocation_put_hostname,
452 HTMLLocation_get_hostname,
453 HTMLLocation_put_port,
454 HTMLLocation_get_port,
455 HTMLLocation_put_pathname,
456 HTMLLocation_get_pathname,
457 HTMLLocation_put_search,
458 HTMLLocation_get_search,
459 HTMLLocation_put_hash,
460 HTMLLocation_get_hash,
461 HTMLLocation_reload,
462 HTMLLocation_replace,
463 HTMLLocation_assign,
464 HTMLLocation_toString
467 static const tid_t HTMLLocation_iface_tids[] = {
468 IHTMLLocation_tid,
471 static dispex_static_data_t HTMLLocation_dispex = {
472 NULL,
473 DispHTMLLocation_tid,
474 NULL,
475 HTMLLocation_iface_tids
479 HRESULT HTMLLocation_Create(HTMLWindow *window, HTMLLocation **ret)
481 HTMLLocation *location;
483 location = heap_alloc(sizeof(*location));
484 if(!location)
485 return E_OUTOFMEMORY;
487 location->lpHTMLLocationVtbl = &HTMLLocationVtbl;
488 location->ref = 1;
489 location->window = window;
491 init_dispex(&location->dispex, (IUnknown*)HTMLLOCATION(location), &HTMLLocation_dispex);
493 *ret = location;
494 return S_OK;