1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
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/.
10 #ifndef INCLUDED_CUI_SOURCE_INC_SFXREDACTIONHELPER_HXX
11 #define INCLUDED_CUI_SOURCE_INC_SFXREDACTIONHELPER_HXX
13 #include <com/sun/star/uno/Reference.hxx>
14 #include <com/sun/star/lang/XComponent.hpp>
15 #include <com/sun/star/frame/XModel.hpp>
16 #include <com/sun/star/drawing/XDrawPage.hpp>
18 #include <sal/types.h>
19 #include <rtl/ustring.hxx>
20 #include <tools/gen.hxx>
22 #include <string_view>
25 using namespace ::com::sun::star
;
26 using namespace ::com::sun::star::lang
;
27 using namespace ::com::sun::star::uno
;
32 class DocumentToGraphicRenderer
;
34 struct RedactionTarget
;
38 struct SearchOptions2
;
43 // Page margins in mm100th
51 * Mostly a bunch of static methods to handle the redaction functionality at
52 * different points of the process.
54 class SfxRedactionHelper
57 /// Checks to see if the request has a parameter of IsRedactMode:bool=true
58 static bool isRedactMode(const SfxRequest
& rReq
);
60 * Returns the value of the given string param as an OUString
61 * Returns empty OUString if no param
63 static OUString
getStringParam(const SfxRequest
& rReq
, sal_uInt16 nParamId
);
65 * Creates metafiles from the pages of the given document,
66 * and pushes into the given vector.
68 static void getPageMetaFilesFromDoc(std::vector
<GDIMetaFile
>& aMetaFiles
,
69 std::vector
<::Size
>& aPageSizes
, sal_Int32 nPages
,
70 DocumentToGraphicRenderer
& aRenderer
);
72 * Creates one shape and one draw page for each gdimetafile,
73 * and inserts the shapes into the newly created draw pages.
76 addPagesToDraw(const uno::Reference
<XComponent
>& xComponent
, sal_Int32 nPages
,
77 const std::vector
<GDIMetaFile
>& aMetaFiles
,
78 const std::vector
<::Size
>& aPageSizes
, const PageMargins
& aPageMargins
,
79 const std::vector
<std::pair
<RedactionTarget
, OUString
>>& r_aTableTargets
,
82 * Makes the Redaction toolbar visible to the user.
83 * Meant to be called after converting a document to a Draw doc
84 * for redaction purposes.
86 static void showRedactionToolbar(const SfxViewFrame
* pViewFrame
);
89 * Used to get the page margins from the original/source Writer document. Then we apply these values to the
90 * pages inserted into Draw for redaction.
93 getPageMarginsForWriter(const css::uno::Reference
<css::frame::XModel
>& xModel
);
96 * Used to get the page margins from the original/source Calc document. Then we apply these values to the
97 * pages inserted into Draw for redaction.
99 static PageMargins
getPageMarginsForCalc(const css::uno::Reference
<css::frame::XModel
>& xModel
);
102 * Used to find the text portions to be redacted. Returns a list of rectangles to cover those
103 * areas to be redacted. Probably the most crucial part of the auto-redaction process.
105 static void searchInMetaFile(const RedactionTarget
& rRedactionTarget
, const GDIMetaFile
& rMtf
,
106 std::vector
<tools::Rectangle
>& aRedactionRectangles
,
107 const uno::Reference
<XComponent
>& xComponent
);
110 * Draws a redaction rectangle on the draw page referenced with its page number (0-based)
112 static void addRedactionRectToPage(const uno::Reference
<XComponent
>& xComponent
,
113 const uno::Reference
<drawing::XDrawPage
>& xPage
,
114 const std::vector
<tools::Rectangle
>& aNewRectangles
);
117 * Search for the given term through the gdimetafile, which has the whole content of a draw page,
118 * and draw redaction rectangles to the appropriate positions with suitable sizes.
120 static void autoRedactPage(const RedactionTarget
& rRedactionTarget
,
121 const GDIMetaFile
& rGDIMetaFile
,
122 const uno::Reference
<drawing::XDrawPage
>& xPage
,
123 const uno::Reference
<XComponent
>& xComponent
);
125 /// Fill the search options based on the given redaction target
126 static void fillSearchOptions(i18nutil::SearchOptions2
& rSearchOpt
,
127 const RedactionTarget
& rTarget
);
130 static constexpr std::u16string_view m_aPredefinedTargets
[6] = {
131 u
"\\b(?:\\d[ -]*?){13,16}\\b", //Credit card numbers
132 u
"\\b[a-zA-Z0-9._%+-]+@[a-zA-Z0-9.-]+\\.[a-zA-Z]{2,}\\b", //Email addresses
133 u
"\\b(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
134 "\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\\.(25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)"
135 "\\b", //IP addresses
136 u
"([12]\\d{3}[./-](0[1-9]|1[0-2])[./"
137 "-](0[1-9]|[12]\\d|3[01]))|((0[1-9]|[12]\\d|3[01])[./-](0[1-9]|1[0-2])[./"
138 "-][12]\\d{3})", //Dates (numerical)
139 u
"\\s*[a-zA-Z]{2}(?:\\s*\\d\\s*){6}[a-zA-Z]?\\s*", //National Insurance Number (UK)
140 u
"([1-9])(?!\\1{2}-\\1{2}-\\1{4})[1-9]{2}-[1-9]{2}-[1-9]{4}" //Social Security Number (US)
144 #endif // INCLUDED_CUI_SOURCE_INC_SFXREDACTIONHELPER_HXX
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */