1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
9 * This file incorporates work covered by the following license notice:
11 * Licensed to the Apache Software Foundation (ASF) under one or more
12 * contributor license agreements. See the NOTICE file distributed
13 * with this work for additional information regarding copyright
14 * ownership. The ASF licenses this file to you under the Apache
15 * License, Version 2.0 (the "License"); you may not use this file
16 * except in compliance with the License. You may obtain a copy of
17 * the License at http://www.apache.org/licenses/LICENSE-2.0 .
20 #include <com/sun/star/uno/Any.hxx>
22 #include <osl/diagnose.h>
23 #include <unotools/intlwrapper.hxx>
24 #include <tools/stream.hxx>
25 #include <svl/ctypeitm.hxx>
26 #include <stringio.hxx>
28 // The following defines are copied from chaos/source/items/cstritem.cxx:
29 #define CNTSTRINGITEM_STREAM_MAGIC ( (sal_uInt32)0xfefefefe )
30 #define CNTSTRINGITEM_STREAM_SEEKREL (-( (long)( sizeof( sal_uInt32 ) ) ) )
33 // class CntContentTypeItem Implementation.
36 TYPEINIT1_AUTOFACTORY( CntContentTypeItem
, CntUnencodedStringItem
);
38 #define CONTENT_TYPE_NOT_INIT ( (INetContentType)-1 )
40 CntContentTypeItem::CntContentTypeItem()
41 : CntUnencodedStringItem(),
42 _eType( CONTENT_TYPE_NOT_INIT
)
46 CntContentTypeItem::CntContentTypeItem( sal_uInt16 which
, const OUString
& rType
)
47 : CntUnencodedStringItem( which
, rType
),
48 _eType( CONTENT_TYPE_NOT_INIT
)
52 CntContentTypeItem::CntContentTypeItem( const CntContentTypeItem
& rOrig
)
53 : CntUnencodedStringItem( rOrig
),
54 _eType( rOrig
._eType
),
55 _aPresentation( rOrig
._aPresentation
)
60 sal_uInt16
CntContentTypeItem::GetVersion(sal_uInt16
) const
62 return 1; // because it uses SfxPoolItem::read/writeUnicodeString()
66 SfxPoolItem
* CntContentTypeItem::Create( SvStream
& rStream
,
67 sal_uInt16 nItemVersion
) const
69 // CntContentTypeItem used to be derived from CntStringItem, so take that
71 OUString aValue
= readUnicodeString(rStream
, nItemVersion
>= 1);
72 sal_uInt32 nMagic
= 0;
73 rStream
.ReadUInt32( nMagic
);
74 if (nMagic
== CNTSTRINGITEM_STREAM_MAGIC
)
76 bool bEncrypted
= false;
77 rStream
.ReadCharAsBool( bEncrypted
);
78 DBG_ASSERT(!bEncrypted
,
79 "CntContentTypeItem::Create() reads encrypted data");
82 rStream
.SeekRel(CNTSTRINGITEM_STREAM_SEEKREL
);
84 return new CntContentTypeItem(Which(), aValue
);
88 SvStream
& CntContentTypeItem::Store(SvStream
& rStream
, sal_uInt16
) const
90 // CntContentTypeItem used to be derived from CntStringItem, so take that
92 writeUnicodeString(rStream
, GetValue());
93 rStream
.WriteUInt32( CNTSTRINGITEM_STREAM_MAGIC
).WriteBool( false );
98 bool CntContentTypeItem::operator==( const SfxPoolItem
& rOrig
) const
100 const CntContentTypeItem
& rOther
= static_cast<const CntContentTypeItem
&>(rOrig
);
102 if ( ( _eType
!= CONTENT_TYPE_NOT_INIT
) &&
103 ( rOther
._eType
!= CONTENT_TYPE_NOT_INIT
) )
104 return _eType
== rOther
._eType
;
106 return CntUnencodedStringItem::operator==( rOther
);
110 SfxPoolItem
* CntContentTypeItem::Clone( SfxItemPool
* /* pPool */ ) const
112 return new CntContentTypeItem( *this );
115 void CntContentTypeItem::SetValue( const OUString
& rNewVal
)
117 // De-initialize enum type and presentation.
118 _eType
= CONTENT_TYPE_NOT_INIT
;
119 _aPresentation
.clear();
121 CntUnencodedStringItem::SetValue( rNewVal
);
124 int CntContentTypeItem::Compare( const SfxPoolItem
&rWith
, const IntlWrapper
& rIntlWrapper
) const
126 OUString aOwnText
, aWithText
;
127 GetPresentation( SFX_ITEM_PRESENTATION_NAMELESS
,
128 SFX_MAPUNIT_APPFONT
, SFX_MAPUNIT_APPFONT
, aOwnText
, &rIntlWrapper
);
129 rWith
.GetPresentation( SFX_ITEM_PRESENTATION_NAMELESS
,
130 SFX_MAPUNIT_APPFONT
, SFX_MAPUNIT_APPFONT
, aWithText
, &rIntlWrapper
);
131 return rIntlWrapper
.getCollator()->compareString( aOwnText
, aWithText
);
134 bool CntContentTypeItem::GetPresentation(
135 SfxItemPresentation ePres
,
136 SfxMapUnit eCoreMetric
,
137 SfxMapUnit ePresMetric
,
139 const IntlWrapper
* pIntlWrapper
) const
141 if (_aPresentation
.isEmpty())
143 DBG_ASSERT(pIntlWrapper
,
144 "CntContentTypeItem::GetPresentation(): No IntlWrapper");
146 (const_cast< CntContentTypeItem
* >(this))->_aPresentation
147 = INetContentTypes::GetPresentation(GetEnumValue(),
148 pIntlWrapper
->getLanguageTag());
150 if (!_aPresentation
.isEmpty())
152 rText
= _aPresentation
;
156 return CntUnencodedStringItem::GetPresentation(ePres
, eCoreMetric
,
161 INetContentType
CntContentTypeItem::GetEnumValue() const
163 if ( _eType
== CONTENT_TYPE_NOT_INIT
)
165 // Not yet initialized... Get enum value for string content type.
167 CntContentTypeItem
* pVarThis
= (const_cast< CntContentTypeItem
* >(this));
169 pVarThis
->_eType
= INetContentTypes::GetContentType( GetValue() );
175 void CntContentTypeItem::SetValue( const INetContentType eType
)
177 SetValue( INetContentTypes::GetContentType( eType
) );
179 // Note: SetValue( const String& ....) resets _eType. Set new enum value
180 // after(!) calling it.
185 bool CntContentTypeItem::QueryValue( com::sun::star::uno::Any
& rVal
, sal_uInt8
) const
187 rVal
<<= OUString(GetValue());
192 bool CntContentTypeItem::PutValue( const com::sun::star::uno::Any
& rVal
, sal_uInt8
)
195 if ( rVal
>>= aValue
)
197 // SetValue with an empty string resets the item; so call that
198 // function when PutValue is called with an empty string
199 if (aValue
.isEmpty())
203 INetContentTypes::RegisterContentType(aValue
, OUString()));
207 OSL_FAIL( "CntContentTypeItem::PutValue - Wrong type!" );
211 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */