msi: Only insert entries into listbox if property value matches.
[wine] / dlls / urlmon / session.c
1 /*
2  * Copyright 2005-2006 Jacek Caban for CodeWeavers
3  *
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.
8  *
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.
13  *
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
17  */
18
19 #include <stdarg.h>
20
21 #define COBJMACROS
22
23 #include "windef.h"
24 #include "winbase.h"
25 #include "winuser.h"
26 #include "winreg.h"
27 #include "ole2.h"
28 #include "urlmon.h"
29 #include "urlmon_main.h"
30
31 #include "wine/debug.h"
32 #include "wine/unicode.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
35
36 typedef struct name_space {
37     LPWSTR protocol;
38     IClassFactory *cf;
39     CLSID clsid;
40
41     struct name_space *next;
42 } name_space;
43
44 static name_space *name_space_list = NULL;
45
46 static name_space *find_name_space(LPCWSTR protocol)
47 {
48     name_space *iter;
49
50     for(iter = name_space_list; iter; iter = iter->next) {
51         if(!strcmpW(iter->protocol, protocol))
52             return iter;
53     }
54
55     return NULL;
56 }
57
58 static HRESULT get_protocol_iface(LPCWSTR schema, DWORD schema_len, CLSID *pclsid, IUnknown **ret)
59 {
60     WCHAR str_clsid[64];
61     HKEY hkey = NULL;
62     DWORD res, type, size;
63     CLSID clsid;
64     LPWSTR wszKey;
65     HRESULT hres;
66
67     static const WCHAR wszProtocolsKey[] =
68         {'P','R','O','T','O','C','O','L','S','\\','H','a','n','d','l','e','r','\\'};
69     static const WCHAR wszCLSID[] = {'C','L','S','I','D',0};
70
71     wszKey = HeapAlloc(GetProcessHeap(), 0, sizeof(wszProtocolsKey)+(schema_len+1)*sizeof(WCHAR));
72     memcpy(wszKey, wszProtocolsKey, sizeof(wszProtocolsKey));
73     memcpy(wszKey + sizeof(wszProtocolsKey)/sizeof(WCHAR), schema, (schema_len+1)*sizeof(WCHAR));
74
75     res = RegOpenKeyW(HKEY_CLASSES_ROOT, wszKey, &hkey);
76     HeapFree(GetProcessHeap(), 0, wszKey);
77     if(res != ERROR_SUCCESS) {
78         TRACE("Could not open protocol handler key\n");
79         return E_FAIL;
80     }
81     
82     size = sizeof(str_clsid);
83     res = RegQueryValueExW(hkey, wszCLSID, NULL, &type, (LPBYTE)str_clsid, &size);
84     RegCloseKey(hkey);
85     if(res != ERROR_SUCCESS || type != REG_SZ) {
86         WARN("Could not get protocol CLSID res=%d\n", res);
87         return E_FAIL;
88     }
89
90     hres = CLSIDFromString(str_clsid, &clsid);
91     if(FAILED(hres)) {
92         WARN("CLSIDFromString failed: %08x\n", hres);
93         return hres;
94     }
95
96     if(pclsid)
97         *pclsid = clsid;
98
99     return CoGetClassObject(&clsid, CLSCTX_INPROC_SERVER, NULL, &IID_IUnknown, (void**)ret);
100 }
101
102 IInternetProtocolInfo *get_protocol_info(LPCWSTR url)
103 {
104     IInternetProtocolInfo *ret = NULL;
105     name_space *ns;
106     IUnknown *unk;
107     WCHAR schema[64];
108     DWORD schema_len;
109     HRESULT hres;
110
111     hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
112             &schema_len, 0);
113     if(FAILED(hres) || !schema_len)
114         return NULL;
115
116     ns = find_name_space(schema);
117     if(ns) {
118         hres = IClassFactory_QueryInterface(ns->cf, &IID_IInternetProtocolInfo, (void**)&ret);
119         if(SUCCEEDED(hres))
120             return ret;
121
122         hres = IClassFactory_CreateInstance(ns->cf, NULL, &IID_IInternetProtocolInfo, (void**)&ret);
123         if(SUCCEEDED(hres))
124             return ret;
125     }
126
127     hres = get_protocol_iface(schema, schema_len, NULL, &unk);
128     if(FAILED(hres))
129         return NULL;
130
131     hres = IUnknown_QueryInterface(unk, &IID_IInternetProtocolInfo, (void**)&ret);
132     IUnknown_Release(unk);
133
134     return ret;
135 }
136
137 HRESULT get_protocol_handler(LPCWSTR url, CLSID *clsid, IClassFactory **ret)
138 {
139     IUnknown *unk;
140     name_space *ns;
141     WCHAR schema[64];
142     DWORD schema_len;
143     HRESULT hres;
144
145     hres = CoInternetParseUrl(url, PARSE_SCHEMA, 0, schema, sizeof(schema)/sizeof(schema[0]),
146             &schema_len, 0);
147     if(FAILED(hres) || !schema_len)
148         return schema_len ? hres : E_FAIL;
149
150     ns = find_name_space(schema);
151     if(ns) {
152         *ret = ns->cf;
153         if(clsid)
154             *clsid = ns->clsid;
155         return S_OK;
156     }
157
158     hres = get_protocol_iface(schema, schema_len, clsid, &unk);
159     if(FAILED(hres))
160         return hres;
161
162     hres = IUnknown_QueryInterface(unk, &IID_IClassFactory, (void**)ret);
163     IUnknown_Release(unk);
164     return hres;
165 }
166
167 static HRESULT WINAPI InternetSession_QueryInterface(IInternetSession *iface,
168         REFIID riid, void **ppv)
169 {
170     TRACE("(%s %p)\n", debugstr_guid(riid), ppv);
171
172     if(IsEqualGUID(&IID_IUnknown, riid) || IsEqualGUID(&IID_IInternetSession, riid)) {
173         *ppv = iface;
174         IInternetSession_AddRef(iface);
175         return S_OK;
176     }
177
178     *ppv = NULL;
179     return E_NOINTERFACE;
180 }
181
182 static ULONG WINAPI InternetSession_AddRef(IInternetSession *iface)
183 {
184     TRACE("()\n");
185     URLMON_LockModule();
186     return 2;
187 }
188
189 static ULONG WINAPI InternetSession_Release(IInternetSession *iface)
190 {
191     TRACE("()\n");
192     URLMON_UnlockModule();
193     return 1;
194 }
195
196 static HRESULT WINAPI InternetSession_RegisterNameSpace(IInternetSession *iface,
197         IClassFactory *pCF, REFCLSID rclsid, LPCWSTR pwzProtocol, ULONG cPatterns,
198         const LPCWSTR *ppwzPatterns, DWORD dwReserved)
199 {
200     name_space *new_name_space;
201     int size;
202
203     TRACE("(%p %s %s %d %p %d)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzProtocol),
204           cPatterns, ppwzPatterns, dwReserved);
205
206     if(cPatterns || ppwzPatterns)
207         FIXME("patterns not supported\n");
208     if(dwReserved)
209         WARN("dwReserved = %d\n", dwReserved);
210
211     if(!pCF || !pwzProtocol)
212         return E_INVALIDARG;
213
214     new_name_space = HeapAlloc(GetProcessHeap(), 0, sizeof(name_space));
215
216     size = (strlenW(pwzProtocol)+1)*sizeof(WCHAR);
217     new_name_space->protocol = HeapAlloc(GetProcessHeap(), 0, size);
218     memcpy(new_name_space->protocol, pwzProtocol, size);
219
220     IClassFactory_AddRef(pCF);
221     new_name_space->cf = pCF;
222     new_name_space->clsid = *rclsid;
223
224     new_name_space->next = name_space_list;
225     name_space_list = new_name_space;
226     return S_OK;
227 }
228
229 static HRESULT WINAPI InternetSession_UnregisterNameSpace(IInternetSession *iface,
230         IClassFactory *pCF, LPCWSTR pszProtocol)
231 {
232     name_space *iter, *last = NULL;
233
234     TRACE("(%p %s)\n", pCF, debugstr_w(pszProtocol));
235
236     if(!pCF || !pszProtocol)
237         return E_INVALIDARG;
238
239     for(iter = name_space_list; iter; iter = iter->next) {
240         if(iter->cf == pCF && !strcmpW(iter->protocol, pszProtocol))
241             break;
242         last = iter;
243     }
244
245     if(!iter)
246         return S_OK;
247
248     if(last)
249         last->next = iter->next;
250     else
251         name_space_list = iter->next;
252
253     IClassFactory_Release(iter->cf);
254     HeapFree(GetProcessHeap(), 0, iter->protocol);
255     HeapFree(GetProcessHeap(), 0, iter);
256
257     return S_OK;
258 }
259
260 static HRESULT WINAPI InternetSession_RegisterMimeFilter(IInternetSession *iface,
261         IClassFactory *pCF, REFCLSID rclsid, LPCWSTR pwzType)
262 {
263     FIXME("(%p %s %s)\n", pCF, debugstr_guid(rclsid), debugstr_w(pwzType));
264     return E_NOTIMPL;
265 }
266
267 static HRESULT WINAPI InternetSession_UnregisterMimeFilter(IInternetSession *iface,
268         IClassFactory *pCF, LPCWSTR pwzType)
269 {
270     FIXME("(%p %s)\n", pCF, debugstr_w(pwzType));
271     return E_NOTIMPL;
272 }
273
274 static HRESULT WINAPI InternetSession_CreateBinding(IInternetSession *iface,
275         LPBC pBC, LPCWSTR szUrl, IUnknown *pUnkOuter, IUnknown **ppUnk,
276         IInternetProtocol **ppOInetProt, DWORD dwOption)
277 {
278     TRACE("(%p %s %p %p %p %08x)\n", pBC, debugstr_w(szUrl), pUnkOuter, ppUnk,
279             ppOInetProt, dwOption);
280
281     if(pBC || pUnkOuter || ppUnk || dwOption)
282         FIXME("Unsupported arguments\n");
283
284     return create_binding_protocol(szUrl, ppOInetProt);
285 }
286
287 static HRESULT WINAPI InternetSession_SetSessionOption(IInternetSession *iface,
288         DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength, DWORD dwReserved)
289 {
290     FIXME("(%08x %p %d %d)\n", dwOption, pBuffer, dwBufferLength, dwReserved);
291     return E_NOTIMPL;
292 }
293
294 static const IInternetSessionVtbl InternetSessionVtbl = {
295     InternetSession_QueryInterface,
296     InternetSession_AddRef,
297     InternetSession_Release,
298     InternetSession_RegisterNameSpace,
299     InternetSession_UnregisterNameSpace,
300     InternetSession_RegisterMimeFilter,
301     InternetSession_UnregisterMimeFilter,
302     InternetSession_CreateBinding,
303     InternetSession_SetSessionOption
304 };
305
306 static IInternetSession InternetSession = { &InternetSessionVtbl };
307
308 /***********************************************************************
309  *           CoInternetGetSession (URLMON.@)
310  *
311  * Create a new internet session and return an IInternetSession interface
312  * representing it.
313  *
314  * PARAMS
315  *    dwSessionMode      [I] Mode for the internet session
316  *    ppIInternetSession [O] Destination for creates IInternetSession object
317  *    dwReserved         [I] Reserved, must be 0.
318  *
319  * RETURNS
320  *    Success: S_OK. ppIInternetSession contains the IInternetSession interface.
321  *    Failure: E_INVALIDARG, if any argument is invalid, or
322  *             E_OUTOFMEMORY if memory allocation fails.
323  */
324 HRESULT WINAPI CoInternetGetSession(DWORD dwSessionMode, IInternetSession **ppIInternetSession,
325         DWORD dwReserved)
326 {
327     TRACE("(%d %p %d)\n", dwSessionMode, ppIInternetSession, dwReserved);
328
329     if(dwSessionMode)
330         ERR("dwSessionMode=%d\n", dwSessionMode);
331     if(dwReserved)
332         ERR("dwReserved=%d\n", dwReserved);
333
334     IInternetSession_AddRef(&InternetSession);
335     *ppIInternetSession = &InternetSession;
336     return S_OK;
337 }
338
339 /**************************************************************************
340  *                 UrlMkGetSessionOption (URLMON.@)
341  */
342 static BOOL get_url_encoding(HKEY root, DWORD *encoding)
343 {
344     DWORD size = sizeof(DWORD), res, type;
345     HKEY hkey;
346
347     static const WCHAR wszKeyName[] = 
348         {'S','O','F','T','W','A','R','E',
349          '\\','M','i','c','r','o','s','o','f','t',
350          '\\','W','i','n','d','o','w','s',
351          '\\','C','u','r','r','e','n','t','V','e','r','s','i','o','n',
352          '\\','I','n','t','e','r','n','e','t',' ','S','e','t','t','i','n','g','s',0};
353     static const WCHAR wszUrlEncoding[] = {'U','r','l','E','n','c','o','d','i','n','g',0};
354
355     res = RegOpenKeyW(root, wszKeyName, &hkey);
356     if(res != ERROR_SUCCESS)
357         return FALSE;
358
359     res = RegQueryValueExW(hkey, wszUrlEncoding, NULL, &type, (LPBYTE)encoding, &size);
360     RegCloseKey(hkey);
361
362     return res == ERROR_SUCCESS;
363 }
364
365 HRESULT WINAPI UrlMkGetSessionOption(DWORD dwOption, LPVOID pBuffer, DWORD dwBufferLength,
366                                      DWORD* pdwBufferLength, DWORD dwReserved)
367 {
368     TRACE("(%x, %p, %d, %p)\n", dwOption, pBuffer, dwBufferLength, pdwBufferLength);
369
370     if(dwReserved)
371         WARN("dwReserved = %d\n", dwReserved);
372
373     switch(dwOption) {
374     case URLMON_OPTION_URL_ENCODING: {
375         DWORD encoding = 0;
376
377         if(!pBuffer || dwBufferLength < sizeof(DWORD) || !pdwBufferLength)
378             return E_INVALIDARG;
379
380         if(!get_url_encoding(HKEY_CURRENT_USER, &encoding))
381             get_url_encoding(HKEY_LOCAL_MACHINE, &encoding);
382
383         *pdwBufferLength = sizeof(DWORD);
384         *(DWORD*)pBuffer = encoding ? URL_ENCODING_DISABLE_UTF8 : URL_ENCODING_ENABLE_UTF8;
385         return S_OK;
386     }
387     default:
388         FIXME("unsupported option %x\n", dwOption);
389     }
390
391     return E_INVALIDARG;
392 }