xref: /aoo41x/main/svl/source/items/ctypeitm.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_svl.hxx"
30 #include <com/sun/star/uno/Any.hxx>
31 
32 #include <unotools/intlwrapper.hxx>
33 #include <tools/stream.hxx>
34 #include <svl/ctypeitm.hxx>
35 
36 //============================================================================
37 // The following defines are copied from chaos/source/items/cstritem.cxx:
38 #define CNTSTRINGITEM_STREAM_MAGIC   ( (sal_uInt32)0xfefefefe )
39 #define CNTSTRINGITEM_STREAM_SEEKREL (-( (long)( sizeof( sal_uInt32 ) ) ) )
40 
41 //============================================================================
42 //
43 // class CntContentTypeItem Implementation.
44 //
45 //============================================================================
46 
47 TYPEINIT1_AUTOFACTORY( CntContentTypeItem, CntUnencodedStringItem );
48 
49 #define CONTENT_TYPE_NOT_INIT ( (INetContentType)-1 )
50 
51 //----------------------------------------------------------------------------
52 CntContentTypeItem::CntContentTypeItem()
53 : CntUnencodedStringItem(),
54   _eType( CONTENT_TYPE_NOT_INIT )
55 {
56 }
57 
58 //----------------------------------------------------------------------------
59 CntContentTypeItem::CntContentTypeItem(	sal_uInt16 which, const XubString& rType )
60 : CntUnencodedStringItem( which, rType ),
61   _eType( CONTENT_TYPE_NOT_INIT )
62 {
63 }
64 
65 //----------------------------------------------------------------------------
66 CntContentTypeItem::CntContentTypeItem(	sal_uInt16 which,
67 										const INetContentType eType )
68 : CntUnencodedStringItem( which, INetContentTypes::GetContentType( eType ) ),
69   _eType( eType )
70 {
71 }
72 
73 //----------------------------------------------------------------------------
74 CntContentTypeItem::CntContentTypeItem( const CntContentTypeItem& rOrig )
75 : CntUnencodedStringItem( rOrig ),
76   _eType( rOrig._eType ),
77   _aPresentation( rOrig._aPresentation )
78 {
79 }
80 
81 //============================================================================
82 // virtual
83 sal_uInt16 CntContentTypeItem::GetVersion(sal_uInt16) const
84 {
85 	return 1; // because it uses SfxPoolItem::read/writeUnicodeString()
86 }
87 
88 //----------------------------------------------------------------------------
89 // virtual
90 SfxPoolItem* CntContentTypeItem::Create( SvStream& rStream,
91 										 sal_uInt16 nItemVersion ) const
92 {
93 	// CntContentTypeItem used to be derived from CntStringItem, so take that
94 	// into account:
95 	UniString aValue;
96 	readUnicodeString(rStream, aValue, nItemVersion >= 1);
97 	sal_uInt32 nMagic = 0;
98 	rStream >> nMagic;
99 	if (nMagic == CNTSTRINGITEM_STREAM_MAGIC)
100 	{
101 		sal_Bool bEncrypted = sal_False;
102 		rStream >> bEncrypted;
103 		DBG_ASSERT(!bEncrypted,
104 				   "CntContentTypeItem::Create() reads encrypted data");
105 	}
106 	else
107 		rStream.SeekRel(CNTSTRINGITEM_STREAM_SEEKREL);
108 
109 	return new CntContentTypeItem(Which(), aValue);
110 }
111 
112 //----------------------------------------------------------------------------
113 // virtual
114 SvStream & CntContentTypeItem::Store(SvStream & rStream, sal_uInt16) const
115 {
116 	// CntContentTypeItem used to be derived from CntStringItem, so take that
117 	// into account:
118 	writeUnicodeString(rStream, GetValue());
119 	rStream << CNTSTRINGITEM_STREAM_MAGIC << sal_Bool(sal_False);
120 	return rStream;
121 }
122 
123 //----------------------------------------------------------------------------
124 // virtual
125 int CntContentTypeItem::operator==( const SfxPoolItem& rOrig ) const
126 {
127 	const CntContentTypeItem& rOther = (const CntContentTypeItem&)rOrig;
128 
129 	if ( ( _eType != CONTENT_TYPE_NOT_INIT ) &&
130 		 ( rOther._eType != CONTENT_TYPE_NOT_INIT ) )
131 		return _eType == rOther._eType;
132 	else
133 		return CntUnencodedStringItem::operator==( rOther );
134 }
135 
136 //----------------------------------------------------------------------------
137 // virtual
138 SfxPoolItem* CntContentTypeItem::Clone( SfxItemPool* /* pPool */ ) const
139 {
140 	return new CntContentTypeItem( *this );
141 }
142 
143 //----------------------------------------------------------------------------
144 void CntContentTypeItem::SetValue( const XubString& rNewVal )
145 {
146 	// De-initialize enum type and presentation.
147 	_eType = CONTENT_TYPE_NOT_INIT;
148 	_aPresentation.Erase();
149 
150 	CntUnencodedStringItem::SetValue( rNewVal );
151 }
152 
153 //----------------------------------------------------------------------------
154 void CntContentTypeItem::SetPresentation( const XubString& rNewVal )
155 {
156 	_aPresentation = rNewVal;
157 }
158 
159 //----------------------------------------------------------------------------
160 int CntContentTypeItem::Compare( const SfxPoolItem &rWith, const IntlWrapper& rIntlWrapper ) const
161 {
162 	String aOwnText, aWithText;
163 	GetPresentation( SFX_ITEM_PRESENTATION_NAMELESS,
164                      SFX_MAPUNIT_APPFONT, SFX_MAPUNIT_APPFONT, aOwnText, &rIntlWrapper );
165 	rWith.GetPresentation( SFX_ITEM_PRESENTATION_NAMELESS,
166                            SFX_MAPUNIT_APPFONT, SFX_MAPUNIT_APPFONT, aWithText, &rIntlWrapper );
167     return rIntlWrapper.getCollator()->compareString( aOwnText, aWithText );
168 }
169 
170 //----------------------------------------------------------------------------
171 SfxItemPresentation CntContentTypeItem::GetPresentation(
172 	SfxItemPresentation ePres,
173 	SfxMapUnit          eCoreMetric,
174 	SfxMapUnit          ePresMetric,
175 	XubString         & rText,
176 	const IntlWrapper * pIntlWrapper) const
177 {
178 	if (_aPresentation.Len() == 0)
179 	{
180         DBG_ASSERT(pIntlWrapper,
181                    "CntContentTypeItem::GetPresentation(): No IntlWrapper");
182         if (pIntlWrapper)
183 			SAL_CONST_CAST(CntContentTypeItem *, this)->_aPresentation
184 			 = INetContentTypes::GetPresentation(GetEnumValue(),
185                                                  pIntlWrapper->
186                                                   getLocale());
187 	}
188 	if (_aPresentation.Len() > 0)
189 	{
190 		rText = _aPresentation;
191 		return SFX_ITEM_PRESENTATION_COMPLETE;
192 	}
193 	else
194 		return CntUnencodedStringItem::GetPresentation(ePres, eCoreMetric,
195 													   ePresMetric, rText,
196                                                        pIntlWrapper);
197 }
198 
199 //----------------------------------------------------------------------------
200 INetContentType CntContentTypeItem::GetEnumValue() const
201 {
202 	if ( _eType == CONTENT_TYPE_NOT_INIT )
203 	{
204 		// Not yet initialized... Get enum value for string content type.
205 
206 		CntContentTypeItem* pVarThis = SAL_CONST_CAST( CntContentTypeItem*, this );
207 
208 		pVarThis->_eType = INetContentTypes::GetContentType( GetValue() );
209 	}
210 
211 	return _eType;
212 }
213 
214 //----------------------------------------------------------------------------
215 void CntContentTypeItem::SetValue( const INetContentType eType )
216 {
217 	SetValue( INetContentTypes::GetContentType( eType ) );
218 
219 	// Note: SetValue( const String& ....) resets _eType. Set new enum value
220 	//       after(!) calling it.
221 	_eType = eType;
222 }
223 
224 //----------------------------------------------------------------------------
225 // virtual
226 sal_Bool CntContentTypeItem::QueryValue( com::sun::star::uno::Any& rVal,sal_uInt8 ) const
227 {
228 	rVal <<= rtl::OUString(GetValue());
229 	return true;
230 }
231 
232 //----------------------------------------------------------------------------
233 // virtual
234 sal_Bool CntContentTypeItem::PutValue( const com::sun::star::uno::Any& rVal,sal_uInt8 )
235 {
236 	rtl::OUString aValue;
237 	if ( rVal >>= aValue )
238 	{
239 		// SetValue with an empty string resets the item; so call that
240 		// function when PutValue is called with an empty string
241 		if (aValue.getLength() == 0)
242 			SetValue(aValue);
243 		else
244 			SetValue(
245 				INetContentTypes::RegisterContentType(aValue, UniString()));
246 		return true;
247 	}
248 
249 	DBG_ERROR( "CntContentTypeItem::PutValue - Wrong type!" );
250 	return false;
251 }
252