2 * Copyright 2009 Vincent Povirk for CodeWeavers
3 * Copyright 2013 Ludger Sprenker
5 * This library is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This library is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this library; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "wincodecs_private.h"
22 typedef struct PropertyBag
{
23 IPropertyBag2 IPropertyBag2_iface
;
30 static inline PropertyBag
*impl_from_IPropertyBag2(IPropertyBag2
*iface
)
32 return CONTAINING_RECORD(iface
, PropertyBag
, IPropertyBag2_iface
);
35 static HRESULT WINAPI
PropertyBag_QueryInterface(IPropertyBag2
*iface
, REFIID iid
,
38 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
39 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
41 if (!ppv
) return E_INVALIDARG
;
43 if (IsEqualIID(&IID_IUnknown
, iid
) ||
44 IsEqualIID(&IID_IPropertyBag2
, iid
))
46 *ppv
= &This
->IPropertyBag2_iface
;
54 IUnknown_AddRef((IUnknown
*)*ppv
);
58 static ULONG WINAPI
PropertyBag_AddRef(IPropertyBag2
*iface
)
60 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
61 ULONG ref
= InterlockedIncrement(&This
->ref
);
63 TRACE("(%p) refcount=%u\n", iface
, ref
);
68 static ULONG WINAPI
PropertyBag_Release(IPropertyBag2
*iface
)
70 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
71 ULONG ref
= InterlockedDecrement(&This
->ref
);
73 TRACE("(%p) refcount=%u\n", iface
, ref
);
78 if (This
->properties
&& This
->values
)
80 for (i
=0; i
< This
->prop_count
; i
++)
82 HeapFree(GetProcessHeap(), 0, This
->properties
[i
].pstrName
);
83 VariantClear( This
->values
+i
);
87 HeapFree(GetProcessHeap(), 0, This
->properties
);
88 HeapFree(GetProcessHeap(), 0, This
->values
);
89 HeapFree(GetProcessHeap(), 0, This
);
95 static LONG
find_item(PropertyBag
*This
, LPCOLESTR name
)
98 if (!This
->properties
)
103 for (i
=0; i
< This
->prop_count
; i
++)
105 if (strcmpW(name
, This
->properties
[i
].pstrName
) == 0)
112 static HRESULT WINAPI
PropertyBag_Read(IPropertyBag2
*iface
, ULONG cProperties
,
113 PROPBAG2
*pPropBag
, IErrorLog
*pErrLog
, VARIANT
*pvarValue
, HRESULT
*phrError
)
117 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
119 TRACE("(%p,%u,%p,%p,%p,%p)\n", iface
, cProperties
, pPropBag
, pErrLog
, pvarValue
, phrError
);
121 for (i
=0; i
< cProperties
; i
++)
124 if (pPropBag
[i
].dwHint
&& pPropBag
[i
].dwHint
<= This
->prop_count
)
125 idx
= pPropBag
[i
].dwHint
-1;
127 idx
= find_item(This
, pPropBag
[i
].pstrName
);
131 VariantInit(pvarValue
+i
);
132 res
= VariantCopy(pvarValue
+i
, This
->values
+idx
);
147 static HRESULT WINAPI
PropertyBag_Write(IPropertyBag2
*iface
, ULONG cProperties
,
148 PROPBAG2
*pPropBag
, VARIANT
*pvarValue
)
152 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
154 TRACE("(%p,%u,%p,%p)\n", iface
, cProperties
, pPropBag
, pvarValue
);
156 for (i
=0; i
< cProperties
; i
++)
159 if (pPropBag
[i
].dwHint
&& pPropBag
[i
].dwHint
<= This
->prop_count
)
160 idx
= pPropBag
[i
].dwHint
-1;
162 idx
= find_item(This
, pPropBag
[i
].pstrName
);
166 if (This
->properties
[idx
].vt
!= V_VT(pvarValue
+i
))
167 return WINCODEC_ERR_PROPERTYUNEXPECTEDTYPE
;
168 res
= VariantCopy(This
->values
+idx
, pvarValue
+i
);
174 if (pPropBag
[i
].pstrName
)
175 FIXME("Application tried to set the unknown option %s.\n",
176 debugstr_w(pPropBag
[i
].pstrName
));
178 /* FIXME: Function is not atomar on error, but MSDN does not say anything about it
179 * (no reset of items between 0 and i-1) */
187 static HRESULT WINAPI
PropertyBag_CountProperties(IPropertyBag2
*iface
, ULONG
*pcProperties
)
189 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
191 TRACE("(%p,%p)\n", iface
, pcProperties
);
196 *pcProperties
= This
->prop_count
;
201 static HRESULT
copy_propbag2(PROPBAG2
*dest
, PROPBAG2
*src
, BOOL useCoAlloc
)
203 dest
->cfType
= src
->cfType
;
204 dest
->clsid
= src
->clsid
;
205 dest
->dwHint
= src
->dwHint
;
206 dest
->dwType
= src
->dwType
;
209 dest
->pstrName
= CoTaskMemAlloc((strlenW(src
->pstrName
)+1) * sizeof(WCHAR
));
211 dest
->pstrName
= HeapAlloc(GetProcessHeap(), 0, (strlenW(src
->pstrName
)+1) * sizeof(WCHAR
));
214 return E_OUTOFMEMORY
;
216 strcpyW(dest
->pstrName
, src
->pstrName
);
221 static HRESULT WINAPI
PropertyBag_GetPropertyInfo(IPropertyBag2
*iface
, ULONG iProperty
,
222 ULONG cProperties
, PROPBAG2
*pPropBag
, ULONG
*pcProperties
)
226 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
228 TRACE("(%p,%u,%u,%p,%p)\n", iface
, iProperty
, cProperties
, pPropBag
, pcProperties
);
230 if (iProperty
>= This
->prop_count
&& iProperty
> 0)
231 return WINCODEC_ERR_VALUEOUTOFRANGE
;
232 if (iProperty
+cProperties
> This
->prop_count
)
233 return WINCODEC_ERR_VALUEOUTOFRANGE
;
235 *pcProperties
= max(cProperties
, This
->prop_count
-iProperty
);
237 for (i
=0; i
< *pcProperties
; i
++)
239 res
= copy_propbag2(pPropBag
+i
, This
->properties
+iProperty
+i
, TRUE
);
243 CoTaskMemFree( pPropBag
[--i
].pstrName
);
252 static HRESULT WINAPI
PropertyBag_LoadObject(IPropertyBag2
*iface
, LPCOLESTR pstrName
,
253 DWORD dwHint
, IUnknown
*pUnkObject
, IErrorLog
*pErrLog
)
255 FIXME("(%p,%s,%u,%p,%p): stub\n", iface
, debugstr_w(pstrName
), dwHint
, pUnkObject
, pErrLog
);
259 static const IPropertyBag2Vtbl PropertyBag_Vtbl
= {
260 PropertyBag_QueryInterface
,
265 PropertyBag_CountProperties
,
266 PropertyBag_GetPropertyInfo
,
267 PropertyBag_LoadObject
270 HRESULT
CreatePropertyBag2(PROPBAG2
*options
, UINT count
,
271 IPropertyBag2
**ppPropertyBag2
)
277 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PropertyBag
));
278 if (!This
) return E_OUTOFMEMORY
;
280 This
->IPropertyBag2_iface
.lpVtbl
= &PropertyBag_Vtbl
;
282 This
->prop_count
= count
;
286 This
->properties
= NULL
;
291 This
->properties
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(PROPBAG2
)*count
);
292 This
->values
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(VARIANT
)*count
);
294 if (!This
->properties
|| !This
->values
)
297 for (i
=0; i
< count
; i
++)
299 res
= copy_propbag2(This
->properties
+i
, options
+i
, FALSE
);
302 This
->properties
[i
].dwHint
= i
+1; /* 0 means unset, so we start with 1 */
308 PropertyBag_Release(&This
->IPropertyBag2_iface
);
309 *ppPropertyBag2
= NULL
;
312 *ppPropertyBag2
= &This
->IPropertyBag2_iface
;