msctf: Implement ITfInputProcessorProfiles::EnableLanguageProfile.
[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     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
156     FIXME("STUB:(%p)\n",This);
157     return E_NOTIMPL;
158 }
159
160 static HRESULT WINAPI InputProcessorProfiles_AddLanguageProfile(
161         ITfInputProcessorProfiles *iface, REFCLSID rclsid,
162         LANGID langid, REFGUID guidProfile, const WCHAR *pchDesc,
163         ULONG cchDesc, const WCHAR *pchIconFile, ULONG cchFile,
164         ULONG uIconIndex)
165 {
166     HKEY tipkey,fmtkey;
167     WCHAR buf[39];
168     WCHAR fullkey[100];
169     ULONG res;
170     DWORD disposition = 0;
171
172     static const WCHAR fmt2[] = {'%','s','\\','0','x','%','0','8','x','\\','%','s',0};
173     static const WCHAR desc[] = {'D','e','s','c','r','i','p','t','i','o','n',0};
174     static const WCHAR icnf[] = {'I','c','o','n','F','i','l','e',0};
175     static const WCHAR icni[] = {'I','c','o','n','I','n','d','e','x',0};
176
177     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
178
179     TRACE("(%p) %s %x %s %s %s %i\n",This,debugstr_guid(rclsid), langid,
180             debugstr_guid(guidProfile), debugstr_wn(pchDesc,cchDesc),
181             debugstr_wn(pchIconFile,cchFile),uIconIndex);
182
183     StringFromGUID2(rclsid, buf, 39);
184     sprintfW(fullkey,szwTipfmt,szwSystemTIPKey,buf);
185
186     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,fullkey, 0, KEY_READ | KEY_WRITE,
187                 &tipkey ) != ERROR_SUCCESS)
188         return E_FAIL;
189
190     StringFromGUID2(guidProfile, buf, 39);
191     sprintfW(fullkey,fmt2,szwLngp,langid,buf);
192
193     res = RegCreateKeyExW(tipkey,fullkey, 0, NULL, 0, KEY_READ | KEY_WRITE,
194             NULL, &fmtkey, &disposition);
195
196     if (!res)
197     {
198         DWORD zero = 0x0;
199         RegSetValueExW(fmtkey, desc, 0, REG_SZ, (LPBYTE)pchDesc, cchDesc * sizeof(WCHAR));
200         RegSetValueExW(fmtkey, icnf, 0, REG_SZ, (LPBYTE)pchIconFile, cchFile * sizeof(WCHAR));
201         RegSetValueExW(fmtkey, icni, 0, REG_DWORD, (LPBYTE)&uIconIndex, sizeof(DWORD));
202         if (disposition == REG_CREATED_NEW_KEY)
203             RegSetValueExW(fmtkey, szwEnabled, 0, REG_DWORD, (LPBYTE)&zero, sizeof(DWORD));
204         RegCloseKey(fmtkey);
205
206         add_userkey(rclsid, langid, guidProfile);
207     }
208     RegCloseKey(tipkey);
209
210     if (!res)
211         return S_OK;
212     else
213         return E_FAIL;
214 }
215
216 static HRESULT WINAPI InputProcessorProfiles_RemoveLanguageProfile(
217         ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
218         REFGUID guidProfile)
219 {
220     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
221     FIXME("STUB:(%p)\n",This);
222     return E_NOTIMPL;
223 }
224
225 static HRESULT WINAPI InputProcessorProfiles_EnumInputProcessorInfo(
226         ITfInputProcessorProfiles *iface, IEnumGUID **ppEnum)
227 {
228     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
229     FIXME("STUB:(%p)\n",This);
230     return E_NOTIMPL;
231 }
232
233 static HRESULT WINAPI InputProcessorProfiles_GetDefaultLanguageProfile(
234         ITfInputProcessorProfiles *iface, LANGID langid, REFGUID catid,
235         CLSID *pclsid, GUID *pguidProfile)
236 {
237     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
238     FIXME("STUB:(%p)\n",This);
239     return E_NOTIMPL;
240 }
241
242 static HRESULT WINAPI InputProcessorProfiles_SetDefaultLanguageProfile(
243         ITfInputProcessorProfiles *iface, LANGID langid, REFCLSID rclsid,
244         REFGUID guidProfiles)
245 {
246     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
247     FIXME("STUB:(%p)\n",This);
248     return E_NOTIMPL;
249 }
250
251 static HRESULT WINAPI InputProcessorProfiles_ActivateLanguageProfile(
252         ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
253         REFGUID guidProfiles)
254 {
255     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
256     FIXME("STUB:(%p)\n",This);
257     return E_NOTIMPL;
258 }
259
260 static HRESULT WINAPI InputProcessorProfiles_GetActiveLanguageProfile(
261         ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID *plangid,
262         GUID *pguidProfile)
263 {
264     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
265     FIXME("STUB:(%p)\n",This);
266     return E_NOTIMPL;
267 }
268
269 static HRESULT WINAPI InputProcessorProfiles_GetLanguageProfileDescription(
270         ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
271         REFGUID guidProfile, BSTR *pbstrProfile)
272 {
273     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
274     FIXME("STUB:(%p)\n",This);
275     return E_NOTIMPL;
276 }
277
278 static HRESULT WINAPI InputProcessorProfiles_GetCurrentLanguage(
279         ITfInputProcessorProfiles *iface, LANGID *plangid)
280 {
281     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
282     TRACE("(%p) 0x%x\n",This,This->currentLanguage);
283
284     if (!plangid)
285         return E_INVALIDARG;
286
287     *plangid = This->currentLanguage;
288
289     return S_OK;
290 }
291
292 static HRESULT WINAPI InputProcessorProfiles_ChangeCurrentLanguage(
293         ITfInputProcessorProfiles *iface, LANGID langid)
294 {
295     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
296     FIXME("STUB:(%p)\n",This);
297     return E_NOTIMPL;
298 }
299
300 static HRESULT WINAPI InputProcessorProfiles_GetLanguageList(
301         ITfInputProcessorProfiles *iface, LANGID **ppLangId, ULONG *pulCount)
302 {
303     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
304     FIXME("STUB:(%p)\n",This);
305     return E_NOTIMPL;
306 }
307
308 static HRESULT WINAPI InputProcessorProfiles_EnumLanguageProfiles(
309         ITfInputProcessorProfiles *iface, LANGID langid,
310         IEnumTfLanguageProfiles **ppEnum)
311 {
312     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
313     FIXME("STUB:(%p)\n",This);
314     return E_NOTIMPL;
315 }
316
317 static HRESULT WINAPI InputProcessorProfiles_EnableLanguageProfile(
318         ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
319         REFGUID guidProfile, BOOL fEnable)
320 {
321     HKEY key;
322     WCHAR buf[39];
323     WCHAR buf2[39];
324     WCHAR fullkey[168];
325     ULONG res;
326
327     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
328     TRACE("(%p) %s %x %s %i\n",This, debugstr_guid(rclsid), langid, debugstr_guid(guidProfile), fEnable);
329
330     StringFromGUID2(rclsid, buf, 39);
331     StringFromGUID2(guidProfile, buf2, 39);
332     sprintfW(fullkey,szwFullLangfmt,szwSystemTIPKey,buf,szwLngp,langid,buf2);
333
334     res = RegOpenKeyExW(HKEY_CURRENT_USER, fullkey, 0, KEY_READ | KEY_WRITE, &key);
335
336     if (!res)
337     {
338         RegSetValueExW(key, szwEnabled, 0, REG_DWORD, (LPBYTE)&fEnable, sizeof(DWORD));
339         RegCloseKey(key);
340     }
341     else
342         return E_FAIL;
343
344     return S_OK;
345 }
346
347 static HRESULT WINAPI InputProcessorProfiles_IsEnabledLanguageProfile(
348         ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
349         REFGUID guidProfile, BOOL *pfEnable)
350 {
351     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
352     FIXME("STUB:(%p)\n",This);
353     return E_NOTIMPL;
354 }
355
356 static HRESULT WINAPI InputProcessorProfiles_EnableLanguageProfileByDefault(
357         ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
358         REFGUID guidProfile, BOOL fEnable)
359 {
360     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
361     FIXME("STUB:(%p)\n",This);
362     return E_NOTIMPL;
363 }
364
365 static HRESULT WINAPI InputProcessorProfiles_SubstituteKeyboardLayout(
366         ITfInputProcessorProfiles *iface, REFCLSID rclsid, LANGID langid,
367         REFGUID guidProfile, HKL hKL)
368 {
369     InputProcessorProfiles *This = (InputProcessorProfiles*)iface;
370     FIXME("STUB:(%p)\n",This);
371     return E_NOTIMPL;
372 }
373
374
375 static const ITfInputProcessorProfilesVtbl InputProcessorProfiles_InputProcessorProfilesVtbl =
376 {
377     InputProcessorProfiles_QueryInterface,
378     InputProcessorProfiles_AddRef,
379     InputProcessorProfiles_Release,
380
381     InputProcessorProfiles_Register,
382     InputProcessorProfiles_Unregister,
383     InputProcessorProfiles_AddLanguageProfile,
384     InputProcessorProfiles_RemoveLanguageProfile,
385     InputProcessorProfiles_EnumInputProcessorInfo,
386     InputProcessorProfiles_GetDefaultLanguageProfile,
387     InputProcessorProfiles_SetDefaultLanguageProfile,
388     InputProcessorProfiles_ActivateLanguageProfile,
389     InputProcessorProfiles_GetActiveLanguageProfile,
390     InputProcessorProfiles_GetLanguageProfileDescription,
391     InputProcessorProfiles_GetCurrentLanguage,
392     InputProcessorProfiles_ChangeCurrentLanguage,
393     InputProcessorProfiles_GetLanguageList,
394     InputProcessorProfiles_EnumLanguageProfiles,
395     InputProcessorProfiles_EnableLanguageProfile,
396     InputProcessorProfiles_IsEnabledLanguageProfile,
397     InputProcessorProfiles_EnableLanguageProfileByDefault,
398     InputProcessorProfiles_SubstituteKeyboardLayout
399 };
400
401 HRESULT InputProcessorProfiles_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
402 {
403     InputProcessorProfiles *This;
404     if (pUnkOuter)
405         return CLASS_E_NOAGGREGATION;
406
407     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(InputProcessorProfiles));
408     if (This == NULL)
409         return E_OUTOFMEMORY;
410
411     This->InputProcessorProfilesVtbl= &InputProcessorProfiles_InputProcessorProfilesVtbl;
412     This->refCount = 1;
413     This->currentLanguage = GetUserDefaultLCID();
414
415     TRACE("returning %p\n", This);
416     *ppOut = (IUnknown *)This;
417     return S_OK;
418 }