/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */ /* * This file is part of the LibreOffice project. * * This Source Code Form is subject to the terms of the Mozilla Public * License, v. 2.0. If a copy of the MPL was not distributed with this * file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
switch (nType)
{ case RedactionTargetType::REDACTION_TARGET_TEXT:
sTypeName = SfxResId(STR_REDACTION_TARGET_TYPE_TEXT); break; case RedactionTargetType::REDACTION_TARGET_REGEX:
sTypeName = SfxResId(STR_REDACTION_TARGET_TYPE_REGEX); break; case RedactionTargetType::REDACTION_TARGET_PREDEFINED:
sTypeName = SfxResId(STR_REDACTION_TARGET_TYPE_PREDEF); break; case RedactionTargetType::REDACTION_TARGET_IMAGE:
sTypeName = SfxResId(STR_REDACTION_TARGET_TYPE_IMAGE); break; case RedactionTargetType::REDACTION_TARGET_UNKNOWN:
sTypeName = SfxResId(STR_REDACTION_TARGET_TYPE_UNKNOWN); break;
}
return sTypeName;
}
/// Returns TypeID to be used in the add/edit target dialog
OUString getTypeID(RedactionTargetType nType)
{
OUString sTypeID(u"unknown"_ustr);
switch (nType)
{ case RedactionTargetType::REDACTION_TARGET_TEXT:
sTypeID = "text"; break; case RedactionTargetType::REDACTION_TARGET_REGEX:
sTypeID = "regex"; break; case RedactionTargetType::REDACTION_TARGET_PREDEFINED:
sTypeID = "predefined"; break; case RedactionTargetType::REDACTION_TARGET_IMAGE:
sTypeID = "image"; break; case RedactionTargetType::REDACTION_TARGET_UNKNOWN:
sTypeID = "unknown"; break;
}
return sTypeID;
}
}
void TargetsTable::InsertTarget(RedactionTarget* pTarget)
{ if (!pTarget)
{
SAL_WARN("sfx.doc", "pTarget is null in TargetsTable::InsertTarget()"); return;
}
// Check if the name is empty or invalid (clashing with another entry's name) if (pTarget->sName.isEmpty() || GetRowByTargetName(pTarget->sName) != -1)
{
pTarget->sName = GetNameProposal();
}
IMPL_LINK_NOARG(SfxAutoRedactDialog, Load, weld::Button&, void)
{ //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, SfxResId(STR_REDACTION_LOAD_TARGETS));
}
IMPL_LINK_NOARG(SfxAutoRedactDialog, Save, weld::Button&, void)
{ //Allow saving the targets into a file
StartFileDialog(StartFileDialogType::SaveAs, SfxResId(STR_REDACTION_SAVE_TARGETS));
}
IMPL_LINK_NOARG(SfxAutoRedactDialog, AddHdl, weld::Button&, void)
{ // Open the Add Target dialog, create a new target and insert into the targets vector and the listbox
SfxAddTargetDialog aAddTargetDialog(getDialog(), m_aTargetsBox.GetNameProposal());
//Alright, we now have everything we need to construct a new target
RedactionTarget* redactiontarget = new RedactionTarget(
{ aAddTargetDialog.getName(), aAddTargetDialog.getType(), aAddTargetDialog.getContent(),
aAddTargetDialog.isCaseSensitive(), aAddTargetDialog.isWholeWords(), 0 });
// Only the visual/display part
m_aTargetsBox.InsertTarget(redactiontarget);
// Actually add to the targets vector if (m_aTargetsBox.GetTargetByName(redactiontarget->sName))
m_aTableTargets.emplace_back(redactiontarget, redactiontarget->sName); else
{
std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(
getDialog(), VclMessageType::Warning, VclButtonsType::Ok,
SfxResId(STR_REDACTION_TARGET_ADD_ERROR)));
xBox->run(); delete redactiontarget;
}
}
// No selection, nothing to edit if (nSelectedRow < 0) return;
// Only one entry should be selected for editing if (m_aTargetsBox.get_selected_rows().size() > 1)
{ //Warn the user about multiple selections
std::unique_ptr<weld::MessageDialog> xBox(
Application::CreateMessageDialog(getDialog(), VclMessageType::Error, VclButtonsType::Ok,
SfxResId(STR_REDACTION_MULTI_EDIT)));
xBox->run(); return;
}
// Get the redaction target to be edited
RedactionTarget* pTarget = weld::fromId<RedactionTarget*>(m_aTargetsBox.get_id(nSelectedRow));
// Construct and run the edit target dialog
SfxAddTargetDialog aEditTargetDialog(getDialog(), pTarget->sName, pTarget->sType,
pTarget->sContent, pTarget->bCaseSensitive,
pTarget->bWholeWords);
// And sync the targets box row with the actual target data
m_aTargetsBox.setRowData(nSelectedRow, pTarget);
}
IMPL_LINK_NOARG(SfxAutoRedactDialog, DoubleClickEditHdl, weld::TreeView&, bool)
{ if (m_xEditBtn->get_sensitive())
m_xEditBtn->clicked(); returntrue;
}
IMPL_LINK_NOARG(SfxAutoRedactDialog, DeleteHdl, weld::Button&, void)
{
std::vector<int> aSelectedRows = m_aTargetsBox.get_selected_rows();
//No selection, so nothing to delete if (aSelectedRows.empty()) return;
if (aSelectedRows.size() > 1)
{
OUString sMsg(SfxResId(STR_REDACTION_MULTI_DELETE)
.replaceFirst("$(TARGETSCOUNT)", OUString::number(aSelectedRows.size()))); //Warn the user about multiple deletions
std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(
getDialog(), VclMessageType::Question, VclButtonsType::OkCancel, sMsg)); if (xBox->run() == RET_CANCEL) return;
}
// After each delete, the indexes of the following items decrease by one. int delta = 0; for (constauto& i : aSelectedRows)
{
m_aTableTargets.erase(m_aTableTargets.begin() + (i - delta));
m_aTargetsBox.remove(i - delta++);
}
}
// Recreate & add the targets to the dialog for (const boost::property_tree::ptree::value_type& rValue :
aTargetsJSON.get_child("RedactionTargets"))
{
addTarget(JSONtoRedactionTarget(rValue));
}
} 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
}
}
try
{ // Put the targets into a JSON array
boost::property_tree::ptree aTargetsArray; for (constauto& targetPair : m_aTableTargets)
{
aTargetsArray.push_back(
std::make_pair("", redactionTargetToJSON(targetPair.first.get())));
}
// 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));
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::addTarget(std::unique_ptr<RedactionTarget> pTarget)
{ // Only the visual/display part
m_aTargetsBox.InsertTarget(pTarget.get());
// Actually add to the targets vector auto name = pTarget->sName; if (m_aTargetsBox.GetTargetByName(name))
m_aTableTargets.emplace_back(std::move(pTarget), name); else
{
std::unique_ptr<weld::MessageDialog> xBox(Application::CreateMessageDialog(
getDialog(), VclMessageType::Warning, VclButtonsType::Ok,
SfxResId(STR_REDACTION_TARGET_ADD_ERROR)));
xBox->run();
}
}
void SfxAutoRedactDialog::clearTargets()
{ // Clear the targets box
m_aTargetsBox.clear();
// Clear the targets vector
m_aTableTargets.clear();
}
SfxAutoRedactDialog::SfxAutoRedactDialog(weld::Window* pParent)
: SfxDialogController(pParent, u"sfx/ui/autoredactdialog.ui"_ustr, u"AutoRedactDialog"_ustr)
, m_bIsValidState(true)
, m_bTargetsCopied(false)
, m_aTargetsBox(m_xBuilder->weld_tree_view(u"targets"_ustr))
, m_xLoadBtn(m_xBuilder->weld_button(u"btnLoadTargets"_ustr))
, m_xSaveBtn(m_xBuilder->weld_button(u"btnSaveTargets"_ustr))
, m_xAddBtn(m_xBuilder->weld_button(u"add"_ustr))
, m_xEditBtn(m_xBuilder->weld_button(u"edit"_ustr))
, m_xDeleteBtn(m_xBuilder->weld_button(u"delete"_ustr))
{ // Can be used to remember the last set of redaction targets?
OUString sExtraData;
SvtViewOptions aDlgOpt(EViewType::Dialog, m_xDialog->get_help_id());
// Recreate & add the targets to the dialog for (const boost::property_tree::ptree::value_type& rValue :
aTargetsJSON.get_child("RedactionTargets"))
{
addTarget(JSONtoRedactionTarget(rValue));
}
} catch (css::uno::Exception& e)
{
SAL_WARN("sfx.doc", "Exception caught while trying to load the last dialog state: " << e.Message); return; //TODO: Warn the user with a message box
}
}
SfxAutoRedactDialog::~SfxAutoRedactDialog()
{ if (m_aTableTargets.empty())
{ // Clear the dialog data
SvtViewOptions aDlgOpt(EViewType::Dialog, m_xDialog->get_help_id());
aDlgOpt.Delete(); return;
}
try
{ // Put the targets into a JSON array
boost::property_tree::ptree aTargetsArray; for (constauto& targetPair : m_aTableTargets)
{
aTargetsArray.push_back(
std::make_pair("", redactionTargetToJSON(targetPair.first.get())));
}
// Build the JSON tree
boost::property_tree::ptree aTargetsTree;
aTargetsTree.add_child("RedactionTargets", aTargetsArray);
std::stringstream aStream;
// Store the dialog data
SvtViewOptions aDlgOpt(EViewType::Dialog, m_xDialog->get_help_id());
aDlgOpt.SetUserItem(u"UserItem"_ustr, css::uno::Any(sUserDataStr));
if (!m_bTargetsCopied)
clearTargets();
} catch (css::uno::Exception& e)
{
SAL_WARN("sfx.doc", "Exception caught while trying to store the dialog state: " << e.Message); return; //TODO: Warn the user with a message box
}
}
bool SfxAutoRedactDialog::hasTargets() const
{ //TODO: Add also some validity checks? if (m_aTableTargets.empty()) returnfalse;
returntrue;
}
bool SfxAutoRedactDialog::getTargets(std::vector<std::pair<RedactionTarget, OUString>>& r_aTargets)
{ if (m_aTableTargets.empty()) returntrue;
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);
} elseif (m_xType->get_active_id() == "image")
{
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);
Die Informationen auf dieser Webseite wurden
nach bestem Wissen sorgfältig zusammengestellt. Es wird jedoch weder Vollständigkeit, noch Richtigkeit,
noch Qualität der bereit gestellten Informationen zugesichert.
Bemerkung:
Die farbliche Syntaxdarstellung und die Messung sind noch experimentell.