Eliminate lots of __WINE__ conditionals from the headers.
[wine] / dlls / urlmon / umon.c
1 /*
2  * UrlMon
3  *
4  * Copyright 1999 Corel Corporation
5  *
6  * Ulrich Czekalla
7  *
8  * Copyright 2002 Huw D M Davies for CodeWeavers
9  *
10  *
11  * This library is free software; you can redistribute it and/or
12  * modify it under the terms of the GNU Lesser General Public
13  * License as published by the Free Software Foundation; either
14  * version 2.1 of the License, or (at your option) any later version.
15  *
16  * This library is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19  * Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public
22  * License along with this library; if not, write to the Free Software
23  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
24  */
25
26 #define COM_NO_WINDOWS_H
27 #include <stdio.h>
28
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winuser.h"
32 #include "objbase.h"
33 #include "wine/debug.h"
34 #include "wine/unicode.h"
35 #include "ole2.h"
36 #include "urlmon.h"
37 #include "wininet.h"
38 #include "urlmon_main.h"
39
40 WINE_DEFAULT_DEBUG_CHANNEL(urlmon);
41
42 /* native urlmon.dll uses this key, too */
43 static WCHAR BSCBHolder[] = { '_','B','S','C','B','_','H','o','l','d','e','r','_',0 };
44
45 /*static BOOL registered_wndclass = FALSE;*/
46
47 /* filemoniker data structure */
48 typedef struct URLMonikerImpl{
49
50     ICOM_VTABLE(IMoniker)*  lpvtbl1;  /* VTable relative to the IMoniker interface.*/
51     ICOM_VTABLE(IBinding)*  lpvtbl2;  /* VTable to IBinding interface */
52
53     ULONG ref; /* reference counter for this object */
54
55     LPOLESTR URLName; /* URL string identified by this URLmoniker */
56
57     HWND hwndCallback;
58     IBindCtx *pBC;
59     HINTERNET hinternet, hconnect, hrequest;
60 } URLMonikerImpl;
61
62 /********************************************************************************/
63 /* URLMoniker prototype functions :                                            */
64
65 /* IUnknown prototype functions */
66 static HRESULT WINAPI URLMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject);
67 static ULONG   WINAPI URLMonikerImpl_AddRef(IMoniker* iface);
68 static ULONG   WINAPI URLMonikerImpl_Release(IMoniker* iface);
69
70 /* IPersist prototype functions */
71 static HRESULT WINAPI URLMonikerImpl_GetClassID(IMoniker* iface, CLSID *pClassID);
72
73 /* IPersistStream prototype functions */
74 static HRESULT WINAPI URLMonikerImpl_IsDirty(IMoniker* iface);
75 static HRESULT WINAPI URLMonikerImpl_Load(IMoniker* iface, IStream* pStm);
76 static HRESULT WINAPI URLMonikerImpl_Save(IMoniker* iface, IStream* pStm, BOOL fClearDirty);
77 static HRESULT WINAPI URLMonikerImpl_GetSizeMax(IMoniker* iface, ULARGE_INTEGER* pcbSize);
78
79 /* IMoniker prototype functions */
80 static HRESULT WINAPI URLMonikerImpl_BindToObject(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
81 static HRESULT WINAPI URLMonikerImpl_BindToStorage(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, REFIID riid, VOID** ppvResult);
82 static HRESULT WINAPI URLMonikerImpl_Reduce(IMoniker* iface,IBindCtx* pbc, DWORD dwReduceHowFar,IMoniker** ppmkToLeft, IMoniker** ppmkReduced);
83 static HRESULT WINAPI URLMonikerImpl_ComposeWith(IMoniker* iface,IMoniker* pmkRight,BOOL fOnlyIfNotGeneric, IMoniker** ppmkComposite);
84 static HRESULT WINAPI URLMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker);
85 static HRESULT WINAPI URLMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker);
86 static HRESULT WINAPI URLMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash);
87 static HRESULT WINAPI URLMonikerImpl_IsRunning(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, IMoniker* pmkNewlyRunning);
88 static HRESULT WINAPI URLMonikerImpl_GetTimeOfLastChange(IMoniker* iface, IBindCtx* pbc, IMoniker* pmkToLeft, FILETIME* pFileTime);
89 static HRESULT WINAPI URLMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk);
90 static HRESULT WINAPI URLMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther, IMoniker** ppmkPrefix);
91 static HRESULT WINAPI URLMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath);
92 static HRESULT WINAPI URLMonikerImpl_GetDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR *ppszDisplayName);
93 static HRESULT WINAPI URLMonikerImpl_ParseDisplayName(IMoniker* iface,IBindCtx* pbc, IMoniker* pmkToLeft, LPOLESTR pszDisplayName, ULONG* pchEaten, IMoniker** ppmkOut);
94 static HRESULT WINAPI URLMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys);
95
96 /* IBinding interface to moniker */
97 static HRESULT WINAPI URLMonikerImpl_IBinding_QueryInterface(IBinding* iface,REFIID riid,void** ppvObject);
98 static ULONG WINAPI URLMonikerImpl_IBinding_AddRef(IBinding* iface);
99 static ULONG WINAPI URLMonikerImpl_IBinding_Release(IBinding* iface);
100 static HRESULT WINAPI URLMonikerImpl_IBinding_Abort(IBinding* iface);
101 static HRESULT WINAPI URLMonikerImpl_IBinding_Suspend(IBinding* iface);
102 static HRESULT WINAPI URLMonikerImpl_IBinding_Resume(IBinding* iface);
103 static HRESULT WINAPI URLMonikerImpl_IBinding_SetPriority(IBinding* iface, LONG nPriority);
104 static HRESULT WINAPI URLMonikerImpl_IBinding_GetPriority(IBinding* iface, LONG* pnPriority);
105 static HRESULT WINAPI URLMonikerImpl_IBinding_GetBindResult(IBinding* iface, CLSID* pclsidProtocol, DWORD* pdwResult, LPOLESTR* pszResult, DWORD* pdwReserved);
106
107 /* Local function used by urlmoniker implementation */
108 static HRESULT URLMonikerImpl_Construct(URLMonikerImpl* iface, LPCOLESTR lpszURL);
109 static HRESULT URLMonikerImpl_Destroy(URLMonikerImpl* iface);
110
111 /********************************************************************************/
112 /* Virtual function table for the URLMonikerImpl class which  include IPersist,*/
113 /* IPersistStream and IMoniker functions.                                       */
114 static ICOM_VTABLE(IMoniker) VT_URLMonikerImpl =
115 {
116     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
117     URLMonikerImpl_QueryInterface,
118     URLMonikerImpl_AddRef,
119     URLMonikerImpl_Release,
120     URLMonikerImpl_GetClassID,
121     URLMonikerImpl_IsDirty,
122     URLMonikerImpl_Load,
123     URLMonikerImpl_Save,
124     URLMonikerImpl_GetSizeMax,
125     URLMonikerImpl_BindToObject,
126     URLMonikerImpl_BindToStorage,
127     URLMonikerImpl_Reduce,
128     URLMonikerImpl_ComposeWith,
129     URLMonikerImpl_Enum,
130     URLMonikerImpl_IsEqual,
131     URLMonikerImpl_Hash,
132     URLMonikerImpl_IsRunning,
133     URLMonikerImpl_GetTimeOfLastChange,
134     URLMonikerImpl_Inverse,
135     URLMonikerImpl_CommonPrefixWith,
136     URLMonikerImpl_RelativePathTo,
137     URLMonikerImpl_GetDisplayName,
138     URLMonikerImpl_ParseDisplayName,
139     URLMonikerImpl_IsSystemMoniker
140 };
141
142 static ICOM_VTABLE(IBinding) VTBinding_URLMonikerImpl =
143 {
144     ICOM_MSVTABLE_COMPAT_DummyRTTIVALUE
145     URLMonikerImpl_IBinding_QueryInterface,
146     URLMonikerImpl_IBinding_AddRef,
147     URLMonikerImpl_IBinding_Release,
148     URLMonikerImpl_IBinding_Abort,
149     URLMonikerImpl_IBinding_Suspend,
150     URLMonikerImpl_IBinding_Resume,
151     URLMonikerImpl_IBinding_SetPriority,
152     URLMonikerImpl_IBinding_GetPriority,
153     URLMonikerImpl_IBinding_GetBindResult
154 };
155
156
157 /*******************************************************************************
158  *        URLMoniker_QueryInterface
159  *******************************************************************************/
160 static HRESULT WINAPI URLMonikerImpl_QueryInterface(IMoniker* iface,REFIID riid,void** ppvObject)
161 {
162     ICOM_THIS(URLMonikerImpl,iface);
163
164     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
165
166     /* Perform a sanity check on the parameters.*/
167     if ( (This==0) || (ppvObject==0) )
168         return E_INVALIDARG;
169
170     /* Initialize the return parameter */
171     *ppvObject = 0;
172
173     /* Compare the riid with the interface IDs implemented by this object.*/
174     if (IsEqualIID(&IID_IUnknown, riid)      ||
175         IsEqualIID(&IID_IPersist, riid)      ||
176         IsEqualIID(&IID_IPersistStream,riid) ||
177         IsEqualIID(&IID_IMoniker, riid)
178        )
179         *ppvObject = iface;
180
181     /* Check that we obtained an interface.*/
182     if ((*ppvObject)==0)
183         return E_NOINTERFACE;
184
185     /* Query Interface always increases the reference count by one when it is successful */
186     URLMonikerImpl_AddRef(iface);
187
188     return S_OK;
189 }
190
191 /******************************************************************************
192  *        URLMoniker_AddRef
193  ******************************************************************************/
194 static ULONG WINAPI URLMonikerImpl_AddRef(IMoniker* iface)
195 {
196     ICOM_THIS(URLMonikerImpl,iface);
197
198     TRACE("(%p)\n",This);
199
200     return ++(This->ref);
201 }
202
203 /******************************************************************************
204  *        URLMoniker_Release
205  ******************************************************************************/
206 static ULONG WINAPI URLMonikerImpl_Release(IMoniker* iface)
207 {
208     ICOM_THIS(URLMonikerImpl,iface);
209
210     TRACE("(%p)\n",This);
211
212     This->ref--;
213
214     /* destroy the object if there's no more reference on it */
215     if (This->ref==0){
216
217         URLMonikerImpl_Destroy(This);
218
219         return 0;
220     }
221     return This->ref;
222 }
223
224 /******************************************************************************
225  *        URLMoniker_GetClassID
226  ******************************************************************************/
227 static HRESULT WINAPI URLMonikerImpl_GetClassID(IMoniker* iface,
228                                                 CLSID *pClassID)/* Pointer to CLSID of object */
229 {
230     ICOM_THIS(URLMonikerImpl,iface);
231
232     FIXME("(%p)->(%p): stub\n",This,pClassID);
233
234     if (pClassID==NULL)
235         return E_POINTER;
236
237     return E_NOTIMPL;
238 }
239
240 /******************************************************************************
241  *        URLMoniker_IsDirty
242  ******************************************************************************/
243 static HRESULT WINAPI URLMonikerImpl_IsDirty(IMoniker* iface)
244 {
245     ICOM_THIS(URLMonikerImpl,iface);
246     /* Note that the OLE-provided implementations of the IPersistStream::IsDirty
247        method in the OLE-provided moniker interfaces always return S_FALSE because
248        their internal state never changes. */
249
250     TRACE("(%p)\n",This);
251
252     return S_FALSE;
253 }
254
255 /******************************************************************************
256  *        URLMoniker_Load
257  ******************************************************************************/
258 static HRESULT WINAPI URLMonikerImpl_Load(IMoniker* iface,IStream* pStm)
259 {
260     ICOM_THIS(URLMonikerImpl,iface);
261
262     FIXME("(%p)->(%p): stub\n",This,pStm);
263
264     return E_NOTIMPL;
265 }
266
267 /******************************************************************************
268  *        URLMoniker_Save
269  ******************************************************************************/
270 static HRESULT WINAPI URLMonikerImpl_Save(IMoniker* iface,
271                                           IStream* pStm,/* pointer to the stream where the object is to be saved */
272                                           BOOL fClearDirty)/* Specifies whether to clear the dirty flag */
273 {
274     ICOM_THIS(URLMonikerImpl,iface);
275
276     FIXME("(%p)->(%p,%d): stub\n",This,pStm,fClearDirty);
277
278     return E_NOTIMPL;
279
280 }
281
282 /******************************************************************************
283  *        URLMoniker_GetSizeMax
284  ******************************************************************************/
285 static HRESULT WINAPI URLMonikerImpl_GetSizeMax(IMoniker* iface,
286                                                 ULARGE_INTEGER* pcbSize)/* Pointer to size of stream needed to save object */
287 {
288     ICOM_THIS(URLMonikerImpl,iface);
289
290     FIXME("(%p)->(%p): stub\n",This,pcbSize);
291
292     return E_NOTIMPL;
293 }
294
295 /******************************************************************************
296  *         URLMoniker_Construct (local function)
297  *******************************************************************************/
298 static HRESULT URLMonikerImpl_Construct(URLMonikerImpl* This, LPCOLESTR lpszURLName)
299 {
300     int sizeStr = strlenW(lpszURLName);
301
302     TRACE("(%p,%s)\n",This,debugstr_w(lpszURLName));
303     memset(This, 0, sizeof(*This));
304
305     /* Initialize the virtual fgunction table. */
306     This->lpvtbl1      = &VT_URLMonikerImpl;
307     This->lpvtbl2      = &VTBinding_URLMonikerImpl;
308     This->ref          = 0;
309
310     This->URLName=HeapAlloc(GetProcessHeap(),0,sizeof(WCHAR)*(sizeStr+1));
311
312     if (This->URLName==NULL)
313         return E_OUTOFMEMORY;
314
315     strcpyW(This->URLName,lpszURLName);
316
317     return S_OK;
318 }
319
320
321
322 /******************************************************************************
323  *        URLMoniker_Destroy (local function)
324  *******************************************************************************/
325 static HRESULT URLMonikerImpl_Destroy(URLMonikerImpl* This)
326 {
327     TRACE("(%p)\n",This);
328
329     if (This->URLName!=NULL)
330             HeapFree(GetProcessHeap(),0,This->URLName);
331
332     HeapFree(GetProcessHeap(),0,This);
333
334     return S_OK;
335 }
336
337 /******************************************************************************
338  *                  URLMoniker_BindToObject
339  ******************************************************************************/
340 static HRESULT WINAPI URLMonikerImpl_BindToObject(IMoniker* iface,
341                                                   IBindCtx* pbc,
342                                                   IMoniker* pmkToLeft,
343                                                   REFIID riid,
344                                                   VOID** ppvResult)
345 {
346     ICOM_THIS(URLMonikerImpl,iface);
347
348     *ppvResult=0;
349
350     FIXME("(%p)->(%p,%p,%s,%p): stub\n",This,pbc,pmkToLeft,debugstr_guid(riid),
351           ppvResult);
352
353     return E_NOTIMPL;
354 }
355
356 typedef struct {
357     enum {OnProgress, OnDataAvailable} callback;
358 } URLMON_CallbackData;
359
360
361 #if 0
362 static LRESULT CALLBACK URLMON_WndProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
363 {
364     return DefWindowProcA(hwnd, msg, wparam, lparam);
365 }
366
367 static void PostOnProgress(URLMonikerImpl *This, UINT progress, UINT maxprogress, DWORD status, LPCWSTR *str)
368 {
369 }
370
371 static void CALLBACK URLMON_InternetCallback(HINTERNET hinet, /*DWORD_PTR*/ DWORD context, DWORD status,
372                                              void *status_info, DWORD status_info_len)
373 {
374     URLMonikerImpl *This = (URLMonikerImpl *)context;
375     TRACE("handle %p this %p status %08lx\n", hinet, This, status);
376
377     if(This->filesize == -1) {
378         switch(status) {
379         case INTERNET_STATUS_RESOLVING_NAME:
380             PostOnProgess(This, 0, 0, BINDSTATUS_FINDINGRESOURCE, status_info);
381             break;
382         case INTERNET_STATUS_CONNECTING_TO_SERVER:
383             PostOnProgress(This, 0, 0, BINDSTATUS_CONNECTING, NULL);
384             break;
385         case INTERNET_STATUS_SENDING_REQUEST:
386             PostOnProgress(This, 0, 0, BINDSTATUS_SENDINGREQUEST, NULL);
387             break;
388         case INTERNET_REQUEST_COMPLETE:
389           {
390               DWORD len, lensz = sizeof(len);
391
392             HttpQueryInfoW(hrequest, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &len, &lensz, NULL);
393             TRACE("res = %ld gle = %08lx url len = %ld\n", hres, GetLastError(), len);
394             This->filesize = len;
395             break;
396           }
397         }
398     }
399
400     return;
401 }
402 #endif
403 /******************************************************************************
404  *        URLMoniker_BindToStorage
405  ******************************************************************************/
406 static HRESULT WINAPI URLMonikerImpl_BindToStorage(IMoniker* iface,
407                                                    IBindCtx* pbc,
408                                                    IMoniker* pmkToLeft,
409                                                    REFIID riid,
410                                                    VOID** ppvObject)
411 {
412     ICOM_THIS(URLMonikerImpl,iface);
413     HRESULT hres;
414     IBindStatusCallback *pbscb;
415     BINDINFO bi;
416     DWORD bindf;
417
418     FIXME("(%p)->(%p,%p,%s,%p): stub\n",This,pbc,pmkToLeft,debugstr_guid(riid),ppvObject);
419     if(pmkToLeft) {
420         FIXME("pmkToLeft != NULL\n");
421         return E_NOTIMPL;
422     }
423     if(!IsEqualIID(&IID_IStream, riid)) {
424         FIXME("unsupported iid\n");
425         return E_NOTIMPL;
426     }
427
428     /* FIXME This is a bad hack (tm).  We should clearly download to a temporary file.
429        We also need to implement IStream ourselves so that IStream_Read can return
430        E_PENDING */
431
432     hres = CreateStreamOnHGlobal(0, TRUE, (IStream**)ppvObject);
433
434
435     if(SUCCEEDED(hres)) {
436         TRACE("Created dummy stream...\n");
437
438         hres = IBindCtx_GetObjectParam(pbc, BSCBHolder, (IUnknown**)&pbscb);
439         if(SUCCEEDED(hres)) {
440             TRACE("Got IBindStatusCallback...\n");
441
442             memset(&bi, 0, sizeof(bi));
443             bi.cbSize = sizeof(bi);
444             bindf = 0;
445             hres = IBindStatusCallback_GetBindInfo(pbscb, &bindf, &bi);
446             if(SUCCEEDED(hres)) {
447                 URL_COMPONENTSW url;
448                 WCHAR *host, *path;
449                 DWORD len, lensz = sizeof(len), total_read = 0;
450                 LARGE_INTEGER last_read_pos;
451                 FORMATETC fmt;
452                 STGMEDIUM stg;
453
454                 TRACE("got bindinfo. bindf = %08lx extrainfo = %s bindinfof = %08lx bindverb = %08lx iid %s\n",
455                       bindf, debugstr_w(bi.szExtraInfo), bi.grfBindInfoF, bi.dwBindVerb, debugstr_guid(&bi.iid));
456                 hres = IBindStatusCallback_OnStartBinding(pbscb, 0, (IBinding*)&This->lpvtbl2);
457                 TRACE("OnStartBinding rets %08lx\n", hres);
458
459 #if 0
460                 if(!registered_wndclass) {
461                     WNDCLASSA urlmon_wndclass = {0, URLMON_WndProc,0, 0, URLMON_hInstance, 0, 0, 0, NULL, "URLMON_Callback_Window_Class"};
462                     RegisterClassA(&urlmon_wndclass);
463                     registered_wndclass = TRUE;
464                 }
465
466                 This->hwndCallback = CreateWindowA("URLMON_Callback_Window_Class", NULL, 0, 0, 0, 0, 0, 0, 0,
467                                                    URLMON_hInstance, NULL);
468
469 #endif
470                 memset(&url, 0, sizeof(url));
471                 url.dwStructSize = sizeof(url);
472                 url.dwSchemeLength = url.dwHostNameLength = url.dwUrlPathLength = 1;
473                 InternetCrackUrlW(This->URLName, 0, 0, &url);
474                 host = HeapAlloc(GetProcessHeap(), 0, (url.dwHostNameLength + 1) * sizeof(WCHAR));
475                 memcpy(host, url.lpszHostName, url.dwHostNameLength * sizeof(WCHAR));
476                 host[url.dwHostNameLength] = '\0';
477                 path = HeapAlloc(GetProcessHeap(), 0, (url.dwUrlPathLength + 1) * sizeof(WCHAR));
478                 memcpy(path, url.lpszUrlPath, url.dwUrlPathLength * sizeof(WCHAR));
479                 path[url.dwUrlPathLength] = '\0';
480
481                 This->hinternet = InternetOpenA("User Agent", 0, NULL, NULL, 0 /*INTERNET_FLAG_ASYNC*/);
482 /*              InternetSetStatusCallback(This->hinternet, URLMON_InternetCallback);*/
483
484                 This->hconnect = InternetConnectW(This->hinternet, host, INTERNET_DEFAULT_HTTP_PORT, NULL, NULL,
485                                             INTERNET_SERVICE_HTTP, 0, (DWORD)This);
486                 This->hrequest = HttpOpenRequestW(This->hconnect, NULL, path, NULL, NULL, NULL, 0, (DWORD)This);
487
488                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, 0x22, NULL);
489                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_FINDINGRESOURCE, NULL);
490                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_CONNECTING, NULL);
491                 hres = IBindStatusCallback_OnProgress(pbscb, 0, 0, BINDSTATUS_SENDINGREQUEST, NULL);
492                 hres = E_OUTOFMEMORY; /* FIXME */
493                 if(HttpSendRequestW(This->hrequest, NULL, 0, NULL, 0)) {
494
495                     len = 0;
496                     HttpQueryInfoW(This->hrequest, HTTP_QUERY_CONTENT_LENGTH | HTTP_QUERY_FLAG_NUMBER, &len, &lensz, NULL);
497
498                     TRACE("res = %ld gle = %08lx url len = %ld\n", hres, GetLastError(), len);
499
500                     last_read_pos.s.LowPart = last_read_pos.s.HighPart = 0;
501                     fmt.cfFormat = 0;
502                     fmt.ptd = NULL;
503                     fmt.dwAspect = 0;
504                     fmt.lindex = -1;
505                     fmt.tymed = TYMED_ISTREAM;
506                     stg.tymed = TYMED_ISTREAM;
507                     stg.u.pstm = *(IStream**)ppvObject;
508                     stg.pUnkForRelease = NULL;
509
510                     while(1) {
511                         char buf[4096];
512                         DWORD bufread;
513                         DWORD written;
514                         if(InternetReadFile(This->hrequest, buf, sizeof(buf), &bufread)) {
515                             TRACE("read %ld bytes %s...\n", bufread, debugstr_an(buf, 10));
516                             if(bufread == 0) break;
517                             IStream_Write(*(IStream**)ppvObject, buf, bufread, &written);
518                             total_read += bufread;
519                             IStream_Seek(*(IStream**)ppvObject, last_read_pos, STREAM_SEEK_SET, NULL);
520                             hres = IBindStatusCallback_OnProgress(pbscb, total_read, len, (total_read == bufread) ?
521                                                                   BINDSTATUS_BEGINDOWNLOADDATA :
522                                                                   BINDSTATUS_DOWNLOADINGDATA, NULL);
523                             hres = IBindStatusCallback_OnDataAvailable(pbscb,
524                                                                        (total_read == bufread) ? BSCF_FIRSTDATANOTIFICATION :
525                                                                        BSCF_INTERMEDIATEDATANOTIFICATION,
526                                                                        total_read, &fmt, &stg);
527                             last_read_pos.s.LowPart += bufread; /* FIXME */
528                         } else
529                             break;
530                     }
531                     hres = IBindStatusCallback_OnProgress(pbscb, total_read, len, BINDSTATUS_ENDDOWNLOADDATA, NULL);
532                     hres = IBindStatusCallback_OnDataAvailable(pbscb, BSCF_LASTDATANOTIFICATION, total_read, &fmt, &stg);
533                     TRACE("OnDataAvail rets %08lx\n", hres);
534                     hres = IBindStatusCallback_OnStopBinding(pbscb, S_OK, NULL);
535                     TRACE("OnStop rets %08lx\n", hres);
536                     hres = S_OK;
537                 }
538                 InternetCloseHandle(This->hrequest);
539                 InternetCloseHandle(This->hconnect);
540                 InternetCloseHandle(This->hinternet);
541             }
542         }
543     }
544     return hres;
545 }
546
547 /******************************************************************************
548  *        URLMoniker_Reduce
549  ******************************************************************************/
550 static HRESULT WINAPI URLMonikerImpl_Reduce(IMoniker* iface,
551                                             IBindCtx* pbc,
552                                             DWORD dwReduceHowFar,
553                                             IMoniker** ppmkToLeft,
554                                             IMoniker** ppmkReduced)
555 {
556     ICOM_THIS(URLMonikerImpl,iface);
557     FIXME("(%p)->(%p,%ld,%p,%p): stub\n",This,pbc,dwReduceHowFar,ppmkToLeft,ppmkReduced);
558
559     return E_NOTIMPL;
560 }
561
562 /******************************************************************************
563  *        URLMoniker_ComposeWith
564  ******************************************************************************/
565 static HRESULT WINAPI URLMonikerImpl_ComposeWith(IMoniker* iface,
566                                                  IMoniker* pmkRight,
567                                                  BOOL fOnlyIfNotGeneric,
568                                                  IMoniker** ppmkComposite)
569 {
570     ICOM_THIS(URLMonikerImpl,iface);
571     FIXME("(%p)->(%p,%d,%p): stub\n",This,pmkRight,fOnlyIfNotGeneric,ppmkComposite);
572
573     return E_NOTIMPL;
574 }
575
576 /******************************************************************************
577  *        URLMoniker_Enum
578  ******************************************************************************/
579 static HRESULT WINAPI URLMonikerImpl_Enum(IMoniker* iface,BOOL fForward, IEnumMoniker** ppenumMoniker)
580 {
581     ICOM_THIS(URLMonikerImpl,iface);
582     FIXME("(%p)->(%d,%p): stub\n",This,fForward,ppenumMoniker);
583
584     return E_NOTIMPL;
585 }
586
587 /******************************************************************************
588  *        URLMoniker_IsEqual
589  ******************************************************************************/
590 static HRESULT WINAPI URLMonikerImpl_IsEqual(IMoniker* iface,IMoniker* pmkOtherMoniker)
591 {
592     ICOM_THIS(URLMonikerImpl,iface);
593     FIXME("(%p)->(%p): stub\n",This,pmkOtherMoniker);
594
595     return E_NOTIMPL;
596 }
597
598
599 /******************************************************************************
600  *        URLMoniker_Hash
601  ******************************************************************************/
602 static HRESULT WINAPI URLMonikerImpl_Hash(IMoniker* iface,DWORD* pdwHash)
603 {
604     ICOM_THIS(URLMonikerImpl,iface);
605     FIXME("(%p)->(%p): stub\n",This,pdwHash);
606
607     return E_NOTIMPL;
608 }
609
610 /******************************************************************************
611  *        URLMoniker_IsRunning
612  ******************************************************************************/
613 static HRESULT WINAPI URLMonikerImpl_IsRunning(IMoniker* iface,
614                                                IBindCtx* pbc,
615                                                IMoniker* pmkToLeft,
616                                                IMoniker* pmkNewlyRunning)
617 {
618     ICOM_THIS(URLMonikerImpl,iface);
619     FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pmkNewlyRunning);
620
621     return E_NOTIMPL;
622 }
623
624 /******************************************************************************
625  *        URLMoniker_GetTimeOfLastChange
626  ******************************************************************************/
627 static HRESULT WINAPI URLMonikerImpl_GetTimeOfLastChange(IMoniker* iface,
628                                                          IBindCtx* pbc,
629                                                          IMoniker* pmkToLeft,
630                                                          FILETIME* pFileTime)
631 {
632     ICOM_THIS(URLMonikerImpl,iface);
633     FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,pFileTime);
634
635     return E_NOTIMPL;
636 }
637
638 /******************************************************************************
639  *        URLMoniker_Inverse
640  ******************************************************************************/
641 static HRESULT WINAPI URLMonikerImpl_Inverse(IMoniker* iface,IMoniker** ppmk)
642 {
643     ICOM_THIS(URLMonikerImpl,iface);
644     FIXME("(%p)->(%p): stub\n",This,ppmk);
645
646     return E_NOTIMPL;
647 }
648
649 /******************************************************************************
650  *        URLMoniker_CommonPrefixWith
651  ******************************************************************************/
652 static HRESULT WINAPI URLMonikerImpl_CommonPrefixWith(IMoniker* iface,IMoniker* pmkOther,IMoniker** ppmkPrefix)
653 {
654     ICOM_THIS(URLMonikerImpl,iface);
655     FIXME("(%p)->(%p,%p): stub\n",This,pmkOther,ppmkPrefix);
656
657     return E_NOTIMPL;
658 }
659
660 /******************************************************************************
661  *        URLMoniker_RelativePathTo
662  ******************************************************************************/
663 static HRESULT WINAPI URLMonikerImpl_RelativePathTo(IMoniker* iface,IMoniker* pmOther, IMoniker** ppmkRelPath)
664 {
665     ICOM_THIS(URLMonikerImpl,iface);
666     FIXME("(%p)->(%p,%p): stub\n",This,pmOther,ppmkRelPath);
667
668     return E_NOTIMPL;
669 }
670
671 /******************************************************************************
672  *        URLMoniker_GetDisplayName
673  ******************************************************************************/
674 static HRESULT WINAPI URLMonikerImpl_GetDisplayName(IMoniker* iface,
675                                                     IBindCtx* pbc,
676                                                     IMoniker* pmkToLeft,
677                                                     LPOLESTR *ppszDisplayName)
678 {
679     ICOM_THIS(URLMonikerImpl,iface);
680     FIXME("(%p)->(%p,%p,%p): stub\n",This,pbc,pmkToLeft,ppszDisplayName);
681
682     return E_NOTIMPL;
683 }
684
685 /******************************************************************************
686  *        URLMoniker_ParseDisplayName
687  ******************************************************************************/
688 static HRESULT WINAPI URLMonikerImpl_ParseDisplayName(IMoniker* iface,
689                                                       IBindCtx* pbc,
690                                                       IMoniker* pmkToLeft,
691                                                       LPOLESTR pszDisplayName,
692                                                       ULONG* pchEaten,
693                                                       IMoniker** ppmkOut)
694 {
695     ICOM_THIS(URLMonikerImpl,iface);
696     FIXME("(%p)->(%p,%p,%p,%p,%p): stub\n",This,pbc,pmkToLeft,pszDisplayName,pchEaten,ppmkOut);
697
698     return E_NOTIMPL;
699 }
700
701 /******************************************************************************
702  *        URLMoniker_IsSystemMoniker
703  ******************************************************************************/
704 static HRESULT WINAPI URLMonikerImpl_IsSystemMoniker(IMoniker* iface,DWORD* pwdMksys)
705 {
706     ICOM_THIS(URLMonikerImpl,iface);
707     FIXME("(%p)->(%p)\n",This,pwdMksys);
708
709     return E_NOTIMPL;
710 }
711
712 static HRESULT WINAPI URLMonikerImpl_IBinding_QueryInterface(IBinding* iface,REFIID riid,void** ppvObject)
713 {
714     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
715
716     TRACE("(%p)->(%s,%p)\n",This,debugstr_guid(riid),ppvObject);
717
718     /* Perform a sanity check on the parameters.*/
719     if ( (This==0) || (ppvObject==0) )
720         return E_INVALIDARG;
721
722     /* Initialize the return parameter */
723     *ppvObject = 0;
724
725     /* Compare the riid with the interface IDs implemented by this object.*/
726     if (IsEqualIID(&IID_IUnknown, riid) || IsEqualIID(&IID_IBinding, riid))
727         *ppvObject = iface;
728
729     /* Check that we obtained an interface.*/
730     if ((*ppvObject)==0)
731         return E_NOINTERFACE;
732
733     /* Query Interface always increases the reference count by one when it is successful */
734     URLMonikerImpl_IBinding_AddRef(iface);
735
736     return S_OK;
737
738 }
739
740 static ULONG WINAPI URLMonikerImpl_IBinding_AddRef(IBinding* iface)
741 {
742     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
743     TRACE("(%p)\n",This);
744
745     return URLMonikerImpl_AddRef((IMoniker*)This);
746 }
747
748 static ULONG WINAPI URLMonikerImpl_IBinding_Release(IBinding* iface)
749 {
750     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
751     TRACE("(%p)\n",This);
752
753     return URLMonikerImpl_Release((IMoniker*)This);
754 }
755
756 static HRESULT WINAPI URLMonikerImpl_IBinding_Abort(IBinding* iface)
757 {
758     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
759     FIXME("(%p): stub\n", This);
760
761     return E_NOTIMPL;
762 }
763
764 static HRESULT WINAPI URLMonikerImpl_IBinding_GetBindResult(IBinding* iface, CLSID* pclsidProtocol, DWORD* pdwResult, LPOLESTR* pszResult, DWORD* pdwReserved)
765 {
766     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
767     FIXME("(%p)->(%s, %p, %p, %p): stub\n", This, debugstr_guid(pclsidProtocol), pdwResult, pszResult, pdwReserved);
768
769     return E_NOTIMPL;
770 }
771
772 static HRESULT WINAPI URLMonikerImpl_IBinding_GetPriority(IBinding* iface, LONG* pnPriority)
773 {
774     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
775     FIXME("(%p)->(%p): stub\n", This, pnPriority);
776
777     return E_NOTIMPL;
778 }
779
780 static HRESULT WINAPI URLMonikerImpl_IBinding_Resume(IBinding* iface)
781 {
782     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
783     FIXME("(%p): stub\n", This);
784
785     return E_NOTIMPL;
786 }
787
788 static HRESULT WINAPI URLMonikerImpl_IBinding_SetPriority(IBinding* iface, LONG nPriority)
789 {
790     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
791     FIXME("(%p)->(%ld): stub\n", This, nPriority);
792
793     return E_NOTIMPL;
794 }
795
796 static HRESULT WINAPI URLMonikerImpl_IBinding_Suspend(IBinding* iface)
797 {
798     ICOM_THIS_MULTI(URLMonikerImpl, lpvtbl2, iface);
799     FIXME("(%p): stub\n", This);
800
801     return E_NOTIMPL;
802 }
803
804 /***********************************************************************
805  *           CreateAsyncBindCtxEx (URLMON.@)
806  *
807  * not implemented
808  *
809  */
810 HRESULT WINAPI CreateAsyncBindCtxEx(IBindCtx *ibind, DWORD options,
811     IBindStatusCallback *callback, IEnumFORMATETC *format, IBindCtx** pbind,
812     DWORD reserved)
813 {
814      FIXME("stub, returns failure\n");
815      return E_INVALIDARG;
816 }
817
818
819 /***********************************************************************
820  *           CreateURLMoniker (URLMON.@)
821  *
822  * Create a url moniker
823  *
824  * RETURNS
825  *    S_OK              success
826  *    E_OUTOFMEMORY     out of memory
827  *    MK_E_SYNTAX       not a valid url
828  *
829  */
830 HRESULT WINAPI CreateURLMoniker(IMoniker *pmkContext, LPCWSTR szURL, IMoniker **ppmk)
831 {
832     URLMonikerImpl *obj;
833     HRESULT hres;
834     IID iid = IID_IMoniker;
835
836     TRACE("(%p, %s, %p)\n", pmkContext, debugstr_w(szURL), ppmk);
837
838     if (NULL != pmkContext)
839         FIXME("Non-null pmkContext not implemented\n");
840
841     if(!(obj = HeapAlloc(GetProcessHeap(), 0, sizeof(*obj))))
842         return E_OUTOFMEMORY;
843
844     hres = URLMonikerImpl_Construct(obj, szURL);
845     if(SUCCEEDED(hres))
846         hres = URLMonikerImpl_QueryInterface((IMoniker*)obj, &iid, (void**)ppmk);
847     else
848         HeapFree(GetProcessHeap(), 0, obj);
849     return hres;
850 }
851
852
853 /***********************************************************************
854  *           CoInternetGetSession (URLMON.@)
855  *
856  * Creates a session and returns a pointer to an InternetSession interface.
857  *
858  * RETURNS
859  *    S_OK              success
860  *    E_OUTOFMEMORY     out of memory
861  *    E_INVALIDARG      invalid argument
862  *
863  */
864 HRESULT WINAPI CoInternetGetSession(DWORD dwSessionMode, /*IInternetSession*/void **ppIInternetSession, DWORD dwReserved)
865 {
866     FIXME("(%ld, %p, %ld): stub\n", dwSessionMode, ppIInternetSession, dwReserved);
867
868     if(dwSessionMode) {
869       ERR("dwSessionMode: %ld, must be zero\n", dwSessionMode);
870     }
871
872     if(dwReserved) {
873       ERR("dwReserved: %ld, must be zero\n", dwReserved);
874     }
875
876     *ppIInternetSession=NULL;
877     return E_OUTOFMEMORY;
878 }
879
880 /***********************************************************************
881  *           FindMimeFromData (URLMON.@)
882  *
883  * Determines the Multipurpose Internet Mail Extensions (MIME) type from the data provided.
884  *
885  */
886 HRESULT WINAPI FindMimeFromData(LPBC pBC, LPCWSTR pwzUrl, LPVOID pBuffer, 
887    DWORD cbSize, LPCWSTR pwzMimeProposed, DWORD dwMimeFlags,
888    LPWSTR* ppwzMimeOut, DWORD dwReserved)
889 {
890   FIXME("stub\n");
891   return E_OUTOFMEMORY;
892 }
893
894 /***********************************************************************
895  *           RegisterBindStatusCallback (URLMON.@)
896  *
897  * Register a bind status callback
898  *
899  * RETURNS
900  *    S_OK              success
901  *    E_INVALIDARG  invalid argument(s)
902  *    E_OUTOFMEMORY     out of memory
903  *
904  */
905 HRESULT WINAPI RegisterBindStatusCallback(
906     IBindCtx *pbc,
907     IBindStatusCallback *pbsc,
908     IBindStatusCallback **ppbscPrevious,
909     DWORD dwReserved)
910 {
911     IBindStatusCallback *prev;
912
913         TRACE("(%p,%p,%p,%lu)\n", pbc, pbsc, ppbscPrevious, dwReserved);
914
915     if (pbc == NULL || pbsc == NULL)
916         return E_INVALIDARG;
917
918     if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, BSCBHolder, (IUnknown **)&prev)))
919     {
920         IBindCtx_RevokeObjectParam(pbc, BSCBHolder);
921         if (ppbscPrevious)
922             *ppbscPrevious = prev;
923         else
924             IBindStatusCallback_Release(prev);
925     }
926
927         return IBindCtx_RegisterObjectParam(pbc, BSCBHolder, (IUnknown *)pbsc);
928 }
929
930 /***********************************************************************
931  *           RevokeBindStatusCallback (URLMON.@)
932  *
933  * Unregister a bind status callback
934  *
935  * RETURNS
936  *    S_OK              success
937  *    E_INVALIDARG  invalid argument(s)
938  *    E_FAIL pbsc wasn't registered with pbc
939  *
940  */
941 HRESULT WINAPI RevokeBindStatusCallback(
942     IBindCtx *pbc,
943     IBindStatusCallback *pbsc)
944 {
945     IBindStatusCallback *callback;
946     HRESULT hr = E_FAIL;
947
948         TRACE("(%p,%p)\n", pbc, pbsc);
949
950     if (pbc == NULL || pbsc == NULL)
951         return E_INVALIDARG;
952
953     if (SUCCEEDED(IBindCtx_GetObjectParam(pbc, BSCBHolder, (IUnknown **)&callback)))
954     {
955         if (callback == pbsc)
956         {
957             IBindCtx_RevokeObjectParam(pbc, BSCBHolder);
958             hr = S_OK;
959         }
960         IBindStatusCallback_Release(pbsc);
961     }
962
963     return hr;
964 }
965
966 /***********************************************************************
967  *           ReleaseBindInfo (URLMON.@)
968  *
969  * Releases the resources used by the specified BINDINFO structure   
970  *
971  */
972 void WINAPI ReleaseBindInfo(BINDINFO* pbindinfo)
973 {
974     FIXME("stub\n");
975 }