From b61a1ca837223ba9d7da1aa8936f228d89bde60a Mon Sep 17 00:00:00 2001 From: Muhammet Kara Date: Sat, 15 Jun 2019 12:08:35 +0300 Subject: Add predefined targets to auto redaction Change-Id: Ib8cf8b50944667d6a87a5cafb6995ad195699358 Reviewed-on: https://gerrit.libreoffice.org/74092 Tested-by: Jenkins Reviewed-by: Muhammet Kara --- sfx2/inc/SfxRedactionHelper.hxx | 14 ++++++ sfx2/inc/autoredactdialog.hxx | 7 ++- sfx2/source/doc/SfxRedactionHelper.cxx | 17 +++++-- sfx2/source/doc/autoredactdialog.cxx | 89 ++++++++++++++++++++++++++++++++-- sfx2/uiconfig/ui/addtargetdialog.ui | 52 ++++++++++++++++++-- 5 files changed, 167 insertions(+), 12 deletions(-) (limited to 'sfx2') diff --git a/sfx2/inc/SfxRedactionHelper.hxx b/sfx2/inc/SfxRedactionHelper.hxx index 2f470386d2dc..ae72c39f4886 100644 --- a/sfx2/inc/SfxRedactionHelper.hxx +++ b/sfx2/inc/SfxRedactionHelper.hxx @@ -120,6 +120,20 @@ public: /// Fill the search options based on the given redaction target static void fillSearchOptions(i18nutil::SearchOptions2& rSearchOpt, const RedactionTarget* pTarget); + +private: + static constexpr OUStringLiteral m_aPredefinedTargets[6] = { + "\\b(?:\\d[ -]*?){13,16}\\b", //Credit card numbers + "\\b[A-Z0-9._%+-]+@[A-Z0-9.-]+\\.[A-Z]{2,}\\b", //Email addresses + "\\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]?)" + "\\.(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]?)" + "\\b", //IP addresses + "([12]\\d{3}[./-](0[1-9]|1[0-2])[./" + "-](0[1-9]|[12]\\d|3[01]))|((0[1-9]|[12]\\d|3[01])[./-](0[1-9]|1[0-2])[./" + "-][12]\\d{3})", //Dates (numerical) + "\\s*[a-zA-Z]{2}(?:\\s*\\d\\s*){6}[a-zA-Z]?\\s*", //National Insurance Number (UK) + "([1-9])(?!\\1{2}-\\1{2}-\\1{4})[1-9]{2}-[1-9]{2}-[1-9]{4}" //Social Security Number (US) + }; }; #endif // INCLUDED_CUI_SOURCE_INC_SFXREDACTIONHELPER_HXX diff --git a/sfx2/inc/autoredactdialog.hxx b/sfx2/inc/autoredactdialog.hxx index 3b6b45c99032..bb633b9a9bb1 100644 --- a/sfx2/inc/autoredactdialog.hxx +++ b/sfx2/inc/autoredactdialog.hxx @@ -155,10 +155,15 @@ class SfxAddTargetDialog : public weld::GenericDialogController private: std::unique_ptr m_xName; std::unique_ptr m_xType; + std::unique_ptr m_xLabelContent; std::unique_ptr m_xContent; + std::unique_ptr m_xLabelPredefContent; + std::unique_ptr m_xPredefContent; std::unique_ptr m_xCaseSensitive; std::unique_ptr m_xWholeWords; + DECL_LINK(SelectTypeHdl, weld::ComboBox&, void); + public: SfxAddTargetDialog(weld::Window* pWindow, const OUString& rName); SfxAddTargetDialog(weld::Window* pWindow, const OUString& sName, @@ -167,7 +172,7 @@ public: OUString getName() const { return m_xName->get_text(); } RedactionTargetType getType() const; - OUString getContent() const { return m_xContent->get_text(); } + OUString getContent() const; bool isCaseSensitive() const { return m_xCaseSensitive->get_state() == TriState::TRISTATE_TRUE; diff --git a/sfx2/source/doc/SfxRedactionHelper.cxx b/sfx2/source/doc/SfxRedactionHelper.cxx index b6c3264d769a..efd06b14fccd 100644 --- a/sfx2/source/doc/SfxRedactionHelper.cxx +++ b/sfx2/source/doc/SfxRedactionHelper.cxx @@ -533,7 +533,8 @@ void SfxRedactionHelper::fillSearchOptions(i18nutil::SearchOptions2& rSearchOpt, return; } - if (pTarget->sType == RedactionTargetType::REDACTION_TARGET_REGEX) + if (pTarget->sType == RedactionTargetType::REDACTION_TARGET_REGEX + || pTarget->sType == RedactionTargetType::REDACTION_TARGET_PREDEFINED) { rSearchOpt.algorithmType = util::SearchAlgorithms_REGEXP; rSearchOpt.AlgorithmType2 = util::SearchAlgorithms2::REGEXP; @@ -545,10 +546,20 @@ void SfxRedactionHelper::fillSearchOptions(i18nutil::SearchOptions2& rSearchOpt, } rSearchOpt.Locale = GetAppLanguageTag().getLocale(); - rSearchOpt.searchString = pTarget->sContent; + if (pTarget->sType == RedactionTargetType::REDACTION_TARGET_PREDEFINED) + { + sal_Int32 nPredefIndex = pTarget->sContent.getToken(0, ';').toInt32(); + //sal_Int32 nPredefIndex = sContent.toInt32(); + + rSearchOpt.searchString = m_aPredefinedTargets[nPredefIndex]; + } + else + rSearchOpt.searchString = pTarget->sContent; + rSearchOpt.replaceString.clear(); - if (!pTarget->bCaseSensitive && pTarget->sType != RedactionTargetType::REDACTION_TARGET_REGEX) + if (!pTarget->bCaseSensitive && pTarget->sType != RedactionTargetType::REDACTION_TARGET_REGEX + && pTarget->sType != RedactionTargetType::REDACTION_TARGET_PREDEFINED) rSearchOpt.transliterateFlags |= TransliterationFlags::IGNORE_CASE; if (pTarget->bWholeWords) rSearchOpt.searchFlag |= util::SearchFlags::NORM_WORD_ONLY; diff --git a/sfx2/source/doc/autoredactdialog.cxx b/sfx2/source/doc/autoredactdialog.cxx index 3ec9872eb380..2a11a2a6e63a 100644 --- a/sfx2/source/doc/autoredactdialog.cxx +++ b/sfx2/source/doc/autoredactdialog.cxx @@ -128,11 +128,19 @@ void TargetsTable::InsertTarget(RedactionTarget* pTarget) pTarget->sName = GetNameProposal(); } + OUString sContent = pTarget->sContent; + + if (pTarget->sType == RedactionTargetType::REDACTION_TARGET_PREDEFINED) + { + //selection_num;selection_name + sContent = sContent.getToken(1, ';'); + } + // Add to the end int nRow = m_xControl->n_children(); m_xControl->append(OUString::number(reinterpret_cast(pTarget)), pTarget->sName); m_xControl->set_text(nRow, getTypeName(pTarget->sType), 1); - m_xControl->set_text(nRow, pTarget->sContent, 2); + m_xControl->set_text(nRow, sContent, 2); m_xControl->set_text(nRow, pTarget->bCaseSensitive ? OUString("Yes") : OUString("No"), 3); m_xControl->set_text(nRow, pTarget->bWholeWords ? OUString("Yes") : OUString("No"), 4); } @@ -176,9 +184,17 @@ OUString TargetsTable::GetNameProposal() void TargetsTable::setRowData(const int& nRowIndex, const RedactionTarget* pTarget) { + OUString sContent = pTarget->sContent; + + if (pTarget->sType == RedactionTargetType::REDACTION_TARGET_PREDEFINED) + { + //selection_num;selection_name + sContent = sContent.getToken(1, ';'); + } + m_xControl->set_text(nRowIndex, pTarget->sName, 0); m_xControl->set_text(nRowIndex, getTypeName(pTarget->sType), 1); - m_xControl->set_text(nRowIndex, pTarget->sContent, 2); + m_xControl->set_text(nRowIndex, sContent, 2); m_xControl->set_text(nRowIndex, pTarget->bCaseSensitive ? OUString("Yes") : OUString("No"), 3); m_xControl->set_text(nRowIndex, pTarget->bWholeWords ? OUString("Yes") : OUString("No"), 4); } @@ -600,16 +616,61 @@ bool SfxAutoRedactDialog::moveTargets( return true; } +IMPL_LINK_NOARG(SfxAddTargetDialog, SelectTypeHdl, weld::ComboBox&, void) +{ + if (m_xType->get_active_id() == "predefined") + { + // Hide the usual content widgets + // We will just set the id as content + // And handle with proper regex in the SfxRedactionHelper + m_xLabelContent->set_sensitive(false); + m_xLabelContent->set_visible(false); + m_xContent->set_sensitive(false); + m_xContent->set_visible(false); + m_xWholeWords->set_sensitive(false); + m_xWholeWords->set_visible(false); + m_xCaseSensitive->set_sensitive(false); + m_xCaseSensitive->set_visible(false); + + // And show the predefined targets + m_xLabelPredefContent->set_sensitive(true); + m_xLabelPredefContent->set_visible(true); + m_xPredefContent->set_sensitive(true); + m_xPredefContent->set_visible(true); + } + else + { + m_xLabelPredefContent->set_sensitive(false); + m_xLabelPredefContent->set_visible(false); + m_xPredefContent->set_sensitive(false); + m_xPredefContent->set_visible(false); + + m_xLabelContent->set_sensitive(true); + m_xLabelContent->set_visible(true); + m_xContent->set_sensitive(true); + m_xContent->set_visible(true); + m_xWholeWords->set_sensitive(true); + m_xWholeWords->set_visible(true); + m_xCaseSensitive->set_sensitive(true); + m_xCaseSensitive->set_visible(true); + } +} + SfxAddTargetDialog::SfxAddTargetDialog(weld::Window* pParent, const OUString& rName) : GenericDialogController(pParent, "sfx/ui/addtargetdialog.ui", "AddTargetDialog") , m_xName(m_xBuilder->weld_entry("name")) , m_xType(m_xBuilder->weld_combo_box("type")) + , m_xLabelContent(m_xBuilder->weld_label("label_content")) , m_xContent(m_xBuilder->weld_entry("content")) + , m_xLabelPredefContent(m_xBuilder->weld_label("label_content_predef")) + , m_xPredefContent(m_xBuilder->weld_combo_box("content_predef")) , m_xCaseSensitive(m_xBuilder->weld_check_button("checkboxCaseSensitive")) , m_xWholeWords(m_xBuilder->weld_check_button("checkboxWholeWords")) { m_xName->set_text(rName); m_xName->select_region(0, rName.getLength()); + + m_xType->connect_changed(LINK(this, SfxAddTargetDialog, SelectTypeHdl)); } SfxAddTargetDialog::SfxAddTargetDialog(weld::Window* pParent, const OUString& sName, @@ -619,7 +680,10 @@ SfxAddTargetDialog::SfxAddTargetDialog(weld::Window* pParent, const OUString& sN : GenericDialogController(pParent, "sfx/ui/addtargetdialog.ui", "AddTargetDialog") , m_xName(m_xBuilder->weld_entry("name")) , m_xType(m_xBuilder->weld_combo_box("type")) + , m_xLabelContent(m_xBuilder->weld_label("label_content")) , m_xContent(m_xBuilder->weld_entry("content")) + , m_xLabelPredefContent(m_xBuilder->weld_label("label_content_predef")) + , m_xPredefContent(m_xBuilder->weld_combo_box("content_predef")) , m_xCaseSensitive(m_xBuilder->weld_check_button("checkboxCaseSensitive")) , m_xWholeWords(m_xBuilder->weld_check_button("checkboxWholeWords")) { @@ -628,7 +692,15 @@ SfxAddTargetDialog::SfxAddTargetDialog(weld::Window* pParent, const OUString& sN m_xType->set_active_id(getTypeID(eTargetType)); - m_xContent->set_text(sContent); + if (eTargetType == RedactionTargetType::REDACTION_TARGET_PREDEFINED) + { + SelectTypeHdl(*m_xPredefContent); + m_xPredefContent->set_active(sContent.getToken(0, ';').toInt32()); + } + else + { + m_xContent->set_text(sContent); + } m_xCaseSensitive->set_active(bCaseSensitive); m_xWholeWords->set_active(bWholeWords); @@ -650,4 +722,15 @@ RedactionTargetType SfxAddTargetDialog::getType() const return RedactionTargetType::REDACTION_TARGET_UNKNOWN; } +OUString SfxAddTargetDialog::getContent() const +{ + if (m_xType->get_active_id() == "predefined") + { + return OUString(OUString::number(m_xPredefContent->get_active()) + ";" + + m_xPredefContent->get_active_text()); + } + + return m_xContent->get_text(); +} + /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */ diff --git a/sfx2/uiconfig/ui/addtargetdialog.ui b/sfx2/uiconfig/ui/addtargetdialog.ui index 3d45c7a240bd..d4283222fb65 100644 --- a/sfx2/uiconfig/ui/addtargetdialog.ui +++ b/sfx2/uiconfig/ui/addtargetdialog.ui @@ -24,8 +24,8 @@ False - - gtk-cancel + + gtk-ok True True True @@ -39,8 +39,8 @@ - - gtk-ok + + gtk-cancel True True True @@ -145,6 +145,7 @@ True True 2 + 2 True @@ -159,6 +160,9 @@ True False + 2 + 2 + 0 Text Regex @@ -173,6 +177,44 @@ 1 + + + False + False + Content: + True + + + + + + 0 + 3 + + + + + False + False + 2 + 0 + + Credit card numbers + Email addresses + IP addresses + Dates (Numerical) + National Insurance Number (UK) + Social Security Number (US) + + + + + + + 1 + 3 + + False @@ -227,8 +269,8 @@ - cancel close + cancel help -- cgit