wshom.ocx: Implement IWshCollection::Item() for public desktop case.
[wine] / dlls / wshom.ocx / shell.c
1 /*
2  * Copyright 2011 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 "wshom_private.h"
20 #include "wshom.h"
21
22 #include "shlobj.h"
23
24 #include "wine/debug.h"
25 #include "wine/unicode.h"
26
27 WINE_DEFAULT_DEBUG_CHANNEL(wshom);
28
29 const char *debugstr_variant(const VARIANT *v)
30 {
31     if(!v)
32         return "(null)";
33
34     switch(V_VT(v)) {
35     case VT_EMPTY:
36         return "{VT_EMPTY}";
37     case VT_NULL:
38         return "{VT_NULL}";
39     case VT_I4:
40         return wine_dbg_sprintf("{VT_I4: %d}", V_I4(v));
41     case VT_R8:
42         return wine_dbg_sprintf("{VT_R8: %lf}", V_R8(v));
43     case VT_BSTR:
44         return wine_dbg_sprintf("{VT_BSTR: %s}", debugstr_w(V_BSTR(v)));
45     case VT_DISPATCH:
46         return wine_dbg_sprintf("{VT_DISPATCH: %p}", V_DISPATCH(v));
47     case VT_BOOL:
48         return wine_dbg_sprintf("{VT_BOOL: %x}", V_BOOL(v));
49     case VT_UNKNOWN:
50         return wine_dbg_sprintf("{VT_UNKNOWN: %p}", V_UNKNOWN(v));
51     case VT_UINT:
52         return wine_dbg_sprintf("{VT_UINT: %u}", V_UINT(v));
53     case VT_BSTR|VT_BYREF:
54         return wine_dbg_sprintf("{VT_BSTR|VT_BYREF: ptr %p, data %s}",
55             V_BSTRREF(v), debugstr_w(V_BSTRREF(v) ? *V_BSTRREF(v) : NULL));
56     default:
57         return wine_dbg_sprintf("{vt %d}", V_VT(v));
58     }
59 }
60
61 static IWshShell3 WshShell3;
62
63 typedef struct
64 {
65     IWshCollection IWshCollection_iface;
66     LONG ref;
67 } WshCollection;
68
69 static inline WshCollection *impl_from_IWshCollection( IWshCollection *iface )
70 {
71     return CONTAINING_RECORD(iface, WshCollection, IWshCollection_iface);
72 }
73
74 static HRESULT WINAPI WshCollection_QueryInterface(IWshCollection *iface, REFIID riid, void **ppv)
75 {
76     WshCollection *This = impl_from_IWshCollection(iface);
77
78     TRACE("(%p)->(%s, %p)\n", This, debugstr_guid(riid), ppv);
79
80     if (IsEqualGUID(riid, &IID_IUnknown)  ||
81         IsEqualGUID(riid, &IID_IDispatch) ||
82         IsEqualGUID(riid, &IID_IWshCollection))
83     {
84         *ppv = iface;
85     }else {
86         FIXME("Unknown iface %s\n", debugstr_guid(riid));
87         *ppv = NULL;
88         return E_NOINTERFACE;
89     }
90
91     IUnknown_AddRef((IUnknown*)*ppv);
92     return S_OK;
93 }
94
95 static ULONG WINAPI WshCollection_AddRef(IWshCollection *iface)
96 {
97     WshCollection *This = impl_from_IWshCollection(iface);
98     LONG ref = InterlockedIncrement(&This->ref);
99     TRACE("(%p) ref = %d\n", This, ref);
100     return ref;
101 }
102
103 static ULONG WINAPI WshCollection_Release(IWshCollection *iface)
104 {
105     WshCollection *This = impl_from_IWshCollection(iface);
106     LONG ref = InterlockedDecrement(&This->ref);
107     TRACE("(%p) ref = %d\n", This, ref);
108
109     if (!ref)
110         HeapFree(GetProcessHeap(), 0, This);
111
112     return ref;
113 }
114
115 static HRESULT WINAPI WshCollection_GetTypeInfoCount(IWshCollection *iface, UINT *pctinfo)
116 {
117     WshCollection *This = impl_from_IWshCollection(iface);
118     TRACE("(%p)->(%p)\n", This, pctinfo);
119     *pctinfo = 1;
120     return S_OK;
121 }
122
123 static HRESULT WINAPI WshCollection_GetTypeInfo(IWshCollection *iface, UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
124 {
125     WshCollection *This = impl_from_IWshCollection(iface);
126     TRACE("(%p)->(%u %u %p)\n", This, iTInfo, lcid, ppTInfo);
127     return get_typeinfo(IWshCollection_tid, ppTInfo);
128 }
129
130 static HRESULT WINAPI WshCollection_GetIDsOfNames(IWshCollection *iface, REFIID riid, LPOLESTR *rgszNames,
131         UINT cNames, LCID lcid, DISPID *rgDispId)
132 {
133     WshCollection *This = impl_from_IWshCollection(iface);
134     ITypeInfo *typeinfo;
135     HRESULT hr;
136
137     TRACE("(%p)->(%s %p %u %u %p)\n", This, debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
138
139     hr = get_typeinfo(IWshCollection_tid, &typeinfo);
140     if(SUCCEEDED(hr))
141     {
142         hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
143         ITypeInfo_Release(typeinfo);
144     }
145
146     return hr;
147 }
148
149 static HRESULT WINAPI WshCollection_Invoke(IWshCollection *iface, DISPID dispIdMember, REFIID riid, LCID lcid,
150         WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
151 {
152     WshCollection *This = impl_from_IWshCollection(iface);
153     ITypeInfo *typeinfo;
154     HRESULT hr;
155
156     TRACE("(%p)->(%d %s %d %d %p %p %p %p)\n", This, dispIdMember, debugstr_guid(riid),
157           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
158
159     hr = get_typeinfo(IWshCollection_tid, &typeinfo);
160     if(SUCCEEDED(hr))
161     {
162         hr = ITypeInfo_Invoke(typeinfo, &This->IWshCollection_iface, dispIdMember, wFlags,
163                 pDispParams, pVarResult, pExcepInfo, puArgErr);
164         ITypeInfo_Release(typeinfo);
165     }
166
167     return hr;
168 }
169
170 static HRESULT WINAPI WshCollection_Item(IWshCollection *iface, VARIANT *index, VARIANT *value)
171 {
172     WshCollection *This = impl_from_IWshCollection(iface);
173     static const WCHAR allusersdesktopW[] = {'A','l','l','U','s','e','r','s','D','e','s','k','t','o','p',0};
174     static const WCHAR desktopW[] = {'D','e','s','k','t','o','p',0};
175     PIDLIST_ABSOLUTE pidl;
176     WCHAR pathW[MAX_PATH];
177     int kind = 0;
178     BSTR folder;
179     HRESULT hr;
180
181     TRACE("(%p)->(%s %p)\n", This, debugstr_variant(index), value);
182
183     if (V_VT(index) != VT_BSTR)
184     {
185         FIXME("only BSTR index supported, got %d\n", V_VT(index));
186         return E_NOTIMPL;
187     }
188
189     folder = V_BSTR(index);
190     if (!strcmpiW(folder, desktopW))
191         kind = CSIDL_DESKTOP;
192     else if (!strcmpiW(folder, allusersdesktopW))
193         kind = CSIDL_COMMON_DESKTOPDIRECTORY;
194     else
195     {
196         FIXME("folder kind %s not supported\n", debugstr_w(folder));
197         return E_NOTIMPL;
198     }
199
200     hr = SHGetSpecialFolderLocation(NULL, kind, &pidl);
201     if (hr != S_OK) return hr;
202
203     if (SHGetPathFromIDListW(pidl, pathW))
204     {
205         V_VT(value) = VT_BSTR;
206         V_BSTR(value) = SysAllocString(pathW);
207         hr = V_BSTR(value) ? S_OK : E_OUTOFMEMORY;
208     }
209     else
210         hr = E_FAIL;
211
212     CoTaskMemFree(pidl);
213
214     return hr;
215 }
216
217 static HRESULT WINAPI WshCollection_Count(IWshCollection *iface, LONG *count)
218 {
219     WshCollection *This = impl_from_IWshCollection(iface);
220     FIXME("(%p)->(%p): stub\n", This, count);
221     return E_NOTIMPL;
222 }
223
224 static HRESULT WINAPI WshCollection_get_length(IWshCollection *iface, LONG *count)
225 {
226     WshCollection *This = impl_from_IWshCollection(iface);
227     FIXME("(%p)->(%p): stub\n", This, count);
228     return E_NOTIMPL;
229 }
230
231 static HRESULT WINAPI WshCollection__NewEnum(IWshCollection *iface, IUnknown *Enum)
232 {
233     WshCollection *This = impl_from_IWshCollection(iface);
234     FIXME("(%p)->(%p): stub\n", This, Enum);
235     return E_NOTIMPL;
236 }
237
238 static const IWshCollectionVtbl WshCollectionVtbl = {
239     WshCollection_QueryInterface,
240     WshCollection_AddRef,
241     WshCollection_Release,
242     WshCollection_GetTypeInfoCount,
243     WshCollection_GetTypeInfo,
244     WshCollection_GetIDsOfNames,
245     WshCollection_Invoke,
246     WshCollection_Item,
247     WshCollection_Count,
248     WshCollection_get_length,
249     WshCollection__NewEnum
250 };
251
252 static HRESULT WshCollection_Create(IWshCollection **collection)
253 {
254     WshCollection *This;
255
256     This = HeapAlloc(GetProcessHeap(), 0, sizeof(*This));
257     if (!This) return E_OUTOFMEMORY;
258
259     This->IWshCollection_iface.lpVtbl = &WshCollectionVtbl;
260     This->ref = 1;
261
262     *collection = &This->IWshCollection_iface;
263
264     return S_OK;
265 }
266
267 static HRESULT WINAPI WshShell3_QueryInterface(IWshShell3 *iface, REFIID riid, void **ppv)
268 {
269     TRACE("(%s, %p)\n", debugstr_guid(riid), ppv);
270
271     if(IsEqualGUID(riid, &IID_IUnknown)   ||
272        IsEqualGUID(riid, &IID_IDispatch) ||
273        IsEqualGUID(riid, &IID_IWshShell3))
274     {
275         *ppv = iface;
276     }else {
277         FIXME("Unknown iface %s\n", debugstr_guid(riid));
278         *ppv = NULL;
279         return E_NOINTERFACE;
280     }
281
282     IWshShell3_AddRef(iface);
283     return S_OK;
284 }
285
286 static ULONG WINAPI WshShell3_AddRef(IWshShell3 *iface)
287 {
288     TRACE("()\n");
289     return 2;
290 }
291
292 static ULONG WINAPI WshShell3_Release(IWshShell3 *iface)
293 {
294     TRACE("()\n");
295     return 2;
296 }
297
298 static HRESULT WINAPI WshShell3_GetTypeInfoCount(IWshShell3 *iface, UINT *pctinfo)
299 {
300     TRACE("(%p)\n", pctinfo);
301     *pctinfo = 1;
302     return S_OK;
303 }
304
305 static HRESULT WINAPI WshShell3_GetTypeInfo(IWshShell3 *iface, UINT iTInfo, LCID lcid, ITypeInfo **ppTInfo)
306 {
307     TRACE("(%u %u %p)\n", iTInfo, lcid, ppTInfo);
308     return get_typeinfo(IWshShell3_tid, ppTInfo);
309 }
310
311 static HRESULT WINAPI WshShell3_GetIDsOfNames(IWshShell3 *iface, REFIID riid, LPOLESTR *rgszNames,
312         UINT cNames, LCID lcid, DISPID *rgDispId)
313 {
314     ITypeInfo *typeinfo;
315     HRESULT hr;
316
317     TRACE("(%s %p %u %u %p)\n", debugstr_guid(riid), rgszNames, cNames, lcid, rgDispId);
318
319     hr = get_typeinfo(IWshShell3_tid, &typeinfo);
320     if(SUCCEEDED(hr))
321     {
322         hr = ITypeInfo_GetIDsOfNames(typeinfo, rgszNames, cNames, rgDispId);
323         ITypeInfo_Release(typeinfo);
324     }
325
326     return hr;
327 }
328
329 static HRESULT WINAPI WshShell3_Invoke(IWshShell3 *iface, DISPID dispIdMember, REFIID riid, LCID lcid,
330         WORD wFlags, DISPPARAMS *pDispParams, VARIANT *pVarResult, EXCEPINFO *pExcepInfo, UINT *puArgErr)
331 {
332     ITypeInfo *typeinfo;
333     HRESULT hr;
334
335     TRACE("(%d %s %d %d %p %p %p %p)\n", dispIdMember, debugstr_guid(riid),
336           lcid, wFlags, pDispParams, pVarResult, pExcepInfo, puArgErr);
337
338     hr = get_typeinfo(IWshShell3_tid, &typeinfo);
339     if(SUCCEEDED(hr))
340     {
341         hr = ITypeInfo_Invoke(typeinfo, &WshShell3, dispIdMember, wFlags,
342                 pDispParams, pVarResult, pExcepInfo, puArgErr);
343         ITypeInfo_Release(typeinfo);
344     }
345
346     return hr;
347 }
348
349 static HRESULT WINAPI WshShell3_get_SpecialFolders(IWshShell3 *iface, IWshCollection **folders)
350 {
351     TRACE("(%p)\n", folders);
352     return WshCollection_Create(folders);
353 }
354
355 static HRESULT WINAPI WshShell3_get_Environment(IWshShell3 *iface, VARIANT *Type, IWshEnvironment **out_Env)
356 {
357     FIXME("(%p %p): stub\n", Type, out_Env);
358     return E_NOTIMPL;
359 }
360
361 static HRESULT WINAPI WshShell3_Run(IWshShell3 *iface, BSTR Command, VARIANT *WindowStyle, VARIANT *WaitOnReturn, int *out_ExitCode)
362 {
363     FIXME("(%s %s %p): stub\n", debugstr_variant(WindowStyle), debugstr_variant(WaitOnReturn), out_ExitCode);
364     return E_NOTIMPL;
365 }
366
367 static HRESULT WINAPI WshShell3_Popup(IWshShell3 *iface, BSTR Text, VARIANT* SecondsToWait, VARIANT *Title, VARIANT *Type, int *button)
368 {
369     FIXME("(%s %s %s %s %p): stub\n", debugstr_w(Text), debugstr_variant(SecondsToWait),
370         debugstr_variant(Title), debugstr_variant(Type), button);
371     return E_NOTIMPL;
372 }
373
374 static HRESULT WINAPI WshShell3_CreateShortcut(IWshShell3 *iface, BSTR PathLink, IDispatch** out_Shortcut)
375 {
376     FIXME("(%s %p): stub\n", debugstr_w(PathLink), out_Shortcut);
377     return E_NOTIMPL;
378 }
379
380 static HRESULT WINAPI WshShell3_ExpandEnvironmentStrings(IWshShell3 *iface, BSTR Src, BSTR* out_Dst)
381 {
382     FIXME("(%s %p): stub\n", debugstr_w(Src), out_Dst);
383     return E_NOTIMPL;
384 }
385
386 static HRESULT WINAPI WshShell3_RegRead(IWshShell3 *iface, BSTR Name, VARIANT* out_Value)
387 {
388     FIXME("(%s %p): stub\n", debugstr_w(Name), out_Value);
389     return E_NOTIMPL;
390 }
391
392 static HRESULT WINAPI WshShell3_RegWrite(IWshShell3 *iface, BSTR Name, VARIANT *Value, VARIANT *Type)
393 {
394     FIXME("(%s %s %s): stub\n", debugstr_w(Name), debugstr_variant(Value), debugstr_variant(Type));
395     return E_NOTIMPL;
396 }
397
398 static HRESULT WINAPI WshShell3_RegDelete(IWshShell3 *iface, BSTR Name)
399 {
400     FIXME("(%s): stub\n", debugstr_w(Name));
401     return E_NOTIMPL;
402 }
403
404 static HRESULT WINAPI WshShell3_LogEvent(IWshShell3 *iface, VARIANT *Type, BSTR Message, BSTR Target, VARIANT_BOOL *out_Success)
405 {
406     FIXME("(%s %s %s %p): stub\n", debugstr_variant(Type), debugstr_w(Message), debugstr_w(Target), out_Success);
407     return E_NOTIMPL;
408 }
409
410 static HRESULT WINAPI WshShell3_AppActivate(IWshShell3 *iface, VARIANT *App, VARIANT *Wait, VARIANT_BOOL *out_Success)
411 {
412     FIXME("(%s %s %p): stub\n", debugstr_variant(App), debugstr_variant(Wait), out_Success);
413     return E_NOTIMPL;
414 }
415
416 static HRESULT WINAPI WshShell3_SendKeys(IWshShell3 *iface, BSTR Keys, VARIANT *Wait)
417 {
418     FIXME("(%s %p): stub\n", debugstr_w(Keys), Wait);
419     return E_NOTIMPL;
420 }
421
422 static const IWshShell3Vtbl WshShell3Vtbl = {
423     WshShell3_QueryInterface,
424     WshShell3_AddRef,
425     WshShell3_Release,
426     WshShell3_GetTypeInfoCount,
427     WshShell3_GetTypeInfo,
428     WshShell3_GetIDsOfNames,
429     WshShell3_Invoke,
430     WshShell3_get_SpecialFolders,
431     WshShell3_get_Environment,
432     WshShell3_Run,
433     WshShell3_Popup,
434     WshShell3_CreateShortcut,
435     WshShell3_ExpandEnvironmentStrings,
436     WshShell3_RegRead,
437     WshShell3_RegWrite,
438     WshShell3_RegDelete,
439     WshShell3_LogEvent,
440     WshShell3_AppActivate,
441     WshShell3_SendKeys
442 };
443
444 static IWshShell3 WshShell3 = { &WshShell3Vtbl };
445
446 HRESULT WINAPI WshShellFactory_CreateInstance(IClassFactory *iface, IUnknown *outer, REFIID riid, void **ppv)
447 {
448     TRACE("(%p %s %p)\n", outer, debugstr_guid(riid), ppv);
449
450     return IWshShell3_QueryInterface(&WshShell3, riid, ppv);
451 }