Bump version to 21.06.18.1
[LibreOffice.git] / sw / inc / shellio.hxx
blobd05293c453650258783a31c12bd02c1aedd13ccd
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 .
19 #ifndef INCLUDED_SW_INC_SHELLIO_HXX
20 #define INCLUDED_SW_INC_SHELLIO_HXX
22 #include <memory>
24 #include <com/sun/star/uno/Reference.h>
25 #include <sot/storage.hxx>
26 #include <tools/date.hxx>
27 #include <tools/time.hxx>
28 #include <tools/datetime.hxx>
29 #include <tools/ref.hxx>
30 #include <rtl/ref.hxx>
31 #include <osl/thread.h>
32 #include <o3tl/deleter.hxx>
33 #include <o3tl/typed_flags_set.hxx>
34 #include "swdllapi.h"
35 #include "docfac.hxx"
36 #include "unocrsr.hxx"
38 class SfxItemPool;
39 class SfxItemSet;
40 class SfxMedium;
41 class SvStream;
42 class SvxFontItem;
43 class SvxMacroTableDtor;
44 class SwContentNode;
45 class SwCursorShell;
46 class SwDoc;
47 class SwPaM;
48 class SwTextBlocks;
49 struct SwPosition;
50 struct Writer_Impl;
51 namespace sw::mark { class IMark; }
52 namespace com::sun::star::embed { class XStorage; }
54 // Defines the count of chars at which a paragraph read via ASCII/W4W-Reader
55 // is forced to wrap. It has to be always greater than 200!!!
56 #define MAX_ASCII_PARA 10000
58 class SW_DLLPUBLIC SwAsciiOptions
60 OUString m_sFont;
61 rtl_TextEncoding m_eCharSet;
62 LanguageType m_nLanguage;
63 LineEnd m_eCRLF_Flag;
64 bool m_bIncludeBOM; // Whether to include a byte-order-mark in the output.
66 public:
68 const OUString& GetFontName() const { return m_sFont; }
69 void SetFontName( const OUString& rFont ) { m_sFont = rFont; }
71 rtl_TextEncoding GetCharSet() const { return m_eCharSet; }
72 void SetCharSet( rtl_TextEncoding nVal ) { m_eCharSet = nVal; }
74 LanguageType GetLanguage() const { return m_nLanguage; }
75 void SetLanguage( LanguageType nVal ) { m_nLanguage = nVal; }
77 LineEnd GetParaFlags() const { return m_eCRLF_Flag; }
78 void SetParaFlags( LineEnd eVal ) { m_eCRLF_Flag = eVal; }
80 bool GetIncludeBOM() const { return m_bIncludeBOM; }
81 void SetIncludeBOM( bool bVal ) { m_bIncludeBOM = bVal; }
83 void Reset()
85 m_sFont.clear();
86 m_eCRLF_Flag = GetSystemLineEnd();
87 m_eCharSet = ::osl_getThreadTextEncoding();
88 m_nLanguage = LANGUAGE_SYSTEM;
89 m_bIncludeBOM = true;
91 // for the automatic conversion (mail/news/...)
92 void ReadUserData( const OUString& );
93 void WriteUserData( OUString& );
95 SwAsciiOptions() { Reset(); }
98 // Base class of possible options for a special reader.
99 class Reader;
101 class SwgReaderOption
103 SwAsciiOptions m_aASCIIOpts;
104 bool m_bFrameFormats;
105 bool m_bPageDescs;
106 bool m_bTextFormats;
107 bool m_bNumRules;
108 bool m_bMerge;
109 css::uno::Reference<css::io::XInputStream> m_xInputStream;
110 public:
111 void ResetAllFormatsOnly() { m_bFrameFormats = m_bPageDescs = m_bTextFormats = m_bNumRules = m_bMerge = false; }
112 bool IsFormatsOnly() const { return m_bFrameFormats || m_bPageDescs || m_bTextFormats || m_bNumRules || m_bMerge; }
114 bool IsFrameFormats() const { return m_bFrameFormats; }
115 void SetFrameFormats( const bool bNew) { m_bFrameFormats = bNew; }
117 bool IsPageDescs() const { return m_bPageDescs; }
118 void SetPageDescs( const bool bNew) { m_bPageDescs = bNew; }
120 bool IsTextFormats() const { return m_bTextFormats; }
121 void SetTextFormats( const bool bNew) { m_bTextFormats = bNew; }
123 bool IsNumRules() const { return m_bNumRules; }
124 void SetNumRules( const bool bNew) { m_bNumRules = bNew; }
126 bool IsMerge() const { return m_bMerge; }
127 void SetMerge( const bool bNew ) { m_bMerge = bNew; }
129 const SwAsciiOptions& GetASCIIOpts() const { return m_aASCIIOpts; }
130 void SetASCIIOpts( const SwAsciiOptions& rOpts ) { m_aASCIIOpts = rOpts; }
131 void ResetASCIIOpts() { m_aASCIIOpts.Reset(); }
133 css::uno::Reference<css::io::XInputStream>& GetInputStream() { return m_xInputStream; }
134 void SetInputStream(const css::uno::Reference<css::io::XInputStream>& xInputStream)
136 m_xInputStream = xInputStream;
139 SwgReaderOption()
140 { ResetAllFormatsOnly(); m_aASCIIOpts.Reset(); }
143 // Calls reader with its options, document, cursor etc.
144 class SW_DLLPUBLIC SwReader: public SwDocFac
146 SvStream* mpStrm;
147 tools::SvRef<SotStorage> mpStg;
148 css::uno::Reference < css::embed::XStorage > mxStg;
149 SfxMedium* mpMedium; // Who wants to obtain a Medium (W4W).
151 SwPaM* mpCursor;
152 OUString maFileName;
153 OUString msBaseURL;
154 bool mbSkipImages;
156 public:
158 // Initial reading. Document is created only at Read(...)
159 // or in case it is given, into that.
160 // Special case for Load with Sw3Reader.
161 SwReader( SfxMedium&, const OUString& rFilename, SwDoc *pDoc = nullptr );
163 // Read into existing document.
164 // Document and position in document are taken from SwPaM.
165 SwReader( SvStream&, const OUString& rFilename, const OUString& rBaseURL, SwPaM& );
166 SwReader( SfxMedium&, const OUString& rFilename, SwPaM& );
167 SwReader( const css::uno::Reference < css::embed::XStorage >&, const OUString& rFilename, SwPaM& );
169 // The only export interface is SwReader::Read(...)!!!
170 ErrCode Read( const Reader& );
172 // Ask for glossaries.
173 bool HasGlossaries( const Reader& );
174 bool ReadGlossaries( const Reader&, SwTextBlocks&, bool bSaveRelFiles );
176 protected:
177 void SetBaseURL( const OUString& rURL ) { msBaseURL = rURL; }
178 void SetSkipImages( bool bSkipImages ) { mbSkipImages = bSkipImages; }
181 typedef std::unique_ptr<SwReader, o3tl::default_delete<SwReader>> SwReaderPtr;
183 // Special Readers can be both!! (Excel, W4W, .. ).
184 enum class SwReaderType {
185 NONE = 0x00,
186 Stream = 0x01,
187 Storage = 0x02
189 namespace o3tl {
190 template<> struct typed_flags<SwReaderType> : is_typed_flags<SwReaderType, 0x03> {};
193 extern "C" SAL_DLLPUBLIC_EXPORT bool TestImportDOC(SvStream &rStream, const OUString &rFltName);
194 extern "C" SAL_DLLPUBLIC_EXPORT bool TestImportDOCX(SvStream &rStream);
195 extern "C" SAL_DLLPUBLIC_EXPORT bool TestImportRTF(SvStream &rStream);
196 extern "C" SAL_DLLPUBLIC_EXPORT bool TestImportHTML(SvStream &rStream);
197 SAL_DLLPUBLIC_EXPORT void FlushFontCache();
199 class SW_DLLPUBLIC Reader
201 friend class SwReader;
202 friend bool TestImportDOC(SvStream &rStream, const OUString &rFltName);
203 friend bool TestImportRTF(SvStream &rStream);
204 friend bool TestImportHTML(SvStream &rStream);
205 rtl::Reference<SwDoc> mxTemplate;
206 OUString m_aTemplateName;
208 Date m_aDateStamp;
209 tools::Time m_aTimeStamp;
210 DateTime m_aCheckDateTime;
212 protected:
213 SvStream* m_pStream;
214 tools::SvRef<SotStorage> m_pStorage;
215 css::uno::Reference < css::embed::XStorage > m_xStorage;
216 SfxMedium* m_pMedium; // Who wants to obtain a Medium (W4W).
218 SwgReaderOption m_aOption;
219 bool m_bInsertMode : 1;
220 bool m_bTemplateBrowseMode : 1;
221 bool m_bReadUTF8: 1; // Interpret stream as UTF-8.
222 bool m_bBlockMode: 1;
223 bool m_bOrganizerMode : 1;
224 bool m_bHasAskTemplateName : 1;
225 bool m_bIgnoreHTMLComments : 1;
226 bool m_bSkipImages : 1;
228 virtual OUString GetTemplateName(SwDoc& rDoc) const;
230 public:
231 Reader();
232 virtual ~Reader();
234 virtual SwReaderType GetReaderType();
235 SwgReaderOption& GetReaderOpt() { return m_aOption; }
237 virtual void SetFltName( const OUString& rFltNm );
239 // Adapt item-set of a Frame-Format to the old format.
240 static void ResetFrameFormatAttrs( SfxItemSet &rFrameSet );
242 // Adapt Frame-/Graphics-/OLE- styles to the old format
243 // (without borders etc.).
244 static void ResetFrameFormats( SwDoc& rDoc );
246 // Load filter template, set it and release it again.
247 SwDoc* GetTemplateDoc(SwDoc& rDoc);
248 bool SetTemplate( SwDoc& rDoc );
249 void ClearTemplate();
250 void SetTemplateName( const OUString& rDir );
251 void MakeHTMLDummyTemplateDoc();
253 bool IsReadUTF8() const { return m_bReadUTF8; }
254 void SetReadUTF8( bool bSet ) { m_bReadUTF8 = bSet; }
256 bool IsBlockMode() const { return m_bBlockMode; }
257 void SetBlockMode( bool bSet ) { m_bBlockMode = bSet; }
259 bool IsOrganizerMode() const { return m_bOrganizerMode; }
260 void SetOrganizerMode( bool bSet ) { m_bOrganizerMode = bSet; }
262 void SetIgnoreHTMLComments( bool bSet ) { m_bIgnoreHTMLComments = bSet; }
264 virtual bool HasGlossaries() const;
265 virtual bool ReadGlossaries( SwTextBlocks&, bool bSaveRelFiles ) const;
267 // Read the sections of the document, which is equal to the medium.
268 // Returns the count of it
269 virtual size_t GetSectionList( SfxMedium& rMedium,
270 std::vector<OUString>& rStrings) const;
272 const tools::SvRef<SotStorage>& getSotStorageRef() const { return m_pStorage; };
273 void setSotStorageRef(const tools::SvRef<SotStorage>& pStgRef) { m_pStorage = pStgRef; };
275 private:
276 virtual ErrCode Read(SwDoc &, const OUString& rBaseURL, SwPaM &, const OUString &)=0;
278 // Everyone who does not need the streams / storages open
279 // has to override the method (W4W!!).
280 virtual bool SetStrmStgPtr();
283 class AsciiReader final : public Reader
285 friend class SwReader;
286 virtual ErrCode Read( SwDoc &, const OUString& rBaseURL, SwPaM &, const OUString &) override;
287 public:
288 AsciiReader(): Reader() {}
291 class SW_DLLPUBLIC StgReader : public Reader
293 OUString m_aFltName;
295 public:
296 virtual SwReaderType GetReaderType() override;
297 const OUString& GetFltName() const { return m_aFltName; }
298 virtual void SetFltName( const OUString& r ) override;
301 // The given stream has to be created dynamically and must
302 // be requested via Stream() before the instance is deleted!
304 class SwImpBlocks;
306 class SW_DLLPUBLIC SwTextBlocks
308 std::unique_ptr<SwImpBlocks> m_pImp;
309 ErrCode m_nErr;
311 public:
312 SwTextBlocks( const OUString& );
313 ~SwTextBlocks();
315 SwDoc* GetDoc();
316 void ClearDoc(); // Delete Doc-contents.
317 OUString GetName() const;
318 void SetName( const OUString& );
319 ErrCode const & GetError() const { return m_nErr; }
321 OUString GetBaseURL() const;
322 void SetBaseURL( const OUString& rURL );
324 sal_uInt16 GetCount() const; // Get count text modules.
325 sal_uInt16 GetIndex( const OUString& ) const; // Get index of short names.
326 sal_uInt16 GetLongIndex( const OUString& ) const; // Get index of long names.
327 OUString GetShortName( sal_uInt16 ) const; // Get short name for index.
328 OUString GetLongName( sal_uInt16 ) const; // Get long name for index.
330 bool Delete( sal_uInt16 );
331 void Rename( sal_uInt16, const OUString*, const OUString* );
333 bool BeginGetDoc( sal_uInt16 ); // Read text modules.
334 void EndGetDoc(); // Release text modules.
336 bool BeginPutDoc( const OUString&, const OUString& ); // Begin save.
337 sal_uInt16 PutDoc(); // End save.
339 sal_uInt16 PutText( const OUString&, const OUString&, const OUString& ); // Save (short name, text).
341 bool IsOnlyTextBlock( sal_uInt16 ) const;
342 bool IsOnlyTextBlock( const OUString& rShort ) const;
344 OUString const & GetFileName() const; // Filename of pImp.
345 bool IsReadOnly() const; // ReadOnly-flag of pImp.
347 bool GetMacroTable( sal_uInt16 nIdx, SvxMacroTableDtor& rMacroTable );
348 bool SetMacroTable( sal_uInt16 nIdx, const SvxMacroTableDtor& rMacroTable );
350 bool StartPutMuchBlockEntries();
351 void EndPutMuchBlockEntries();
354 // BEGIN source/filter/basflt/fltini.cxx
356 extern Reader *ReadAscii, *ReadHTML, *ReadXML;
358 SW_DLLPUBLIC Reader* SwGetReaderXML();
360 // END source/filter/basflt/fltini.cxx
362 extern bool SetHTMLTemplate( SwDoc &rDoc ); //For templates from HTML before loading shellio.cxx.
364 // Base-class of all writers.
366 class IDocumentSettingAccess;
367 class IDocumentStylePoolAccess;
369 class SW_DLLPUBLIC Writer
370 : public SvRefBase
372 SwAsciiOptions m_aAsciiOptions;
373 OUString m_sBaseURL;
375 void AddFontItem( SfxItemPool& rPool, const SvxFontItem& rFont );
376 void AddFontItems_( SfxItemPool& rPool, sal_uInt16 nWhichId );
378 std::unique_ptr<Writer_Impl> m_pImpl;
380 Writer(Writer const&) = delete;
381 Writer& operator=(Writer const&) = delete;
383 protected:
385 const OUString* m_pOrigFileName;
387 void ResetWriter();
388 bool CopyNextPam( SwPaM ** );
390 void PutNumFormatFontsInAttrPool();
391 void PutEditEngFontsInAttrPool();
393 virtual ErrCode WriteStream() = 0;
394 void SetBaseURL( const OUString& rURL ) { m_sBaseURL = rURL; }
396 IDocumentSettingAccess& getIDocumentSettingAccess();
397 const IDocumentSettingAccess& getIDocumentSettingAccess() const;
399 IDocumentStylePoolAccess& getIDocumentStylePoolAccess();
400 const IDocumentStylePoolAccess& getIDocumentStylePoolAccess() const;
402 public:
403 SwDoc* m_pDoc;
404 SwPaM* m_pOrigPam; // Last Pam that has to be processed.
405 std::shared_ptr<SwUnoCursor> m_pCurrentPam;
406 bool m_bWriteAll : 1;
407 bool m_bShowProgress : 1;
408 bool m_bWriteClipboardDoc : 1;
409 bool m_bWriteOnlyFirstTable : 1;
410 bool m_bASCII_ParaAsCR : 1;
411 bool m_bASCII_ParaAsBlank : 1;
412 bool m_bASCII_NoLastLineEnd : 1;
413 bool m_bUCS2_WithStartChar : 1;
414 bool m_bExportParagraphNumbering : 1;
416 bool m_bBlock : 1;
417 bool m_bOrganizerMode : 1;
418 bool m_bHideDeleteRedlines : 1;
420 Writer();
421 virtual ~Writer() override;
423 virtual ErrCode Write( SwPaM&, SfxMedium&, const OUString* );
424 ErrCode Write( SwPaM&, SvStream&, const OUString* );
425 virtual ErrCode Write( SwPaM&, const css::uno::Reference < css::embed::XStorage >&, const OUString*, SfxMedium* = nullptr );
426 virtual ErrCode Write( SwPaM&, SotStorage&, const OUString* );
428 virtual void SetupFilterOptions(SfxMedium& rMedium);
430 virtual bool IsStgWriter() const;
432 void SetShowProgress( bool bFlag ) { m_bShowProgress = bFlag; }
434 const OUString* GetOrigFileName() const { return m_pOrigFileName; }
436 const SwAsciiOptions& GetAsciiOptions() const { return m_aAsciiOptions; }
437 void SetAsciiOptions( const SwAsciiOptions& rOpt ) { m_aAsciiOptions = rOpt; }
439 const OUString& GetBaseURL() const { return m_sBaseURL;}
441 // Look up next bookmark position from bookmark-table.
442 sal_Int32 FindPos_Bkmk( const SwPosition& rPos ) const;
443 // Build a bookmark table, which is sort by the node position. The
444 // OtherPos of the bookmarks also inserted.
445 void CreateBookmarkTable();
446 // Search all Bookmarks in the range and return it in the Array.
447 bool GetBookmarks( const SwContentNode& rNd,
448 sal_Int32 nStt, sal_Int32 nEnd,
449 std::vector< const ::sw::mark::IMark* >& rArr );
451 // Create new PaM at position.
452 static std::shared_ptr<SwUnoCursor> NewUnoCursor(SwDoc & rDoc,
453 sal_uLong const nStartIdx, sal_uLong const nEndIdx);
455 // If applicable copy a local file into internet.
456 bool CopyLocalFileToINet( OUString& rFileNm );
458 // Stream-specific routines. Do not use in storage-writer!
460 // Optimizing output on stream.
461 static SvStream& OutLong( SvStream& rStrm, tools::Long nVal );
462 static SvStream& OutULong( SvStream& rStrm, sal_uLong nVal );
464 void SetStream(SvStream *const pStream);
465 SvStream& Strm();
467 void SetOrganizerMode( bool bSet ) { m_bOrganizerMode = bSet; }
470 typedef tools::SvRef<Writer> WriterRef;
472 // Base class for all storage writers.
473 class SW_DLLPUBLIC StgWriter : public Writer
475 protected:
476 tools::SvRef<SotStorage> m_pStg;
477 css::uno::Reference < css::embed::XStorage > m_xStg;
479 // Create error at call.
480 virtual ErrCode WriteStream() override;
481 virtual ErrCode WriteStorage() = 0;
482 virtual ErrCode WriteMedium( SfxMedium& ) = 0;
484 using Writer::Write;
486 public:
487 StgWriter() : Writer() {}
489 virtual bool IsStgWriter() const override;
491 virtual ErrCode Write( SwPaM&, const css::uno::Reference < css::embed::XStorage >&, const OUString*, SfxMedium* = nullptr ) override;
492 virtual ErrCode Write( SwPaM&, SotStorage&, const OUString* ) override;
494 SotStorage& GetStorage() const { return *m_pStg; }
497 // Interface class for general access on special writers.
499 class SW_DLLPUBLIC SwWriter
501 SvStream* m_pStrm;
502 css::uno::Reference < css::embed::XStorage > m_xStg;
503 SfxMedium* m_pMedium;
505 SwPaM* m_pOutPam;
506 SwCursorShell *m_pShell;
507 SwDoc &m_rDoc;
509 bool m_bWriteAll;
511 public:
512 ErrCode Write( WriterRef const & rxWriter, const OUString* = nullptr);
514 SwWriter( SvStream&, SwCursorShell &, bool bWriteAll = false );
515 SwWriter( SvStream&, SwDoc & );
516 SwWriter( SvStream&, SwPaM &, bool bWriteAll = false );
518 SwWriter( const css::uno::Reference < css::embed::XStorage >&, SwDoc& );
520 SwWriter( SfxMedium&, SwCursorShell &, bool bWriteAll );
521 SwWriter( SfxMedium&, SwDoc & );
524 typedef Reader* (*FnGetReader)();
525 typedef void (*FnGetWriter)(const OUString&, const OUString& rBaseURL, WriterRef&);
526 ErrCode SaveOrDelMSVBAStorage( SfxObjectShell&, SotStorage&, bool, const OUString& );
527 ErrCode GetSaveWarningOfMSVBAStorage( SfxObjectShell &rDocS );
529 struct SwReaderWriterEntry
531 Reader* pReader;
532 FnGetReader fnGetReader;
533 FnGetWriter fnGetWriter;
534 bool bDelReader;
536 SwReaderWriterEntry( const FnGetReader fnReader, const FnGetWriter fnWriter, bool bDel )
537 : pReader( nullptr ), fnGetReader( fnReader ), fnGetWriter( fnWriter ), bDelReader( bDel )
540 /// Get access to the reader.
541 Reader* GetReader();
543 /// Get access to the writer.
544 void GetWriter( const OUString& rNm, const OUString& rBaseURL, WriterRef& xWrt ) const;
547 namespace SwReaderWriter
549 SW_DLLPUBLIC Reader* GetRtfReader();
550 SW_DLLPUBLIC Reader* GetDOCXReader();
552 /// Return reader based on the name.
553 Reader* GetReader( const OUString& rFltName );
555 /// Return writer based on the name.
556 SW_DLLPUBLIC void GetWriter( const OUString& rFltName, const OUString& rBaseURL, WriterRef& xWrt );
559 void GetRTFWriter( const OUString&, const OUString&, WriterRef& );
560 void GetASCWriter(const OUString&, const OUString&, WriterRef&);
561 void GetHTMLWriter( const OUString&, const OUString&, WriterRef& );
562 void GetXMLWriter( const OUString&, const OUString&, WriterRef& );
564 #endif
566 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */