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 1238 2006-09-25 17:50:57Z 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(cancelChanges()));
00085   
00086   /* Select the first action */
00087   grp->actions()[0]->setChecked(true);
00088 
00089 #if defined(Q_WS_MAC)
00090   helpAct->setShortcut(QString("Ctrl+?"));
00091   cancelAct->setShortcut(QString("Ctrl+W"));
00092 #else
00093   helpAct->setShortcut(QString("F1"));
00094   cancelAct->setShortcut(QString("Esc"));
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 /** Overloads the default show so we can load settings */
00119 void
00120 ConfigDialog::show()
00121 {
00122   /* Load saved settings */
00123   loadSettings();
00124   /* Show the window */
00125   VidaliaWindow::show();
00126 }
00127 
00128 /** Shows the config dialog with focus set to the given page. */
00129 void
00130 ConfigDialog::show(Page page)
00131 {
00132   /* Show the dialog. */
00133   show();
00134 
00135   /* Set the focus to the specified page. */
00136   ui.stackPages->setCurrentIndex((int)page);
00137 }
00138 
00139 /** Loads the saved ConfigDialog settings. */
00140 void
00141 ConfigDialog::loadSettings()
00142 {
00143   /* Call each config page's load() method to load its data */
00144   foreach (ConfigPage *page, ui.stackPages->pages()) {
00145     page->load();
00146   }
00147 }
00148 
00149 /** Cancels changes made to settings. */
00150 void
00151 ConfigDialog::cancelChanges()
00152 {
00153   /* Reload saved settings and exit */
00154   loadSettings();
00155   QMainWindow::close();
00156 }
00157 
00158 /** Saves changes made to settings. */
00159 void
00160 ConfigDialog::saveChanges()
00161 {
00162   QString errmsg;
00163   
00164   /* Call each config page's save() method to save its data */
00165   foreach (ConfigPage *page, ui.stackPages->pages()) {
00166     if (!page->save(errmsg)) {
00167       /* Display the offending page */
00168       ui.stackPages->setCurrentPage(page);
00169       
00170       /* Show the user what went wrong */
00171       VMessageBox::warning(this, 
00172         tr("Error Saving Configuration"), errmsg,
00173         VMessageBox::Ok);
00174 
00175       /* Don't process the rest of the pages */
00176       return;
00177     }
00178   }
00179   QMainWindow::close();
00180 }
00181 
00182 /** Shows help information about the configuration dialog. */
00183 void
00184 ConfigDialog::help()
00185 {
00186   Vidalia::help("config");
00187 }
00188 

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