Line data Source code
1 : /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 : /*
3 : * This file is part of the LibreOffice project.
4 : *
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/.
8 : *
9 : * This file incorporates work covered by the following license notice:
10 : *
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 .
18 : */
19 :
20 : #include <com/sun/star/uno/Any.hxx>
21 :
22 : #include <unotools/intlwrapper.hxx>
23 : #include <tools/stream.hxx>
24 : #include <svl/ctypeitm.hxx>
25 :
26 : // The following defines are copied from chaos/source/items/cstritem.cxx:
27 : #define CNTSTRINGITEM_STREAM_MAGIC ( (sal_uInt32)0xfefefefe )
28 : #define CNTSTRINGITEM_STREAM_SEEKREL (-( (long)( sizeof( sal_uInt32 ) ) ) )
29 :
30 :
31 : // class CntContentTypeItem Implementation.
32 :
33 :
34 0 : TYPEINIT1_AUTOFACTORY( CntContentTypeItem, CntUnencodedStringItem );
35 :
36 : #define CONTENT_TYPE_NOT_INIT ( (INetContentType)-1 )
37 :
38 0 : CntContentTypeItem::CntContentTypeItem()
39 : : CntUnencodedStringItem(),
40 0 : _eType( CONTENT_TYPE_NOT_INIT )
41 : {
42 0 : }
43 :
44 0 : CntContentTypeItem::CntContentTypeItem( sal_uInt16 which, const OUString& rType )
45 : : CntUnencodedStringItem( which, rType ),
46 0 : _eType( CONTENT_TYPE_NOT_INIT )
47 : {
48 0 : }
49 :
50 0 : CntContentTypeItem::CntContentTypeItem( const CntContentTypeItem& rOrig )
51 : : CntUnencodedStringItem( rOrig ),
52 : _eType( rOrig._eType ),
53 0 : _aPresentation( rOrig._aPresentation )
54 : {
55 0 : }
56 :
57 : // virtual
58 0 : sal_uInt16 CntContentTypeItem::GetVersion(sal_uInt16) const
59 : {
60 0 : return 1; // because it uses SfxPoolItem::read/writeUnicodeString()
61 : }
62 :
63 : // virtual
64 0 : SfxPoolItem* CntContentTypeItem::Create( SvStream& rStream,
65 : sal_uInt16 nItemVersion ) const
66 : {
67 : // CntContentTypeItem used to be derived from CntStringItem, so take that
68 : // into account:
69 0 : OUString aValue = readUnicodeString(rStream, nItemVersion >= 1);
70 0 : sal_uInt32 nMagic = 0;
71 0 : rStream.ReadUInt32( nMagic );
72 0 : if (nMagic == CNTSTRINGITEM_STREAM_MAGIC)
73 : {
74 0 : bool bEncrypted = false;
75 0 : rStream.ReadCharAsBool( bEncrypted );
76 : DBG_ASSERT(!bEncrypted,
77 : "CntContentTypeItem::Create() reads encrypted data");
78 : }
79 : else
80 0 : rStream.SeekRel(CNTSTRINGITEM_STREAM_SEEKREL);
81 :
82 0 : return new CntContentTypeItem(Which(), aValue);
83 : }
84 :
85 : // virtual
86 0 : SvStream & CntContentTypeItem::Store(SvStream & rStream, sal_uInt16) const
87 : {
88 : // CntContentTypeItem used to be derived from CntStringItem, so take that
89 : // into account:
90 0 : writeUnicodeString(rStream, GetValue());
91 0 : rStream.WriteUInt32( CNTSTRINGITEM_STREAM_MAGIC ).WriteUChar( false );
92 0 : return rStream;
93 : }
94 :
95 : // virtual
96 0 : bool CntContentTypeItem::operator==( const SfxPoolItem& rOrig ) const
97 : {
98 0 : const CntContentTypeItem& rOther = (const CntContentTypeItem&)rOrig;
99 :
100 0 : if ( ( _eType != CONTENT_TYPE_NOT_INIT ) &&
101 0 : ( rOther._eType != CONTENT_TYPE_NOT_INIT ) )
102 0 : return _eType == rOther._eType;
103 : else
104 0 : return CntUnencodedStringItem::operator==( rOther );
105 : }
106 :
107 : // virtual
108 0 : SfxPoolItem* CntContentTypeItem::Clone( SfxItemPool* /* pPool */ ) const
109 : {
110 0 : return new CntContentTypeItem( *this );
111 : }
112 :
113 0 : void CntContentTypeItem::SetValue( const OUString& rNewVal )
114 : {
115 : // De-initialize enum type and presentation.
116 0 : _eType = CONTENT_TYPE_NOT_INIT;
117 0 : _aPresentation = OUString();
118 :
119 0 : CntUnencodedStringItem::SetValue( rNewVal );
120 0 : }
121 :
122 0 : int CntContentTypeItem::Compare( const SfxPoolItem &rWith, const IntlWrapper& rIntlWrapper ) const
123 : {
124 0 : OUString aOwnText, aWithText;
125 : GetPresentation( SFX_ITEM_PRESENTATION_NAMELESS,
126 0 : SFX_MAPUNIT_APPFONT, SFX_MAPUNIT_APPFONT, aOwnText, &rIntlWrapper );
127 : rWith.GetPresentation( SFX_ITEM_PRESENTATION_NAMELESS,
128 0 : SFX_MAPUNIT_APPFONT, SFX_MAPUNIT_APPFONT, aWithText, &rIntlWrapper );
129 0 : return rIntlWrapper.getCollator()->compareString( aOwnText, aWithText );
130 : }
131 :
132 0 : SfxItemPresentation CntContentTypeItem::GetPresentation(
133 : SfxItemPresentation ePres,
134 : SfxMapUnit eCoreMetric,
135 : SfxMapUnit ePresMetric,
136 : OUString & rText,
137 : const IntlWrapper * pIntlWrapper) const
138 : {
139 0 : if (_aPresentation.isEmpty())
140 : {
141 : DBG_ASSERT(pIntlWrapper,
142 : "CntContentTypeItem::GetPresentation(): No IntlWrapper");
143 0 : if (pIntlWrapper)
144 : (const_cast< CntContentTypeItem * >(this))->_aPresentation
145 0 : = INetContentTypes::GetPresentation(GetEnumValue(),
146 0 : pIntlWrapper->getLanguageTag());
147 : }
148 0 : if (!_aPresentation.isEmpty())
149 : {
150 0 : rText = _aPresentation;
151 0 : return SFX_ITEM_PRESENTATION_COMPLETE;
152 : }
153 : else
154 : return CntUnencodedStringItem::GetPresentation(ePres, eCoreMetric,
155 : ePresMetric, rText,
156 0 : pIntlWrapper);
157 : }
158 :
159 0 : INetContentType CntContentTypeItem::GetEnumValue() const
160 : {
161 0 : if ( _eType == CONTENT_TYPE_NOT_INIT )
162 : {
163 : // Not yet initialized... Get enum value for string content type.
164 :
165 0 : CntContentTypeItem* pVarThis = (const_cast< CntContentTypeItem* >(this));
166 :
167 0 : pVarThis->_eType = INetContentTypes::GetContentType( GetValue() );
168 : }
169 :
170 0 : return _eType;
171 : }
172 :
173 0 : void CntContentTypeItem::SetValue( const INetContentType eType )
174 : {
175 0 : SetValue( INetContentTypes::GetContentType( eType ) );
176 :
177 : // Note: SetValue( const String& ....) resets _eType. Set new enum value
178 : // after(!) calling it.
179 0 : _eType = eType;
180 0 : }
181 :
182 : // virtual
183 0 : bool CntContentTypeItem::QueryValue( com::sun::star::uno::Any& rVal, sal_uInt8) const
184 : {
185 0 : rVal <<= OUString(GetValue());
186 0 : return true;
187 : }
188 :
189 : // virtual
190 0 : bool CntContentTypeItem::PutValue( const com::sun::star::uno::Any& rVal, sal_uInt8)
191 : {
192 0 : OUString aValue;
193 0 : if ( rVal >>= aValue )
194 : {
195 : // SetValue with an empty string resets the item; so call that
196 : // function when PutValue is called with an empty string
197 0 : if (aValue.isEmpty())
198 0 : SetValue(aValue);
199 : else
200 : SetValue(
201 0 : INetContentTypes::RegisterContentType(aValue, OUString()));
202 0 : return true;
203 : }
204 :
205 : OSL_FAIL( "CntContentTypeItem::PutValue - Wrong type!" );
206 0 : return false;
207 : }
208 :
209 : /* vim:set shiftwidth=4 softtabstop=4 expandtab: */
|