bwgraph.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006,  Matt Edman, Justin Hipple
00005  *
00006  *  This program is free software; you can redistribute it and/or
00007  *  modify it under the terms of the GNU General Public License
00008  *  as published by the Free Software Foundation; either version 2
00009  *  of the License, or (at your option) any later version.
00010  *
00011  *  This program is distributed in the hope that it will be useful,
00012  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00013  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00014  *  GNU General Public License for more details.
00015  *
00016  *  You should have received a copy of the GNU General Public License
00017  *  along with this program; if not, write to the Free Software
00018  *  Foundation, Inc., 51 Franklin Street, Fifth Floor, 
00019  *  Boston, MA  02110-1301, USA.
00020  ****************************************************************/
00021 
00022 /** 
00023  * \file bwgraph.cpp
00024  * \version $Id: bwgraph.cpp 1238 2006-09-25 17:50:57Z edmanm $
00025  * \brief Displays a graph of Tor's bandwidth usage
00026  */
00027 
00028 #include <vidalia.h>
00029 #include <control/bandwidthevent.h>
00030 #include "bwgraph.h"
00031 
00032 #define BWGRAPH_LINE_SEND       (1u<<0)
00033 #define BWGRAPH_LINE_RECV       (1u<<1)
00034 #define SETTING_FILTER          "LineFilter"
00035 #define SETTING_OPACITY         "Opacity"
00036 #define SETTING_ALWAYS_ON_TOP   "AlwaysOnTop"
00037 #define DEFAULT_FILTER          (BWGRAPH_LINE_SEND|BWGRAPH_LINE_RECV)
00038 #define DEFAULT_ALWAYS_ON_TOP   false
00039 #define DEFAULT_OPACITY         100
00040 
00041 #define ADD_TO_FILTER(f,v,b)  (f = ((b) ? ((f) | (v)) : ((f) & ~(v))))
00042 
00043 /* Define the format used for displaying the date and time */
00044 #define DATETIME_FMT  "MMM dd hh:mm:ss"
00045 
00046 
00047 /** Default constructor */
00048 BandwidthGraph::BandwidthGraph(QWidget *parent, Qt::WFlags flags)
00049   : VidaliaWindow("BandwidthGraph", parent, flags)
00050 {
00051   /* Invoke Qt Designer generated QObject setup routine */
00052   ui.setupUi(this);
00053 #if defined(Q_WS_MAC)
00054   setShortcut("Ctrl+W", SLOT(close()));
00055 #else
00056   setShortcut("Esc", SLOT(close()));
00057 #endif
00058 
00059   /* Bind events to actions */
00060   createActions();
00061 
00062   /* Ask Tor to notify us about bandwidth updates */
00063   _torControl = Vidalia::torControl();
00064   _torControl->setEvent(TorEvents::Bandwidth, this, true);
00065 
00066   /* Initialize Sent/Receive data counters */
00067   reset();
00068   /* Hide Bandwidth Graph Settings frame */
00069   showSettingsFrame(false);
00070   /* Load the previously saved settings */
00071   loadSettings();
00072 
00073   /* Turn off opacity group on unsupported platforms */
00074 #if defined(Q_WS_WIN)
00075   if(!(QSysInfo::WV_2000 <= QSysInfo::WindowsVersion <= QSysInfo::WV_2003)) {
00076     ui.frmOpacity->setVisible(false);
00077   }
00078 #endif
00079   
00080 #if defined(Q_WS_X11)
00081   ui.frmOpacity->setVisible(false);
00082 #endif
00083 }
00084 
00085 /** Custom event handler. Checks if the event is a bandwidth update event. If it
00086  * is, it will add the data point to the history and updates the graph. */
00087 void
00088 BandwidthGraph::customEvent(QEvent *event)
00089 {
00090   if (event->type() == CustomEventType::BandwidthEvent) {
00091     BandwidthEvent *bw = (BandwidthEvent *)event;
00092     updateGraph(bw->bytesRead(), bw->bytesWritten());
00093   }
00094 }
00095 
00096 /** Binds events to actions. */
00097 void
00098 BandwidthGraph::createActions()
00099 {
00100   connect(ui.btnToggleSettings, SIGNAL(toggled(bool)),
00101       this, SLOT(showSettingsFrame(bool)));
00102 
00103   connect(ui.btnReset, SIGNAL(clicked()),
00104       this, SLOT(reset()));
00105 
00106   connect(ui.btnSaveSettings, SIGNAL(clicked()),
00107       this, SLOT(saveChanges()));
00108 
00109   connect(ui.btnCancelSettings, SIGNAL(clicked()),
00110       this, SLOT(cancelChanges()));
00111   
00112   connect(ui.sldrOpacity, SIGNAL(valueChanged(int)),
00113       this, SLOT(setOpacity(int)));
00114 }
00115 
00116 /** Adds new data to the graph. */
00117 void
00118 BandwidthGraph::updateGraph(quint64 bytesRead, quint64 bytesWritten)
00119 {
00120   /* Graph only cares about kilobytes */
00121   ui.frmGraph->addPoints(bytesRead/1024.0, bytesWritten/1024.0);
00122 }
00123 
00124 /** Loads the saved Bandwidth Graph settings. */
00125 void
00126 BandwidthGraph::loadSettings()
00127 {
00128   /* Set window opacity slider widget */
00129   ui.sldrOpacity->setValue(getSetting(SETTING_OPACITY, DEFAULT_OPACITY).toInt());
00130   setOpacity(ui.sldrOpacity->value());
00131 
00132   /* Set whether the window appears on top. */
00133   ui.chkAlwaysOnTop->setChecked(getSetting(SETTING_ALWAYS_ON_TOP,
00134                                            DEFAULT_ALWAYS_ON_TOP).toBool());
00135   if (ui.chkAlwaysOnTop->isChecked()) {
00136     setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
00137   } else {
00138     setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
00139   }
00140 
00141   /* Set the line filter checkboxes accordingly */
00142   uint filter = getSetting(SETTING_FILTER, DEFAULT_FILTER).toUInt();
00143   ui.chkReceiveRate->setChecked(filter & BWGRAPH_LINE_RECV);
00144   ui.chkSendRate->setChecked(filter & BWGRAPH_LINE_SEND);
00145 
00146   /* Set graph frame settings */
00147   ui.frmGraph->setShowCounters(ui.chkReceiveRate->isChecked(),
00148                                ui.chkSendRate->isChecked());
00149 }
00150 
00151 /** Resets the log start time. */
00152 void
00153 BandwidthGraph::reset()
00154 {
00155   /* Set to current time */
00156   ui.statusbar->showMessage(tr("Since:") + " " + 
00157                             QDateTime::currentDateTime()
00158                             .toString(DATETIME_FMT));
00159   /* Reset the graph */
00160   ui.frmGraph->resetGraph();
00161 }
00162 
00163 /** Saves the Bandwidth Graph settings and adjusts the graph if necessary. */
00164 void
00165 BandwidthGraph::saveChanges()
00166 {
00167   /* Hide the settings frame and reset toggle button */
00168   showSettingsFrame(false);
00169   
00170   /* Save the opacity */
00171   saveSetting(SETTING_OPACITY, ui.sldrOpacity->value());
00172 
00173   /* Save the Always On Top setting */
00174   saveSetting(SETTING_ALWAYS_ON_TOP, ui.chkAlwaysOnTop->isChecked());
00175   if (ui.chkAlwaysOnTop->isChecked()) {
00176     setWindowFlags(windowFlags() | Qt::WindowStaysOnTopHint);
00177   } else {
00178     setWindowFlags(windowFlags() & ~Qt::WindowStaysOnTopHint);
00179   }
00180   setOpacity(ui.sldrOpacity->value());
00181 
00182   /* Save the line filter values */
00183   uint filter = 0;
00184   ADD_TO_FILTER(filter, BWGRAPH_LINE_RECV, ui.chkReceiveRate->isChecked());
00185   ADD_TO_FILTER(filter, BWGRAPH_LINE_SEND, ui.chkSendRate->isChecked());
00186   saveSetting(SETTING_FILTER, filter);
00187 
00188 
00189   /* Update the graph frame settings */
00190   ui.frmGraph->setShowCounters(ui.chkReceiveRate->isChecked(),
00191                                ui.chkSendRate->isChecked());
00192 
00193   /* A change in window flags causes the window to disappear, so make sure
00194    * it's still visible. */
00195   showNormal();
00196 }
00197 
00198 /** Simply restores the previously saved settings. */
00199 void 
00200 BandwidthGraph::cancelChanges()
00201 {
00202   /* Hide the settings frame and reset toggle button */
00203   showSettingsFrame(false);
00204 
00205   /* Reload the settings */
00206   loadSettings();
00207 }
00208 
00209 /** Toggles the Settings pane on and off, changes toggle button text. */
00210 void
00211 BandwidthGraph::showSettingsFrame(bool show)
00212 {
00213   static QSize minSize = minimumSize();
00214   
00215   QSize newSize = size();
00216   if (show) {
00217     /* Extend the bottom of the bandwidth graph and show the settings */
00218     ui.frmSettings->setVisible(true);
00219     ui.btnToggleSettings->setChecked(true);
00220     ui.btnToggleSettings->setText(tr("Hide Settings"));
00221 
00222     /* 6 = vertical spacing between the settings frame and graph frame */
00223     newSize.setHeight(newSize.height() + ui.frmSettings->height() + 6);
00224   } else {
00225     /* Shrink the height of the bandwidth graph and hide the settings */
00226     ui.frmSettings->setVisible(false);
00227     ui.btnToggleSettings->setChecked(false);
00228     ui.btnToggleSettings->setText(tr("Show Settings"));
00229     
00230     /* 6 = vertical spacing between the settings frame and graph frame */
00231     newSize.setHeight(newSize.height() - ui.frmSettings->height() - 6);
00232     setMinimumSize(minSize);
00233   }
00234   resize(newSize);
00235 }
00236 
00237 /** Sets the opacity of the Bandwidth Graph window. */
00238 void
00239 BandwidthGraph::setOpacity(int value)
00240 {
00241   qreal newValue = value / 100.0;
00242   
00243   /* Opacity only supported by Mac and Win32 */
00244 #if defined(Q_WS_MAC)
00245   this->setWindowOpacity(newValue);
00246   ui.lblPercentOpacity->setText(QString::number(value));
00247 #elif defined(Q_WS_WIN)
00248   if(QSysInfo::WV_2000 <= QSysInfo::WindowsVersion <= QSysInfo::WV_2003) {
00249     this->setWindowOpacity(newValue);
00250     ui.lblPercentOpacity->setText(QString::number(value));
00251   }
00252 #else
00253   Q_UNUSED(newValue);
00254 #endif
00255 }
00256 
00257 /** Overloads the default show() slot so we can set opacity. */
00258 void
00259 BandwidthGraph::show()
00260 {
00261   /* Load saved settings */
00262   loadSettings();
00263   /* Show the window */
00264   VidaliaWindow::show();
00265 }
00266 

Generated on Mon Oct 23 20:08:15 2006 for Vidalia by  doxygen 1.5.0