msctf: Implement ITfCompartmentMgr::GetCompartment.
[wine] / dlls / msctf / compartmentmgr.c
1 /*
2  *  ITfCompartmentMgr implementation
3  *
4  *  Copyright 2009 Aric Stewart, CodeWeavers
5  *
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.
10  *
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.
15  *
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
19  */
20
21 #include "config.h"
22
23 #include <stdarg.h>
24
25 #define COBJMACROS
26
27 #include "wine/debug.h"
28 #include "windef.h"
29 #include "winbase.h"
30 #include "winreg.h"
31 #include "winuser.h"
32 #include "shlwapi.h"
33 #include "winerror.h"
34 #include "objbase.h"
35 #include "oleauto.h"
36 #include "olectl.h"
37
38 #include "wine/unicode.h"
39 #include "wine/list.h"
40
41 #include "msctf.h"
42 #include "msctf_internal.h"
43
44 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
45
46 typedef struct tagCompartmentValue {
47     struct list entry;
48     GUID guid;
49     TfClientId owner;
50     ITfCompartment *compartment;
51 } CompartmentValue;
52
53 typedef struct tagCompartmentMgr {
54     const ITfCompartmentMgrVtbl *CompartmentMgrVtbl;
55     LONG refCount;
56
57     IUnknown *pUnkOuter;
58
59     struct list values;
60 } CompartmentMgr;
61
62 typedef struct tagCompartmentEnumGuid {
63     const IEnumGUIDVtbl *Vtbl;
64     LONG refCount;
65
66     struct list *values;
67     struct list *cursor;
68 } CompartmentEnumGuid;
69
70 typedef struct tagCompartment {
71     const ITfCompartmentVtbl *Vtbl;
72     LONG refCount;
73
74     /* Only VT_I4, VT_UNKNOWN and VT_BSTR data types are allowed */
75     VARIANT variant;
76     CompartmentValue *valueData;
77 } Compartment;
78
79 static HRESULT CompartmentEnumGuid_Constructor(struct list* values, IEnumGUID **ppOut);
80 static HRESULT Compartment_Constructor(CompartmentValue *value, ITfCompartment **ppOut);
81
82 HRESULT CompartmentMgr_Destructor(ITfCompartmentMgr *iface)
83 {
84     CompartmentMgr *This = (CompartmentMgr *)iface;
85     struct list *cursor, *cursor2;
86
87     LIST_FOR_EACH_SAFE(cursor, cursor2, &This->values)
88     {
89         CompartmentValue* value = LIST_ENTRY(cursor,CompartmentValue,entry);
90         list_remove(cursor);
91         ITfCompartment_Release(value->compartment);
92         HeapFree(GetProcessHeap(),0,value);
93     }
94
95     HeapFree(GetProcessHeap(),0,This);
96     return S_OK;
97 }
98
99 /*****************************************************
100  * ITfCompartmentMgr functions
101  *****************************************************/
102 static HRESULT WINAPI CompartmentMgr_QueryInterface(ITfCompartmentMgr *iface, REFIID iid, LPVOID *ppvOut)
103 {
104     CompartmentMgr *This = (CompartmentMgr *)iface;
105     if (This->pUnkOuter)
106         return IUnknown_QueryInterface(This->pUnkOuter, iid, *ppvOut);
107     else
108     {
109         *ppvOut = NULL;
110
111         if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfCompartmentMgr))
112         {
113             *ppvOut = This;
114         }
115
116         if (*ppvOut)
117         {
118             IUnknown_AddRef(iface);
119             return S_OK;
120         }
121
122         WARN("unsupported interface: %s\n", debugstr_guid(iid));
123         return E_NOINTERFACE;
124     }
125 }
126
127 static ULONG WINAPI CompartmentMgr_AddRef(ITfCompartmentMgr *iface)
128 {
129     CompartmentMgr *This = (CompartmentMgr *)iface;
130     if (This->pUnkOuter)
131         return IUnknown_AddRef(This->pUnkOuter);
132     else
133         return InterlockedIncrement(&This->refCount);
134 }
135
136 static ULONG WINAPI CompartmentMgr_Release(ITfCompartmentMgr *iface)
137 {
138     CompartmentMgr *This = (CompartmentMgr *)iface;
139     if (This->pUnkOuter)
140         return IUnknown_Release(This->pUnkOuter);
141     else
142     {
143         ULONG ret;
144
145         ret = InterlockedDecrement(&This->refCount);
146         if (ret == 0)
147             CompartmentMgr_Destructor(iface);
148         return ret;
149     }
150 }
151
152 static HRESULT WINAPI CompartmentMgr_GetCompartment(ITfCompartmentMgr *iface,
153         REFGUID rguid, ITfCompartment **ppcomp)
154 {
155     CompartmentMgr *This = (CompartmentMgr *)iface;
156     CompartmentValue* value;
157     struct list *cursor;
158     HRESULT hr;
159
160     TRACE("(%p) %s  %p\n",This,debugstr_guid(rguid),ppcomp);
161
162     LIST_FOR_EACH(cursor, &This->values)
163     {
164         value = LIST_ENTRY(cursor,CompartmentValue,entry);
165         if (IsEqualGUID(rguid,&value->guid))
166         {
167             ITfCompartment_AddRef(value->compartment);
168             *ppcomp = value->compartment;
169             return S_OK;
170         }
171     }
172
173     value = HeapAlloc(GetProcessHeap(),0,sizeof(CompartmentValue));
174     value->guid = *rguid;
175     value->owner = 0;
176     hr = Compartment_Constructor(value,&value->compartment);
177     if (SUCCEEDED(hr))
178     {
179         list_add_head(&This->values,&value->entry);
180         ITfCompartment_AddRef(value->compartment);
181         *ppcomp = value->compartment;
182     }
183     else
184     {
185         HeapFree(GetProcessHeap(),0,value);
186         *ppcomp = NULL;
187     }
188     return hr;
189 }
190
191 static HRESULT WINAPI CompartmentMgr_ClearCompartment(ITfCompartmentMgr *iface,
192     TfClientId tid, REFGUID rguid)
193 {
194     struct list *cursor;
195     CompartmentMgr *This = (CompartmentMgr *)iface;
196     TRACE("(%p) %i %s\n",This,tid,debugstr_guid(rguid));
197
198     LIST_FOR_EACH(cursor, &This->values)
199     {
200         CompartmentValue* value = LIST_ENTRY(cursor,CompartmentValue,entry);
201         if (IsEqualGUID(rguid,&value->guid))
202         {
203             if (value->owner && tid != value->owner)
204                 return E_UNEXPECTED;
205             list_remove(cursor);
206             ITfCompartment_Release(value->compartment);
207             HeapFree(GetProcessHeap(),0,value);
208             return S_OK;
209         }
210     }
211
212     return CONNECT_E_NOCONNECTION;
213 }
214
215 static HRESULT WINAPI CompartmentMgr_EnumCompartments(ITfCompartmentMgr *iface,
216  IEnumGUID **ppEnum)
217 {
218     CompartmentMgr *This = (CompartmentMgr *)iface;
219     TRACE("(%p) %p\n",This,ppEnum);
220     if (!ppEnum)
221         return E_INVALIDARG;
222     return CompartmentEnumGuid_Constructor(&This->values, ppEnum);
223 }
224
225 static const ITfCompartmentMgrVtbl CompartmentMgr_CompartmentMgrVtbl =
226 {
227     CompartmentMgr_QueryInterface,
228     CompartmentMgr_AddRef,
229     CompartmentMgr_Release,
230
231     CompartmentMgr_GetCompartment,
232     CompartmentMgr_ClearCompartment,
233     CompartmentMgr_EnumCompartments
234 };
235
236 HRESULT CompartmentMgr_Constructor(IUnknown *pUnkOuter, REFIID riid, IUnknown **ppOut)
237 {
238     CompartmentMgr *This;
239
240     if (!ppOut)
241         return E_POINTER;
242
243     if (pUnkOuter && !IsEqualIID (riid, &IID_IUnknown))
244         return CLASS_E_NOAGGREGATION;
245
246     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CompartmentMgr));
247     if (This == NULL)
248         return E_OUTOFMEMORY;
249
250     This->CompartmentMgrVtbl = &CompartmentMgr_CompartmentMgrVtbl;
251     This->pUnkOuter = pUnkOuter;
252     list_init(&This->values);
253
254     if (pUnkOuter)
255     {
256         TRACE("returning %p\n", This);
257         *ppOut = (IUnknown*)This;
258         return S_OK;
259     }
260     else
261     {
262         HRESULT hr;
263         hr = IUnknown_QueryInterface((IUnknown*)This, riid, (LPVOID*)ppOut);
264         if (FAILED(hr))
265             HeapFree(GetProcessHeap(),0,This);
266         return hr;
267     }
268 }
269
270 /**************************************************
271  * IEnumGUID implementaion for ITfCompartmentMgr::EnumCompartments
272  **************************************************/
273 static void CompartmentEnumGuid_Destructor(CompartmentEnumGuid *This)
274 {
275     TRACE("destroying %p\n", This);
276     HeapFree(GetProcessHeap(),0,This);
277 }
278
279 static HRESULT WINAPI CompartmentEnumGuid_QueryInterface(IEnumGUID *iface, REFIID iid, LPVOID *ppvOut)
280 {
281     CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
282     *ppvOut = NULL;
283
284     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumGUID))
285     {
286         *ppvOut = This;
287     }
288
289     if (*ppvOut)
290     {
291         IUnknown_AddRef(iface);
292         return S_OK;
293     }
294
295     WARN("unsupported interface: %s\n", debugstr_guid(iid));
296     return E_NOINTERFACE;
297 }
298
299 static ULONG WINAPI CompartmentEnumGuid_AddRef(IEnumGUID *iface)
300 {
301     CompartmentEnumGuid *This = (CompartmentEnumGuid*)iface;
302     return InterlockedIncrement(&This->refCount);
303 }
304
305 static ULONG WINAPI CompartmentEnumGuid_Release(IEnumGUID *iface)
306 {
307     CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
308     ULONG ret;
309
310     ret = InterlockedDecrement(&This->refCount);
311     if (ret == 0)
312         CompartmentEnumGuid_Destructor(This);
313     return ret;
314 }
315
316 /*****************************************************
317  * IEnumGuid functions
318  *****************************************************/
319 static HRESULT WINAPI CompartmentEnumGuid_Next( LPENUMGUID iface,
320     ULONG celt, GUID *rgelt, ULONG *pceltFetched)
321 {
322     CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
323     ULONG fetched = 0;
324
325     TRACE("(%p)\n",This);
326
327     if (rgelt == NULL) return E_POINTER;
328
329     while (fetched < celt && This->cursor)
330     {
331         CompartmentValue* value = LIST_ENTRY(This->cursor,CompartmentValue,entry);
332         if (!value)
333             break;
334
335         This->cursor = list_next(This->values,This->cursor);
336         *rgelt = value->guid;
337
338         ++fetched;
339         ++rgelt;
340     }
341
342     if (pceltFetched) *pceltFetched = fetched;
343     return fetched == celt ? S_OK : S_FALSE;
344 }
345
346 static HRESULT WINAPI CompartmentEnumGuid_Skip( LPENUMGUID iface, ULONG celt)
347 {
348     CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
349     TRACE("(%p)\n",This);
350
351     This->cursor = list_next(This->values,This->cursor);
352     return S_OK;
353 }
354
355 static HRESULT WINAPI CompartmentEnumGuid_Reset( LPENUMGUID iface)
356 {
357     CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
358     TRACE("(%p)\n",This);
359     This->cursor = list_head(This->values);
360     return S_OK;
361 }
362
363 static HRESULT WINAPI CompartmentEnumGuid_Clone( LPENUMGUID iface,
364     IEnumGUID **ppenum)
365 {
366     CompartmentEnumGuid *This = (CompartmentEnumGuid *)iface;
367     HRESULT res;
368
369     TRACE("(%p)\n",This);
370
371     if (ppenum == NULL) return E_POINTER;
372
373     res = CompartmentEnumGuid_Constructor(This->values, ppenum);
374     if (SUCCEEDED(res))
375     {
376         CompartmentEnumGuid *new_This = (CompartmentEnumGuid *)*ppenum;
377         new_This->cursor = This->cursor;
378     }
379     return res;
380 }
381
382 static const IEnumGUIDVtbl IEnumGUID_Vtbl ={
383     CompartmentEnumGuid_QueryInterface,
384     CompartmentEnumGuid_AddRef,
385     CompartmentEnumGuid_Release,
386
387     CompartmentEnumGuid_Next,
388     CompartmentEnumGuid_Skip,
389     CompartmentEnumGuid_Reset,
390     CompartmentEnumGuid_Clone
391 };
392
393 static HRESULT CompartmentEnumGuid_Constructor(struct list *values, IEnumGUID **ppOut)
394 {
395     CompartmentEnumGuid *This;
396
397     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CompartmentEnumGuid));
398     if (This == NULL)
399         return E_OUTOFMEMORY;
400
401     This->Vtbl= &IEnumGUID_Vtbl;
402     This->refCount = 1;
403
404     This->values = values;
405     This->cursor = list_head(values);
406
407     TRACE("returning %p\n", This);
408     *ppOut = (IEnumGUID*)This;
409     return S_OK;
410 }
411
412 /**************************************************
413  * ITfCompartment
414  **************************************************/
415 static void Compartment_Destructor(Compartment *This)
416 {
417     TRACE("destroying %p\n", This);
418     VariantClear(&This->variant);
419     HeapFree(GetProcessHeap(),0,This);
420 }
421
422 static HRESULT WINAPI Compartment_QueryInterface(ITfCompartment *iface, REFIID iid, LPVOID *ppvOut)
423 {
424     Compartment *This = (Compartment *)iface;
425     *ppvOut = NULL;
426
427     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfCompartment))
428     {
429         *ppvOut = This;
430     }
431
432     if (*ppvOut)
433     {
434         IUnknown_AddRef(iface);
435         return S_OK;
436     }
437
438     WARN("unsupported interface: %s\n", debugstr_guid(iid));
439     return E_NOINTERFACE;
440 }
441
442 static ULONG WINAPI Compartment_AddRef(ITfCompartment *iface)
443 {
444     Compartment *This = (Compartment*)iface;
445     return InterlockedIncrement(&This->refCount);
446 }
447
448 static ULONG WINAPI Compartment_Release(ITfCompartment *iface)
449 {
450     Compartment *This = (Compartment *)iface;
451     ULONG ret;
452
453     ret = InterlockedDecrement(&This->refCount);
454     if (ret == 0)
455         Compartment_Destructor(This);
456     return ret;
457 }
458
459 static HRESULT WINAPI Compartment_SetValue(ITfCompartment *iface,
460     TfClientId tid, const VARIANT *pvarValue)
461 {
462     Compartment *This = (Compartment *)iface;
463
464     TRACE("(%p) %i %p\n",This,tid,pvarValue);
465
466     if (!pvarValue)
467         return E_INVALIDARG;
468
469     if (!(V_VT(pvarValue) == VT_BSTR || V_VT(pvarValue) == VT_I4 ||
470           V_VT(pvarValue) == VT_UNKNOWN))
471         return E_INVALIDARG;
472
473     if (!This->valueData->owner)
474         This->valueData->owner = tid;
475
476     VariantClear(&This->variant);
477
478     /* Shallow copy of value and type */
479     This->variant = *pvarValue;
480
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));
486
487     return S_OK;
488 }
489
490 static HRESULT WINAPI Compartment_GetValue(ITfCompartment *iface,
491     VARIANT *pvarValue)
492 {
493     HRESULT hr = S_OK;
494     Compartment *This = (Compartment *)iface;
495     TRACE("(%p) %p\n",This, pvarValue);
496
497     if (!pvarValue)
498         return E_INVALIDARG;
499
500     pvarValue->n1.n2.vt = VT_EMPTY;
501     if (!This->variant.n1.n2.vt == VT_EMPTY)
502         hr = VariantCopy(pvarValue,&This->variant);
503     return hr;
504 }
505
506 static const ITfCompartmentVtbl ITfCompartment_Vtbl ={
507     Compartment_QueryInterface,
508     Compartment_AddRef,
509     Compartment_Release,
510
511     Compartment_SetValue,
512     Compartment_GetValue
513 };
514
515 static HRESULT Compartment_Constructor(CompartmentValue *valueData, ITfCompartment **ppOut)
516 {
517     Compartment *This;
518
519     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(Compartment));
520     if (This == NULL)
521         return E_OUTOFMEMORY;
522
523     This->Vtbl= &ITfCompartment_Vtbl;
524     This->refCount = 1;
525
526     This->valueData = valueData;
527     VariantInit(&This->variant);
528
529     TRACE("returning %p\n", This);
530     *ppOut = (ITfCompartment*)This;
531     return S_OK;
532 }