xref: /trunk/main/svl/source/items/ctypeitm.cxx (revision 40df464e)
1 /**************************************************************
2  *
3  * Licensed to the Apache Software Foundation (ASF) under one
4  * or more contributor license agreements.  See the NOTICE file
5  * distributed with this work for additional information
6  * regarding copyright ownership.  The ASF licenses this file
7  * to you under the Apache License, Version 2.0 (the
8  * "License"); you may not use this file except in compliance
9  * with the License.  You may obtain a copy of the License at
10  *
11  *   http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing,
14  * software distributed under the License is distributed on an
15  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16  * KIND, either express or implied.  See the License for the
17  * specific language governing permissions and limitations
18  * under the License.
19  *
20  *************************************************************/
21 
22 
23 
24 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_svl.hxx"
26 #include <com/sun/star/uno/Any.hxx>
27 
28 #include <unotools/intlwrapper.hxx>
29 #include <tools/stream.hxx>
30 #include <svl/ctypeitm.hxx>
31 
32 //============================================================================
33 // The following defines are copied from chaos/source/items/cstritem.cxx:
34 #define CNTSTRINGITEM_STREAM_MAGIC   ( (sal_uInt32)0xfefefefe )
35 #define CNTSTRINGITEM_STREAM_SEEKREL (-( (long)( sizeof( sal_uInt32 ) ) ) )
36 
37 //============================================================================
38 //
39 // class CntContentTypeItem Implementation.
40 //
41 //============================================================================
42 
43 TYPEINIT1_AUTOFACTORY( CntContentTypeItem, CntUnencodedStringItem );
44 
45 #define CONTENT_TYPE_NOT_INIT ( (INetContentType)-1 )
46 
47 //----------------------------------------------------------------------------
CntContentTypeItem()48 CntContentTypeItem::CntContentTypeItem()
49 : CntUnencodedStringItem(),
50   _eType( CONTENT_TYPE_NOT_INIT )
51 {
52 }
53 
54 //----------------------------------------------------------------------------
CntContentTypeItem(sal_uInt16 which,const XubString & rType)55 CntContentTypeItem::CntContentTypeItem(	sal_uInt16 which, const XubString& rType )
56 : CntUnencodedStringItem( which, rType ),
57   _eType( CONTENT_TYPE_NOT_INIT )
58 {
59 }
60 
61 //----------------------------------------------------------------------------
CntContentTypeItem(sal_uInt16 which,const INetContentType eType)62 CntContentTypeItem::CntContentTypeItem(	sal_uInt16 which,
63 										const INetContentType eType )
64 : CntUnencodedStringItem( which, INetContentTypes::GetContentType( eType ) ),
65   _eType( eType )
66 {
67 }
68 
69 //----------------------------------------------------------------------------
CntContentTypeItem(const CntContentTypeItem & rOrig)70 CntContentTypeItem::CntContentTypeItem( const CntContentTypeItem& rOrig )
71 : CntUnencodedStringItem( rOrig ),
72   _eType( rOrig._eType ),
73   _aPresentation( rOrig._aPresentation )
74 {
75 }
76 
77 //============================================================================
78 // virtual
GetVersion(sal_uInt16) const79 sal_uInt16 CntContentTypeItem::GetVersion(sal_uInt16) const
80 {
81 	return 1; // because it uses SfxPoolItem::read/writeUnicodeString()
82 }
83 
84 //----------------------------------------------------------------------------
85 // virtual
Create(SvStream & rStream,sal_uInt16 nItemVersion) const86 SfxPoolItem* CntContentTypeItem::Create( SvStream& rStream,
87 										 sal_uInt16 nItemVersion ) const
88 {
89 	// CntContentTypeItem used to be derived from CntStringItem, so take that
90 	// into account:
91 	UniString aValue;
92 	readUnicodeString(rStream, aValue, nItemVersion >= 1);
93 	sal_uInt32 nMagic = 0;
94 	rStream >> nMagic;
95 	if (nMagic == CNTSTRINGITEM_STREAM_MAGIC)
96 	{
97 		sal_Bool bEncrypted = sal_False;
98 		rStream >> bEncrypted;
99 		DBG_ASSERT(!bEncrypted,
100 				   "CntContentTypeItem::Create() reads encrypted data");
101 	}
102 	else
103 		rStream.SeekRel(CNTSTRINGITEM_STREAM_SEEKREL);
104 
105 	return new CntContentTypeItem(Which(), aValue);
106 }
107 
108 //----------------------------------------------------------------------------
109 // virtual
Store(SvStream & rStream,sal_uInt16) const110 SvStream & CntContentTypeItem::Store(SvStream & rStream, sal_uInt16) const
111 {
112 	// CntContentTypeItem used to be derived from CntStringItem, so take that
113 	// into account:
114 	writeUnicodeString(rStream, GetValue());
115 	rStream << CNTSTRINGITEM_STREAM_MAGIC << sal_Bool(sal_False);
116 	return rStream;
117 }
118 
119 //----------------------------------------------------------------------------
120 // virtual
operator ==(const SfxPoolItem & rOrig) const121 int CntContentTypeItem::operator==( const SfxPoolItem& rOrig ) const
122 {
123 	const CntContentTypeItem& rOther = (const CntContentTypeItem&)rOrig;
124 
125 	if ( ( _eType != CONTENT_TYPE_NOT_INIT ) &&
126 		 ( rOther._eType != CONTENT_TYPE_NOT_INIT ) )
127 		return _eType == rOther._eType;
128 	else
129 		return CntUnencodedStringItem::operator==( rOther );
130 }
131 
132 //----------------------------------------------------------------------------
133 // virtual
Clone(SfxItemPool *) const134 SfxPoolItem* CntContentTypeItem::Clone( SfxItemPool* /* pPool */ ) const
135 {
136 	return new CntContentTypeItem( *this );
137 }
138 
139 //----------------------------------------------------------------------------
SetValue(const XubString & rNewVal)140 void CntContentTypeItem::SetValue( const XubString& rNewVal )
141 {
142 	// De-initialize enum type and presentation.
143 	_eType = CONTENT_TYPE_NOT_INIT;
144 	_aPresentation.Erase();
145 
146 	CntUnencodedStringItem::SetValue( rNewVal );
147 }
148 
149 //----------------------------------------------------------------------------
SetPresentation(const XubString & rNewVal)150 void CntContentTypeItem::SetPresentation( const XubString& rNewVal )
151 {
152 	_aPresentation = rNewVal;
153 }
154 
155 //----------------------------------------------------------------------------
Compare(const SfxPoolItem & rWith,const IntlWrapper & rIntlWrapper) const156 int CntContentTypeItem::Compare( const SfxPoolItem &rWith, const IntlWrapper& rIntlWrapper ) const
157 {
158 	String aOwnText, aWithText;
159 	GetPresentation( SFX_ITEM_PRESENTATION_NAMELESS,
160                      SFX_MAPUNIT_APPFONT, SFX_MAPUNIT_APPFONT, aOwnText, &rIntlWrapper );
161 	rWith.GetPresentation( SFX_ITEM_PRESENTATION_NAMELESS,
162                            SFX_MAPUNIT_APPFONT, SFX_MAPUNIT_APPFONT, aWithText, &rIntlWrapper );
163     return rIntlWrapper.getCollator()->compareString( aOwnText, aWithText );
164 }
165 
166 //----------------------------------------------------------------------------
GetPresentation(SfxItemPresentation ePres,SfxMapUnit eCoreMetric,SfxMapUnit ePresMetric,XubString & rText,const IntlWrapper * pIntlWrapper) const167 SfxItemPresentation CntContentTypeItem::GetPresentation(
168 	SfxItemPresentation ePres,
169 	SfxMapUnit          eCoreMetric,
170 	SfxMapUnit          ePresMetric,
171 	XubString         & rText,
172 	const IntlWrapper * pIntlWrapper) const
173 {
174 	if (_aPresentation.Len() == 0)
175 	{
176         DBG_ASSERT(pIntlWrapper,
177                    "CntContentTypeItem::GetPresentation(): No IntlWrapper");
178         if (pIntlWrapper)
179 			SAL_CONST_CAST(CntContentTypeItem *, this)->_aPresentation
180 			 = INetContentTypes::GetPresentation(GetEnumValue(),
181                                                  pIntlWrapper->
182                                                   getLocale());
183 	}
184 	if (_aPresentation.Len() > 0)
185 	{
186 		rText = _aPresentation;
187 		return SFX_ITEM_PRESENTATION_COMPLETE;
188 	}
189 	else
190 		return CntUnencodedStringItem::GetPresentation(ePres, eCoreMetric,
191 													   ePresMetric, rText,
192                                                        pIntlWrapper);
193 }
194 
195 //----------------------------------------------------------------------------
GetEnumValue() const196 INetContentType CntContentTypeItem::GetEnumValue() const
197 {
198 	if ( _eType == CONTENT_TYPE_NOT_INIT )
199 	{
200 		// Not yet initialized... Get enum value for string content type.
201 
202 		CntContentTypeItem* pVarThis = SAL_CONST_CAST( CntContentTypeItem*, this );
203 
204 		pVarThis->_eType = INetContentTypes::GetContentType( GetValue() );
205 	}
206 
207 	return _eType;
208 }
209 
210 //----------------------------------------------------------------------------
SetValue(const INetContentType eType)211 void CntContentTypeItem::SetValue( const INetContentType eType )
212 {
213 	SetValue( INetContentTypes::GetContentType( eType ) );
214 
215 	// Note: SetValue( const String& ....) resets _eType. Set new enum value
216 	//       after(!) calling it.
217 	_eType = eType;
218 }
219 
220 //----------------------------------------------------------------------------
221 // virtual
QueryValue(com::sun::star::uno::Any & rVal,sal_uInt8) const222 sal_Bool CntContentTypeItem::QueryValue( com::sun::star::uno::Any& rVal,sal_uInt8 ) const
223 {
224 	rVal <<= rtl::OUString(GetValue());
225 	return true;
226 }
227 
228 //----------------------------------------------------------------------------
229 // virtual
PutValue(const com::sun::star::uno::Any & rVal,sal_uInt8)230 sal_Bool CntContentTypeItem::PutValue( const com::sun::star::uno::Any& rVal,sal_uInt8 )
231 {
232 	rtl::OUString aValue;
233 	if ( rVal >>= aValue )
234 	{
235 		// SetValue with an empty string resets the item; so call that
236 		// function when PutValue is called with an empty string
237 		if (aValue.getLength() == 0)
238 			SetValue(aValue);
239 		else
240 			SetValue(
241 				INetContentTypes::RegisterContentType(aValue, UniString()));
242 		return true;
243 	}
244 
245 	DBG_ERROR( "CntContentTypeItem::PutValue - Wrong type!" );
246 	return false;
247 }
248