Explorar o código

Test und Verfeinerungen, erster Wurf , Besipiel mit IP ADresseingabe

Reinhard Russinger %!s(int64=8) %!d(string=hai) anos
pai
achega
7108a95faf

+ 1 - 0
README.md

@@ -42,6 +42,7 @@ import ApplicationLauncher 1.0
 	- arguments : arguments if not given with appName
 	- stdERR : output of stderr as QString
 	- stdOUT : output as QString
+	- outFName : copy stdout to outFName when set
 	- exitError
 	- exitStatus
 	- exitCode

+ 3 - 1
application.cpp

@@ -96,7 +96,9 @@ void Application::launchScript()
     m_error = -1;
 
     m_process->start(m_AppName + " " +  m_Arguments);
-    qDebug() << "launching application" << m_AppName << "\n with the Argument of \n" + m_Arguments  ;
+    qDebug() << "launching application ::" << m_AppName ;
+    qDebug() << "with the Argument of";
+    qDebug() << m_Arguments  ;
 }
 
 

+ 3 - 2
applicationlauncher_plugin.h

@@ -6,8 +6,9 @@
 class ApplicationLauncherPlugin : public QQmlExtensionPlugin
 {
     Q_OBJECT
-    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
-    
+//    Q_PLUGIN_METADATA(IID "org.qt-project.Qt.QQmlExtensionInterface")
+    Q_PLUGIN_METADATA(IID QQmlExtensionInterface_iid)
+
 public:
     void registerTypes(const char *uri);
 };

+ 120 - 0
example/AppTest/IpAddressInput.qml

@@ -0,0 +1,120 @@
+import QtQuick 2.0
+
+Item {
+    id: ipAddresInput
+    property double space: 4
+    property double ipwidth: 200
+    property double partwidth: (ipwidth / 4) - (3 * space)
+    property color focuscolor: "green"
+    property color ipcolor: "black"
+    property string ipAddress: ""
+
+    function makeIpAddress() {
+        ipAddress = textInput1.text + "." + textInput2.text + "." +
+                textInput3.text + "." + textInput4.text;
+    }
+
+
+    function fillIpFields() {
+        var iplist = ipAddress.split(".");
+        if(iplist.length >= 1) textInput1.text = iplist[0];
+        if(iplist.length >= 2) textInput2.text = iplist[1];
+        if(iplist.length >= 3) textInput3.text = iplist[2];
+        if(iplist.length >= 4) textInput4.text = iplist[3];
+    }
+
+    onIpAddressChanged: {
+        fillIpFields();
+
+    }
+
+    Component.onCompleted: {
+        fillIpFields();
+    }
+
+    TextInput {
+        id: textInput1
+        x: ipAddresInput.x
+        y: ipAddresInput.y
+        width: partwidth
+        KeyNavigation.tab: textInput2
+        Keys.onReturnPressed:
+        {
+            textInput2.focus = true;
+        }
+
+        color: ipcolor
+        font.pixelSize: 14
+        validator: IntValidator {bottom:0; top:255}
+        onFocusChanged: {
+            makeIpAddress();
+            color = (focus) ? focuscolor : ipcolor;
+            cursorPosition = 0;
+        }
+    }
+
+    TextInput {
+        id: textInput2
+        x: ipAddresInput.x + space + partwidth
+        y: ipAddresInput.y
+        width: partwidth
+        KeyNavigation.tab: textInput3
+        Keys.onReturnPressed:
+        {
+            textInput3.focus = true;
+        }
+
+        color: ipcolor
+        font.pixelSize: 14
+        validator: IntValidator {bottom:0; top:255}
+        onFocusChanged: {
+            makeIpAddress();
+            color = (focus) ? focuscolor : ipcolor;
+            cursorPosition = 0;
+        }
+    }
+
+    TextInput {
+        id: textInput3
+        x: ipAddresInput.x + 2*space + 2*partwidth
+        y: ipAddresInput.y
+        width: partwidth
+        KeyNavigation.tab: textInput4
+        Keys.onReturnPressed:
+        {
+            textInput4.focus = true;
+        }
+
+        color: ipcolor
+        font.pixelSize: 14
+        validator: IntValidator {bottom:0; top:255}
+        onFocusChanged: {
+            makeIpAddress();
+            color = (focus) ? focuscolor : ipcolor;
+            cursorPosition = 0;
+        }
+    }
+
+    TextInput {
+        id: textInput4
+        x: ipAddresInput.x + 3*space + 3*partwidth
+        y: ipAddresInput.y
+        width: partwidth
+        KeyNavigation.tab: textInput1
+        Keys.onReturnPressed:
+        {
+            textInput1.focus = true;
+        }
+
+        color: ipcolor
+        font.pixelSize: 14
+        validator: IntValidator {bottom:0; top:255}
+
+        onFocusChanged: {
+            makeIpAddress();
+            color = (focus) ? focuscolor : ipcolor;
+            cursorPosition = 0;
+        }
+    }
+
+}

+ 144 - 57
example/AppTest/SetNetworkInterface.qml

@@ -1,19 +1,50 @@
 import QtQuick 2.5
 import ApplicationLauncher 1.0
 
+
 Item {
     property string mode: "static"
     property string ifacename: "eth0"
     property string address: "192.168.0.125"
     property string netmask: "255.255.255.0"
     property string gateway: "192.168.0.10"
-    property string infile: "/etc/networks/interfaces"
-    property string outfile: "/etc/networks/interfaces"
+    property string nameserver: "192.168.0.10"
+    property string infile: "/etc/network/interfaces"
+    property string outfile: "/etc/network/interfaces"
+
 
     function doSetInterface() {
+        setInterface.interfaceMode = "Set";
+        setInterface.launchScript();
+    }
+
+    function doGetInterface() {
+        setInterface.interfaceMode = "Get";
         setInterface.launchScript();
     }
 
+    function doRestartNetwork() {
+        worker.appName = "/etc/init.d/S40network";
+        worker.arguments = "restart";
+        worker.launchScript();
+    }
+
+    Application{
+        id: worker
+        onAppFinished: {
+            console.log("WorkerDone");
+        }
+
+        onAppStarted: {
+            console.debug("WorkerappStarted :");
+        }
+
+        onAppError: {
+            console.debug("WorkerappError : " + exitError + " appStatus : " + exitStatus + " appCode " + exitCode );
+        }
+
+    }
+
     Application {
         id: writeInterfaces
         outFName: outfile
@@ -31,16 +62,18 @@ Item {
     }
 
     Application {
+        property string interfaceMode:""
+
         id: setInterface
         appName: "cat"
         arguments: infile
 
         onAppFinished: {
             console.debug("AppFinished :");
-            //console.debug("appError : " + exitError + " appStatus : " + exitStatus + " appCode " + exitCode );
+            console.debug("appError : " + exitError + " appStatus : " + exitStatus + " appCode " + exitCode );
             console.debug("stdERR :" + stdERR);
-            //console.debug("stdOUT :" + stdOUT);
-            //console.debug("======================================");
+            console.debug("stdOUT :" + stdOUT);
+            console.debug("======================================");
 
             var lines = stdOUT.split(/[\r\n]+/g);
             var idx = 0;
@@ -71,68 +104,122 @@ Item {
                 ++idx;
             });
 
-            lines.splice(iface[0].index, iface[iface.length - 1].index - iface[0].index + 1);
-
-            var insertIdx = iface[0].index;
-            var insertText;
-            if(mode === "static") {
-                insertText = "iface " + ifacename + " inet " + mode;
-                lines.splice(insertIdx, 0, insertText);
-                ++insertIdx;
-                insertText = "\taddress " + address;
-                lines.splice(insertIdx, 0, insertText);
-                ++insertIdx;
-                insertText = "\tnetmask " + netmask;
-                lines.splice(insertIdx, 0, insertText);
-                ++insertIdx;
-
-
-                //Broadcas Adresse ausrechnen
-                var wrkAddr = address.split(".");
-                var ipAddress = new Uint8Array(4);
-                var i;
-
-                for (i = 0; i< 4; i++) {
-                    ipAddress[i] = wrkAddr[i];
-                }
+            var interfaceCMD = interfaceMode;
 
-                wrkAddr = netmask.split(".");
-                var ipMask = new Uint8Array(4);
-                for (i = 0; i< 4; i++) {
-                    ipMask[i] = wrkAddr[i];
+            if (interfaceCMD === "Get"){ // Auslesen der aktuellen Einstellungen
+                console.log(">>>>>>>>>:: " + JSON.stringify(interfaceCMD));
+                mode = "";
+                address = "";
+                netmask = "";
+                nameserver = "";
+                gateway = "";
+
+                if (iface[0].txt.indexOf("dhcp") !== -1) {
+                    mode = "dhcp";
                 }
+                if (iface[0].txt.indexOf("static") !== -1) {
+                    mode = "static";
+
+                    var i;
+                    var wdata;
+                    for ( i = 1; i < iface.length; i++) {
+                        if ( iface[i].txt.indexOf("address") !== -1) {
+                            wdata = iface[i].txt.split(" ");
+                            address = wdata[1];
+                        }
 
-                var broadcastUINT = new Uint8Array(4);
+                        if ( iface[i].txt.indexOf("netmask") !== -1) {
+                            wdata = iface[i].txt.split(" ");
+                            netmask = wdata[1];
+                        }
+
+                        if ( iface[i].txt.indexOf("gateway") !== -1) {
+                            wdata = iface[i].txt.split(" ");
+                            gateway = wdata[1];
+                        }
 
-                for (i = 0; i< 4; i++) {
-                    broadcastUINT[i] = ipAddress[i] | (~ipMask[i]);
+                        if ( iface[i].txt.indexOf("nameserver") !== -1) {
+                            wdata = iface[i].txt.split(" ");
+                            wdata[3] = wdata[3].replace(/\'/g, '').replace(/\"/g, '');
+                            nameserver = wdata[3];
+                        }
+                    }
                 }
+            }
+
+            if (interfaceCMD === "Set") { // Setzen der Gewälten Einstellungen
+                console.log(">>>>>>>>>:: " + JSON.stringify(interfaceCMD));
+                lines.splice(iface[0].index, iface[iface.length - 1].index - iface[0].index + 1);
+
+                var insertIdx = iface[0].index;
+                var insertText;
+                if(mode === "static") {
+                    insertText = "iface " + ifacename + " inet " + mode;
+                    lines.splice(insertIdx, 0, insertText);
+                    ++insertIdx;
+                    insertText = "\taddress " + address;
+                    lines.splice(insertIdx, 0, insertText);
+                    ++insertIdx;
+                    insertText = "\tnetmask " + netmask;
+                    lines.splice(insertIdx, 0, insertText);
+                    ++insertIdx;
+                    insertText = "\tgateway " + gateway;
+                    lines.splice(insertIdx, 0, insertText);
+                    ++insertIdx;
+
+                    //Broadcast Adresse ausrechnen
+                    var wrkAddr = address.split(".");
+                    var ipAddress = new Uint8Array(4);
+                    var i;
+
+                    for (i = 0; i< 4; i++) {
+                        ipAddress[i] = wrkAddr[i];
+                    }
 
-                var broadcast = broadcastUINT[0] +"." +
-                        broadcastUINT[1] +"." + broadcastUINT[2] +"." +
-                        broadcastUINT[3];
+                    wrkAddr = netmask.split(".");
+                    var ipMask = new Uint8Array(4);
+                    for (i = 0; i< 4; i++) {
+                        ipMask[i] = wrkAddr[i];
+                    }
 
-                insertText = "\tbroadcast " + broadcast;
-                lines.splice(insertIdx, 0, insertText);
-                ++insertIdx;
+                    var broadcastUINT = new Uint8Array(4);
 
-                insertText = "\tpost-up echo " + "'nameserver "  + gateway + "' > /etc/resolv.conf";
-                lines.splice(insertIdx, 0, insertText);
-            } else if (mode === "dhcp") {
-                insertText = "iface " + ifacename + " inet " + mode;
-                lines.splice(insertIdx, 0, insertText);
-                ++insertIdx;
-            }
+                    for (i = 0; i< 4; i++) {
+                        broadcastUINT[i] = ipAddress[i] | (~ipMask[i]);
+                    }
 
-            var cmd = "echo -e " + "\"";
-            lines.forEach(function(entry){
-                cmd = cmd + entry + "\\n"
-                //console.log(JSON.stringify(entry));
-            });
+                    var broadcast = broadcastUINT[0] +"." +
+                            broadcastUINT[1] +"." + broadcastUINT[2] +"." +
+                            broadcastUINT[3];
+
+                    insertText = "\tbroadcast " + broadcast;
+                    lines.splice(insertIdx, 0, insertText);
+                    ++insertIdx;
+
+                    insertText = "\tpost-up echo " + "'nameserver "  + nameserver + "' > /etc/resolv.conf";
+                    lines.splice(insertIdx, 0, insertText);
+                } else if (mode === "dhcp") {
+                    insertText = "iface " + ifacename + " inet " + mode;
+                    lines.splice(insertIdx, 0, insertText);
+                    ++insertIdx;
+                }
+
+                var cmd = "echo -e " + "\"";
+                lines.forEach(function(entry){
+                    cmd = cmd + entry + "\\n"
+                    //console.log(JSON.stringify(entry));
+                });
+
+                cmd = cmd + "\"";
+                writeInterfaces.appName = cmd;
+                writeInterfaces.launchScript();
+            }
 
-            cmd = cmd + "\"";
-            writeInterfaces.appName = cmd;
-            writeInterfaces.launchScript();
+            console.log(">> mode     :: " + JSON.stringify(mode));
+            console.log(">> address     :: " + JSON.stringify(address));
+            console.log(">> netmask     :: " + JSON.stringify(netmask));
+            console.log(">> gateway     :: " + JSON.stringify(gateway));
+            console.log(">> nameserver     :: " + JSON.stringify(nameserver));
 
         }
 

+ 51 - 8
example/AppTest/main.qml

@@ -1,9 +1,7 @@
 import QtQuick 2.5
 import QtQuick.Controls 1.4
 import QtQuick.Layouts 1.0
-import ApplicationLauncher 1.0
 
-import "."
 
 ApplicationWindow {
     visible: true
@@ -15,12 +13,20 @@ ApplicationWindow {
         id: setEth0
         mode: "static"
         ifacename: "eth0"
-        address: "192.168.0.233"
+        address: "192.168.0.126"
         netmask: "255.255.255.0"
-        gateway: "192.168.0.123"
+        nameserver: "192.168.0.101"
+        gateway: "192.168.0.10"
 
-        infile: "/home/ru/BUILD/QT562_di-soric/GfA/board/GfA/Display001/rootfs/etc/network/interfaces"
-        outfile: "/home/ru/out.txt"
+        //        infile: "/home/ru/BUILD/QT562_di-soric/GfA/board/GfA/Display001/rootfs/etc/network/interfaces"
+        //        outfile: "/root/out.txt"
+        infile: "/home/ru/IFACE/interfaces"
+        outfile: "/home/ru/IFACE/interfaces"
+
+
+        onAddressChanged: {
+            ipAddress.ipAddress = address;
+        }
     }
 
     Button {
@@ -31,7 +37,7 @@ ApplicationWindow {
         text: "Eth0 static"
         onClicked: {
             setEth0.mode = "static";
-        setEth0.doSetInterface();
+            setEth0.doSetInterface();
         }
     }
 
@@ -43,7 +49,44 @@ ApplicationWindow {
         text: "Eth0 dhcp"
         onClicked: {
             setEth0.mode = "dhcp";
-        setEth0.doSetInterface();
+            setEth0.doSetInterface();
+        }
+    }
+
+    Button {
+        x:10
+        y:150
+        height: 40
+        width:120
+        text: "restart Netw."
+        onClicked: {
+            //setEth0.doRestartNetwork();
+            setEth0.doGetInterface();
         }
     }
+
+    Label {
+        x:500
+        y:220
+        text: ipAddress.ipAddress
+    }
+
+    IpAddressInput {
+        id: ipAddress
+        x: 20
+        y: 220
+        ipwidth: 150
+        ipcolor: "red"
+        ipAddress: setEth0.address
+
+        Component.onCompleted: {
+            setEth0.doGetInterface();
+
+        }
+
+        onIpAddressChanged: {
+            setEth0.address = ipAddress.ipAddress;
+        }
+    }
+
 }

+ 1 - 0
example/AppTest/qml.qrc

@@ -2,5 +2,6 @@
     <qresource prefix="/">
         <file>main.qml</file>
         <file>SetNetworkInterface.qml</file>
+        <file>IpAddressInput.qml</file>
     </qresource>
 </RCC>