00001 #include <qapplication.h>
00002 #include <cstdlib>
00003 #include <signal.h>
00004 #include <sys/types.h>
00005 #include <sys/stat.h>
00006 #include <fcntl.h>
00007 #include <unistd.h>
00008
00009 #include "libmyth/mythcontext.h"
00010 #include "libmyth/settings.h"
00011 #include "libmyth/langsettings.h"
00012 #include "libmyth/mythdbcon.h"
00013 #include "libmyth/exitcodes.h"
00014 #include "libmyth/compat.h"
00015
00016 #include "libmythtv/tv.h"
00017
00018 #include "welcomedialog.h"
00019 #include "welcomesettings.h"
00020
00021
00022 QString logfile = "";
00023
00024 static bool log_rotate(bool report_error);
00025 static void log_rotate_handler(int);
00026
00027
00028 void initKeys(void)
00029 {
00030 REG_KEY("Welcome", "STARTXTERM", "Open an Xterm window", "F12");
00031 REG_KEY("Welcome", "SHOWSETTINGS", "Show Mythshutdown settings", "F11");
00032 REG_KEY("Welcome", "STARTSETUP", "Start Mythtv-Setup", "");
00033 }
00034
00035 int main(int argc, char **argv)
00036 {
00037 bool bShowSettings = false;
00038
00039 QApplication a(argc, argv);
00040
00041 gContext = NULL;
00042 gContext = new MythContext(MYTH_BINARY_VERSION);
00043 if (!gContext->Init())
00044 {
00045 VERBOSE(VB_IMPORTANT, "mythwelcome: Could not initialize myth context. "
00046 "Exiting.");
00047 return FRONTEND_EXIT_NO_MYTHCONTEXT;
00048 }
00049
00050 if (!MSqlQuery::testDBConnection())
00051 {
00052 VERBOSE(VB_IMPORTANT, "mythwelcome: Could not open the database. "
00053 "Exiting.");
00054 return -1;
00055 }
00056
00057
00058 for (int argpos = 1; argpos < a.argc(); ++argpos)
00059 {
00060 if (!strcmp(a.argv()[argpos],"-v") ||
00061 !strcmp(a.argv()[argpos],"--verbose"))
00062 {
00063 if (a.argc()-1 > argpos)
00064 {
00065 if (parse_verbose_arg(a.argv()[argpos+1]) ==
00066 GENERIC_EXIT_INVALID_CMDLINE)
00067 return FRONTEND_EXIT_INVALID_CMDLINE;
00068
00069 ++argpos;
00070 }
00071 else
00072 {
00073 cerr << "Missing argument to -v/--verbose option\n";
00074 return FRONTEND_EXIT_INVALID_CMDLINE;
00075 }
00076 }
00077 else if (!strcmp(a.argv()[argpos],"-s") ||
00078 !strcmp(a.argv()[argpos],"--setup"))
00079 {
00080 bShowSettings = true;
00081 }
00082 else if (!strcmp(a.argv()[argpos], "-l") ||
00083 !strcmp(a.argv()[argpos], "--logfile"))
00084 {
00085 if (a.argc()-1 > argpos)
00086 {
00087 logfile = a.argv()[argpos+1];
00088 if (logfile.startsWith("-"))
00089 {
00090 cerr << "Invalid or missing argument to -l/--logfile option\n";
00091 return FRONTEND_EXIT_INVALID_CMDLINE;
00092 }
00093 else
00094 {
00095 ++argpos;
00096 }
00097 }
00098 else
00099 {
00100 cerr << "Missing argument to -l/--logfile option\n";
00101 return FRONTEND_EXIT_INVALID_CMDLINE;
00102 }
00103 }
00104 else
00105 {
00106 cerr << "Invalid argument: " << a.argv()[argpos] << endl <<
00107 "Valid options are: " << endl <<
00108 "-v or --verbose debug-level Use '-v help' for level info" << endl <<
00109 "-s or --setup Run setup for the mythshutdown program" << endl <<
00110 "-l or --logfile filename Writes STDERR and STDOUT messages to filename" << endl;
00111 return FRONTEND_EXIT_INVALID_CMDLINE;
00112 }
00113 }
00114
00115 if (logfile != "")
00116 {
00117 if (!log_rotate(true))
00118 cerr << "cannot open logfile; using stdout/stderr" << endl;
00119 else
00120 signal(SIGHUP, &log_rotate_handler);
00121 }
00122
00123 LanguageSettings::load("mythfrontend");
00124
00125 gContext->LoadQtConfig();
00126
00127 MythMainWindow *mainWindow = GetMythMainWindow();
00128 mainWindow->Init();
00129 gContext->SetMainWindow(mainWindow);
00130
00131 initKeys();
00132
00133 if (bShowSettings)
00134 {
00135 MythShutdownSettings settings;
00136 settings.exec();
00137 }
00138 else
00139 {
00140 WelcomeDialog *mythWelcome = new WelcomeDialog(mainWindow,
00141 "welcome_screen", "welcome-", "welcome_screen");
00142 mythWelcome->exec();
00143
00144 delete mythWelcome;
00145 }
00146
00147 delete gContext;
00148
00149 return 0;
00150 }
00151
00152
00153 static bool log_rotate(bool report_error)
00154 {
00155 int new_logfd = open(logfile, O_WRONLY|O_CREAT|O_APPEND, 0664);
00156
00157 if (new_logfd < 0)
00158 {
00159
00160 if (report_error)
00161 {
00162 cerr << "cannot open logfile " << logfile << endl;
00163 return false;
00164 }
00165
00166 new_logfd = open("/dev/null", O_WRONLY);
00167
00168 if (new_logfd < 0)
00169 {
00170
00171 return false;
00172 }
00173 }
00174
00175 while (dup2(new_logfd, 1) < 0 && errno == EINTR);
00176 while (dup2(new_logfd, 2) < 0 && errno == EINTR);
00177 while (close(new_logfd) < 0 && errno == EINTR);
00178
00179 return true;
00180 }
00181
00182
00183 static void log_rotate_handler(int)
00184 {
00185 log_rotate(false);
00186 }