shell32: COM cleanup for IPersistFile.
[wine] / dlls / shell32 / shfldr_printers.c
1 /*
2  *    Virtual Printers folder
3  *
4  *    Copyright 2010 Nikolay Sivov for CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include <stdarg.h>
22 #include <stdio.h>
23
24 #define COBJMACROS
25 #define NONAMELESSUNION
26 #define NONAMELESSSTRUCT
27
28 #include "winerror.h"
29 #include "windef.h"
30 #include "winbase.h"
31
32 #include "shell32_main.h"
33 #include "wine/debug.h"
34 #include "debughlp.h"
35
36 #include "shresdef.h"
37 #include "shfldr.h"
38
39 WINE_DEFAULT_DEBUG_CHANNEL (shell);
40
41 typedef struct {
42     IShellFolder2 IShellFolder2_iface;
43     IPersistFolder2 IPersistFolder2_iface;
44     LONG ref;
45
46     LPITEMIDLIST pidl;
47 } IPrintersFolderImpl;
48
49 static inline IPrintersFolderImpl *impl_from_IShellFolder2(IShellFolder2 *iface)
50 {
51     return CONTAINING_RECORD(iface, IPrintersFolderImpl, IShellFolder2_iface);
52 }
53
54 static inline IPrintersFolderImpl *impl_from_IPersistFolder2(IPersistFolder2 *iface)
55 {
56     return CONTAINING_RECORD(iface, IPrintersFolderImpl, IPersistFolder2_iface);
57 }
58
59 static const shvheader printers_header[] = {
60     { IDS_SHV_COLUMN8,      SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 20 },
61     { IDS_SHV_COL_DOCS,     SHCOLSTATE_TYPE_INT | SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 12 },
62     { IDS_SHV_COL_STATUS,   SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 12 },
63     { IDS_SHV_COLUMN9,      SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 30 },
64     { IDS_SHV_COL_LOCATION, SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 20 },
65     { IDS_SHV_COL_MODEL,    SHCOLSTATE_TYPE_STR | SHCOLSTATE_ONBYDEFAULT, LVCFMT_LEFT, 20 }
66 };
67
68 #define PRINTERS_FOLDER_COL_NUM sizeof(printers_header)/sizeof(shvheader)
69
70 static HRESULT WINAPI IShellFolder_Printers_fnQueryInterface(IShellFolder2 *iface,
71                REFIID riid, LPVOID *ppvObj)
72 {
73     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
74
75     TRACE ("(%p)->(%s,%p)\n", This, shdebugstr_guid (riid), ppvObj);
76
77     *ppvObj = NULL;
78
79     if (IsEqualIID (riid, &IID_IUnknown) ||
80         IsEqualIID (riid, &IID_IShellFolder) ||
81         IsEqualIID (riid, &IID_IShellFolder2))
82     {
83         *ppvObj = This;
84     }
85     else if (IsEqualIID (riid, &IID_IPersist) ||
86              IsEqualIID (riid, &IID_IPersistFolder) ||
87              IsEqualIID (riid, &IID_IPersistFolder2))
88     {
89         *ppvObj = &This->IPersistFolder2_iface;
90     }
91
92     if (*ppvObj)
93     {
94         IUnknown_AddRef ((IUnknown *) (*ppvObj));
95         TRACE ("-- Interface: (%p)->(%p)\n", ppvObj, *ppvObj);
96         return S_OK;
97     }
98
99     ERR ("-- interface %s not supported\n", shdebugstr_guid (riid));
100     return E_NOINTERFACE;
101 }
102
103 static ULONG WINAPI IShellFolder_Printers_fnAddRef (IShellFolder2 * iface)
104 {
105     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
106     ULONG refCount = InterlockedIncrement(&This->ref);
107     TRACE ("(%p)->(count=%u)\n", This, refCount - 1);
108     return refCount;
109 }
110
111 static ULONG WINAPI IShellFolder_Printers_fnRelease (IShellFolder2 * iface)
112 {
113     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
114     ULONG refCount = InterlockedDecrement(&This->ref);
115
116     TRACE ("(%p)->(count=%u)\n", This, refCount + 1);
117
118     if (!refCount)
119     {
120         TRACE ("-- destroying IShellFolder(%p)\n", This);
121         SHFree(This->pidl);
122         LocalFree (This);
123     }
124     return refCount;
125 }
126
127 static HRESULT WINAPI IShellFolder_Printers_fnParseDisplayName (IShellFolder2 *iface,
128                HWND hwndOwner, LPBC pbc, LPOLESTR lpszDisplayName,
129                DWORD * pchEaten, LPITEMIDLIST * ppidl, DWORD * pdwAttributes)
130 {
131     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
132
133     FIXME("(%p)->(HWND=%p,%p,%p=%s,%p,pidl=%p,%p) stub\n", This,
134           hwndOwner, pbc, lpszDisplayName, debugstr_w (lpszDisplayName),
135           pchEaten, ppidl, pdwAttributes);
136
137     return E_NOTIMPL;
138 }
139
140 static HRESULT WINAPI IShellFolder_Printers_fnEnumObjects (IShellFolder2 *iface,
141                HWND hwndOwner, DWORD dwFlags, LPENUMIDLIST *ppEnumIDList)
142 {
143     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
144     FIXME("(%p)->(HWND=%p flags=0x%08x pplist=%p) stub\n", This,
145           hwndOwner, dwFlags, ppEnumIDList);
146     return E_NOTIMPL;
147 }
148
149 static HRESULT WINAPI IShellFolder_Printers_fnBindToObject (IShellFolder2 *iface,
150                LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID *ppvOut)
151 {
152     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
153     FIXME("(%p)->(pidl=%p,%p,%s,%p) stub\n", This,
154           pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
155     return E_NOTIMPL;
156 }
157
158 static HRESULT WINAPI IShellFolder_Printers_fnBindToStorage (IShellFolder2 * iface,
159                LPCITEMIDLIST pidl, LPBC pbcReserved, REFIID riid, LPVOID *ppvOut)
160 {
161     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
162
163     FIXME("(%p)->(pidl=%p,%p,%s,%p) stub\n", This,
164           pidl, pbcReserved, shdebugstr_guid (riid), ppvOut);
165
166     *ppvOut = NULL;
167     return E_NOTIMPL;
168 }
169
170 static HRESULT WINAPI IShellFolder_Printers_fnCompareIDs(IShellFolder2 *iface,
171                LPARAM lParam, LPCITEMIDLIST pidl1, LPCITEMIDLIST pidl2)
172 {
173     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
174     FIXME("(%p)->(0x%08lx,pidl1=%p,pidl2=%p) stub\n", This, lParam, pidl1, pidl2);
175     return E_NOTIMPL;
176 }
177
178 static HRESULT WINAPI IShellFolder_Printers_fnCreateViewObject(IShellFolder2 *iface,
179                HWND hwndOwner, REFIID riid, LPVOID *ppvOut)
180 {
181     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
182     HRESULT hr = E_NOTIMPL;
183
184     TRACE("(%p)->(hwnd=%p,%s,%p)\n", This,
185           hwndOwner, shdebugstr_guid (riid), ppvOut);
186
187     if (!ppvOut)
188         return E_INVALIDARG;
189
190     *ppvOut = NULL;
191
192     if (IsEqualIID (riid, &IID_IShellView))
193     {
194         IShellView *pShellView = IShellView_Constructor ((IShellFolder *) iface);
195         if (pShellView)
196         {
197             hr = IShellView_QueryInterface (pShellView, riid, ppvOut);
198             IShellView_Release (pShellView);
199         }
200     }
201     else
202         WARN("unsupported interface %s\n", shdebugstr_guid (riid));
203
204     return hr;
205 }
206
207 static HRESULT WINAPI IShellFolder_Printers_fnGetAttributesOf (IShellFolder2 * iface,
208                 UINT cidl, LPCITEMIDLIST * apidl, DWORD * rgfInOut)
209 {
210     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
211     FIXME("(%p)->(cidl=%d apidl=%p mask=%p (0x%08x)) stub\n",
212            This, cidl, apidl, rgfInOut, rgfInOut ? *rgfInOut : 0);
213     return E_NOTIMPL;
214 }
215
216 static HRESULT WINAPI IShellFolder_Printers_fnGetUIObjectOf (IShellFolder2 * iface,
217                 HWND hwndOwner, UINT cidl, LPCITEMIDLIST * apidl, REFIID riid,
218                 UINT * prgfInOut, LPVOID * ppvOut)
219 {
220     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
221
222     FIXME("(%p)->(%p,%u,apidl=%p,%s,%p,%p) stub\n", This,
223           hwndOwner, cidl, apidl, shdebugstr_guid (riid), prgfInOut, ppvOut);
224
225     if (!ppvOut)
226         return E_INVALIDARG;
227
228     *ppvOut = NULL;
229
230     return E_NOTIMPL;
231 }
232
233 static HRESULT WINAPI IShellFolder_Printers_fnGetDisplayNameOf (IShellFolder2 *iface,
234                LPCITEMIDLIST pidl, DWORD dwFlags, LPSTRRET strRet)
235 {
236     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
237
238     FIXME("(%p)->(pidl=%p,0x%08x,%p) stub\n", This, pidl, dwFlags, strRet);
239
240     if (!strRet)
241         return E_INVALIDARG;
242
243     return E_NOTIMPL;
244 }
245
246 static HRESULT WINAPI IShellFolder_Printers_fnSetNameOf (
247                IShellFolder2 * iface, HWND hwndOwner, LPCITEMIDLIST pidl,
248                LPCOLESTR lpName, DWORD dwFlags, LPITEMIDLIST * pPidlOut)
249 {
250     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
251     FIXME ("(%p)->(%p,pidl=%p,%s,%u,%p) stub\n", This,
252            hwndOwner, pidl, debugstr_w (lpName), dwFlags, pPidlOut);
253     return E_FAIL;
254 }
255
256 static HRESULT WINAPI IShellFolder_Printers_fnGetDefaultSearchGUID (
257                IShellFolder2 * iface, GUID * pguid)
258 {
259     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
260     FIXME ("(%p) stub\n", This);
261     return E_NOTIMPL;
262 }
263
264 static HRESULT WINAPI IShellFolder_Printers_fnEnumSearches (
265                IShellFolder2 * iface, IEnumExtraSearch ** ppenum)
266 {
267     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
268     FIXME ("(%p) stub\n", This);
269     return E_NOTIMPL;
270 }
271
272 static HRESULT WINAPI IShellFolder_Printers_fnGetDefaultColumn (
273                IShellFolder2 *iface, DWORD dwRes, ULONG *pSort, ULONG *pDisplay)
274 {
275     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
276     FIXME("(%p) stub\n", This);
277     return E_NOTIMPL;
278 }
279
280 static HRESULT WINAPI IShellFolder_Printers_fnGetDefaultColumnState (
281                IShellFolder2 *iface, UINT iColumn, DWORD *pcsFlags)
282 {
283     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
284
285     TRACE("(%p)->(%d %p)\n", This, iColumn, pcsFlags);
286
287     if (iColumn >= PRINTERS_FOLDER_COL_NUM)
288         return E_INVALIDARG;
289
290     *pcsFlags = printers_header[iColumn].pcsFlags;
291
292     return S_OK;
293 }
294
295 static HRESULT WINAPI IShellFolder_Printers_fnGetDetailsEx (IShellFolder2 * iface,
296                LPCITEMIDLIST pidl, const SHCOLUMNID * pscid, VARIANT * pv)
297 {
298     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
299     FIXME ("(%p) stub\n", This);
300     return E_NOTIMPL;
301 }
302
303 static HRESULT WINAPI IShellFolder_Printers_fnGetDetailsOf (IShellFolder2 *iface,
304                LPCITEMIDLIST pidl, UINT iColumn, SHELLDETAILS *psd)
305 {
306     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
307
308     TRACE("(%p)->(%p %i %p)\n", This, pidl, iColumn, psd);
309
310     if (iColumn >= PRINTERS_FOLDER_COL_NUM)
311         return E_NOTIMPL;
312
313     if (!pidl)
314         return SHELL32_GetColumnDetails(printers_header, iColumn, psd);
315
316     FIXME("unimplemented for supplied pidl\n");
317
318     return E_NOTIMPL;
319 }
320
321 static HRESULT WINAPI IShellFolder_Printers_fnMapColumnToSCID (
322                IShellFolder2 * iface, UINT column, SHCOLUMNID * pscid)
323 {
324     IPrintersFolderImpl *This = impl_from_IShellFolder2(iface);
325     FIXME ("(%p)->(%u %p) stub\n", This, column, pscid);
326     return E_NOTIMPL;
327 }
328
329 static const IShellFolder2Vtbl vtbl_ShellFolder2 =
330 {
331     /* IUnknown */
332     IShellFolder_Printers_fnQueryInterface,
333     IShellFolder_Printers_fnAddRef,
334     IShellFolder_Printers_fnRelease,
335     /* IShellFolder */
336     IShellFolder_Printers_fnParseDisplayName,
337     IShellFolder_Printers_fnEnumObjects,
338     IShellFolder_Printers_fnBindToObject,
339     IShellFolder_Printers_fnBindToStorage,
340     IShellFolder_Printers_fnCompareIDs,
341     IShellFolder_Printers_fnCreateViewObject,
342     IShellFolder_Printers_fnGetAttributesOf,
343     IShellFolder_Printers_fnGetUIObjectOf,
344     IShellFolder_Printers_fnGetDisplayNameOf,
345     IShellFolder_Printers_fnSetNameOf,
346     /* IShellFolder2 */
347     IShellFolder_Printers_fnGetDefaultSearchGUID,
348     IShellFolder_Printers_fnEnumSearches,
349     IShellFolder_Printers_fnGetDefaultColumn,
350     IShellFolder_Printers_fnGetDefaultColumnState,
351     IShellFolder_Printers_fnGetDetailsEx,
352     IShellFolder_Printers_fnGetDetailsOf,
353     IShellFolder_Printers_fnMapColumnToSCID
354 };
355
356 /*** IPersistFolder2 ***/
357 static HRESULT WINAPI IPersistFolder2_Printers_fnQueryInterface(IPersistFolder2 * iface,
358                REFIID iid, LPVOID * ppvObj)
359 {
360     IPrintersFolderImpl *This = impl_from_IPersistFolder2(iface);
361     return IShellFolder2_QueryInterface(&This->IShellFolder2_iface, iid, ppvObj);
362 }
363
364 static ULONG WINAPI IPersistFolder2_Printers_fnAddRef(IPersistFolder2 *iface)
365 {
366     IPrintersFolderImpl *This = impl_from_IPersistFolder2(iface);
367     return IShellFolder2_AddRef(&This->IShellFolder2_iface);
368 }
369
370 static ULONG WINAPI IPersistFolder2_Printers_fnRelease(IPersistFolder2 *iface)
371 {
372     IPrintersFolderImpl *This = impl_from_IPersistFolder2(iface);
373     return IShellFolder2_Release(&This->IShellFolder2_iface);
374 }
375
376 static HRESULT WINAPI IPersistFolder2_Printers_fnGetClassID(IPersistFolder2 *iface, CLSID *classId)
377 {
378     IPrintersFolderImpl *This = impl_from_IPersistFolder2(iface);
379
380     FIXME("(%p) stub\n", This);
381
382     if (!classId)
383         return E_POINTER;
384
385     return E_NOTIMPL;
386 }
387
388 static HRESULT WINAPI IPersistFolder2_Printers_fnInitialize(IPersistFolder2 *iface, LPCITEMIDLIST pidl)
389 {
390     IPrintersFolderImpl *This = impl_from_IPersistFolder2(iface);
391
392     TRACE("(%p)->(%p)\n", This, pidl);
393
394     SHFree(This->pidl);
395     This->pidl = ILClone(pidl);
396
397     return S_OK;
398 }
399
400 static HRESULT WINAPI IPersistFolder2_Printers_fnGetCurFolder(IPersistFolder2 *iface, LPITEMIDLIST *pidl)
401 {
402     IPrintersFolderImpl *This = impl_from_IPersistFolder2(iface);
403
404     TRACE("(%p)->(%p)\n", This, pidl);
405
406     *pidl = ILClone(This->pidl);
407
408     return *pidl ? S_OK : S_FALSE;
409 }
410
411 static const IPersistFolder2Vtbl vtbl_PersistFolder2 =
412 {
413     IPersistFolder2_Printers_fnQueryInterface,
414     IPersistFolder2_Printers_fnAddRef,
415     IPersistFolder2_Printers_fnRelease,
416     IPersistFolder2_Printers_fnGetClassID,
417     IPersistFolder2_Printers_fnInitialize,
418     IPersistFolder2_Printers_fnGetCurFolder
419 };
420
421 HRESULT WINAPI Printers_Constructor(IUnknown * pUnkOuter, REFIID riid, LPVOID * ppv)
422 {
423     IPrintersFolderImpl *sf;
424
425     TRACE("unkOut=%p %s\n", pUnkOuter, shdebugstr_guid (riid));
426
427     if (!ppv)
428         return E_POINTER;
429     if (pUnkOuter)
430         return CLASS_E_NOAGGREGATION;
431
432     sf = LocalAlloc(LMEM_FIXED, sizeof(IPrintersFolderImpl));
433     if (!sf)
434         return E_OUTOFMEMORY;
435
436     sf->ref = 0;
437     sf->pidl = NULL;
438     sf->IShellFolder2_iface.lpVtbl = &vtbl_ShellFolder2;
439     sf->IPersistFolder2_iface.lpVtbl = &vtbl_PersistFolder2;
440
441     if (IShellFolder2_QueryInterface(&sf->IShellFolder2_iface, riid, ppv) != S_OK)
442     {
443         IShellFolder2_Release(&sf->IShellFolder2_iface);
444         return E_NOINTERFACE;
445     }
446
447     TRACE("--(%p)\n", sf);
448     return S_OK;
449 }