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 #define WIN32_NO_STATUS
22 #define COM_NO_WINDOWS_H
34 #include <wine/unicode.h>
36 //#include "wincodecs_private.h"
38 #include <wine/debug.h>
40 WINE_DEFAULT_DEBUG_CHANNEL(wincodecs
);
42 typedef struct PropertyBag
{
43 IPropertyBag2 IPropertyBag2_iface
;
50 static inline PropertyBag
*impl_from_IPropertyBag2(IPropertyBag2
*iface
)
52 return CONTAINING_RECORD(iface
, PropertyBag
, IPropertyBag2_iface
);
55 static HRESULT WINAPI
PropertyBag_QueryInterface(IPropertyBag2
*iface
, REFIID iid
,
58 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
59 TRACE("(%p,%s,%p)\n", iface
, debugstr_guid(iid
), ppv
);
61 if (!ppv
) return E_INVALIDARG
;
63 if (IsEqualIID(&IID_IUnknown
, iid
) ||
64 IsEqualIID(&IID_IPropertyBag2
, iid
))
66 *ppv
= &This
->IPropertyBag2_iface
;
74 IUnknown_AddRef((IUnknown
*)*ppv
);
78 static ULONG WINAPI
PropertyBag_AddRef(IPropertyBag2
*iface
)
80 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
81 ULONG ref
= InterlockedIncrement(&This
->ref
);
83 TRACE("(%p) refcount=%u\n", iface
, ref
);
88 static ULONG WINAPI
PropertyBag_Release(IPropertyBag2
*iface
)
90 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
91 ULONG ref
= InterlockedDecrement(&This
->ref
);
93 TRACE("(%p) refcount=%u\n", iface
, ref
);
98 if (This
->properties
&& This
->values
)
100 for (i
=0; i
< This
->prop_count
; i
++)
102 HeapFree(GetProcessHeap(), 0, This
->properties
[i
].pstrName
);
103 VariantClear( This
->values
+i
);
107 HeapFree(GetProcessHeap(), 0, This
->properties
);
108 HeapFree(GetProcessHeap(), 0, This
->values
);
109 HeapFree(GetProcessHeap(), 0, This
);
115 static LONG
find_item(PropertyBag
*This
, LPCOLESTR name
)
118 if (!This
->properties
)
123 for (i
=0; i
< This
->prop_count
; i
++)
125 if (strcmpW(name
, This
->properties
[i
].pstrName
) == 0)
132 static HRESULT WINAPI
PropertyBag_Read(IPropertyBag2
*iface
, ULONG cProperties
,
133 PROPBAG2
*pPropBag
, IErrorLog
*pErrLog
, VARIANT
*pvarValue
, HRESULT
*phrError
)
137 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
139 TRACE("(%p,%u,%p,%p,%p,%p)\n", iface
, cProperties
, pPropBag
, pErrLog
, pvarValue
, phrError
);
141 for (i
=0; i
< cProperties
; i
++)
144 if (pPropBag
[i
].dwHint
&& pPropBag
[i
].dwHint
<= This
->prop_count
)
145 idx
= pPropBag
[i
].dwHint
-1;
147 idx
= find_item(This
, pPropBag
[i
].pstrName
);
151 VariantInit(pvarValue
+i
);
152 res
= VariantCopy(pvarValue
+i
, This
->values
+idx
);
167 static HRESULT WINAPI
PropertyBag_Write(IPropertyBag2
*iface
, ULONG cProperties
,
168 PROPBAG2
*pPropBag
, VARIANT
*pvarValue
)
172 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
174 TRACE("(%p,%u,%p,%p)\n", iface
, cProperties
, pPropBag
, pvarValue
);
176 for (i
=0; i
< cProperties
; i
++)
179 if (pPropBag
[i
].dwHint
&& pPropBag
[i
].dwHint
<= This
->prop_count
)
180 idx
= pPropBag
[i
].dwHint
-1;
182 idx
= find_item(This
, pPropBag
[i
].pstrName
);
186 if (This
->properties
[idx
].vt
!= V_VT(pvarValue
+i
))
187 return WINCODEC_ERR_PROPERTYUNEXPECTEDTYPE
;
188 res
= VariantCopy(This
->values
+idx
, pvarValue
+i
);
194 if (pPropBag
[i
].pstrName
)
195 FIXME("Application tried to set the unknown option %s.\n",
196 debugstr_w(pPropBag
[i
].pstrName
));
198 /* FIXME: Function is not atomar on error, but MSDN does not say anything about it
199 * (no reset of items between 0 and i-1) */
207 static HRESULT WINAPI
PropertyBag_CountProperties(IPropertyBag2
*iface
, ULONG
*pcProperties
)
209 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
211 TRACE("(%p,%p)\n", iface
, pcProperties
);
216 *pcProperties
= This
->prop_count
;
221 static HRESULT
copy_propbag2(PROPBAG2
*dest
, PROPBAG2
*src
, BOOL useCoAlloc
)
223 dest
->cfType
= src
->cfType
;
224 dest
->clsid
= src
->clsid
;
225 dest
->dwHint
= src
->dwHint
;
226 dest
->dwType
= src
->dwType
;
229 dest
->pstrName
= CoTaskMemAlloc((strlenW(src
->pstrName
)+1) * sizeof(WCHAR
));
231 dest
->pstrName
= HeapAlloc(GetProcessHeap(), 0, (strlenW(src
->pstrName
)+1) * sizeof(WCHAR
));
234 return E_OUTOFMEMORY
;
236 strcpyW(dest
->pstrName
, src
->pstrName
);
241 static HRESULT WINAPI
PropertyBag_GetPropertyInfo(IPropertyBag2
*iface
, ULONG iProperty
,
242 ULONG cProperties
, PROPBAG2
*pPropBag
, ULONG
*pcProperties
)
246 PropertyBag
*This
= impl_from_IPropertyBag2(iface
);
248 TRACE("(%p,%u,%u,%p,%p)\n", iface
, iProperty
, cProperties
, pPropBag
, pcProperties
);
250 if (iProperty
>= This
->prop_count
&& iProperty
> 0)
251 return WINCODEC_ERR_VALUEOUTOFRANGE
;
252 if (iProperty
+cProperties
> This
->prop_count
)
253 return WINCODEC_ERR_VALUEOUTOFRANGE
;
255 *pcProperties
= max(cProperties
, This
->prop_count
-iProperty
);
257 for (i
=0; i
< *pcProperties
; i
++)
259 res
= copy_propbag2(pPropBag
+i
, This
->properties
+iProperty
+i
, TRUE
);
263 CoTaskMemFree( pPropBag
[--i
].pstrName
);
272 static HRESULT WINAPI
PropertyBag_LoadObject(IPropertyBag2
*iface
, LPCOLESTR pstrName
,
273 DWORD dwHint
, IUnknown
*pUnkObject
, IErrorLog
*pErrLog
)
275 FIXME("(%p,%s,%u,%p,%p): stub\n", iface
, debugstr_w(pstrName
), dwHint
, pUnkObject
, pErrLog
);
279 static const IPropertyBag2Vtbl PropertyBag_Vtbl
= {
280 PropertyBag_QueryInterface
,
285 PropertyBag_CountProperties
,
286 PropertyBag_GetPropertyInfo
,
287 PropertyBag_LoadObject
290 HRESULT
CreatePropertyBag2(PROPBAG2
*options
, UINT count
,
291 IPropertyBag2
**ppPropertyBag2
)
297 This
= HeapAlloc(GetProcessHeap(), 0, sizeof(PropertyBag
));
298 if (!This
) return E_OUTOFMEMORY
;
300 This
->IPropertyBag2_iface
.lpVtbl
= &PropertyBag_Vtbl
;
302 This
->prop_count
= count
;
306 This
->properties
= NULL
;
311 This
->properties
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(PROPBAG2
)*count
);
312 This
->values
= HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY
, sizeof(VARIANT
)*count
);
314 if (!This
->properties
|| !This
->values
)
317 for (i
=0; i
< count
; i
++)
319 res
= copy_propbag2(This
->properties
+i
, options
+i
, FALSE
);
322 This
->properties
[i
].dwHint
= i
+1; /* 0 means unset, so we start with 1 */
328 PropertyBag_Release(&This
->IPropertyBag2_iface
);
329 *ppPropertyBag2
= NULL
;
332 *ppPropertyBag2
= &This
->IPropertyBag2_iface
;