imm32: Implement ImmConfigureIME using the loaded IME.
[wine] / dlls / urlmon / mk.c
1 /*
2  * Copyright 2007 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 "urlmon_main.h"
20 #include "wine/debug.h"
21
22 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
23
24 typedef struct {
25     const IInternetProtocolVtbl  *lpInternetProtocolVtbl;
26
27     LONG ref;
28
29     IStream *stream;
30 } MkProtocol;
31
32 #define PROTOCOL_THIS(iface) DEFINE_THIS(MkProtocol, InternetProtocol, iface)
33
34 #define PROTOCOL(x)  ((IInternetProtocol*)  &(x)->lpInternetProtocolVtbl)
35
36 static HRESULT WINAPI MkProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
37 {
38     MkProtocol *This = PROTOCOL_THIS(iface);
39
40     *ppv = NULL;
41     if(IsEqualGUID(&IID_IUnknown, riid)) {
42         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
43         *ppv = PROTOCOL(This);
44     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
45         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
46         *ppv = PROTOCOL(This);
47     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
48         TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
49         *ppv = PROTOCOL(This);
50     }
51
52     if(*ppv) {
53         IInternetProtocol_AddRef(iface);
54         return S_OK;
55     }
56
57     WARN("not supported interface %s\n", debugstr_guid(riid));
58     return E_NOINTERFACE;
59 }
60
61 static ULONG WINAPI MkProtocol_AddRef(IInternetProtocol *iface)
62 {
63     MkProtocol *This = PROTOCOL_THIS(iface);
64     LONG ref = InterlockedIncrement(&This->ref);
65     TRACE("(%p) ref=%d\n", This, ref);
66     return ref;
67 }
68
69 static ULONG WINAPI MkProtocol_Release(IInternetProtocol *iface)
70 {
71     MkProtocol *This = PROTOCOL_THIS(iface);
72     LONG ref = InterlockedDecrement(&This->ref);
73
74     TRACE("(%p) ref=%d\n", This, ref);
75
76     if(!ref) {
77         if(This->stream)
78             IStream_Release(This->stream);
79
80         heap_free(This);
81
82         URLMON_UnlockModule();
83     }
84
85     return ref;
86 }
87
88 static HRESULT report_result(IInternetProtocolSink *sink, HRESULT hres, DWORD dwError)
89 {
90     IInternetProtocolSink_ReportResult(sink, hres, dwError, NULL);
91     return hres;
92 }
93
94 static HRESULT WINAPI MkProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
95         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
96         DWORD grfPI, DWORD dwReserved)
97 {
98     MkProtocol *This = PROTOCOL_THIS(iface);
99     IParseDisplayName *pdn;
100     IMoniker *mon;
101     LPWSTR mime, progid, display_name;
102     LPCWSTR ptr, ptr2;
103     BINDINFO bindinfo;
104     STATSTG statstg;
105     DWORD bindf=0, eaten=0, len;
106     CLSID clsid;
107     HRESULT hres;
108
109     static const WCHAR wszMK[] = {'m','k',':'};
110
111     TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
112             pOIBindInfo, grfPI, dwReserved);
113
114     if(strncmpiW(szUrl, wszMK, sizeof(wszMK)/sizeof(WCHAR)))
115         return INET_E_INVALID_URL;
116
117     memset(&bindinfo, 0, sizeof(bindinfo));
118     bindinfo.cbSize = sizeof(BINDINFO);
119     hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &bindf, &bindinfo);
120     if(FAILED(hres)) {
121         WARN("GetBindInfo failed: %08x\n", hres);
122         return hres;
123     }
124
125     ReleaseBindInfo(&bindinfo);
126
127     IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, NULL);
128
129     hres = FindMimeFromData(NULL, szUrl, NULL, 0, NULL, 0, &mime, 0);
130     if(SUCCEEDED(hres)) {
131         IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_MIMETYPEAVAILABLE, mime);
132         CoTaskMemFree(mime);
133     }
134
135     ptr2 = szUrl + sizeof(wszMK)/sizeof(WCHAR);
136     if(*ptr2 != '@')
137         return report_result(pOIProtSink, INET_E_RESOURCE_NOT_FOUND, ERROR_INVALID_PARAMETER);
138     ptr2++;
139
140     ptr = strchrW(ptr2, ':');
141     if(!ptr)
142         return report_result(pOIProtSink, INET_E_RESOURCE_NOT_FOUND, ERROR_INVALID_PARAMETER);
143
144     progid = heap_alloc((ptr-ptr2+1)*sizeof(WCHAR));
145     memcpy(progid, ptr2, (ptr-ptr2)*sizeof(WCHAR));
146     progid[ptr-ptr2] = 0;
147     hres = CLSIDFromProgID(progid, &clsid);
148     heap_free(progid);
149     if(FAILED(hres))
150         return report_result(pOIProtSink, INET_E_RESOURCE_NOT_FOUND, ERROR_INVALID_PARAMETER);
151
152     hres = CoCreateInstance(&clsid, NULL, CLSCTX_INPROC_SERVER|CLSCTX_INPROC_HANDLER,
153             &IID_IParseDisplayName, (void**)&pdn);
154     if(FAILED(hres)) {
155         WARN("Could not create object %s\n", debugstr_guid(&clsid));
156         return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
157     }
158
159     len = strlenW(--ptr2);
160     display_name = heap_alloc((len+1)*sizeof(WCHAR));
161     memcpy(display_name, ptr2, (len+1)*sizeof(WCHAR));
162     hres = IParseDisplayName_ParseDisplayName(pdn, NULL /* FIXME */, display_name, &eaten, &mon);
163     heap_free(display_name);
164     IParseDisplayName_Release(pdn);
165     if(FAILED(hres)) {
166         WARN("ParseDisplayName failed: %08x\n", hres);
167         return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
168     }
169
170     if(This->stream) {
171         IStream_Release(This->stream);
172         This->stream = NULL;
173     }
174
175     hres = IMoniker_BindToStorage(mon, NULL /* FIXME */, NULL, &IID_IStream, (void**)&This->stream);
176     IMoniker_Release(mon);
177     if(FAILED(hres)) {
178         WARN("BindToStorage failed: %08x\n", hres);
179         return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
180     }
181
182     hres = IStream_Stat(This->stream, &statstg, STATFLAG_NONAME);
183     if(FAILED(hres)) {
184         WARN("Stat failed: %08x\n", hres);
185         return report_result(pOIProtSink, hres, ERROR_INVALID_PARAMETER);
186     }
187
188     IInternetProtocolSink_ReportData(pOIProtSink,
189             BSCF_FIRSTDATANOTIFICATION | BSCF_LASTDATANOTIFICATION,
190             statstg.cbSize.u.LowPart, statstg.cbSize.u.LowPart);
191
192     return report_result(pOIProtSink, S_OK, ERROR_SUCCESS);
193 }
194
195 static HRESULT WINAPI MkProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
196 {
197     MkProtocol *This = PROTOCOL_THIS(iface);
198     FIXME("(%p)->(%p)\n", This, pProtocolData);
199     return E_NOTIMPL;
200 }
201
202 static HRESULT WINAPI MkProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
203         DWORD dwOptions)
204 {
205     MkProtocol *This = PROTOCOL_THIS(iface);
206     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
207     return E_NOTIMPL;
208 }
209
210 static HRESULT WINAPI MkProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
211 {
212     MkProtocol *This = PROTOCOL_THIS(iface);
213
214     TRACE("(%p)->(%08x)\n", This, dwOptions);
215
216     return S_OK;
217 }
218
219 static HRESULT WINAPI MkProtocol_Suspend(IInternetProtocol *iface)
220 {
221     MkProtocol *This = PROTOCOL_THIS(iface);
222     FIXME("(%p)\n", This);
223     return E_NOTIMPL;
224 }
225
226 static HRESULT WINAPI MkProtocol_Resume(IInternetProtocol *iface)
227 {
228     MkProtocol *This = PROTOCOL_THIS(iface);
229     FIXME("(%p)\n", This);
230     return E_NOTIMPL;
231 }
232
233 static HRESULT WINAPI MkProtocol_Read(IInternetProtocol *iface, void *pv,
234         ULONG cb, ULONG *pcbRead)
235 {
236     MkProtocol *This = PROTOCOL_THIS(iface);
237
238     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
239
240     if(!This->stream)
241         return E_FAIL;
242
243     return IStream_Read(This->stream, pv, cb, pcbRead);
244 }
245
246 static HRESULT WINAPI MkProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
247         DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
248 {
249     MkProtocol *This = PROTOCOL_THIS(iface);
250     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
251     return E_NOTIMPL;
252 }
253
254 static HRESULT WINAPI MkProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
255 {
256     MkProtocol *This = PROTOCOL_THIS(iface);
257
258     TRACE("(%p)->(%08x)\n", This, dwOptions);
259
260     return S_OK;
261 }
262
263 static HRESULT WINAPI MkProtocol_UnlockRequest(IInternetProtocol *iface)
264 {
265     MkProtocol *This = PROTOCOL_THIS(iface);
266
267     TRACE("(%p)\n", This);
268
269     return S_OK;
270 }
271
272 #undef PROTOCOL_THIS
273
274 static const IInternetProtocolVtbl MkProtocolVtbl = {
275     MkProtocol_QueryInterface,
276     MkProtocol_AddRef,
277     MkProtocol_Release,
278     MkProtocol_Start,
279     MkProtocol_Continue,
280     MkProtocol_Abort,
281     MkProtocol_Terminate,
282     MkProtocol_Suspend,
283     MkProtocol_Resume,
284     MkProtocol_Read,
285     MkProtocol_Seek,
286     MkProtocol_LockRequest,
287     MkProtocol_UnlockRequest
288 };
289
290 HRESULT MkProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
291 {
292     MkProtocol *ret;
293
294     TRACE("(%p %p)\n", pUnkOuter, ppobj);
295
296     URLMON_LockModule();
297
298     ret = heap_alloc(sizeof(MkProtocol));
299
300     ret->lpInternetProtocolVtbl = &MkProtocolVtbl;
301     ret->ref = 1;
302     ret->stream = NULL;
303
304     /* NOTE:
305      * Native returns NULL ppobj and S_OK in CreateInstance if called with IID_IUnknown riid.
306      */
307     *ppobj = PROTOCOL(ret);
308
309     return S_OK;
310 }