1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
3 * This file is part of the LibreOffice project.
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/.
10 #include <autoredactdialog.hxx>
12 #include <sfx2/filedlghelper.hxx>
13 #include <sfx2/sfxresid.hxx>
14 #include <sfx2/strings.hrc>
16 #include <osl/file.hxx>
17 #include <sal/log.hxx>
18 #include <vcl/svapp.hxx>
19 #include <vcl/weld.hxx>
20 #include <unotools/viewoptions.hxx>
21 #include <o3tl/string_view.hxx>
23 #include <com/sun/star/ui/dialogs/TemplateDescription.hpp>
25 #include <boost/property_tree/json_parser.hpp>
27 constexpr OUStringLiteral FILEDIALOG_FILTER_JSON
= u
"*.json";
29 int TargetsTable::GetRowByTargetName(std::u16string_view sName
)
31 for (int i
= 0, nCount
= m_xControl
->n_children(); i
< nCount
; ++i
)
33 RedactionTarget
* pTarget
= weld::fromId
<RedactionTarget
*>(m_xControl
->get_id(i
));
34 if (pTarget
->sName
== sName
)
42 TargetsTable::TargetsTable(std::unique_ptr
<weld::TreeView
> xControl
)
43 : m_xControl(std::move(xControl
))
45 m_xControl
->set_size_request(555, 250);
46 std::vector
<int> aWidths
{ 100, 50, 200, 105, 105 };
47 m_xControl
->set_column_fixed_widths(aWidths
);
48 m_xControl
->set_selection_mode(SelectionMode::Multiple
);
53 OUString
getTypeName(RedactionTargetType nType
)
55 OUString
sTypeName(SfxResId(STR_REDACTION_TARGET_TYPE_UNKNOWN
));
59 case RedactionTargetType::REDACTION_TARGET_TEXT
:
60 sTypeName
= SfxResId(STR_REDACTION_TARGET_TYPE_TEXT
);
62 case RedactionTargetType::REDACTION_TARGET_REGEX
:
63 sTypeName
= SfxResId(STR_REDACTION_TARGET_TYPE_REGEX
);
65 case RedactionTargetType::REDACTION_TARGET_PREDEFINED
:
66 sTypeName
= SfxResId(STR_REDACTION_TARGET_TYPE_PREDEF
);
68 case RedactionTargetType::REDACTION_TARGET_UNKNOWN
:
69 sTypeName
= SfxResId(STR_REDACTION_TARGET_TYPE_UNKNOWN
);
76 /// Returns TypeID to be used in the add/edit target dialog
77 OUString
getTypeID(RedactionTargetType nType
)
79 OUString
sTypeID(u
"unknown"_ustr
);
83 case RedactionTargetType::REDACTION_TARGET_TEXT
:
86 case RedactionTargetType::REDACTION_TARGET_REGEX
:
89 case RedactionTargetType::REDACTION_TARGET_PREDEFINED
:
90 sTypeID
= "predefined";
92 case RedactionTargetType::REDACTION_TARGET_UNKNOWN
:
101 void TargetsTable::InsertTarget(RedactionTarget
* pTarget
)
105 SAL_WARN("sfx.doc", "pTarget is null in TargetsTable::InsertTarget()");
109 // Check if the name is empty or invalid (clashing with another entry's name)
110 if (pTarget
->sName
.isEmpty() || GetRowByTargetName(pTarget
->sName
) != -1)
112 pTarget
->sName
= GetNameProposal();
115 OUString sContent
= pTarget
->sContent
;
117 if (pTarget
->sType
== RedactionTargetType::REDACTION_TARGET_PREDEFINED
)
119 //selection_num;selection_name
120 sContent
= sContent
.getToken(1, ';');
124 int nRow
= m_xControl
->n_children();
125 m_xControl
->append(weld::toId(pTarget
), pTarget
->sName
);
126 m_xControl
->set_text(nRow
, getTypeName(pTarget
->sType
), 1);
127 m_xControl
->set_text(nRow
, sContent
, 2);
128 m_xControl
->set_text(
129 nRow
, pTarget
->bCaseSensitive
? SfxResId(STR_REDACTION_YES
) : SfxResId(STR_REDACTION_NO
),
131 m_xControl
->set_text(
132 nRow
, pTarget
->bWholeWords
? SfxResId(STR_REDACTION_YES
) : SfxResId(STR_REDACTION_NO
), 4);
135 RedactionTarget
* TargetsTable::GetTargetByName(std::u16string_view sName
)
137 int nEntry
= GetRowByTargetName(sName
);
141 return weld::fromId
<RedactionTarget
*>(m_xControl
->get_id(nEntry
));
144 OUString
TargetsTable::GetNameProposal() const
146 OUString
sDefaultTargetName(SfxResId(STR_REDACTION_TARGET
));
147 sal_Int32 nHighestTargetId
= 0;
148 for (int i
= 0, nCount
= m_xControl
->n_children(); i
< nCount
; ++i
)
150 RedactionTarget
* pTarget
= weld::fromId
<RedactionTarget
*>(m_xControl
->get_id(i
));
151 const OUString
& sName
= pTarget
->sName
;
152 sal_Int32 nIndex
= 0;
153 if (o3tl::getToken(sName
, 0, ' ', nIndex
) == sDefaultTargetName
)
155 sal_Int32 nCurrTargetId
= o3tl::toInt32(o3tl::getToken(sName
, 0, ' ', nIndex
));
156 nHighestTargetId
= std::max
<sal_Int32
>(nHighestTargetId
, nCurrTargetId
);
159 return sDefaultTargetName
+ " " + OUString::number(nHighestTargetId
+ 1);
162 void TargetsTable::setRowData(int nRowIndex
, const RedactionTarget
* pTarget
)
164 OUString sContent
= pTarget
->sContent
;
166 if (pTarget
->sType
== RedactionTargetType::REDACTION_TARGET_PREDEFINED
)
168 //selection_num;selection_name
169 sContent
= sContent
.getToken(1, ';');
172 m_xControl
->set_text(nRowIndex
, pTarget
->sName
, 0);
173 m_xControl
->set_text(nRowIndex
, getTypeName(pTarget
->sType
), 1);
174 m_xControl
->set_text(nRowIndex
, sContent
, 2);
175 m_xControl
->set_text(
177 pTarget
->bCaseSensitive
? SfxResId(STR_REDACTION_YES
) : SfxResId(STR_REDACTION_NO
), 3);
178 m_xControl
->set_text(
179 nRowIndex
, pTarget
->bWholeWords
? SfxResId(STR_REDACTION_YES
) : SfxResId(STR_REDACTION_NO
),
183 IMPL_LINK_NOARG(SfxAutoRedactDialog
, Load
, weld::Button
&, void)
185 //Load a targets list from a previously saved file (a json file?)
186 // ask for filename, where we should load the new config data from
187 StartFileDialog(StartFileDialogType::Open
, SfxResId(STR_REDACTION_LOAD_TARGETS
));
190 IMPL_LINK_NOARG(SfxAutoRedactDialog
, Save
, weld::Button
&, void)
192 //Allow saving the targets into a file
193 StartFileDialog(StartFileDialogType::SaveAs
, SfxResId(STR_REDACTION_SAVE_TARGETS
));
196 IMPL_LINK_NOARG(SfxAutoRedactDialog
, AddHdl
, weld::Button
&, void)
198 // Open the Add Target dialog, create a new target and insert into the targets vector and the listbox
199 SfxAddTargetDialog
aAddTargetDialog(getDialog(), m_aTargetsBox
.GetNameProposal());
206 if (aAddTargetDialog
.run() != RET_OK
)
209 if (aAddTargetDialog
.getName().isEmpty()
210 || aAddTargetDialog
.getType() == RedactionTargetType::REDACTION_TARGET_UNKNOWN
211 || aAddTargetDialog
.getContent().isEmpty())
214 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(
215 getDialog(), VclMessageType::Warning
, VclButtonsType::Ok
,
216 SfxResId(STR_REDACTION_FIELDS_REQUIRED
)));
219 else if (m_aTargetsBox
.GetTargetByName(aAddTargetDialog
.getName()))
222 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(
223 getDialog(), VclMessageType::Warning
, VclButtonsType::Ok
,
224 SfxResId(STR_REDACTION_TARGET_NAME_CLASH
)));
228 } while (bIncomplete
);
230 //Alright, we now have everything we need to construct a new target
231 RedactionTarget
* redactiontarget
= new RedactionTarget(
232 { aAddTargetDialog
.getName(), aAddTargetDialog
.getType(), aAddTargetDialog
.getContent(),
233 aAddTargetDialog
.isCaseSensitive(), aAddTargetDialog
.isWholeWords(), 0 });
235 // Only the visual/display part
236 m_aTargetsBox
.InsertTarget(redactiontarget
);
238 // Actually add to the targets vector
239 if (m_aTargetsBox
.GetTargetByName(redactiontarget
->sName
))
240 m_aTableTargets
.emplace_back(redactiontarget
, redactiontarget
->sName
);
243 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(
244 getDialog(), VclMessageType::Warning
, VclButtonsType::Ok
,
245 SfxResId(STR_REDACTION_TARGET_ADD_ERROR
)));
247 delete redactiontarget
;
251 IMPL_LINK_NOARG(SfxAutoRedactDialog
, EditHdl
, weld::Button
&, void)
253 sal_Int32 nSelectedRow
= m_aTargetsBox
.get_selected_index();
255 // No selection, nothing to edit
256 if (nSelectedRow
< 0)
259 // Only one entry should be selected for editing
260 if (m_aTargetsBox
.get_selected_rows().size() > 1)
262 //Warn the user about multiple selections
263 std::unique_ptr
<weld::MessageDialog
> xBox(
264 Application::CreateMessageDialog(getDialog(), VclMessageType::Error
, VclButtonsType::Ok
,
265 SfxResId(STR_REDACTION_MULTI_EDIT
)));
270 // Get the redaction target to be edited
271 RedactionTarget
* pTarget
= weld::fromId
<RedactionTarget
*>(m_aTargetsBox
.get_id(nSelectedRow
));
273 // Construct and run the edit target dialog
274 SfxAddTargetDialog
aEditTargetDialog(getDialog(), pTarget
->sName
, pTarget
->sType
,
275 pTarget
->sContent
, pTarget
->bCaseSensitive
,
276 pTarget
->bWholeWords
);
283 if (aEditTargetDialog
.run() != RET_OK
)
286 if (aEditTargetDialog
.getName().isEmpty()
287 || aEditTargetDialog
.getType() == RedactionTargetType::REDACTION_TARGET_UNKNOWN
288 || aEditTargetDialog
.getContent().isEmpty())
291 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(
292 getDialog(), VclMessageType::Warning
, VclButtonsType::Ok
,
293 SfxResId(STR_REDACTION_FIELDS_REQUIRED
)));
296 else if (aEditTargetDialog
.getName() != pTarget
->sName
297 && m_aTargetsBox
.GetTargetByName(aEditTargetDialog
.getName()))
300 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(
301 getDialog(), VclMessageType::Warning
, VclButtonsType::Ok
,
302 SfxResId(STR_REDACTION_TARGET_NAME_CLASH
)));
306 } while (bIncomplete
);
308 // Update the redaction target
309 pTarget
->sName
= aEditTargetDialog
.getName();
310 pTarget
->sType
= aEditTargetDialog
.getType();
311 pTarget
->sContent
= aEditTargetDialog
.getContent();
312 pTarget
->bCaseSensitive
= aEditTargetDialog
.isCaseSensitive();
313 pTarget
->bWholeWords
= aEditTargetDialog
.isWholeWords();
315 // And sync the targets box row with the actual target data
316 m_aTargetsBox
.setRowData(nSelectedRow
, pTarget
);
318 IMPL_LINK_NOARG(SfxAutoRedactDialog
, DoubleClickEditHdl
, weld::TreeView
&, bool)
320 if (m_xEditBtn
->get_sensitive())
321 m_xEditBtn
->clicked();
324 IMPL_LINK_NOARG(SfxAutoRedactDialog
, DeleteHdl
, weld::Button
&, void)
326 std::vector
<int> aSelectedRows
= m_aTargetsBox
.get_selected_rows();
328 //No selection, so nothing to delete
329 if (aSelectedRows
.empty())
332 if (aSelectedRows
.size() > 1)
334 OUString
sMsg(SfxResId(STR_REDACTION_MULTI_DELETE
)
335 .replaceFirst("$(TARGETSCOUNT)", OUString::number(aSelectedRows
.size())));
336 //Warn the user about multiple deletions
337 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(
338 getDialog(), VclMessageType::Question
, VclButtonsType::OkCancel
, sMsg
));
339 if (xBox
->run() == RET_CANCEL
)
343 // After each delete, the indexes of the following items decrease by one.
345 for (const auto& i
: aSelectedRows
)
347 m_aTableTargets
.erase(m_aTableTargets
.begin() + (i
- delta
));
348 m_aTargetsBox
.remove(i
- delta
++);
354 boost::property_tree::ptree
redactionTargetToJSON(const RedactionTarget
* pTarget
)
356 boost::property_tree::ptree aNode
;
357 aNode
.put("sName", pTarget
->sName
.toUtf8().getStr());
358 aNode
.put("eType", pTarget
->sType
);
359 aNode
.put("sContent", pTarget
->sContent
.toUtf8().getStr());
360 aNode
.put("bWholeWords", pTarget
->bWholeWords
);
361 aNode
.put("bCaseSensitive", pTarget
->bCaseSensitive
);
362 aNode
.put("nID", pTarget
->nID
);
367 std::unique_ptr
<RedactionTarget
>
368 JSONtoRedactionTarget(const boost::property_tree::ptree::value_type
& rValue
)
370 OUString sName
= OUString::fromUtf8(rValue
.second
.get
<std::string
>("sName"));
371 RedactionTargetType eType
372 = static_cast<RedactionTargetType
>(atoi(rValue
.second
.get
<std::string
>("eType").c_str()));
373 OUString sContent
= OUString::fromUtf8(rValue
.second
.get
<std::string
>("sContent"));
375 = OUString::fromUtf8(rValue
.second
.get
<std::string
>("bCaseSensitive")).toBoolean();
377 = OUString::fromUtf8(rValue
.second
.get
<std::string
>("bWholeWords")).toBoolean();
378 sal_uInt32 nID
= atoi(rValue
.second
.get
<std::string
>("nID").c_str());
380 return std::unique_ptr
<RedactionTarget
>(
381 new RedactionTarget
{ sName
, eType
, sContent
, bCaseSensitive
, bWholeWords
, nID
});
385 IMPL_LINK_NOARG(SfxAutoRedactDialog
, LoadHdl
, sfx2::FileDialogHelper
*, void)
389 OUString sTargetsFile
;
390 if (ERRCODE_NONE
== m_pFileDlg
->GetError())
391 sTargetsFile
= m_pFileDlg
->GetPath();
393 if (sTargetsFile
.isEmpty())
397 osl::File::getSystemPathFromFileURL(sTargetsFile
, sSysPath
);
398 sTargetsFile
= sSysPath
;
400 weld::WaitObject
aWaitObject(getDialog());
404 // Create path string, and read JSON from file
405 std::string
sPathStr(OUStringToOString(sTargetsFile
, RTL_TEXTENCODING_UTF8
));
407 boost::property_tree::ptree aTargetsJSON
;
409 boost::property_tree::read_json(sPathStr
, aTargetsJSON
);
414 // Recreate & add the targets to the dialog
415 for (const boost::property_tree::ptree::value_type
& rValue
:
416 aTargetsJSON
.get_child("RedactionTargets"))
418 addTarget(JSONtoRedactionTarget(rValue
));
421 catch (css::uno::Exception
& e
)
424 "Exception caught while trying to load the targets JSON from file: " << e
.Message
);
426 //TODO: Warn the user with a message box
430 IMPL_LINK_NOARG(SfxAutoRedactDialog
, SaveHdl
, sfx2::FileDialogHelper
*, void)
434 OUString sTargetsFile
;
435 if (ERRCODE_NONE
== m_pFileDlg
->GetError())
436 sTargetsFile
= m_pFileDlg
->GetPath();
438 if (sTargetsFile
.isEmpty())
442 osl::File::getSystemPathFromFileURL(sTargetsFile
, sSysPath
);
443 sTargetsFile
= sSysPath
;
445 weld::WaitObject
aWaitObject(getDialog());
449 // Put the targets into a JSON array
450 boost::property_tree::ptree aTargetsArray
;
451 for (const auto& targetPair
: m_aTableTargets
)
453 aTargetsArray
.push_back(
454 std::make_pair("", redactionTargetToJSON(targetPair
.first
.get())));
457 // Build the JSON tree
458 boost::property_tree::ptree aTargetsTree
;
459 aTargetsTree
.add_child("RedactionTargets", aTargetsArray
);
461 // Create path string, and write JSON to file
462 std::string
sPathStr(OUStringToOString(sTargetsFile
, RTL_TEXTENCODING_UTF8
));
464 boost::property_tree::write_json(sPathStr
, aTargetsTree
);
466 catch (css::uno::Exception
& e
)
469 "Exception caught while trying to save the targets JSON to file: " << e
.Message
);
471 //TODO: Warn the user with a message box
475 void SfxAutoRedactDialog::StartFileDialog(StartFileDialogType nType
, const OUString
& rTitle
)
477 OUString
aFilterAllStr(SfxResId(STR_SFX_FILTERNAME_ALL
));
478 OUString
aFilterJsonStr(SfxResId(STR_REDACTION_JSON_FILE_FILTER
));
480 bool bSave
= nType
== StartFileDialogType::SaveAs
;
481 short nDialogType
= bSave
? css::ui::dialogs::TemplateDescription::FILESAVE_AUTOEXTENSION
482 : css::ui::dialogs::TemplateDescription::FILEOPEN_SIMPLE
;
483 m_pFileDlg
.reset(new sfx2::FileDialogHelper(nDialogType
, FileDialogFlags::NONE
, getDialog()));
485 m_pFileDlg
->SetTitle(rTitle
);
486 m_pFileDlg
->AddFilter(aFilterAllStr
, FILEDIALOG_FILTER_ALL
);
487 m_pFileDlg
->AddFilter(aFilterJsonStr
, FILEDIALOG_FILTER_JSON
);
488 m_pFileDlg
->SetCurrentFilter(aFilterJsonStr
);
490 Link
<sfx2::FileDialogHelper
*, void> aDlgClosedLink
491 = bSave
? LINK(this, SfxAutoRedactDialog
, SaveHdl
)
492 : LINK(this, SfxAutoRedactDialog
, LoadHdl
);
493 m_pFileDlg
->SetContext(sfx2::FileDialogHelper::AutoRedact
);
494 m_pFileDlg
->StartExecuteModal(aDlgClosedLink
);
497 void SfxAutoRedactDialog::addTarget(std::unique_ptr
<RedactionTarget
> pTarget
)
499 // Only the visual/display part
500 m_aTargetsBox
.InsertTarget(pTarget
.get());
502 // Actually add to the targets vector
503 auto name
= pTarget
->sName
;
504 if (m_aTargetsBox
.GetTargetByName(name
))
505 m_aTableTargets
.emplace_back(std::move(pTarget
), name
);
508 std::unique_ptr
<weld::MessageDialog
> xBox(Application::CreateMessageDialog(
509 getDialog(), VclMessageType::Warning
, VclButtonsType::Ok
,
510 SfxResId(STR_REDACTION_TARGET_ADD_ERROR
)));
515 void SfxAutoRedactDialog::clearTargets()
517 // Clear the targets box
518 m_aTargetsBox
.clear();
520 // Clear the targets vector
521 m_aTableTargets
.clear();
524 SfxAutoRedactDialog::SfxAutoRedactDialog(weld::Window
* pParent
)
525 : SfxDialogController(pParent
, u
"sfx/ui/autoredactdialog.ui"_ustr
, u
"AutoRedactDialog"_ustr
)
526 , m_bIsValidState(true)
527 , m_bTargetsCopied(false)
528 , m_aTargetsBox(m_xBuilder
->weld_tree_view(u
"targets"_ustr
))
529 , m_xLoadBtn(m_xBuilder
->weld_button(u
"btnLoadTargets"_ustr
))
530 , m_xSaveBtn(m_xBuilder
->weld_button(u
"btnSaveTargets"_ustr
))
531 , m_xAddBtn(m_xBuilder
->weld_button(u
"add"_ustr
))
532 , m_xEditBtn(m_xBuilder
->weld_button(u
"edit"_ustr
))
533 , m_xDeleteBtn(m_xBuilder
->weld_button(u
"delete"_ustr
))
535 // Can be used to remember the last set of redaction targets?
537 SvtViewOptions
aDlgOpt(EViewType::Dialog
, m_xDialog
->get_help_id());
539 if (aDlgOpt
.Exists())
541 css::uno::Any aUserItem
= aDlgOpt
.GetUserItem(u
"UserItem"_ustr
);
542 aUserItem
>>= sExtraData
;
545 // update the targets configuration if necessary
546 if (!sExtraData
.isEmpty())
548 weld::WaitObject
aWaitCursor(m_xDialog
.get());
552 // Create path string, and read JSON from file
553 boost::property_tree::ptree aTargetsJSON
;
554 std::stringstream
aStream(std::string(sExtraData
.toUtf8()));
556 boost::property_tree::read_json(aStream
, aTargetsJSON
);
558 // Recreate & add the targets to the dialog
559 for (const boost::property_tree::ptree::value_type
& rValue
:
560 aTargetsJSON
.get_child("RedactionTargets"))
562 addTarget(JSONtoRedactionTarget(rValue
));
565 catch (css::uno::Exception
& e
)
568 "Exception caught while trying to load the last dialog state: " << e
.Message
);
570 //TODO: Warn the user with a message box
574 // Handler connections
575 m_xLoadBtn
->connect_clicked(LINK(this, SfxAutoRedactDialog
, Load
));
576 m_xSaveBtn
->connect_clicked(LINK(this, SfxAutoRedactDialog
, Save
));
577 m_xAddBtn
->connect_clicked(LINK(this, SfxAutoRedactDialog
, AddHdl
));
578 m_xEditBtn
->connect_clicked(LINK(this, SfxAutoRedactDialog
, EditHdl
));
579 m_xDeleteBtn
->connect_clicked(LINK(this, SfxAutoRedactDialog
, DeleteHdl
));
580 m_aTargetsBox
.connect_row_activated(LINK(this, SfxAutoRedactDialog
, DoubleClickEditHdl
));
583 SfxAutoRedactDialog::~SfxAutoRedactDialog()
585 if (m_aTableTargets
.empty())
587 // Clear the dialog data
588 SvtViewOptions
aDlgOpt(EViewType::Dialog
, m_xDialog
->get_help_id());
595 // Put the targets into a JSON array
596 boost::property_tree::ptree aTargetsArray
;
597 for (const auto& targetPair
: m_aTableTargets
)
599 aTargetsArray
.push_back(
600 std::make_pair("", redactionTargetToJSON(targetPair
.first
.get())));
603 // Build the JSON tree
604 boost::property_tree::ptree aTargetsTree
;
605 aTargetsTree
.add_child("RedactionTargets", aTargetsArray
);
606 std::stringstream aStream
;
608 boost::property_tree::write_json(aStream
, aTargetsTree
, false);
610 OUString
sUserDataStr(OUString::fromUtf8(aStream
.str()));
612 // Store the dialog data
613 SvtViewOptions
aDlgOpt(EViewType::Dialog
, m_xDialog
->get_help_id());
614 aDlgOpt
.SetUserItem(u
"UserItem"_ustr
, css::uno::Any(sUserDataStr
));
616 if (!m_bTargetsCopied
)
619 catch (css::uno::Exception
& e
)
622 "Exception caught while trying to store the dialog state: " << e
.Message
);
624 //TODO: Warn the user with a message box
628 bool SfxAutoRedactDialog::hasTargets() const
630 //TODO: Add also some validity checks?
631 if (m_aTableTargets
.empty())
637 bool SfxAutoRedactDialog::getTargets(std::vector
<std::pair
<RedactionTarget
, OUString
>>& r_aTargets
)
639 if (m_aTableTargets
.empty())
642 for (auto const& rPair
: m_aTableTargets
)
643 r_aTargets
.push_back({ *rPair
.first
, rPair
.second
});
644 m_bTargetsCopied
= true;
648 IMPL_LINK_NOARG(SfxAddTargetDialog
, SelectTypeHdl
, weld::ComboBox
&, void)
650 if (m_xType
->get_active_id() == "predefined")
652 // Hide the usual content widgets
653 // We will just set the id as content
654 // And handle with proper regex in the SfxRedactionHelper
655 m_xLabelContent
->set_sensitive(false);
656 m_xLabelContent
->set_visible(false);
657 m_xContent
->set_sensitive(false);
658 m_xContent
->set_visible(false);
659 m_xWholeWords
->set_sensitive(false);
660 m_xWholeWords
->set_visible(false);
661 m_xCaseSensitive
->set_sensitive(false);
662 m_xCaseSensitive
->set_visible(false);
664 // And show the predefined targets
665 m_xLabelPredefContent
->set_sensitive(true);
666 m_xLabelPredefContent
->set_visible(true);
667 m_xPredefContent
->set_sensitive(true);
668 m_xPredefContent
->set_visible(true);
672 m_xLabelPredefContent
->set_sensitive(false);
673 m_xLabelPredefContent
->set_visible(false);
674 m_xPredefContent
->set_sensitive(false);
675 m_xPredefContent
->set_visible(false);
677 m_xLabelContent
->set_sensitive(true);
678 m_xLabelContent
->set_visible(true);
679 m_xContent
->set_sensitive(true);
680 m_xContent
->set_visible(true);
681 m_xWholeWords
->set_sensitive(true);
682 m_xWholeWords
->set_visible(true);
683 m_xCaseSensitive
->set_sensitive(true);
684 m_xCaseSensitive
->set_visible(true);
688 SfxAddTargetDialog::SfxAddTargetDialog(weld::Window
* pParent
, const OUString
& rName
)
689 : GenericDialogController(pParent
, u
"sfx/ui/addtargetdialog.ui"_ustr
, u
"AddTargetDialog"_ustr
)
690 , m_xName(m_xBuilder
->weld_entry(u
"name"_ustr
))
691 , m_xType(m_xBuilder
->weld_combo_box(u
"type"_ustr
))
692 , m_xLabelContent(m_xBuilder
->weld_label(u
"label_content"_ustr
))
693 , m_xContent(m_xBuilder
->weld_entry(u
"content"_ustr
))
694 , m_xLabelPredefContent(m_xBuilder
->weld_label(u
"label_content_predef"_ustr
))
695 , m_xPredefContent(m_xBuilder
->weld_combo_box(u
"content_predef"_ustr
))
696 , m_xCaseSensitive(m_xBuilder
->weld_check_button(u
"checkboxCaseSensitive"_ustr
))
697 , m_xWholeWords(m_xBuilder
->weld_check_button(u
"checkboxWholeWords"_ustr
))
699 m_xName
->set_text(rName
);
700 m_xName
->select_region(0, rName
.getLength());
702 m_xType
->connect_changed(LINK(this, SfxAddTargetDialog
, SelectTypeHdl
));
705 SfxAddTargetDialog::SfxAddTargetDialog(weld::Window
* pParent
, const OUString
& sName
,
706 const RedactionTargetType
& eTargetType
,
707 const OUString
& sContent
, bool bCaseSensitive
,
709 : GenericDialogController(pParent
, u
"sfx/ui/addtargetdialog.ui"_ustr
, u
"AddTargetDialog"_ustr
)
710 , m_xName(m_xBuilder
->weld_entry(u
"name"_ustr
))
711 , m_xType(m_xBuilder
->weld_combo_box(u
"type"_ustr
))
712 , m_xLabelContent(m_xBuilder
->weld_label(u
"label_content"_ustr
))
713 , m_xContent(m_xBuilder
->weld_entry(u
"content"_ustr
))
714 , m_xLabelPredefContent(m_xBuilder
->weld_label(u
"label_content_predef"_ustr
))
715 , m_xPredefContent(m_xBuilder
->weld_combo_box(u
"content_predef"_ustr
))
716 , m_xCaseSensitive(m_xBuilder
->weld_check_button(u
"checkboxCaseSensitive"_ustr
))
717 , m_xWholeWords(m_xBuilder
->weld_check_button(u
"checkboxWholeWords"_ustr
))
719 m_xName
->set_text(sName
);
720 m_xName
->select_region(0, sName
.getLength());
722 m_xType
->set_active_id(getTypeID(eTargetType
));
723 m_xType
->connect_changed(LINK(this, SfxAddTargetDialog
, SelectTypeHdl
));
725 if (eTargetType
== RedactionTargetType::REDACTION_TARGET_PREDEFINED
)
727 SelectTypeHdl(*m_xPredefContent
);
728 m_xPredefContent
->set_active(o3tl::toInt32(o3tl::getToken(sContent
, 0, ';')));
732 m_xContent
->set_text(sContent
);
735 m_xCaseSensitive
->set_active(bCaseSensitive
);
736 m_xWholeWords
->set_active(bWholeWords
);
738 set_title(SfxResId(STR_REDACTION_EDIT_TARGET
));
741 RedactionTargetType
SfxAddTargetDialog::getType() const
743 OUString sTypeID
= m_xType
->get_active_id();
745 if (sTypeID
== "text")
746 return RedactionTargetType::REDACTION_TARGET_TEXT
;
747 else if (sTypeID
== "regex")
748 return RedactionTargetType::REDACTION_TARGET_REGEX
;
749 else if (sTypeID
== "predefined")
750 return RedactionTargetType::REDACTION_TARGET_PREDEFINED
;
752 return RedactionTargetType::REDACTION_TARGET_UNKNOWN
;
755 OUString
SfxAddTargetDialog::getContent() const
757 if (m_xType
->get_active_id() == "predefined")
759 return OUString(OUString::number(m_xPredefContent
->get_active()) + ";"
760 + m_xPredefContent
->get_active_text());
763 return m_xContent
->get_text();
766 /* vim:set shiftwidth=4 softtabstop=4 expandtab cinoptions=b1,g0,N-s cinkeys+=0=break: */