push 93d38255e04d8d2fa525a0100c5e1cee4a9f11a8
[wine/hacks.git] / dlls / inetcomm / mimeintl.c
blob626ad62ebcee749692a7e6563f0e03a953fddc40
1 /*
2 * MIME OLE International interface
4 * Copyright 2008 Huw Davies for CodeWeavers
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.
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.
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
21 #define COBJMACROS
22 #define NONAMELESSUNION
24 #include <stdarg.h>
25 #include <stdio.h>
27 #include "windef.h"
28 #include "winbase.h"
29 #include "winuser.h"
30 #include "winnls.h"
31 #include "objbase.h"
32 #include "ole2.h"
33 #include "mimeole.h"
34 #include "mlang.h"
36 #include "wine/list.h"
37 #include "wine/debug.h"
39 #include "inetcomm_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(inetcomm);
43 typedef struct
45 struct list entry;
46 INETCSETINFO cs_info;
47 } charset_entry;
49 typedef struct
51 const IMimeInternationalVtbl *lpVtbl;
52 LONG refs;
53 CRITICAL_SECTION cs;
55 struct list charsets;
56 LONG next_charset_handle;
57 HCHARSET default_charset;
58 } internat;
60 static inline internat *impl_from_IMimeInternational( IMimeInternational *iface )
62 return (internat *)((char*)iface - FIELD_OFFSET(internat, lpVtbl));
65 static inline HRESULT get_mlang(IMultiLanguage **ml)
67 return CoCreateInstance(&CLSID_CMultiLanguage, NULL, CLSCTX_INPROC_SERVER | CLSCTX_INPROC_HANDLER,
68 &IID_IMultiLanguage, (void **)ml);
71 static HRESULT WINAPI MimeInternat_QueryInterface( IMimeInternational *iface, REFIID riid, LPVOID *ppobj )
73 if (IsEqualGUID(riid, &IID_IUnknown) ||
74 IsEqualGUID(riid, &IID_IMimeInternational))
76 IMimeInternational_AddRef( iface );
77 *ppobj = iface;
78 return S_OK;
81 FIXME("interface %s not implemented\n", debugstr_guid(riid));
82 return E_NOINTERFACE;
85 static ULONG WINAPI MimeInternat_AddRef( IMimeInternational *iface )
87 internat *This = impl_from_IMimeInternational( iface );
88 return InterlockedIncrement(&This->refs);
91 static ULONG WINAPI MimeInternat_Release( IMimeInternational *iface )
93 internat *This = impl_from_IMimeInternational( iface );
94 ULONG refs;
96 refs = InterlockedDecrement(&This->refs);
97 if (!refs)
99 charset_entry *charset, *cursor2;
101 LIST_FOR_EACH_ENTRY_SAFE(charset, cursor2, &This->charsets, charset_entry, entry)
103 list_remove(&charset->entry);
104 HeapFree(GetProcessHeap(), 0, charset);
106 HeapFree(GetProcessHeap(), 0, This);
109 return refs;
112 static HRESULT WINAPI MimeInternat_SetDefaultCharset(IMimeInternational *iface, HCHARSET hCharset)
114 internat *This = impl_from_IMimeInternational( iface );
116 TRACE("(%p)->(%p)\n", iface, hCharset);
118 if(hCharset == NULL) return E_INVALIDARG;
119 /* FIXME check hCharset is valid */
121 InterlockedExchangePointer(&This->default_charset, hCharset);
123 return S_OK;
126 static HRESULT WINAPI MimeInternat_GetDefaultCharset(IMimeInternational *iface, LPHCHARSET phCharset)
128 internat *This = impl_from_IMimeInternational( iface );
129 HRESULT hr = S_OK;
131 TRACE("(%p)->(%p)\n", iface, phCharset);
133 if(This->default_charset == NULL)
135 HCHARSET hcs;
136 hr = IMimeInternational_GetCodePageCharset(iface, GetACP(), CHARSET_BODY, &hcs);
137 if(SUCCEEDED(hr))
138 InterlockedCompareExchangePointer(&This->default_charset, hcs, NULL);
140 *phCharset = This->default_charset;
142 return hr;
145 static HRESULT mlang_getcodepageinfo(UINT cp, MIMECPINFO *mlang_cp_info)
147 HRESULT hr;
148 IMultiLanguage *ml;
150 hr = get_mlang(&ml);
152 if(SUCCEEDED(hr))
154 hr = IMultiLanguage_GetCodePageInfo(ml, cp, mlang_cp_info);
155 IMultiLanguage_Release(ml);
157 return hr;
160 static HRESULT WINAPI MimeInternat_GetCodePageCharset(IMimeInternational *iface, CODEPAGEID cpiCodePage,
161 CHARSETTYPE ctCsetType,
162 LPHCHARSET phCharset)
164 HRESULT hr;
165 MIMECPINFO mlang_cp_info;
167 TRACE("(%p)->(%d, %d, %p)\n", iface, cpiCodePage, ctCsetType, phCharset);
169 *phCharset = NULL;
171 hr = mlang_getcodepageinfo(cpiCodePage, &mlang_cp_info);
172 if(SUCCEEDED(hr))
174 const WCHAR *charset_nameW = NULL;
175 char *charset_name;
176 DWORD len;
178 switch(ctCsetType)
180 case CHARSET_BODY:
181 charset_nameW = mlang_cp_info.wszBodyCharset;
182 break;
183 case CHARSET_HEADER:
184 charset_nameW = mlang_cp_info.wszHeaderCharset;
185 break;
186 case CHARSET_WEB:
187 charset_nameW = mlang_cp_info.wszWebCharset;
188 break;
189 default:
190 return MIME_E_INVALID_CHARSET_TYPE;
192 len = WideCharToMultiByte(CP_ACP, 0, charset_nameW, -1, NULL, 0, NULL, NULL);
193 charset_name = HeapAlloc(GetProcessHeap(), 0, len);
194 WideCharToMultiByte(CP_ACP, 0, charset_nameW, -1, charset_name, len, NULL, NULL);
195 hr = IMimeInternational_FindCharset(iface, charset_name, phCharset);
196 HeapFree(GetProcessHeap(), 0, charset_name);
198 return hr;
201 static HRESULT mlang_getcsetinfo(const char *charset, MIMECSETINFO *mlang_info)
203 DWORD len = MultiByteToWideChar(CP_ACP, 0, charset, -1, NULL, 0);
204 BSTR bstr = SysAllocStringLen(NULL, len - 1);
205 HRESULT hr;
206 IMultiLanguage *ml;
208 MultiByteToWideChar(CP_ACP, 0, charset, -1, bstr, len);
210 hr = get_mlang(&ml);
212 if(SUCCEEDED(hr))
214 hr = IMultiLanguage_GetCharsetInfo(ml, bstr, mlang_info);
215 IMultiLanguage_Release(ml);
217 SysFreeString(bstr);
218 if(FAILED(hr)) hr = MIME_E_NOT_FOUND;
219 return hr;
222 static HCHARSET add_charset(struct list *list, MIMECSETINFO *mlang_info, HCHARSET handle)
224 charset_entry *charset = HeapAlloc(GetProcessHeap(), 0, sizeof(*charset));
226 WideCharToMultiByte(CP_ACP, 0, mlang_info->wszCharset, -1,
227 charset->cs_info.szName, sizeof(charset->cs_info.szName), NULL, NULL);
228 charset->cs_info.cpiWindows = mlang_info->uiCodePage;
229 charset->cs_info.cpiInternet = mlang_info->uiInternetEncoding;
230 charset->cs_info.hCharset = handle;
231 charset->cs_info.dwReserved1 = 0;
232 list_add_head(list, &charset->entry);
234 return charset->cs_info.hCharset;
237 static HRESULT WINAPI MimeInternat_FindCharset(IMimeInternational *iface, LPCSTR pszCharset,
238 LPHCHARSET phCharset)
240 internat *This = impl_from_IMimeInternational( iface );
241 HRESULT hr = MIME_E_NOT_FOUND;
242 charset_entry *charset;
244 TRACE("(%p)->(%s, %p)\n", iface, debugstr_a(pszCharset), phCharset);
246 *phCharset = NULL;
248 EnterCriticalSection(&This->cs);
250 LIST_FOR_EACH_ENTRY(charset, &This->charsets, charset_entry, entry)
252 if(!strcmp(charset->cs_info.szName, pszCharset))
254 *phCharset = charset->cs_info.hCharset;
255 hr = S_OK;
256 break;
260 if(hr == MIME_E_NOT_FOUND)
262 MIMECSETINFO mlang_info;
264 LeaveCriticalSection(&This->cs);
265 hr = mlang_getcsetinfo(pszCharset, &mlang_info);
266 EnterCriticalSection(&This->cs);
268 if(SUCCEEDED(hr))
269 *phCharset = add_charset(&This->charsets, &mlang_info,
270 (HCHARSET)InterlockedIncrement(&This->next_charset_handle));
273 LeaveCriticalSection(&This->cs);
274 return hr;
277 static HRESULT WINAPI MimeInternat_GetCharsetInfo(IMimeInternational *iface, HCHARSET hCharset,
278 LPINETCSETINFO pCsetInfo)
280 internat *This = impl_from_IMimeInternational( iface );
281 HRESULT hr = MIME_E_INVALID_HANDLE;
282 charset_entry *charset;
284 TRACE("(%p)->(%p, %p)\n", iface, hCharset, pCsetInfo);
286 EnterCriticalSection(&This->cs);
288 LIST_FOR_EACH_ENTRY(charset, &This->charsets, charset_entry, entry)
290 if(charset->cs_info.hCharset == hCharset)
292 *pCsetInfo = charset->cs_info;
293 hr = S_OK;
294 break;
298 LeaveCriticalSection(&This->cs);
300 return hr;
303 static HRESULT WINAPI MimeInternat_GetCodePageInfo(IMimeInternational *iface, CODEPAGEID cpiCodePage,
304 LPCODEPAGEINFO pCodePageInfo)
306 FIXME("stub\n");
307 return E_NOTIMPL;
310 static HRESULT WINAPI MimeInternat_CanConvertCodePages(IMimeInternational *iface, CODEPAGEID cpiSource,
311 CODEPAGEID cpiDest)
313 HRESULT hr;
314 IMultiLanguage *ml;
316 TRACE("(%p)->(%d, %d)\n", iface, cpiSource, cpiDest);
318 /* Could call mlang.IsConvertINetStringAvailable() to avoid the COM overhead if need be. */
320 hr = get_mlang(&ml);
321 if(SUCCEEDED(hr))
323 hr = IMultiLanguage_IsConvertible(ml, cpiSource, cpiDest);
324 IMultiLanguage_Release(ml);
327 return hr;
330 static HRESULT WINAPI MimeInternat_DecodeHeader(IMimeInternational *iface, HCHARSET hCharset,
331 LPCSTR pszData,
332 LPPROPVARIANT pDecoded,
333 LPRFC1522INFO pRfc1522Info)
335 FIXME("stub\n");
336 return E_NOTIMPL;
339 static HRESULT WINAPI MimeInternat_EncodeHeader(IMimeInternational *iface, HCHARSET hCharset,
340 LPPROPVARIANT pData,
341 LPSTR *ppszEncoded,
342 LPRFC1522INFO pRfc1522Info)
344 FIXME("stub\n");
345 return E_NOTIMPL;
348 static HRESULT WINAPI MimeInternat_ConvertBuffer(IMimeInternational *iface, CODEPAGEID cpiSource,
349 CODEPAGEID cpiDest,
350 LPBLOB pIn,
351 LPBLOB pOut,
352 ULONG *pcbRead)
354 FIXME("stub\n");
355 return E_NOTIMPL;
358 static HRESULT WINAPI MimeInternat_ConvertString(IMimeInternational *iface, CODEPAGEID cpiSource,
359 CODEPAGEID cpiDest,
360 LPPROPVARIANT pIn,
361 LPPROPVARIANT pOut)
363 FIXME("stub\n");
364 return E_NOTIMPL;
367 static HRESULT WINAPI MimeInternat_MLANG_ConvertInetReset(IMimeInternational *iface)
369 FIXME("stub\n");
370 return E_NOTIMPL;
373 static HRESULT WINAPI MimeInternat_MLANG_ConvertInetString(IMimeInternational *iface, CODEPAGEID cpiSource,
374 CODEPAGEID cpiDest,
375 LPCSTR pSource,
376 int *pnSizeOfSource,
377 LPSTR pDestination,
378 int *pnDstSize)
380 FIXME("stub\n");
381 return E_NOTIMPL;
384 static HRESULT WINAPI MimeInternat_Rfc1522Decode(IMimeInternational *iface, LPCSTR pszValue,
385 LPSTR pszCharset,
386 ULONG cchmax,
387 LPSTR *ppszDecoded)
389 FIXME("stub\n");
390 return E_NOTIMPL;
393 static HRESULT WINAPI MimeInternat_Rfc1522Encode(IMimeInternational *iface, LPCSTR pszValue,
394 HCHARSET hCharset,
395 LPSTR *ppszEncoded)
397 FIXME("stub\n");
398 return E_NOTIMPL;
401 static IMimeInternationalVtbl mime_internat_vtbl =
403 MimeInternat_QueryInterface,
404 MimeInternat_AddRef,
405 MimeInternat_Release,
406 MimeInternat_SetDefaultCharset,
407 MimeInternat_GetDefaultCharset,
408 MimeInternat_GetCodePageCharset,
409 MimeInternat_FindCharset,
410 MimeInternat_GetCharsetInfo,
411 MimeInternat_GetCodePageInfo,
412 MimeInternat_CanConvertCodePages,
413 MimeInternat_DecodeHeader,
414 MimeInternat_EncodeHeader,
415 MimeInternat_ConvertBuffer,
416 MimeInternat_ConvertString,
417 MimeInternat_MLANG_ConvertInetReset,
418 MimeInternat_MLANG_ConvertInetString,
419 MimeInternat_Rfc1522Decode,
420 MimeInternat_Rfc1522Encode
423 static internat *global_internat;
425 HRESULT MimeInternational_Construct(IMimeInternational **internat)
427 global_internat = HeapAlloc(GetProcessHeap(), 0, sizeof(*global_internat));
428 global_internat->lpVtbl = &mime_internat_vtbl;
429 global_internat->refs = 0;
430 InitializeCriticalSection(&global_internat->cs);
432 list_init(&global_internat->charsets);
433 global_internat->next_charset_handle = 0;
434 global_internat->default_charset = NULL;
436 *internat = (IMimeInternational*)&global_internat->lpVtbl;
438 IMimeInternational_AddRef(*internat);
439 return S_OK;
442 HRESULT WINAPI MimeOleGetInternat(IMimeInternational **internat)
444 TRACE("(%p)\n", internat);
446 *internat = (IMimeInternational *)&global_internat->lpVtbl;
447 IMimeInternational_AddRef(*internat);
448 return S_OK;