Sunc with trunk revision 58971.
[reactos.git] / dll / win32 / msctf / inputprocessor.c
1 /*
2 * ITfInputProcessorProfiles 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 #define WIN32_NO_STATUS
22 #define _INC_WINDOWS
23 #define COM_NO_WINDOWS_H
24
25 #include <config.h>
26
27 //#include <stdarg.h>
28
29 #define COBJMACROS
30
31 #include <wine/debug.h>
32 //#include "windef.h"
33 #include <winbase.h>
34 #include <winreg.h>
35 //#include "winuser.h"
36 //#include "shlwapi.h"
37 //#include "winerror.h"
38 #include <objbase.h>
39 #include <olectl.h>
40
41 #include <wine/unicode.h>
42 #include <wine/list.h>
43
44 #include <msctf.h>
45 #include "msctf_internal.h"
46
47 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
48
49 static const WCHAR szwLngp[] = {'L','a','n','g','u','a','g','e','P','r','o','f','i','l','e',0};
50 static const WCHAR szwEnable[] = {'E','n','a','b','l','e',0};
51 static const WCHAR szwTipfmt[] = {'%','s','\\','%','s',0};
52 static const WCHAR szwFullLangfmt[] = {'%','s','\\','%','s','\\','%','s','\\','0','x','%','0','8','x','\\','%','s',0};
53
54 static const WCHAR szwAssemblies[] = {'A','s','s','e','m','b','l','i','e','s',0};
55 static const WCHAR szwDefault[] = {'D','e','f','a','u','l','t',0};
56 static const WCHAR szwProfile[] = {'P','r','o','f','i','l','e',0};
57 static const WCHAR szwDefaultFmt[] = {'%','s','\\','%','s','\\','0','x','%','0','8','x','\\','%','s',0};
58
59 typedef struct tagInputProcessorProfilesSink {
60 struct list entry;
61 union {
62 /* InputProcessorProfile Sinks */
63 IUnknown *pIUnknown;
64 ITfLanguageProfileNotifySink *pITfLanguageProfileNotifySink;
65 } interfaces;
66 } InputProcessorProfilesSink;
67
68 typedef struct tagInputProcessorProfiles {
69 const ITfInputProcessorProfilesVtbl *InputProcessorProfilesVtbl;
70 const ITfSourceVtbl *SourceVtbl;
71 /* const ITfInputProcessorProfileMgrVtbl *InputProcessorProfileMgrVtbl; */
72 /* const ITfInputProcessorProfilesExVtbl *InputProcessorProfilesExVtbl; */
73 /* const ITfInputProcessorProfileSubstituteLayoutVtbl *InputProcessorProfileSubstituteLayoutVtbl; */
74 LONG refCount;
75
76 LANGID currentLanguage;
77
78 struct list LanguageProfileNotifySink;
79 } InputProcessorProfiles;
80
81 typedef struct tagProfilesEnumGuid {
82 const IEnumGUIDVtbl *Vtbl;
83 LONG refCount;
84
85 HKEY key;
86 DWORD next_index;
87 } ProfilesEnumGuid;
88
89 typedef struct tagEnumTfLanguageProfiles {
90 const IEnumTfLanguageProfilesVtbl *Vtbl;
91 LONG refCount;
92
93 HKEY tipkey;
94 DWORD tip_index;
95 WCHAR szwCurrentClsid[39];
96
97 HKEY langkey;
98 DWORD lang_index;
99
100 LANGID langid;
101 ITfCategoryMgr *catmgr;
102 } EnumTfLanguageProfiles;
103
104 static HRESULT ProfilesEnumGuid_Constructor(IEnumGUID **ppOut);
105 static HRESULT EnumTfLanguageProfiles_Constructor(LANGID langid, IEnumTfLanguageProfiles **ppOut);
106
107 static inline InputProcessorProfiles *impl_from_ITfSourceVtbl(ITfSource *iface)
108 {
109 return (InputProcessorProfiles *)((char *)iface - FIELD_OFFSET(InputProcessorProfiles,SourceVtbl));
110 }
111
112 static void free_sink(InputProcessorProfilesSink *sink)
113 {
114 IUnknown_Release(sink->interfaces.pIUnknown);
115 HeapFree(GetProcessHeap(),0,sink);
116 }
117
118 static void InputProcessorProfiles_Destructor(InputProcessorProfiles *This)
119 {
120 struct list *cursor, *cursor2;
121 TRACE("destroying %p\n", This);
122
123 /* free sinks */
124 LIST_FOR_EACH_SAFE(cursor, cursor2, &This->LanguageProfileNotifySink)
125 {
126 InputProcessorProfilesSink* sink = LIST_ENTRY(cursor,InputProcessorProfilesSink,entry);
127 list_remove(cursor);
128 free_sink(sink);
129 }
130
131 HeapFree(GetProcessHeap(),0,This);
132 }
133
134 static void add_userkey( REFCLSID rclsid, LANGID langid,
135 REFGUID guidProfile)
136 {
137 HKEY key;
138 WCHAR buf[39];
139 WCHAR buf2[39];
140 WCHAR fullkey[168];
141 DWORD disposition = 0;
142 ULONG res;
143
144 TRACE("\n");
145
146 StringFromGUID2(rclsid, buf, 39);
147 StringFromGUID2(guidProfile, buf2, 39);
148 sprintfW(fullkey,szwFullLangfmt,szwSystemTIPKey,buf,szwLngp,langid,buf2);
149
150 res = RegCreateKeyExW(HKEY_CURRENT_USER,fullkey, 0, NULL, 0,
151 KEY_READ | KEY_WRITE, NULL, &key, &disposition);
152
153 if (!res && disposition == REG_CREATED_NEW_KEY)
154 {
155 DWORD zero = 0x0;
156 RegSetValueExW(key, szwEnable, 0, REG_DWORD, (LPBYTE)&zero, sizeof(DWORD));
157 }
158
159 if (!res)
160 RegCloseKey(key);
161 }
162
163 static HRESULT WINAPI InputProcessorProfiles_QueryInterface(ITfInputProcessorProfiles *iface, REFIID iid, LPVOID *ppvOut)
164 {
165 InputProcessorProfiles *This = (InputProcessorProfiles *)iface;
166 *ppvOut = NULL;
167
168 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfInputProcessorProfiles))
169 {
170 *ppvOut = This;
171 }
172 else if (IsEqualIID(iid, &IID_ITfSource))
173 {
174 *ppvOut = &This->SourceVtbl;
175 }
176
177 if (*ppvOut)
178 {
179 IUnknown_AddRef(iface);
180 return S_OK;
181 }
182
183 WARN("unsupported interface: %s\n", debugstr_guid(iid));
184 return E_NOINTERFACE;
185 }
186
187 static ULONG WINAPI InputProcessorProfiles_AddRef(ITfInputProcessorProfiles *iface)
188 {
189 InputProcessorProfiles *This = (InputProcessorProfiles *)iface;
190 return InterlockedIncrement(&This->refCount);
191 }
192
193 static ULONG WINAPI InputProcessorProfiles_Release(ITfInputProcessorProfiles *iface)
194 {
195 InputProcessorProfiles *This = (InputProcessorProfiles *)iface;
196 ULONG ret;
197
198 ret = InterlockedDecrement(&This->refCount);
199 if (ret == 0)
200 InputProcessorProfiles_Destructor(This);
201 return ret;
202 }
203
204 /*****************************************************
205 * ITfInputProcessorProfiles functions
206 *****************************************************/
207 static HRESULT WINAPI InputProcessorProfiles_Register(
208 ITfInputProcessorProfiles *iface, REFCLSID rclsid)
209 {
210 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
211 HKEY tipkey;
212 WCHAR buf[39];
213 WCHAR fullkey[68];
214
215 TRACE("(%p) %s\n",This,debugstr_guid(rclsid));
216
217 StringFromGUID2(rclsid, buf, 39);
218 sprintfW(fullkey,szwTipfmt,szwSystemTIPKey,buf);
219
220 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE,fullkey, 0, NULL, 0,
221 KEY_READ | KEY_WRITE, NULL, &tipkey, NULL) != ERROR_SUCCESS)
222 return E_FAIL;
223
224 RegCloseKey(tipkey);
225
226 return S_OK;
227 }
228
229 static HRESULT WINAPI InputProcessorProfiles_Unregister(
230 ITfInputProcessorProfiles *iface, REFCLSID rclsid)
231 {
232 WCHAR buf[39];
233 WCHAR fullkey[68];
234 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
235
236 TRACE("(%p) %s\n",This,debugstr_guid(rclsid));
237
238 StringFromGUID2(rclsid, buf, 39);
239 sprintfW(fullkey,szwTipfmt,szwSystemTIPKey,buf);
240
241 RegDeleteTreeW(HKEY_LOCAL_MACHINE, fullkey);
242 RegDeleteTreeW(HKEY_CURRENT_USER, fullkey);
243
244 return S_OK;
245 }
246
247 static HRESULT WINAPI InputProcessorProfiles_AddLanguageProfile(
248 ITfInputProcessorProfiles *iface, REFCLSID rclsid,
249 LANGID langid, REFGUID guidProfile, const WCHAR *pchDesc,
250 ULONG cchDesc, const WCHAR *pchIconFile, ULONG cchFile,
251 ULONG uIconIndex)
252 {
253 HKEY tipkey,fmtkey;
254 WCHAR buf[39];
255 WCHAR fullkey[100];
256 ULONG res;
257 DWORD disposition = 0;
258
259 static const WCHAR fmt2[] = {'%','s','\\','0','x','%','0','8','x','\\','%','s',0};
260 static const WCHAR desc[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
261 static const WCHAR icnf[] = {'I','c','o','n','F','i','l','e',0};
262 static const WCHAR icni[] = {'I','c','o','n','I','n','d','e','x',0};
263
264 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
265
266 TRACE("(%p) %s %x %s %s %s %i\n",This,debugstr_guid(rclsid), langid,
267 debugstr_guid(guidProfile), debugstr_wn(pchDesc,cchDesc),
268 debugstr_wn(pchIconFile,cchFile),uIconIndex);
269
270 StringFromGUID2(rclsid, buf, 39);
271 sprintfW(fullkey,szwTipfmt,szwSystemTIPKey,buf);
272
273 if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,fullkey, 0, KEY_READ | KEY_WRITE,
274 &tipkey ) != ERROR_SUCCESS)
275 return E_FAIL;
276
277 StringFromGUID2(guidProfile, buf, 39);
278 sprintfW(fullkey,fmt2,szwLngp,langid,buf);
279
280 res = RegCreateKeyExW(tipkey,fullkey, 0, NULL, 0, KEY_READ | KEY_WRITE,
281 NULL, &fmtkey, &disposition);
282
283 if (!res)
284 {
285 DWORD zero = 0x0;
286 RegSetValueExW(fmtkey, desc, 0, REG_SZ, (const BYTE*)pchDesc, cchDesc * sizeof(WCHAR));
287 RegSetValueExW(fmtkey, icnf, 0, REG_SZ, (const BYTE*)pchIconFile, cchFile * sizeof(WCHAR));
288 RegSetValueExW(fmtkey, icni, 0, REG_DWORD, (LPBYTE)&uIconIndex, sizeof(DWORD));
289 if (disposition == REG_CREATED_NEW_KEY)
290 RegSetValueExW(fmtkey, szwEnable, 0, REG_DWORD, (LPBYTE)&zero, sizeof(DWORD));
291 RegCloseKey(fmtkey);
292
293 add_userkey(rclsid, langid, guidProfile);
294 }
295 RegCloseKey(tipkey);
296
297 if (!res)
298 return S_OK;
299 else
300 return E_FAIL;
301 }
302
303 static HRESULT WINAPI InputProcessorProfiles_RemoveLanguageProfile(
304 ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
305 REFGUID guidProfile)
306 {
307 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
308 FIXME("STUB:(%p)\n",This);
309 return E_NOTIMPL;
310 }
311
312 static HRESULT WINAPI InputProcessorProfiles_EnumInputProcessorInfo(
313 ITfInputProcessorProfiles *iface, IEnumGUID **ppEnum)
314 {
315 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
316 TRACE("(%p) %p\n",This,ppEnum);
317 return ProfilesEnumGuid_Constructor(ppEnum);
318 }
319
320 static HRESULT WINAPI InputProcessorProfiles_GetDefaultLanguageProfile(
321 ITfInputProcessorProfiles *iface, LANGID langid, REFGUID catid,
322 CLSID *pclsid, GUID *pguidProfile)
323 {
324 WCHAR fullkey[168];
325 WCHAR buf[39];
326 HKEY hkey;
327 DWORD count;
328 ULONG res;
329 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
330
331 TRACE("%p) %x %s %p %p\n",This, langid, debugstr_guid(catid),pclsid,pguidProfile);
332
333 if (!catid || !pclsid || !pguidProfile)
334 return E_INVALIDARG;
335
336 StringFromGUID2(catid, buf, 39);
337 sprintfW(fullkey, szwDefaultFmt, szwSystemCTFKey, szwAssemblies, langid, buf);
338
339 if (RegOpenKeyExW(HKEY_CURRENT_USER, fullkey, 0, KEY_READ | KEY_WRITE,
340 &hkey ) != ERROR_SUCCESS)
341 return S_FALSE;
342
343 count = sizeof(buf);
344 res = RegQueryValueExW(hkey, szwDefault, 0, NULL, (LPBYTE)buf, &count);
345 if (res != ERROR_SUCCESS)
346 {
347 RegCloseKey(hkey);
348 return S_FALSE;
349 }
350 CLSIDFromString(buf,pclsid);
351
352 res = RegQueryValueExW(hkey, szwProfile, 0, NULL, (LPBYTE)buf, &count);
353 if (res == ERROR_SUCCESS)
354 CLSIDFromString(buf,pguidProfile);
355
356 RegCloseKey(hkey);
357
358 return S_OK;
359 }
360
361 static HRESULT WINAPI InputProcessorProfiles_SetDefaultLanguageProfile(
362 ITfInputProcessorProfiles *iface, LANGID langid, REFCLSID rclsid,
363 REFGUID guidProfiles)
364 {
365 WCHAR fullkey[168];
366 WCHAR buf[39];
367 HKEY hkey;
368 GUID catid;
369 HRESULT hr;
370 ITfCategoryMgr *catmgr;
371 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
372 static const GUID * tipcats[3] = { &GUID_TFCAT_TIP_KEYBOARD,
373 &GUID_TFCAT_TIP_SPEECH,
374 &GUID_TFCAT_TIP_HANDWRITING };
375
376 TRACE("%p) %x %s %s\n",This, langid, debugstr_guid(rclsid),debugstr_guid(guidProfiles));
377
378 if (!rclsid || !guidProfiles)
379 return E_INVALIDARG;
380
381 hr = CategoryMgr_Constructor(NULL,(IUnknown**)&catmgr);
382
383 if (FAILED(hr))
384 return hr;
385
386 if (ITfCategoryMgr_FindClosestCategory(catmgr, rclsid,
387 &catid, tipcats, 3) != S_OK)
388 hr = ITfCategoryMgr_FindClosestCategory(catmgr, rclsid,
389 &catid, NULL, 0);
390 ITfCategoryMgr_Release(catmgr);
391
392 if (FAILED(hr))
393 return E_FAIL;
394
395 StringFromGUID2(&catid, buf, 39);
396 sprintfW(fullkey, szwDefaultFmt, szwSystemCTFKey, szwAssemblies, langid, buf);
397
398 if (RegCreateKeyExW(HKEY_CURRENT_USER, fullkey, 0, NULL, 0, KEY_READ | KEY_WRITE,
399 NULL, &hkey, NULL ) != ERROR_SUCCESS)
400 return E_FAIL;
401
402 StringFromGUID2(rclsid, buf, 39);
403 RegSetValueExW(hkey, szwDefault, 0, REG_SZ, (LPBYTE)buf, sizeof(buf));
404 StringFromGUID2(guidProfiles, buf, 39);
405 RegSetValueExW(hkey, szwProfile, 0, REG_SZ, (LPBYTE)buf, sizeof(buf));
406 RegCloseKey(hkey);
407
408 return S_OK;
409 }
410
411 static HRESULT WINAPI InputProcessorProfiles_ActivateLanguageProfile(
412 ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
413 REFGUID guidProfiles)
414 {
415 HRESULT hr;
416 BOOL enabled;
417 TF_LANGUAGEPROFILE LanguageProfile;
418 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
419
420 TRACE("(%p) %s %x %s\n",This,debugstr_guid(rclsid),langid,debugstr_guid(guidProfiles));
421
422 if (langid != This->currentLanguage) return E_INVALIDARG;
423
424 if (get_active_textservice(rclsid,NULL))
425 {
426 TRACE("Already Active\n");
427 return E_FAIL;
428 }
429
430 hr = ITfInputProcessorProfiles_IsEnabledLanguageProfile(iface, rclsid,
431 langid, guidProfiles, &enabled);
432 if (FAILED(hr) || !enabled)
433 {
434 TRACE("Not Enabled\n");
435 return E_FAIL;
436 }
437
438 LanguageProfile.clsid = *rclsid;
439 LanguageProfile.langid = langid;
440 LanguageProfile.guidProfile = *guidProfiles;
441
442 hr = add_active_textservice(&LanguageProfile);
443
444 return hr;
445 }
446
447 static HRESULT WINAPI InputProcessorProfiles_GetActiveLanguageProfile(
448 ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID *plangid,
449 GUID *pguidProfile)
450 {
451 TF_LANGUAGEPROFILE profile;
452 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
453
454 TRACE("(%p) %s %p %p\n",This,debugstr_guid(rclsid),plangid,pguidProfile);
455
456 if (!rclsid || !plangid || !pguidProfile)
457 return E_INVALIDARG;
458
459 if (get_active_textservice(rclsid, &profile))
460 {
461 *plangid = profile.langid;
462 *pguidProfile = profile.guidProfile;
463 return S_OK;
464 }
465 else
466 {
467 *pguidProfile = GUID_NULL;
468 return S_FALSE;
469 }
470 }
471
472 static HRESULT WINAPI InputProcessorProfiles_GetLanguageProfileDescription(
473 ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
474 REFGUID guidProfile, BSTR *pbstrProfile)
475 {
476 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
477 FIXME("STUB:(%p)\n",This);
478 return E_NOTIMPL;
479 }
480
481 static HRESULT WINAPI InputProcessorProfiles_GetCurrentLanguage(
482 ITfInputProcessorProfiles *iface, LANGID *plangid)
483 {
484 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
485 TRACE("(%p) 0x%x\n",This,This->currentLanguage);
486
487 if (!plangid)
488 return E_INVALIDARG;
489
490 *plangid = This->currentLanguage;
491
492 return S_OK;
493 }
494
495 static HRESULT WINAPI InputProcessorProfiles_ChangeCurrentLanguage(
496 ITfInputProcessorProfiles *iface, LANGID langid)
497 {
498 struct list *cursor;
499 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
500 BOOL accept;
501
502 FIXME("STUB:(%p)\n",This);
503
504 LIST_FOR_EACH(cursor, &This->LanguageProfileNotifySink)
505 {
506 InputProcessorProfilesSink* sink = LIST_ENTRY(cursor,InputProcessorProfilesSink,entry);
507 accept = TRUE;
508 ITfLanguageProfileNotifySink_OnLanguageChange(sink->interfaces.pITfLanguageProfileNotifySink, langid, &accept);
509 if (!accept)
510 return E_FAIL;
511 }
512
513 /* TODO: On successful language change call OnLanguageChanged sink */
514 return E_NOTIMPL;
515 }
516
517 static HRESULT WINAPI InputProcessorProfiles_GetLanguageList(
518 ITfInputProcessorProfiles *iface, LANGID **ppLangId, ULONG *pulCount)
519 {
520 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
521 FIXME("Semi-STUB:(%p)\n",This);
522 *ppLangId = CoTaskMemAlloc(sizeof(LANGID));
523 **ppLangId = This->currentLanguage;
524 *pulCount = 1;
525 return S_OK;
526 }
527
528 static HRESULT WINAPI InputProcessorProfiles_EnumLanguageProfiles(
529 ITfInputProcessorProfiles *iface, LANGID langid,
530 IEnumTfLanguageProfiles **ppEnum)
531 {
532 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
533 TRACE("(%p) %x %p\n",This,langid,ppEnum);
534 return EnumTfLanguageProfiles_Constructor(langid, ppEnum);
535 }
536
537 static HRESULT WINAPI InputProcessorProfiles_EnableLanguageProfile(
538 ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
539 REFGUID guidProfile, BOOL fEnable)
540 {
541 HKEY key;
542 WCHAR buf[39];
543 WCHAR buf2[39];
544 WCHAR fullkey[168];
545 ULONG res;
546
547 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
548 TRACE("(%p) %s %x %s %i\n",This, debugstr_guid(rclsid), langid, debugstr_guid(guidProfile), fEnable);
549
550 StringFromGUID2(rclsid, buf, 39);
551 StringFromGUID2(guidProfile, buf2, 39);
552 sprintfW(fullkey,szwFullLangfmt,szwSystemTIPKey,buf,szwLngp,langid,buf2);
553
554 res = RegOpenKeyExW(HKEY_CURRENT_USER, fullkey, 0, KEY_READ | KEY_WRITE, &key);
555
556 if (!res)
557 {
558 RegSetValueExW(key, szwEnable, 0, REG_DWORD, (LPBYTE)&fEnable, sizeof(DWORD));
559 RegCloseKey(key);
560 }
561 else
562 return E_FAIL;
563
564 return S_OK;
565 }
566
567 static HRESULT WINAPI InputProcessorProfiles_IsEnabledLanguageProfile(
568 ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
569 REFGUID guidProfile, BOOL *pfEnable)
570 {
571 HKEY key;
572 WCHAR buf[39];
573 WCHAR buf2[39];
574 WCHAR fullkey[168];
575 ULONG res;
576
577 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
578 TRACE("(%p) %s, %i, %s, %p\n",This,debugstr_guid(rclsid),langid,debugstr_guid(guidProfile),pfEnable);
579
580 if (!pfEnable)
581 return E_INVALIDARG;
582
583 StringFromGUID2(rclsid, buf, 39);
584 StringFromGUID2(guidProfile, buf2, 39);
585 sprintfW(fullkey,szwFullLangfmt,szwSystemTIPKey,buf,szwLngp,langid,buf2);
586
587 res = RegOpenKeyExW(HKEY_CURRENT_USER, fullkey, 0, KEY_READ | KEY_WRITE, &key);
588
589 if (!res)
590 {
591 DWORD count = sizeof(DWORD);
592 res = RegQueryValueExW(key, szwEnable, 0, NULL, (LPBYTE)pfEnable, &count);
593 RegCloseKey(key);
594 }
595
596 if (res) /* Try Default */
597 {
598 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, fullkey, 0, KEY_READ | KEY_WRITE, &key);
599
600 if (!res)
601 {
602 DWORD count = sizeof(DWORD);
603 res = RegQueryValueExW(key, szwEnable, 0, NULL, (LPBYTE)pfEnable, &count);
604 RegCloseKey(key);
605 }
606 }
607
608 if (!res)
609 return S_OK;
610 else
611 return E_FAIL;
612 }
613
614 static HRESULT WINAPI InputProcessorProfiles_EnableLanguageProfileByDefault(
615 ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
616 REFGUID guidProfile, BOOL fEnable)
617 {
618 HKEY key;
619 WCHAR buf[39];
620 WCHAR buf2[39];
621 WCHAR fullkey[168];
622 ULONG res;
623
624 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
625 TRACE("(%p) %s %x %s %i\n",This,debugstr_guid(rclsid),langid,debugstr_guid(guidProfile),fEnable);
626
627 StringFromGUID2(rclsid, buf, 39);
628 StringFromGUID2(guidProfile, buf2, 39);
629 sprintfW(fullkey,szwFullLangfmt,szwSystemTIPKey,buf,szwLngp,langid,buf2);
630
631 res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, fullkey, 0, KEY_READ | KEY_WRITE, &key);
632
633 if (!res)
634 {
635 RegSetValueExW(key, szwEnable, 0, REG_DWORD, (LPBYTE)&fEnable, sizeof(DWORD));
636 RegCloseKey(key);
637 }
638 else
639 return E_FAIL;
640
641 return S_OK;
642 }
643
644 static HRESULT WINAPI InputProcessorProfiles_SubstituteKeyboardLayout(
645 ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
646 REFGUID guidProfile, HKL hKL)
647 {
648 InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
649 FIXME("STUB:(%p)\n",This);
650 return E_NOTIMPL;
651 }
652
653
654 static const ITfInputProcessorProfilesVtbl InputProcessorProfiles_InputProcessorProfilesVtbl =
655 {
656 InputProcessorProfiles_QueryInterface,
657 InputProcessorProfiles_AddRef,
658 InputProcessorProfiles_Release,
659
660 InputProcessorProfiles_Register,
661 InputProcessorProfiles_Unregister,
662 InputProcessorProfiles_AddLanguageProfile,
663 InputProcessorProfiles_RemoveLanguageProfile,
664 InputProcessorProfiles_EnumInputProcessorInfo,
665 InputProcessorProfiles_GetDefaultLanguageProfile,
666 InputProcessorProfiles_SetDefaultLanguageProfile,
667 InputProcessorProfiles_ActivateLanguageProfile,
668 InputProcessorProfiles_GetActiveLanguageProfile,
669 InputProcessorProfiles_GetLanguageProfileDescription,
670 InputProcessorProfiles_GetCurrentLanguage,
671 InputProcessorProfiles_ChangeCurrentLanguage,
672 InputProcessorProfiles_GetLanguageList,
673 InputProcessorProfiles_EnumLanguageProfiles,
674 InputProcessorProfiles_EnableLanguageProfile,
675 InputProcessorProfiles_IsEnabledLanguageProfile,
676 InputProcessorProfiles_EnableLanguageProfileByDefault,
677 InputProcessorProfiles_SubstituteKeyboardLayout
678 };
679
680 /*****************************************************
681 * ITfSource functions
682 *****************************************************/
683 static HRESULT WINAPI IPPSource_QueryInterface(ITfSource *iface, REFIID iid, LPVOID *ppvOut)
684 {
685 InputProcessorProfiles *This = impl_from_ITfSourceVtbl(iface);
686 return InputProcessorProfiles_QueryInterface((ITfInputProcessorProfiles *)This, iid, *ppvOut);
687 }
688
689 static ULONG WINAPI IPPSource_AddRef(ITfSource *iface)
690 {
691 InputProcessorProfiles *This = impl_from_ITfSourceVtbl(iface);
692 return InputProcessorProfiles_AddRef((ITfInputProcessorProfiles*)This);
693 }
694
695 static ULONG WINAPI IPPSource_Release(ITfSource *iface)
696 {
697 InputProcessorProfiles *This = impl_from_ITfSourceVtbl(iface);
698 return InputProcessorProfiles_Release((ITfInputProcessorProfiles *)This);
699 }
700
701 static HRESULT WINAPI IPPSource_AdviseSink(ITfSource *iface,
702 REFIID riid, IUnknown *punk, DWORD *pdwCookie)
703 {
704 InputProcessorProfilesSink *ipps;
705 InputProcessorProfiles *This = impl_from_ITfSourceVtbl(iface);
706
707 TRACE("(%p) %s %p %p\n",This,debugstr_guid(riid),punk,pdwCookie);
708
709 if (!riid || !punk || !pdwCookie)
710 return E_INVALIDARG;
711
712 if (IsEqualIID(riid, &IID_ITfLanguageProfileNotifySink))
713 {
714 ipps = HeapAlloc(GetProcessHeap(),0,sizeof(InputProcessorProfilesSink));
715 if (!ipps)
716 return E_OUTOFMEMORY;
717 if (FAILED(IUnknown_QueryInterface(punk, riid, (LPVOID *)&ipps->interfaces.pITfLanguageProfileNotifySink)))
718 {
719 HeapFree(GetProcessHeap(),0,ipps);
720 return CONNECT_E_CANNOTCONNECT;
721 }
722 list_add_head(&This->LanguageProfileNotifySink,&ipps->entry);
723 *pdwCookie = generate_Cookie(COOKIE_MAGIC_IPPSINK, ipps);
724 }
725 else
726 {
727 FIXME("(%p) Unhandled Sink: %s\n",This,debugstr_guid(riid));
728 return E_NOTIMPL;
729 }
730
731 TRACE("cookie %x\n",*pdwCookie);
732
733 return S_OK;
734 }
735
736 static HRESULT WINAPI IPPSource_UnadviseSink(ITfSource *iface, DWORD pdwCookie)
737 {
738 InputProcessorProfilesSink *sink;
739 InputProcessorProfiles *This = impl_from_ITfSourceVtbl(iface);
740
741 TRACE("(%p) %x\n",This,pdwCookie);
742
743 if (get_Cookie_magic(pdwCookie)!=COOKIE_MAGIC_IPPSINK)
744 return E_INVALIDARG;
745
746 sink = (InputProcessorProfilesSink*)remove_Cookie(pdwCookie);
747 if (!sink)
748 return CONNECT_E_NOCONNECTION;
749
750 list_remove(&sink->entry);
751 free_sink(sink);
752
753 return S_OK;
754 }
755
756 static const ITfSourceVtbl InputProcessorProfiles_SourceVtbl =
757 {
758 IPPSource_QueryInterface,
759 IPPSource_AddRef,
760 IPPSource_Release,
761
762 IPPSource_AdviseSink,
763 IPPSource_UnadviseSink,
764 };
765
766 HRESULT InputProcessorProfiles_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
767 {
768 InputProcessorProfiles *This;
769 if (pUnkOuter)
770 return CLASS_E_NOAGGREGATION;
771
772 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(InputProcessorProfiles));
773 if (This == NULL)
774 return E_OUTOFMEMORY;
775
776 This->InputProcessorProfilesVtbl= &InputProcessorProfiles_InputProcessorProfilesVtbl;
777 This->SourceVtbl = &InputProcessorProfiles_SourceVtbl;
778 This->refCount = 1;
779 This->currentLanguage = GetUserDefaultLCID();
780
781 list_init(&This->LanguageProfileNotifySink);
782
783 TRACE("returning %p\n", This);
784 *ppOut = (IUnknown *)This;
785 return S_OK;
786 }
787
788 /**************************************************
789 * IEnumGUID implementation for ITfInputProcessorProfiles::EnumInputProcessorInfo
790 **************************************************/
791 static void ProfilesEnumGuid_Destructor(ProfilesEnumGuid *This)
792 {
793 TRACE("destroying %p\n", This);
794 RegCloseKey(This->key);
795 HeapFree(GetProcessHeap(),0,This);
796 }
797
798 static HRESULT WINAPI ProfilesEnumGuid_QueryInterface(IEnumGUID *iface, REFIID iid, LPVOID *ppvOut)
799 {
800 ProfilesEnumGuid *This = (ProfilesEnumGuid *)iface;
801 *ppvOut = NULL;
802
803 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumGUID))
804 {
805 *ppvOut = This;
806 }
807
808 if (*ppvOut)
809 {
810 IUnknown_AddRef(iface);
811 return S_OK;
812 }
813
814 WARN("unsupported interface: %s\n", debugstr_guid(iid));
815 return E_NOINTERFACE;
816 }
817
818 static ULONG WINAPI ProfilesEnumGuid_AddRef(IEnumGUID *iface)
819 {
820 ProfilesEnumGuid *This = (ProfilesEnumGuid*)iface;
821 return InterlockedIncrement(&This->refCount);
822 }
823
824 static ULONG WINAPI ProfilesEnumGuid_Release(IEnumGUID *iface)
825 {
826 ProfilesEnumGuid *This = (ProfilesEnumGuid *)iface;
827 ULONG ret;
828
829 ret = InterlockedDecrement(&This->refCount);
830 if (ret == 0)
831 ProfilesEnumGuid_Destructor(This);
832 return ret;
833 }
834
835 /*****************************************************
836 * IEnumGuid functions
837 *****************************************************/
838 static HRESULT WINAPI ProfilesEnumGuid_Next( LPENUMGUID iface,
839 ULONG celt, GUID *rgelt, ULONG *pceltFetched)
840 {
841 ProfilesEnumGuid *This = (ProfilesEnumGuid *)iface;
842 ULONG fetched = 0;
843
844 TRACE("(%p)\n",This);
845
846 if (rgelt == NULL) return E_POINTER;
847
848 if (This->key) while (fetched < celt)
849 {
850 LSTATUS res;
851 HRESULT hr;
852 WCHAR catid[39];
853 DWORD cName = 39;
854
855 res = RegEnumKeyExW(This->key, This->next_index, catid, &cName,
856 NULL, NULL, NULL, NULL);
857 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
858 ++(This->next_index);
859
860 hr = CLSIDFromString(catid, rgelt);
861 if (FAILED(hr)) continue;
862
863 ++fetched;
864 ++rgelt;
865 }
866
867 if (pceltFetched) *pceltFetched = fetched;
868 return fetched == celt ? S_OK : S_FALSE;
869 }
870
871 static HRESULT WINAPI ProfilesEnumGuid_Skip( LPENUMGUID iface, ULONG celt)
872 {
873 ProfilesEnumGuid *This = (ProfilesEnumGuid *)iface;
874 TRACE("(%p)\n",This);
875
876 This->next_index += celt;
877 return S_OK;
878 }
879
880 static HRESULT WINAPI ProfilesEnumGuid_Reset( LPENUMGUID iface)
881 {
882 ProfilesEnumGuid *This = (ProfilesEnumGuid *)iface;
883 TRACE("(%p)\n",This);
884 This->next_index = 0;
885 return S_OK;
886 }
887
888 static HRESULT WINAPI ProfilesEnumGuid_Clone( LPENUMGUID iface,
889 IEnumGUID **ppenum)
890 {
891 ProfilesEnumGuid *This = (ProfilesEnumGuid *)iface;
892 HRESULT res;
893
894 TRACE("(%p)\n",This);
895
896 if (ppenum == NULL) return E_POINTER;
897
898 res = ProfilesEnumGuid_Constructor(ppenum);
899 if (SUCCEEDED(res))
900 {
901 ProfilesEnumGuid *new_This = (ProfilesEnumGuid *)*ppenum;
902 new_This->next_index = This->next_index;
903 }
904 return res;
905 }
906
907 static const IEnumGUIDVtbl IEnumGUID_Vtbl ={
908 ProfilesEnumGuid_QueryInterface,
909 ProfilesEnumGuid_AddRef,
910 ProfilesEnumGuid_Release,
911
912 ProfilesEnumGuid_Next,
913 ProfilesEnumGuid_Skip,
914 ProfilesEnumGuid_Reset,
915 ProfilesEnumGuid_Clone
916 };
917
918 static HRESULT ProfilesEnumGuid_Constructor(IEnumGUID **ppOut)
919 {
920 ProfilesEnumGuid *This;
921
922 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(ProfilesEnumGuid));
923 if (This == NULL)
924 return E_OUTOFMEMORY;
925
926 This->Vtbl= &IEnumGUID_Vtbl;
927 This->refCount = 1;
928
929 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE, szwSystemTIPKey, 0, NULL, 0,
930 KEY_READ | KEY_WRITE, NULL, &This->key, NULL) != ERROR_SUCCESS)
931 {
932 HeapFree(GetProcessHeap(), 0, This);
933 return E_FAIL;
934 }
935
936 TRACE("returning %p\n", This);
937 *ppOut = (IEnumGUID*)This;
938 return S_OK;
939 }
940
941 /**************************************************
942 * IEnumTfLanguageProfiles implementation
943 **************************************************/
944 static void EnumTfLanguageProfiles_Destructor(EnumTfLanguageProfiles *This)
945 {
946 TRACE("destroying %p\n", This);
947 RegCloseKey(This->tipkey);
948 if (This->langkey)
949 RegCloseKey(This->langkey);
950 ITfCategoryMgr_Release(This->catmgr);
951 HeapFree(GetProcessHeap(),0,This);
952 }
953
954 static HRESULT WINAPI EnumTfLanguageProfiles_QueryInterface(IEnumTfLanguageProfiles *iface, REFIID iid, LPVOID *ppvOut)
955 {
956 EnumTfLanguageProfiles *This = (EnumTfLanguageProfiles *)iface;
957 *ppvOut = NULL;
958
959 if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_IEnumTfLanguageProfiles))
960 {
961 *ppvOut = This;
962 }
963
964 if (*ppvOut)
965 {
966 IUnknown_AddRef(iface);
967 return S_OK;
968 }
969
970 WARN("unsupported interface: %s\n", debugstr_guid(iid));
971 return E_NOINTERFACE;
972 }
973
974 static ULONG WINAPI EnumTfLanguageProfiles_AddRef(IEnumTfLanguageProfiles *iface)
975 {
976 EnumTfLanguageProfiles *This = (EnumTfLanguageProfiles*)iface;
977 return InterlockedIncrement(&This->refCount);
978 }
979
980 static ULONG WINAPI EnumTfLanguageProfiles_Release(IEnumTfLanguageProfiles *iface)
981 {
982 EnumTfLanguageProfiles *This = (EnumTfLanguageProfiles *)iface;
983 ULONG ret;
984
985 ret = InterlockedDecrement(&This->refCount);
986 if (ret == 0)
987 EnumTfLanguageProfiles_Destructor(This);
988 return ret;
989 }
990
991 /*****************************************************
992 * IEnumGuid functions
993 *****************************************************/
994 static INT next_LanguageProfile(EnumTfLanguageProfiles *This, CLSID clsid, TF_LANGUAGEPROFILE *tflp)
995 {
996 WCHAR fullkey[168];
997 ULONG res;
998 WCHAR profileid[39];
999 DWORD cName = 39;
1000 GUID profile;
1001
1002 static const WCHAR fmt[] = {'%','s','\\','%','s','\\','0','x','%','0','8','x',0};
1003
1004 if (This->langkey == NULL)
1005 {
1006 sprintfW(fullkey,fmt,This->szwCurrentClsid,szwLngp,This->langid);
1007 res = RegOpenKeyExW(This->tipkey, fullkey, 0, KEY_READ | KEY_WRITE, &This->langkey);
1008 if (res)
1009 {
1010 This->langkey = NULL;
1011 return -1;
1012 }
1013 This->lang_index = 0;
1014 }
1015 res = RegEnumKeyExW(This->langkey, This->lang_index, profileid, &cName,
1016 NULL, NULL, NULL, NULL);
1017 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA)
1018 {
1019 RegCloseKey(This->langkey);
1020 This->langkey = NULL;
1021 return -1;
1022 }
1023 ++(This->lang_index);
1024
1025 if (tflp)
1026 {
1027 static const GUID * tipcats[3] = { &GUID_TFCAT_TIP_KEYBOARD,
1028 &GUID_TFCAT_TIP_SPEECH,
1029 &GUID_TFCAT_TIP_HANDWRITING };
1030 res = CLSIDFromString(profileid, &profile);
1031 if (FAILED(res)) return 0;
1032
1033 tflp->clsid = clsid;
1034 tflp->langid = This->langid;
1035 tflp->fActive = get_active_textservice(&clsid, NULL);
1036 tflp->guidProfile = profile;
1037 if (ITfCategoryMgr_FindClosestCategory(This->catmgr, &clsid,
1038 &tflp->catid, tipcats, 3) != S_OK)
1039 ITfCategoryMgr_FindClosestCategory(This->catmgr, &clsid,
1040 &tflp->catid, NULL, 0);
1041 }
1042
1043 return 1;
1044 }
1045
1046 static HRESULT WINAPI EnumTfLanguageProfiles_Next(IEnumTfLanguageProfiles *iface,
1047 ULONG ulCount, TF_LANGUAGEPROFILE *pProfile, ULONG *pcFetch)
1048 {
1049 EnumTfLanguageProfiles *This = (EnumTfLanguageProfiles *)iface;
1050 ULONG fetched = 0;
1051
1052 TRACE("(%p)\n",This);
1053
1054 if (pProfile == NULL) return E_POINTER;
1055
1056 if (This->tipkey) while (fetched < ulCount)
1057 {
1058 LSTATUS res;
1059 HRESULT hr;
1060 DWORD cName = 39;
1061 GUID clsid;
1062
1063 res = RegEnumKeyExW(This->tipkey, This->tip_index,
1064 This->szwCurrentClsid, &cName, NULL, NULL, NULL, NULL);
1065 if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
1066 ++(This->tip_index);
1067 hr = CLSIDFromString(This->szwCurrentClsid, &clsid);
1068 if (FAILED(hr)) continue;
1069
1070 while ( fetched < ulCount)
1071 {
1072 INT res = next_LanguageProfile(This, clsid, pProfile);
1073 if (res == 1)
1074 {
1075 ++fetched;
1076 ++pProfile;
1077 }
1078 else if (res == -1)
1079 break;
1080 else
1081 continue;
1082 }
1083 }
1084
1085 if (pcFetch) *pcFetch = fetched;
1086 return fetched == ulCount ? S_OK : S_FALSE;
1087 }
1088
1089 static HRESULT WINAPI EnumTfLanguageProfiles_Skip( IEnumTfLanguageProfiles* iface, ULONG celt)
1090 {
1091 EnumTfLanguageProfiles *This = (EnumTfLanguageProfiles *)iface;
1092 FIXME("STUB (%p)\n",This);
1093 return E_NOTIMPL;
1094 }
1095
1096 static HRESULT WINAPI EnumTfLanguageProfiles_Reset( IEnumTfLanguageProfiles* iface)
1097 {
1098 EnumTfLanguageProfiles *This = (EnumTfLanguageProfiles *)iface;
1099 TRACE("(%p)\n",This);
1100 This->tip_index = 0;
1101 if (This->langkey)
1102 RegCloseKey(This->langkey);
1103 This->langkey = NULL;
1104 This->lang_index = 0;
1105 return S_OK;
1106 }
1107
1108 static HRESULT WINAPI EnumTfLanguageProfiles_Clone( IEnumTfLanguageProfiles *iface,
1109 IEnumTfLanguageProfiles **ppenum)
1110 {
1111 EnumTfLanguageProfiles *This = (EnumTfLanguageProfiles *)iface;
1112 HRESULT res;
1113
1114 TRACE("(%p)\n",This);
1115
1116 if (ppenum == NULL) return E_POINTER;
1117
1118 res = EnumTfLanguageProfiles_Constructor(This->langid, ppenum);
1119 if (SUCCEEDED(res))
1120 {
1121 EnumTfLanguageProfiles *new_This = (EnumTfLanguageProfiles *)*ppenum;
1122 new_This->tip_index = This->tip_index;
1123 lstrcpynW(new_This->szwCurrentClsid,This->szwCurrentClsid,39);
1124
1125 if (This->langkey)
1126 {
1127 WCHAR fullkey[168];
1128 static const WCHAR fmt[] = {'%','s','\\','%','s','\\','0','x','%','0','8','x',0};
1129
1130 sprintfW(fullkey,fmt,This->szwCurrentClsid,szwLngp,This->langid);
1131 res = RegOpenKeyExW(new_This->tipkey, fullkey, 0, KEY_READ | KEY_WRITE, &This->langkey);
1132 new_This->lang_index = This->lang_index;
1133 }
1134 }
1135 return res;
1136 }
1137
1138 static const IEnumTfLanguageProfilesVtbl IEnumTfLanguageProfiles_Vtbl ={
1139 EnumTfLanguageProfiles_QueryInterface,
1140 EnumTfLanguageProfiles_AddRef,
1141 EnumTfLanguageProfiles_Release,
1142
1143 EnumTfLanguageProfiles_Clone,
1144 EnumTfLanguageProfiles_Next,
1145 EnumTfLanguageProfiles_Reset,
1146 EnumTfLanguageProfiles_Skip
1147 };
1148
1149 static HRESULT EnumTfLanguageProfiles_Constructor(LANGID langid, IEnumTfLanguageProfiles **ppOut)
1150 {
1151 HRESULT hr;
1152 EnumTfLanguageProfiles *This;
1153
1154 This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(EnumTfLanguageProfiles));
1155 if (This == NULL)
1156 return E_OUTOFMEMORY;
1157
1158 This->Vtbl= &IEnumTfLanguageProfiles_Vtbl;
1159 This->refCount = 1;
1160 This->langid = langid;
1161
1162 hr = CategoryMgr_Constructor(NULL,(IUnknown**)&This->catmgr);
1163 if (FAILED(hr))
1164 {
1165 HeapFree(GetProcessHeap(),0,This);
1166 return hr;
1167 }
1168
1169 if (RegCreateKeyExW(HKEY_LOCAL_MACHINE, szwSystemTIPKey, 0, NULL, 0,
1170 KEY_READ | KEY_WRITE, NULL, &This->tipkey, NULL) != ERROR_SUCCESS)
1171 {
1172 HeapFree(GetProcessHeap(), 0, This);
1173 return E_FAIL;
1174 }
1175
1176 TRACE("returning %p\n", This);
1177 *ppOut = (IEnumTfLanguageProfiles*)This;
1178 return S_OK;
1179 }