Kaydet (Commit) 42b21bf3 authored tarafından Ashod Nakashian's avatar Ashod Nakashian Kaydeden (comit) Ashod Nakashian

TSCP: Refactor and simplify paragraph signature helpers

Change-Id: I286c685aad38b850c3b988b7f9ba418092746d66
Reviewed-on: https://gerrit.libreoffice.org/44257Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarAshod Nakashian <ashnakash@gmail.com>
üst 3a88795c
...@@ -123,7 +123,7 @@ std::vector<OUString> lcl_getUsedPageStyles(SwViewShell const * pShell) ...@@ -123,7 +123,7 @@ std::vector<OUString> lcl_getUsedPageStyles(SwViewShell const * pShell)
} }
/// Search for a field named rFieldName of type rServiceName in xText and return it. /// Search for a field named rFieldName of type rServiceName in xText and return it.
uno::Reference<text::XTextField> lcl_findClassificationField(const uno::Reference<text::XText>& xText, const OUString& rServiceName, const OUString& rFieldName) uno::Reference<text::XTextField> lcl_findField(const uno::Reference<text::XText>& xText, const OUString& rServiceName, const OUString& rFieldName)
{ {
uno::Reference<text::XTextField> xField; uno::Reference<text::XTextField> xField;
uno::Reference<container::XEnumerationAccess> xParagraphEnumerationAccess(xText, uno::UNO_QUERY); uno::Reference<container::XEnumerationAccess> xParagraphEnumerationAccess(xText, uno::UNO_QUERY);
...@@ -162,7 +162,7 @@ uno::Reference<text::XTextField> lcl_findClassificationField(const uno::Referenc ...@@ -162,7 +162,7 @@ uno::Reference<text::XTextField> lcl_findClassificationField(const uno::Referenc
/// Search for a field named rFieldName of type rServiceName in xText and return true iff found. /// Search for a field named rFieldName of type rServiceName in xText and return true iff found.
bool lcl_hasField(const uno::Reference<text::XText>& xText, const OUString& rServiceName, const OUString& rFieldName) bool lcl_hasField(const uno::Reference<text::XText>& xText, const OUString& rServiceName, const OUString& rFieldName)
{ {
return lcl_findClassificationField(xText, rServiceName, rFieldName).is(); return lcl_findField(xText, rServiceName, rFieldName).is();
} }
/// Search for a frame with WATERMARK_NAME in name of type rServiceName in xText. Returns found name in rShapeName. /// Search for a frame with WATERMARK_NAME in name of type rServiceName in xText. Returns found name in rShapeName.
...@@ -249,13 +249,13 @@ OString lcl_getParagraphBodyText(const uno::Reference<text::XTextContent>& xText ...@@ -249,13 +249,13 @@ OString lcl_getParagraphBodyText(const uno::Reference<text::XTextContent>& xText
/// Returns RDF (key, value) pair associated with the field, if any. /// Returns RDF (key, value) pair associated with the field, if any.
std::pair<OUString, OUString> lcl_getFieldRDFByPrefix(const uno::Reference<frame::XModel>& xModel, std::pair<OUString, OUString> lcl_getFieldRDFByPrefix(const uno::Reference<frame::XModel>& xModel,
const uno::Reference<css::text::XTextField>& xField, const uno::Reference<css::text::XTextField>& xField,
const OUString& rsPrefix) const OUString& sPrefix)
{ {
const css::uno::Reference<css::rdf::XResource> xSubject(xField, uno::UNO_QUERY); const css::uno::Reference<css::rdf::XResource> xSubject(xField, uno::UNO_QUERY);
std::map<OUString, OUString> aStatements = SwRDFHelper::getStatements(xModel, MetaNS, xSubject); std::map<OUString, OUString> aStatements = SwRDFHelper::getStatements(xModel, MetaNS, xSubject);
for (const auto& pair : aStatements) for (const auto& pair : aStatements)
{ {
if (pair.first.startsWith(rsPrefix)) if (pair.first.startsWith(sPrefix))
return pair; return pair;
} }
...@@ -265,11 +265,11 @@ std::pair<OUString, OUString> lcl_getFieldRDFByPrefix(const uno::Reference<frame ...@@ -265,11 +265,11 @@ std::pair<OUString, OUString> lcl_getFieldRDFByPrefix(const uno::Reference<frame
/// Returns RDF (key, value) pair associated with the field, if any. /// Returns RDF (key, value) pair associated with the field, if any.
std::pair<OUString, OUString> lcl_getFieldRDF(const uno::Reference<frame::XModel>& xModel, std::pair<OUString, OUString> lcl_getFieldRDF(const uno::Reference<frame::XModel>& xModel,
const uno::Reference<css::text::XTextField>& xField, const uno::Reference<css::text::XTextField>& xField,
const OUString& rRDFName) const OUString& sRDFName)
{ {
const css::uno::Reference<css::rdf::XResource> xSubject(xField, uno::UNO_QUERY); const css::uno::Reference<css::rdf::XResource> xSubject(xField, uno::UNO_QUERY);
std::map<OUString, OUString> aStatements = SwRDFHelper::getStatements(xModel, MetaNS, xSubject); std::map<OUString, OUString> aStatements = SwRDFHelper::getStatements(xModel, MetaNS, xSubject);
const auto it = aStatements.find(rRDFName); const auto it = aStatements.find(sRDFName);
return it != aStatements.end() ? std::make_pair(it->first, it->second) : std::make_pair(OUString(), OUString()); return it != aStatements.end() ? std::make_pair(it->first, it->second) : std::make_pair(OUString(), OUString());
} }
...@@ -322,17 +322,14 @@ SignatureDescr lcl_getSignatureDescr(const uno::Reference<frame::XModel>& xModel ...@@ -322,17 +322,14 @@ SignatureDescr lcl_getSignatureDescr(const uno::Reference<frame::XModel>& xModel
return (itId != aStatements.end() ? lcl_getSignatureDescr(xModel, xParagraph, itId->second) : SignatureDescr()); return (itId != aStatements.end() ? lcl_getSignatureDescr(xModel, xParagraph, itId->second) : SignatureDescr());
} }
/// Validate and return validation result and signature field display text. /// Validate and create the signature field display text from the fields.
std::pair<bool, OUString> std::pair<bool, OUString>
lcl_MakeParagraphSignatureFieldText(const uno::Reference<frame::XModel>& xModel, lcl_MakeParagraphSignatureFieldText(const SignatureDescr& aDescr,
const uno::Reference<css::text::XTextContent>& xParagraph,
const uno::Reference<css::text::XTextField>& xField,
const OString& utf8Text) const OString& utf8Text)
{ {
OUString msg = SwResId(STR_INVALID_SIGNATURE); OUString msg = SwResId(STR_INVALID_SIGNATURE);
bool valid = false; bool valid = false;
const SignatureDescr aDescr = lcl_getSignatureDescr(xModel, xParagraph, xField);
if (aDescr.isValid()) if (aDescr.isValid())
{ {
const sal_Char* pData = utf8Text.getStr(); const sal_Char* pData = utf8Text.getStr();
...@@ -356,6 +353,18 @@ lcl_MakeParagraphSignatureFieldText(const uno::Reference<frame::XModel>& xModel, ...@@ -356,6 +353,18 @@ lcl_MakeParagraphSignatureFieldText(const uno::Reference<frame::XModel>& xModel,
return std::make_pair(valid, msg); return std::make_pair(valid, msg);
} }
/// Validate and return validation result and signature field display text.
std::pair<bool, OUString>
lcl_MakeParagraphSignatureFieldText(const uno::Reference<frame::XModel>& xModel,
const uno::Reference<css::text::XTextContent>& xParagraph,
const uno::Reference<css::text::XTextField>& xField,
const OString& utf8Text)
{
const SignatureDescr aDescr = lcl_getSignatureDescr(xModel, xParagraph, xField);
return lcl_MakeParagraphSignatureFieldText(aDescr, utf8Text);
}
/// Generate the next valid ID for the a new signature on this paragraph. /// Generate the next valid ID for the a new signature on this paragraph.
OUString lcl_getNextSignatureId(const uno::Reference<frame::XModel>& xModel, OUString lcl_getNextSignatureId(const uno::Reference<frame::XModel>& xModel,
const uno::Reference<text::XTextContent>& xParagraph) const uno::Reference<text::XTextContent>& xParagraph)
...@@ -409,31 +418,39 @@ uno::Reference<text::XTextField> lcl_InsertParagraphSignature(const uno::Referen ...@@ -409,31 +418,39 @@ uno::Reference<text::XTextField> lcl_InsertParagraphSignature(const uno::Referen
} }
/// Updates the signature field text if changed and returns true only iff updated. /// Updates the signature field text if changed and returns true only iff updated.
bool lcl_UpdateParagraphSignatureField(SwDoc* pDoc, bool lcl_DoUpdateParagraphSignatureField(SwDoc* pDoc,
const uno::Reference<frame::XModel>& xModel, const uno::Reference<css::text::XTextField>& xField,
const uno::Reference<css::text::XTextContent>& xParagraph, const OUString& sDisplayText)
const uno::Reference<css::text::XTextField>& xField,
const OString& utf8Text)
{ {
// Disable undo to avoid introducing noise when we edit the metadata field. // Disable undo to avoid introducing noise when we edit the metadata field.
const bool isUndoEnabled = pDoc->GetIDocumentUndoRedo().DoesUndo(); const bool isUndoEnabled = pDoc->GetIDocumentUndoRedo().DoesUndo();
pDoc->GetIDocumentUndoRedo().DoUndo(false); pDoc->GetIDocumentUndoRedo().DoUndo(false);
comphelper::ScopeGuard const g([pDoc, isUndoEnabled] () { comphelper::ScopeGuard const g([pDoc, isUndoEnabled]() {
pDoc->GetIDocumentUndoRedo().DoUndo(isUndoEnabled); pDoc->GetIDocumentUndoRedo().DoUndo(isUndoEnabled);
}); });
const std::pair<bool, OUString> res = lcl_MakeParagraphSignatureFieldText(xModel, xParagraph, xField, utf8Text);
uno::Reference<css::text::XTextRange> xText(xField, uno::UNO_QUERY); uno::Reference<css::text::XTextRange> xText(xField, uno::UNO_QUERY);
const OUString curText = xText->getString(); const OUString curText = xText->getString();
if (curText != res.second) if (curText != sDisplayText)
{ {
xText->setString(res.second + " "); xText->setString(sDisplayText);
return true; return true;
} }
return false; return false;
} }
/// Updates the signature field text if changed and returns true only iff updated.
bool lcl_UpdateParagraphSignatureField(SwDoc* pDoc,
const uno::Reference<frame::XModel>& xModel,
const uno::Reference<css::text::XTextContent>& xParagraph,
const uno::Reference<css::text::XTextField>& xField,
const OString& utf8Text)
{
const std::pair<bool, OUString> res = lcl_MakeParagraphSignatureFieldText(xModel, xParagraph, xField, utf8Text);
return lcl_DoUpdateParagraphSignatureField(pDoc, xField, res.second);
}
void lcl_RemoveParagraphMetadataField(const uno::Reference<css::text::XTextField>& xField) void lcl_RemoveParagraphMetadataField(const uno::Reference<css::text::XTextField>& xField)
{ {
uno::Reference<css::text::XTextContent> xFieldTextContent(xField, uno::UNO_QUERY); uno::Reference<css::text::XTextContent> xFieldTextContent(xField, uno::UNO_QUERY);
...@@ -525,15 +542,7 @@ bool lcl_UpdateParagraphClassificationField(SwDoc* pDoc, ...@@ -525,15 +542,7 @@ bool lcl_UpdateParagraphClassificationField(SwDoc* pDoc,
css::uno::Reference<css::rdf::XResource> xNodeSubject(xTextNode, uno::UNO_QUERY); css::uno::Reference<css::rdf::XResource> xNodeSubject(xTextNode, uno::UNO_QUERY);
SwRDFHelper::addStatement(xModel, MetaNS, MetaFilename, xNodeSubject, sKey, sValue); SwRDFHelper::addStatement(xModel, MetaNS, MetaFilename, xNodeSubject, sKey, sValue);
uno::Reference<css::text::XTextRange> xText(xField, uno::UNO_QUERY); return lcl_DoUpdateParagraphSignatureField(pDoc, xField, sDisplayText);
const OUString curDisplayText = xText->getString();
if (curDisplayText != sDisplayText)
{
xText->setString(sDisplayText);
return true;
}
return false;
} }
void lcl_ValidateParagraphSignatures(SwDoc* pDoc, const uno::Reference<text::XTextContent>& xParagraph, const bool updateDontRemove) void lcl_ValidateParagraphSignatures(SwDoc* pDoc, const uno::Reference<text::XTextContent>& xParagraph, const bool updateDontRemove)
...@@ -1567,9 +1576,7 @@ void SwUndoParagraphSigning::Insert() ...@@ -1567,9 +1576,7 @@ void SwUndoParagraphSigning::Insert()
}); });
m_xField = lcl_InsertParagraphSignature(m_pDoc->GetDocShell()->GetBaseModel(), m_xParent, m_signature, m_usage); m_xField = lcl_InsertParagraphSignature(m_pDoc->GetDocShell()->GetBaseModel(), m_xParent, m_signature, m_usage);
lcl_DoUpdateParagraphSignatureField(m_pDoc, m_xField, m_display);
uno::Reference<css::text::XTextRange> xText(m_xField, uno::UNO_QUERY);
xText->setString(m_display);
} }
void SwUndoParagraphSigning::Remove() void SwUndoParagraphSigning::Remove()
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment