d3d10: Implement ID3D10EffectVariable::AsDepthStencil().
[wine/multimedia.git] / dlls / urlmon / file.c
blobed4802b8a3b0251a1fd8b9d43fdb0f95dc45cd72
1 /*
2 * Copyright 2005 Jacek Caban
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 "urlmon_main.h"
20 #include "wine/debug.h"
22 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
24 typedef struct {
25 const IInternetProtocolVtbl *lpIInternetProtocolVtbl;
26 const IInternetPriorityVtbl *lpInternetPriorityVtbl;
28 HANDLE file;
29 LONG priority;
31 LONG ref;
32 } FileProtocol;
34 #define PRIORITY(x) ((IInternetPriority*) &(x)->lpInternetPriorityVtbl)
36 #define PROTOCOL_THIS(iface) DEFINE_THIS(FileProtocol, IInternetProtocol, iface)
38 static HRESULT WINAPI FileProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
40 FileProtocol *This = PROTOCOL_THIS(iface);
42 *ppv = NULL;
43 if(IsEqualGUID(&IID_IUnknown, riid)) {
44 TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
45 *ppv = PROTOCOL(This);
46 }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
47 TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
48 *ppv = PROTOCOL(This);
49 }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
50 TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
51 *ppv = PROTOCOL(This);
52 }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
53 TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
54 *ppv = PRIORITY(This);
57 if(*ppv) {
58 IInternetProtocol_AddRef(iface);
59 return S_OK;
62 WARN("not supported interface %s\n", debugstr_guid(riid));
63 return E_NOINTERFACE;
66 static ULONG WINAPI FileProtocol_AddRef(IInternetProtocol *iface)
68 FileProtocol *This = PROTOCOL_THIS(iface);
69 LONG ref = InterlockedIncrement(&This->ref);
70 TRACE("(%p) ref=%d\n", This, ref);
71 return ref;
74 static ULONG WINAPI FileProtocol_Release(IInternetProtocol *iface)
76 FileProtocol *This = PROTOCOL_THIS(iface);
77 LONG ref = InterlockedDecrement(&This->ref);
79 TRACE("(%p) ref=%d\n", This, ref);
81 if(!ref) {
82 if(This->file)
83 CloseHandle(This->file);
84 heap_free(This);
86 URLMON_UnlockModule();
89 return ref;
92 static HRESULT WINAPI FileProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
93 IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
94 DWORD grfPI, HANDLE_PTR dwReserved)
96 FileProtocol *This = PROTOCOL_THIS(iface);
97 BINDINFO bindinfo;
98 DWORD grfBINDF = 0;
99 LARGE_INTEGER size;
100 DWORD len;
101 LPWSTR url, mime = NULL, file_name;
102 WCHAR null_char = 0;
103 BOOL first_call = FALSE;
104 HRESULT hres;
106 static const WCHAR wszFile[] = {'f','i','l','e',':'};
108 TRACE("(%p)->(%s %p %p %08x %lx)\n", This, debugstr_w(szUrl), pOIProtSink,
109 pOIBindInfo, grfPI, dwReserved);
111 if(!szUrl || strlenW(szUrl) < sizeof(wszFile)/sizeof(WCHAR)
112 || memcmp(szUrl, wszFile, sizeof(wszFile)))
113 return E_INVALIDARG;
115 memset(&bindinfo, 0, sizeof(bindinfo));
116 bindinfo.cbSize = sizeof(BINDINFO);
117 hres = IInternetBindInfo_GetBindInfo(pOIBindInfo, &grfBINDF, &bindinfo);
118 if(FAILED(hres)) {
119 WARN("GetBindInfo failed: %08x\n", hres);
120 return hres;
123 ReleaseBindInfo(&bindinfo);
125 len = lstrlenW(szUrl)+16;
126 url = heap_alloc(len*sizeof(WCHAR));
127 hres = CoInternetParseUrl(szUrl, PARSE_ENCODE, 0, url, len, &len, 0);
128 if(FAILED(hres)) {
129 heap_free(url);
130 return hres;
133 if(!(grfBINDF & BINDF_FROMURLMON))
134 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_DIRECTBIND, NULL);
136 if(!This->file) {
137 WCHAR *ptr;
139 first_call = TRUE;
141 IInternetProtocolSink_ReportProgress(pOIProtSink, BINDSTATUS_SENDINGREQUEST, &null_char);
143 file_name = url+sizeof(wszFile)/sizeof(WCHAR);
144 if(file_name[0] == '/' && file_name[1] == '/')
145 file_name += 2;
146 if(*file_name == '/')
147 file_name++;
149 for(ptr = file_name; *ptr; ptr++) {
150 if(*ptr == '?' || *ptr == '#') {
151 *ptr = 0;
152 break;
156 if(file_name[1] == '|')
157 file_name[1] = ':';
159 This->file = CreateFileW(file_name, GENERIC_READ, FILE_SHARE_READ, NULL,
160 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
162 if(This->file == INVALID_HANDLE_VALUE) {
163 This->file = NULL;
164 IInternetProtocolSink_ReportResult(pOIProtSink, INET_E_RESOURCE_NOT_FOUND,
165 GetLastError(), NULL);
166 heap_free(url);
167 return INET_E_RESOURCE_NOT_FOUND;
170 IInternetProtocolSink_ReportProgress(pOIProtSink,
171 BINDSTATUS_CACHEFILENAMEAVAILABLE, file_name);
173 hres = FindMimeFromData(NULL, url, NULL, 0, NULL, 0, &mime, 0);
174 if(SUCCEEDED(hres)) {
175 IInternetProtocolSink_ReportProgress(pOIProtSink,
176 (grfBINDF & BINDF_FROMURLMON) ?
177 BINDSTATUS_VERIFIEDMIMETYPEAVAILABLE : BINDSTATUS_MIMETYPEAVAILABLE,
178 mime);
179 CoTaskMemFree(mime);
183 heap_free(url);
185 if(GetFileSizeEx(This->file, &size))
186 IInternetProtocolSink_ReportData(pOIProtSink,
187 BSCF_FIRSTDATANOTIFICATION|BSCF_LASTDATANOTIFICATION,
188 size.u.LowPart, size.u.LowPart);
190 if(first_call)
191 IInternetProtocolSink_ReportResult(pOIProtSink, S_OK, 0, NULL);
193 return S_OK;
196 static HRESULT WINAPI FileProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
198 FileProtocol *This = PROTOCOL_THIS(iface);
199 FIXME("(%p)->(%p)\n", This, pProtocolData);
200 return E_NOTIMPL;
203 static HRESULT WINAPI FileProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
204 DWORD dwOptions)
206 FileProtocol *This = PROTOCOL_THIS(iface);
207 FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
208 return E_NOTIMPL;
211 static HRESULT WINAPI FileProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
213 FileProtocol *This = PROTOCOL_THIS(iface);
215 TRACE("(%p)->(%08x)\n", This, dwOptions);
217 return S_OK;
220 static HRESULT WINAPI FileProtocol_Suspend(IInternetProtocol *iface)
222 FileProtocol *This = PROTOCOL_THIS(iface);
223 FIXME("(%p)\n", This);
224 return E_NOTIMPL;
227 static HRESULT WINAPI FileProtocol_Resume(IInternetProtocol *iface)
229 FileProtocol *This = PROTOCOL_THIS(iface);
230 FIXME("(%p)\n", This);
231 return E_NOTIMPL;
234 static HRESULT WINAPI FileProtocol_Read(IInternetProtocol *iface, void *pv,
235 ULONG cb, ULONG *pcbRead)
237 FileProtocol *This = PROTOCOL_THIS(iface);
238 DWORD read = 0;
240 TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
242 if (pcbRead)
243 *pcbRead = 0;
245 if(!This->file)
246 return INET_E_DATA_NOT_AVAILABLE;
248 if (!ReadFile(This->file, pv, cb, &read, NULL))
249 return INET_E_DOWNLOAD_FAILURE;
251 if(pcbRead)
252 *pcbRead = read;
254 return cb == read ? S_OK : S_FALSE;
257 static HRESULT WINAPI FileProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
258 DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
260 FileProtocol *This = PROTOCOL_THIS(iface);
261 FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
262 return E_NOTIMPL;
265 static HRESULT WINAPI FileProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
267 FileProtocol *This = PROTOCOL_THIS(iface);
269 TRACE("(%p)->(%08x)\n", This, dwOptions);
271 return S_OK;
274 static HRESULT WINAPI FileProtocol_UnlockRequest(IInternetProtocol *iface)
276 FileProtocol *This = PROTOCOL_THIS(iface);
278 TRACE("(%p)\n", This);
280 return S_OK;
283 #undef PROTOCOL_THIS
285 static const IInternetProtocolVtbl FileProtocolVtbl = {
286 FileProtocol_QueryInterface,
287 FileProtocol_AddRef,
288 FileProtocol_Release,
289 FileProtocol_Start,
290 FileProtocol_Continue,
291 FileProtocol_Abort,
292 FileProtocol_Terminate,
293 FileProtocol_Suspend,
294 FileProtocol_Resume,
295 FileProtocol_Read,
296 FileProtocol_Seek,
297 FileProtocol_LockRequest,
298 FileProtocol_UnlockRequest
301 #define PRIORITY_THIS(iface) DEFINE_THIS(FileProtocol, InternetPriority, iface)
303 static HRESULT WINAPI FilePriority_QueryInterface(IInternetPriority *iface,
304 REFIID riid, void **ppv)
306 FileProtocol *This = PRIORITY_THIS(iface);
307 return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
310 static ULONG WINAPI FilePriority_AddRef(IInternetPriority *iface)
312 FileProtocol *This = PRIORITY_THIS(iface);
313 return IInternetProtocol_AddRef(PROTOCOL(This));
316 static ULONG WINAPI FilePriority_Release(IInternetPriority *iface)
318 FileProtocol *This = PRIORITY_THIS(iface);
319 return IInternetProtocol_Release(PROTOCOL(This));
322 static HRESULT WINAPI FilePriority_SetPriority(IInternetPriority *iface, LONG nPriority)
324 FileProtocol *This = PRIORITY_THIS(iface);
326 TRACE("(%p)->(%d)\n", This, nPriority);
328 This->priority = nPriority;
329 return S_OK;
332 static HRESULT WINAPI FilePriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
334 FileProtocol *This = PRIORITY_THIS(iface);
336 TRACE("(%p)->(%p)\n", This, pnPriority);
338 *pnPriority = This->priority;
339 return S_OK;
342 #undef PRIORITY_THIS
344 static const IInternetPriorityVtbl FilePriorityVtbl = {
345 FilePriority_QueryInterface,
346 FilePriority_AddRef,
347 FilePriority_Release,
348 FilePriority_SetPriority,
349 FilePriority_GetPriority
352 HRESULT FileProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
354 FileProtocol *ret;
356 TRACE("(%p %p)\n", pUnkOuter, ppobj);
358 URLMON_LockModule();
360 ret = heap_alloc(sizeof(FileProtocol));
362 ret->lpIInternetProtocolVtbl = &FileProtocolVtbl;
363 ret->lpInternetPriorityVtbl = &FilePriorityVtbl;
364 ret->file = NULL;
365 ret->priority = 0;
366 ret->ref = 1;
368 *ppobj = PROTOCOL(ret);
370 return S_OK;