[tor-commits] [vidalia/alpha] Adds a checkbox to VMessageLog::question to easily remember the answer

chiiph at torproject.org chiiph at torproject.org
Mon Jan 23 16:45:42 UTC 2012


commit 92d5fd3bffd1cee93532e4e3ee525af2328c9787
Author: Tomás Touceda <chiiph at torproject.org>
Date:   Fri Jan 20 19:03:45 2012 -0300

    Adds a checkbox to VMessageLog::question to easily remember the answer
    
    Also uses it for shutting down tor gracefully
---
 src/vidalia/MainWindow.cpp             |    9 ++++-
 src/vidalia/VMessageBox.cpp            |   55 ++++++++++++++++++++++++++-----
 src/vidalia/VMessageBox.h              |   11 +++++-
 src/vidalia/config/VidaliaSettings.cpp |   13 +++++++
 src/vidalia/config/VidaliaSettings.h   |    8 +++++
 5 files changed, 83 insertions(+), 13 deletions(-)

diff --git a/src/vidalia/MainWindow.cpp b/src/vidalia/MainWindow.cpp
index 183e78f..0ba6dda 100644
--- a/src/vidalia/MainWindow.cpp
+++ b/src/vidalia/MainWindow.cpp
@@ -717,12 +717,15 @@ MainWindow::stop()
   QString errmsg;
   TorStatus prevStatus;
   bool rc;
+  VidaliaSettings settings;
 
   /* If we're running a server, give users the option of terminating
    * gracefully so clients have time to find new servers. */
   if (server.isServerEnabled() && !_delayedShutdownStarted) {
     /* Ask the user if they want to shutdown nicely. */
-    int response = VMessageBox::question(this, tr("Relaying is Enabled"),
+    int response = VMessageBox::Yes;
+    if(!settings.rememberShutdown()) {
+      response = VMessageBox::question(this, tr("Relaying is Enabled"),
                      tr("You are currently running a relay. "
                         "Terminating your relay will interrupt any "
                         "open connections from clients.\n\n"
@@ -730,7 +733,9 @@ MainWindow::stop()
                         "give clients time to find a new relay?"),
                         VMessageBox::Yes|VMessageBox::Default, 
                         VMessageBox::No, 
-                        VMessageBox::Cancel|VMessageBox::Escape);
+                        VMessageBox::Cancel|VMessageBox::Escape,
+                        "Remember this answer", &settings, SETTING_REMEMBER_SHUTDOWN);
+    }
     if (response == VMessageBox::Yes)
       _delayedShutdownStarted = true;
     else if (response == VMessageBox::Cancel)
diff --git a/src/vidalia/VMessageBox.cpp b/src/vidalia/VMessageBox.cpp
index 0dbf927..1600bc3 100644
--- a/src/vidalia/VMessageBox.cpp
+++ b/src/vidalia/VMessageBox.cpp
@@ -17,11 +17,19 @@
 
 #include "html.h"
 
+#include <QGridLayout>
+#include <QCheckBox>
+#include <QDialogButtonBox>
+#include <QPushButton>
 
 /** Default constructor. */
 VMessageBox::VMessageBox(QWidget *parent)
   : QMessageBox(parent)
 {
+  _chkRemember = new QCheckBox("");
+  QGridLayout *gl = qobject_cast<QGridLayout *>(layout());
+  gl->addWidget(_chkRemember, gl->rowCount() - 2, 1);
+  _chkRemember->setVisible(false);
 }
 
 /** Returns the button (0, 1, or 2) that is OR-ed with QMessageBox::Default,
@@ -113,15 +121,45 @@ VMessageBox::critical(QWidget *parent, QString caption, QString text,
  * QMessageBox::Escape, respectively. */
 int
 VMessageBox::question(QWidget *parent, QString caption, QString text,
-                      int button0, int button1, int button2)
+                      int button0, int button1, int button2, 
+                      QString remember, VSettings *settings, QString key)
 {
-  int ret = QMessageBox::question(parent, caption, p(text),
-              VMessageBox::buttonText(button0), 
-              VMessageBox::buttonText(button1), 
-              VMessageBox::buttonText(button2),
-              VMessageBox::defaultButton(button0, button1, button2), 
-              VMessageBox::escapeButton(button0, button1, button2));
-  return VMessageBox::selected(ret, button0, button1, button2);
+  VMessageBox messageBox(parent);
+
+  messageBox.setIcon(QMessageBox::Question);
+  messageBox.setWindowTitle(caption);
+  messageBox.setText(text);
+  messageBox.setStandardButtons(QMessageBox::NoButton);
+
+  if(settings) {
+    messageBox._chkRemember->setVisible(true);
+    messageBox._chkRemember->setText(remember);
+  }
+
+  QString myButton0Text = VMessageBox::buttonText(button0);
+  if (myButton0Text.isEmpty())
+    myButton0Text = QDialogButtonBox::tr("OK");
+  messageBox.addButton(myButton0Text, QMessageBox::ActionRole);
+
+  if (!VMessageBox::buttonText(button1).isEmpty())
+    messageBox.addButton(VMessageBox::buttonText(button1), QMessageBox::ActionRole);
+
+  if (!VMessageBox::buttonText(button2).isEmpty())
+    messageBox.addButton(VMessageBox::buttonText(button2), QMessageBox::ActionRole);
+
+  const QList<QAbstractButton *> &buttonList = messageBox.buttons();
+  messageBox.setDefaultButton(qobject_cast<QPushButton *>(buttonList
+                                                          .value(VMessageBox::defaultButton(button0, 
+                                                                                            button1, 
+                                                                                            button2))));
+  messageBox.setEscapeButton(buttonList.value(VMessageBox::escapeButton(button0, button1, button2)));
+  int ret = messageBox.exec();
+
+  if(!settings)
+    return ret;
+  settings->setValue(key, messageBox._chkRemember->checkState() == Qt::Checked);
+
+  return ret;
 }
 
 /** Displays an information message box with the given caption, message text, and
@@ -157,4 +195,3 @@ VMessageBox::warning(QWidget *parent, QString caption, QString text,
               VMessageBox::escapeButton(button0, button1, button2));
   return VMessageBox::selected(ret, button0, button1, button2);
 }
-
diff --git a/src/vidalia/VMessageBox.h b/src/vidalia/VMessageBox.h
index fc96405..0dd3fe5 100644
--- a/src/vidalia/VMessageBox.h
+++ b/src/vidalia/VMessageBox.h
@@ -19,6 +19,9 @@
 #include <QMessageBox>
 #include <QString>
 
+#include "VSettings.h"
+
+class QCheckBox;
 
 class VMessageBox : public QMessageBox
 {
@@ -72,8 +75,10 @@ public:
    * button, OR the Button enum value with QMessageBox::Default or
    * QMessageBox::Escape, respectively. */
   static int question(QWidget *parent, QString caption, QString text,
-                         int button0, int button1 = NoButton, 
-                         int button2 = NoButton);
+                        int button0, int button1 = NoButton, 
+                        int button2 = NoButton,
+                        QString remember = QString(), VSettings *settings = 0, 
+                        QString key = QString());
   
   /** Converts a Button enum value to a translated string. */
   static QString buttonText(int button);
@@ -87,6 +92,8 @@ private:
   static int escapeButton(int button0, int button1, int button2);
   /** Returns the Button enum value from the given return value. */
   static int selected(int ret, int button0, int button1, int button2);
+
+  QCheckBox *_chkRemember;
 };
 
 #endif
diff --git a/src/vidalia/config/VidaliaSettings.cpp b/src/vidalia/config/VidaliaSettings.cpp
index 022c427..48cd051 100644
--- a/src/vidalia/config/VidaliaSettings.cpp
+++ b/src/vidalia/config/VidaliaSettings.cpp
@@ -87,6 +87,7 @@ VidaliaSettings::VidaliaSettings()
 
   setDefault(SETTING_PLUGIN_PATH, vApp->dataDirectory());
   setDefault(SETTING_ICON_PREF, Both);
+  setDefault(SETTING_REMEMBER_SHUTDOWN, false);
 }
 
 /** Gets the currently preferred language code for Vidalia. */
@@ -370,3 +371,15 @@ VidaliaSettings::fromString(QString iconPref)
 
   return Both;
 }
+
+bool
+VidaliaSettings::rememberShutdown()
+{
+  return value(SETTING_REMEMBER_SHUTDOWN).toBool();
+}
+
+void
+VidaliaSettings::setRememberShutdown(bool val)
+{
+  setValue(SETTING_REMEMBER_SHUTDOWN, val);
+}
diff --git a/src/vidalia/config/VidaliaSettings.h b/src/vidalia/config/VidaliaSettings.h
index bb6f1a0..3efbdb5 100644
--- a/src/vidalia/config/VidaliaSettings.h
+++ b/src/vidalia/config/VidaliaSettings.h
@@ -20,6 +20,8 @@
 
 #include <QDateTime>
 
+/** Public setting keys */
+#define SETTING_REMEMBER_SHUTDOWN   "RememberShutdown"
 
 /** Handles saving and restoring Vidalia's settings, such as the
  * location of Tor, the control port, etc.
@@ -144,6 +146,12 @@ public:
   void setIconPref(const IconPosition iconPref);
   QString toString(const IconPosition iconPref);
   IconPosition fromString(QString iconPref);
+
+  /** Returns true if Vidalia should not ask if the user wants to shutdown 
+   *  tor gracefully */
+  bool rememberShutdown();
+  /** Sets RememberShutdown to val */
+  void setRememberShutdown(bool val);
 };
 
 #endif





More information about the tor-commits mailing list