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
28 #include "wincodecs_private.h"
30 #include "wine/debug.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
34 typedef struct PropertyBag
{
35 IPropertyBag2 IPropertyBag2_iface
;
42 static inline PropertyBag
*impl_from_IPropertyBag2(IPropertyBag2
*iface
)
44 return CONTAINING_RECORD(iface
, PropertyBag
, IPropertyBag2_iface
);
47 static HRESULT WINAPI
PropertyBag_QueryInterface(IPropertyBag2
*iface
, REFIID iid
,
50 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
51 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
53 if (!ppv
) return E_INVALIDARG
;
55 if (IsEqualIID(&IID_IUnknown
, iid
) ||
56 IsEqualIID(&IID_IPropertyBag2
, iid
))
58 *ppv
= &This
->IPropertyBag2_iface
;
66 IUnknown_AddRef((IUnknown
*)*ppv
);
70 static ULONG WINAPI
PropertyBag_AddRef(IPropertyBag2
*iface
)
72 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
73 ULONG ref
= InterlockedIncrement(&This
->ref
);
75 TRACE("(%p) refcount=%u\n", iface
, ref
);
80 static ULONG WINAPI
PropertyBag_Release(IPropertyBag2
*iface
)
82 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
83 ULONG ref
= InterlockedDecrement(&This
->ref
);
85 TRACE("(%p) refcount=%u\n", iface
, ref
);
90 if (This
->properties
&& This
->values
)
92 for (i
=0; i
< This
->prop_count
; i
++)
94 CoTaskMemFree(This
->properties
[i
].pstrName
);
95 VariantClear( This
->values
+i
);
99 HeapFree(GetProcessHeap(), 0, This
->properties
);
100 HeapFree(GetProcessHeap(), 0, This
->values
);
101 HeapFree(GetProcessHeap(), 0, This
);
107 static LONG
find_item(PropertyBag
*This
, LPCOLESTR name
)
110 if (!This
->properties
)
115 for (i
=0; i
< This
->prop_count
; i
++)
117 if (wcscmp(name
, This
->properties
[i
].pstrName
) == 0)
124 static HRESULT WINAPI
PropertyBag_Read(IPropertyBag2
*iface
, ULONG cProperties
,
125 PROPBAG2
*pPropBag
, IErrorLog
*pErrLog
, VARIANT
*pvarValue
, HRESULT
*phrError
)
129 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
131 TRACE("(%p,%u,%p,%p,%p,%p)\n", iface
, cProperties
, pPropBag
, pErrLog
, pvarValue
, phrError
);
133 for (i
=0; i
< cProperties
; i
++)
136 if (pPropBag
[i
].dwHint
&& pPropBag
[i
].dwHint
<= This
->prop_count
)
137 idx
= pPropBag
[i
].dwHint
-1;
139 idx
= find_item(This
, pPropBag
[i
].pstrName
);
143 VariantInit(pvarValue
+i
);
144 res
= VariantCopy(pvarValue
+i
, This
->values
+idx
);
159 static HRESULT WINAPI
PropertyBag_Write(IPropertyBag2
*iface
, ULONG cProperties
,
160 PROPBAG2
*pPropBag
, VARIANT
*pvarValue
)
164 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
166 TRACE("(%p,%u,%p,%p)\n", iface
, cProperties
, pPropBag
, pvarValue
);
168 for (i
=0; i
< cProperties
; i
++)
171 if (pPropBag
[i
].dwHint
&& pPropBag
[i
].dwHint
<= This
->prop_count
)
172 idx
= pPropBag
[i
].dwHint
-1;
174 idx
= find_item(This
, pPropBag
[i
].pstrName
);
178 if (This
->properties
[idx
].vt
!= V_VT(pvarValue
+i
))
179 return WINCODEC_ERR_PROPERTYUNEXPECTEDTYPE
;
180 res
= VariantCopy(This
->values
+idx
, pvarValue
+i
);
186 if (pPropBag
[i
].pstrName
)
187 FIXME("Application tried to set the unknown option %s.\n",
188 debugstr_w(pPropBag
[i
].pstrName
));
190 /* FIXME: Function is not atomar on error, but MSDN does not say anything about it
191 * (no reset of items between 0 and i-1) */
199 static HRESULT WINAPI
PropertyBag_CountProperties(IPropertyBag2
*iface
, ULONG
*pcProperties
)
201 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
203 TRACE("(%p,%p)\n", iface
, pcProperties
);
208 *pcProperties
= This
->prop_count
;
213 static HRESULT
copy_propbag2(PROPBAG2
*dest
, const PROPBAG2
*src
)
215 dest
->cfType
= src
->cfType
;
216 dest
->clsid
= src
->clsid
;
217 dest
->dwHint
= src
->dwHint
;
218 dest
->dwType
= src
->dwType
;
220 dest
->pstrName
= CoTaskMemAlloc((lstrlenW(src
->pstrName
)+1) * sizeof(WCHAR
));
222 return E_OUTOFMEMORY
;
224 lstrcpyW(dest
->pstrName
, src
->pstrName
);
229 static HRESULT WINAPI
PropertyBag_GetPropertyInfo(IPropertyBag2
*iface
, ULONG iProperty
,
230 ULONG cProperties
, PROPBAG2
*pPropBag
, ULONG
*pcProperties
)
234 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
236 TRACE("(%p,%u,%u,%p,%p)\n", iface
, iProperty
, cProperties
, pPropBag
, pcProperties
);
238 if (iProperty
>= This
->prop_count
&& iProperty
> 0)
239 return WINCODEC_ERR_VALUEOUTOFRANGE
;
240 if (iProperty
+cProperties
> This
->prop_count
)
241 return WINCODEC_ERR_VALUEOUTOFRANGE
;
243 *pcProperties
= min(cProperties
, This
->prop_count
-iProperty
);
245 for (i
=0; i
< *pcProperties
; i
++)
247 res
= copy_propbag2(pPropBag
+i
, This
->properties
+iProperty
+i
);
251 CoTaskMemFree( pPropBag
[--i
].pstrName
);
260 static HRESULT WINAPI
PropertyBag_LoadObject(IPropertyBag2
*iface
, LPCOLESTR pstrName
,
261 DWORD dwHint
, IUnknown
*pUnkObject
, IErrorLog
*pErrLog
)
263 FIXME("(%p,%s,%u,%p,%p): stub\n", iface
, debugstr_w(pstrName
), dwHint
, pUnkObject
, pErrLog
);
267 static const IPropertyBag2Vtbl PropertyBag_Vtbl
= {
268 PropertyBag_QueryInterface
,
273 PropertyBag_CountProperties
,
274 PropertyBag_GetPropertyInfo
,
275 PropertyBag_LoadObject
278 HRESULT
CreatePropertyBag2(const PROPBAG2
*options
, UINT count
,
279 IPropertyBag2
**ppPropertyBag2
)
285 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PropertyBag
));
286 if (!This
) return E_OUTOFMEMORY
;
288 This
->IPropertyBag2_iface
.lpVtbl
= &PropertyBag_Vtbl
;
290 This
->prop_count
= count
;
294 This
->properties
= NULL
;
299 This
->properties
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(PROPBAG2
)*count
);
300 This
->values
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(VARIANT
)*count
);
302 if (!This
->properties
|| !This
->values
)
305 for (i
=0; i
< count
; i
++)
307 res
= copy_propbag2(This
->properties
+i
, options
+i
);
310 This
->properties
[i
].dwHint
= i
+1; /* 0 means unset, so we start with 1 */
316 PropertyBag_Release(&This
->IPropertyBag2_iface
);
317 *ppPropertyBag2
= NULL
;
320 *ppPropertyBag2
= &This
->IPropertyBag2_iface
;