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 <svtools/htmlkywd.hxx>
22 #include <rtl/tencinfo.h>
24 #include <unotools/configmgr.hxx>
25 #include <svl/urihelper.hxx>
26 #include <tools/datetime.hxx>
28 #include <sfx2/frmhtmlw.hxx>
29 #include <sfx2/evntconf.hxx>
30 #include <sfx2/frame.hxx>
31 #include <sfx2/app.hxx>
32 #include <sfx2/viewfrm.hxx>
33 #include <sfx2/docfile.hxx>
34 #include <sfx2/sfxresid.hxx>
35 #include <sfx2/objsh.hxx>
36 #include <sfx2/sfx.hrc>
39 #include <comphelper/processfactory.hxx>
40 #include <comphelper/string.hxx>
42 #include <com/sun/star/script/Converter.hpp>
43 #include <com/sun/star/document/XDocumentProperties.hpp>
45 #include <rtl/bootstrap.hxx>
46 #include <rtl/strbuf.hxx>
47 #include <sax/tools/converter.hxx>
49 using namespace ::com::sun::star
;
51 static sal_Char
const sHTML_SC_yes
[] = "YES";
52 static sal_Char
const sHTML_SC_no
[] = "NO";
53 static sal_Char
const sHTML_MIME_text_html
[] = "text/html; charset=";
55 void SfxFrameHTMLWriter::OutMeta( SvStream
& rStrm
,
56 const sal_Char
*pIndent
,
57 const OUString
& rName
,
58 const OUString
& rContent
,
60 rtl_TextEncoding eDestEnc
,
61 OUString
*pNonConvertableChars
)
63 rStrm
.WriteCharPtr( SAL_NEWLINE_STRING
);
65 rStrm
.WriteCharPtr( pIndent
);
68 sOut
.append('<').append(OOO_STRING_SVTOOLS_HTML_meta
).append(' ')
69 .append(bHTTPEquiv
? OOO_STRING_SVTOOLS_HTML_O_httpequiv
: OOO_STRING_SVTOOLS_HTML_O_name
).append("=\"");
70 rStrm
.WriteCharPtr( sOut
.makeStringAndClear().getStr() );
72 HTMLOutFuncs::Out_String( rStrm
, rName
, eDestEnc
, pNonConvertableChars
);
74 sOut
.append("\" ").append(OOO_STRING_SVTOOLS_HTML_O_content
).append("=\"");
75 rStrm
.WriteCharPtr( sOut
.makeStringAndClear().getStr() );
77 HTMLOutFuncs::Out_String( rStrm
, rContent
, eDestEnc
, pNonConvertableChars
).WriteCharPtr( "\"/>" );
80 void SfxFrameHTMLWriter::Out_DocInfo( SvStream
& rStrm
, const OUString
& rBaseURL
,
81 const uno::Reference
<document::XDocumentProperties
> & i_xDocProps
,
82 const sal_Char
*pIndent
,
83 rtl_TextEncoding eDestEnc
,
84 OUString
*pNonConvertableChars
)
86 const sal_Char
*pCharSet
=
87 rtl_getBestMimeCharsetFromTextEncoding( eDestEnc
);
91 OUString
aContentType(sHTML_MIME_text_html
);
92 aContentType
+= OUString(pCharSet
, strlen(pCharSet
), RTL_TEXTENCODING_UTF8
);
93 OutMeta( rStrm
, pIndent
, OOO_STRING_SVTOOLS_HTML_META_content_type
, aContentType
, true,
94 eDestEnc
, pNonConvertableChars
);
97 // Titel (auch wenn er leer ist)
98 rStrm
.WriteCharPtr( SAL_NEWLINE_STRING
);
100 rStrm
.WriteCharPtr( pIndent
);
101 HTMLOutFuncs::Out_AsciiTag( rStrm
, OOO_STRING_SVTOOLS_HTML_title
);
102 if( i_xDocProps
.is() )
104 const OUString
& rTitle
= i_xDocProps
->getTitle();
105 if( !rTitle
.isEmpty() )
106 HTMLOutFuncs::Out_String( rStrm
, rTitle
, eDestEnc
, pNonConvertableChars
);
108 HTMLOutFuncs::Out_AsciiTag( rStrm
, OOO_STRING_SVTOOLS_HTML_title
, false );
111 if( i_xDocProps
.is() )
113 const OUString
& rTarget
= i_xDocProps
->getDefaultTarget();
114 if( !rTarget
.isEmpty() )
116 rStrm
.WriteCharPtr( SAL_NEWLINE_STRING
);
118 rStrm
.WriteCharPtr( pIndent
);
121 sOut
.append('<').append(OOO_STRING_SVTOOLS_HTML_base
).append(' ')
122 .append(OOO_STRING_SVTOOLS_HTML_O_target
).append("=\"");
123 rStrm
.WriteCharPtr( sOut
.makeStringAndClear().getStr() );
124 HTMLOutFuncs::Out_String( rStrm
, rTarget
, eDestEnc
, pNonConvertableChars
)
125 .WriteCharPtr( "\">" );
130 OUString
sGenerator( SfxResId(STR_HTML_GENERATOR
).toString() );
131 OUString
os( "$_OS" );
132 ::rtl::Bootstrap::expandMacros(os
);
133 sGenerator
= sGenerator
.replaceFirst( "%1", os
);
134 OutMeta( rStrm
, pIndent
, OOO_STRING_SVTOOLS_HTML_META_generator
, sGenerator
, false, eDestEnc
, pNonConvertableChars
);
136 if( i_xDocProps
.is() )
139 if( (i_xDocProps
->getAutoloadSecs() != 0) ||
140 !i_xDocProps
->getAutoloadURL().isEmpty() )
142 OUString sContent
= OUString::number(
143 i_xDocProps
->getAutoloadSecs() );
145 const OUString
&rReloadURL
= i_xDocProps
->getAutoloadURL();
146 if( !rReloadURL
.isEmpty() )
149 sContent
+= URIHelper::simpleNormalizedMakeRelative(
150 rBaseURL
, rReloadURL
);
153 OutMeta( rStrm
, pIndent
, OOO_STRING_SVTOOLS_HTML_META_refresh
, sContent
, true,
154 eDestEnc
, pNonConvertableChars
);
158 const OUString
& rAuthor
= i_xDocProps
->getAuthor();
159 if( !rAuthor
.isEmpty() )
160 OutMeta( rStrm
, pIndent
, OOO_STRING_SVTOOLS_HTML_META_author
, rAuthor
, false,
161 eDestEnc
, pNonConvertableChars
);
164 ::util::DateTime uDT
= i_xDocProps
->getCreationDate();
165 OUStringBuffer aBuffer
;
166 ::sax::Converter::convertTimeOrDateTime(aBuffer
, uDT
, 0);
168 OutMeta( rStrm
, pIndent
, OOO_STRING_SVTOOLS_HTML_META_created
, aBuffer
.makeStringAndClear(), false,
169 eDestEnc
, pNonConvertableChars
);
172 const OUString
& rChangedBy
= i_xDocProps
->getModifiedBy();
173 if( !rChangedBy
.isEmpty() )
174 OutMeta( rStrm
, pIndent
, OOO_STRING_SVTOOLS_HTML_META_changedby
, rChangedBy
, false,
175 eDestEnc
, pNonConvertableChars
);
178 uDT
= i_xDocProps
->getModificationDate();
179 ::sax::Converter::convertTimeOrDateTime(aBuffer
, uDT
, 0);
181 OutMeta( rStrm
, pIndent
, OOO_STRING_SVTOOLS_HTML_META_changed
, aBuffer
.makeStringAndClear(), false,
182 eDestEnc
, pNonConvertableChars
);
185 const OUString
& rTheme
= i_xDocProps
->getSubject();
186 if( !rTheme
.isEmpty() )
187 OutMeta( rStrm
, pIndent
, OOO_STRING_SVTOOLS_HTML_META_classification
, rTheme
, false,
188 eDestEnc
, pNonConvertableChars
);
191 const OUString
& rComment
= i_xDocProps
->getDescription();
192 if( !rComment
.isEmpty() )
193 OutMeta( rStrm
, pIndent
, OOO_STRING_SVTOOLS_HTML_META_description
, rComment
, false,
194 eDestEnc
, pNonConvertableChars
);
197 OUString Keywords
= ::comphelper::string::convertCommaSeparated(
198 i_xDocProps
->getKeywords());
199 if( !Keywords
.isEmpty() )
200 OutMeta( rStrm
, pIndent
, OOO_STRING_SVTOOLS_HTML_META_keywords
, Keywords
, false,
201 eDestEnc
, pNonConvertableChars
);
203 uno::Reference
< script::XTypeConverter
> xConverter( script::Converter::create(
204 ::comphelper::getProcessComponentContext() ) );
205 uno::Reference
<beans::XPropertySet
> xUserDefinedProps(
206 i_xDocProps
->getUserDefinedProperties(), uno::UNO_QUERY_THROW
);
207 DBG_ASSERT(xUserDefinedProps
.is(), "UserDefinedProperties is null");
208 uno::Reference
<beans::XPropertySetInfo
> xPropInfo
=
209 xUserDefinedProps
->getPropertySetInfo();
210 DBG_ASSERT(xPropInfo
.is(), "UserDefinedProperties Info is null");
211 uno::Sequence
<beans::Property
> props
= xPropInfo
->getProperties();
212 for (sal_Int32 i
= 0; i
< props
.getLength(); ++i
)
216 OUString name
= props
[i
].Name
;
217 uno::Any aStr
= xConverter
->convertToSimpleType(
218 xUserDefinedProps
->getPropertyValue(name
),
219 uno::TypeClass_STRING
);
222 OUString
valstr(comphelper::string::stripEnd(str
, ' '));
223 OutMeta( rStrm
, pIndent
, name
, valstr
, false,
224 eDestEnc
, pNonConvertableChars
);
226 catch (const uno::Exception
&)
228 // may happen with concurrent modification...
229 DBG_WARNING("SfxFrameHTMLWriter::Out_DocInfo: exception");
235 void SfxFrameHTMLWriter::Out_FrameDescriptor(
236 SvStream
& rOut
, const OUString
& rBaseURL
, const uno::Reference
< beans::XPropertySet
>& xSet
,
237 rtl_TextEncoding eDestEnc
, OUString
*pNonConvertableChars
)
243 uno::Any aAny
= xSet
->getPropertyValue("FrameURL");
244 if ( (aAny
>>= aStr
) && !aStr
.isEmpty() )
246 OUString aURL
= INetURLObject( aStr
).GetMainURL( INetURLObject::DECODE_TO_IURI
);
247 if( !aURL
.isEmpty() )
249 aURL
= URIHelper::simpleNormalizedMakeRelative(
251 sOut
.append(' ').append(OOO_STRING_SVTOOLS_HTML_O_src
)
253 rOut
.WriteCharPtr( sOut
.makeStringAndClear().getStr() );
254 HTMLOutFuncs::Out_String( rOut
, aURL
, eDestEnc
, pNonConvertableChars
);
259 aAny
= xSet
->getPropertyValue("FrameName");
260 if ( (aAny
>>= aStr
) && !aStr
.isEmpty() )
262 sOut
.append(' ').append(OOO_STRING_SVTOOLS_HTML_O_name
)
264 rOut
.WriteCharPtr( sOut
.makeStringAndClear().getStr() );
265 HTMLOutFuncs::Out_String( rOut
, aStr
, eDestEnc
, pNonConvertableChars
);
269 sal_Int32 nVal
= SIZE_NOT_SET
;
270 aAny
= xSet
->getPropertyValue("FrameMarginWidth");
271 if ( (aAny
>>= nVal
) && nVal
!= SIZE_NOT_SET
)
273 sOut
.append(' ').append(OOO_STRING_SVTOOLS_HTML_O_marginwidth
)
274 .append('=').append(nVal
);
276 aAny
= xSet
->getPropertyValue("FrameMarginHeight");
277 if ( (aAny
>>= nVal
) && nVal
!= SIZE_NOT_SET
)
279 sOut
.append(' ').append(OOO_STRING_SVTOOLS_HTML_O_marginheight
)
280 .append('=').append(nVal
);
284 aAny
= xSet
->getPropertyValue("FrameIsAutoScroll");
285 if ( (aAny
>>= bVal
) && !bVal
)
287 aAny
= xSet
->getPropertyValue("FrameIsScrollingMode");
290 const sal_Char
*pStr
= bVal
? sHTML_SC_yes
: sHTML_SC_no
;
291 sOut
.append(' ').append(OOO_STRING_SVTOOLS_HTML_O_scrolling
)
296 // frame border (MS+Netscape-Extension)
297 aAny
= xSet
->getPropertyValue("FrameIsAutoBorder");
298 if ( (aAny
>>= bVal
) && !bVal
)
300 aAny
= xSet
->getPropertyValue("FrameIsBorder");
303 const char* pStr
= bVal
? sHTML_SC_yes
: sHTML_SC_no
;
304 sOut
.append(' ').append(OOO_STRING_SVTOOLS_HTML_O_frameborder
)
305 .append('=').append(pStr
);
308 rOut
.WriteCharPtr( sOut
.getStr() );
310 catch (const uno::Exception
&)
314 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */