msctf: Implement ITfInputProcessorProfiles::Unregister.
[wine] / dlls / 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 "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
36 #include "wine/unicode.h"
37
38 #include "msctf.h"
39 #include "msctf_internal.h"
40
41 WINE_DEFAULT_DEBUG_CHANNEL(msctf);
42
43 static const WCHAR szwLngp[] = {'L','a','n','g','u','a','g','e','P','r','o','f','i','l','e',0};
44 static const WCHAR szwEnabled[] = {'E','n','a','b','l','e','d',0};
45 static const WCHAR szwTipfmt[] = {'%','s','\\','%','s',0};
46 static const WCHAR szwFullLangfmt[] = {'%','s','\\','%','s','\\','%','s','\\','0','x','%','0','8','x','\\','%','s',0};
47
48 typedef struct tagInputProcessorProfiles {
49     const ITfInputProcessorProfilesVtbl *InputProcessorProfilesVtbl;
50     LONG refCount;
51
52     LANGID  currentLanguage;
53 } InputProcessorProfiles;
54
55 static void InputProcessorProfiles_Destructor(InputProcessorProfiles *This)
56 {
57     TRACE("destroying %p\n", This);
58     HeapFree(GetProcessHeap(),0,This);
59 }
60
61 static void add_userkey( REFCLSID rclsid, LANGID langid,
62                                 REFGUID guidProfile)
63 {
64     HKEY key;
65     WCHAR buf[39];
66     WCHAR buf2[39];
67     WCHAR fullkey[168];
68     DWORD disposition = 0;
69     ULONG res;
70
71     TRACE("\n");
72
73     StringFromGUID2(rclsid, buf, 39);
74     StringFromGUID2(guidProfile, buf2, 39);
75     sprintfW(fullkey,szwFullLangfmt,szwSystemTIPKey,buf,szwLngp,langid,buf2);
76
77     res = RegCreateKeyExW(HKEY_CURRENT_USER,fullkey, 0, NULL, 0,
78                    KEY_READ | KEY_WRITE, NULL, &key, &disposition);
79
80     if (!res && disposition == REG_CREATED_NEW_KEY)
81     {
82         DWORD zero = 0x0;
83         RegSetValueExW(key, szwEnabled, 0, REG_DWORD, (LPBYTE)&zero, sizeof(DWORD));
84     }
85
86     if (!res)
87         RegCloseKey(key);
88 }
89
90 static HRESULT WINAPI InputProcessorProfiles_QueryInterface(ITfInputProcessorProfiles *iface, REFIID iid, LPVOID *ppvOut)
91 {
92     InputProcessorProfiles *This = (InputProcessorProfiles *)iface;
93     *ppvOut = NULL;
94
95     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfInputProcessorProfiles))
96     {
97         *ppvOut = This;
98     }
99
100     if (*ppvOut)
101     {
102         IUnknown_AddRef(iface);
103         return S_OK;
104     }
105
106     WARN("unsupported interface: %s\n", debugstr_guid(iid));
107     return E_NOINTERFACE;
108 }
109
110 static ULONG WINAPI InputProcessorProfiles_AddRef(ITfInputProcessorProfiles *iface)
111 {
112     InputProcessorProfiles *This = (InputProcessorProfiles *)iface;
113     return InterlockedIncrement(&This->refCount);
114 }
115
116 static ULONG WINAPI InputProcessorProfiles_Release(ITfInputProcessorProfiles *iface)
117 {
118     InputProcessorProfiles *This = (InputProcessorProfiles *)iface;
119     ULONG ret;
120
121     ret = InterlockedDecrement(&This->refCount);
122     if (ret == 0)
123         InputProcessorProfiles_Destructor(This);
124     return ret;
125 }
126
127 /*****************************************************
128  * ITfInputProcessorProfiles functions
129  *****************************************************/
130 static HRESULT WINAPI InputProcessorProfiles_Register(
131         ITfInputProcessorProfiles *iface, REFCLSID rclsid)
132 {
133     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
134     HKEY tipkey;
135     WCHAR buf[39];
136     WCHAR fullkey[68];
137
138     TRACE("(%p) %s\n",This,debugstr_guid(rclsid));
139
140     StringFromGUID2(rclsid, buf, 39);
141     sprintfW(fullkey,szwTipfmt,szwSystemTIPKey,buf);
142
143     if (RegCreateKeyExW(HKEY_LOCAL_MACHINE,fullkey, 0, NULL, 0,
144                     KEY_READ | KEY_WRITE, NULL, &tipkey, NULL) != ERROR_SUCCESS)
145         return E_FAIL;
146
147     RegCloseKey(tipkey);
148
149     return S_OK;
150 }
151
152 static HRESULT WINAPI InputProcessorProfiles_Unregister(
153         ITfInputProcessorProfiles *iface, REFCLSID rclsid)
154 {
155     WCHAR buf[39];
156     WCHAR fullkey[68];
157     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
158
159     TRACE("(%p) %s\n",This,debugstr_guid(rclsid));
160
161     StringFromGUID2(rclsid, buf, 39);
162     sprintfW(fullkey,szwTipfmt,szwSystemTIPKey,buf);
163
164     RegDeleteTreeW(HKEY_LOCAL_MACHINE, fullkey);
165     RegDeleteTreeW(HKEY_CURRENT_USER, fullkey);
166
167     return S_OK;
168 }
169
170 static HRESULT WINAPI InputProcessorProfiles_AddLanguageProfile(
171         ITfInputProcessorProfiles *iface, REFCLSID rclsid,
172         LANGID langid, REFGUID guidProfile, const WCHAR *pchDesc,
173         ULONG cchDesc, const WCHAR *pchIconFile, ULONG cchFile,
174         ULONG uIconIndex)
175 {
176     HKEY tipkey,fmtkey;
177     WCHAR buf[39];
178     WCHAR fullkey[100];
179     ULONG res;
180     DWORD disposition = 0;
181
182     static const WCHAR fmt2[] = {'%','s','\\','0','x','%','0','8','x','\\','%','s',0};
183     static const WCHAR desc[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
184     static const WCHAR icnf[] = {'I','c','o','n','F','i','l','e',0};
185     static const WCHAR icni[] = {'I','c','o','n','I','n','d','e','x',0};
186
187     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
188
189     TRACE("(%p) %s %x %s %s %s %i\n",This,debugstr_guid(rclsid), langid,
190             debugstr_guid(guidProfile), debugstr_wn(pchDesc,cchDesc),
191             debugstr_wn(pchIconFile,cchFile),uIconIndex);
192
193     StringFromGUID2(rclsid, buf, 39);
194     sprintfW(fullkey,szwTipfmt,szwSystemTIPKey,buf);
195
196     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,fullkey, 0, KEY_READ | KEY_WRITE,
197                 &tipkey ) != ERROR_SUCCESS)
198         return E_FAIL;
199
200     StringFromGUID2(guidProfile, buf, 39);
201     sprintfW(fullkey,fmt2,szwLngp,langid,buf);
202
203     res = RegCreateKeyExW(tipkey,fullkey, 0, NULL, 0, KEY_READ | KEY_WRITE,
204             NULL, &fmtkey, &disposition);
205
206     if (!res)
207     {
208         DWORD zero = 0x0;
209         RegSetValueExW(fmtkey, desc, 0, REG_SZ, (LPBYTE)pchDesc, cchDesc * sizeof(WCHAR));
210         RegSetValueExW(fmtkey, icnf, 0, REG_SZ, (LPBYTE)pchIconFile, cchFile * sizeof(WCHAR));
211         RegSetValueExW(fmtkey, icni, 0, REG_DWORD, (LPBYTE)&uIconIndex, sizeof(DWORD));
212         if (disposition == REG_CREATED_NEW_KEY)
213             RegSetValueExW(fmtkey, szwEnabled, 0, REG_DWORD, (LPBYTE)&zero, sizeof(DWORD));
214         RegCloseKey(fmtkey);
215
216         add_userkey(rclsid, langid, guidProfile);
217     }
218     RegCloseKey(tipkey);
219
220     if (!res)
221         return S_OK;
222     else
223         return E_FAIL;
224 }
225
226 static HRESULT WINAPI InputProcessorProfiles_RemoveLanguageProfile(
227         ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
228         REFGUID guidProfile)
229 {
230     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
231     FIXME("STUB:(%p)\n",This);
232     return E_NOTIMPL;
233 }
234
235 static HRESULT WINAPI InputProcessorProfiles_EnumInputProcessorInfo(
236         ITfInputProcessorProfiles *iface, IEnumGUID **ppEnum)
237 {
238     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
239     FIXME("STUB:(%p)\n",This);
240     return E_NOTIMPL;
241 }
242
243 static HRESULT WINAPI InputProcessorProfiles_GetDefaultLanguageProfile(
244         ITfInputProcessorProfiles *iface, LANGID langid, REFGUID catid,
245         CLSID *pclsid, GUID *pguidProfile)
246 {
247     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
248     FIXME("STUB:(%p)\n",This);
249     return E_NOTIMPL;
250 }
251
252 static HRESULT WINAPI InputProcessorProfiles_SetDefaultLanguageProfile(
253         ITfInputProcessorProfiles *iface, LANGID langid, REFCLSID rclsid,
254         REFGUID guidProfiles)
255 {
256     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
257     FIXME("STUB:(%p)\n",This);
258     return E_NOTIMPL;
259 }
260
261 static HRESULT WINAPI InputProcessorProfiles_ActivateLanguageProfile(
262         ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
263         REFGUID guidProfiles)
264 {
265     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
266     FIXME("STUB:(%p)\n",This);
267     return E_NOTIMPL;
268 }
269
270 static HRESULT WINAPI InputProcessorProfiles_GetActiveLanguageProfile(
271         ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID *plangid,
272         GUID *pguidProfile)
273 {
274     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
275     FIXME("STUB:(%p)\n",This);
276     return E_NOTIMPL;
277 }
278
279 static HRESULT WINAPI InputProcessorProfiles_GetLanguageProfileDescription(
280         ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
281         REFGUID guidProfile, BSTR *pbstrProfile)
282 {
283     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
284     FIXME("STUB:(%p)\n",This);
285     return E_NOTIMPL;
286 }
287
288 static HRESULT WINAPI InputProcessorProfiles_GetCurrentLanguage(
289         ITfInputProcessorProfiles *iface, LANGID *plangid)
290 {
291     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
292     TRACE("(%p) 0x%x\n",This,This->currentLanguage);
293
294     if (!plangid)
295         return E_INVALIDARG;
296
297     *plangid = This->currentLanguage;
298
299     return S_OK;
300 }
301
302 static HRESULT WINAPI InputProcessorProfiles_ChangeCurrentLanguage(
303         ITfInputProcessorProfiles *iface, LANGID langid)
304 {
305     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
306     FIXME("STUB:(%p)\n",This);
307     return E_NOTIMPL;
308 }
309
310 static HRESULT WINAPI InputProcessorProfiles_GetLanguageList(
311         ITfInputProcessorProfiles *iface, LANGID **ppLangId, ULONG *pulCount)
312 {
313     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
314     FIXME("STUB:(%p)\n",This);
315     return E_NOTIMPL;
316 }
317
318 static HRESULT WINAPI InputProcessorProfiles_EnumLanguageProfiles(
319         ITfInputProcessorProfiles *iface, LANGID langid,
320         IEnumTfLanguageProfiles **ppEnum)
321 {
322     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
323     FIXME("STUB:(%p)\n",This);
324     return E_NOTIMPL;
325 }
326
327 static HRESULT WINAPI InputProcessorProfiles_EnableLanguageProfile(
328         ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
329         REFGUID guidProfile, BOOL fEnable)
330 {
331     HKEY key;
332     WCHAR buf[39];
333     WCHAR buf2[39];
334     WCHAR fullkey[168];
335     ULONG res;
336
337     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
338     TRACE("(%p) %s %x %s %i\n",This, debugstr_guid(rclsid), langid, debugstr_guid(guidProfile), fEnable);
339
340     StringFromGUID2(rclsid, buf, 39);
341     StringFromGUID2(guidProfile, buf2, 39);
342     sprintfW(fullkey,szwFullLangfmt,szwSystemTIPKey,buf,szwLngp,langid,buf2);
343
344     res = RegOpenKeyExW(HKEY_CURRENT_USER, fullkey, 0, KEY_READ | KEY_WRITE, &key);
345
346     if (!res)
347     {
348         RegSetValueExW(key, szwEnabled, 0, REG_DWORD, (LPBYTE)&fEnable, sizeof(DWORD));
349         RegCloseKey(key);
350     }
351     else
352         return E_FAIL;
353
354     return S_OK;
355 }
356
357 static HRESULT WINAPI InputProcessorProfiles_IsEnabledLanguageProfile(
358         ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
359         REFGUID guidProfile, BOOL *pfEnable)
360 {
361     HKEY key;
362     WCHAR buf[39];
363     WCHAR buf2[39];
364     WCHAR fullkey[168];
365     ULONG res;
366
367     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
368     TRACE("(%p) %s, %i, %s, %p\n",This,debugstr_guid(rclsid),langid,debugstr_guid(guidProfile),pfEnable);
369
370     if (!pfEnable)
371         return E_INVALIDARG;
372
373     StringFromGUID2(rclsid, buf, 39);
374     StringFromGUID2(guidProfile, buf2, 39);
375     sprintfW(fullkey,szwFullLangfmt,szwSystemTIPKey,buf,szwLngp,langid,buf2);
376
377     res = RegOpenKeyExW(HKEY_CURRENT_USER, fullkey, 0, KEY_READ | KEY_WRITE, &key);
378
379     if (!res)
380     {
381         DWORD count = sizeof(DWORD);
382         res = RegQueryValueExW(key, szwEnabled, 0, NULL, (LPBYTE)pfEnable, &count);
383         RegCloseKey(key);
384     }
385
386     if (res)  /* Try Default */
387     {
388         res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, fullkey, 0, KEY_READ | KEY_WRITE, &key);
389
390         if (!res)
391         {
392             DWORD count = sizeof(DWORD);
393             res = RegQueryValueExW(key, szwEnabled, 0, NULL, (LPBYTE)pfEnable, &count);
394             RegCloseKey(key);
395         }
396     }
397
398     if (!res)
399         return S_OK;
400     else
401         return E_FAIL;
402 }
403
404 static HRESULT WINAPI InputProcessorProfiles_EnableLanguageProfileByDefault(
405         ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
406         REFGUID guidProfile, BOOL fEnable)
407 {
408     HKEY key;
409     WCHAR buf[39];
410     WCHAR buf2[39];
411     WCHAR fullkey[168];
412     ULONG res;
413
414     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
415     TRACE("(%p) %s %x %s %i\n",This,debugstr_guid(rclsid),langid,debugstr_guid(guidProfile),fEnable);
416
417     StringFromGUID2(rclsid, buf, 39);
418     StringFromGUID2(guidProfile, buf2, 39);
419     sprintfW(fullkey,szwFullLangfmt,szwSystemTIPKey,buf,szwLngp,langid,buf2);
420
421     res = RegOpenKeyExW(HKEY_LOCAL_MACHINE, fullkey, 0, KEY_READ | KEY_WRITE, &key);
422
423     if (!res)
424     {
425         RegSetValueExW(key, szwEnabled, 0, REG_DWORD, (LPBYTE)&fEnable, sizeof(DWORD));
426         RegCloseKey(key);
427     }
428     else
429         return E_FAIL;
430
431     return S_OK;
432 }
433
434 static HRESULT WINAPI InputProcessorProfiles_SubstituteKeyboardLayout(
435         ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
436         REFGUID guidProfile, HKL hKL)
437 {
438     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
439     FIXME("STUB:(%p)\n",This);
440     return E_NOTIMPL;
441 }
442
443
444 static const ITfInputProcessorProfilesVtbl InputProcessorProfiles_InputProcessorProfilesVtbl =
445 {
446     InputProcessorProfiles_QueryInterface,
447     InputProcessorProfiles_AddRef,
448     InputProcessorProfiles_Release,
449
450     InputProcessorProfiles_Register,
451     InputProcessorProfiles_Unregister,
452     InputProcessorProfiles_AddLanguageProfile,
453     InputProcessorProfiles_RemoveLanguageProfile,
454     InputProcessorProfiles_EnumInputProcessorInfo,
455     InputProcessorProfiles_GetDefaultLanguageProfile,
456     InputProcessorProfiles_SetDefaultLanguageProfile,
457     InputProcessorProfiles_ActivateLanguageProfile,
458     InputProcessorProfiles_GetActiveLanguageProfile,
459     InputProcessorProfiles_GetLanguageProfileDescription,
460     InputProcessorProfiles_GetCurrentLanguage,
461     InputProcessorProfiles_ChangeCurrentLanguage,
462     InputProcessorProfiles_GetLanguageList,
463     InputProcessorProfiles_EnumLanguageProfiles,
464     InputProcessorProfiles_EnableLanguageProfile,
465     InputProcessorProfiles_IsEnabledLanguageProfile,
466     InputProcessorProfiles_EnableLanguageProfileByDefault,
467     InputProcessorProfiles_SubstituteKeyboardLayout
468 };
469
470 HRESULT InputProcessorProfiles_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
471 {
472     InputProcessorProfiles *This;
473     if (pUnkOuter)
474         return CLASS_E_NOAGGREGATION;
475
476     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(InputProcessorProfiles));
477     if (This == NULL)
478         return E_OUTOFMEMORY;
479
480     This->InputProcessorProfilesVtbl= &InputProcessorProfiles_InputProcessorProfilesVtbl;
481     This->refCount = 1;
482     This->currentLanguage = GetUserDefaultLCID();
483
484     TRACE("returning %p\n", This);
485     *ppOut = (IUnknown *)This;
486     return S_OK;
487 }