main.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 main.cpp
00024  * \version $Id: main.cpp 1257 2006-10-02 00:33:50Z edmanm $
00025  * \brief Main Vidalia entry point
00026  */
00027 
00028 #include <QObject>
00029 #include <vidalia.h>
00030 #include <gui/mainwindow.h>
00031 #include <gui/common/vmessagebox.h>
00032 #include <util/process.h>
00033 #include <util/string.h>
00034 
00035 
00036 /** Returns true if there is already another Vidalia process running. */
00037 bool
00038 is_vidalia_running(QString pidfile)
00039 {
00040   /* Read the pidfile and find out if that process still exists */
00041   qint64 pid = read_pidfile(pidfile);
00042   if (pid > 0) {
00043     return (is_process_running(pid));
00044   }
00045   return false;
00046 }
00047 
00048 /** Main application entry point. */
00049 int
00050 main(int argc, char *argv[])
00051 {
00052   Q_INIT_RESOURCE(vidalia_common);
00053   QStringList args = char_array_to_stringlist(argv+1, argc-1);
00054 
00055   /* Construct the application object. Qt strips any command-line arguments
00056    * that it recognizes in argv, so we'll pass a stringlist of the original
00057    * list of command-line arguments too. */
00058   Vidalia vidalia(args, argc, argv);
00059 
00060 
00061 #if !defined(Q_OS_WIN32)
00062   /* Validate any command-line arguments. Don't bother doing this on Win32
00063    * since they can't see the output anyway. */
00064   QString errmsg;
00065   if (!vidalia.validateArguments(errmsg)) {
00066     vidalia.printUsage(errmsg);
00067     return -1;
00068   }
00069 #endif
00070 
00071   /* Check if Vidalia is already running. */
00072   QString pidfile = vidalia.pidFile();
00073   if (is_vidalia_running(pidfile)) {
00074     /* Let the user know another Vidalia is running and we are going to exit
00075      * now. */
00076     VMessageBox::critical(0, 
00077       qApp->translate("Vidalia",
00078         QT_TRANSLATE_NOOP("Vidalia", "Vidalia is already running")),
00079       qApp->translate("Vidalia",
00080         QT_TRANSLATE_NOOP("Vidalia", 
00081           "Another Vidalia process is already running. \n\n"
00082           "This Vidalia process will now exit.")),
00083       VMessageBox::Ok);   
00084     return 0;
00085   }
00086   write_pidfile(pidfile);
00087 
00088   /* Since we don't have a visible main window, if we were to display a
00089    * QMessageBox (for example, to display an error when starting or stopping
00090    * Tor) then the application would exit when that message box was closed.
00091    * Setting quitOnLastWindowClosed to false fixes this behavior. */
00092   Vidalia::setQuitOnLastWindowClosed(false);
00093 
00094   /* Create an instance of the mainwindow and start the application */
00095   MainWindow mainWin;
00096 
00097   /* Handle the shutdown signal by closing the mainwindow, which closes
00098    * all the necessary child windows. */
00099   QObject::connect(&vidalia, SIGNAL(shutdown()), &mainWin, SLOT(close()));
00100 
00101   /* Run Vidalia */
00102   int ret = vidalia.exec();
00103 
00104   /* Vidalia exited, so cleanup our pidfile and return */
00105   QFile::remove(pidfile);
00106   return ret;
00107 }
00108 

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