Skip to content

Commit

Permalink
feat: add a method to change the context of a message (#121)
Browse files Browse the repository at this point in the history
  • Loading branch information
smnppKDAB authored Jul 23, 2024
1 parent 89805dd commit 3cbb216
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 2 deletions.
5 changes: 5 additions & 0 deletions docs/API/knut/qttsdocument.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import Knut
|-|-|
||**[addMessage](#addMessage)**(string context, string location, string source, string translation)|
||**[setLanguage](#setLanguage)**(string lang)|
||**[setMessageContext](#setMessageContext)**(string context, string comment, string source, string newContext)|
||**[setSourceLanguage](#setSourceLanguage)**(string lang)|

## Property Documentation
Expand All @@ -46,6 +47,10 @@ Adds a new `source` text, its `translation` located in `location` within the giv

Changes the language.

#### <a name="setMessageContext"></a>**setMessageContext**(string context, string comment, string source, string newContext)

Set a `newContext` for the message identified by the `context`, `comment` and `source` strings

#### <a name="setSourceLanguage"></a>**setSourceLanguage**(string lang)

Changes the source language.
72 changes: 72 additions & 0 deletions src/core/qttsdocument.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,78 @@ void QtTsDocument::addMessage(const QString &context, const QString &fileName, c
Q_EMIT fileUpdated();
}

/*!
* \qmlmethod QtTsDocument::setMessageContext(string context, string comment, string source, string newContext)
* Set a `newContext` for the message identified by the `context`, `comment` and `source` strings
*/

void QtTsDocument::setMessageContext(const QString &context, const QString &comment, const QString &source,
const QString &newContext)
{
LOG("QtTsDocument::setContext", context, comment, source, newContext);

initializeXml();

const auto contexts = m_document.select_nodes("//context");
for (const auto &contextNode : contexts) {

const auto messages = contextNode.node().select_nodes("message");
for (const auto &messageNode : messages) {

pugi::xml_node commentNode = messageNode.node().child("comment");
pugi::xml_node sourceNode = messageNode.node().child("source");
auto commentsAreEqual = [&]() {
return QString::fromUtf8(commentNode.text().as_string()) == comment;
};
auto sourcesAreEqual = [&]() {
return QString::fromUtf8(sourceNode.text().as_string()) == source;
};
auto contextsAreEqual = [&]() {
return QString::fromUtf8(contextNode.node().child("name").text().as_string()) == context;
};
if (commentsAreEqual() && sourcesAreEqual() && contextsAreEqual()) {

pugi::xml_node targetContextNode = findOrCreateContext(newContext);
targetContextNode.append_move(messageNode.node());

setHasChanged(true);
Q_EMIT messagesChanged();
Q_EMIT fileUpdated();

return;
}
}
}
}

pugi::xml_node QtTsDocument::findOrCreateContext(const QString &context)
{
pugi::xml_node contextNode = findContext(context);

if (contextNode == nullptr) {
pugi::xml_node tsNode = m_document.select_node("TS").node();
contextNode = tsNode.append_child("context");
contextNode.append_child("name").append_child(pugi::node_pcdata).set_value(context.toLatin1().constData());
}

return contextNode;
}

pugi::xml_node QtTsDocument::findContext(const QString &context) const
{
pugi::xml_node contextNode;
const auto contexts = m_document.select_nodes("//context");
for (const auto &existingContext : contexts) {
pugi::xml_node nameNode = existingContext.node().child("name");
if (QString::fromLatin1(nameNode.text().as_string()) == context) {
contextNode = existingContext.node();
break;
}
}

return contextNode;
}

bool QtTsDocument::doSave(const QString &fileName)
{
return m_document.save_file(fileName.toLatin1().constData(), " ");
Expand Down
8 changes: 6 additions & 2 deletions src/core/qttsdocument.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class QtTsMessage : public QObject

private:
friend QtTsDocument;
const QString m_context;
QString m_context;
pugi::xml_node m_message;
};

Expand All @@ -73,7 +73,8 @@ class QtTsDocument : public Document
Q_INVOKABLE void setLanguage(const QString &lang);
Q_INVOKABLE void addMessage(const QString &context, const QString &fileName, const QString &source,
const QString &translation, const QString &comment = QString());

Q_INVOKABLE void setMessageContext(const QString &context, const QString &comment, const QString &source,
const QString &newContext);
QString language() const;
QString sourceLanguage() const;
QList<QtTsMessage *> messages() const;
Expand All @@ -94,6 +95,9 @@ class QtTsDocument : public Document
void initializeXml();
pugi::xml_document m_document;

pugi::xml_node findContext(const QString &context) const;
pugi::xml_node findOrCreateContext(const QString &context);

QList<QtTsMessage *> m_messages;
};

Expand Down

0 comments on commit 3cbb216

Please sign in to comment.