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
22 #define NONAMELESSUNION
36 #include "wine/list.h"
37 #include "wine/debug.h"
39 #include "inetcomm_private.h"
41 WINE_DEFAULT_DEBUG_CHANNEL(inetcomm
);
51 IMimeInternational IMimeInternational_iface
;
56 LONG next_charset_handle
;
57 HCHARSET default_charset
;
60 static inline internat_impl
*impl_from_IMimeInternational(IMimeInternational
*iface
)
62 return CONTAINING_RECORD(iface
, internat_impl
, IMimeInternational_iface
);
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
);
81 FIXME("interface %s not implemented\n", debugstr_guid(riid
));
85 static ULONG WINAPI
MimeInternat_AddRef( IMimeInternational
*iface
)
87 internat_impl
*This
= impl_from_IMimeInternational( iface
);
88 return InterlockedIncrement(&This
->refs
);
91 static ULONG WINAPI
MimeInternat_Release( IMimeInternational
*iface
)
93 internat_impl
*This
= impl_from_IMimeInternational( iface
);
96 refs
= InterlockedDecrement(&This
->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 This
->cs
.DebugInfo
->Spare
[0] = 0;
107 DeleteCriticalSection(&This
->cs
);
108 HeapFree(GetProcessHeap(), 0, This
);
114 static HRESULT WINAPI
MimeInternat_SetDefaultCharset(IMimeInternational
*iface
, HCHARSET hCharset
)
116 internat_impl
*This
= impl_from_IMimeInternational( iface
);
118 TRACE("(%p)->(%p)\n", iface
, hCharset
);
120 if(hCharset
== NULL
) return E_INVALIDARG
;
121 /* FIXME check hCharset is valid */
123 InterlockedExchangePointer(&This
->default_charset
, hCharset
);
128 static HRESULT WINAPI
MimeInternat_GetDefaultCharset(IMimeInternational
*iface
, LPHCHARSET phCharset
)
130 internat_impl
*This
= impl_from_IMimeInternational( iface
);
133 TRACE("(%p)->(%p)\n", iface
, phCharset
);
135 if(This
->default_charset
== NULL
)
138 hr
= IMimeInternational_GetCodePageCharset(iface
, GetACP(), CHARSET_BODY
, &hcs
);
140 InterlockedCompareExchangePointer(&This
->default_charset
, hcs
, NULL
);
142 *phCharset
= This
->default_charset
;
147 static HRESULT
mlang_getcodepageinfo(UINT cp
, MIMECPINFO
*mlang_cp_info
)
156 hr
= IMultiLanguage_GetCodePageInfo(ml
, cp
, mlang_cp_info
);
157 IMultiLanguage_Release(ml
);
162 static HRESULT WINAPI
MimeInternat_GetCodePageCharset(IMimeInternational
*iface
, CODEPAGEID cpiCodePage
,
163 CHARSETTYPE ctCsetType
,
164 LPHCHARSET phCharset
)
167 MIMECPINFO mlang_cp_info
;
169 TRACE("(%p)->(%ld, %d, %p)\n", iface
, cpiCodePage
, ctCsetType
, phCharset
);
173 hr
= mlang_getcodepageinfo(cpiCodePage
, &mlang_cp_info
);
176 const WCHAR
*charset_nameW
= NULL
;
183 charset_nameW
= mlang_cp_info
.wszBodyCharset
;
186 charset_nameW
= mlang_cp_info
.wszHeaderCharset
;
189 charset_nameW
= mlang_cp_info
.wszWebCharset
;
192 return MIME_E_INVALID_CHARSET_TYPE
;
194 len
= WideCharToMultiByte(CP_ACP
, 0, charset_nameW
, -1, NULL
, 0, NULL
, NULL
);
195 charset_name
= HeapAlloc(GetProcessHeap(), 0, len
);
196 WideCharToMultiByte(CP_ACP
, 0, charset_nameW
, -1, charset_name
, len
, NULL
, NULL
);
197 hr
= IMimeInternational_FindCharset(iface
, charset_name
, phCharset
);
198 HeapFree(GetProcessHeap(), 0, charset_name
);
203 static HRESULT
mlang_getcsetinfo(const char *charset
, MIMECSETINFO
*mlang_info
)
205 DWORD len
= MultiByteToWideChar(CP_ACP
, 0, charset
, -1, NULL
, 0);
206 BSTR bstr
= SysAllocStringLen(NULL
, len
- 1);
210 MultiByteToWideChar(CP_ACP
, 0, charset
, -1, bstr
, len
);
216 hr
= IMultiLanguage_GetCharsetInfo(ml
, bstr
, mlang_info
);
217 IMultiLanguage_Release(ml
);
220 if(FAILED(hr
)) hr
= MIME_E_NOT_FOUND
;
224 static HCHARSET
add_charset(struct list
*list
, MIMECSETINFO
*mlang_info
, HCHARSET handle
)
226 charset_entry
*charset
= HeapAlloc(GetProcessHeap(), 0, sizeof(*charset
));
228 WideCharToMultiByte(CP_ACP
, 0, mlang_info
->wszCharset
, -1,
229 charset
->cs_info
.szName
, sizeof(charset
->cs_info
.szName
), NULL
, NULL
);
230 charset
->cs_info
.cpiWindows
= mlang_info
->uiCodePage
;
231 charset
->cs_info
.cpiInternet
= mlang_info
->uiInternetEncoding
;
232 charset
->cs_info
.hCharset
= handle
;
233 charset
->cs_info
.dwReserved1
= 0;
234 list_add_head(list
, &charset
->entry
);
236 return charset
->cs_info
.hCharset
;
239 static HRESULT WINAPI
MimeInternat_FindCharset(IMimeInternational
*iface
, LPCSTR pszCharset
,
240 LPHCHARSET phCharset
)
242 internat_impl
*This
= impl_from_IMimeInternational( iface
);
243 HRESULT hr
= MIME_E_NOT_FOUND
;
244 charset_entry
*charset
;
246 TRACE("(%p)->(%s, %p)\n", iface
, debugstr_a(pszCharset
), phCharset
);
250 EnterCriticalSection(&This
->cs
);
252 LIST_FOR_EACH_ENTRY(charset
, &This
->charsets
, charset_entry
, entry
)
254 if(!lstrcmpiA(charset
->cs_info
.szName
, pszCharset
))
256 *phCharset
= charset
->cs_info
.hCharset
;
262 if(hr
== MIME_E_NOT_FOUND
)
264 MIMECSETINFO mlang_info
;
266 LeaveCriticalSection(&This
->cs
);
267 hr
= mlang_getcsetinfo(pszCharset
, &mlang_info
);
268 EnterCriticalSection(&This
->cs
);
271 *phCharset
= add_charset(&This
->charsets
, &mlang_info
,
272 UlongToHandle(InterlockedIncrement(&This
->next_charset_handle
)));
275 LeaveCriticalSection(&This
->cs
);
279 static HRESULT WINAPI
MimeInternat_GetCharsetInfo(IMimeInternational
*iface
, HCHARSET hCharset
,
280 LPINETCSETINFO pCsetInfo
)
282 internat_impl
*This
= impl_from_IMimeInternational( iface
);
283 HRESULT hr
= MIME_E_INVALID_HANDLE
;
284 charset_entry
*charset
;
286 TRACE("(%p)->(%p, %p)\n", iface
, hCharset
, pCsetInfo
);
288 EnterCriticalSection(&This
->cs
);
290 LIST_FOR_EACH_ENTRY(charset
, &This
->charsets
, charset_entry
, entry
)
292 if(charset
->cs_info
.hCharset
== hCharset
)
294 *pCsetInfo
= charset
->cs_info
;
300 LeaveCriticalSection(&This
->cs
);
305 static HRESULT WINAPI
MimeInternat_GetCodePageInfo(IMimeInternational
*iface
, CODEPAGEID cpiCodePage
,
306 LPCODEPAGEINFO pCodePageInfo
)
312 static HRESULT WINAPI
MimeInternat_CanConvertCodePages(IMimeInternational
*iface
, CODEPAGEID cpiSource
,
318 TRACE("(%p)->(%ld, %ld)\n", iface
, cpiSource
, cpiDest
);
320 /* Could call mlang.IsConvertINetStringAvailable() to avoid the COM overhead if need be. */
325 hr
= IMultiLanguage_IsConvertible(ml
, cpiSource
, cpiDest
);
326 IMultiLanguage_Release(ml
);
332 static HRESULT WINAPI
MimeInternat_DecodeHeader(IMimeInternational
*iface
, HCHARSET hCharset
,
334 LPPROPVARIANT pDecoded
,
335 LPRFC1522INFO pRfc1522Info
)
341 static HRESULT WINAPI
MimeInternat_EncodeHeader(IMimeInternational
*iface
, HCHARSET hCharset
,
344 LPRFC1522INFO pRfc1522Info
)
350 static HRESULT WINAPI
MimeInternat_ConvertBuffer(IMimeInternational
*iface
, CODEPAGEID cpiSource
,
351 CODEPAGEID cpiDest
, LPBLOB pIn
, LPBLOB pOut
,
357 TRACE("(%p)->(%ld, %ld, %p, %p, %p)\n", iface
, cpiSource
, cpiDest
, pIn
, pOut
, pcbRead
);
362 /* Could call mlang.ConvertINetString() to avoid the COM overhead if need be. */
368 UINT in_size
= pIn
->cbSize
, out_size
;
370 hr
= IMultiLanguage_ConvertString(ml
, &mode
, cpiSource
, cpiDest
, pIn
->pBlobData
, &in_size
,
372 if(hr
== S_OK
) /* S_FALSE means the conversion could not be performed */
374 pOut
->pBlobData
= CoTaskMemAlloc(out_size
);
380 in_size
= pIn
->cbSize
;
381 hr
= IMultiLanguage_ConvertString(ml
, &mode
, cpiSource
, cpiDest
, pIn
->pBlobData
, &in_size
,
382 pOut
->pBlobData
, &out_size
);
387 pOut
->cbSize
= out_size
;
390 CoTaskMemFree(pOut
->pBlobData
);
393 IMultiLanguage_Release(ml
);
399 static HRESULT WINAPI
MimeInternat_ConvertString(IMimeInternational
*iface
, CODEPAGEID cpiSource
,
400 CODEPAGEID cpiDest
, LPPROPVARIANT pIn
,
407 TRACE("(%p)->(%ld, %ld, %p %p)\n", iface
, cpiSource
, cpiDest
, pIn
, pOut
);
412 if(cpiSource
== CP_UNICODE
) cpiSource
= GetACP();
413 src_len
= strlen(pIn
->pszVal
);
416 cpiSource
= CP_UNICODE
;
417 src_len
= lstrlenW(pIn
->pwszVal
) * sizeof(WCHAR
);
427 UINT in_size
= src_len
, out_size
;
429 hr
= IMultiLanguage_ConvertString(ml
, &mode
, cpiSource
, cpiDest
, (BYTE
*)pIn
->pszVal
, &in_size
,
431 if(hr
== S_OK
) /* S_FALSE means the conversion could not be performed */
433 out_size
+= (cpiDest
== CP_UNICODE
) ? sizeof(WCHAR
) : sizeof(char);
435 pOut
->pszVal
= CoTaskMemAlloc(out_size
);
442 hr
= IMultiLanguage_ConvertString(ml
, &mode
, cpiSource
, cpiDest
, (BYTE
*)pIn
->pszVal
, &in_size
,
443 (BYTE
*)pOut
->pszVal
, &out_size
);
447 if(cpiDest
== CP_UNICODE
)
449 pOut
->pwszVal
[out_size
/ sizeof(WCHAR
)] = 0;
450 pOut
->vt
= VT_LPWSTR
;
454 pOut
->pszVal
[out_size
] = '\0';
459 CoTaskMemFree(pOut
->pszVal
);
462 IMultiLanguage_Release(ml
);
467 static HRESULT WINAPI
MimeInternat_MLANG_ConvertInetReset(IMimeInternational
*iface
)
473 static HRESULT WINAPI
MimeInternat_MLANG_ConvertInetString(IMimeInternational
*iface
, CODEPAGEID cpiSource
,
484 static HRESULT WINAPI
MimeInternat_Rfc1522Decode(IMimeInternational
*iface
, LPCSTR pszValue
,
493 static HRESULT WINAPI
MimeInternat_Rfc1522Encode(IMimeInternational
*iface
, LPCSTR pszValue
,
501 static IMimeInternationalVtbl mime_internat_vtbl
=
503 MimeInternat_QueryInterface
,
505 MimeInternat_Release
,
506 MimeInternat_SetDefaultCharset
,
507 MimeInternat_GetDefaultCharset
,
508 MimeInternat_GetCodePageCharset
,
509 MimeInternat_FindCharset
,
510 MimeInternat_GetCharsetInfo
,
511 MimeInternat_GetCodePageInfo
,
512 MimeInternat_CanConvertCodePages
,
513 MimeInternat_DecodeHeader
,
514 MimeInternat_EncodeHeader
,
515 MimeInternat_ConvertBuffer
,
516 MimeInternat_ConvertString
,
517 MimeInternat_MLANG_ConvertInetReset
,
518 MimeInternat_MLANG_ConvertInetString
,
519 MimeInternat_Rfc1522Decode
,
520 MimeInternat_Rfc1522Encode
523 static internat_impl
*global_internat
;
525 HRESULT
MimeInternational_Construct(IMimeInternational
**internat
)
527 global_internat
= HeapAlloc(GetProcessHeap(), 0, sizeof(*global_internat
));
528 global_internat
->IMimeInternational_iface
.lpVtbl
= &mime_internat_vtbl
;
529 global_internat
->refs
= 0;
530 InitializeCriticalSection(&global_internat
->cs
);
531 global_internat
->cs
.DebugInfo
->Spare
[0] = (DWORD_PTR
)(__FILE__
": global_internat.cs");
533 list_init(&global_internat
->charsets
);
534 global_internat
->next_charset_handle
= 0;
535 global_internat
->default_charset
= NULL
;
537 *internat
= &global_internat
->IMimeInternational_iface
;
539 IMimeInternational_AddRef(*internat
);
543 HRESULT WINAPI
MimeOleGetInternat(IMimeInternational
**internat
)
545 TRACE("(%p)\n", internat
);
547 *internat
= &global_internat
->IMimeInternational_iface
;
548 IMimeInternational_AddRef(*internat
);
552 HRESULT WINAPI
MimeOleFindCharset(LPCSTR name
, LPHCHARSET charset
)
554 IMimeInternational
*internat
;
557 TRACE("(%s, %p)\n", debugstr_a(name
), charset
);
559 hr
= MimeOleGetInternat(&internat
);
562 hr
= IMimeInternational_FindCharset(internat
, name
, charset
);
563 IMimeInternational_Release(internat
);
568 HRESULT WINAPI
MimeOleGetCharsetInfo(HCHARSET hCharset
, LPINETCSETINFO pCsetInfo
)
570 IMimeInternational
*internat
;
573 TRACE("(%p, %p)\n", hCharset
, pCsetInfo
);
575 hr
= MimeOleGetInternat(&internat
);
578 hr
= IMimeInternational_GetCharsetInfo(internat
, hCharset
, pCsetInfo
);
579 IMimeInternational_Release(internat
);
584 HRESULT WINAPI
MimeOleGetDefaultCharset(LPHCHARSET charset
)
586 IMimeInternational
*internat
;
589 TRACE("(%p)\n", charset
);
591 hr
= MimeOleGetInternat(&internat
);
594 hr
= IMimeInternational_GetDefaultCharset(internat
, charset
);
595 IMimeInternational_Release(internat
);