[tor-commits] [vidalia/alpha] Add accounting options to ServerPage and ServerSettings

chiiph at torproject.org chiiph at torproject.org
Wed Mar 7 19:46:41 UTC 2012


commit ae18cd4f362b5a215ef76f8e8953552cdc5bde35
Author: Tomás Touceda <chiiph at torproject.org>
Date:   Wed Mar 7 16:44:23 2012 -0300

    Add accounting options to ServerPage and ServerSettings
---
 changes/bug2944                       |    2 +
 src/vidalia/config/ServerPage.cpp     |   69 ++++++++
 src/vidalia/config/ServerPage.h       |    5 +
 src/vidalia/config/ServerPage.ui      |  281 ++++++++++++++++++++++++++++-----
 src/vidalia/config/ServerSettings.cpp |  108 +++++++++++++-
 src/vidalia/config/ServerSettings.h   |   20 +++
 6 files changed, 445 insertions(+), 40 deletions(-)

diff --git a/changes/bug2944 b/changes/bug2944
new file mode 100644
index 0000000..968befb
--- /dev/null
+++ b/changes/bug2944
@@ -0,0 +1,2 @@
+  o Add accounting options to the relay configuration panel. Fixes bug
+    2944.
\ No newline at end of file
diff --git a/src/vidalia/config/ServerPage.cpp b/src/vidalia/config/ServerPage.cpp
index 51514ca..eb23c0b 100644
--- a/src/vidalia/config/ServerPage.cpp
+++ b/src/vidalia/config/ServerPage.cpp
@@ -106,6 +106,11 @@ ServerPage::ServerPage(QWidget *parent)
   connect(ui.lblWhatsThis, SIGNAL(linkActivated(QString)),
                        this, SLOT(linkActivated(QString)));
 
+  connect(ui.chkAccounting, SIGNAL(stateChanged(int)),
+          this, SLOT(toggleAccounting(int)));
+  connect(ui.cmbTime, SIGNAL(currentIndexChanged(const QString &)),
+          this, SLOT(toggleDisplayDay(const QString &)));
+
   /* Set validators for address, mask and various port number fields */
   ui.lineServerNickname->setValidator(new NicknameValidator(this));
   ui.lineServerPort->setValidator(new QIntValidator(1, 65535, this));
@@ -338,6 +343,16 @@ ServerPage::save(QString &errmsg)
   saveBandwidthLimits();
   saveExitPolicies();
 
+  if(ui.chkAccounting->checkState() != Qt::Checked)
+    _settings->disableAccounting();
+  else {
+    _settings->setAccountingMax(ui.spnAmount->value(), 
+                                ui.cmbUnit->currentText());
+    _settings->setAccountingStart(ui.cmbTime->currentText(),
+                                  ui.spnDay->value(),
+                                  ui.spnTime->text());
+  }
+
 #if defined(USE_MINIUPNPC)
   _settings->setUpnpEnabled(ui.chkEnableUpnp->isChecked());
 #endif
@@ -377,6 +392,19 @@ ServerPage::load()
   loadExitPolicies();
   loadBridgeIdentity();
 
+  ui.chkAccounting->setCheckState(Qt::Unchecked);
+  toggleAccounting(Qt::Unchecked);
+
+  if(_settings->isAccountingEnabled()) {
+    ui.chkAccounting->setCheckState(Qt::Checked);
+    ui.spnAmount->setValue(_settings->accountingMaxAmount());
+    ui.cmbUnit->setCurrentIndex(ui.cmbUnit->findText(_settings->accountingMaxUnit()));
+    ui.cmbTime->setCurrentIndex(ui.cmbTime->findText(_settings->accountingStartDwm()));
+    if(ui.spnDay->isVisible())
+      ui.spnDay->setValue(_settings->accountingStartDay());
+    ui.spnTime->setTime(QTime::fromString(_settings->accountingStartTime(), "h:mm"));
+  }
+
 #if defined(USE_MINIUPNPC)
   ui.chkEnableUpnp->setChecked(_settings->isUpnpEnabled());
 #endif
@@ -681,3 +709,44 @@ err:
   dlg.exec();
 }
 
+void
+ServerPage::toggleAccounting(int state)
+{
+  if(state == Qt::Checked) {
+    ui.lblPush->setVisible(true);
+    ui.spnAmount->setVisible(true);
+    ui.cmbUnit->setVisible(true);
+    ui.lblPer->setVisible(true);
+    ui.cmbTime->setVisible(true);
+    if(ui.cmbTime->currentText() == "month" or
+       ui.cmbTime->currentText() == "week") {
+      ui.spnDay->setVisible(true);
+      ui.lblDay->setVisible(true);
+    }
+    ui.lblAt->setVisible(true);
+    ui.spnTime->setVisible(true);
+    ui.lblTime->setVisible(true);
+  } else {
+    ui.lblPush->setVisible(false);
+    ui.spnAmount->setVisible(false);
+    ui.cmbUnit->setVisible(false);
+    ui.lblPer->setVisible(false);
+    ui.cmbTime->setVisible(false);
+    ui.spnDay->setVisible(false);
+    ui.lblDay->setVisible(false);
+    ui.lblTime->setVisible(false);
+    ui.lblAt->setVisible(false);
+    ui.spnTime->setVisible(false);
+  }
+}
+
+void
+ServerPage::toggleDisplayDay(const QString &str)
+{
+  ui.spnDay->setVisible(str == "month" or str == "week");
+  ui.spnDay->setMinimum(1);
+  if(str == "month")
+    ui.spnDay->setMaximum(28);
+  else
+    ui.spnDay->setMaximum(7);
+}
diff --git a/src/vidalia/config/ServerPage.h b/src/vidalia/config/ServerPage.h
index 54549a2..46b38af 100644
--- a/src/vidalia/config/ServerPage.h
+++ b/src/vidalia/config/ServerPage.h
@@ -82,6 +82,11 @@ private slots:
   /** Called when the user clicks on a QLabel containing a hyperlink. */
   void linkActivated(const QString &url);
 
+  /** Called when the user toggles the Enable Accounting checkbox */
+  void toggleAccounting(int state);
+  /** Called when the user selects a new item from cmbTime */
+  void toggleDisplayDay(const QString &str);
+
 private:
   /** Index values of rate values in the bandwidth limits dropdown box. */
   enum BwRateIndex {
diff --git a/src/vidalia/config/ServerPage.ui b/src/vidalia/config/ServerPage.ui
index ec277f7..f26f7e4 100644
--- a/src/vidalia/config/ServerPage.ui
+++ b/src/vidalia/config/ServerPage.ui
@@ -6,8 +6,8 @@
    <rect>
     <x>0</x>
     <y>0</y>
-    <width>658</width>
-    <height>578</height>
+    <width>704</width>
+    <height>688</height>
    </rect>
   </property>
   <property name="contextMenuPolicy">
@@ -547,26 +547,32 @@ problem with your relay. You might also include your PGP or GPG fingerprint.</st
               </widget>
              </item>
              <item row="1" column="0">
-              <layout class="QHBoxLayout">
-               <item>
+              <layout class="QGridLayout" name="gridLayout_2">
+               <item row="0" column="2" colspan="6">
                 <layout class="QVBoxLayout">
                  <item>
-                  <widget class="QLabel" name="label_2">
+                  <widget class="QLabel" name="label_5">
                    <property name="sizePolicy">
                     <sizepolicy hsizetype="Minimum" vsizetype="Preferred">
                      <horstretch>0</horstretch>
                      <verstretch>0</verstretch>
                     </sizepolicy>
                    </property>
+                   <property name="maximumSize">
+                    <size>
+                     <width>100</width>
+                     <height>20</height>
+                    </size>
+                   </property>
                    <property name="text">
-                    <string>Average Rate</string>
+                    <string>Maximum Rate</string>
                    </property>
                   </widget>
                  </item>
                  <item>
                   <layout class="QHBoxLayout">
                    <item>
-                    <widget class="QLineEdit" name="lineAvgRateLimit">
+                    <widget class="QLineEdit" name="lineMaxRateLimit">
                      <property name="sizePolicy">
                       <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
                        <horstretch>0</horstretch>
@@ -576,11 +582,11 @@ problem with your relay. You might also include your PGP or GPG fingerprint.</st
                      <property name="maximumSize">
                       <size>
                        <width>60</width>
-                       <height>22</height>
+                       <height>20</height>
                       </size>
                      </property>
                      <property name="toolTip">
-                      <string>Long-term average bandwidth limit</string>
+                      <string>Peak bandwidth rate limit</string>
                      </property>
                      <property name="maxLength">
                       <number>12</number>
@@ -591,41 +597,51 @@ problem with your relay. You might also include your PGP or GPG fingerprint.</st
                     </widget>
                    </item>
                    <item>
-                    <widget class="QLabel" name="label_6">
+                    <widget class="QLabel" name="label_7">
                      <property name="text">
                       <string>KB/s</string>
                      </property>
                     </widget>
                    </item>
+                   <item>
+                    <spacer>
+                     <property name="orientation">
+                      <enum>Qt::Horizontal</enum>
+                     </property>
+                     <property name="sizeType">
+                      <enum>QSizePolicy::Expanding</enum>
+                     </property>
+                     <property name="sizeHint" stdset="0">
+                      <size>
+                       <width>71</width>
+                       <height>20</height>
+                      </size>
+                     </property>
+                    </spacer>
+                   </item>
                   </layout>
                  </item>
                 </layout>
                </item>
-               <item>
+               <item row="0" column="0" colspan="2">
                 <layout class="QVBoxLayout">
                  <item>
-                  <widget class="QLabel" name="label_5">
+                  <widget class="QLabel" name="label_2">
                    <property name="sizePolicy">
                     <sizepolicy hsizetype="Minimum" vsizetype="Preferred">
                      <horstretch>0</horstretch>
                      <verstretch>0</verstretch>
                     </sizepolicy>
                    </property>
-                   <property name="maximumSize">
-                    <size>
-                     <width>100</width>
-                     <height>20</height>
-                    </size>
-                   </property>
                    <property name="text">
-                    <string>Maximum Rate</string>
+                    <string>Average Rate</string>
                    </property>
                   </widget>
                  </item>
                  <item>
                   <layout class="QHBoxLayout">
                    <item>
-                    <widget class="QLineEdit" name="lineMaxRateLimit">
+                    <widget class="QLineEdit" name="lineAvgRateLimit">
                      <property name="sizePolicy">
                       <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
                        <horstretch>0</horstretch>
@@ -635,11 +651,11 @@ problem with your relay. You might also include your PGP or GPG fingerprint.</st
                      <property name="maximumSize">
                       <size>
                        <width>60</width>
-                       <height>20</height>
+                       <height>22</height>
                       </size>
                      </property>
                      <property name="toolTip">
-                      <string>Peak bandwidth rate limit</string>
+                      <string>Long-term average bandwidth limit</string>
                      </property>
                      <property name="maxLength">
                       <number>12</number>
@@ -650,28 +666,12 @@ problem with your relay. You might also include your PGP or GPG fingerprint.</st
                     </widget>
                    </item>
                    <item>
-                    <widget class="QLabel" name="label_7">
+                    <widget class="QLabel" name="label_6">
                      <property name="text">
                       <string>KB/s</string>
                      </property>
                     </widget>
                    </item>
-                   <item>
-                    <spacer>
-                     <property name="orientation">
-                      <enum>Qt::Horizontal</enum>
-                     </property>
-                     <property name="sizeType">
-                      <enum>QSizePolicy::Expanding</enum>
-                     </property>
-                     <property name="sizeHint" stdset="0">
-                      <size>
-                       <width>71</width>
-                       <height>20</height>
-                      </size>
-                     </property>
-                    </spacer>
-                   </item>
                   </layout>
                  </item>
                 </layout>
@@ -682,6 +682,209 @@ problem with your relay. You might also include your PGP or GPG fingerprint.</st
            </widget>
           </item>
           <item>
+           <layout class="QGridLayout" name="gridLayout_3">
+            <item row="1" column="4">
+             <widget class="QComboBox" name="cmbTime">
+              <property name="sizePolicy">
+               <sizepolicy hsizetype="Minimum" vsizetype="Fixed">
+                <horstretch>0</horstretch>
+                <verstretch>0</verstretch>
+               </sizepolicy>
+              </property>
+              <property name="maximumSize">
+               <size>
+                <width>80</width>
+                <height>16777215</height>
+               </size>
+              </property>
+              <item>
+               <property name="text">
+                <string>day</string>
+               </property>
+              </item>
+              <item>
+               <property name="text">
+                <string>week</string>
+               </property>
+              </item>
+              <item>
+               <property name="text">
+                <string>month</string>
+               </property>
+              </item>
+             </widget>
+            </item>
+            <item row="0" column="0" colspan="2">
+             <widget class="QCheckBox" name="chkAccounting">
+              <property name="text">
+               <string>Enable accounting</string>
+              </property>
+             </widget>
+            </item>
+            <item row="1" column="9">
+             <widget class="QTimeEdit" name="spnTime">
+              <property name="maximumSize">
+               <size>
+                <width>60</width>
+                <height>16777215</height>
+               </size>
+              </property>
+              <property name="displayFormat">
+               <string>h:mm</string>
+              </property>
+             </widget>
+            </item>
+            <item row="1" column="5">
+             <widget class="QLabel" name="lblAt">
+              <property name="maximumSize">
+               <size>
+                <width>20</width>
+                <height>16777215</height>
+               </size>
+              </property>
+              <property name="text">
+               <string>at</string>
+              </property>
+              <property name="alignment">
+               <set>Qt::AlignCenter</set>
+              </property>
+             </widget>
+            </item>
+            <item row="1" column="3">
+             <widget class="QLabel" name="lblPer">
+              <property name="sizePolicy">
+               <sizepolicy hsizetype="Minimum" vsizetype="Preferred">
+                <horstretch>0</horstretch>
+                <verstretch>0</verstretch>
+               </sizepolicy>
+              </property>
+              <property name="maximumSize">
+               <size>
+                <width>40</width>
+                <height>16777215</height>
+               </size>
+              </property>
+              <property name="text">
+               <string>per</string>
+              </property>
+              <property name="alignment">
+               <set>Qt::AlignCenter</set>
+              </property>
+             </widget>
+            </item>
+            <item row="1" column="7">
+             <widget class="QSpinBox" name="spnDay">
+              <property name="maximumSize">
+               <size>
+                <width>40</width>
+                <height>16777215</height>
+               </size>
+              </property>
+              <property name="minimum">
+               <number>1</number>
+              </property>
+              <property name="maximum">
+               <number>31</number>
+              </property>
+             </widget>
+            </item>
+            <item row="1" column="2">
+             <widget class="QComboBox" name="cmbUnit">
+              <property name="maximumSize">
+               <size>
+                <width>80</width>
+                <height>16777215</height>
+               </size>
+              </property>
+              <item>
+               <property name="text">
+                <string>bytes</string>
+               </property>
+              </item>
+              <item>
+               <property name="text">
+                <string>KB</string>
+               </property>
+              </item>
+              <item>
+               <property name="text">
+                <string>MB</string>
+               </property>
+              </item>
+              <item>
+               <property name="text">
+                <string>GB</string>
+               </property>
+              </item>
+              <item>
+               <property name="text">
+                <string>TB</string>
+               </property>
+              </item>
+             </widget>
+            </item>
+            <item row="1" column="0">
+             <widget class="QLabel" name="lblPush">
+              <property name="text">
+               <string>Push no more than</string>
+              </property>
+              <property name="alignment">
+               <set>Qt::AlignRight|Qt::AlignTrailing|Qt::AlignVCenter</set>
+              </property>
+             </widget>
+            </item>
+            <item row="1" column="8">
+             <widget class="QLabel" name="lblTime">
+              <property name="text">
+               <string>time</string>
+              </property>
+              <property name="alignment">
+               <set>Qt::AlignCenter</set>
+              </property>
+             </widget>
+            </item>
+            <item row="1" column="6">
+             <widget class="QLabel" name="lblDay">
+              <property name="maximumSize">
+               <size>
+                <width>50</width>
+                <height>16777215</height>
+               </size>
+              </property>
+              <property name="text">
+               <string>day</string>
+              </property>
+              <property name="alignment">
+               <set>Qt::AlignCenter</set>
+              </property>
+             </widget>
+            </item>
+            <item row="1" column="10">
+             <spacer name="horizontalSpacer_2">
+              <property name="orientation">
+               <enum>Qt::Horizontal</enum>
+              </property>
+              <property name="sizeHint" stdset="0">
+               <size>
+                <width>40</width>
+                <height>20</height>
+               </size>
+              </property>
+             </spacer>
+            </item>
+            <item row="1" column="1">
+             <widget class="QSpinBox" name="spnAmount">
+              <property name="minimum">
+               <number>1</number>
+              </property>
+              <property name="maximum">
+               <number>999999</number>
+              </property>
+             </widget>
+            </item>
+           </layout>
+          </item>
+          <item>
            <spacer>
             <property name="orientation">
              <enum>Qt::Vertical</enum>
diff --git a/src/vidalia/config/ServerSettings.cpp b/src/vidalia/config/ServerSettings.cpp
index 2876d7a..2593dda 100644
--- a/src/vidalia/config/ServerSettings.cpp
+++ b/src/vidalia/config/ServerSettings.cpp
@@ -49,7 +49,8 @@
 #define SETTING_RELAY_BANDWIDTH_RATE   "RelayBandwidthRate"
 #define SETTING_RELAY_BANDWIDTH_BURST  "RelayBandwidthBurst"
 #define SETTING_PUBLISH_SERVER_DESCRIPTOR "PublishServerDescriptor"
-
+#define SETTING_ACCOUNTING_MAX "AccountingMax"
+#define SETTING_ACCOUNTING_START "AccountingStart"
 
 /** Constructor.
  * \param torControl a TorControl object used to read and apply the server
@@ -137,6 +138,9 @@ ServerSettings::apply(QString *errmsg)
       torrc->setValue(SETTING_BRIDGE_RELAY, "0");
       torrc->setValue(SETTING_PUBLISH_SERVER_DESCRIPTOR, "1");
     }
+
+    torrc->setValue(SETTING_ACCOUNTING_MAX, volatileValue(SETTING_ACCOUNTING_MAX).toString());
+    torrc->setValue(SETTING_ACCOUNTING_START, volatileValue(SETTING_ACCOUNTING_START).toString());
   } else {
     QStringList resetKeys;
     quint32 torVersion = torControl()->getTorVersion();
@@ -479,3 +483,105 @@ ServerSettings::setUpnpEnabled(bool enabled)
 #endif
 }
 
+bool
+ServerSettings::isAccountingEnabled()
+{
+  with_torrc_value(SETTING_ACCOUNTING_MAX) {
+    return true;
+  }
+
+  return false;
+}
+
+void
+ServerSettings::disableAccounting()
+{
+  Vidalia::torrc()->clear(QStringList() << SETTING_ACCOUNTING_MAX);
+}
+
+void
+ServerSettings::setAccountingMax(int amount, const QString &unit)
+{
+  setVolatileValue(SETTING_ACCOUNTING_MAX, QString("%1 %2").arg(amount).arg(unit));
+}
+
+void
+ServerSettings::setAccountingStart(const QString &dwm,
+                                   int day,
+                                   const QString &time)
+{
+  if(dwm == "month" or dwm == "week")
+    setVolatileValue(SETTING_ACCOUNTING_START,
+                     QString("%1 %2 %3").arg(dwm)
+                     .arg(day).arg(time));
+  else
+    setVolatileValue(SETTING_ACCOUNTING_START,
+                     QString("%1 %2").arg(dwm).arg(time));
+}
+
+int
+ServerSettings::accountingMaxAmount()
+{
+  with_torrc_value(SETTING_ACCOUNTING_MAX) {
+    QStringList parts = ret.at(0).split(" ");
+    if(parts.size() < 2)
+      return 0;
+    return parts.at(0).trimmed().toInt();
+  }
+  
+  return 0;
+}
+
+const QString
+ServerSettings::accountingMaxUnit()
+{
+  with_torrc_value(SETTING_ACCOUNTING_MAX) {
+    QStringList parts = ret.at(0).split(" ");
+    if(parts.size() < 2)
+      return QString();
+    return parts.at(1).trimmed();
+  }
+  
+  return QString();
+}
+
+const QString
+ServerSettings::accountingStartDwm()
+{
+  with_torrc_value(SETTING_ACCOUNTING_START) {
+    QStringList parts = ret.at(0).split(" ");
+    if(parts.size() < 2)
+      return QString();
+    return parts.at(0).trimmed();
+  }
+  
+  return QString();
+}
+
+int
+ServerSettings::accountingStartDay()
+{
+  with_torrc_value(SETTING_ACCOUNTING_START) {
+    QStringList parts = ret.at(0).split(" ");
+    if(parts.size() < 3)
+      return 1;
+    return parts.at(1).trimmed().toInt();
+  }
+  
+  return 1;
+}
+
+const QString
+ServerSettings::accountingStartTime()
+{
+  with_torrc_value(SETTING_ACCOUNTING_START) {
+    QStringList parts = ret.at(0).split(" ");
+    if(parts.size() < 2)
+      return QString();
+    if(parts.size() == 3)
+      return parts.at(2).trimmed();
+    return parts.at(1).trimmed();
+  }
+  
+  return QString();
+}
diff --git a/src/vidalia/config/ServerSettings.h b/src/vidalia/config/ServerSettings.h
index 023d250..9f7831d 100644
--- a/src/vidalia/config/ServerSettings.h
+++ b/src/vidalia/config/ServerSettings.h
@@ -106,6 +106,26 @@ public:
   /** Sets whether Vidalia should try to configure port forwarding using UPnP. */
   void setUpnpEnabled(bool enabled);
 
+  /** Returns true if accounting is enabled */
+  bool isAccountingEnabled();
+  /** Disables accounting options */
+  void disableAccounting();
+  /** Sets AccountingMax to amount unit */
+  void setAccountingMax(int amount, const QString &unit);
+  /** Sets AccountingStart for dwm (day/week/month) day and time */
+  void setAccountingStart(const QString &dwm, int day, const QString &time);
+
+  /** Returns the max amount of bytes/KB/etc for accounting */
+  int accountingMaxAmount();
+  /** Returns the unit for the max amount */
+  const QString accountingMaxUnit();
+  /** Returns the configured AccountingStart (day/week/month) */
+  const QString accountingStartDwm();
+  /** Returns the day of the week/month for AccountingStart */
+  int accountingStartDay();
+  /** Returns the time of day for AccountingStart */
+  const QString accountingStartTime();
+
 protected:
   /** Virtual method called when we retrieve a server-related setting from Tor.
    * Currently this just translates BandwidthFoo to RelayBandwidthFoo when



More information about the tor-commits mailing list