configdialog.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 configdialog.cpp
00024  * \version $Id: configdialog.cpp 1563 2006-12-26 06:06:04Z edmanm $
00025  * \brief Contains a series of Vidalia and Tor configuration pages
00026  */
00027 
00028 #include <gui/common/vmessagebox.h>
00029 #include <vidalia.h>
00030 
00031 #include "configdialog.h"
00032 
00033 #define FONT        QFont(tr("Arial"), 10)
00034 
00035 /* Images for toolbar icons */
00036 #define IMAGE_GENERAL       ":/images/22x22/preferences-system.png"
00037 #define IMAGE_SERVER        ":/images/22x22/network-server.png"
00038 #define IMAGE_APPEARANCE    ":/images/22x22/preferences-desktop-locale.png"
00039 #define IMAGE_ADVANCED      ":/images/22x22/emblem-system.png"
00040 #define IMAGE_SAVE          ":/images/22x22/media-floppy.png"
00041 #define IMAGE_CANCEL        ":/images/22x22/emblem-unreadable.png"
00042 #define IMAGE_HELP          ":/images/22x22/help-browser.png"
00043 
00044 /** Constructor */
00045 ConfigDialog::ConfigDialog(QWidget* parent)
00046 : VidaliaWindow("ConfigDialog", parent)
00047 {
00048   QAction *helpAct, *saveAct, *cancelAct;
00049 
00050   /* Invoke the Qt Designer generated QObject setup routine */
00051   ui.setupUi(this);
00052 
00053   /* Create the config pages and actions */
00054   QActionGroup *grp = new QActionGroup(this);
00055   ui.stackPages->add(new GeneralPage(ui.stackPages),
00056                      createPageAction(QIcon(IMAGE_GENERAL), tr("General"), grp));
00057   
00058   ui.stackPages->add(new ServerPage(ui.stackPages),
00059                      createPageAction(QIcon(IMAGE_SERVER), tr("Server"), grp));
00060   
00061   ui.stackPages->add(new AppearancePage(ui.stackPages),
00062                      createPageAction(QIcon(IMAGE_APPEARANCE), tr("Appearance"), grp));
00063   
00064   ui.stackPages->add(new AdvancedPage(ui.stackPages),
00065                      createPageAction(QIcon(IMAGE_ADVANCED), tr("Advanced"), grp));
00066   
00067   /* Create the toolbar */
00068   ui.toolBar->addActions(grp->actions());
00069   ui.toolBar->addSeparator();
00070   connect(grp, SIGNAL(triggered(QAction *)), ui.stackPages, SLOT(showPage(QAction *)));
00071   
00072   
00073   /* Create and bind the Save button */
00074   helpAct = new QAction(QIcon(IMAGE_HELP), tr("Help"), ui.toolBar);
00075   addAction(helpAct, SLOT(help()));
00076   
00077   /* Create and bind the Save button */  
00078   saveAct = new QAction(QIcon(IMAGE_SAVE), tr("Save"), ui.toolBar);
00079   saveAct->setShortcut(QString("Ctrl+S"));
00080   addAction(saveAct, SLOT(saveChanges()));
00081   
00082   /* Create and bind the Cancel button */
00083   cancelAct = new QAction(QIcon(IMAGE_CANCEL), tr("Cancel"), ui.toolBar);
00084   addAction(cancelAct, SLOT(close()));
00085   
00086   /* Select the first action */
00087   grp->actions()[0]->setChecked(true);
00088 
00089 #if defined(Q_WS_WIN)
00090   helpAct->setShortcut(QString("F1"));
00091   cancelAct->setShortcut(QString("Esc"));
00092 #else
00093   helpAct->setShortcut(QString("Ctrl+?"));
00094   cancelAct->setShortcut(QString("Ctrl+W"));
00095 #endif
00096 }
00097 
00098 /** Creates a new action associated with a config page. */
00099 QAction*
00100 ConfigDialog::createPageAction(QIcon img, QString text, QActionGroup *group)
00101 {
00102   QAction *action = new QAction(img, text, group);
00103   action->setCheckable(true);
00104   action->setFont(FONT);
00105   return action;
00106 }
00107 
00108 /** Adds the given action to the toolbar and hooks its triggered() signal to
00109  * the specified slot (if given). */
00110 void
00111 ConfigDialog::addAction(QAction *action, const char *slot)
00112 {
00113   action->setFont(FONT);
00114   ui.toolBar->addAction(action);
00115   connect(action, SIGNAL(triggered()), this, slot);
00116 }
00117 
00118 /** Shows the config dialog with focus set to the given page. */
00119 void
00120 ConfigDialog::showWindow(Page page)
00121 {
00122   /* Load saved settings */
00123   loadSettings();
00124   /* Show the dialog. */
00125   VidaliaWindow::showWindow();
00126   /* Set the focus to the specified page. */
00127   ui.stackPages->setCurrentIndex((int)page);
00128 }
00129 
00130 /** Loads the saved ConfigDialog settings. */
00131 void
00132 ConfigDialog::loadSettings()
00133 {
00134   /* Call each config page's load() method to load its data */
00135   foreach (ConfigPage *page, ui.stackPages->pages()) {
00136     page->load();
00137   }
00138 }
00139 
00140 /** Saves changes made to settings. */
00141 void
00142 ConfigDialog::saveChanges()
00143 {
00144   QString errmsg;
00145   
00146   /* Call each config page's save() method to save its data */
00147   foreach (ConfigPage *page, ui.stackPages->pages()) {
00148     if (!page->save(errmsg)) {
00149       /* Display the offending page */
00150       ui.stackPages->setCurrentPage(page);
00151       
00152       /* Show the user what went wrong */
00153       VMessageBox::warning(this, 
00154         tr("Error Saving Configuration"), errmsg,
00155         VMessageBox::Ok);
00156 
00157       /* Don't process the rest of the pages */
00158       return;
00159     }
00160   }
00161   QMainWindow::close();
00162 }
00163 
00164 /** Shows help information about the configuration dialog. */
00165 void
00166 ConfigDialog::help()
00167 {
00168   Vidalia::help("config");
00169 }
00170 

Generated on Wed Sep 5 15:49:27 2007 for Vidalia by  doxygen 1.5.3