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_SFX2_INC_AUTOREDACTDIALOG_HXX
11 #define INCLUDED_SFX2_INC_AUTOREDACTDIALOG_HXX
12 
13 #include <memory>
14 #include <sal/config.h>
15 #include <sfx2/dllapi.h>
16 #include <sfx2/basedlgs.hxx>
17 #include <sfx2/objsh.hxx>
18 
19 #include <vcl/idle.hxx>
20 #include <o3tl/typed_flags_set.hxx>
21 
22 namespace weld
23 {
24 class Button;
25 }
26 namespace weld
27 {
28 class ComboBox;
29 }
30 namespace weld
31 {
32 class Label;
33 }
34 namespace weld
35 {
36 class Window;
37 }
38 namespace weld
39 {
40 class TreeView;
41 }
42 
43 enum RedactionTargetType
44 {
45     REDACTION_TARGET_TEXT,
46     REDACTION_TARGET_REGEX,
47     REDACTION_TARGET_PREDEFINED,
48     REDACTION_TARGET_UNKNOWN
49 };
50 
51 /// Keeps information for a single redaction target
52 struct RedactionTarget
53 {
54     OUString sName;
55     RedactionTargetType sType;
56     OUString sContent;
57     bool bCaseSensitive;
58     bool bWholeWords;
59     sal_uInt32 nID;
60 };
61 
62 /// Used to display the targets list
63 class TargetsTable
64 {
65     std::unique_ptr<weld::TreeView> m_xControl;
66     int GetRowByTargetName(const OUString& sName);
67 
68 public:
69     TargetsTable(std::unique_ptr<weld::TreeView> xControl);
70     void InsertTarget(RedactionTarget* pTarget);
71     void SelectByName(const OUString& sName);
72     RedactionTarget* GetTargetByName(const OUString& sName);
73     OUString GetNameProposal() const;
74 
unselect_all()75     void unselect_all() { m_xControl->unselect_all(); }
has_focus() const76     bool has_focus() const { return m_xControl->has_focus(); }
n_children() const77     int n_children() const { return m_xControl->n_children(); }
get_selected_index() const78     int get_selected_index() const { return m_xControl->get_selected_index(); }
get_selected_rows() const79     std::vector<int> get_selected_rows() const { return m_xControl->get_selected_rows(); }
clear()80     void clear() { m_xControl->clear(); }
remove(int nRow)81     void remove(int nRow) { m_xControl->remove(nRow); }
select(int nRow)82     void select(int nRow) { m_xControl->select(nRow); }
get_id(int nRow) const83     OUString get_id(int nRow) const { return m_xControl->get_id(nRow); }
84 
85     // Sync data on the targets box with the data on the target
86     void setRowData(int nRowIndex, const RedactionTarget* pTarget);
87 
88     //void connect_changed(const Link<weld::TreeView&, void>& rLink) { m_xControl->connect_changed(rLink); }
89     //void connect_row_activated(const Link<weld::TreeView&, void>& rLink) { m_xControl->connect_row_activated(rLink); }
90 };
91 
92 namespace sfx2
93 {
94 class FileDialogHelper;
95 }
96 
97 enum class StartFileDialogType
98 {
99     Open,
100     SaveAs
101 };
102 
103 class SfxAutoRedactDialog : public SfxDialogController
104 {
105     SfxObjectShellLock m_xDocShell;
106     std::vector<std::pair<RedactionTarget*, OUString>> m_aTableTargets;
107     std::unique_ptr<sfx2::FileDialogHelper> m_pFileDlg;
108     bool m_bIsValidState;
109     bool m_bTargetsCopied;
110 
111     std::unique_ptr<weld::Label> m_xRedactionTargetsLabel;
112     std::unique_ptr<TargetsTable> m_xTargetsBox;
113     std::unique_ptr<weld::Button> m_xLoadBtn;
114     std::unique_ptr<weld::Button> m_xSaveBtn;
115     std::unique_ptr<weld::Button> m_xAddBtn;
116     std::unique_ptr<weld::Button> m_xEditBtn;
117     std::unique_ptr<weld::Button> m_xDeleteBtn;
118 
119     DECL_LINK(Load, weld::Button&, void);
120     DECL_LINK(Save, weld::Button&, void);
121     DECL_LINK(AddHdl, weld::Button&, void);
122     DECL_LINK(EditHdl, weld::Button&, void);
123     DECL_LINK(DeleteHdl, weld::Button&, void);
124 
125     DECL_LINK(LoadHdl, sfx2::FileDialogHelper*, void);
126     DECL_LINK(SaveHdl, sfx2::FileDialogHelper*, void);
127 
128     void StartFileDialog(StartFileDialogType nType, const OUString& rTitle);
129     /// Carry out proper addition both to the targets box, and to the tabletargets vector.
130     void addTarget(RedactionTarget* pTarget);
131     /// Clear all targets both visually and from the targets vector
132     void clearTargets();
133 
134 public:
135     SfxAutoRedactDialog(weld::Window* pParent);
136     virtual ~SfxAutoRedactDialog() override;
137 
138     /// Check if the dialog has any valid redaction targets.
139     bool hasTargets() const;
140     /// Check if the dialog is in a valid state.
isValidState() const141     bool isValidState() const { return m_bIsValidState; }
142     /** Copies targets vector
143      *  Does a shallow copy.
144      *  Returns true if successful.
145      */
146     bool getTargets(std::vector<std::pair<RedactionTarget*, OUString>>& r_aTargets);
147 };
148 
149 class SfxAddTargetDialog : public weld::GenericDialogController
150 {
151 private:
152     std::unique_ptr<weld::Entry> m_xName;
153     std::unique_ptr<weld::ComboBox> m_xType;
154     std::unique_ptr<weld::Label> m_xLabelContent;
155     std::unique_ptr<weld::Entry> m_xContent;
156     std::unique_ptr<weld::Label> m_xLabelPredefContent;
157     std::unique_ptr<weld::ComboBox> m_xPredefContent;
158     std::unique_ptr<weld::CheckButton> m_xCaseSensitive;
159     std::unique_ptr<weld::CheckButton> m_xWholeWords;
160 
161     DECL_LINK(SelectTypeHdl, weld::ComboBox&, void);
162 
163 public:
164     SfxAddTargetDialog(weld::Window* pWindow, const OUString& rName);
165     SfxAddTargetDialog(weld::Window* pWindow, const OUString& sName,
166                        const RedactionTargetType& eTargetType, const OUString& sContent,
167                        bool bCaseSensitive, bool bWholeWords);
168 
getName() const169     OUString getName() const { return m_xName->get_text(); }
170     RedactionTargetType getType() const;
171     OUString getContent() const;
isCaseSensitive() const172     bool isCaseSensitive() const
173     {
174         return m_xCaseSensitive->get_state() == TriState::TRISTATE_TRUE;
175     }
isWholeWords() const176     bool isWholeWords() const { return m_xWholeWords->get_state() == TriState::TRISTATE_TRUE; }
177 };
178 
179 #endif
180 
181 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */
182