xref: /trunk/main/dbaccess/source/ui/misc/charsets.cxx (revision cdf0e10c)
1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 // MARKER(update_precomp.py): autogen include statement, do not remove
29 #include "precompiled_dbaccess.hxx"
30 
31 #ifndef _DBAUI_CHARSETS_HXX_
32 #include "charsets.hxx"
33 #endif
34 #ifndef _TOOLS_DEBUG_HXX
35 #include <tools/debug.hxx>
36 #endif
37 #ifndef _DBU_MISC_HRC_
38 #include "dbu_misc.hrc"
39 #endif
40 #ifndef _RTL_TENCINFO_H
41 #include <rtl/tencinfo.h>
42 #endif
43 #ifndef _TOOLS_RCID_H
44 #include <tools/rcid.h>
45 #endif
46 #ifndef _DBAUI_LOCALRESACCESS_HXX_
47 #include "localresaccess.hxx"
48 #endif
49 
50 //.........................................................................
51 namespace dbaui
52 {
53 //.........................................................................
54 	using namespace ::dbtools;
55 
56 	//=========================================================================
57 	//= OCharsetDisplay
58 	//=========================================================================
59 	//-------------------------------------------------------------------------
60 	OCharsetDisplay::OCharsetDisplay()
61 		:OCharsetMap()
62 		,SvxTextEncodingTable()
63 	{
64 		{
65 			LocalResourceAccess aCharsetStrings( RSC_CHARSETS, RSC_RESOURCE );
66 			m_aSystemDisplayName = String( ModuleRes( 1 ) );
67 		}
68 	}
69 
70 	//-------------------------------------------------------------------------
71 	sal_Bool OCharsetDisplay::approveEncoding( const rtl_TextEncoding _eEncoding, const rtl_TextEncodingInfo& _rInfo ) const
72 	{
73 		if ( !OCharsetMap::approveEncoding( _eEncoding, _rInfo ) )
74 			return sal_False;
75 
76 		if ( RTL_TEXTENCODING_DONTKNOW == _eEncoding )
77 			return sal_True;
78 
79 		return 0 != GetTextString( _eEncoding ).Len();
80 	}
81 
82 	//-------------------------------------------------------------------------
83 	OCharsetDisplay::const_iterator OCharsetDisplay::begin() const
84 	{
85 		return const_iterator( this, OCharsetMap::begin() );
86 	}
87 
88 	//-------------------------------------------------------------------------
89 	OCharsetDisplay::const_iterator OCharsetDisplay::end() const
90 	{
91 		return const_iterator( this, OCharsetMap::end() );
92 	}
93 
94 	//-------------------------------------------------------------------------
95 	OCharsetDisplay::const_iterator OCharsetDisplay::findEncoding(const rtl_TextEncoding _eEncoding) const
96 	{
97 		OCharsetMap::const_iterator aBaseIter = OCharsetMap::find(_eEncoding);
98 		return const_iterator( this, aBaseIter );
99 	}
100 
101 	//-------------------------------------------------------------------------
102 	OCharsetDisplay::const_iterator OCharsetDisplay::findIanaName(const ::rtl::OUString& _rIanaName) const
103 	{
104 		OCharsetMap::const_iterator aBaseIter = OCharsetMap::find(_rIanaName, OCharsetMap::IANA());
105 		return const_iterator( this, aBaseIter );
106 	}
107 
108 	//-------------------------------------------------------------------------
109 	OCharsetDisplay::const_iterator OCharsetDisplay::findDisplayName(const ::rtl::OUString& _rDisplayName) const
110 	{
111 		rtl_TextEncoding eEncoding = RTL_TEXTENCODING_DONTKNOW;
112 		if ( _rDisplayName != m_aSystemDisplayName )
113 		{
114 			eEncoding = GetTextEncoding( _rDisplayName );
115 			OSL_ENSURE( RTL_TEXTENCODING_DONTKNOW != eEncoding,
116 				"OCharsetDisplay::find: non-empty display name, but DONTKNOW!" );
117 		}
118 		return const_iterator( this, OCharsetMap::find( eEncoding ) );
119 	}
120 
121 	//=========================================================================
122 	//= CharsetDisplayDerefHelper
123 	//=========================================================================
124 	//-------------------------------------------------------------------------
125 	CharsetDisplayDerefHelper::CharsetDisplayDerefHelper(const CharsetDisplayDerefHelper& _rSource)
126 		:CharsetDisplayDerefHelper_Base(_rSource)
127 		,m_sDisplayName(m_sDisplayName)
128 	{
129 	}
130 
131 	//-------------------------------------------------------------------------
132 	CharsetDisplayDerefHelper::CharsetDisplayDerefHelper(const CharsetDisplayDerefHelper_Base& _rBase, const ::rtl::OUString& _rDisplayName)
133 		:CharsetDisplayDerefHelper_Base(_rBase)
134 		,m_sDisplayName(_rDisplayName)
135 	{
136 		DBG_ASSERT( m_sDisplayName.getLength(), "CharsetDisplayDerefHelper::CharsetDisplayDerefHelper: invalid display name!" );
137 	}
138 
139 	//=========================================================================
140 	//= OCharsetDisplay::ExtendedCharsetIterator
141 	//=========================================================================
142 	//-------------------------------------------------------------------------
143 	OCharsetDisplay::ExtendedCharsetIterator::ExtendedCharsetIterator( const OCharsetDisplay* _pContainer, const base_iterator& _rPosition )
144 		:m_pContainer(_pContainer)
145 		,m_aPosition(_rPosition)
146 	{
147 		DBG_ASSERT(m_pContainer, "OCharsetDisplay::ExtendedCharsetIterator::ExtendedCharsetIterator : invalid container!");
148 	}
149 
150 	//-------------------------------------------------------------------------
151 	OCharsetDisplay::ExtendedCharsetIterator::ExtendedCharsetIterator(const ExtendedCharsetIterator& _rSource)
152 		:m_pContainer( _rSource.m_pContainer )
153 		,m_aPosition( _rSource.m_aPosition )
154 	{
155 	}
156 
157 	//-------------------------------------------------------------------------
158 	CharsetDisplayDerefHelper OCharsetDisplay::ExtendedCharsetIterator::operator*() const
159 	{
160 		DBG_ASSERT( m_aPosition != m_pContainer->OCharsetDisplay_Base::end(), "OCharsetDisplay::ExtendedCharsetIterator::operator* : invalid position!");
161 
162 		rtl_TextEncoding eEncoding = (*m_aPosition).getEncoding();
163 		return CharsetDisplayDerefHelper(
164 			*m_aPosition,
165 			RTL_TEXTENCODING_DONTKNOW == eEncoding ? m_pContainer->m_aSystemDisplayName : (::rtl::OUString)m_pContainer->GetTextString( eEncoding )
166 		);
167 	}
168 
169 	//-------------------------------------------------------------------------
170 	const OCharsetDisplay::ExtendedCharsetIterator&	OCharsetDisplay::ExtendedCharsetIterator::operator++()
171 	{
172 		DBG_ASSERT( m_aPosition != m_pContainer->OCharsetDisplay_Base::end(), "OCharsetDisplay::ExtendedCharsetIterator::operator++ : invalid position!");
173 		if ( m_aPosition != m_pContainer->OCharsetDisplay_Base::end() )
174 			++m_aPosition;
175 		return *this;
176 	}
177 
178 	//-------------------------------------------------------------------------
179 	const OCharsetDisplay::ExtendedCharsetIterator&	OCharsetDisplay::ExtendedCharsetIterator::operator--()
180 	{
181 		DBG_ASSERT( m_aPosition != m_pContainer->OCharsetDisplay_Base::begin(), "OCharsetDisplay::ExtendedCharsetIterator::operator-- : invalid position!");
182 		if ( m_aPosition != m_pContainer->OCharsetDisplay_Base::begin() )
183 			--m_aPosition;
184 		return *this;
185 	}
186 
187 	//-------------------------------------------------------------------------
188 	bool operator==(const OCharsetDisplay::ExtendedCharsetIterator& lhs, const OCharsetDisplay::ExtendedCharsetIterator& rhs)
189 	{
190 		return (lhs.m_pContainer == rhs.m_pContainer) && (lhs.m_aPosition == rhs.m_aPosition);
191 	}
192 
193 //.........................................................................
194 }	// namespace dbaui
195 //.........................................................................
196 
197