mshtml: Implement IHTMLLocation::get_search.
[wine/wine-gecko.git] / dlls / mshtml / htmllocation.c
blob5db526702c5731c7d4814dec6896b1beca6f3d3a
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 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
169 WCHAR *buf = NULL, *url_path = NULL;
170 HRESULT hres, ret;
171 DWORD len = 0;
172 int i;
174 TRACE("(%p)->(%p)\n", This, p);
176 if(!p)
177 return E_POINTER;
179 url.dwSchemeLength = 1;
180 url.dwHostNameLength = 1;
181 url.dwUrlPathLength = 1;
182 url.dwExtraInfoLength = 1;
183 hres = get_url_components(This, &url);
184 if(FAILED(hres))
185 return hres;
187 switch(url.nScheme) {
188 case INTERNET_SCHEME_FILE:
190 /* prepend a slash */
191 url_path = HeapAlloc(GetProcessHeap(), 0, (url.dwUrlPathLength + 1) * sizeof(WCHAR));
192 if(!url_path)
193 return E_OUTOFMEMORY;
194 url_path[0] = '/';
195 memcpy(url_path + 1, url.lpszUrlPath, url.dwUrlPathLength * sizeof(WCHAR));
196 url.lpszUrlPath = url_path;
197 url.dwUrlPathLength = url.dwUrlPathLength + 1;
199 break;
201 case INTERNET_SCHEME_HTTP:
202 case INTERNET_SCHEME_HTTPS:
203 case INTERNET_SCHEME_FTP:
204 if(!url.dwUrlPathLength) {
205 /* add a slash if it's blank */
206 url_path = url.lpszUrlPath = HeapAlloc(GetProcessHeap(), 0, 1 * sizeof(WCHAR));
207 if(!url.lpszUrlPath)
208 return E_OUTOFMEMORY;
209 url.lpszUrlPath[0] = '/';
210 url.dwUrlPathLength = 1;
212 break;
214 default:
215 break;
218 /* replace \ with / */
219 for(i = 0; i < url.dwUrlPathLength; ++i)
220 if(url.lpszUrlPath[i] == '\\')
221 url.lpszUrlPath[i] = '/';
223 if(InternetCreateUrlW(&url, ICU_ESCAPE, NULL, &len)) {
224 FIXME("InternetCreateUrl succeeded with NULL buffer?\n");
225 ret = E_FAIL;
226 goto cleanup;
229 if(GetLastError() != ERROR_INSUFFICIENT_BUFFER) {
230 FIXME("InternetCreateUrl failed with error: %08x\n", GetLastError());
231 SetLastError(0);
232 ret = E_FAIL;
233 goto cleanup;
235 SetLastError(0);
237 buf = HeapAlloc(GetProcessHeap(), 0, len * sizeof(WCHAR));
238 if(!buf) {
239 ret = E_OUTOFMEMORY;
240 goto cleanup;
243 if(!InternetCreateUrlW(&url, ICU_ESCAPE, buf, &len)) {
244 FIXME("InternetCreateUrl failed with error: %08x\n", GetLastError());
245 SetLastError(0);
246 ret = E_FAIL;
247 goto cleanup;
250 *p = SysAllocStringLen(buf, len);
251 if(!*p) {
252 ret = E_OUTOFMEMORY;
253 goto cleanup;
256 ret = S_OK;
258 cleanup:
259 if(buf)
260 HeapFree(GetProcessHeap(), 0, buf);
261 if(url_path)
262 HeapFree(GetProcessHeap(), 0, url_path);
264 return ret;
267 static HRESULT WINAPI HTMLLocation_put_protocol(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_protocol(IHTMLLocation *iface, BSTR *p)
276 HTMLLocation *This = HTMLLOCATION_THIS(iface);
277 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
278 HRESULT hres;
280 TRACE("(%p)->(%p)\n", This, p);
282 if(!p)
283 return E_POINTER;
285 url.dwSchemeLength = 1;
286 hres = get_url_components(This, &url);
287 if(FAILED(hres))
288 return hres;
290 if(!url.dwSchemeLength) {
291 FIXME("Unexpected blank protocol\n");
292 return E_NOTIMPL;
293 }else {
294 WCHAR buf[url.dwSchemeLength + 1];
295 memcpy(buf, url.lpszScheme, url.dwSchemeLength * sizeof(WCHAR));
296 buf[url.dwSchemeLength] = ':';
297 *p = SysAllocStringLen(buf, url.dwSchemeLength + 1);
299 if(!*p)
300 return E_OUTOFMEMORY;
301 return S_OK;
304 static HRESULT WINAPI HTMLLocation_put_host(IHTMLLocation *iface, BSTR v)
306 HTMLLocation *This = HTMLLOCATION_THIS(iface);
307 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
308 return E_NOTIMPL;
311 static HRESULT WINAPI HTMLLocation_get_host(IHTMLLocation *iface, BSTR *p)
313 HTMLLocation *This = HTMLLOCATION_THIS(iface);
314 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
315 HRESULT hres;
317 TRACE("(%p)->(%p)\n", This, p);
319 if(!p)
320 return E_POINTER;
322 url.dwHostNameLength = 1;
323 hres = get_url_components(This, &url);
324 if(FAILED(hres))
325 return hres;
327 if(!url.dwHostNameLength){
328 *p = NULL;
329 return S_OK;
332 if(url.nPort) {
333 /* <hostname>:<port> */
334 const WCHAR format[] = {'%','d',0};
335 DWORD len = url.dwHostNameLength + 1 + 5 + 1;
336 WCHAR buf[len];
338 memcpy(buf, url.lpszHostName, url.dwHostNameLength * sizeof(WCHAR));
339 buf[url.dwHostNameLength] = ':';
340 snprintfW(buf + url.dwHostNameLength + 1, 6, format, url.nPort);
341 *p = SysAllocString(buf);
342 }else
343 *p = SysAllocStringLen(url.lpszHostName, url.dwHostNameLength);
345 if(!*p)
346 return E_OUTOFMEMORY;
347 return S_OK;
350 static HRESULT WINAPI HTMLLocation_put_hostname(IHTMLLocation *iface, BSTR v)
352 HTMLLocation *This = HTMLLOCATION_THIS(iface);
353 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
354 return E_NOTIMPL;
357 static HRESULT WINAPI HTMLLocation_get_hostname(IHTMLLocation *iface, BSTR *p)
359 HTMLLocation *This = HTMLLOCATION_THIS(iface);
360 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
361 HRESULT hres;
363 TRACE("(%p)->(%p)\n", This, p);
365 if(!p)
366 return E_POINTER;
368 url.dwHostNameLength = 1;
369 hres = get_url_components(This, &url);
370 if(FAILED(hres))
371 return hres;
373 if(!url.dwHostNameLength){
374 *p = NULL;
375 return S_OK;
378 *p = SysAllocStringLen(url.lpszHostName, url.dwHostNameLength);
379 if(!*p)
380 return E_OUTOFMEMORY;
381 return S_OK;
384 static HRESULT WINAPI HTMLLocation_put_port(IHTMLLocation *iface, BSTR v)
386 HTMLLocation *This = HTMLLOCATION_THIS(iface);
387 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
388 return E_NOTIMPL;
391 static HRESULT WINAPI HTMLLocation_get_port(IHTMLLocation *iface, BSTR *p)
393 HTMLLocation *This = HTMLLOCATION_THIS(iface);
394 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
395 HRESULT hres;
397 TRACE("(%p)->(%p)\n", This, p);
399 if(!p)
400 return E_POINTER;
402 hres = get_url_components(This, &url);
403 if(FAILED(hres))
404 return hres;
406 if(url.nPort) {
407 const WCHAR format[] = {'%','d',0};
408 WCHAR buf[6];
409 snprintfW(buf, 6, format, url.nPort);
410 *p = SysAllocString(buf);
411 }else {
412 const WCHAR empty[] = {0};
413 *p = SysAllocString(empty);
416 if(!*p)
417 return E_OUTOFMEMORY;
418 return S_OK;
421 static HRESULT WINAPI HTMLLocation_put_pathname(IHTMLLocation *iface, BSTR v)
423 HTMLLocation *This = HTMLLOCATION_THIS(iface);
424 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
425 return E_NOTIMPL;
428 static HRESULT WINAPI HTMLLocation_get_pathname(IHTMLLocation *iface, BSTR *p)
430 HTMLLocation *This = HTMLLOCATION_THIS(iface);
431 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
432 HRESULT hres;
434 TRACE("(%p)->(%p)\n", This, p);
436 if(!p)
437 return E_POINTER;
439 url.dwUrlPathLength = 1;
440 url.dwExtraInfoLength = 1;
441 hres = get_url_components(This, &url);
442 if(FAILED(hres))
443 return hres;
445 if(url.dwUrlPathLength && url.lpszUrlPath[0] == '/')
446 *p = SysAllocStringLen(url.lpszUrlPath + 1, url.dwUrlPathLength - 1);
447 else
448 *p = SysAllocStringLen(url.lpszUrlPath, url.dwUrlPathLength);
450 if(!*p)
451 return E_OUTOFMEMORY;
452 return S_OK;
455 static HRESULT WINAPI HTMLLocation_put_search(IHTMLLocation *iface, BSTR v)
457 HTMLLocation *This = HTMLLOCATION_THIS(iface);
458 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
459 return E_NOTIMPL;
462 static HRESULT WINAPI HTMLLocation_get_search(IHTMLLocation *iface, BSTR *p)
464 HTMLLocation *This = HTMLLOCATION_THIS(iface);
465 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
466 HRESULT hres;
467 const WCHAR hash[] = {'#',0};
469 TRACE("(%p)->(%p)\n", This, p);
471 if(!p)
472 return E_POINTER;
474 url.dwExtraInfoLength = 1;
475 hres = get_url_components(This, &url);
476 if(FAILED(hres))
477 return hres;
479 if(!url.dwExtraInfoLength){
480 *p = NULL;
481 return S_OK;
484 url.dwExtraInfoLength = strcspnW(url.lpszExtraInfo, hash);
486 *p = SysAllocStringLen(url.lpszExtraInfo, url.dwExtraInfoLength);
488 if(!*p)
489 return E_OUTOFMEMORY;
490 return S_OK;
493 static HRESULT WINAPI HTMLLocation_put_hash(IHTMLLocation *iface, BSTR v)
495 HTMLLocation *This = HTMLLOCATION_THIS(iface);
496 FIXME("(%p)->(%s)\n", This, debugstr_w(v));
497 return E_NOTIMPL;
500 static HRESULT WINAPI HTMLLocation_get_hash(IHTMLLocation *iface, BSTR *p)
502 HTMLLocation *This = HTMLLOCATION_THIS(iface);
503 URL_COMPONENTSW url = {sizeof(URL_COMPONENTSW)};
504 const WCHAR hash[] = {'#',0};
505 DWORD hash_pos = 0;
506 HRESULT hres;
508 TRACE("(%p)->(%p)\n", This, p);
510 if(!p)
511 return E_POINTER;
513 url.dwExtraInfoLength = 1;
514 hres = get_url_components(This, &url);
515 if(FAILED(hres))
516 return hres;
518 if(!url.dwExtraInfoLength){
519 *p = NULL;
520 return S_OK;
523 hash_pos = strcspnW(url.lpszExtraInfo, hash);
524 url.dwExtraInfoLength -= hash_pos;
526 *p = SysAllocStringLen(url.lpszExtraInfo + hash_pos, url.dwExtraInfoLength);
528 if(!*p)
529 return E_OUTOFMEMORY;
530 return S_OK;
533 static HRESULT WINAPI HTMLLocation_reload(IHTMLLocation *iface, VARIANT_BOOL flag)
535 HTMLLocation *This = HTMLLOCATION_THIS(iface);
536 FIXME("(%p)->(%x)\n", This, flag);
537 return E_NOTIMPL;
540 static HRESULT WINAPI HTMLLocation_replace(IHTMLLocation *iface, BSTR bstr)
542 HTMLLocation *This = HTMLLOCATION_THIS(iface);
543 FIXME("(%p)->(%s)\n", This, debugstr_w(bstr));
544 return E_NOTIMPL;
547 static HRESULT WINAPI HTMLLocation_assign(IHTMLLocation *iface, BSTR bstr)
549 HTMLLocation *This = HTMLLOCATION_THIS(iface);
550 FIXME("(%p)->(%s)\n", This, debugstr_w(bstr));
551 return E_NOTIMPL;
554 static HRESULT WINAPI HTMLLocation_toString(IHTMLLocation *iface, BSTR *String)
556 HTMLLocation *This = HTMLLOCATION_THIS(iface);
557 FIXME("(%p)->(%p)\n", This, String);
558 return E_NOTIMPL;
561 #undef HTMLLOCATION_THIS
563 static const IHTMLLocationVtbl HTMLLocationVtbl = {
564 HTMLLocation_QueryInterface,
565 HTMLLocation_AddRef,
566 HTMLLocation_Release,
567 HTMLLocation_GetTypeInfoCount,
568 HTMLLocation_GetTypeInfo,
569 HTMLLocation_GetIDsOfNames,
570 HTMLLocation_Invoke,
571 HTMLLocation_put_href,
572 HTMLLocation_get_href,
573 HTMLLocation_put_protocol,
574 HTMLLocation_get_protocol,
575 HTMLLocation_put_host,
576 HTMLLocation_get_host,
577 HTMLLocation_put_hostname,
578 HTMLLocation_get_hostname,
579 HTMLLocation_put_port,
580 HTMLLocation_get_port,
581 HTMLLocation_put_pathname,
582 HTMLLocation_get_pathname,
583 HTMLLocation_put_search,
584 HTMLLocation_get_search,
585 HTMLLocation_put_hash,
586 HTMLLocation_get_hash,
587 HTMLLocation_reload,
588 HTMLLocation_replace,
589 HTMLLocation_assign,
590 HTMLLocation_toString
593 static const tid_t HTMLLocation_iface_tids[] = {
594 IHTMLLocation_tid,
597 static dispex_static_data_t HTMLLocation_dispex = {
598 NULL,
599 DispHTMLLocation_tid,
600 NULL,
601 HTMLLocation_iface_tids
605 HRESULT HTMLLocation_Create(HTMLWindow *window, HTMLLocation **ret)
607 HTMLLocation *location;
609 location = heap_alloc(sizeof(*location));
610 if(!location)
611 return E_OUTOFMEMORY;
613 location->lpHTMLLocationVtbl = &HTMLLocationVtbl;
614 location->ref = 1;
615 location->window = window;
617 init_dispex(&location->dispex, (IUnknown*)HTMLLOCATION(location), &HTMLLocation_dispex);
619 *ret = location;
620 return S_OK;