]> SALOME platform Git repositories - modules/gui.git/commitdiff
Salome HOME
LightApp_msg_customize: initialize
authorDUC ANH HOANG <dh77501n@dsp1043837>
Tue, 23 Apr 2024 13:49:12 +0000 (15:49 +0200)
committerDUC ANH HOANG <dh77501n@dsp1043837>
Tue, 23 Apr 2024 13:49:12 +0000 (15:49 +0200)
src/LightAppMsg_Customize/AppNameCustomize.cpp [new file with mode: 0644]
src/LightAppMsg_Customize/AppNameCustomize.pro [new file with mode: 0644]
src/LightAppMsg_Customize/LightApp_msg.cpp [new file with mode: 0644]
src/LightAppMsg_Customize/LightApp_msg.h [new file with mode: 0644]

diff --git a/src/LightAppMsg_Customize/AppNameCustomize.cpp b/src/LightAppMsg_Customize/AppNameCustomize.cpp
new file mode 100644 (file)
index 0000000..0defc2e
--- /dev/null
@@ -0,0 +1,138 @@
+#include "LightApp_msg.h"
+
+using namespace LightApp_msg;
+
+bool qmTots(const QString &qmFilePath, const QString &tsFilePath) {
+    QProcess process;
+
+    QString command = "lconvert";
+    QStringList arguments;
+    arguments << qmFilePath;
+    arguments << "-o";
+    arguments << tsFilePath;
+    
+    process.start(command, arguments);
+
+    process.waitForFinished();
+
+    if (process.exitCode()== 0)
+        return true;
+    else
+        return false;
+}
+
+bool tsToqm(const QString &tsFilePath, const QString &qmFilePath){
+    QProcess process;
+
+    QString command = "lrelease";
+    QStringList arguments;
+    arguments << tsFilePath;
+    arguments << "-qm";
+    arguments << qmFilePath;
+    
+    process.start(command, arguments);
+
+    process.waitForFinished();
+
+    if (process.exitCode()== 0)
+        return true;
+    else
+    {
+        qDebug()<< QString("Can not compile %1 to %2").arg(tsFilePath,qmFilePath);
+        return false;
+    }
+}
+
+bool LightApp_msg_patching(const QString& app_name,const QString& qmFilePath)
+{
+    // Get GUI_ROOT_DIR
+    QByteArray gui_root_dir_env = qgetenv("GUI_ROOT_DIR");
+    if (gui_root_dir_env.isEmpty())
+    {
+        qCritical()<< "GUI_ROOT_DIR must be defined";
+        return false;
+    }
+
+    QString gui_root_dir = QString::fromUtf8(gui_root_dir_env);
+    QDir gui_root_qdir(gui_root_dir);
+    QString qmFilePathAbs(gui_root_qdir.filePath(qmFilePath));
+    QStringList qmFilePathAbs_splited = qmFilePathAbs.split("/");
+    QString tsFilePath(qmFilePathAbs_splited[qmFilePathAbs_splited.size() - 1]);
+    tsFilePath.replace(".qm",".ts");
+
+    if (qmTots(qmFilePathAbs,tsFilePath))
+        qInfo() <<"qmTots OK";
+    else
+    {
+        qDebug() <<"qmTots KO";
+        return false;   
+    }
+
+    try
+    {
+        QFile input_tsFile(tsFilePath);
+        if(!input_tsFile.open(QFile::ReadOnly | QFile::Text)) {
+            qDebug() << "Could not open tsfile";
+            throw;
+        }
+        std::unique_ptr<QList<Context>> Context_list(new QList<Context>);
+
+        LightApp_msg_handling LAMsg_handling(std::move(Context_list));
+
+        LAMsg_handling.read(&input_tsFile);
+
+        LAMsg_handling.GetContextbyName("@default")->GetMessagebySrc("APP_NAME")->Set_Translation(app_name);
+
+        input_tsFile.close();
+
+        // Write in tsfile
+        QFile output_tsFile(tsFilePath);
+        if (!output_tsFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
+            qDebug() << "Cannot open file for writing.";
+            throw;
+        }
+        QTextStream out(&output_tsFile);
+        out << LAMsg_handling.write();
+        output_tsFile.close();
+    }
+    catch(...)
+    {
+        qCritical() << "Something wrong with " << qmFilePathAbs;
+        return false;
+    }
+
+    // write to qm file
+    if (tsToqm(tsFilePath,qmFilePathAbs))
+        qInfo() <<"tsToqm OK";
+    else
+    {
+        qDebug() <<"tsToqm KO";
+        return false;   
+    }
+    
+    return true;
+    
+}
+int main(int argc, char *argv[]) {
+
+    if (argc < 2) {
+        qDebug() << "Usage: app_name must be declared";
+        return 1;
+    }
+
+    QString app_name = QString::fromUtf8(argv[1]);
+    QStringList qmFilePath_list = {"share/salome/resources/gui/LightApp_msg_fr.qm",
+                                    "share/salome/resources/gui/LightApp_msg_en.qm",
+                                    "share/salome/resources/gui/LightApp_msg_ja.qm"};
+
+    for (const QString& qmFilePath:qmFilePath_list)
+    {
+        if(!LightApp_msg_patching(app_name,qmFilePath))
+        {
+            qDebug() << "LightApp_msg_patching failed!!!!";
+            return 1;
+        }
+    }
+    return 0;
+
+}
diff --git a/src/LightAppMsg_Customize/AppNameCustomize.pro b/src/LightAppMsg_Customize/AppNameCustomize.pro
new file mode 100644 (file)
index 0000000..5fa86fc
--- /dev/null
@@ -0,0 +1,7 @@
+TEMPLATE = app
+QT += core gui
+CONFIG += c++11
+SOURCES += \
+    AppNameCustomize.cpp \
+    LightApp_msg.cpp
+HEADERS += LightApp_msg.h
\ No newline at end of file
diff --git a/src/LightAppMsg_Customize/LightApp_msg.cpp b/src/LightAppMsg_Customize/LightApp_msg.cpp
new file mode 100644 (file)
index 0000000..7168014
--- /dev/null
@@ -0,0 +1,163 @@
+#include "LightApp_msg.h"
+
+using namespace LightApp_msg;
+
+QString CustomEntityResolver::resolveUndeclaredEntity(const QString &name) {
+    if (name == "&amp;")
+        return "&amp;";
+    else if (name == "&quot;")
+        return "&quot;";
+    else if (name == "&apos;")
+        return "&apos;";
+    return QString();
+}
+
+
+QString Context::ExportContext()
+{
+    QString context_string;
+    context_string.append(QString("<context>\n"));
+    context_string.append(QString("<name>%1</name>\n").arg(Context_name));
+    foreach (const Message& msg, Message_list)
+        {
+            context_string.append(QString("<message>\n<source>%1</source>\n<translation>%2</translation>\n</message>\n").arg(msg.GetSource(),msg.GetTranslation()));
+        }
+    context_string.append(QString("</context>\n"));
+    
+    return context_string;
+}
+
+Message* Context::GetMessagebySrc(const QString& src)
+{
+    uint i(0);
+    while(i < Message_list.size() && Message_list[i].GetSource() != src)
+        i++;
+    if (i < Message_list.size())
+        return &Message_list[i];
+}
+
+
+
+Context* LightApp_msg_handling::GetContextbyName(const QString& name)
+{
+    int i(0);
+    while(i < Context_list->size() && (*Context_list)[i].GetContext_name() != name)
+        i++;
+    if (i < Context_list->size())
+        return &(*Context_list)[i];
+    else
+    {
+        qDebug()<<"Context with name " << name << " doesn't exist in LightApp_msg_handling";
+        return nullptr;
+    }
+}
+
+bool LightApp_msg_handling::HaveContext(const QString& name)
+{
+    int i(0);
+    while(i < Context_list->size() && (*Context_list)[i].GetContext_name() != name)
+        i++;
+    if (i < Context_list->size())
+        return true;
+    else
+        return false;
+}
+
+QString LightApp_msg_handling::write()
+{
+    QString updateXml;
+    updateXml.append(header);
+    for(auto ct:*Context_list)
+        updateXml.append(ct.ExportContext());
+    updateXml.append("</TS>");
+
+    return updateXml;
+
+}
+void LightApp_msg_handling::read(QIODevice *device)
+{
+
+    //header
+    QTextStream textStream(device);
+    // Read 3 first header line of ts file
+    for (int i = 0; i < 3 && !textStream.atEnd(); ++i) {
+        header.append(textStream.readLine() + "\n");
+    }
+    device->seek(0);
+    
+    //context
+    xmlReader.setDevice(device);
+    //CustomEntityResolver resolver;
+    //xmlReader.setEntityResolver(&resolver);
+
+    while(xmlReader.readNextStartElement()){
+        if(xmlReader.name().toString() == "context")
+            readContext();
+    }
+
+}
+
+void LightApp_msg_handling::readContext()
+{
+    Q_ASSERT(xmlReader.isStartElement() &&
+             xmlReader.name() == "context");
+
+    while(xmlReader.readNextStartElement()){
+        if(xmlReader.name() == "name")
+        {
+            QString name = xmlReader.readElementText();
+            if (!HaveContext(name))
+                Context_list->append(Context(name));
+            else
+                qWarning()<<"Context with name " << name << " is already appended in LightApp_msg_handling";
+        }
+        else if (xmlReader.name() == "message")
+        {
+            readMessage();
+        }
+        else
+            xmlReader.skipCurrentElement();
+    }
+}
+
+void LightApp_msg_handling::readMessage()
+{
+    Q_ASSERT(xmlReader.isStartElement() &&
+             xmlReader.name() == "message");
+    Message msg = Message("","");
+    Message& msg_ref = msg;
+    while(xmlReader.readNextStartElement()){
+        if(xmlReader.name() == "source")
+            readSrc(msg_ref);
+        else if(xmlReader.name() == "translation")
+            readTranslation(msg_ref);
+        else
+            xmlReader.skipCurrentElement();
+        
+    }
+    
+    (*Context_list)[Context_list->size() - 1].AddMsg(msg);
+}
+
+void LightApp_msg_handling::readSrc(Message& msg)
+{
+    Q_ASSERT(xmlReader.isStartElement() &&
+             xmlReader.name() == "source");
+    QString src = xmlReader.readElementText();
+
+    msg.Set_Source(src);
+}
+
+void LightApp_msg_handling::readTranslation(Message& msg)
+{
+    Q_ASSERT(xmlReader.isStartElement() &&
+             xmlReader.name() == "translation");
+
+    QString trl = xmlReader.readElementText();
+    trl.replace("&","&amp;");
+    trl.replace("\"","&quot;");
+    trl.replace("\'","&apos;");
+    trl.replace("<","&lt;");
+    trl.replace(">","&gt;");
+    msg.Set_Translation(trl);
+}
diff --git a/src/LightAppMsg_Customize/LightApp_msg.h b/src/LightAppMsg_Customize/LightApp_msg.h
new file mode 100644 (file)
index 0000000..c4b322f
--- /dev/null
@@ -0,0 +1,69 @@
+#include <QTextStream>
+#include <QBuffer>
+#include <QString>
+#include <QXmlStreamReader>
+#include <QDebug>
+#include <QTranslator>
+#include <QFile>
+#include <QCoreApplication>
+#include <QProcess>
+#include <QDir>
+#include <memory>
+
+namespace LightApp_msg
+{
+
+class Message
+{
+    public:
+        Message(const QString& src, const QString& trl):source(src),translation(trl){};
+        QString GetSource() const { return source ;}
+        QString GetTranslation() const { return translation ;}
+        void Set_Source(const QString& str) { source = str; }
+        void Set_Translation(const QString& str) { translation = str; }
+    private:
+        QString source;
+        QString translation;
+};
+
+class Context
+{
+    public:
+        Context(const QString& name):Context_name(name){};
+        QString GetContext_name(){return Context_name;}
+        void AddMsg(Message& msg) {Message_list.append(msg);}
+        void AddMsg(const QString& src, const QString& trl) {Message_list.append(Message(src,trl));}
+        Message* GetMessagebySrc(const QString&);
+        QList<Message>& GetMessageList(){return Message_list;}
+        QString ExportContext();
+    private:
+        QString Context_name;
+        QList<Message> Message_list;
+};
+
+class LightApp_msg_handling
+{
+public:
+    LightApp_msg_handling(std::unique_ptr<QList<Context>> Context_list):
+                                            Context_list(std::move(Context_list)){};
+    void read(QIODevice *device);
+    QString write();
+    Context* GetContextbyName(const QString &name);
+    bool HaveContext(const QString &name);
+private:
+    QXmlStreamReader xmlReader;
+    std::unique_ptr<QList<Context>> Context_list;
+    QString header;
+
+    void readContext();
+    void readMessage();
+    void readSrc(Message& msg);
+    void readTranslation(Message& msg);
+};
+
+}
+
+class CustomEntityResolver : public QXmlStreamEntityResolver {
+public:
+    QString resolveUndeclaredEntity(const QString &name);
+};
\ No newline at end of file