sane.ds: Implement support for ICAP_PHYSICALHEIGHT and ICAP_PHYSICALWIDTH.
[wine] / dlls / urlmon / ftp.c
1 /*
2  * Copyright 2005-2009 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     Protocol base;
26
27     const IInternetProtocolVtbl  *lpInternetProtocolVtbl;
28     const IInternetPriorityVtbl  *lpInternetPriorityVtbl;
29
30     LONG ref;
31 } FtpProtocol;
32
33 #define PROTOCOL(x)  ((IInternetProtocol*)  &(x)->lpInternetProtocolVtbl)
34 #define PRIORITY(x)  ((IInternetPriority*)  &(x)->lpInternetPriorityVtbl)
35
36 #define ASYNCPROTOCOL_THIS(iface) DEFINE_THIS2(FtpProtocol, base, iface)
37
38 static HRESULT FtpProtocol_open_request(Protocol *prot, LPCWSTR url, DWORD request_flags,
39                                         IInternetBindInfo *bind_info)
40 {
41     FtpProtocol *This = ASYNCPROTOCOL_THIS(prot);
42
43     This->base.request = InternetOpenUrlW(This->base.internet, url, NULL, 0,
44             request_flags|INTERNET_FLAG_EXISTING_CONNECT|INTERNET_FLAG_PASSIVE,
45             (DWORD_PTR)&This->base);
46     if (!This->base.request && GetLastError() != ERROR_IO_PENDING) {
47         WARN("InternetOpenUrl failed: %d\n", GetLastError());
48         return INET_E_RESOURCE_NOT_FOUND;
49     }
50
51     return S_OK;
52 }
53
54 static HRESULT FtpProtocol_start_downloading(Protocol *prot)
55 {
56     FtpProtocol *This = ASYNCPROTOCOL_THIS(prot);
57     DWORD size;
58     BOOL res;
59
60     res = FtpGetFileSize(This->base.request, &size);
61     if(res)
62         This->base.content_length = size;
63     else
64         WARN("FtpGetFileSize failed: %d\n", GetLastError());
65
66     return S_OK;
67 }
68
69 static void FtpProtocol_close_connection(Protocol *prot)
70 {
71 }
72
73 #undef ASYNCPROTOCOL_THIS
74
75 static const ProtocolVtbl AsyncProtocolVtbl = {
76     FtpProtocol_open_request,
77     FtpProtocol_start_downloading,
78     FtpProtocol_close_connection
79 };
80
81 #define PROTOCOL_THIS(iface) DEFINE_THIS(FtpProtocol, InternetProtocol, iface)
82
83 static HRESULT WINAPI FtpProtocol_QueryInterface(IInternetProtocol *iface, REFIID riid, void **ppv)
84 {
85     FtpProtocol *This = PROTOCOL_THIS(iface);
86
87     *ppv = NULL;
88     if(IsEqualGUID(&IID_IUnknown, riid)) {
89         TRACE("(%p)->(IID_IUnknown %p)\n", This, ppv);
90         *ppv = PROTOCOL(This);
91     }else if(IsEqualGUID(&IID_IInternetProtocolRoot, riid)) {
92         TRACE("(%p)->(IID_IInternetProtocolRoot %p)\n", This, ppv);
93         *ppv = PROTOCOL(This);
94     }else if(IsEqualGUID(&IID_IInternetProtocol, riid)) {
95         TRACE("(%p)->(IID_IInternetProtocol %p)\n", This, ppv);
96         *ppv = PROTOCOL(This);
97     }else if(IsEqualGUID(&IID_IInternetPriority, riid)) {
98         TRACE("(%p)->(IID_IInternetPriority %p)\n", This, ppv);
99         *ppv = PRIORITY(This);
100     }
101
102     if(*ppv) {
103         IInternetProtocol_AddRef(iface);
104         return S_OK;
105     }
106
107     WARN("not supported interface %s\n", debugstr_guid(riid));
108     return E_NOINTERFACE;
109 }
110
111 static ULONG WINAPI FtpProtocol_AddRef(IInternetProtocol *iface)
112 {
113     FtpProtocol *This = PROTOCOL_THIS(iface);
114     LONG ref = InterlockedIncrement(&This->ref);
115     TRACE("(%p) ref=%d\n", This, ref);
116     return ref;
117 }
118
119 static ULONG WINAPI FtpProtocol_Release(IInternetProtocol *iface)
120 {
121     FtpProtocol *This = PROTOCOL_THIS(iface);
122     LONG ref = InterlockedDecrement(&This->ref);
123
124     TRACE("(%p) ref=%d\n", This, ref);
125
126     if(!ref) {
127         protocol_close_connection(&This->base);
128         heap_free(This);
129
130         URLMON_UnlockModule();
131     }
132
133     return ref;
134 }
135
136 static HRESULT WINAPI FtpProtocol_Start(IInternetProtocol *iface, LPCWSTR szUrl,
137         IInternetProtocolSink *pOIProtSink, IInternetBindInfo *pOIBindInfo,
138         DWORD grfPI, DWORD dwReserved)
139 {
140     FtpProtocol *This = PROTOCOL_THIS(iface);
141
142     static const WCHAR ftpW[] = {'f','t','p',':'};
143
144     TRACE("(%p)->(%s %p %p %08x %d)\n", This, debugstr_w(szUrl), pOIProtSink,
145           pOIBindInfo, grfPI, dwReserved);
146
147     if(strncmpW(szUrl, ftpW, sizeof(ftpW)/sizeof(WCHAR)))
148         return MK_E_SYNTAX;
149
150     return protocol_start(&This->base, PROTOCOL(This), szUrl, pOIProtSink, pOIBindInfo);
151 }
152
153 static HRESULT WINAPI FtpProtocol_Continue(IInternetProtocol *iface, PROTOCOLDATA *pProtocolData)
154 {
155     FtpProtocol *This = PROTOCOL_THIS(iface);
156
157     TRACE("(%p)->(%p)\n", This, pProtocolData);
158
159     return protocol_continue(&This->base, pProtocolData);
160 }
161
162 static HRESULT WINAPI FtpProtocol_Abort(IInternetProtocol *iface, HRESULT hrReason,
163         DWORD dwOptions)
164 {
165     FtpProtocol *This = PROTOCOL_THIS(iface);
166     FIXME("(%p)->(%08x %08x)\n", This, hrReason, dwOptions);
167     return E_NOTIMPL;
168 }
169
170 static HRESULT WINAPI FtpProtocol_Terminate(IInternetProtocol *iface, DWORD dwOptions)
171 {
172     FtpProtocol *This = PROTOCOL_THIS(iface);
173
174     TRACE("(%p)->(%08x)\n", This, dwOptions);
175
176     protocol_close_connection(&This->base);
177     return S_OK;
178 }
179
180 static HRESULT WINAPI FtpProtocol_Suspend(IInternetProtocol *iface)
181 {
182     FtpProtocol *This = PROTOCOL_THIS(iface);
183     FIXME("(%p)\n", This);
184     return E_NOTIMPL;
185 }
186
187 static HRESULT WINAPI FtpProtocol_Resume(IInternetProtocol *iface)
188 {
189     FtpProtocol *This = PROTOCOL_THIS(iface);
190     FIXME("(%p)\n", This);
191     return E_NOTIMPL;
192 }
193
194 static HRESULT WINAPI FtpProtocol_Read(IInternetProtocol *iface, void *pv,
195         ULONG cb, ULONG *pcbRead)
196 {
197     FtpProtocol *This = PROTOCOL_THIS(iface);
198
199     TRACE("(%p)->(%p %u %p)\n", This, pv, cb, pcbRead);
200
201     return protocol_read(&This->base, pv, cb, pcbRead);
202 }
203
204 static HRESULT WINAPI FtpProtocol_Seek(IInternetProtocol *iface, LARGE_INTEGER dlibMove,
205         DWORD dwOrigin, ULARGE_INTEGER *plibNewPosition)
206 {
207     FtpProtocol *This = PROTOCOL_THIS(iface);
208     FIXME("(%p)->(%d %d %p)\n", This, dlibMove.u.LowPart, dwOrigin, plibNewPosition);
209     return E_NOTIMPL;
210 }
211
212 static HRESULT WINAPI FtpProtocol_LockRequest(IInternetProtocol *iface, DWORD dwOptions)
213 {
214     FtpProtocol *This = PROTOCOL_THIS(iface);
215
216     TRACE("(%p)->(%08x)\n", This, dwOptions);
217
218     return protocol_lock_request(&This->base);
219 }
220
221 static HRESULT WINAPI FtpProtocol_UnlockRequest(IInternetProtocol *iface)
222 {
223     FtpProtocol *This = PROTOCOL_THIS(iface);
224
225     TRACE("(%p)\n", This);
226
227     return protocol_unlock_request(&This->base);
228 }
229
230 #undef PROTOCOL_THIS
231
232 static const IInternetProtocolVtbl FtpProtocolVtbl = {
233     FtpProtocol_QueryInterface,
234     FtpProtocol_AddRef,
235     FtpProtocol_Release,
236     FtpProtocol_Start,
237     FtpProtocol_Continue,
238     FtpProtocol_Abort,
239     FtpProtocol_Terminate,
240     FtpProtocol_Suspend,
241     FtpProtocol_Resume,
242     FtpProtocol_Read,
243     FtpProtocol_Seek,
244     FtpProtocol_LockRequest,
245     FtpProtocol_UnlockRequest
246 };
247
248 #define PRIORITY_THIS(iface) DEFINE_THIS(FtpProtocol, InternetPriority, iface)
249
250 static HRESULT WINAPI FtpPriority_QueryInterface(IInternetPriority *iface, REFIID riid, void **ppv)
251 {
252     FtpProtocol *This = PRIORITY_THIS(iface);
253     return IInternetProtocol_QueryInterface(PROTOCOL(This), riid, ppv);
254 }
255
256 static ULONG WINAPI FtpPriority_AddRef(IInternetPriority *iface)
257 {
258     FtpProtocol *This = PRIORITY_THIS(iface);
259     return IInternetProtocol_AddRef(PROTOCOL(This));
260 }
261
262 static ULONG WINAPI FtpPriority_Release(IInternetPriority *iface)
263 {
264     FtpProtocol *This = PRIORITY_THIS(iface);
265     return IInternetProtocol_Release(PROTOCOL(This));
266 }
267
268 static HRESULT WINAPI FtpPriority_SetPriority(IInternetPriority *iface, LONG nPriority)
269 {
270     FtpProtocol *This = PRIORITY_THIS(iface);
271
272     TRACE("(%p)->(%d)\n", This, nPriority);
273
274     This->base.priority = nPriority;
275     return S_OK;
276 }
277
278 static HRESULT WINAPI FtpPriority_GetPriority(IInternetPriority *iface, LONG *pnPriority)
279 {
280     FtpProtocol *This = PRIORITY_THIS(iface);
281
282     TRACE("(%p)->(%p)\n", This, pnPriority);
283
284     *pnPriority = This->base.priority;
285     return S_OK;
286 }
287
288 #undef PRIORITY_THIS
289
290 static const IInternetPriorityVtbl FtpPriorityVtbl = {
291     FtpPriority_QueryInterface,
292     FtpPriority_AddRef,
293     FtpPriority_Release,
294     FtpPriority_SetPriority,
295     FtpPriority_GetPriority
296 };
297
298 HRESULT FtpProtocol_Construct(IUnknown *pUnkOuter, LPVOID *ppobj)
299 {
300     FtpProtocol *ret;
301
302     TRACE("(%p %p)\n", pUnkOuter, ppobj);
303
304     URLMON_LockModule();
305
306     ret = heap_alloc_zero(sizeof(FtpProtocol));
307
308     ret->base.vtbl = &AsyncProtocolVtbl;
309     ret->lpInternetProtocolVtbl = &FtpProtocolVtbl;
310     ret->lpInternetPriorityVtbl = &FtpPriorityVtbl;
311     ret->ref = 1;
312
313     *ppobj = PROTOCOL(ret);
314     
315     return S_OK;
316 }