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"
23 #include <wine/unicode.h>
25 typedef struct PropertyBag
{
26 IPropertyBag2 IPropertyBag2_iface
;
33 static inline PropertyBag
*impl_from_IPropertyBag2(IPropertyBag2
*iface
)
35 return CONTAINING_RECORD(iface
, PropertyBag
, IPropertyBag2_iface
);
38 static HRESULT WINAPI
PropertyBag_QueryInterface(IPropertyBag2
*iface
, REFIID iid
,
41 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
42 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
44 if (!ppv
) return E_INVALIDARG
;
46 if (IsEqualIID(&IID_IUnknown
, iid
) ||
47 IsEqualIID(&IID_IPropertyBag2
, iid
))
49 *ppv
= &This
->IPropertyBag2_iface
;
57 IUnknown_AddRef((IUnknown
*)*ppv
);
61 static ULONG WINAPI
PropertyBag_AddRef(IPropertyBag2
*iface
)
63 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
64 ULONG ref
= InterlockedIncrement(&This
->ref
);
66 TRACE("(%p) refcount=%u\n", iface
, ref
);
71 static ULONG WINAPI
PropertyBag_Release(IPropertyBag2
*iface
)
73 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
74 ULONG ref
= InterlockedDecrement(&This
->ref
);
76 TRACE("(%p) refcount=%u\n", iface
, ref
);
81 if (This
->properties
&& This
->values
)
83 for (i
=0; i
< This
->prop_count
; i
++)
85 HeapFree(GetProcessHeap(), 0, This
->properties
[i
].pstrName
);
86 VariantClear( This
->values
+i
);
90 HeapFree(GetProcessHeap(), 0, This
->properties
);
91 HeapFree(GetProcessHeap(), 0, This
->values
);
92 HeapFree(GetProcessHeap(), 0, This
);
98 static LONG
find_item(PropertyBag
*This
, LPCOLESTR name
)
101 if (!This
->properties
)
106 for (i
=0; i
< This
->prop_count
; i
++)
108 if (strcmpW(name
, This
->properties
[i
].pstrName
) == 0)
115 static HRESULT WINAPI
PropertyBag_Read(IPropertyBag2
*iface
, ULONG cProperties
,
116 PROPBAG2
*pPropBag
, IErrorLog
*pErrLog
, VARIANT
*pvarValue
, HRESULT
*phrError
)
120 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
122 TRACE("(%p,%u,%p,%p,%p,%p)\n", iface
, cProperties
, pPropBag
, pErrLog
, pvarValue
, phrError
);
124 for (i
=0; i
< cProperties
; i
++)
127 if (pPropBag
[i
].dwHint
&& pPropBag
[i
].dwHint
<= This
->prop_count
)
128 idx
= pPropBag
[i
].dwHint
-1;
130 idx
= find_item(This
, pPropBag
[i
].pstrName
);
134 VariantInit(pvarValue
+i
);
135 res
= VariantCopy(pvarValue
+i
, This
->values
+idx
);
150 static HRESULT WINAPI
PropertyBag_Write(IPropertyBag2
*iface
, ULONG cProperties
,
151 PROPBAG2
*pPropBag
, VARIANT
*pvarValue
)
155 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
157 TRACE("(%p,%u,%p,%p)\n", iface
, cProperties
, pPropBag
, pvarValue
);
159 for (i
=0; i
< cProperties
; i
++)
162 if (pPropBag
[i
].dwHint
&& pPropBag
[i
].dwHint
<= This
->prop_count
)
163 idx
= pPropBag
[i
].dwHint
-1;
165 idx
= find_item(This
, pPropBag
[i
].pstrName
);
169 if (This
->properties
[idx
].vt
!= V_VT(pvarValue
+i
))
170 return WINCODEC_ERR_PROPERTYUNEXPECTEDTYPE
;
171 res
= VariantCopy(This
->values
+idx
, pvarValue
+i
);
177 if (pPropBag
[i
].pstrName
)
178 FIXME("Application tried to set the unknown option %s.\n",
179 debugstr_w(pPropBag
[i
].pstrName
));
181 /* FIXME: Function is not atomar on error, but MSDN does not say anything about it
182 * (no reset of items between 0 and i-1) */
190 static HRESULT WINAPI
PropertyBag_CountProperties(IPropertyBag2
*iface
, ULONG
*pcProperties
)
192 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
194 TRACE("(%p,%p)\n", iface
, pcProperties
);
199 *pcProperties
= This
->prop_count
;
204 static HRESULT
copy_propbag2(PROPBAG2
*dest
, PROPBAG2
*src
, BOOL useCoAlloc
)
206 dest
->cfType
= src
->cfType
;
207 dest
->clsid
= src
->clsid
;
208 dest
->dwHint
= src
->dwHint
;
209 dest
->dwType
= src
->dwType
;
212 dest
->pstrName
= CoTaskMemAlloc((strlenW(src
->pstrName
)+1) * sizeof(WCHAR
));
214 dest
->pstrName
= HeapAlloc(GetProcessHeap(), 0, (strlenW(src
->pstrName
)+1) * sizeof(WCHAR
));
217 return E_OUTOFMEMORY
;
219 strcpyW(dest
->pstrName
, src
->pstrName
);
224 static HRESULT WINAPI
PropertyBag_GetPropertyInfo(IPropertyBag2
*iface
, ULONG iProperty
,
225 ULONG cProperties
, PROPBAG2
*pPropBag
, ULONG
*pcProperties
)
229 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
231 TRACE("(%p,%u,%u,%p,%p)\n", iface
, iProperty
, cProperties
, pPropBag
, pcProperties
);
233 if (iProperty
>= This
->prop_count
&& iProperty
> 0)
234 return WINCODEC_ERR_VALUEOUTOFRANGE
;
235 if (iProperty
+cProperties
> This
->prop_count
)
236 return WINCODEC_ERR_VALUEOUTOFRANGE
;
238 *pcProperties
= max(cProperties
, This
->prop_count
-iProperty
);
240 for (i
=0; i
< *pcProperties
; i
++)
242 res
= copy_propbag2(pPropBag
+i
, This
->properties
+iProperty
+i
, TRUE
);
246 CoTaskMemFree( pPropBag
[--i
].pstrName
);
255 static HRESULT WINAPI
PropertyBag_LoadObject(IPropertyBag2
*iface
, LPCOLESTR pstrName
,
256 DWORD dwHint
, IUnknown
*pUnkObject
, IErrorLog
*pErrLog
)
258 FIXME("(%p,%s,%u,%p,%p): stub\n", iface
, debugstr_w(pstrName
), dwHint
, pUnkObject
, pErrLog
);
262 static const IPropertyBag2Vtbl PropertyBag_Vtbl
= {
263 PropertyBag_QueryInterface
,
268 PropertyBag_CountProperties
,
269 PropertyBag_GetPropertyInfo
,
270 PropertyBag_LoadObject
273 HRESULT
CreatePropertyBag2(PROPBAG2
*options
, UINT count
,
274 IPropertyBag2
**ppPropertyBag2
)
280 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PropertyBag
));
281 if (!This
) return E_OUTOFMEMORY
;
283 This
->IPropertyBag2_iface
.lpVtbl
= &PropertyBag_Vtbl
;
285 This
->prop_count
= count
;
289 This
->properties
= NULL
;
294 This
->properties
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(PROPBAG2
)*count
);
295 This
->values
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(VARIANT
)*count
);
297 if (!This
->properties
|| !This
->values
)
300 for (i
=0; i
< count
; i
++)
302 res
= copy_propbag2(This
->properties
+i
, options
+i
, FALSE
);
305 This
->properties
[i
].dwHint
= i
+1; /* 0 means unset, so we start with 1 */
311 PropertyBag_Release(&This
->IPropertyBag2_iface
);
312 *ppPropertyBag2
= NULL
;
315 *ppPropertyBag2
= &This
->IPropertyBag2_iface
;