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