Use correct object
[LibreOffice.git] / sfx2 / source / bastyp / frmhtmlw.cxx
blob9d9da3a79b95c53b0f52745ad59b5d9a2e6aec89
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
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>
23 #include <sal/log.hxx>
25 #include <svl/urihelper.hxx>
26 #include <tools/stream.hxx>
27 #include <tools/debug.hxx>
28 #include <unotools/resmgr.hxx>
29 #include <svtools/htmlout.hxx>
31 #include <sfx2/frmdescr.hxx>
32 #include <sfx2/frmhtmlw.hxx>
33 #include <strings.hxx>
35 #include <comphelper/processfactory.hxx>
36 #include <comphelper/string.hxx>
38 #include <com/sun/star/script/Converter.hpp>
39 #include <com/sun/star/document/XDocumentProperties.hpp>
40 #include <com/sun/star/beans/XPropertySet.hpp>
42 #include <rtl/bootstrap.hxx>
43 #include <rtl/strbuf.hxx>
44 #include <sax/tools/converter.hxx>
46 using namespace ::com::sun::star;
48 char const sHTML_SC_yes[] = "YES";
49 char const sHTML_SC_no[] = "NO";
51 void SfxFrameHTMLWriter::OutMeta( SvStream& rStrm,
52 const char *pIndent,
53 std::u16string_view rName,
54 std::u16string_view rContent,
55 bool bHTTPEquiv,
56 OUString *pNonConvertableChars )
58 rStrm.WriteOString( SAL_NEWLINE_STRING );
59 if( pIndent )
60 rStrm.WriteOString( pIndent );
62 OStringBuffer sOut;
63 sOut.append("<" OOO_STRING_SVTOOLS_HTML_meta " ")
64 .append(bHTTPEquiv ? OOO_STRING_SVTOOLS_HTML_O_httpequiv : OOO_STRING_SVTOOLS_HTML_O_name).append("=\"");
65 rStrm.WriteOString( sOut );
66 sOut.setLength(0);
68 HTMLOutFuncs::Out_String( rStrm, rName, pNonConvertableChars );
70 sOut.append("\" " OOO_STRING_SVTOOLS_HTML_O_content "=\"");
71 rStrm.WriteOString( sOut );
72 sOut.setLength(0);
74 HTMLOutFuncs::Out_String( rStrm, rContent, pNonConvertableChars ).WriteOString( "\"/>" );
77 void SfxFrameHTMLWriter::Out_DocInfo( SvStream& rStrm, const OUString& rBaseURL,
78 const uno::Reference<document::XDocumentProperties> & i_xDocProps,
79 const char *pIndent,
80 OUString *pNonConvertableChars )
82 OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_content_type, u"text/html; charset=utf-8", true,
83 pNonConvertableChars );
85 // Title (regardless if empty)
86 rStrm.WriteOString( SAL_NEWLINE_STRING );
87 if( pIndent )
88 rStrm.WriteOString( pIndent );
89 HTMLOutFuncs::Out_AsciiTag( rStrm, OOO_STRING_SVTOOLS_HTML_title );
90 if( i_xDocProps.is() )
92 const OUString aTitle = i_xDocProps->getTitle();
93 if( !aTitle.isEmpty() )
94 HTMLOutFuncs::Out_String( rStrm, aTitle, pNonConvertableChars );
96 HTMLOutFuncs::Out_AsciiTag( rStrm, OOO_STRING_SVTOOLS_HTML_title, false );
98 // Target-Frame
99 if( i_xDocProps.is() )
101 const OUString aTarget = i_xDocProps->getDefaultTarget();
102 if( !aTarget.isEmpty() )
104 rStrm.WriteOString( SAL_NEWLINE_STRING );
105 if( pIndent )
106 rStrm.WriteOString( pIndent );
108 rStrm.WriteOString( "<" OOO_STRING_SVTOOLS_HTML_base " "
109 OOO_STRING_SVTOOLS_HTML_O_target "=\"" );
110 HTMLOutFuncs::Out_String( rStrm, aTarget, pNonConvertableChars )
111 .WriteOString( "\">" );
115 // Who we are
116 OUString sGenerator(Translate::ExpandVariables(STR_HTML_GENERATOR));
117 OUString os( u"$_OS"_ustr );
118 ::rtl::Bootstrap::expandMacros(os);
119 sGenerator = sGenerator.replaceFirst( "%1", os );
120 OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_generator, sGenerator, false, pNonConvertableChars );
122 if( !i_xDocProps.is() )
123 return;
125 // Reload
126 if( (i_xDocProps->getAutoloadSecs() != 0) ||
127 !i_xDocProps->getAutoloadURL().isEmpty() )
129 OUString sContent = OUString::number(
130 i_xDocProps->getAutoloadSecs() );
132 const OUString aReloadURL = i_xDocProps->getAutoloadURL();
133 if( !aReloadURL.isEmpty() )
135 sContent += ";URL=" + URIHelper::simpleNormalizedMakeRelative(
136 rBaseURL, aReloadURL);
139 OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_refresh, sContent, true,
140 pNonConvertableChars );
143 // Author
144 const OUString aAuthor = i_xDocProps->getAuthor();
145 if( !aAuthor.isEmpty() )
146 OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_author, aAuthor, false,
147 pNonConvertableChars );
149 // created
150 ::util::DateTime uDT = i_xDocProps->getCreationDate();
151 OUStringBuffer aBuffer;
152 ::sax::Converter::convertTimeOrDateTime(aBuffer, uDT);
154 OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_created, aBuffer.makeStringAndClear(), false,
155 pNonConvertableChars );
157 // changedby
158 const OUString aChangedBy = i_xDocProps->getModifiedBy();
159 if( !aChangedBy.isEmpty() )
160 OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_changedby, aChangedBy, false,
161 pNonConvertableChars );
163 // changed
164 uDT = i_xDocProps->getModificationDate();
165 ::sax::Converter::convertTimeOrDateTime(aBuffer, uDT);
167 OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_changed, aBuffer.makeStringAndClear(), false,
168 pNonConvertableChars );
170 // Subject
171 const OUString aTheme = i_xDocProps->getSubject();
172 if( !aTheme.isEmpty() )
173 OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_classification, aTheme, false,
174 pNonConvertableChars );
176 // Description
177 const OUString aComment = i_xDocProps->getDescription();
178 if( !aComment.isEmpty() )
179 OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_description, aComment, false,
180 pNonConvertableChars);
182 // Keywords
183 OUString Keywords = ::comphelper::string::convertCommaSeparated(
184 i_xDocProps->getKeywords());
185 if( !Keywords.isEmpty() )
186 OutMeta( rStrm, pIndent, OOO_STRING_SVTOOLS_HTML_META_keywords, Keywords, false,
187 pNonConvertableChars);
189 uno::Reference < script::XTypeConverter > xConverter( script::Converter::create(
190 ::comphelper::getProcessComponentContext() ) );
191 uno::Reference<beans::XPropertySet> xUserDefinedProps(
192 i_xDocProps->getUserDefinedProperties(), uno::UNO_QUERY_THROW);
193 uno::Reference<beans::XPropertySetInfo> xPropInfo =
194 xUserDefinedProps->getPropertySetInfo();
195 DBG_ASSERT(xPropInfo.is(), "UserDefinedProperties Info is null");
196 const uno::Sequence<beans::Property> props = xPropInfo->getProperties();
197 for (const auto& rProp : props)
201 OUString name = rProp.Name;
202 uno::Any aStr = xConverter->convertToSimpleType(
203 xUserDefinedProps->getPropertyValue(name),
204 uno::TypeClass_STRING);
205 OUString str;
206 aStr >>= str;
207 OUString valstr(comphelper::string::stripEnd(str, ' '));
208 OutMeta( rStrm, pIndent, name, valstr, false,
209 pNonConvertableChars );
211 catch (const uno::Exception&)
213 // may happen with concurrent modification...
214 SAL_INFO("sfx", "SfxFrameHTMLWriter::Out_DocInfo: exception");
219 void SfxFrameHTMLWriter::Out_FrameDescriptor(
220 SvStream& rOut, const OUString& rBaseURL, const uno::Reference < beans::XPropertySet >& xSet )
224 OStringBuffer sOut;
225 OUString aStr;
226 uno::Any aAny = xSet->getPropertyValue(u"FrameURL"_ustr);
227 if ( (aAny >>= aStr) && !aStr.isEmpty() )
229 OUString aURL = INetURLObject( aStr ).GetMainURL( INetURLObject::DecodeMechanism::ToIUri );
230 if( !aURL.isEmpty() )
232 aURL = URIHelper::simpleNormalizedMakeRelative(
233 rBaseURL, aURL );
234 sOut.append(" " OOO_STRING_SVTOOLS_HTML_O_src "=\"");
235 rOut.WriteOString( sOut );
236 sOut.setLength(0);
237 HTMLOutFuncs::Out_String( rOut, aURL );
238 sOut.append('\"');
242 aAny = xSet->getPropertyValue(u"FrameName"_ustr);
243 if ( (aAny >>= aStr) && !aStr.isEmpty() )
245 sOut.append(" " OOO_STRING_SVTOOLS_HTML_O_name "=\"");
246 rOut.WriteOString( sOut );
247 sOut.setLength(0);
248 HTMLOutFuncs::Out_String( rOut, aStr );
249 sOut.append('\"');
252 sal_Int32 nVal = SIZE_NOT_SET;
253 aAny = xSet->getPropertyValue(u"FrameMarginWidth"_ustr);
254 if ( (aAny >>= nVal) && nVal != SIZE_NOT_SET )
256 sOut.append(" " OOO_STRING_SVTOOLS_HTML_O_marginwidth
257 "=" + OString::number(nVal));
259 aAny = xSet->getPropertyValue(u"FrameMarginHeight"_ustr);
260 if ( (aAny >>= nVal) && nVal != SIZE_NOT_SET )
262 sOut.append(" " OOO_STRING_SVTOOLS_HTML_O_marginheight
263 "=" + OString::number(nVal));
266 bool bVal = true;
267 aAny = xSet->getPropertyValue(u"FrameIsAutoScroll"_ustr);
268 if ( (aAny >>= bVal) && !bVal )
270 aAny = xSet->getPropertyValue(u"FrameIsScrollingMode"_ustr);
271 if ( aAny >>= bVal )
273 const char *pStr = bVal ? sHTML_SC_yes : sHTML_SC_no;
274 sOut.append(OString::Concat(" " OOO_STRING_SVTOOLS_HTML_O_scrolling) +
275 pStr);
279 // frame border (MS+Netscape-Extension)
280 aAny = xSet->getPropertyValue(u"FrameIsAutoBorder"_ustr);
281 if ( (aAny >>= bVal) && !bVal )
283 aAny = xSet->getPropertyValue(u"FrameIsBorder"_ustr);
284 if ( aAny >>= bVal )
286 const char* pStr = bVal ? sHTML_SC_yes : sHTML_SC_no;
287 sOut.append(OString::Concat(" " OOO_STRING_SVTOOLS_HTML_O_frameborder
288 "=") + pStr);
291 rOut.WriteOString( sOut );
292 sOut.setLength(0);
294 catch (const uno::Exception&)
298 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */