main.cpp

Go to the documentation of this file.
00001 /*
00002 **  This file is part of Vidalia, and is subject to the license terms in the
00003 **  LICENSE file, found in the top level directory of this distribution. If you
00004 **  did not receive the LICENSE file with this file, you may obtain it from the
00005 **  Vidalia source package distributed by the Vidalia Project at
00006 **  http://www.vidalia-project.net/. No part of Vidalia, including this file,
00007 **  may be copied, modified, propagated, or distributed except according to the
00008 **  terms described in the LICENSE file.
00009 */
00010 
00011 /*
00012 ** \file main.cpp
00013 ** \version $Id: main.cpp 2648 2008-06-03 03:34:42Z edmanm $
00014 ** \brief Main Vidalia entry point
00015 */
00016 
00017 #include <QObject>
00018 #include <vidalia.h>
00019 #include <mainwindow.h>
00020 #include <vmessagebox.h>
00021 #include <procutil.h>
00022 #include <stringutil.h>
00023 
00024 #if defined(Q_OS_WIN32)
00025 #include <QSysInfo>
00026 #endif
00027 
00028 /** Returns true if there is already another Vidalia process running. */
00029 bool
00030 is_vidalia_running(QString pidfile)
00031 {
00032   /* Read the pidfile and find out if that process still exists */
00033   qint64 pid = read_pidfile(pidfile);
00034   if (pid > 0) {
00035 #if defined(Q_OS_WIN32)
00036     if (QSysInfo::WindowsVersion == QSysInfo::WV_NT) {
00037       /* We currently can't get a list of running processes on Windows NT, so
00038        * be pessimistic and assume the existence of a nonzero pidfile means
00039        * Vidalia is running. */
00040       return true;
00041     } else
00042       return (is_process_running(pid));
00043 #else
00044     return (is_process_running(pid));
00045 #endif
00046   }
00047   return false;
00048 }
00049 
00050 /** Main application entry point. */
00051 int
00052 main(int argc, char *argv[])
00053 {
00054   Q_INIT_RESOURCE(vidalia);
00055   QStringList args = char_array_to_stringlist(argv+1, argc-1);
00056 
00057   /* Construct the application object. Qt strips any command-line arguments
00058    * that it recognizes in argv, so we'll pass a stringlist of the original
00059    * list of command-line arguments too. */
00060   Vidalia vidalia(args, argc, argv);
00061   vNotice("Vidalia %1 using Qt %2").arg(Vidalia::version())
00062                                    .arg(QT_VERSION_STR);
00063 
00064   /* Validate any command-line arguments, or show usage message box, if
00065    * necessary. */
00066   QString errmsg;
00067   if (vidalia.showUsage()) {
00068     Vidalia::showUsageMessageBox();
00069     return 0;
00070   } else if (!vidalia.validateArguments(errmsg)) {
00071     vError("Unable to apply command-line arguments: %1").arg(errmsg);
00072     VMessageBox::critical(0,
00073       vApp->translate("Vidalia",
00074         QT_TRANSLATE_NOOP("Vidalia", "Invalid Argument")), errmsg,
00075       VMessageBox::Ok);
00076     return 1;
00077   }
00078 
00079   /* Check if Vidalia is already running. */
00080   QString pidfile = vidalia.pidFile();
00081   if (is_vidalia_running(pidfile)) {
00082     vWarn("Detected another process with pid %1. Is Vidalia already running?")
00083                                                                .arg(get_pid());
00084     /* Let the user know another Vidalia is running and we are going to exit
00085      * now. */
00086     int ret = VMessageBox::critical(0, 
00087                 vApp->translate("Vidalia",
00088                   QT_TRANSLATE_NOOP("Vidalia", "Vidalia is already running")),
00089                 vApp->translate("Vidalia",
00090                   QT_TRANSLATE_NOOP("Vidalia", 
00091                     "Another Vidalia process is possibly already running. "
00092                     "If there really is not another Vidalia process running, "
00093                     "you can choose to continue anyway.\n\n"
00094                     "Would you like to continue starting Vidalia?")),
00095                 VMessageBox::Continue, VMessageBox::Quit|VMessageBox::Default);
00096     if (ret != VMessageBox::Continue) {
00097       /* Don't start a second instance of Vidalia */
00098       vError("Exiting duplicate Vidalia process.");
00099       return 1;
00100     }
00101   }
00102   write_pidfile(pidfile);
00103 
00104   /* Since we don't have a visible main window, if we were to display a
00105    * QMessageBox (for example, to display an error when starting or stopping
00106    * Tor) then the application would exit when that message box was closed.
00107    * Setting quitOnLastWindowClosed to false fixes this behavior. */
00108   Vidalia::setQuitOnLastWindowClosed(false);
00109 
00110   /* Create an instance of the main window  */
00111   MainWindow mainWin;
00112 
00113   /* Run Vidalia */
00114   int ret = vidalia.run();
00115 
00116   /* Vidalia exited, so cleanup our pidfile and return */
00117   QFile::remove(pidfile);
00118   vNotice("Vidalia is exiting cleanly (return code %1).").arg(ret);
00119   return ret;
00120 }
00121 

Generated on Wed Dec 23 21:06:55 2009 for Vidalia by  doxygen 1.6.1