1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
2 /*
3  * This file is part of the LibreOffice project.
4  *
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/.
8  */
9 
10 #ifndef INCLUDED_CUI_SOURCE_INC_SFXREDACTIONHELPER_HXX
11 #define INCLUDED_CUI_SOURCE_INC_SFXREDACTIONHELPER_HXX
12 
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>
17 
18 #include <sal/types.h>
19 #include <rtl/ustring.hxx>
20 #include <tools/gen.hxx>
21 
22 #include <string_view>
23 #include <vector>
24 
25 using namespace ::com::sun::star;
26 using namespace ::com::sun::star::lang;
27 using namespace ::com::sun::star::uno;
28 
29 class SfxRequest;
30 class SfxStringItem;
31 class GDIMetaFile;
32 class DocumentToGraphicRenderer;
33 class SfxViewFrame;
34 struct RedactionTarget;
35 
36 namespace i18nutil
37 {
38 struct SearchOptions2;
39 }
40 
41 struct PageMargins
42 {
43     // Page margins in mm100th
44     sal_Int32 nTop;
45     sal_Int32 nBottom;
46     sal_Int32 nLeft;
47     sal_Int32 nRight;
48 };
49 
50 /*
51  * Mostly a bunch of static methods to handle the redaction functionality at
52  * different points of the process.
53  **/
54 class SfxRedactionHelper
55 {
56 public:
57     /// Checks to see if the request has a parameter of IsRedactMode:bool=true
58     static bool isRedactMode(const SfxRequest& rReq);
59     /*
60      * Returns the value of the given string param as an OUString
61      * Returns empty OUString if no param
62      * */
63     static OUString getStringParam(const SfxRequest& rReq, sal_uInt16 nParamId);
64     /*
65      * Creates metafiles from the pages of the given document,
66      * and pushes into the given vector.
67      * */
68     static void getPageMetaFilesFromDoc(std::vector<GDIMetaFile>& aMetaFiles,
69                                         std::vector<::Size>& aPageSizes, sal_Int32 nPages,
70                                         DocumentToGraphicRenderer& aRenderer);
71     /*
72      * Creates one shape and one draw page for each gdimetafile,
73      * and inserts the shapes into the newly created draw pages.
74      * */
75     static void
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,
80                    bool bIsAutoRedact);
81     /*
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.
85      * */
86     static void showRedactionToolbar(const SfxViewFrame* pViewFrame);
87 
88     /*
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.
91      * */
92     static PageMargins
93     getPageMarginsForWriter(const css::uno::Reference<css::frame::XModel>& xModel);
94 
95     /*
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.
98      * */
99     static PageMargins getPageMarginsForCalc(const css::uno::Reference<css::frame::XModel>& xModel);
100 
101     /*
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.
104      * */
105     static void searchInMetaFile(const RedactionTarget& rRedactionTarget, const GDIMetaFile& rMtf,
106                                  std::vector<tools::Rectangle>& aRedactionRectangles,
107                                  const uno::Reference<XComponent>& xComponent);
108 
109     /*
110      * Draws a redaction rectangle on the draw page referenced with its page number (0-based)
111      * */
112     static void addRedactionRectToPage(const uno::Reference<XComponent>& xComponent,
113                                        const uno::Reference<drawing::XDrawPage>& xPage,
114                                        const std::vector<tools::Rectangle>& aNewRectangles);
115 
116     /*
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.
119      * */
120     static void autoRedactPage(const RedactionTarget& rRedactionTarget,
121                                const GDIMetaFile& rGDIMetaFile,
122                                const uno::Reference<drawing::XDrawPage>& xPage,
123                                const uno::Reference<XComponent>& xComponent);
124 
125     /// Fill the search options based on the given redaction target
126     static void fillSearchOptions(i18nutil::SearchOptions2& rSearchOpt,
127                                   const RedactionTarget& rTarget);
128 
129 private:
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)
141     };
142 };
143 
144 #endif // INCLUDED_CUI_SOURCE_INC_SFXREDACTIONHELPER_HXX
145 
146 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
147