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