related: tdf#162601 UNIQUE function is case-insensitive also for non ASCII
[LibreOffice.git] / sw / source / filter / docx / swdocxreader.cxx
blob54f8599034bbcd290fa77861607c4c6b4995988c
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 "swdocxreader.hxx"
22 #include <com/sun/star/document/XFilter.hpp>
23 #include <com/sun/star/document/XImporter.hpp>
24 #include <com/sun/star/frame/XModel.hpp>
25 #include <com/sun/star/lang/XMultiServiceFactory.hpp>
26 #include <comphelper/processfactory.hxx>
27 #include <comphelper/propertysequence.hxx>
28 #include <doc.hxx>
29 #include <docsh.hxx>
30 #include <IDocumentStylePoolAccess.hxx>
31 #include <ndtxt.hxx>
32 #include <poolfmt.hxx>
33 #include <swerror.h>
34 #include <unotools/streamwrap.hxx>
35 #include <unotextrange.hxx>
36 #include <sfx2/docfile.hxx>
37 #include <comphelper/diagnose_ex.hxx>
39 constexpr OUString AUTOTEXT_GALLERY = u"autoTxt"_ustr;
41 using namespace css;
43 extern "C" SAL_DLLPUBLIC_EXPORT Reader* ImportDOCX()
45 return new SwDOCXReader;
48 ErrCodeMsg SwDOCXReader::Read(SwDoc& rDoc, const OUString& /* rBaseURL */, SwPaM& rPam, const OUString& /* FileName */ )
50 if (!m_pMedium->GetInStream())
51 return ERR_SWG_READ_ERROR;
53 // We want to work in an empty paragraph.
54 const SwPosition* pPos = rPam.GetPoint();
55 rDoc.getIDocumentContentOperations().SplitNode(*pPos, false);
56 rDoc.SetTextFormatColl(rPam, rDoc.getIDocumentStylePoolAccess().GetTextCollFromPool(RES_POOLCOLL_STANDARD, false));
58 uno::Reference<lang::XMultiServiceFactory> xMultiServiceFactory(comphelper::getProcessServiceFactory());
59 uno::Reference<uno::XInterface> xInterface(xMultiServiceFactory->createInstance(u"com.sun.star.comp.Writer.WriterFilter"_ustr), uno::UNO_SET_THROW);
61 ErrCode ret = ERRCODE_NONE;
62 SwDocShell* pDocShell(rDoc.GetDocShell());
63 if (!pDocShell)
64 return ret;
66 uno::Reference<lang::XComponent> xDstDoc(pDocShell->GetModel(), uno::UNO_QUERY_THROW);
67 uno::Reference<document::XImporter> xImporter(xInterface, uno::UNO_QUERY_THROW);
68 xImporter->setTargetDocument(xDstDoc);
70 const rtl::Reference<SwXTextRange> xInsertTextRange = SwXTextRange::CreateXTextRange(rDoc, *rPam.GetPoint(), nullptr);
71 uno::Reference<io::XStream> xStream(new utl::OStreamWrapper(*m_pMedium->GetInStream()));
73 //SetLoading hack because the document properties will be re-initted
74 //by the xml filter and during the init, while it's considered uninitialized,
75 //setting a property will inform the document it's modified, which attempts
76 //to update the properties, which throws cause the properties are uninitialized
77 pDocShell->SetLoading(SfxLoadedFlags::NONE);
79 uno::Sequence<beans::PropertyValue> aDescriptor(comphelper::InitPropertySequence(
81 { "InputStream", uno::Any(xStream) },
82 { "InsertMode", uno::Any(true) },
83 { "TextInsertModeRange", uno::Any(uno::Reference<text::XTextRange>(xInsertTextRange)) }
84 }));
86 uno::Reference<document::XFilter> xFilter(xInterface, uno::UNO_QUERY_THROW);
87 try
89 xFilter->filter(aDescriptor);
91 catch (uno::Exception const&)
93 TOOLS_WARN_EXCEPTION("sw.docx", "SwDOCXReader::Read()");
94 ret = ERR_SWG_READ_ERROR;
96 pDocShell->SetLoading(SfxLoadedFlags::ALL);
98 return ret;
101 SwReaderType SwDOCXReader::GetReaderType()
103 return SwReaderType::Storage | SwReaderType::Stream;
106 bool SwDOCXReader::HasGlossaries() const
108 // TODO
109 return true;
112 bool SwDOCXReader::ReadGlossaries( SwTextBlocks& rBlocks, bool /* bSaveRelFiles */ ) const
114 uno::Reference<lang::XMultiServiceFactory> xMultiServiceFactory(
115 comphelper::getProcessServiceFactory() );
117 uno::Reference<uno::XInterface> xInterface(
118 xMultiServiceFactory->createInstance( u"com.sun.star.comp.Writer.WriterFilter"_ustr ),
119 uno::UNO_SET_THROW );
121 uno::Reference<document::XFilter> xFilter( xInterface, uno::UNO_QUERY_THROW );
122 uno::Reference<document::XImporter> xImporter( xFilter, uno::UNO_QUERY_THROW );
124 SfxObjectShellLock xDocSh( new SwDocShell( SfxObjectCreateMode::INTERNAL ) );
125 if( xDocSh->DoInitNew() )
127 uno::Reference<lang::XComponent> xDstDoc( xDocSh->GetModel(), uno::UNO_QUERY_THROW );
128 xImporter->setTargetDocument( xDstDoc );
130 uno::Reference<io::XStream> xStream( new utl::OStreamWrapper( *m_pMedium->GetInStream() ) );
132 uno::Sequence<beans::PropertyValue> aDescriptor( comphelper::InitPropertySequence({
133 { "InputStream", uno::Any(xStream) },
134 { "ReadGlossaries", uno::Any(true) }
135 }));
137 if( xFilter->filter( aDescriptor ) )
139 if (rBlocks.StartPutMuchBlockEntries())
141 bool bRet = MakeEntries(static_cast<SwDocShell*>(&xDocSh)->GetDoc(), rBlocks);
142 rBlocks.EndPutMuchBlockEntries();
143 return bRet;
148 return false;
151 bool SwDOCXReader::MakeEntries( SwDoc *pD, SwTextBlocks &rBlocks )
153 const OUString aOldURL( rBlocks.GetBaseURL() );
154 rBlocks.SetBaseURL( OUString() );
156 bool bRet = false;
158 SwNodeIndex aDocEnd( pD->GetNodes().GetEndOfContent() );
159 SwNodeIndex aStart( *aDocEnd.GetNode().StartOfSectionNode(), 1 );
160 bool bIsAutoText = false;
162 if( aStart < aDocEnd && ( aDocEnd.GetIndex() - aStart.GetIndex() > SwNodeOffset(2) ) )
164 SwTextFormatColl* pColl = pD->getIDocumentStylePoolAccess().GetTextCollFromPool
165 (RES_POOLCOLL_STANDARD, false);
166 SwContentNode* pCNd = nullptr;
167 bRet = true;
168 do {
169 // Get name - first paragraph
170 OUString aLNm;
172 SwPaM aPam( aStart );
173 aPam.GetPoint()->Adjust(SwNodeOffset(1));
174 aLNm = aPam.GetPointNode().GetTextNode()->GetText();
176 // is AutoText?
177 bIsAutoText = aLNm.startsWith(AUTOTEXT_GALLERY);
178 aLNm = aLNm.copy(AUTOTEXT_GALLERY.getLength() + 1);
181 // Do not copy name
182 ++aStart;
184 // Get content
185 SwPaM aPam( aStart );
187 SwNodeIndex aIdx( aPam.GetPoint()->GetNode(), SwNodeOffset(1) );
188 pCNd = aIdx.GetNode().GetTextNode();
189 if( nullptr == pCNd )
191 pCNd = pD->GetNodes().MakeTextNode( aIdx.GetNode(), pColl );
195 aPam.GetPoint()->Assign( *pCNd );
196 aPam.SetMark();
198 SwNodeIndex aIdx( *aStart.GetNode().EndOfSectionNode(), SwNodeOffset(-1) );
199 // don't add extra empty text node if exist (.dotx but not .dotm)
200 if( aIdx.GetNode().GetTextNode() &&
201 aIdx.GetNode().GetTextNode()->GetText().isEmpty() )
202 aIdx = aStart.GetNode().EndOfSectionIndex() - 2;
203 pCNd = aIdx.GetNode().GetContentNode();
204 if( nullptr == pCNd )
206 ++aIdx;
207 pCNd = pD->GetNodes().MakeTextNode( aIdx.GetNode(), pColl );
210 aPam.GetPoint()->Assign( *pCNd, pCNd->Len() );
212 if( bIsAutoText )
214 // Now we have the right selection for one entry
215 rBlocks.ClearDoc();
217 OUString sShortcut = aLNm;
219 // Need to check make sure the shortcut is not already being used
220 sal_Int32 nStart = 0;
221 sal_uInt16 nCurPos = rBlocks.GetIndex( sShortcut );
223 while( sal_uInt16(-1) != nCurPos )
225 // add a Number to it
226 sShortcut = aLNm + OUString::number( ++nStart );
227 nCurPos = rBlocks.GetIndex( sShortcut );
230 if( rBlocks.BeginPutDoc( sShortcut, sShortcut ) )
232 SwDoc* pGlDoc = rBlocks.GetDoc();
233 SwNodeIndex aIdx( pGlDoc->GetNodes().GetEndOfContent(), -1 );
234 pCNd = aIdx.GetNode().GetContentNode();
235 SwPosition aPos( aIdx, pCNd, pCNd ? pCNd->Len() : 0 );
236 pD->getIDocumentContentOperations().CopyRange(aPam, aPos, SwCopyFlags::CheckPosInFly);
237 rBlocks.PutDoc();
239 else
241 bRet = false;
245 aStart = aStart.GetNode().EndOfSectionIndex() + 1;
246 } while( aStart < aDocEnd && aStart.GetNode().IsStartNode() );
249 rBlocks.SetBaseURL( aOldURL );
251 return bRet;
254 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */