push 52d6b63ba2f2d4f9b02b6b922d27bff05a60596f
[wine/hacks.git] / dlls / shdocvw / intshcut.c
blobb389ec3727a01d9af44c3856951b9d398807c558
1 /*
2 * Copyright 2008 Damjan Jovanovic
4 * ShellLink's barely documented cousin that handles URLs.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
22 * TODO:
23 * Implement the IShellLinkA/W interfaces
24 * Handle the SetURL flags
25 * Implement any other interfaces? Does any software actually use them?
27 * The installer for the Zuma Deluxe Popcap game is good for testing.
30 #include "wine/debug.h"
31 #include "shdocvw.h"
32 #include "objidl.h"
33 #include "shobjidl.h"
34 #include "intshcut.h"
36 WINE_DEFAULT_DEBUG_CHANNEL(shdocvw);
38 typedef struct
40 IUniformResourceLocatorA uniformResourceLocatorA;
41 IUniformResourceLocatorW uniformResourceLocatorW;
42 IPersistFile persistFile;
44 LONG refCount;
46 WCHAR *url;
47 BOOLEAN isDirty;
48 LPOLESTR currentFile;
49 } InternetShortcut;
51 /* utility functions */
53 static inline InternetShortcut* impl_from_IUniformResourceLocatorA(IUniformResourceLocatorA *iface)
55 return (InternetShortcut*)((char*)iface - FIELD_OFFSET(InternetShortcut, uniformResourceLocatorA));
58 static inline InternetShortcut* impl_from_IUniformResourceLocatorW(IUniformResourceLocatorW *iface)
60 return (InternetShortcut*)((char*)iface - FIELD_OFFSET(InternetShortcut, uniformResourceLocatorW));
63 static inline InternetShortcut* impl_from_IPersistFile(IPersistFile *iface)
65 return (InternetShortcut*)((char*)iface - FIELD_OFFSET(InternetShortcut, persistFile));
68 static BOOL run_winemenubuilder( const WCHAR *args )
70 static const WCHAR menubuilder[] = {'\\','w','i','n','e','m','e','n','u','b','u','i','l','d','e','r','.','e','x','e',0};
71 LONG len;
72 LPWSTR buffer;
73 STARTUPINFOW si;
74 PROCESS_INFORMATION pi;
75 BOOL ret;
76 WCHAR app[MAX_PATH];
78 GetSystemDirectoryW( app, MAX_PATH - sizeof(menubuilder)/sizeof(WCHAR) );
79 strcatW( app, menubuilder );
81 len = (strlenW( app ) + strlenW( args ) + 1) * sizeof(WCHAR);
82 buffer = heap_alloc( len );
83 if( !buffer )
84 return FALSE;
86 strcpyW( buffer, app );
87 strcatW( buffer, args );
89 TRACE("starting %s\n",debugstr_w(buffer));
91 memset(&si, 0, sizeof(si));
92 si.cb = sizeof(si);
94 ret = CreateProcessW( app, buffer, NULL, NULL, FALSE, 0, NULL, NULL, &si, &pi );
96 heap_free( buffer );
98 if (ret)
100 CloseHandle( pi.hProcess );
101 CloseHandle( pi.hThread );
104 return ret;
107 static BOOL StartLinkProcessor( LPCOLESTR szLink )
109 static const WCHAR szFormat[] = { ' ','-','w',' ','-','u',' ','"','%','s','"',0 };
110 LONG len;
111 LPWSTR buffer;
112 BOOL ret;
114 len = sizeof(szFormat) + lstrlenW( szLink ) * sizeof(WCHAR);
115 buffer = heap_alloc( len );
116 if( !buffer )
117 return FALSE;
119 wsprintfW( buffer, szFormat, szLink );
120 ret = run_winemenubuilder( buffer );
121 heap_free( buffer );
122 return ret;
125 /* interface functions */
127 static HRESULT Unknown_QueryInterface(InternetShortcut *This, REFIID riid, PVOID *ppvObject)
129 TRACE("(%p, %s, %p)\n", This, debugstr_guid(riid), ppvObject);
130 *ppvObject = NULL;
131 if (IsEqualGUID(&IID_IUnknown, riid))
132 *ppvObject = &This->uniformResourceLocatorA;
133 else if (IsEqualGUID(&IID_IUniformResourceLocatorA, riid))
134 *ppvObject = &This->uniformResourceLocatorA;
135 else if (IsEqualGUID(&IID_IUniformResourceLocatorW, riid))
136 *ppvObject = &This->uniformResourceLocatorW;
137 else if (IsEqualGUID(&IID_IPersistFile, riid))
138 *ppvObject = &This->persistFile;
139 else if (IsEqualGUID(&IID_IShellLinkA, riid))
141 FIXME("The IShellLinkA interface is not yet supported by InternetShortcut\n");
142 return E_NOINTERFACE;
144 else if (IsEqualGUID(&IID_IShellLinkW, riid))
146 FIXME("The IShellLinkW interface is not yet supported by InternetShortcut\n");
147 return E_NOINTERFACE;
149 else
151 FIXME("Interface with GUID %s not yet implemented by InternetShortcut\n", debugstr_guid(riid));
152 return E_NOINTERFACE;
154 IUnknown_AddRef((IUnknown*)*ppvObject);
155 return S_OK;
158 static ULONG Unknown_AddRef(InternetShortcut *This)
160 TRACE("(%p)\n", This);
161 return InterlockedIncrement(&This->refCount);
164 static ULONG Unknown_Release(InternetShortcut *This)
166 ULONG count;
167 TRACE("(%p)\n", This);
168 count = InterlockedDecrement(&This->refCount);
169 if (count == 0)
171 CoTaskMemFree(This->url);
172 CoTaskMemFree(This->currentFile);
173 heap_free(This);
174 SHDOCVW_UnlockModule();
176 return count;
179 static HRESULT WINAPI UniformResourceLocatorW_QueryInterface(IUniformResourceLocatorW *url, REFIID riid, PVOID *ppvObject)
181 InternetShortcut *This = impl_from_IUniformResourceLocatorW(url);
182 TRACE("(%p, %s, %p)\n", url, debugstr_guid(riid), ppvObject);
183 return Unknown_QueryInterface(This, riid, ppvObject);
186 static ULONG WINAPI UniformResourceLocatorW_AddRef(IUniformResourceLocatorW *url)
188 InternetShortcut *This = impl_from_IUniformResourceLocatorW(url);
189 TRACE("(%p)\n", url);
190 return Unknown_AddRef(This);
193 static ULONG WINAPI UniformResourceLocatorW_Release(IUniformResourceLocatorW *url)
195 InternetShortcut *This = impl_from_IUniformResourceLocatorW(url);
196 TRACE("(%p)\n", url);
197 return Unknown_Release(This);
200 static HRESULT WINAPI UniformResourceLocatorW_SetUrl(IUniformResourceLocatorW *url, LPCWSTR pcszURL, DWORD dwInFlags)
202 WCHAR *newURL = NULL;
203 InternetShortcut *This = impl_from_IUniformResourceLocatorW(url);
204 TRACE("(%p, %s, 0x%x)\n", url, debugstr_w(pcszURL), dwInFlags);
205 if (dwInFlags != 0)
206 FIXME("ignoring unsupported flags 0x%x\n", dwInFlags);
207 if (pcszURL != NULL)
209 newURL = co_strdupW(pcszURL);
210 if (newURL == NULL)
211 return E_OUTOFMEMORY;
213 CoTaskMemFree(This->url);
214 This->url = newURL;
215 This->isDirty = TRUE;
216 return S_OK;
219 static HRESULT WINAPI UniformResourceLocatorW_GetUrl(IUniformResourceLocatorW *url, LPWSTR *ppszURL)
221 HRESULT hr = S_OK;
222 InternetShortcut *This = impl_from_IUniformResourceLocatorW(url);
223 TRACE("(%p, %p)\n", url, ppszURL);
224 if (This->url == NULL)
225 *ppszURL = NULL;
226 else
228 *ppszURL = co_strdupW(This->url);
229 if (*ppszURL == NULL)
230 hr = E_OUTOFMEMORY;
232 return hr;
235 static HRESULT WINAPI UniformResourceLocatorW_InvokeCommand(IUniformResourceLocatorW *url, PURLINVOKECOMMANDINFOW pCommandInfo)
237 FIXME("(%p, %p): stub\n", url, pCommandInfo);
238 return E_NOTIMPL;
241 static HRESULT WINAPI UniformResourceLocatorA_QueryInterface(IUniformResourceLocatorA *url, REFIID riid, PVOID *ppvObject)
243 InternetShortcut *This = impl_from_IUniformResourceLocatorA(url);
244 TRACE("(%p, %s, %p)\n", url, debugstr_guid(riid), ppvObject);
245 return Unknown_QueryInterface(This, riid, ppvObject);
248 static ULONG WINAPI UniformResourceLocatorA_AddRef(IUniformResourceLocatorA *url)
250 InternetShortcut *This = impl_from_IUniformResourceLocatorA(url);
251 TRACE("(%p)\n", url);
252 return Unknown_AddRef(This);
255 static ULONG WINAPI UniformResourceLocatorA_Release(IUniformResourceLocatorA *url)
257 InternetShortcut *This = impl_from_IUniformResourceLocatorA(url);
258 TRACE("(%p)\n", url);
259 return Unknown_Release(This);
262 static HRESULT WINAPI UniformResourceLocatorA_SetUrl(IUniformResourceLocatorA *url, LPCSTR pcszURL, DWORD dwInFlags)
264 WCHAR *newURL = NULL;
265 InternetShortcut *This = impl_from_IUniformResourceLocatorA(url);
266 TRACE("(%p, %s, 0x%x)\n", url, debugstr_a(pcszURL), dwInFlags);
267 if (dwInFlags != 0)
268 FIXME("ignoring unsupported flags 0x%x\n", dwInFlags);
269 if (pcszURL != NULL)
271 newURL = co_strdupAtoW(pcszURL);
272 if (newURL == NULL)
273 return E_OUTOFMEMORY;
275 CoTaskMemFree(This->url);
276 This->url = newURL;
277 This->isDirty = TRUE;
278 return S_OK;
281 static HRESULT WINAPI UniformResourceLocatorA_GetUrl(IUniformResourceLocatorA *url, LPSTR *ppszURL)
283 HRESULT hr = S_OK;
284 InternetShortcut *This = impl_from_IUniformResourceLocatorA(url);
285 TRACE("(%p, %p)\n", url, ppszURL);
286 if (This->url == NULL)
287 *ppszURL = NULL;
288 else
290 *ppszURL = co_strdupWtoA(This->url);
291 if (*ppszURL == NULL)
292 hr = E_OUTOFMEMORY;
294 return hr;
297 static HRESULT WINAPI UniformResourceLocatorA_InvokeCommand(IUniformResourceLocatorA *url, PURLINVOKECOMMANDINFOA pCommandInfo)
299 FIXME("(%p, %p): stub\n", url, pCommandInfo);
300 return E_NOTIMPL;
303 static HRESULT WINAPI PersistFile_QueryInterface(IPersistFile *pFile, REFIID riid, PVOID *ppvObject)
305 InternetShortcut *This = impl_from_IPersistFile(pFile);
306 TRACE("(%p, %s, %p)\n", pFile, debugstr_guid(riid), ppvObject);
307 return Unknown_QueryInterface(This, riid, ppvObject);
310 static ULONG WINAPI PersistFile_AddRef(IPersistFile *pFile)
312 InternetShortcut *This = impl_from_IPersistFile(pFile);
313 TRACE("(%p)\n", pFile);
314 return Unknown_AddRef(This);
317 static ULONG WINAPI PersistFile_Release(IPersistFile *pFile)
319 InternetShortcut *This = impl_from_IPersistFile(pFile);
320 TRACE("(%p)\n", pFile);
321 return Unknown_Release(This);
324 static HRESULT WINAPI PersistFile_GetClassID(IPersistFile *pFile, CLSID *pClassID)
326 TRACE("(%p, %p)\n", pFile, pClassID);
327 *pClassID = CLSID_InternetShortcut;
328 return S_OK;
331 static HRESULT WINAPI PersistFile_IsDirty(IPersistFile *pFile)
333 InternetShortcut *This = impl_from_IPersistFile(pFile);
334 TRACE("(%p)\n", pFile);
335 return This->isDirty ? S_OK : S_FALSE;
338 static HRESULT WINAPI PersistFile_Load(IPersistFile *pFile, LPCOLESTR pszFileName, DWORD dwMode)
340 WCHAR str_header[] = {'I','n','t','e','r','n','e','t','S','h','o','r','t','c','u','t',0};
341 WCHAR str_URL[] = {'U','R','L',0};
342 WCHAR *filename = NULL;
343 HRESULT hr;
344 InternetShortcut *This = impl_from_IPersistFile(pFile);
345 TRACE("(%p, %s, 0x%x)\n", pFile, debugstr_w(pszFileName), dwMode);
346 if (dwMode != 0)
347 FIXME("ignoring unimplemented mode 0x%x\n", dwMode);
348 filename = co_strdupW(pszFileName);
349 if (filename != NULL)
351 DWORD len = 128;
352 DWORD r;
353 WCHAR *url = CoTaskMemAlloc(len*sizeof(WCHAR));
354 if (url != NULL)
356 r = GetPrivateProfileStringW(str_header, str_URL, NULL, url, len, pszFileName);
357 while (r == len-1)
359 CoTaskMemFree(url);
360 len *= 2;
361 url = CoTaskMemAlloc(len);
362 if (url == NULL)
363 break;
364 r = GetPrivateProfileStringW(str_header, str_URL, NULL, url, len, pszFileName);
366 if (r == 0)
367 hr = E_FAIL;
368 else if (url != NULL)
370 CoTaskMemFree(This->currentFile);
371 This->currentFile = filename;
372 CoTaskMemFree(This->url);
373 This->url = url;
374 This->isDirty = FALSE;
375 return S_OK;
377 else
378 hr = E_OUTOFMEMORY;
379 CoTaskMemFree(url);
381 else
382 hr = E_OUTOFMEMORY;
383 CoTaskMemFree(filename);
385 else
386 hr = E_OUTOFMEMORY;
387 return hr;
390 static HRESULT WINAPI PersistFile_Save(IPersistFile *pFile, LPCOLESTR pszFileName, BOOL fRemember)
392 HRESULT hr = S_OK;
393 INT len;
394 CHAR *url;
395 InternetShortcut *This = impl_from_IPersistFile(pFile);
397 TRACE("(%p, %s, %d)\n", pFile, debugstr_w(pszFileName), fRemember);
399 if (pszFileName != NULL && fRemember)
401 LPOLESTR oldFile = This->currentFile;
402 This->currentFile = co_strdupW(pszFileName);
403 if (This->currentFile == NULL)
405 This->currentFile = oldFile;
406 return E_OUTOFMEMORY;
408 CoTaskMemFree(oldFile);
410 if (This->url == NULL)
411 return E_FAIL;
413 /* Windows seems to always write:
414 * ASCII "[InternetShortcut]" headers
415 * ASCII names in "name=value" pairs
416 * An ASCII (probably UTF8?) value in "URL=..."
418 len = WideCharToMultiByte(CP_UTF8, 0, This->url, -1, NULL, 0, 0, 0);
419 url = heap_alloc(len);
420 if (url != NULL)
422 HANDLE file;
423 WideCharToMultiByte(CP_UTF8, 0, This->url, -1, url, len, 0, 0);
424 file = CreateFileW(pszFileName, GENERIC_WRITE, FILE_SHARE_READ, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
425 if (file != INVALID_HANDLE_VALUE)
427 DWORD bytesWritten;
428 char str_header[] = "[InternetShortcut]";
429 char str_URL[] = "URL=";
430 char str_eol[] = "\r\n";
432 WriteFile(file, str_header, lstrlenA(str_header), &bytesWritten, NULL);
433 WriteFile(file, str_eol, lstrlenA(str_eol), &bytesWritten, NULL);
434 WriteFile(file, str_URL, lstrlenA(str_URL), &bytesWritten, NULL);
435 WriteFile(file, url, lstrlenA(url), &bytesWritten, NULL);
436 WriteFile(file, str_eol, lstrlenA(str_eol), &bytesWritten, NULL);
437 CloseHandle(file);
438 if (pszFileName == NULL || fRemember)
439 This->isDirty = FALSE;
440 StartLinkProcessor(pszFileName);
442 else
443 hr = E_FAIL;
444 heap_free(url);
446 else
447 hr = E_OUTOFMEMORY;
449 return hr;
452 static HRESULT WINAPI PersistFile_SaveCompleted(IPersistFile *pFile, LPCOLESTR pszFileName)
454 FIXME("(%p, %p): stub\n", pFile, pszFileName);
455 return E_NOTIMPL;
458 static HRESULT WINAPI PersistFile_GetCurFile(IPersistFile *pFile, LPOLESTR *ppszFileName)
460 HRESULT hr = S_OK;
461 InternetShortcut *This = impl_from_IPersistFile(pFile);
462 TRACE("(%p, %p)\n", pFile, ppszFileName);
463 if (This->currentFile == NULL)
464 *ppszFileName = NULL;
465 else
467 *ppszFileName = co_strdupW(This->currentFile);
468 if (*ppszFileName == NULL)
469 hr = E_OUTOFMEMORY;
471 return hr;
476 static const IUniformResourceLocatorWVtbl uniformResourceLocatorWVtbl = {
477 UniformResourceLocatorW_QueryInterface,
478 UniformResourceLocatorW_AddRef,
479 UniformResourceLocatorW_Release,
480 UniformResourceLocatorW_SetUrl,
481 UniformResourceLocatorW_GetUrl,
482 UniformResourceLocatorW_InvokeCommand
485 static const IUniformResourceLocatorAVtbl uniformResourceLocatorAVtbl = {
486 UniformResourceLocatorA_QueryInterface,
487 UniformResourceLocatorA_AddRef,
488 UniformResourceLocatorA_Release,
489 UniformResourceLocatorA_SetUrl,
490 UniformResourceLocatorA_GetUrl,
491 UniformResourceLocatorA_InvokeCommand
494 static const IPersistFileVtbl persistFileVtbl = {
495 PersistFile_QueryInterface,
496 PersistFile_AddRef,
497 PersistFile_Release,
498 PersistFile_GetClassID,
499 PersistFile_IsDirty,
500 PersistFile_Load,
501 PersistFile_Save,
502 PersistFile_SaveCompleted,
503 PersistFile_GetCurFile
506 HRESULT InternetShortcut_Create(IUnknown *pOuter, REFIID riid, void **ppv)
508 InternetShortcut *This;
509 HRESULT hr;
511 TRACE("(%p, %s, %p)\n", pOuter, debugstr_guid(riid), ppv);
513 *ppv = NULL;
515 if(pOuter)
516 return CLASS_E_NOAGGREGATION;
518 This = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(InternetShortcut));
519 if (This)
521 This->uniformResourceLocatorA.lpVtbl = &uniformResourceLocatorAVtbl;
522 This->uniformResourceLocatorW.lpVtbl = &uniformResourceLocatorWVtbl;
523 This->persistFile.lpVtbl = &persistFileVtbl;
524 This->refCount = 0;
525 hr = Unknown_QueryInterface(This, riid, ppv);
526 if (SUCCEEDED(hr))
527 SHDOCVW_LockModule();
528 else
529 heap_free(This);
530 return hr;
532 else
533 return E_OUTOFMEMORY;