[tor-commits] [vidalia-plugins/master] Subclass QWidget to handle hidden service widgets

chiiph at torproject.org chiiph at torproject.org
Sat Aug 4 00:36:26 UTC 2012


commit 460acc2c3688e50de5babe0ef65130ab9a48181e
Author: Feroze Naina <ferozenaina at gmail.com>
Date:   Sat Jul 21 15:51:40 2012 +0530

    Subclass QWidget to handle hidden service widgets
---
 hiddenservice/customWidget.ui  |   15 +++++-
 hiddenservice/hiddenservice.js |  110 +++++++++-------------------------------
 hiddenservice/hswidget.js      |  107 ++++++++++++++++++++++++++++++++++++++
 hiddenservice/info.xml         |    1 +
 4 files changed, 147 insertions(+), 86 deletions(-)

diff --git a/hiddenservice/customWidget.ui b/hiddenservice/customWidget.ui
index 2345872..c96e584 100644
--- a/hiddenservice/customWidget.ui
+++ b/hiddenservice/customWidget.ui
@@ -31,7 +31,7 @@
      <property name="minimumSize">
       <size>
        <width>410</width>
-       <height>150</height>
+       <height>170</height>
       </size>
      </property>
      <property name="title">
@@ -115,9 +115,15 @@
           <height>0</height>
          </size>
         </property>
+        <property name="minimum">
+         <number>1</number>
+        </property>
         <property name="maximum">
          <number>65535</number>
         </property>
+        <property name="value">
+         <number>80</number>
+        </property>
        </widget>
       </item>
       <item row="4" column="2">
@@ -152,6 +158,13 @@
         </property>
        </widget>
       </item>
+      <item row="6" column="2">
+       <widget class="QLabel" name="lineError">
+        <property name="text">
+         <string/>
+        </property>
+       </widget>
+      </item>
      </layout>
     </widget>
    </item>
diff --git a/hiddenservice/hiddenservice.js b/hiddenservice/hiddenservice.js
index 606a759..862856e 100644
--- a/hiddenservice/hiddenservice.js
+++ b/hiddenservice/hiddenservice.js
@@ -8,23 +8,26 @@ var hiddenservice = {
     removeServices: function() {
         torrc.clear(["HiddenServiceDir", "HiddenServicePort"]);
 
-        for(i = 0; i < this.customwidget.length; i++) {
-            if (this.chkBox[i].checkState() == Qt.Unchecked) {
-            torrc.setValue("HiddenServiceDir", this.hidservdir[i]);
-            torrc.setValue("HiddenServicePort", this.hidservport[i]);
+        for(var i = 0; i < this.customwidget.length; i++) {
+            if (this.customwidget[i].checked() == false) {
+                torrc.setValue("HiddenServiceDir", this.hidservdir[i]);
+                torrc.setValue("HiddenServicePort", this.hidservport[i]);
+            }
+
+            else {
+                this.customwidget[i].hide();
+                this.listLayout.removeWidget(this.customwidget[i]);
             }
         }
 
         torrc.apply(torControl);
 
-        this.updateList();
         this.btnRemove.enabled = false;
     },
 
     updateList: function() {
-
         try{
-            for(i = 0; i < this.customwidget.length; i++) {
+            for(var i = 0; i < this.customwidget.length; i++) {
                 this.customwidget[i].hide();
                 this.listLayout.removeWidget(this.customwidget[i]);
             }
@@ -35,51 +38,15 @@ var hiddenservice = {
         this.hidservport = torrc.value("HiddenServicePort");
 
         this.customwidget = new Array();
-        this.grpBox = new Array();
-        this.lineDir = new Array();
-        this.spinPort = new Array();
-        this.lineAddress = new Array();
-        this.lineName = new Array();
-        this.chkBox = new Array();
-        this.btnBrowse = new Array();
-
-        this.address = new QHostAddress();
 
         this.listLayout = this.scrollArea.widget().layout();
         while(this.listLayout.count() > 1) this.listLayout.takeAt(0);
 
         var loader = new QUiLoader(this.containerWidget);
 
-        for(i = 0; i< this.hidservdir.length; i++) {
-            this.customwidget[i] = loader.load(new QFile(pluginPath + "/hiddenservice/customWidget.ui"));
-            this.grpBox[i] = this.customwidget[i].children()[findWidget(this.customwidget[i], "groupBox")];
-            this.lineDir[i] = this.grpBox[i].children()[findWidget(this.grpBox[i], "lineDir")];
-            this.spinPort[i] = this.grpBox[i].children()[findWidget(this.grpBox[i], "spinPort")];
-            this.lineAddress[i] = this.grpBox[i].children()[findWidget(this.grpBox[i], "lineAddress")];
-            this.lineName[i] = this.grpBox[i].children()[findWidget(this.grpBox[i], "lineName")];
-            this.chkBox[i] = this.grpBox[i].children()[findWidget(this.grpBox[i], "checkBox")];
-            this.btnBrowse[i] = this.grpBox[i].children()[findWidget(this.grpBox[i], "btnBrowse")];
-
-            var file = new QFile(this.hidservdir[i] + "/hostname");
-            if(file.open(QIODevice.ReadOnly))
-            {
-                var textStream = new QTextStream(file);
-                this.lineName[i].setText(textStream.readLine());
-            }
-
-            this.lineDir[i].setText(this.hidservdir[i]);
-            this.spinPort[i].value = this.hidservport[i].split(" ")[0];
-            if (this.hidservport[i].split(" ")[1])
-                this.lineAddress[i].setText(this.hidservport[i].split(" ")[1]);
-            else
-                this.lineAddress[i].setText("127.0.0.1");
-
-            this.chkBox[i]['stateChanged(int)'].connect(this, this.enableRemove);
-            this.lineDir[i]['textEdited(QString)'].connect(this, this.enableApply);
-            this.spinPort[i]['valueChanged(int)'].connect(this, this.enableApply);
-            this.lineAddress[i]['textEdited(QString)'].connect(this, this.enableApply);
-            this.btnBrowse[i]['clicked()'].connect(this, this.browseDir);
-
+        for(var i = 0; i< this.hidservdir.length; i++) {
+            this.customwidget[i] = new HSWidget(i, this);
+            this.customwidget[i].setParent(this);
             this.listLayout.insertWidget(0, this.customwidget[i], 0, 0);
         }
 
@@ -87,53 +54,40 @@ var hiddenservice = {
         this.btnDiscard.enabled = false;
     },
 
-    browseDir: function() {
-        QFileDialog.getExistingDirectory(this, "Browse Directory", "/home");
-    },
 
     applyServices: function() {
         torrc.clear(["HiddenServiceDir", "HiddenServicePort"]);
 
-        for(i = this.customwidget.length-1; i >= 0; i--) {
-            torrc.setValue("HiddenServiceDir", this.lineDir[i].text);
-            torrc.setValue("HiddenServicePort", this.spinPort[i].value + " " + this.lineAddress[i].text);
+        for(var i = this.customwidget.length-1; i >= 0; i--) {
+            this.customwidget[i].apply();
         }
 
         torrc.apply(torControl);
 
         this.updateList();
-        this.btnApply.enabled = false;
-        this.btnDiscard.enabled = false;
     },
 
-    enableApply: function() {
+    toggleApply: function() {
         this.btnDiscard.enabled = true;
-        if(this.sanityCheck()) 
+        if(this.sanityCheck())
             this.btnApply.enabled = true;
         else
             this.btnApply.enabled = false;
     },
 
     sanityCheck: function() {
-        for(i = 0; i < this.customwidget.length; i++) {
-            var ip = this.lineAddress[i].text.split(":");
-            var port = this.spinPort[i].value;
-
-            if(! this.address.setAddress(ip[0]))
-                return false;
-            if(port < 1 || port >65535)
-                return false;
-            if(ip[1] < 1 || ip[1] > 65535)
+        for(var i = 0; i < this.customwidget.length; i++) {
+            if (! this.customwidget[i].sanityHS())
                 return false;
         }
 
-            return true;
+        return true;
     },
 
     enableRemove: function() {
         this.btnRemove.enabled = false;
-        for (i = 0; i < this.hidservdir.length; i++) {
-            if(this.chkBox[i].checkState() == Qt.Checked) {
+        for (var i = 0; i < this.customwidget.length; i++) {
+            if (this.customwidget[i].checked()) {
                 this.btnRemove.enabled = true;
                 break;
             }
@@ -141,24 +95,10 @@ var hiddenservice = {
     },
 
     addService: function() {
+        var pos = this.customwidget.length;
+        this.customwidget.push(new HSWidget(pos, this));
 
-        var loader = new QUiLoader(this.containerWidget);
-        this.customwidget.push(loader.load(new QFile(pluginPath + "/hiddenservice/customWidget.ui")));
-        var pos = this.customwidget.length-1;
-
-        this.grpBox[pos] = this.customwidget[pos].children()[findWidget(this.customwidget[pos], "groupBox")];
-        this.lineDir[pos] = this.grpBox[pos].children()[findWidget(this.grpBox[pos], "lineDir")];
-        this.spinPort[pos] = this.grpBox[pos].children()[findWidget(this.grpBox[pos], "spinPort")];
-        this.lineAddress[pos] = this.grpBox[i].children()[findWidget(this.grpBox[pos], "lineAddress")];
-        this.lineName[pos] = this.grpBox[pos].children()[findWidget(this.grpBox[pos], "lineName")];
-        this.chkBox[pos] = this.grpBox[pos].children()[findWidget(this.grpBox[pos], "checkBox")];
-
-        this.chkBox[pos]['stateChanged(int)'].connect(this, this.enableRemove);
-        this.lineDir[pos]['textEdited(QString)'].connect(this, this.enableApply);
-        this.spinPort[pos]['valueChanged(int)'].connect(this, this.enableApply);
-        this.lineAddress[pos]['textEdited(QString)'].connect(this, this.enableApply);
-
-        this.listLayout.insertWidget(0, this.customwidget[this.customwidget.length-1], 0, 0);
+        this.listLayout.insertWidget(0, this.customwidget[pos], 0, 0);
     },
 
 
diff --git a/hiddenservice/hswidget.js b/hiddenservice/hswidget.js
new file mode 100644
index 0000000..77f058d
--- /dev/null
+++ b/hiddenservice/hswidget.js
@@ -0,0 +1,107 @@
+importExtension("qt");
+importExtension("qt.core");
+importExtension("qt.gui");
+
+function HSWidget(index, parent) {
+    QWidget.call(this, parent);
+
+    this.pluginParent = parent;
+
+    var widgetui = new QFile(pluginPath + "/hiddenservice/customWidget.ui");
+    var loader = new QUiLoader();
+    widgetui.open(QIODevice.ReadOnly);
+
+    this.address = new QHostAddress();
+
+    this.customwidget = loader.load(widgetui);
+
+    this.grpBox = this.customwidget.children()[findWidget(this.customwidget, "groupBox")];
+    this.lineDir = this.grpBox.children()[findWidget(this.grpBox, "lineDir")];
+    this.spinPort = this.grpBox.children()[findWidget(this.grpBox, "spinPort")];
+    this.lineAddress = this.grpBox.children()[findWidget(this.grpBox, "lineAddress")];
+    this.lineName = this.grpBox.children()[findWidget(this.grpBox, "lineName")];
+    this.chkBox = this.grpBox.children()[findWidget(this.grpBox, "checkBox")];
+    this.btnBrowse = this.grpBox.children()[findWidget(this.grpBox, "btnBrowse")];
+    this.lineError = this.grpBox.children()[findWidget(this.grpBox, "lineError")];
+
+    try {
+        this.hidservdir = torrc.value("HiddenServiceDir")[index];
+        this.hidservport = torrc.value("HiddenServicePort")[index];
+        var file = new QFile(this.hidservdir + "/hostname");
+        if(file.open(QIODevice.ReadOnly))
+        {
+            var textStream = new QTextStream(file);
+            this.lineName.setText(textStream.readLine());
+        }
+
+        this.lineDir.setText(this.hidservdir);
+        this.spinPort.value = this.hidservport.split(" ")[0];
+
+        if (this.hidservport.split(" ")[1])
+            this.lineAddress.setText(this.hidservport.split(" ")[1]);
+        else
+            this.lineAddress.setText("127.0.0.1");
+    }
+    catch(err) {}
+
+    this.lineError.hide();
+
+    this.chkBox['stateChanged(int)'].connect(this, this.enableRemove);
+    this.lineDir['textChanged(QString)'].connect(this, this.toggleApply);
+    this.spinPort['valueChanged(int)'].connect(this, this.toggleApply);
+    this.lineAddress['textEdited(QString)'].connect(this, this.toggleApply);
+    this.btnBrowse['clicked()'].connect(this, this.browseDir);
+
+    var layout = new QVBoxLayout(this);
+    layout.addWidget(this.customwidget, 0, 0);
+}
+
+HSWidget.prototype = new QWidget();
+
+HSWidget.prototype.browseDir = function() {
+    dir = QFileDialog.getExistingDirectory(this, "Browse Directory", "/home");
+    if (dir)
+        this.lineDir.setText(dir);
+}
+
+HSWidget.prototype.apply = function() {
+    torrc.setValue("HiddenServiceDir", this.lineDir.text);
+    torrc.setValue("HiddenServicePort", this.spinPort.value + " " + this.lineAddress.text);
+}
+
+HSWidget.prototype.toggleApply = function() {
+    this.pluginParent.toggleApply();
+}
+
+HSWidget.prototype.sanityHS = function() {
+    var ip = this.lineAddress.text.split(":");
+    var port = this.spinPort.value;
+
+    if(! this.address.setAddress(ip[0])) {
+        this.lineError.text = "Invalid IP address";
+        this.lineError.show();
+        return false;
+    }
+
+    if (ip[1]) {
+        if(/\D/.test(ip[1]) || ip[1] < 1 || ip[1] > 65535) {
+            this.lineError.text = "Invalid port address";
+            this.lineError.show();
+            return false;
+        }
+    }
+
+    this.lineError.hide();
+    return true;
+}
+
+HSWidget.prototype.enableRemove = function() {
+    this.pluginParent.enableRemove();
+}
+
+HSWidget.prototype.checked = function() {
+    if (this.chkBox.checkState() == Qt.Checked)
+        return true;
+    else
+        return false;
+}
diff --git a/hiddenservice/info.xml b/hiddenservice/info.xml
index 0460e12..06660c3 100644
--- a/hiddenservice/info.xml
+++ b/hiddenservice/info.xml
@@ -5,6 +5,7 @@
   <type persistent="false" gui="true" />
   <files>
     <file>hiddenservice.js</file>
+    <file>hswidget.js</file>
   </files>
   <namespace>hiddenservice</namespace>
 </VidaliaPlugin>





More information about the tor-commits mailing list