sfx2/inc/autoredactdialog.hxx | 25 +++ sfx2/source/doc/autoredactdialog.cxx | 231 +++++++++++++++++++++++++++++++---- 2 files changed, 229 insertions(+), 27 deletions(-)
New commits: commit 1c15e0e889ccd37cf91a049511589195651636df Author: Muhammet Kara <muhammet.k...@collabora.com> AuthorDate: Tue Jun 11 15:20:55 2019 +0300 Commit: Muhammet Kara <muhammet.k...@collabora.com> CommitDate: Mon Jun 17 23:30:32 2019 +0200 Auto redaction dialog 5th iteration * Add the Load handler, and the related bits * The dialog can be considered fully functional now, as all buttons are working. * Next is to handle its output in the redaction phase. Change-Id: Idd558e13b50f82a95f5f6e226bc855257837b351 Reviewed-on: https://gerrit.libreoffice.org/73822 Tested-by: Jenkins Reviewed-by: Muhammet Kara <muhammet.k...@collabora.com> Reviewed-on: https://gerrit.libreoffice.org/74230 Tested-by: Muhammet Kara <muhammet.k...@collabora.com> diff --git a/sfx2/inc/autoredactdialog.hxx b/sfx2/inc/autoredactdialog.hxx index 99c956246d69..2336d66c4063 100644 --- a/sfx2/inc/autoredactdialog.hxx +++ b/sfx2/inc/autoredactdialog.hxx @@ -124,6 +124,10 @@ class SFX2_DLLPUBLIC SfxAutoRedactDialog : public SfxDialogController DECL_LINK(SaveHdl, sfx2::FileDialogHelper*, void); void StartFileDialog(StartFileDialogType nType, const OUString& rTitle); + /// Carry out proper addition both to the targets box, and to the tabletargets vector. + void addTarget(RedactionTarget* pTarget); + /// Clear all targets both visually and from the targets vector + void clearTargets(); public: SfxAutoRedactDialog(weld::Window* pParent); diff --git a/sfx2/source/doc/autoredactdialog.cxx b/sfx2/source/doc/autoredactdialog.cxx index 7c414cfc3189..7025baa81576 100644 --- a/sfx2/source/doc/autoredactdialog.cxx +++ b/sfx2/source/doc/autoredactdialog.cxx @@ -185,7 +185,6 @@ void TargetsTable::setRowData(const int& nRowIndex, const RedactionTarget* pTarg IMPL_LINK_NOARG(SfxAutoRedactDialog, Load, weld::Button&, void) { - //TODO: Implement //Load a targets list from a previously saved file (a json file?) // ask for filename, where we should load the new config data from StartFileDialog(StartFileDialogType::Open, "Load Targets"); @@ -193,7 +192,6 @@ IMPL_LINK_NOARG(SfxAutoRedactDialog, Load, weld::Button&, void) IMPL_LINK_NOARG(SfxAutoRedactDialog, Save, weld::Button&, void) { - //TODO: Implement //Allow saving the targets into a file StartFileDialog(StartFileDialogType::SaveAs, "Save Targets"); } @@ -365,15 +363,70 @@ boost::property_tree::ptree redactionTargetToJSON(RedactionTarget* pTarget) return aNode; } + +RedactionTarget* JSONtoRedactionTarget(const boost::property_tree::ptree::value_type& rValue) +{ + OUString sName = OUString::fromUtf8(rValue.second.get<std::string>("sName").c_str()); + RedactionTargetType eType + = static_cast<RedactionTargetType>(atoi(rValue.second.get<std::string>("sName").c_str())); + OUString sContent = OUString::fromUtf8(rValue.second.get<std::string>("sContent").c_str()); + bool bCaseSensitive + = OUString::fromUtf8(rValue.second.get<std::string>("bCaseSensitive").c_str()).toBoolean(); + bool bWholeWords + = OUString::fromUtf8(rValue.second.get<std::string>("bWholeWords").c_str()).toBoolean(); + sal_uInt32 nID = atoi(rValue.second.get<std::string>("nID").c_str()); + + RedactionTarget* pTarget + = new RedactionTarget({ sName, eType, sContent, bCaseSensitive, bWholeWords, nID }); + + return pTarget; +} } IMPL_LINK_NOARG(SfxAutoRedactDialog, LoadHdl, sfx2::FileDialogHelper*, void) { - //TODO: Implement - bool bDummy = hasTargets(); + assert(m_pFileDlg); + + OUString sTargetsFile; + if (ERRCODE_NONE == m_pFileDlg->GetError()) + sTargetsFile = m_pFileDlg->GetPath(); + + if (sTargetsFile.isEmpty()) + return; + + OUString sSysPath; + osl::File::getSystemPathFromFileURL(sTargetsFile, sSysPath); + sTargetsFile = sSysPath; + + weld::WaitObject aWaitObject(getDialog()); + + try + { + // Create path string, and read JSON from file + std::string sPathStr(OUStringToOString(sTargetsFile, RTL_TEXTENCODING_UTF8).getStr()); + + boost::property_tree::ptree aTargetsJSON; + + boost::property_tree::read_json(sPathStr, aTargetsJSON); + + // Clear the dialog + clearTargets(); - if (bDummy) - void(); + // Recreate & add the targets to the dialog + for (const boost::property_tree::ptree::value_type& rValue : + aTargetsJSON.get_child("RedactionTargets")) + { + RedactionTarget* pTarget = JSONtoRedactionTarget(rValue); + addTarget(pTarget); + } + } + catch (css::uno::Exception& e) + { + SAL_WARN("sfx.doc", + "Exception caught while trying to load the targets JSON from file: " << e.Message); + return; + //TODO: Warn the user with a message box + } } IMPL_LINK_NOARG(SfxAutoRedactDialog, SaveHdl, sfx2::FileDialogHelper*, void) @@ -441,6 +494,38 @@ void SfxAutoRedactDialog::StartFileDialog(StartFileDialogType nType, const OUStr m_pFileDlg->StartExecuteModal(aDlgClosedLink); } +void SfxAutoRedactDialog::addTarget(RedactionTarget* pTarget) +{ + // Only the visual/display part + m_xTargetsBox->InsertTarget(pTarget); + + // Actually add to the targets vector + if (m_xTargetsBox->GetTargetByName(pTarget->sName)) + m_aTableTargets.emplace_back(pTarget, pTarget->sName); + else + { + std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog( + getDialog(), VclMessageType::Warning, VclButtonsType::Ok, + "An error occurred while adding new target. Please report this incident.")); + xBox->run(); + delete pTarget; + } +} + +void SfxAutoRedactDialog::clearTargets() +{ + // Clear the targets box + m_xTargetsBox->clear(); + + // Clear the targets vector + auto delTarget + = [](const std::pair<RedactionTarget*, OUString>& targetPair) { delete targetPair.first; }; + + std::for_each(m_aTableTargets.begin(), m_aTableTargets.end(), delTarget); + + m_aTableTargets.clear(); +} + SfxAutoRedactDialog::SfxAutoRedactDialog(weld::Window* pParent) : SfxDialogController(pParent, "sfx/ui/autoredactdialog.ui", "AutoRedactDialog") , m_xRedactionTargetsLabel(m_xBuilder->weld_label("labelRedactionTargets")) commit 5a4b2c8d2f2df4e1bb532baad632fd2a348d7ef8 Author: Muhammet Kara <muhammet.k...@collabora.com> AuthorDate: Sat Jun 8 23:27:56 2019 +0300 Commit: Muhammet Kara <muhammet.k...@collabora.com> CommitDate: Mon Jun 17 23:30:18 2019 +0200 Auto redaction dialog 4th iteration * Add the Save & SaveHdl handlers * Add stubs for Load & LoadHdl handlers Change-Id: I5f58213c86e99f8bfc9075e04eedbb5cb546d9ad Reviewed-on: https://gerrit.libreoffice.org/73724 Tested-by: Jenkins Reviewed-by: Muhammet Kara <muhammet.k...@collabora.com> Reviewed-on: https://gerrit.libreoffice.org/74229 Tested-by: Muhammet Kara <muhammet.k...@collabora.com> diff --git a/sfx2/inc/autoredactdialog.hxx b/sfx2/inc/autoredactdialog.hxx index 267427c90b21..99c956246d69 100644 --- a/sfx2/inc/autoredactdialog.hxx +++ b/sfx2/inc/autoredactdialog.hxx @@ -89,10 +89,22 @@ public: //void connect_row_activated(const Link<weld::TreeView&, void>& rLink) { m_xControl->connect_row_activated(rLink); } }; +namespace sfx2 +{ +class FileDialogHelper; +} + +enum class StartFileDialogType +{ + Open, + SaveAs +}; + class SFX2_DLLPUBLIC SfxAutoRedactDialog : public SfxDialogController { SfxObjectShellLock m_xDocShell; std::vector<std::pair<RedactionTarget*, OUString>> m_aTableTargets; + std::unique_ptr<sfx2::FileDialogHelper> m_pFileDlg; std::unique_ptr<weld::Label> m_xRedactionTargetsLabel; std::unique_ptr<TargetsTable> m_xTargetsBox; @@ -102,12 +114,17 @@ class SFX2_DLLPUBLIC SfxAutoRedactDialog : public SfxDialogController std::unique_ptr<weld::Button> m_xEditBtn; std::unique_ptr<weld::Button> m_xDeleteBtn; - /*DECL_LINK(LoadHdl, weld::Button&, void); - DECL_LINK(SaveHdl, weld::Button&, void);*/ + DECL_LINK(Load, weld::Button&, void); + DECL_LINK(Save, weld::Button&, void); DECL_LINK(AddHdl, weld::Button&, void); DECL_LINK(EditHdl, weld::Button&, void); DECL_LINK(DeleteHdl, weld::Button&, void); + DECL_LINK(LoadHdl, sfx2::FileDialogHelper*, void); + DECL_LINK(SaveHdl, sfx2::FileDialogHelper*, void); + + void StartFileDialog(StartFileDialogType nType, const OUString& rTitle); + public: SfxAutoRedactDialog(weld::Window* pParent); virtual ~SfxAutoRedactDialog() override; diff --git a/sfx2/source/doc/autoredactdialog.cxx b/sfx2/source/doc/autoredactdialog.cxx index 266859a129ec..7c414cfc3189 100644 --- a/sfx2/source/doc/autoredactdialog.cxx +++ b/sfx2/source/doc/autoredactdialog.cxx @@ -7,31 +7,34 @@ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ -#include <osl/file.hxx> #include <autoredactdialog.hxx> -#include <vcl/layout.hxx> -#include <vcl/idle.hxx> -#include <vcl/gdimtf.hxx> -#include <svl/itemset.hxx> -#include <svl/eitem.hxx> -#include <svtools/sfxecode.hxx> -#include <svtools/ehdl.hxx> -#include <tools/urlobj.hxx> -#include <tools/debug.hxx> -#include <sfx2/strings.hrc> -#include <sfx2/sfxsids.hrc> +#include <preview.hxx> #include <sfx2/app.hxx> +#include <sfx2/docfile.hxx> +#include <sfx2/filedlghelper.hxx> #include <sfx2/objsh.hxx> #include <sfx2/sfxresid.hxx> -#include <sfx2/docfile.hxx> -#include <preview.hxx> -#include <sfx2/printer.hxx> -#include <unotools/viewoptions.hxx> +#include <sfx2/sfxsids.hrc> +#include <sfx2/strings.hrc> + +#include <osl/file.hxx> +#include <sal/log.hxx> +#include <svl/eitem.hxx> +#include <svl/itemset.hxx> +#include <svtools/ehdl.hxx> +#include <svtools/sfxecode.hxx> +#include <vcl/idle.hxx> +#include <vcl/layout.hxx> #include <vcl/waitobj.hxx> #include <vcl/weld.hxx> +#include <tools/debug.hxx> +#include <tools/urlobj.hxx> +#include <unotools/viewoptions.hxx> -#include <sal/log.hxx> +#include <com/sun/star/ui/dialogs/TemplateDescription.hpp> + +#include <boost/property_tree/json_parser.hpp> int TargetsTable::GetRowByTargetName(const OUString& sName) { @@ -180,17 +183,20 @@ void TargetsTable::setRowData(const int& nRowIndex, const RedactionTarget* pTarg m_xControl->set_text(nRowIndex, pTarget->bWholeWords ? OUString("Yes") : OUString("No"), 4); } -/*IMPL_LINK_NOARG(SfxAutoRedactDialog, LoadHdl, weld::Button&, void) +IMPL_LINK_NOARG(SfxAutoRedactDialog, Load, weld::Button&, void) { //TODO: Implement - //Load a targets list from a previously saved file (a json file in the user profile dir?) + //Load a targets list from a previously saved file (a json file?) + // ask for filename, where we should load the new config data from + StartFileDialog(StartFileDialogType::Open, "Load Targets"); } -IMPL_LINK_NOARG(SfxAutoRedactDialog, SaveHdl, weld::Button&, void) +IMPL_LINK_NOARG(SfxAutoRedactDialog, Save, weld::Button&, void) { //TODO: Implement //Allow saving the targets into a file -}*/ + StartFileDialog(StartFileDialogType::SaveAs, "Save Targets"); +} IMPL_LINK_NOARG(SfxAutoRedactDialog, AddHdl, weld::Button&, void) { @@ -345,6 +351,96 @@ IMPL_LINK_NOARG(SfxAutoRedactDialog, DeleteHdl, weld::Button&, void) } } +namespace +{ +boost::property_tree::ptree redactionTargetToJSON(RedactionTarget* pTarget) +{ + boost::property_tree::ptree aNode; + aNode.put("sName", pTarget->sName.toUtf8().getStr()); + aNode.put("eType", pTarget->sType); + aNode.put("sContent", pTarget->sContent.toUtf8().getStr()); + aNode.put("bWholeWords", pTarget->bWholeWords); + aNode.put("bCaseSensitive", pTarget->bCaseSensitive); + aNode.put("nID", pTarget->nID); + + return aNode; +} +} + +IMPL_LINK_NOARG(SfxAutoRedactDialog, LoadHdl, sfx2::FileDialogHelper*, void) +{ + //TODO: Implement + bool bDummy = hasTargets(); + + if (bDummy) + void(); +} + +IMPL_LINK_NOARG(SfxAutoRedactDialog, SaveHdl, sfx2::FileDialogHelper*, void) +{ + assert(m_pFileDlg); + + OUString sTargetsFile; + if (ERRCODE_NONE == m_pFileDlg->GetError()) + sTargetsFile = m_pFileDlg->GetPath(); + + if (sTargetsFile.isEmpty()) + return; + + OUString sSysPath; + osl::File::getSystemPathFromFileURL(sTargetsFile, sSysPath); + sTargetsFile = sSysPath; + + weld::WaitObject aWaitObject(getDialog()); + + try + { + // Put the targets into a JSON array + boost::property_tree::ptree aTargetsArray; + for (const auto& targetPair : m_aTableTargets) + { + aTargetsArray.push_back(std::make_pair("", redactionTargetToJSON(targetPair.first))); + } + + // Build the JSON tree + boost::property_tree::ptree aTargetsTree; + aTargetsTree.add_child("RedactionTargets", aTargetsArray); + + // Create path string, and write JSON to file + std::string sPathStr(OUStringToOString(sTargetsFile, RTL_TEXTENCODING_UTF8).getStr()); + + boost::property_tree::write_json(sPathStr, aTargetsTree); + } + catch (css::uno::Exception& e) + { + SAL_WARN("sfx.doc", + "Exception caught while trying to save the targets JSON to file: " << e.Message); + return; + //TODO: Warn the user with a message box + } +} + +void SfxAutoRedactDialog::StartFileDialog(StartFileDialogType nType, const OUString& rTitle) +{ + OUString aFilterAllStr(SfxResId(STR_SFX_FILTERNAME_ALL)); + OUString aFilterCfgStr("Target Set (*.json)"); + + bool bSave = nType == StartFileDialogType::SaveAs; + short nDialogType = bSave ? css::ui::dialogs::TemplateDescription::FILESAVE_AUTOEXTENSION + : css::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE; + m_pFileDlg.reset(new sfx2::FileDialogHelper(nDialogType, FileDialogFlags::NONE, getDialog())); + + m_pFileDlg->SetTitle(rTitle); + m_pFileDlg->AddFilter(aFilterAllStr, FILEDIALOG_FILTER_ALL); + m_pFileDlg->AddFilter(aFilterCfgStr, "*.json"); + m_pFileDlg->SetCurrentFilter(aFilterCfgStr); + + Link<sfx2::FileDialogHelper*, void> aDlgClosedLink + = bSave ? LINK(this, SfxAutoRedactDialog, SaveHdl) + : LINK(this, SfxAutoRedactDialog, LoadHdl); + m_pFileDlg->StartExecuteModal(aDlgClosedLink); +} + SfxAutoRedactDialog::SfxAutoRedactDialog(weld::Window* pParent) : SfxDialogController(pParent, "sfx/ui/autoredactdialog.ui", "AutoRedactDialog") , m_xRedactionTargetsLabel(m_xBuilder->weld_label("labelRedactionTargets")) @@ -374,8 +470,8 @@ SfxAutoRedactDialog::SfxAutoRedactDialog(weld::Window* pParent) // TODO: fill the targets box // Handler connections - //m_xLoadBtn->connect_clicked(LINK(this, SfxAutoRedactDialog, LoadHdl)); - //m_xSaveBtn->connect_clicked(LINK(this, SfxAutoRedactDialog, SaveHdl)); + m_xLoadBtn->connect_clicked(LINK(this, SfxAutoRedactDialog, Load)); + m_xSaveBtn->connect_clicked(LINK(this, SfxAutoRedactDialog, Save)); m_xAddBtn->connect_clicked(LINK(this, SfxAutoRedactDialog, AddHdl)); m_xEditBtn->connect_clicked(LINK(this, SfxAutoRedactDialog, EditHdl)); m_xDeleteBtn->connect_clicked(LINK(this, SfxAutoRedactDialog, DeleteHdl)); _______________________________________________ Libreoffice-commits mailing list libreoffice-comm...@lists.freedesktop.org https://lists.freedesktop.org/mailman/listinfo/libreoffice-commits