Sweden-Number/dlls/msctf/compartmentmgr.c

630 lines
17 KiB
C
Raw Normal View History

/*
* ITfCompartmentMgr implementation
*
* Copyright 2009 Aric Stewart, CodeWeavers
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
*/
#include "config.h"
#include <stdarg.h>
#define COBJMACROS
#include "wine/debug.h"
#include "windef.h"
#include "winbase.h"
#include "winreg.h"
#include "winuser.h"
#include "shlwapi.h"
#include "winerror.h"
#include "objbase.h"
#include "oleauto.h"
#include "olectl.h"
#include "wine/unicode.h"
#include "msctf.h"
#include "msctf_internal.h"
WINE_DEFAULT_DEBUG_CHANNEL(msctf);
typedef struct tagCompartmentValue {
struct list entry;
GUID guid;
TfClientId owner;
ITfCompartment *compartment;
} CompartmentValue;
typedef struct tagCompartmentMgr {
2013-11-17 09:21:51 +01:00
ITfCompartmentMgr ITfCompartmentMgr_iface;
LONG refCount;
IUnknown *pUnkOuter;
struct list values;
} CompartmentMgr;
typedef struct tagCompartmentEnumGuid {
2013-11-17 09:21:51 +01:00
IEnumGUID IEnumGUID_iface;
LONG refCount;
struct list *values;
struct list *cursor;
} CompartmentEnumGuid;
2009-06-22 05:02:08 +02:00
typedef struct tagCompartment {
2013-11-17 09:21:51 +01:00
ITfCompartment ITfCompartment_iface;
ITfSource ITfSource_iface;
2009-06-22 05:02:08 +02:00
LONG refCount;
/* Only VT_I4, VT_UNKNOWN and VT_BSTR data types are allowed */
VARIANT variant;
CompartmentValue *valueData;
struct list CompartmentEventSink;
2009-06-22 05:02:08 +02:00
} Compartment;
static HRESULT CompartmentEnumGuid_Constructor(struct list* values, IEnumGUID **ppOut);
2009-06-22 05:02:08 +02:00
static HRESULT Compartment_Constructor(CompartmentValue *value, ITfCompartment **ppOut);
2013-11-17 09:21:51 +01:00
static inline CompartmentMgr *impl_from_ITfCompartmentMgr(ITfCompartmentMgr *iface)
{
2013-11-17 09:21:51 +01:00
return CONTAINING_RECORD(iface, CompartmentMgr, ITfCompartmentMgr_iface);
}
static inline Compartment *impl_from_ITfCompartment(ITfCompartment *iface)
{
return CONTAINING_RECORD(iface, Compartment, ITfCompartment_iface);
}
static inline Compartment *impl_from_ITfSource(ITfSource *iface)
{
return CONTAINING_RECORD(iface, Compartment, ITfSource_iface);
}
static inline CompartmentEnumGuid *impl_from_IEnumGUID(IEnumGUID *iface)
{
return CONTAINING_RECORD(iface, CompartmentEnumGuid, IEnumGUID_iface);
}
HRESULT CompartmentMgr_Destructor(ITfCompartmentMgr *iface)
{
2013-11-17 09:21:51 +01:00
CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
struct list *cursor, *cursor2;
LIST_FOR_EACH_SAFE(cursor, cursor2, &This->values)
{
CompartmentValue* value = LIST_ENTRY(cursor,CompartmentValue,entry);
list_remove(cursor);
ITfCompartment_Release(value->compartment);
HeapFree(GetProcessHeap(),0,value);
}
HeapFree(GetProcessHeap(),0,This);
return S_OK;
}
/*****************************************************
* ITfCompartmentMgr functions
*****************************************************/
static HRESULT WINAPI CompartmentMgr_QueryInterface(ITfCompartmentMgr *iface, REFIID iid, LPVOID *ppvOut)
{
2013-11-17 09:21:51 +01:00
CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
if (This->pUnkOuter)
return IUnknown_QueryInterface(This->pUnkOuter, iid, ppvOut);
else
{
*ppvOut = NULL;
if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfCompartmentMgr))
{
*ppvOut = &This->ITfCompartmentMgr_iface;
}
if (*ppvOut)
{
ITfCompartmentMgr_AddRef(iface);
return S_OK;
}
WARN("unsupported interface: %s\n", debugstr_guid(iid));
return E_NOINTERFACE;
}
}
static ULONG WINAPI CompartmentMgr_AddRef(ITfCompartmentMgr *iface)
{
2013-11-17 09:21:51 +01:00
CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
if (This->pUnkOuter)
return IUnknown_AddRef(This->pUnkOuter);
else
return InterlockedIncrement(&This->refCount);
}
static ULONG WINAPI CompartmentMgr_Release(ITfCompartmentMgr *iface)
{
2013-11-17 09:21:51 +01:00
CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
if (This->pUnkOuter)
return IUnknown_Release(This->pUnkOuter);
else
{
ULONG ret;
ret = InterlockedDecrement(&This->refCount);
if (ret == 0)
CompartmentMgr_Destructor(iface);
return ret;
}
}
static HRESULT WINAPI CompartmentMgr_GetCompartment(ITfCompartmentMgr *iface,
REFGUID rguid, ITfCompartment **ppcomp)
{
2013-11-17 09:21:51 +01:00
CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
CompartmentValue* value;
struct list *cursor;
HRESULT hr;
TRACE("(%p) %s %p\n",This,debugstr_guid(rguid),ppcomp);
LIST_FOR_EACH(cursor, &This->values)
{
value = LIST_ENTRY(cursor,CompartmentValue,entry);
if (IsEqualGUID(rguid,&value->guid))
{
ITfCompartment_AddRef(value->compartment);
*ppcomp = value->compartment;
return S_OK;
}
}
value = HeapAlloc(GetProcessHeap(),0,sizeof(CompartmentValue));
value->guid = *rguid;
value->owner = 0;
hr = Compartment_Constructor(value,&value->compartment);
if (SUCCEEDED(hr))
{
list_add_head(&This->values,&value->entry);
ITfCompartment_AddRef(value->compartment);
*ppcomp = value->compartment;
}
else
{
HeapFree(GetProcessHeap(),0,value);
*ppcomp = NULL;
}
return hr;
}
static HRESULT WINAPI CompartmentMgr_ClearCompartment(ITfCompartmentMgr *iface,
TfClientId tid, REFGUID rguid)
{
2013-11-17 09:21:51 +01:00
CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
struct list *cursor;
2013-11-17 09:21:51 +01:00
TRACE("(%p) %i %s\n",This,tid,debugstr_guid(rguid));
LIST_FOR_EACH(cursor, &This->values)
{
CompartmentValue* value = LIST_ENTRY(cursor,CompartmentValue,entry);
if (IsEqualGUID(rguid,&value->guid))
{
if (value->owner && tid != value->owner)
return E_UNEXPECTED;
list_remove(cursor);
ITfCompartment_Release(value->compartment);
HeapFree(GetProcessHeap(),0,value);
return S_OK;
}
}
return CONNECT_E_NOCONNECTION;
}
static HRESULT WINAPI CompartmentMgr_EnumCompartments(ITfCompartmentMgr *iface,
IEnumGUID **ppEnum)
{
2013-11-17 09:21:51 +01:00
CompartmentMgr *This = impl_from_ITfCompartmentMgr(iface);
TRACE("(%p) %p\n",This,ppEnum);
if (!ppEnum)
return E_INVALIDARG;
return CompartmentEnumGuid_Constructor(&This->values, ppEnum);
}
2013-11-17 09:21:51 +01:00
static const ITfCompartmentMgrVtbl CompartmentMgrVtbl =
{
CompartmentMgr_QueryInterface,
CompartmentMgr_AddRef,
CompartmentMgr_Release,
CompartmentMgr_GetCompartment,
CompartmentMgr_ClearCompartment,
CompartmentMgr_EnumCompartments
};
HRESULT CompartmentMgr_Constructor(IUnknown *pUnkOuter, REFIID riid, IUnknown **ppOut)
{
CompartmentMgr *This;
if (!ppOut)
return E_POINTER;
if (pUnkOuter && !IsEqualIID (riid, &IID_IUnknown))
return CLASS_E_NOAGGREGATION;
This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CompartmentMgr));
if (This == NULL)
return E_OUTOFMEMORY;
2013-11-17 09:21:51 +01:00
This->ITfCompartmentMgr_iface.lpVtbl = &CompartmentMgrVtbl;
This->pUnkOuter = pUnkOuter;
list_init(&This->values);
if (pUnkOuter)
{
*ppOut = (IUnknown*)&This->ITfCompartmentMgr_iface;
TRACE("returning %p\n", *ppOut);
return S_OK;
}
else
{
HRESULT hr;
2013-11-17 09:21:51 +01:00
hr = ITfCompartmentMgr_QueryInterface(&This->ITfCompartmentMgr_iface, riid, (void**)ppOut);
if (FAILED(hr))
HeapFree(GetProcessHeap(),0,This);
return hr;
}
}
/**************************************************
2011-08-02 22:20:32 +02:00
* IEnumGUID implementation for ITfCompartmentMgr::EnumCompartments
**************************************************/
static void CompartmentEnumGuid_Destructor(CompartmentEnumGuid *This)
{
TRACE("destroying %p\n", This);
HeapFree(GetProcessHeap(),0,This);
}
static HRESULT WINAPI CompartmentEnumGuid_QueryInterface(IEnumGUID *iface, REFIID iid, LPVOID *ppvOut)
{
2013-11-17 09:21:51 +01:00
CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
*ppvOut = NULL;
if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumGUID))
{
2013-11-17 09:21:51 +01:00
*ppvOut = &This->IEnumGUID_iface;
}
if (*ppvOut)
{
IEnumGUID_AddRef(iface);
return S_OK;
}
WARN("unsupported interface: %s\n", debugstr_guid(iid));
return E_NOINTERFACE;
}
static ULONG WINAPI CompartmentEnumGuid_AddRef(IEnumGUID *iface)
{
2013-11-17 09:21:51 +01:00
CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
return InterlockedIncrement(&This->refCount);
}
static ULONG WINAPI CompartmentEnumGuid_Release(IEnumGUID *iface)
{
2013-11-17 09:21:51 +01:00
CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
ULONG ret;
ret = InterlockedDecrement(&This->refCount);
if (ret == 0)
CompartmentEnumGuid_Destructor(This);
return ret;
}
/*****************************************************
* IEnumGuid functions
*****************************************************/
2013-11-17 09:21:51 +01:00
static HRESULT WINAPI CompartmentEnumGuid_Next(IEnumGUID *iface,
ULONG celt, GUID *rgelt, ULONG *pceltFetched)
{
2013-11-17 09:21:51 +01:00
CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
ULONG fetched = 0;
TRACE("(%p)\n",This);
if (rgelt == NULL) return E_POINTER;
while (fetched < celt && This->cursor)
{
CompartmentValue* value = LIST_ENTRY(This->cursor,CompartmentValue,entry);
if (!value)
break;
This->cursor = list_next(This->values,This->cursor);
*rgelt = value->guid;
++fetched;
++rgelt;
}
if (pceltFetched) *pceltFetched = fetched;
return fetched == celt ? S_OK : S_FALSE;
}
2013-11-17 09:21:51 +01:00
static HRESULT WINAPI CompartmentEnumGuid_Skip(IEnumGUID *iface, ULONG celt)
{
2013-11-17 09:21:51 +01:00
CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
TRACE("(%p)\n",This);
This->cursor = list_next(This->values,This->cursor);
return S_OK;
}
2013-11-17 09:21:51 +01:00
static HRESULT WINAPI CompartmentEnumGuid_Reset(IEnumGUID *iface)
{
2013-11-17 09:21:51 +01:00
CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
TRACE("(%p)\n",This);
This->cursor = list_head(This->values);
return S_OK;
}
2013-11-17 09:21:51 +01:00
static HRESULT WINAPI CompartmentEnumGuid_Clone(IEnumGUID *iface,
IEnumGUID **ppenum)
{
2013-11-17 09:21:51 +01:00
CompartmentEnumGuid *This = impl_from_IEnumGUID(iface);
HRESULT res;
TRACE("(%p)\n",This);
if (ppenum == NULL) return E_POINTER;
res = CompartmentEnumGuid_Constructor(This->values, ppenum);
if (SUCCEEDED(res))
{
2013-11-17 09:21:51 +01:00
CompartmentEnumGuid *new_This = impl_from_IEnumGUID(*ppenum);
new_This->cursor = This->cursor;
}
return res;
}
2013-11-17 09:21:51 +01:00
static const IEnumGUIDVtbl EnumGUIDVtbl =
{
CompartmentEnumGuid_QueryInterface,
CompartmentEnumGuid_AddRef,
CompartmentEnumGuid_Release,
CompartmentEnumGuid_Next,
CompartmentEnumGuid_Skip,
CompartmentEnumGuid_Reset,
CompartmentEnumGuid_Clone
};
static HRESULT CompartmentEnumGuid_Constructor(struct list *values, IEnumGUID **ppOut)
{
CompartmentEnumGuid *This;
This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CompartmentEnumGuid));
if (This == NULL)
return E_OUTOFMEMORY;
2013-11-17 09:21:51 +01:00
This->IEnumGUID_iface.lpVtbl= &EnumGUIDVtbl;
This->refCount = 1;
This->values = values;
This->cursor = list_head(values);
*ppOut = &This->IEnumGUID_iface;
TRACE("returning %p\n", *ppOut);
return S_OK;
}
2009-06-22 05:02:08 +02:00
/**************************************************
* ITfCompartment
**************************************************/
static void Compartment_Destructor(Compartment *This)
{
TRACE("destroying %p\n", This);
VariantClear(&This->variant);
free_sinks(&This->CompartmentEventSink);
2009-06-22 05:02:08 +02:00
HeapFree(GetProcessHeap(),0,This);
}
static HRESULT WINAPI Compartment_QueryInterface(ITfCompartment *iface, REFIID iid, LPVOID *ppvOut)
{
2013-11-17 09:21:51 +01:00
Compartment *This = impl_from_ITfCompartment(iface);
2009-06-22 05:02:08 +02:00
*ppvOut = NULL;
if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfCompartment))
{
2013-11-17 09:21:51 +01:00
*ppvOut = &This->ITfCompartment_iface;
2009-06-22 05:02:08 +02:00
}
else if (IsEqualIID(iid, &IID_ITfSource))
{
2013-11-17 09:21:51 +01:00
*ppvOut = &This->ITfSource_iface;
}
2009-06-22 05:02:08 +02:00
if (*ppvOut)
{
ITfCompartment_AddRef(iface);
2009-06-22 05:02:08 +02:00
return S_OK;
}
WARN("unsupported interface: %s\n", debugstr_guid(iid));
return E_NOINTERFACE;
}
static ULONG WINAPI Compartment_AddRef(ITfCompartment *iface)
{
2013-11-17 09:21:51 +01:00
Compartment *This = impl_from_ITfCompartment(iface);
2009-06-22 05:02:08 +02:00
return InterlockedIncrement(&This->refCount);
}
static ULONG WINAPI Compartment_Release(ITfCompartment *iface)
{
2013-11-17 09:21:51 +01:00
Compartment *This = impl_from_ITfCompartment(iface);
2009-06-22 05:02:08 +02:00
ULONG ret;
ret = InterlockedDecrement(&This->refCount);
if (ret == 0)
Compartment_Destructor(This);
return ret;
}
static HRESULT WINAPI Compartment_SetValue(ITfCompartment *iface,
TfClientId tid, const VARIANT *pvarValue)
{
2013-11-17 09:21:51 +01:00
Compartment *This = impl_from_ITfCompartment(iface);
ITfCompartmentEventSink *sink;
struct list *cursor;
TRACE("(%p) %i %p\n",This,tid,pvarValue);
if (!pvarValue)
return E_INVALIDARG;
if (!(V_VT(pvarValue) == VT_BSTR || V_VT(pvarValue) == VT_I4 ||
V_VT(pvarValue) == VT_UNKNOWN))
return E_INVALIDARG;
if (!This->valueData->owner)
This->valueData->owner = tid;
VariantClear(&This->variant);
/* Shallow copy of value and type */
This->variant = *pvarValue;
if (V_VT(pvarValue) == VT_BSTR)
V_BSTR(&This->variant) = SysAllocStringByteLen((char*)V_BSTR(pvarValue),
SysStringByteLen(V_BSTR(pvarValue)));
else if (V_VT(pvarValue) == VT_UNKNOWN)
IUnknown_AddRef(V_UNKNOWN(&This->variant));
SINK_FOR_EACH(cursor, &This->CompartmentEventSink, ITfCompartmentEventSink, sink)
{
ITfCompartmentEventSink_OnChange(sink, &This->valueData->guid);
}
return S_OK;
2009-06-22 05:02:08 +02:00
}
static HRESULT WINAPI Compartment_GetValue(ITfCompartment *iface,
VARIANT *pvarValue)
{
2013-11-17 09:21:51 +01:00
Compartment *This = impl_from_ITfCompartment(iface);
TRACE("(%p) %p\n",This, pvarValue);
if (!pvarValue)
return E_INVALIDARG;
VariantInit(pvarValue);
if (V_VT(&This->variant) == VT_EMPTY) return S_FALSE;
return VariantCopy(pvarValue,&This->variant);
2009-06-22 05:02:08 +02:00
}
2013-11-17 09:21:51 +01:00
static const ITfCompartmentVtbl CompartmentVtbl =
{
2009-06-22 05:02:08 +02:00
Compartment_QueryInterface,
Compartment_AddRef,
Compartment_Release,
Compartment_SetValue,
Compartment_GetValue
};
/*****************************************************
* ITfSource functions
*****************************************************/
2013-11-17 09:21:51 +01:00
static HRESULT WINAPI CompartmentSource_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
{
2013-11-17 09:21:51 +01:00
Compartment *This = impl_from_ITfSource(iface);
return ITfCompartment_QueryInterface(&This->ITfCompartment_iface, iid, ppvOut);
}
2013-11-17 09:21:51 +01:00
static ULONG WINAPI CompartmentSource_AddRef(ITfSource *iface)
{
2013-11-17 09:21:51 +01:00
Compartment *This = impl_from_ITfSource(iface);
return ITfCompartment_AddRef(&This->ITfCompartment_iface);
}
2013-11-17 09:21:51 +01:00
static ULONG WINAPI CompartmentSource_Release(ITfSource *iface)
{
2013-11-17 09:21:51 +01:00
Compartment *This = impl_from_ITfSource(iface);
return ITfCompartment_Release(&This->ITfCompartment_iface);
}
static HRESULT WINAPI CompartmentSource_AdviseSink(ITfSource *iface,
REFIID riid, IUnknown *punk, DWORD *pdwCookie)
{
2013-11-17 09:21:51 +01:00
Compartment *This = impl_from_ITfSource(iface);
TRACE("(%p) %s %p %p\n",This,debugstr_guid(riid),punk,pdwCookie);
if (!riid || !punk || !pdwCookie)
return E_INVALIDARG;
if (IsEqualIID(riid, &IID_ITfCompartmentEventSink))
return advise_sink(&This->CompartmentEventSink, &IID_ITfCompartmentEventSink,
COOKIE_MAGIC_COMPARTMENTSINK, punk, pdwCookie);
FIXME("(%p) Unhandled Sink: %s\n",This,debugstr_guid(riid));
return E_NOTIMPL;
}
static HRESULT WINAPI CompartmentSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
{
2013-11-17 09:21:51 +01:00
Compartment *This = impl_from_ITfSource(iface);
TRACE("(%p) %x\n",This,pdwCookie);
if (get_Cookie_magic(pdwCookie)!=COOKIE_MAGIC_COMPARTMENTSINK)
return E_INVALIDARG;
return unadvise_sink(pdwCookie);
}
2013-11-17 09:21:51 +01:00
static const ITfSourceVtbl CompartmentSourceVtbl =
{
2013-11-17 09:21:51 +01:00
CompartmentSource_QueryInterface,
CompartmentSource_AddRef,
CompartmentSource_Release,
CompartmentSource_AdviseSink,
CompartmentSource_UnadviseSink,
};
2009-06-22 05:02:08 +02:00
static HRESULT Compartment_Constructor(CompartmentValue *valueData, ITfCompartment **ppOut)
{
Compartment *This;
This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(Compartment));
if (This == NULL)
return E_OUTOFMEMORY;
2013-11-17 09:21:51 +01:00
This->ITfCompartment_iface.lpVtbl= &CompartmentVtbl;
This->ITfSource_iface.lpVtbl = &CompartmentSourceVtbl;
2009-06-22 05:02:08 +02:00
This->refCount = 1;
This->valueData = valueData;
VariantInit(&This->variant);
list_init(&This->CompartmentEventSink);
*ppOut = &This->ITfCompartment_iface;
TRACE("returning %p\n", *ppOut);
2009-06-22 05:02:08 +02:00
return S_OK;
}