Salome HOME
AppName_customize: initialize duc/LightApp_AppNameCustomize master
authorDUC ANH HOANG <dh77501n@dsp1043837>
Tue, 23 Apr 2024 13:49:12 +0000 (15:49 +0200)
committerDUC ANH HOANG <dh77501n@dsp1043837>
Tue, 30 Apr 2024 08:14:36 +0000 (10:14 +0200)
cmake for compilation of AppNameCustomize

src/LightApp/AppNameCustomize.cxx [new file with mode: 0644]
src/LightApp/CMakeLists.txt
src/LightApp/LightApp_Msg.cxx [new file with mode: 0644]
src/LightApp/LightApp_Msg.h [new file with mode: 0644]

diff --git a/src/LightApp/AppNameCustomize.cxx b/src/LightApp/AppNameCustomize.cxx
new file mode 100644 (file)
index 0000000..a73c2b4
--- /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 SALOME_APPLICATION_DIR
+    QByteArray gui_root_dir_env = qgetenv("SALOME_APPLICATION_DIR");
+    if (gui_root_dir_env.isEmpty())
+    {
+        qCritical()<< "SALOME_APPLICATION_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;
+
+}
index 26ba1e865daef678ac3c32cf74efd39b64baf86e..bcc0372895727f26d25ed150763d301b1df4a121 100644 (file)
@@ -294,6 +294,20 @@ SET(_other_SOURCES
   LightApp_WgViewModel.cxx
   LightApp_ExtInfoDlg.cxx
 )
+
+SET(APPNAMECUSTOMIZE_SRC
+  AppNameCustomize.cxx
+  LightApp_Msg.cxx
+)
+
+SET(APPNAMECUSTOMIZE_header
+  LightApp_Msg.h
+)
+
+QT_WRAP_MOC(moc_APPNAMECUSTOMIZE_header ${APPNAMECUSTOMIZE_header})
+
+SET(APPNAMECUSTOMIZE_SOURCES  ${moc_APPNAMECUSTOMIZE_header} ${APPNAMECUSTOMIZE_SRC})
+
 IF(SALOME_USE_GLVIEWER)
   LIST(APPEND _other_SOURCES LightApp_GLSelector.cxx)
 ENDIF()
@@ -338,3 +352,7 @@ INSTALL(FILES ${LightApp_HEADERS} DESTINATION ${SALOME_INSTALL_HEADERS})
 QT_INSTALL_TS_RESOURCES("${_ts_RESOURCES}" "${SALOME_GUI_INSTALL_RES_DATA}")
 
 INSTALL(FILES ${_other_RESOURCES} DESTINATION ${SALOME_GUI_INSTALL_RES_DATA})
+
+ADD_EXECUTABLE(AppNameCustomize ${APPNAMECUSTOMIZE_SOURCES} )
+TARGET_LINK_LIBRARIES(AppNameCustomize ${QT_LIBRARIES})
+INSTALL(TARGETS AppNameCustomize EXPORT ${PROJECT_NAME}TargetGroup DESTINATION ${SALOME_INSTALL_BINS})
diff --git a/src/LightApp/LightApp_Msg.cxx b/src/LightApp/LightApp_Msg.cxx
new file mode 100644 (file)
index 0000000..4d1247b
--- /dev/null
@@ -0,0 +1,152 @@
+#include "LightApp_Msg.h"
+
+using namespace LightApp_Msg;
+
+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/LightApp/LightApp_Msg.h b/src/LightApp/LightApp_Msg.h
new file mode 100644 (file)
index 0000000..5fb5bdf
--- /dev/null
@@ -0,0 +1,64 @@
+#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);
+};
+
+}