persiststreaminit.cpp 14.1 KB
Newer Older
1 2 3
/*****************************************************************************
 * persiststreaminit.cpp: ActiveX control for VLC
 *****************************************************************************
4
 * Copyright (C) 2005 the VideoLAN team
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
 *
 * Authors: Damien Fouilleul <Damien.Fouilleul@laposte.net>
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
20
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21 22 23 24 25
 *****************************************************************************/

#include "plugin.h"
#include "persiststreaminit.h"

26 27 28 29 30 31
#include "utils.h"
#include <map>

#include <malloc.h>
#include <wchar.h>

32 33
using namespace std;

34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
class AxVLCVariant 
{

public:

    AxVLCVariant(void)
    {
        VariantInit(&_v);
    };

    ~AxVLCVariant(void)
    {
        VariantClear(&_v);
    }

    AxVLCVariant(VARIANTARG &v)
    {
        //VariantInit(&_v);
        //VariantCopy(&_v, &v);
        _v = v;
    };

    AxVLCVariant(VARIANTARG *v)
    {
        VariantInit(&_v);
        VariantCopy(&_v, v);
    };

    AxVLCVariant(const AxVLCVariant &vv)
    {
        VariantInit(&_v);
        VariantCopy(&_v, const_cast<VARIANTARG *>(&(vv._v)));
    };

    AxVLCVariant(int i)
    {
        V_VT(&_v) = VT_I4;
        V_I4(&_v) = i;
    };

    AxVLCVariant(BSTR bstr)
    {
        VARIANT arg;
        V_VT(&arg) = VT_BSTR;
        V_BSTR(&arg) = bstr;
Damien Fouilleul's avatar
Damien Fouilleul committed
79
        VariantInit(&_v);
80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156
        VariantCopy(&_v, &arg);
    };

    inline const VARIANTARG *variantArg(void) const {
        return &_v;
    }

    inline void swap(AxVLCVariant &v1, AxVLCVariant &v2)
    {
        VARIANTARG tmp = v1._v;
        v1._v = v2._v;
        v2._v = tmp;
    };

private:

    VARIANTARG _v;
};

class AxVLCWSTR 
{

public:

    AxVLCWSTR(void) : _data(NULL) {};

    virtual ~AxVLCWSTR()
    {
        if( NULL != _data )
        {
            ULONG refcount = InterlockedDecrement(&(_data->refcount));
            if( 0 == refcount )
                CoTaskMemFree(_data);
        }
    };

    AxVLCWSTR(LPCWSTR s)
    {
        if( NULL != s )
        {
            size_t len = wcslen(s);
            if( len > 0 )
            {
                size_t size = len*sizeof(WCHAR);
                _data = (struct data *)CoTaskMemAlloc(sizeof(struct data)+size);
                if( NULL != _data )
                {
                    _data->len = len;
                    _data->refcount = 1;
                    memcpy(_data->wstr, s, size);
                    _data->wstr[len]=L'\0';
                    return;
                }
            }
        }
        _data = NULL;
    };

    AxVLCWSTR(const AxVLCWSTR &s)
    {
        _data = s._data;
        if( NULL != _data )
            InterlockedIncrement(&(_data->refcount));
    };

    inline bool operator<(const AxVLCWSTR &s) const
    {
        return compareNoCase(s.wstr()) < 0;
    };

    inline bool operator<(LPCWSTR s) const
    {
        return compareNoCase(s) < 0;
    };

    inline bool operator==(const AxVLCWSTR &s) const
    {
Damien Fouilleul's avatar
Damien Fouilleul committed
157 158
        return size() == s.size() ?
                    (compareNoCase(s.wstr()) == 0) : false;
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
    };

    inline bool operator==(LPCWSTR s) const
    {
        return compareNoCase(s) == 0;
    };

    LPCWSTR wstr(void) const
    {
        return (NULL != _data) ? _data->wstr : NULL;
    };

    size_t size(void) const
    {
        return (NULL != _data) ? _data->len : 0;
    };

private:

    inline int compareNoCase(LPCWSTR s) const
    {
        if( NULL == _data )
        {
            return (NULL == s) ? 0 : -1;
        }
        if( NULL == s )
            return 1;

        return _wcsicmp(_data->wstr, s);
    };

    struct data {
        size_t  len;
        LONG    refcount;
        wchar_t wstr[1];
    } *_data;
};

typedef pair<class AxVLCWSTR, class AxVLCVariant> AxVLCPropertyPair;
typedef map<class AxVLCWSTR, class AxVLCVariant> AxVLCPropertyMap;

///////////////////////////

class VLCPropertyBag : public IPropertyBag
{

public:

    VLCPropertyBag(void) : _i_ref(1) {};
    virtual ~VLCPropertyBag() {};

    // IUnknown methods
    STDMETHODIMP QueryInterface(REFIID riid, void **ppv)
    {
        if( NULL == ppv )
            return E_POINTER;
        if( (IID_IUnknown == riid) 
         && (IID_IPropertyBag == riid) )
        {
            AddRef();
            *ppv = reinterpret_cast<LPVOID>(this);
            return NOERROR;
        }
        // standalone object
        return E_NOINTERFACE;
    };

    STDMETHODIMP_(ULONG) AddRef(void)
        { return InterlockedIncrement(&_i_ref); };

    STDMETHODIMP_(ULONG) Release(void)
    {
        ULONG refcount = InterlockedDecrement(&_i_ref);
        if( 0 == refcount )
        {
            delete this;
            return 0;
        }
        return refcount;
    };

    // IPropertyBag methods

    STDMETHODIMP Read(LPCOLESTR pszPropName, VARIANT *pVar, IErrorLog *pErrorLog)
    {
        if( (NULL == pszPropName) || (NULL == pVar) )
            return E_POINTER;

        AxVLCPropertyMap::const_iterator notfound = _pm.end();
        AxVLCPropertyMap::const_iterator iter = _pm.find(pszPropName);
        if( notfound != iter )
        {
            VARTYPE vtype = V_VT(pVar);
Damien Fouilleul's avatar
Damien Fouilleul committed
252 253 254 255
            VARIANTARG v;
            VariantInit(&v);
            VariantCopy(&v, const_cast<VARIANTARG*>((*iter).second.variantArg()));
            if( (V_VT(&v) != vtype) && FAILED(VariantChangeType(&v, &v, 0, vtype)) )
256
            {
Damien Fouilleul's avatar
Damien Fouilleul committed
257
                VariantClear(&v);
258 259
                return E_FAIL;
            }
Damien Fouilleul's avatar
Damien Fouilleul committed
260
            *pVar = v;
261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407
            return S_OK;
        }
        else
            return E_INVALIDARG;
    };
    
    STDMETHODIMP Write(LPCOLESTR pszPropName, VARIANT *pVar)
    {
        if( (NULL == pszPropName) || (NULL == pVar) )
            return E_POINTER;

        AxVLCPropertyPair val(pszPropName, pVar);
        pair<AxVLCPropertyMap::iterator, bool> p = _pm.insert(val);
        if( false == p.second )
            // replace existing key value
            (*p.first).second = val.second;
        return S_OK;
    };
   
    // custom methods

    HRESULT Load(LPSTREAM pStm)
    {
        if( NULL == pStm )
            return E_INVALIDARG;

        HRESULT result;

        AxVLCPropertyPair *val;
        result = ReadProperty(pStm, &val);
        if( SUCCEEDED(result) )
        {
            if( (val->first == L"(Count)") && (VT_I4 == V_VT(val->second.variantArg())) )
            {
                size_t count =  V_I4(val->second.variantArg());
                delete val;
                while( count-- )
                {
                    result = ReadProperty(pStm, &val);
                    if( FAILED(result) )
                        return result;

                    pair<AxVLCPropertyMap::iterator, bool> p = _pm.insert(*val);
                    if( false == p.second )
                        // replace existing key value
                        (*p.first).second = val->second;
                    delete val;
                }
            }
        }
        return result;
    };

    HRESULT Save(LPSTREAM pStm)
    {
        if( NULL == pStm )
            return E_INVALIDARG;

        HRESULT result;

        AxVLCPropertyPair header(L"(Count)", _pm.size());
        result = WriteProperty(pStm, header);
        if( SUCCEEDED(result) )
        {
            AxVLCPropertyMap::const_iterator iter = _pm.begin();
            AxVLCPropertyMap::const_iterator end  = _pm.end();

            while( iter != end )
            {
                result = WriteProperty(pStm, *(iter++));
                if( FAILED(result) )
                    return result;
            }
        }
        return result;
    };

    BOOL IsEmpty()
    {
        return _pm.size() == 0;
    }

private:

    HRESULT WriteProperty(LPSTREAM pStm, const AxVLCPropertyPair &prop)
    {
        HRESULT result;

        const AxVLCWSTR propName = prop.first;

        ULONG len = propName.size();

        if( 0 == len )
            return E_INVALIDARG;

        result = pStm->Write(&len, sizeof(len), NULL);
        if( FAILED(result) )
            return result;

        result = pStm->Write(propName.wstr(), len*sizeof(WCHAR), NULL);
        if( FAILED(result) )
            return result;

        const VARIANTARG *propValue = prop.second.variantArg();
        VARTYPE vtype = V_VT(propValue);
        switch( vtype )
        {
            case VT_BOOL:
                result = pStm->Write(&vtype, sizeof(vtype), NULL);
                if( FAILED(result) )
                    return result;
                result = pStm->Write(&V_BOOL(propValue), sizeof(V_BOOL(propValue)), NULL);
                if( FAILED(result) )
                    return result;
                break;
            case VT_I4:
                result = pStm->Write(&vtype, sizeof(vtype), NULL);
                if( FAILED(result) )
                    return result;
                result = pStm->Write(&V_I4(propValue), sizeof(V_I4(propValue)), NULL);
                if( FAILED(result) )
                    return result;
                break;
            case VT_BSTR:
                result = pStm->Write(&vtype, sizeof(vtype), NULL);
                if( FAILED(result) )
                    return result;
                len = SysStringLen(V_BSTR(propValue));
                result = pStm->Write(&len, sizeof(len), NULL);
                if( FAILED(result) )
                    return result;
                if( len > 0 )
                { 
                    result = pStm->Write(V_BSTR(propValue), len*sizeof(OLECHAR), NULL);
                    if( FAILED(result) )
                        return result;
                }
                break;
            default:
                vtype = VT_EMPTY;
                result = pStm->Write(&vtype, sizeof(vtype), NULL);
                if( FAILED(result) )
                    return result;
        }
        return result;
    };

Damien Fouilleul's avatar
Damien Fouilleul committed
408
    HRESULT ReadProperty(LPSTREAM pStm, AxVLCPropertyPair **prop)
409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497
    {
        HRESULT result;

        ULONG len;

        result = pStm->Read(&len, sizeof(len), NULL);
        if( FAILED(result) )
            return result;

        if( 0 == len )
            return E_INVALIDARG;

        LPWSTR propName = (LPOLESTR)::alloca((len+1)*sizeof(WCHAR));
        if( NULL == propName )
            return E_OUTOFMEMORY;

        result = pStm->Read(propName, len*sizeof(WCHAR), NULL);
        if( FAILED(result) )
            return result;

        propName[len] = L'\0';

        VARIANTARG propValue;

        VARTYPE vtype;
        result = pStm->Read(&vtype, sizeof(vtype), NULL);
        if( FAILED(result) )
            return result;

        switch( vtype )
        {
            case VT_BOOL:
                V_VT(&propValue) = vtype;
                result = pStm->Read(&V_BOOL(&propValue), sizeof(V_BOOL(&propValue)), NULL);
                if( FAILED(result) )
                    return result;
                break;
            case VT_I4:
                V_VT(&propValue) = vtype;
                result = pStm->Read(&V_I4(&propValue), sizeof(V_I4(&propValue)), NULL);
                if( FAILED(result) )
                    return result;
                break;
            case VT_BSTR:
                V_VT(&propValue) = vtype;
                result = pStm->Read(&len, sizeof(len), NULL);
                if( FAILED(result) )
                    return result;

                V_BSTR(&propValue) = NULL;
                if( len > 0 )
                {
                    V_BSTR(&propValue) = SysAllocStringLen(NULL, len);
                    if( NULL == V_BSTR(&propValue) )
                        return E_OUTOFMEMORY;

                    result = pStm->Read(V_BSTR(&propValue), len*sizeof(OLECHAR), NULL);
                    if( FAILED(result) )
                    {
                        SysFreeString(V_BSTR(&propValue));
                        return result;
                    }
                }
                break;
            default:
                VariantInit(&propValue);
        }

        *prop = new AxVLCPropertyPair(propName, propValue);

        return S_OK;
    };

    AxVLCPropertyMap _pm;
    LONG _i_ref;
};

///////////////////////////

VLCPersistStreamInit::VLCPersistStreamInit(VLCPlugin *p_instance) : _p_instance(p_instance)
{
    _p_props = new VLCPropertyBag();
};

VLCPersistStreamInit::~VLCPersistStreamInit()
{
    _p_props->Release();
};

498 499 500 501 502 503 504 505 506 507 508 509
STDMETHODIMP VLCPersistStreamInit::GetClassID(LPCLSID pClsID)
{
    if( NULL == pClsID )
        return E_POINTER;

    *pClsID = _p_instance->getClassID();

    return S_OK;
};

STDMETHODIMP VLCPersistStreamInit::InitNew(void)
{
510
    return _p_instance->onInit();
511 512 513 514
};

STDMETHODIMP VLCPersistStreamInit::Load(LPSTREAM pStm)
{
515 516 517
    HRESULT result = _p_props->Load(pStm);
    if( FAILED(result) )
        return result;
518

519 520 521 522 523 524 525 526
    LPPERSISTPROPERTYBAG pPersistPropBag;
    if( FAILED(QueryInterface(IID_IPersistPropertyBag, (void**)&pPersistPropBag)) )
        return E_FAIL;

    result = pPersistPropBag->Load(_p_props, NULL);
    pPersistPropBag->Release();

    return result;
527 528 529 530 531
};

STDMETHODIMP VLCPersistStreamInit::Save(LPSTREAM pStm, BOOL fClearDirty)
{
    if( NULL == pStm )
532
        return E_INVALIDARG;
533

534 535 536 537 538 539 540 541 542 543
    LPPERSISTPROPERTYBAG pPersistPropBag;
    if( FAILED(QueryInterface(IID_IPersistPropertyBag, (void**)&pPersistPropBag)) )
        return E_FAIL;

    HRESULT result = pPersistPropBag->Save(_p_props, fClearDirty, _p_props->IsEmpty());
    pPersistPropBag->Release();
    if( FAILED(result) )
        return result;

    return _p_props->Save(pStm);
544 545 546 547
};

STDMETHODIMP VLCPersistStreamInit::IsDirty(void)
{
548
    return _p_instance->isDirty() ? S_OK : S_FALSE;
549 550 551 552
};

STDMETHODIMP VLCPersistStreamInit::GetSizeMax(ULARGE_INTEGER *pcbSize)
{
553 554
    pcbSize->HighPart = 0UL;
    pcbSize->LowPart  = 4096UL; // just a guess
555 556 557 558

    return S_OK;
};