Back to index

lightning-sunbird  0.9+nobinonly
ImportTranslate.cpp
Go to the documentation of this file.
00001 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
00002 /* ***** BEGIN LICENSE BLOCK *****
00003  * Version: MPL 1.1/GPL 2.0/LGPL 2.1
00004  *
00005  * The contents of this file are subject to the Mozilla Public License Version
00006  * 1.1 (the "License"); you may not use this file except in compliance with
00007  * the License. You may obtain a copy of the License at
00008  * http://www.mozilla.org/MPL/
00009  *
00010  * Software distributed under the License is distributed on an "AS IS" basis,
00011  * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
00012  * for the specific language governing rights and limitations under the
00013  * License.
00014  *
00015  * The Original Code is mozilla.org code.
00016  *
00017  * The Initial Developer of the Original Code is
00018  * Netscape Communications Corporation.
00019  * Portions created by the Initial Developer are Copyright (C) 1998
00020  * the Initial Developer. All Rights Reserved.
00021  *
00022  * Contributor(s):
00023  *
00024  * Alternatively, the contents of this file may be used under the terms of
00025  * either of the GNU General Public License Version 2 or later (the "GPL"),
00026  * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
00027  * in which case the provisions of the GPL or the LGPL are applicable instead
00028  * of those above. If you wish to allow use of your version of this file only
00029  * under the terms of either the GPL or the LGPL, and not to allow others to
00030  * use your version of this file under the terms of the MPL, indicate your
00031  * decision by deleting the provisions above and replace them with the notice
00032  * and other provisions required by the GPL or the LGPL. If you do not delete
00033  * the provisions above, a recipient may use your version of this file under
00034  * the terms of any one of the MPL, the GPL or the LGPL.
00035  *
00036  * ***** END LICENSE BLOCK ***** */
00037 
00038 #include "ImportTranslate.h"
00039 
00040 int ImportTranslate::m_useTranslator = -1;
00041 
00042 
00043 PRBool ImportTranslate::ConvertString( const nsCString& inStr, nsCString& outStr, PRBool mimeHeader)
00044 {
00045        if (inStr.IsEmpty()) {
00046               outStr = inStr;
00047               return( PR_TRUE);
00048        }
00049 
00050        nsImportTranslator *pTrans = GetTranslator();
00051        // int               maxLen = (int) pTrans->GetMaxBufferSize( inStr.Length());
00052        // int               hLen = 0;
00053        nsCString     set;
00054        nsCString     lang;
00055 
00056        if (mimeHeader) {
00057               // add the charset and language
00058               pTrans->GetCharset( set);
00059               pTrans->GetLanguage( lang);
00060        }
00061        
00062        // Unfortunatly, we didn't implement ConvertBuffer for all translators,
00063        // just ConvertToFile.  This means that this data will not always
00064        // be converted to the charset of pTrans.  In that case...
00065        // We don't always have the data in the same charset as the current
00066        // translator...
00067        // It is safer to leave the charset and language field blank
00068        set.Truncate();
00069        lang.Truncate();
00070 
00071        PRUint8 *     pBuf;
00072        /*
00073        pBuf = (P_U8) outStr.GetBuffer( maxLen);
00074        if (!pBuf) {
00075               delete pTrans;
00076               return( FALSE);
00077        }
00078        pTrans->ConvertBuffer( (PC_U8)(PC_S8)inStr, inStr.GetLength(), pBuf);
00079        outStr.ReleaseBuffer();
00080        */
00081        outStr = inStr;
00082        delete pTrans;
00083        
00084 
00085        // Now I need to run the string through the mime-header special char
00086        // encoder.
00087 
00088        pTrans = new CMHTranslator;
00089        pBuf = new PRUint8[pTrans->GetMaxBufferSize( outStr.Length())];
00090        pTrans->ConvertBuffer( (const PRUint8 *)(outStr.get()), outStr.Length(), pBuf);
00091        delete pTrans;
00092        outStr.Truncate();
00093        if (mimeHeader) {
00094               outStr = set;
00095               outStr += "'";
00096               outStr += lang;
00097               outStr += "'";
00098        }
00099        outStr += (const char *)pBuf;
00100        delete [] pBuf;
00101 
00102        return( PR_TRUE);
00103 }
00104 
00105 
00106 nsImportTranslator *ImportTranslate::GetTranslator( void)
00107 {
00108        if (m_useTranslator == -1) {
00109               // get the translator to use...
00110               // CString           trans;
00111               // trans.LoadString( IDS_LANGUAGE_TRANSLATION);
00112               m_useTranslator = 0;
00113               // if (!trans.CompareNoCase( "iso-2022-jp"))
00114               //     gWizData.m_useTranslator = 1;
00115        }
00116 
00117        switch( m_useTranslator) {
00118        case 0:
00119               return( new nsImportTranslator);
00120        //case 1:
00121        //     return( new CSJis2JisTranslator);
00122        default:
00123               return( new nsImportTranslator);
00124        }
00125 }
00126 
00127 nsImportTranslator *ImportTranslate::GetMatchingTranslator( const char *pCharSet)
00128 {
00129 /*
00130        CString              jp = "iso-2022-jp";
00131        if (!jp.CompareNoCase( pCharSet))
00132               return( new CSJis2JisTranslator);
00133 */
00134 
00135        return( nsnull);
00136 }
00137