msctf: Helper functions for generating DWORD cookies.
[wine] / dlls / msctf / categorymgr.c
1 /*
2  *  ITfCategoryMgr 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 typedef struct tagCategoryMgr {
44     const ITfCategoryMgrVtbl *CategoryMgrVtbl;
45     LONG refCount;
46 } CategoryMgr;
47
48 static void CategoryMgr_Destructor(CategoryMgr *This)
49 {
50     TRACE("destroying %p\n", This);
51     HeapFree(GetProcessHeap(),0,This);
52 }
53
54 static HRESULT WINAPI CategoryMgr_QueryInterface(ITfCategoryMgr *iface, REFIID iid, LPVOID *ppvOut)
55 {
56     CategoryMgr *This = (CategoryMgr *)iface;
57     *ppvOut = NULL;
58
59     if (IsEqualIID(iid, &IID_IUnknown) || IsEqualIID(iid, &IID_ITfCategoryMgr))
60     {
61         *ppvOut = This;
62     }
63
64     if (*ppvOut)
65     {
66         IUnknown_AddRef(iface);
67         return S_OK;
68     }
69
70     WARN("unsupported interface: %s\n", debugstr_guid(iid));
71     return E_NOINTERFACE;
72 }
73
74 static ULONG WINAPI CategoryMgr_AddRef(ITfCategoryMgr *iface)
75 {
76     CategoryMgr *This = (CategoryMgr *)iface;
77     return InterlockedIncrement(&This->refCount);
78 }
79
80 static ULONG WINAPI CategoryMgr_Release(ITfCategoryMgr *iface)
81 {
82     CategoryMgr *This = (CategoryMgr *)iface;
83     ULONG ret;
84
85     ret = InterlockedDecrement(&This->refCount);
86     if (ret == 0)
87         CategoryMgr_Destructor(This);
88     return ret;
89 }
90
91 /*****************************************************
92  * ITfCategoryMgr functions
93  *****************************************************/
94
95 static HRESULT WINAPI CategoryMgr_RegisterCategory ( ITfCategoryMgr *iface,
96         REFCLSID rclsid, REFGUID rcatid, REFGUID rguid)
97 {
98     WCHAR fullkey[110];
99     WCHAR buf[39];
100     WCHAR buf2[39];
101     ULONG res;
102     HKEY tipkey,catkey,itmkey;
103     CategoryMgr *This = (CategoryMgr*)iface;
104
105     static const WCHAR ctg[] = {'C','a','t','e','g','o','r','y',0};
106     static const WCHAR itm[] = {'I','t','e','m',0};
107     static const WCHAR fmt[] = {'%','s','\\','%','s',0};
108     static const WCHAR fmt2[] = {'%','s','\\','%','s','\\','%','s','\\','%','s',0};
109
110     TRACE("(%p) %s %s %s\n",This,debugstr_guid(rclsid), debugstr_guid(rcatid), debugstr_guid(rguid));
111
112     StringFromGUID2(rclsid, buf, 39);
113     sprintfW(fullkey,fmt,szwSystemTIPKey,buf);
114
115     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,fullkey, 0, KEY_READ | KEY_WRITE,
116                 &tipkey ) != ERROR_SUCCESS)
117         return E_FAIL;
118
119     StringFromGUID2(rcatid, buf, 39);
120     StringFromGUID2(rguid, buf2, 39);
121     sprintfW(fullkey,fmt2,ctg,ctg,buf,buf2);
122
123     res = RegCreateKeyExW(tipkey, fullkey, 0, NULL, 0, KEY_READ | KEY_WRITE,
124             NULL, &catkey, NULL);
125     RegCloseKey(catkey);
126
127     if (!res)
128     {
129         sprintfW(fullkey,fmt2,ctg,itm,buf2,buf);
130         res = RegCreateKeyExW(tipkey, fullkey, 0, NULL, 0, KEY_READ | KEY_WRITE,
131                 NULL, &itmkey, NULL);
132
133         RegCloseKey(itmkey);
134     }
135
136     RegCloseKey(tipkey);
137
138     if (!res)
139         return S_OK;
140     else
141         return E_FAIL;
142 }
143
144 static HRESULT WINAPI CategoryMgr_UnregisterCategory ( ITfCategoryMgr *iface,
145         REFCLSID rclsid, REFGUID rcatid, REFGUID rguid)
146 {
147     CategoryMgr *This = (CategoryMgr*)iface;
148     FIXME("STUB:(%p)\n",This);
149     return E_NOTIMPL;
150 }
151
152 static HRESULT WINAPI CategoryMgr_EnumCategoriesInItem ( ITfCategoryMgr *iface,
153         REFGUID rguid, IEnumGUID **ppEnum)
154 {
155     CategoryMgr *This = (CategoryMgr*)iface;
156     FIXME("STUB:(%p)\n",This);
157     return E_NOTIMPL;
158 }
159
160 static HRESULT WINAPI CategoryMgr_EnumItemsInCategory ( ITfCategoryMgr *iface,
161         REFGUID rcatid, IEnumGUID **ppEnum)
162 {
163     CategoryMgr *This = (CategoryMgr*)iface;
164     FIXME("STUB:(%p)\n",This);
165     return E_NOTIMPL;
166 }
167
168 static HRESULT WINAPI CategoryMgr_FindClosestCategory ( ITfCategoryMgr *iface,
169         REFGUID rguid, GUID *pcatid, const GUID **ppcatidList, ULONG ulCount)
170 {
171     static const WCHAR fmt[] = { '%','s','\\','%','s','\\','C','a','t','e','g','o','r','y','\\','I','t','e','m','\\','%','s',0};
172
173     WCHAR fullkey[110];
174     WCHAR buf[39];
175     HKEY key;
176     HRESULT hr = S_FALSE;
177     INT index = 0;
178     CategoryMgr *This = (CategoryMgr*)iface;
179
180     TRACE("(%p)\n",This);
181
182     if (!pcatid || (ulCount && ppcatidList == NULL))
183         return E_INVALIDARG;
184
185     StringFromGUID2(rguid, buf, 39);
186     sprintfW(fullkey,fmt,szwSystemTIPKey,buf,buf);
187     *pcatid = GUID_NULL;
188
189     if (RegOpenKeyExW(HKEY_LOCAL_MACHINE,fullkey, 0, KEY_READ, &key ) !=
190             ERROR_SUCCESS)
191         return S_FALSE;
192
193     while (1)
194     {
195         HRESULT hr2;
196         ULONG res;
197         GUID guid;
198         WCHAR catid[39];
199         DWORD cName;
200
201         cName = 39;
202         res = RegEnumKeyExW(key, index, catid, &cName, NULL, NULL, NULL, NULL);
203         if (res != ERROR_SUCCESS && res != ERROR_MORE_DATA) break;
204         index ++;
205
206         hr2 = CLSIDFromString(catid, &guid);
207         if (FAILED(hr2)) continue;
208
209         if (ulCount)
210         {
211             int j;
212             BOOL found = FALSE;
213             for (j = 0; j < ulCount; j++)
214                 if (IsEqualGUID(&guid, ppcatidList[j]))
215                 {
216                     found = TRUE;
217                     *pcatid = guid;
218                     hr = S_OK;
219                     break;
220                 }
221             if (found) break;
222         }
223         else
224         {
225             *pcatid = guid;
226             hr = S_OK;
227             break;
228         }
229     }
230
231     return hr;
232 }
233
234 static HRESULT WINAPI CategoryMgr_RegisterGUIDDescription (
235         ITfCategoryMgr *iface, REFCLSID rclsid, REFGUID rguid,
236         const WCHAR *pchDesc, ULONG cch)
237 {
238     CategoryMgr *This = (CategoryMgr*)iface;
239     FIXME("STUB:(%p)\n",This);
240     return E_NOTIMPL;
241 }
242
243 static HRESULT WINAPI CategoryMgr_UnregisterGUIDDescription (
244         ITfCategoryMgr *iface, REFCLSID rclsid, REFGUID rguid)
245 {
246     CategoryMgr *This = (CategoryMgr*)iface;
247     FIXME("STUB:(%p)\n",This);
248     return E_NOTIMPL;
249 }
250
251 static HRESULT WINAPI CategoryMgr_GetGUIDDescription ( ITfCategoryMgr *iface,
252         REFGUID rguid, BSTR *pbstrDesc)
253 {
254     CategoryMgr *This = (CategoryMgr*)iface;
255     FIXME("STUB:(%p)\n",This);
256     return E_NOTIMPL;
257 }
258
259 static HRESULT WINAPI CategoryMgr_RegisterGUIDDWORD ( ITfCategoryMgr *iface,
260         REFCLSID rclsid, REFGUID rguid, DWORD dw)
261 {
262     CategoryMgr *This = (CategoryMgr*)iface;
263     FIXME("STUB:(%p)\n",This);
264     return E_NOTIMPL;
265 }
266
267 static HRESULT WINAPI CategoryMgr_UnregisterGUIDDWORD ( ITfCategoryMgr *iface,
268         REFCLSID rclsid, REFGUID rguid)
269 {
270     CategoryMgr *This = (CategoryMgr*)iface;
271     FIXME("STUB:(%p)\n",This);
272     return E_NOTIMPL;
273 }
274
275 static HRESULT WINAPI CategoryMgr_GetGUIDDWORD ( ITfCategoryMgr *iface,
276         REFGUID rguid, DWORD *pdw)
277 {
278     CategoryMgr *This = (CategoryMgr*)iface;
279     FIXME("STUB:(%p)\n",This);
280     return E_NOTIMPL;
281 }
282
283 static HRESULT WINAPI CategoryMgr_RegisterGUID ( ITfCategoryMgr *iface,
284         REFGUID rguid, TfGuidAtom *pguidatom
285 )
286 {
287     CategoryMgr *This = (CategoryMgr*)iface;
288     FIXME("STUB:(%p)\n",This);
289     return E_NOTIMPL;
290 }
291
292 static HRESULT WINAPI CategoryMgr_GetGUID ( ITfCategoryMgr *iface,
293         TfGuidAtom guidatom, GUID *pguid)
294 {
295     CategoryMgr *This = (CategoryMgr*)iface;
296     FIXME("STUB:(%p)\n",This);
297     return E_NOTIMPL;
298 }
299
300 static HRESULT WINAPI CategoryMgr_IsEqualTfGuidAtom ( ITfCategoryMgr *iface,
301         TfGuidAtom guidatom, REFGUID rguid, BOOL *pfEqual)
302 {
303     CategoryMgr *This = (CategoryMgr*)iface;
304     FIXME("STUB:(%p)\n",This);
305     return E_NOTIMPL;
306 }
307
308
309 static const ITfCategoryMgrVtbl CategoryMgr_CategoryMgrVtbl =
310 {
311     CategoryMgr_QueryInterface,
312     CategoryMgr_AddRef,
313     CategoryMgr_Release,
314
315     CategoryMgr_RegisterCategory,
316     CategoryMgr_UnregisterCategory,
317     CategoryMgr_EnumCategoriesInItem,
318     CategoryMgr_EnumItemsInCategory,
319     CategoryMgr_FindClosestCategory,
320     CategoryMgr_RegisterGUIDDescription,
321     CategoryMgr_UnregisterGUIDDescription,
322     CategoryMgr_GetGUIDDescription,
323     CategoryMgr_RegisterGUIDDWORD,
324     CategoryMgr_UnregisterGUIDDWORD,
325     CategoryMgr_GetGUIDDWORD,
326     CategoryMgr_RegisterGUID,
327     CategoryMgr_GetGUID,
328     CategoryMgr_IsEqualTfGuidAtom
329 };
330
331 HRESULT CategoryMgr_Constructor(IUnknown *pUnkOuter, IUnknown **ppOut)
332 {
333     CategoryMgr *This;
334     if (pUnkOuter)
335         return CLASS_E_NOAGGREGATION;
336
337     This = HeapAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,sizeof(CategoryMgr));
338     if (This == NULL)
339         return E_OUTOFMEMORY;
340
341     This->CategoryMgrVtbl= &CategoryMgr_CategoryMgrVtbl;
342     This->refCount = 1;
343
344     TRACE("returning %p\n", This);
345     *ppOut = (IUnknown *)This;
346     return S_OK;
347 }