main.cpp

Go to the documentation of this file.
00001 /****************************************************************
00002  *  Vidalia is distributed under the following license:
00003  *
00004  *  Copyright (C) 2006-2007,  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 1878 2007-08-26 03:23:17Z 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 #if defined(Q_OS_WIN32)
00036 #include <QSysInfo>
00037 #endif
00038 
00039 /** Returns true if there is already another Vidalia process running. */
00040 bool
00041 is_vidalia_running(QString pidfile)
00042 {
00043   /* Read the pidfile and find out if that process still exists */
00044   qint64 pid = read_pidfile(pidfile);
00045   if (pid > 0) {
00046 #if defined(Q_OS_WIN32)
00047     if (QSysInfo::WindowsVersion == QSysInfo::WV_NT) {
00048       /* We currently can't get a list of running processes on Windows NT, so
00049        * be pessimistic and assume the existence of a nonzero pidfile means
00050        * Vidalia is running. */
00051       return true;
00052     } else
00053       return (is_process_running(pid));
00054 #else
00055     return (is_process_running(pid));
00056 #endif
00057   }
00058   return false;
00059 }
00060 
00061 /** Main application entry point. */
00062 int
00063 main(int argc, char *argv[])
00064 {
00065   Q_INIT_RESOURCE(vidalia_common);
00066   QStringList args = char_array_to_stringlist(argv+1, argc-1);
00067 
00068   /* Construct the application object. Qt strips any command-line arguments
00069    * that it recognizes in argv, so we'll pass a stringlist of the original
00070    * list of command-line arguments too. */
00071   Vidalia vidalia(args, argc, argv);
00072   vNotice("Vidalia %1 using Qt %2").arg(Vidalia::version())
00073                                    .arg(QT_VERSION_STR);
00074 
00075   /* Validate any command-line arguments, or show usage message box, if
00076    * necessary. */
00077   QString errmsg;
00078   if (vidalia.showUsage()) {
00079     Vidalia::showUsageMessageBox();
00080     return 0;
00081   } else if (!vidalia.validateArguments(errmsg)) {
00082     vError("Unable to apply command-line arguments: %1").arg(errmsg);
00083     VMessageBox::critical(0,
00084       vApp->translate("Vidalia",
00085         QT_TRANSLATE_NOOP("Vidalia", "Invalid Argument")), errmsg,
00086       VMessageBox::Ok);
00087     return 1;
00088   }
00089 
00090   /* Check if Vidalia is already running. */
00091   QString pidfile = vidalia.pidFile();
00092   if (is_vidalia_running(pidfile)) {
00093     vWarn("Detected another process with pid %1. Is Vidalia already running?")
00094                                                                .arg(get_pid());
00095     /* Let the user know another Vidalia is running and we are going to exit
00096      * now. */
00097     int ret = VMessageBox::critical(0, 
00098                 vApp->translate("Vidalia",
00099                   QT_TRANSLATE_NOOP("Vidalia", "Vidalia is already running")),
00100                 vApp->translate("Vidalia",
00101                   QT_TRANSLATE_NOOP("Vidalia", 
00102                     "Another Vidalia process is possibly already running. "
00103                     "If there really is not another Vidalia process running, "
00104                     "you can choose to continue anyway.\n\n"
00105                     "Would you like to continue starting Vidalia?")),
00106                 VMessageBox::Continue, VMessageBox::Quit|VMessageBox::Default);
00107     if (ret != VMessageBox::Continue) {
00108       /* Don't start a second instance of Vidalia */
00109       vError("Exiting duplicate Vidalia process.");
00110       return 1;
00111     }
00112   }
00113   write_pidfile(pidfile);
00114 
00115   /* Since we don't have a visible main window, if we were to display a
00116    * QMessageBox (for example, to display an error when starting or stopping
00117    * Tor) then the application would exit when that message box was closed.
00118    * Setting quitOnLastWindowClosed to false fixes this behavior. */
00119   Vidalia::setQuitOnLastWindowClosed(false);
00120 
00121   /* Create an instance of the main window  */
00122   MainWindow mainWin;
00123 
00124   /* Run Vidalia */
00125   int ret = vidalia.exec();
00126 
00127   /* Vidalia exited, so cleanup our pidfile and return */
00128   QFile::remove(pidfile);
00129   vNotice("Vidalia is exiting cleanly (return code %1).").arg(ret);
00130   return ret;
00131 }
00132 

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