bump product version to 7.6.3.2-android
[LibreOffice.git] / xmloff / source / style / XMLPageExport.cxx
bloba428cdf4c546cf866ceacbbe43d5faea32737065
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 <xmloff/XMLPageExport.hxx>
21 #include <o3tl/any.hxx>
22 #include <sal/log.hxx>
23 #include <xmloff/xmlnamespace.hxx>
24 #include <xmloff/xmltoken.hxx>
25 #include <com/sun/star/frame/XModel.hpp>
26 #include <com/sun/star/style/XStyleFamiliesSupplier.hpp>
27 #include <com/sun/star/style/XStyle.hpp>
28 #include <com/sun/star/beans/XPropertySet.hpp>
29 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
30 #include <xmloff/families.hxx>
31 #include <xmloff/xmlexp.hxx>
32 #include <PageMasterPropHdlFactory.hxx>
33 #include <PageMasterStyleMap.hxx>
34 #include <PageMasterPropMapper.hxx>
35 #include "PageMasterExportPropMapper.hxx"
37 using namespace ::com::sun::star;
38 using namespace ::com::sun::star::uno;
39 using namespace ::com::sun::star::style;
40 using namespace ::com::sun::star::container;
41 using namespace ::com::sun::star::beans;
42 using namespace ::xmloff::token;
44 constexpr OUStringLiteral gsIsPhysical( u"IsPhysical" );
45 constexpr OUStringLiteral gsFollowStyle( u"FollowStyle" );
47 namespace {
49 bool findPageMasterNameEntry(
50 ::std::vector<XMLPageExportNameEntry> const& aNameVector,
51 const OUString& rStyleName, XMLPageExportNameEntry & o_rEntry)
53 auto pEntry = std::find_if(aNameVector.cbegin(), aNameVector.cend(),
54 [&rStyleName](const XMLPageExportNameEntry& rEntry) { return rEntry.sStyleName == rStyleName; });
56 if( pEntry != aNameVector.cend() )
58 o_rEntry = *pEntry;
59 return true;
62 return false;
65 } // namespace
67 void XMLPageExport::collectPageMasterAutoStyle(
68 const Reference < XPropertySet > & rPropSet,
69 XMLPageExportNameEntry & rEntry)
71 SAL_WARN_IF( !m_xPageMasterPropSetMapper.is(), "xmloff", "page master family/XMLPageMasterPropSetMapper not found" );
72 if( m_xPageMasterPropSetMapper.is() )
74 ::std::vector<XMLPropertyState> aPropStates = m_xPageMasterExportPropMapper->Filter(m_rExport, rPropSet);
75 if( !aPropStates.empty())
77 OUString sParent;
78 rEntry.sPageMasterName = m_rExport.GetAutoStylePool()->Find( XmlStyleFamily::PAGE_MASTER, sParent, aPropStates );
79 if (rEntry.sPageMasterName.isEmpty())
81 rEntry.sPageMasterName = m_rExport.GetAutoStylePool()->Add(XmlStyleFamily::PAGE_MASTER, sParent, std::move(aPropStates));
85 assert(m_xPageMasterDrawingPageExportPropMapper.is());
86 ::std::vector<XMLPropertyState> aPropStates(
87 m_xPageMasterDrawingPageExportPropMapper->Filter(m_rExport, rPropSet));
88 if (!aPropStates.empty())
90 OUString sParent;
91 rEntry.sDrawingPageStyleName = m_rExport.GetAutoStylePool()->Find(XmlStyleFamily::SD_DRAWINGPAGE_ID, sParent, aPropStates);
92 if (rEntry.sDrawingPageStyleName.isEmpty())
94 rEntry.sDrawingPageStyleName = m_rExport.GetAutoStylePool()->Add(XmlStyleFamily::SD_DRAWINGPAGE_ID, sParent, std::move(aPropStates));
99 void XMLPageExport::exportMasterPageContent(
100 const Reference < XPropertySet > &,
101 bool /*bAutoStyles*/ )
106 bool XMLPageExport::exportStyle(
107 const Reference< XStyle >& rStyle,
108 bool bAutoStyles )
110 Reference< XPropertySet > xPropSet( rStyle, UNO_QUERY );
111 Reference< XPropertySetInfo > xPropSetInfo = xPropSet->getPropertySetInfo();
113 // Don't export styles that aren't existing really. This may be the
114 // case for StarOffice Writer's pool styles.
115 if( xPropSetInfo->hasPropertyByName( gsIsPhysical ) )
117 Any aAny = xPropSet->getPropertyValue( gsIsPhysical );
118 if( !*o3tl::doAccess<bool>(aAny) )
119 return false;
122 if( bAutoStyles )
124 XMLPageExportNameEntry aEntry;
125 collectPageMasterAutoStyle(xPropSet, aEntry);
126 aEntry.sStyleName = rStyle->getName();
127 m_aNameVector.push_back( aEntry );
129 exportMasterPageContent( xPropSet, true );
131 else
133 OUString sName( rStyle->getName() );
134 bool bEncoded = false;
135 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_NAME,
136 GetExport().EncodeStyleName( sName, &bEncoded ) );
138 if ( xPropSetInfo->hasPropertyByName( "Hidden" ) )
140 uno::Any aValue = xPropSet->getPropertyValue( "Hidden" );
141 bool bHidden = false;
142 if ((aValue >>= bHidden) && bHidden
143 && GetExport().getSaneDefaultVersion() & SvtSaveOptions::ODFSVER_EXTENDED)
145 GetExport().AddAttribute(XML_NAMESPACE_LO_EXT, XML_HIDDEN, "true");
146 GetExport().AddAttribute(XML_NAMESPACE_STYLE, XML_HIDDEN, "true"); // FIXME for compatibility
150 if( bEncoded )
151 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_DISPLAY_NAME,
152 sName);
154 XMLPageExportNameEntry entry;
155 if (findPageMasterNameEntry(m_aNameVector, sName, entry))
157 GetExport().AddAttribute(XML_NAMESPACE_STYLE, XML_PAGE_LAYOUT_NAME, GetExport().EncodeStyleName(entry.sPageMasterName));
158 if (!entry.sDrawingPageStyleName.isEmpty())
160 GetExport().AddAttribute(XML_NAMESPACE_DRAW, XML_STYLE_NAME, GetExport().EncodeStyleName(entry.sDrawingPageStyleName));
164 Reference<XPropertySetInfo> xInfo = xPropSet->getPropertySetInfo();
165 if ( xInfo.is() && xInfo->hasPropertyByName(gsFollowStyle) )
167 OUString sNextName;
168 xPropSet->getPropertyValue( gsFollowStyle ) >>= sNextName;
170 if( sName != sNextName && !sNextName.isEmpty() )
172 GetExport().AddAttribute( XML_NAMESPACE_STYLE, XML_NEXT_STYLE_NAME,
173 GetExport().EncodeStyleName( sNextName ) );
177 SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE,
178 XML_MASTER_PAGE, true, true );
180 exportMasterPageContent( xPropSet, false );
183 return true;
186 XMLPageExport::XMLPageExport(SvXMLExport & rExp)
187 : m_rExport(rExp)
188 , m_xPageMasterPropHdlFactory(new XMLPageMasterPropHdlFactory)
189 , m_xPageMasterPropSetMapper(new XMLPageMasterPropSetMapper(
190 aXMLPageMasterStyleMap,
191 m_xPageMasterPropHdlFactory))
192 , m_xPageMasterExportPropMapper(new XMLPageMasterExportPropMapper(
193 m_xPageMasterPropSetMapper, rExp))
194 , m_xPageMasterDrawingPagePropSetMapper(new XMLPageMasterPropSetMapper(
195 g_XMLPageMasterDrawingPageStyleMap,
196 m_xPageMasterPropHdlFactory))
197 // use same class but with different map, need its ContextFilter()
198 , m_xPageMasterDrawingPageExportPropMapper(new XMLPageMasterExportPropMapper(
199 m_xPageMasterDrawingPagePropSetMapper, rExp))
201 m_rExport.GetAutoStylePool()->AddFamily( XmlStyleFamily::PAGE_MASTER, XML_STYLE_FAMILY_PAGE_MASTER_NAME,
202 m_xPageMasterExportPropMapper, XML_STYLE_FAMILY_PAGE_MASTER_PREFIX, false );
203 m_rExport.GetAutoStylePool()->AddFamily(XmlStyleFamily::SD_DRAWINGPAGE_ID, XML_STYLE_FAMILY_SD_DRAWINGPAGE_NAME,
204 m_xPageMasterDrawingPageExportPropMapper, XML_STYLE_FAMILY_SD_DRAWINGPAGE_PREFIX);
206 Reference< XStyleFamiliesSupplier > xFamiliesSupp( GetExport().GetModel(),
207 UNO_QUERY );
208 SAL_WARN_IF( !xFamiliesSupp.is(), "xmloff",
209 "No XStyleFamiliesSupplier from XModel for export!" );
210 if( !xFamiliesSupp.is() )
211 return;
213 Reference< XNameAccess > xFamilies( xFamiliesSupp->getStyleFamilies() );
214 SAL_WARN_IF( !xFamiliesSupp.is(), "xmloff",
215 "getStyleFamilies() from XModel failed for export!" );
216 if( xFamilies.is() )
218 static const OUStringLiteral aPageStyleName(u"PageStyles");
220 if( xFamilies->hasByName( aPageStyleName ) )
222 m_xPageStyles.set(xFamilies->getByName( aPageStyleName ),uno::UNO_QUERY);
224 SAL_WARN_IF( !m_xPageStyles.is(), "xmloff",
225 "Page Styles not found for export!" );
229 if (GetExport().GetModelType() != SvtModuleOptions::EFactory::WRITER)
230 return;
232 uno::Reference<lang::XMultiServiceFactory> xFac(GetExport().GetModel(), uno::UNO_QUERY);
233 if (!xFac.is())
234 return;
236 uno::Reference<beans::XPropertySet> xProps(
237 xFac->createInstance("com.sun.star.document.Settings"), uno::UNO_QUERY);
238 if (!xProps.is())
239 return;
241 bool bGutterAtTop{};
242 xProps->getPropertyValue("GutterAtTop") >>= bGutterAtTop;
243 if (bGutterAtTop)
245 static_cast<XMLPageMasterExportPropMapper*>(m_xPageMasterExportPropMapper.get())
246 ->SetGutterAtTop(true);
250 XMLPageExport::~XMLPageExport()
254 void XMLPageExport::exportStyles( bool bUsed, bool bAutoStyles )
256 if( m_xPageStyles.is() )
258 const uno::Sequence< OUString> aSeq = m_xPageStyles->getElementNames();
259 for(const auto& rName : aSeq)
261 Reference< XStyle > xStyle(m_xPageStyles->getByName( rName ),uno::UNO_QUERY);
262 if( !bUsed || xStyle->isInUse() )
263 exportStyle( xStyle, bAutoStyles );
268 void XMLPageExport::exportAutoStyles()
270 m_rExport.GetAutoStylePool()->exportXML(XmlStyleFamily::PAGE_MASTER);
271 // tdf#103602 this is called by both Writer and Calc but Calc doesn't
272 // have fill properties yet
273 m_rExport.GetAutoStylePool()->exportXML(XmlStyleFamily::SD_DRAWINGPAGE_ID);
276 void XMLPageExport::exportDefaultStyle()
278 Reference < lang::XMultiServiceFactory > xFactory (GetExport().GetModel(), UNO_QUERY);
279 if (!xFactory.is())
280 return;
282 Reference < XPropertySet > xPropSet (xFactory->createInstance ( "com.sun.star.text.Defaults" ), UNO_QUERY);
283 if (!xPropSet.is())
284 return;
286 // <style:default-style ...>
287 GetExport().CheckAttrList();
289 ::std::vector< XMLPropertyState > aPropStates =
290 m_xPageMasterExportPropMapper->FilterDefaults(m_rExport, xPropSet);
292 bool bExport = false;
293 rtl::Reference < XMLPropertySetMapper > aPropMapper(m_xPageMasterExportPropMapper->getPropertySetMapper());
294 for( const auto& rProp : aPropStates )
296 sal_Int16 nContextId = aPropMapper->GetEntryContextId( rProp.mnIndex );
297 if( nContextId == CTF_PM_STANDARD_MODE )
299 bExport = true;
300 break;
304 if( !bExport )
305 return;
307 assert(GetExport().getSaneDefaultVersion()
308 >= SvtSaveOptions::ODFSVER_012);
310 //<style:default-page-layout>
311 SvXMLElementExport aElem( GetExport(), XML_NAMESPACE_STYLE,
312 XML_DEFAULT_PAGE_LAYOUT,
313 true, true );
315 m_xPageMasterExportPropMapper->exportXML( GetExport(), aPropStates,
316 SvXmlExportFlags::IGN_WS );
319 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */