1 /*****************************************************************************
2 * persiststreaminit.cpp: ActiveX control for VLC
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
6 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
8 * This program is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
21 *****************************************************************************/
24 #include "persiststreaminit.h"
49 AxVLCVariant(VARIANTARG &v)
52 //VariantCopy(&_v, &v);
56 AxVLCVariant(VARIANTARG *v)
62 AxVLCVariant(const AxVLCVariant &vv)
65 VariantCopy(&_v, const_cast<VARIANTARG *>(&(vv._v)));
74 AxVLCVariant(BSTR bstr)
79 VariantCopy(&_v, &arg);
82 inline const VARIANTARG *variantArg(void) const {
86 inline void swap(AxVLCVariant &v1, AxVLCVariant &v2)
88 VARIANTARG tmp = v1._v;
103 AxVLCWSTR(void) : _data(NULL) {};
109 ULONG refcount = InterlockedDecrement(&(_data->refcount));
111 CoTaskMemFree(_data);
119 size_t len = wcslen(s);
122 size_t size = len*sizeof(WCHAR);
123 _data = (struct data *)CoTaskMemAlloc(sizeof(struct data)+size);
128 memcpy(_data->wstr, s, size);
129 _data->wstr[len]=L'\0';
137 AxVLCWSTR(const AxVLCWSTR &s)
141 InterlockedIncrement(&(_data->refcount));
144 inline bool operator<(const AxVLCWSTR &s) const
146 return compareNoCase(s.wstr()) < 0;
149 inline bool operator<(LPCWSTR s) const
151 return compareNoCase(s) < 0;
154 inline bool operator==(const AxVLCWSTR &s) const
156 return compareNoCase(s.wstr()) == 0;
159 inline bool operator==(LPCWSTR s) const
161 return compareNoCase(s) == 0;
164 LPCWSTR wstr(void) const
166 return (NULL != _data) ? _data->wstr : NULL;
169 size_t size(void) const
171 return (NULL != _data) ? _data->len : 0;
176 inline int compareNoCase(LPCWSTR s) const
180 return (NULL == s) ? 0 : -1;
185 return _wcsicmp(_data->wstr, s);
195 typedef pair<class AxVLCWSTR, class AxVLCVariant> AxVLCPropertyPair;
196 typedef map<class AxVLCWSTR, class AxVLCVariant> AxVLCPropertyMap;
198 ///////////////////////////
200 class VLCPropertyBag : public IPropertyBag
205 VLCPropertyBag(void) : _i_ref(1) {};
206 virtual ~VLCPropertyBag() {};
209 STDMETHODIMP QueryInterface(REFIID riid, void **ppv)
213 if( (IID_IUnknown == riid)
214 && (IID_IPropertyBag == riid) )
217 *ppv = reinterpret_cast<LPVOID>(this);
221 return E_NOINTERFACE;
224 STDMETHODIMP_(ULONG) AddRef(void)
225 { return InterlockedIncrement(&_i_ref); };
227 STDMETHODIMP_(ULONG) Release(void)
229 ULONG refcount = InterlockedDecrement(&_i_ref);
238 // IPropertyBag methods
240 STDMETHODIMP Read(LPCOLESTR pszPropName, VARIANT *pVar, IErrorLog *pErrorLog)
242 if( (NULL == pszPropName) || (NULL == pVar) )
245 AxVLCPropertyMap::const_iterator notfound = _pm.end();
246 AxVLCPropertyMap::const_iterator iter = _pm.find(pszPropName);
247 if( notfound != iter )
249 VARTYPE vtype = V_VT(pVar);
250 VariantCopy(pVar, const_cast<VARIANTARG*>((*iter).second.variantArg()));
251 if( (V_VT(pVar) != vtype) && FAILED(VariantChangeType(pVar, pVar, 0, vtype)) )
262 STDMETHODIMP Write(LPCOLESTR pszPropName, VARIANT *pVar)
264 if( (NULL == pszPropName) || (NULL == pVar) )
267 AxVLCPropertyPair val(pszPropName, pVar);
268 pair<AxVLCPropertyMap::iterator, bool> p = _pm.insert(val);
269 if( false == p.second )
270 // replace existing key value
271 (*p.first).second = val.second;
277 HRESULT Load(LPSTREAM pStm)
284 AxVLCPropertyPair *val;
285 result = ReadProperty(pStm, &val);
286 if( SUCCEEDED(result) )
288 if( (val->first == L"(Count)") && (VT_I4 == V_VT(val->second.variantArg())) )
290 size_t count = V_I4(val->second.variantArg());
294 result = ReadProperty(pStm, &val);
298 pair<AxVLCPropertyMap::iterator, bool> p = _pm.insert(*val);
299 if( false == p.second )
300 // replace existing key value
301 (*p.first).second = val->second;
309 HRESULT Save(LPSTREAM pStm)
316 AxVLCPropertyPair header(L"(Count)", _pm.size());
317 result = WriteProperty(pStm, header);
318 if( SUCCEEDED(result) )
320 AxVLCPropertyMap::const_iterator iter = _pm.begin();
321 AxVLCPropertyMap::const_iterator end = _pm.end();
325 result = WriteProperty(pStm, *(iter++));
335 return _pm.size() == 0;
340 HRESULT WriteProperty(LPSTREAM pStm, const AxVLCPropertyPair &prop)
344 const AxVLCWSTR propName = prop.first;
346 ULONG len = propName.size();
351 result = pStm->Write(&len, sizeof(len), NULL);
355 result = pStm->Write(propName.wstr(), len*sizeof(WCHAR), NULL);
359 const VARIANTARG *propValue = prop.second.variantArg();
360 VARTYPE vtype = V_VT(propValue);
364 result = pStm->Write(&vtype, sizeof(vtype), NULL);
367 result = pStm->Write(&V_BOOL(propValue), sizeof(V_BOOL(propValue)), NULL);
372 result = pStm->Write(&vtype, sizeof(vtype), NULL);
375 result = pStm->Write(&V_I4(propValue), sizeof(V_I4(propValue)), NULL);
380 result = pStm->Write(&vtype, sizeof(vtype), NULL);
383 len = SysStringLen(V_BSTR(propValue));
384 result = pStm->Write(&len, sizeof(len), NULL);
389 result = pStm->Write(V_BSTR(propValue), len*sizeof(OLECHAR), NULL);
396 result = pStm->Write(&vtype, sizeof(vtype), NULL);
403 static HRESULT ReadProperty(LPSTREAM pStm, AxVLCPropertyPair **prop)
409 result = pStm->Read(&len, sizeof(len), NULL);
416 LPWSTR propName = (LPOLESTR)::alloca((len+1)*sizeof(WCHAR));
417 if( NULL == propName )
418 return E_OUTOFMEMORY;
420 result = pStm->Read(propName, len*sizeof(WCHAR), NULL);
424 propName[len] = L'\0';
426 VARIANTARG propValue;
429 result = pStm->Read(&vtype, sizeof(vtype), NULL);
436 V_VT(&propValue) = vtype;
437 result = pStm->Read(&V_BOOL(&propValue), sizeof(V_BOOL(&propValue)), NULL);
442 V_VT(&propValue) = vtype;
443 result = pStm->Read(&V_I4(&propValue), sizeof(V_I4(&propValue)), NULL);
448 V_VT(&propValue) = vtype;
449 result = pStm->Read(&len, sizeof(len), NULL);
453 V_BSTR(&propValue) = NULL;
456 V_BSTR(&propValue) = SysAllocStringLen(NULL, len);
457 if( NULL == V_BSTR(&propValue) )
458 return E_OUTOFMEMORY;
460 result = pStm->Read(V_BSTR(&propValue), len*sizeof(OLECHAR), NULL);
463 SysFreeString(V_BSTR(&propValue));
469 VariantInit(&propValue);
472 *prop = new AxVLCPropertyPair(propName, propValue);
477 AxVLCPropertyMap _pm;
481 ///////////////////////////
483 VLCPersistStreamInit::VLCPersistStreamInit(VLCPlugin *p_instance) : _p_instance(p_instance)
485 _p_props = new VLCPropertyBag();
488 VLCPersistStreamInit::~VLCPersistStreamInit()
493 STDMETHODIMP VLCPersistStreamInit::GetClassID(LPCLSID pClsID)
498 *pClsID = _p_instance->getClassID();
503 STDMETHODIMP VLCPersistStreamInit::InitNew(void)
505 return _p_instance->onInit();
508 STDMETHODIMP VLCPersistStreamInit::Load(LPSTREAM pStm)
510 HRESULT result = _p_props->Load(pStm);
514 LPPERSISTPROPERTYBAG pPersistPropBag;
515 if( FAILED(QueryInterface(IID_IPersistPropertyBag, (void**)&pPersistPropBag)) )
518 result = pPersistPropBag->Load(_p_props, NULL);
519 pPersistPropBag->Release();
524 STDMETHODIMP VLCPersistStreamInit::Save(LPSTREAM pStm, BOOL fClearDirty)
529 LPPERSISTPROPERTYBAG pPersistPropBag;
530 if( FAILED(QueryInterface(IID_IPersistPropertyBag, (void**)&pPersistPropBag)) )
533 HRESULT result = pPersistPropBag->Save(_p_props, fClearDirty, _p_props->IsEmpty());
534 pPersistPropBag->Release();
538 return _p_props->Save(pStm);
541 STDMETHODIMP VLCPersistStreamInit::IsDirty(void)
543 return _p_instance->isDirty() ? S_OK : S_FALSE;
546 STDMETHODIMP VLCPersistStreamInit::GetSizeMax(ULARGE_INTEGER *pcbSize)
548 pcbSize->HighPart = 0UL;
549 pcbSize->LowPart = 4096UL; // just a guess