Changeset 86

Show
Ignore:
Timestamp:
01/14/06 00:10:41 (3 years ago)
Author:
edmanm
Message:

Vidalia can now start and stop Tor. Upon starting Tor, Vidalia will try to
connect the control socket and authenticate. If Tor stops, Vidalia checks to
see if the Tor process exited cleanly or if it crashed or threw an error.
Also, we now use the 128x128 icons on Mac so the application icon looks better
in the dock.

Location:
trunk/src/gui
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/gui/mainwindow.cpp

    r81 r86  
    2525 
    2626#include "mainwindow.h" 
    27  
     27#include "../util/compat.h" 
     28 
     29/* On Mac, we need to put the 128x128 icon in the dock */ 
     30#if defined(Q_WS_MAC) 
     31#define IMG_TOR_STOPPED    ":/images/tor_off128.png" 
     32#define IMG_TOR_RUNNING    ":/images/tor_on128.png" 
     33#else 
     34#define IMG_TOR_STOPPED    ":/images/tor_off32.png" 
     35#define IMG_TOR_RUNNING    ":/images/tor_on32.png" 
     36#endif 
     37 
     38 
     39/** Default constructor */ 
    2840MainWindow::MainWindow() 
    2941{ 
    3042  /* Set Vidalia's application icon */ 
    31   setWindowIcon(QIcon(":/images/tor_on32.png")); 
    32    
     43  setWindowIcon(QIcon(IMG_TOR_STOPPED)); 
     44  QApplication::setWindowIcon(QIcon(IMG_TOR_STOPPED)); 
     45 
    3346  createActions(); 
    3447  createMenus(); 
     
    3649  /* Create a new TorControl object, used to communicate with and manipulate Tor */ 
    3750  _torControl = new TorControl(); 
    38    
     51  connect(_torControl, SIGNAL(started()), this, SLOT(started())); 
     52  connect(_torControl, SIGNAL(stopped(int, QProcess::ExitStatus)), 
     53                 this, SLOT(stopped(int, QProcess::ExitStatus))); 
     54 
    3955  /* Put an icon in the system tray to indicate the status of Tor */ 
    40   _trayIcon = new TrayIcon(QPixmap(":/images/tor_on32.png"), 
    41                            tr("Tor is Started"), _trayMenu, this); 
     56  _trayIcon = new TrayIcon(QPixmap(IMG_TOR_STOPPED), 
     57                           tr("Tor is Stopped"), _trayMenu, this); 
    4258  _trayIcon->show(); 
    4359} 
    4460 
     61/** Default destructor */ 
    4562MainWindow::~MainWindow() 
    4663{ 
     
    5875  _startAct = new QAction(tr("Start"), this); 
    5976  connect(_startAct, SIGNAL(triggered()), this, SLOT(start())); 
    60   _startAct->setEnabled(false); 
     77  _startAct->setEnabled(true); 
    6178   
    6279  _stopAct = new QAction(tr("Stop"), this); 
    6380  connect(_stopAct, SIGNAL(triggered()), this, SLOT(stop())); 
     81  _stopAct->setEnabled(false); 
    6482 
    6583  _configAct = new QAction(tr("Configure"), this); 
     
    118136void MainWindow::start() 
    119137{ 
    120   /* Set correct tray icon */ 
    121   _trayIcon->setIcon(QPixmap(":/images/tor_on32.png")); 
    122    
    123   /* Set ToolTip */ 
    124   _trayIcon->setToolTip(QString("Tor is started")); 
     138  QString errmsg; 
     139  if (!_torControl->start(&errmsg)) { 
     140    QMessageBox::warning(this, tr("Error Starting Tor"), 
     141       tr("Vidalia was unable to start Tor.\r\n\r\nError: ") + errmsg, 
     142       QMessageBox::Ok, QMessageBox::NoButton); 
     143  } 
     144} 
     145 
     146/** Slot: Called when the Tor process is started. It will connect the control 
     147 * socket and set the icons and tooltips accordingly. */ 
     148void MainWindow::started() 
     149{ 
     150  /* Set the window icon */ 
     151  QApplication::setWindowIcon(QIcon(IMG_TOR_RUNNING)); 
     152   
     153  /* Set correct tray icon and tooltip */ 
     154  _trayIcon->setIcon(QPixmap(IMG_TOR_RUNNING)); 
     155  _trayIcon->setToolTip(tr("Tor is started")); 
    125156 
    126157  /* Set menu actions appropriately */ 
     
    128159  _stopAct->setEnabled(true); 
    129160 
    130   /* FIXME Add code for starting Tor */ 
    131  
    132 } 
    133  
    134 /* 
    135  Stops Tor, modifies tray icon and tray menu appropriately 
     161  /* Try to connect to Tor's control port */ 
     162  QString errmsg; 
     163  if (!_torControl->connect(&errmsg)) { 
     164    /* There's a possibility that Tor has started, but it just hasn't had a 
     165     * chance to bind the control socket yet. So pause for a second and try 
     166     * again.*/ 
     167    v_sleep(1); 
     168    if(!_torControl->connect(&errmsg)) { 
     169      /* Ok, ok. It really isn't going to connect. I give up. */ 
     170      QMessageBox::warning(this, tr("Error Connecting to Tor"), 
     171         tr("Tor started successfully, but Vidalia was unable to " 
     172            "connect to it. Check your control port settings and try " 
     173            "again.\r\n\r\n") + errmsg, 
     174         QMessageBox::Ok, QMessageBox::NoButton); 
     175    } 
     176  } else { 
     177    /* The controller connected, so now send the AUTHENTICATE command */ 
     178    if (!_torControl->authenticate(&errmsg)) { 
     179      QMessageBox::warning(this, tr("Authentication Error"), 
     180        tr("Vidalia was unable to authenticate itself to Tor." 
     181           "Check your authentication information and try again." 
     182           "\r\n\r\nError: ") + errmsg, 
     183        QMessageBox::Ok, QMessageBox::NoButton); 
     184    } 
     185  } 
     186} 
     187 
     188/* 
     189 Stops Tor 
    136190*/ 
    137191void MainWindow::stop() 
    138192{ 
    139   /* Set correct tray icon */ 
    140   _trayIcon->setIcon(QPixmap(":/images/tor_off32.png")); 
    141  
    142   /* Set ToolTip */ 
    143   _trayIcon->setToolTip(QString("Tor is stopped")); 
     193  QString errmsg; 
     194   
     195  /* Disconnect the controller. Note that we don't check any error messages or 
     196   * return values, since we'll just be killing Tor anyway. */ 
     197  if (_torControl->isConnected()) { 
     198    _torControl->disconnect(); 
     199  } 
     200 
     201  /* Stop the Tor process */ 
     202  _isIntentionalExit = true; 
     203  if (!_torControl->stop(&errmsg)) { 
     204    QMessageBox::warning(this, tr("Error Stopping Tor"), 
     205       tr("Vidalia was unable to stop Tor.\r\n\r\nError: ") + errmsg, 
     206       QMessageBox::Ok, QMessageBox::NoButton); 
     207  } 
     208  _isIntentionalExit = false; 
     209} 
     210 
     211/** Slot: Called when the Tor process has exited. It will adjust the tray 
     212 * icons and tooltips accordingly. */ 
     213void MainWindow::stopped(int exitCode, QProcess::ExitStatus exitStatus) 
     214{ 
     215  /* Set the window icon */ 
     216  QApplication::setWindowIcon(QIcon(IMG_TOR_STOPPED)); 
     217   
     218  /* Set correct tray icon and tooltip */ 
     219  _trayIcon->setIcon(QPixmap(IMG_TOR_STOPPED)); 
     220  _trayIcon->setToolTip(tr("Tor is stopped")); 
    144221 
    145222  /* Set menu actions appropriately */ 
     
    147224  _startAct->setEnabled(true); 
    148225 
    149   /* FIXME Add code for stopping Tor */ 
    150    
     226  /* Check if the Tor process fell over or exited cleanly */ 
     227  if (exitStatus == QProcess::CrashExit) { 
     228    if (!_isIntentionalExit) { 
     229      QMessageBox::warning(this, tr("Tor Crashed"), 
     230        tr("Vidalia detected that the Tor process crashed. " 
     231            "Please check the message log."), 
     232         QMessageBox::Ok, QMessageBox::NoButton); 
     233    } 
     234  } else if (exitCode != 0) { 
     235    /* A quick overview of Tor's code tells me that if it catches a SIGTERM or 
     236     * SIGINT, Tor will exit(0). We might need to change this warning message 
     237     * if this turns out to not be the case. */ 
     238    QMessageBox::warning(this, tr("Tor Exited"), 
     239       tr("Tor exited and returned a non-zero exit code. " 
     240          "Please check the message log."), 
     241       QMessageBox::Ok, QMessageBox::NoButton); 
     242  } 
    151243} 
    152244 
  • trunk/src/gui/mainwindow.h

    r82 r86  
    4242private slots: 
    4343  void start(); 
     44  void started(); 
    4445  void stop(); 
     46  void stopped(int errorCode, QProcess::ExitStatus exitStatus); 
    4547  void about(); 
    4648 
     
    4951  void createActions(); 
    5052 
     53  /* Used to determine if the Tor process exiting was intentional or not */ 
     54  bool _isIntentionalExit; 
     55   
    5156  TorControl* _torControl; 
    5257