2 * ITfCompartmentMgr implementation
4 * Copyright 2009 Aric Stewart, CodeWeavers
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 #include "wine/debug.h"
38 #include "wine/unicode.h"
39 #include "wine/list.h"
42 #include "msctf_internal.h"
44 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
46 typedef struct tagCompartmentValue {
50 ITfCompartment *compartment;
53 typedef struct tagCompartmentMgr {
54 const ITfCompartmentMgrVtbl *CompartmentMgrVtbl;
62 typedef struct tagCompartmentEnumGuid {
63 const IEnumGUIDVtbl *Vtbl;
68 } CompartmentEnumGuid;
70 typedef struct tagCompartment {
71 const ITfCompartmentVtbl *Vtbl;
74 /* Only VT_I4, VT_UNKNOWN and VT_BSTR data types are allowed */
76 CompartmentValue *valueData;
79 static HRESULT CompartmentEnumGuid_Constructor(struct list* values, IEnumGUID **ppOut);
80 static HRESULT Compartment_Constructor(CompartmentValue *value, ITfCompartment **ppOut);
82 HRESULT CompartmentMgr_Destructor(ITfCompartmentMgr *iface)
84 CompartmentMgr *This = (CompartmentMgr *)iface;
85 struct list *cursor, *cursor2;
87 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->values)
89 CompartmentValue* value = LIST_ENTRY(cursor,CompartmentValue,entry);
91 ITfCompartment_Release(value->compartment);
92 HeapFree(GetProcessHeap(),0,value);
95 HeapFree(GetProcessHeap(),0,This);
99 /*****************************************************
100 * ITfCompartmentMgr functions
101 *****************************************************/
102 static HRESULT WINAPI CompartmentMgr_QueryInterface(ITfCompartmentMgr *iface, REFIID iid, LPVOID *ppvOut)
104 CompartmentMgr *This = (CompartmentMgr *)iface;
106 return IUnknown_QueryInterface(This->pUnkOuter, iid, *ppvOut);
111 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfCompartmentMgr))
118 IUnknown_AddRef(iface);
122 WARN("unsupported interface: %s\n", debugstr_guid(iid));
123 return E_NOINTERFACE;
127 static ULONG WINAPI CompartmentMgr_AddRef(ITfCompartmentMgr *iface)
129 CompartmentMgr *This = (CompartmentMgr *)iface;
131 return IUnknown_AddRef(This->pUnkOuter);
133 return InterlockedIncrement(&This->refCount);
136 static ULONG WINAPI CompartmentMgr_Release(ITfCompartmentMgr *iface)
138 CompartmentMgr *This = (CompartmentMgr *)iface;
140 return IUnknown_Release(This->pUnkOuter);
145 ret = InterlockedDecrement(&This->refCount);
147 CompartmentMgr_Destructor(iface);
152 static HRESULT WINAPI CompartmentMgr_GetCompartment(ITfCompartmentMgr *iface,
153 REFGUID rguid, ITfCompartment **ppcomp)
155 CompartmentMgr *This = (CompartmentMgr *)iface;
156 CompartmentValue* value;
160 TRACE("(%p) %s %p\n",This,debugstr_guid(rguid),ppcomp);
162 LIST_FOR_EACH(cursor, &This->values)
164 value = LIST_ENTRY(cursor,CompartmentValue,entry);
165 if (IsEqualGUID(rguid,&value->guid))
167 ITfCompartment_AddRef(value->compartment);
168 *ppcomp = value->compartment;
173 value = HeapAlloc(GetProcessHeap(),0,sizeof(CompartmentValue));
174 value->guid = *rguid;
176 hr = Compartment_Constructor(value,&value->compartment);
179 list_add_head(&This->values,&value->entry);
180 ITfCompartment_AddRef(value->compartment);
181 *ppcomp = value->compartment;
185 HeapFree(GetProcessHeap(),0,value);
191 static HRESULT WINAPI CompartmentMgr_ClearCompartment(ITfCompartmentMgr *iface,
192 TfClientId tid, REFGUID rguid)
195 CompartmentMgr *This = (CompartmentMgr *)iface;
196 TRACE("(%p) %i %s\n",This,tid,debugstr_guid(rguid));
198 LIST_FOR_EACH(cursor, &This->values)
200 CompartmentValue* value = LIST_ENTRY(cursor,CompartmentValue,entry);
201 if (IsEqualGUID(rguid,&value->guid))
203 if (value->owner && tid != value->owner)
206 ITfCompartment_Release(value->compartment);
207 HeapFree(GetProcessHeap(),0,value);
212 return CONNECT_E_NOCONNECTION;
215 static HRESULT WINAPI CompartmentMgr_EnumCompartments(ITfCompartmentMgr *iface,
218 CompartmentMgr *This = (CompartmentMgr *)iface;
219 TRACE("(%p) %p\n",This,ppEnum);
222 return CompartmentEnumGuid_Constructor(&This->values, ppEnum);
225 static const ITfCompartmentMgrVtbl CompartmentMgr_CompartmentMgrVtbl =
227 CompartmentMgr_QueryInterface,
228 CompartmentMgr_AddRef,
229 CompartmentMgr_Release,
231 CompartmentMgr_GetCompartment,
232 CompartmentMgr_ClearCompartment,
233 CompartmentMgr_EnumCompartments
236 HRESULT CompartmentMgr_Constructor(IUnknown *pUnkOuter, REFIID riid, IUnknown **ppOut)
238 CompartmentMgr *This;
243 if (pUnkOuter && !IsEqualIID (riid, &IID_IUnknown))
244 return CLASS_E_NOAGGREGATION;
246 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CompartmentMgr));
248 return E_OUTOFMEMORY;
250 This->CompartmentMgrVtbl = &CompartmentMgr_CompartmentMgrVtbl;
251 This->pUnkOuter = pUnkOuter;
252 list_init(&This->values);
256 TRACE("returning %p\n", This);
257 *ppOut = (IUnknown*)This;
263 hr = IUnknown_QueryInterface((IUnknown*)This, riid, (LPVOID*)ppOut);
265 HeapFree(GetProcessHeap(),0,This);
270 /**************************************************
271 * IEnumGUID implementaion for ITfCompartmentMgr::EnumCompartments
272 **************************************************/
273 static void CompartmentEnumGuid_Destructor(CompartmentEnumGuid *This)
275 TRACE("destroying %p\n", This);
276 HeapFree(GetProcessHeap(),0,This);
279 static HRESULT WINAPI CompartmentEnumGuid_QueryInterface(IEnumGUID *iface, REFIID iid, LPVOID *ppvOut)
281 CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
284 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumGUID))
291 IUnknown_AddRef(iface);
295 WARN("unsupported interface: %s\n", debugstr_guid(iid));
296 return E_NOINTERFACE;
299 static ULONG WINAPI CompartmentEnumGuid_AddRef(IEnumGUID *iface)
301 CompartmentEnumGuid *This = (CompartmentEnumGuid*)iface;
302 return InterlockedIncrement(&This->refCount);
305 static ULONG WINAPI CompartmentEnumGuid_Release(IEnumGUID *iface)
307 CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
310 ret = InterlockedDecrement(&This->refCount);
312 CompartmentEnumGuid_Destructor(This);
316 /*****************************************************
317 * IEnumGuid functions
318 *****************************************************/
319 static HRESULT WINAPI CompartmentEnumGuid_Next( LPENUMGUID iface,
320 ULONG celt, GUID *rgelt, ULONG *pceltFetched)
322 CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
325 TRACE("(%p)\n",This);
327 if (rgelt == NULL) return E_POINTER;
329 while (fetched < celt && This->cursor)
331 CompartmentValue* value = LIST_ENTRY(This->cursor,CompartmentValue,entry);
335 This->cursor = list_next(This->values,This->cursor);
336 *rgelt = value->guid;
342 if (pceltFetched) *pceltFetched = fetched;
343 return fetched == celt ? S_OK : S_FALSE;
346 static HRESULT WINAPI CompartmentEnumGuid_Skip( LPENUMGUID iface, ULONG celt)
348 CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
349 TRACE("(%p)\n",This);
351 This->cursor = list_next(This->values,This->cursor);
355 static HRESULT WINAPI CompartmentEnumGuid_Reset( LPENUMGUID iface)
357 CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
358 TRACE("(%p)\n",This);
359 This->cursor = list_head(This->values);
363 static HRESULT WINAPI CompartmentEnumGuid_Clone( LPENUMGUID iface,
366 CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
369 TRACE("(%p)\n",This);
371 if (ppenum == NULL) return E_POINTER;
373 res = CompartmentEnumGuid_Constructor(This->values, ppenum);
376 CompartmentEnumGuid *new_This = (CompartmentEnumGuid *)*ppenum;
377 new_This->cursor = This->cursor;
382 static const IEnumGUIDVtbl IEnumGUID_Vtbl ={
383 CompartmentEnumGuid_QueryInterface,
384 CompartmentEnumGuid_AddRef,
385 CompartmentEnumGuid_Release,
387 CompartmentEnumGuid_Next,
388 CompartmentEnumGuid_Skip,
389 CompartmentEnumGuid_Reset,
390 CompartmentEnumGuid_Clone
393 static HRESULT CompartmentEnumGuid_Constructor(struct list *values, IEnumGUID **ppOut)
395 CompartmentEnumGuid *This;
397 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CompartmentEnumGuid));
399 return E_OUTOFMEMORY;
401 This->Vtbl= &IEnumGUID_Vtbl;
404 This->values = values;
405 This->cursor = list_head(values);
407 TRACE("returning %p\n", This);
408 *ppOut = (IEnumGUID*)This;
412 /**************************************************
414 **************************************************/
415 static void Compartment_Destructor(Compartment *This)
417 TRACE("destroying %p\n", This);
418 VariantClear(&This->variant);
419 HeapFree(GetProcessHeap(),0,This);
422 static HRESULT WINAPI Compartment_QueryInterface(ITfCompartment *iface, REFIID iid, LPVOID *ppvOut)
424 Compartment *This = (Compartment *)iface;
427 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfCompartment))
434 IUnknown_AddRef(iface);
438 WARN("unsupported interface: %s\n", debugstr_guid(iid));
439 return E_NOINTERFACE;
442 static ULONG WINAPI Compartment_AddRef(ITfCompartment *iface)
444 Compartment *This = (Compartment*)iface;
445 return InterlockedIncrement(&This->refCount);
448 static ULONG WINAPI Compartment_Release(ITfCompartment *iface)
450 Compartment *This = (Compartment *)iface;
453 ret = InterlockedDecrement(&This->refCount);
455 Compartment_Destructor(This);
459 static HRESULT WINAPI Compartment_SetValue(ITfCompartment *iface,
460 TfClientId tid, const VARIANT *pvarValue)
462 Compartment *This = (Compartment *)iface;
464 TRACE("(%p) %i %p\n",This,tid,pvarValue);
469 if (!(V_VT(pvarValue) == VT_BSTR || V_VT(pvarValue) == VT_I4 ||
470 V_VT(pvarValue) == VT_UNKNOWN))
473 if (!This->valueData->owner)
474 This->valueData->owner = tid;
476 VariantClear(&This->variant);
478 /* Shallow copy of value and type */
479 This->variant = *pvarValue;
481 if (V_VT(pvarValue) == VT_BSTR)
482 V_BSTR(&This->variant) = SysAllocStringByteLen((char*)V_BSTR(pvarValue),
483 SysStringByteLen(V_BSTR(pvarValue)));
484 else if (V_VT(pvarValue) == VT_UNKNOWN)
485 IUnknown_AddRef(V_UNKNOWN(&This->variant));
490 static HRESULT WINAPI Compartment_GetValue(ITfCompartment *iface,
494 Compartment *This = (Compartment *)iface;
495 TRACE("(%p) %p\n",This, pvarValue);
500 pvarValue->n1.n2.vt = VT_EMPTY;
501 if (!This->variant.n1.n2.vt == VT_EMPTY)
502 hr = VariantCopy(pvarValue,&This->variant);
506 static const ITfCompartmentVtbl ITfCompartment_Vtbl ={
507 Compartment_QueryInterface,
511 Compartment_SetValue,
515 static HRESULT Compartment_Constructor(CompartmentValue *valueData, ITfCompartment **ppOut)
519 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(Compartment));
521 return E_OUTOFMEMORY;
523 This->Vtbl= &ITfCompartment_Vtbl;
526 This->valueData = valueData;
527 VariantInit(&This->variant);
529 TRACE("returning %p\n", This);
530 *ppOut = (ITfCompartment*)This;