00001
00002
00003
00004
00005
00006
00007 #include "ColorFilterMode.h"
00008 #include <iostream>
00009 #include "Logger.h"
00010 #include "MainWindow.h"
00011 #include <QApplication>
00012 #include <QCoreApplication>
00013 #include <QDebug>
00014 #include <QDir>
00015 #include <QFileInfo>
00016 #include <QObject>
00017 #include <QProcessEnvironment>
00018 #include <QStyleFactory>
00019 #include "TranslatorContainer.h"
00020
00021 using namespace std;
00022
00023 const QString CMD_DEBUG ("debug");
00024 const QString CMD_ERROR_REPORT ("errorreport");
00025 const QString CMD_FILE_CMD_SCRIPT ("filecmdscript");
00026 const QString CMD_GNUPLOT ("gnuplot");
00027 const QString CMD_HELP ("help");
00028 const QString CMD_REGRESSION ("regression");
00029 const QString CMD_RESET ("reset");
00030 const QString CMD_STYLES ("styles");
00031 const QString DASH ("-");
00032 const QString DASH_DEBUG ("-" + CMD_DEBUG);
00033 const QString DASH_ERROR_REPORT ("-" + CMD_ERROR_REPORT);
00034 const QString DASH_FILE_CMD_SCRIPT ("-" + CMD_FILE_CMD_SCRIPT);
00035 const QString DASH_GNUPLOT ("-" + CMD_GNUPLOT);
00036 const QString DASH_HELP ("-" + CMD_HELP);
00037 const QString DASH_REGRESSION ("-" + CMD_REGRESSION);
00038 const QString DASH_RESET ("-" + CMD_RESET);
00039 const QString DASH_STYLES ("-" + CMD_STYLES);
00040 const QString ENGAUGE_LOG_FILE ("engauge.log");
00041
00042
00043 bool checkFileExists (const QString &file);
00044 QString engaugeLogFilename ();
00045 bool engaugeLogFilenameAttempt (const QString &path,
00046 QString &pathAndFile);
00047 void parseCmdLine (int argc,
00048 char **argv,
00049 bool &isDebug,
00050 bool &isReset,
00051 QString &errorReportFile,
00052 QString &fileCmdScriptFile,
00053 bool &isErrorReportRegressionTest,
00054 bool &isGnuplot,
00055 QStringList &loadStartupFiles);
00056 void showStylesAndExit ();
00057
00058
00059 bool checkFileExists (const QString &file)
00060 {
00061 QFileInfo check (file);
00062 return check.exists() && check.isFile();
00063 }
00064
00065 QString engaugeLogFilename()
00066 {
00067 QString pathAndFile;
00068
00069 #if !defined(OSX_RELEASE) && !defined(WIN_RELEASE) && !defined(APPIMAGE_RELEASE)
00070 QProcessEnvironment env;
00071
00072
00073 if (!engaugeLogFilenameAttempt (QCoreApplication::applicationDirPath(), pathAndFile)) {
00074 if (!engaugeLogFilenameAttempt (env.value ("HOME"), pathAndFile)) {
00075 if (!engaugeLogFilenameAttempt (env.value ("TEMP"), pathAndFile)) {
00076 pathAndFile = ENGAUGE_LOG_FILE;
00077 }
00078 }
00079 }
00080 #endif
00081
00082 return pathAndFile;
00083 }
00084
00085 bool engaugeLogFilenameAttempt (const QString &path,
00086 QString &pathAndFile)
00087 {
00088 bool success = false;
00089
00090
00091 pathAndFile = QString ("%1%2%3")
00092 .arg (path)
00093 .arg (QDir::separator())
00094 .arg (ENGAUGE_LOG_FILE);
00095 QFile file (pathAndFile);
00096 if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
00097
00098 file.close();
00099 success = true;
00100 }
00101
00102 return success;
00103 }
00104
00105 int main(int argc, char *argv[])
00106 {
00107 qRegisterMetaType<ColorFilterMode> ("ColorFilterMode");
00108
00109 QApplication app(argc, argv);
00110
00111
00112 TranslatorContainer translatorContainer (app);
00113
00114
00115 bool isDebug, isReset, isGnuplot, isErrorReportRegressionTest;
00116 QString errorReportFile, fileCmdScriptFile;
00117 QStringList loadStartupFiles;
00118 parseCmdLine (argc,
00119 argv,
00120 isDebug,
00121 isReset,
00122 errorReportFile,
00123 fileCmdScriptFile,
00124 isErrorReportRegressionTest,
00125 isGnuplot,
00126 loadStartupFiles);
00127
00128
00129 initializeLogging ("engauge",
00130 engaugeLogFilename(),
00131 isDebug);
00132 LOG4CPP_INFO_S ((*mainCat)) << "main args=" << QApplication::arguments().join (" ").toLatin1().data();
00133
00134
00135 MainWindow w (errorReportFile,
00136 fileCmdScriptFile,
00137 isErrorReportRegressionTest,
00138 isGnuplot,
00139 isReset,
00140 loadStartupFiles);
00141 w.show();
00142
00143
00144 return app.exec();
00145 }
00146
00147 void parseCmdLine (int argc,
00148 char **argv,
00149 bool &isDebug,
00150 bool &isReset,
00151 QString &errorReportFile,
00152 QString &fileCmdScriptFile,
00153 bool &isErrorReportRegressionTest,
00154 bool &isGnuplot,
00155 QStringList &loadStartupFiles)
00156 {
00157 const int COLUMN_WIDTH = 20;
00158 bool showUsage = false;
00159
00160
00161 bool nextIsErrorReportFile = false;
00162 bool nextIsFileCmdScript = false;
00163
00164
00165 isDebug = false;
00166 isReset = false;
00167 errorReportFile = "";
00168 fileCmdScriptFile = "";
00169 isErrorReportRegressionTest = false;
00170 isGnuplot = false;
00171
00172 for (int i = 1; i < argc; i++) {
00173
00174 if (nextIsErrorReportFile) {
00175 errorReportFile = argv [i];
00176 showUsage |= !checkFileExists (errorReportFile);
00177 nextIsErrorReportFile = false;
00178 } else if (nextIsFileCmdScript) {
00179 fileCmdScriptFile = argv [i];
00180 showUsage |= !checkFileExists (fileCmdScriptFile);
00181 nextIsFileCmdScript = false;
00182 } else if (strcmp (argv [i], DASH_DEBUG.toLatin1().data()) == 0) {
00183 isDebug = true;
00184 } else if (strcmp (argv [i], DASH_ERROR_REPORT.toLatin1().data()) == 0) {
00185 nextIsErrorReportFile = true;
00186 } else if (strcmp (argv [i], DASH_FILE_CMD_SCRIPT.toLatin1().data()) == 0) {
00187 nextIsFileCmdScript = true;
00188 } else if (strcmp (argv [i], DASH_GNUPLOT.toLatin1().data()) == 0) {
00189 isGnuplot = true;
00190 } else if (strcmp (argv [i], DASH_HELP.toLatin1().data()) == 0) {
00191 showUsage = true;
00192 } else if (strcmp (argv [i], DASH_REGRESSION.toLatin1().data()) == 0) {
00193 isErrorReportRegressionTest = true;
00194 } else if (strcmp (argv [i], DASH_RESET.toLatin1().data()) == 0) {
00195 isReset = true;
00196 } else if (strcmp (argv [i], DASH_STYLES.toLatin1().data()) == 0) {
00197 showStylesAndExit ();
00198 } else if (strncmp (argv [i], DASH.toLatin1().data(), 1) == 0) {
00199 showUsage = true;
00200 } else {
00201
00202
00203 QString fileName = argv [i];
00204 QFileInfo fInfo (fileName);
00205 if (fInfo.isRelative()) {
00206 fileName = fInfo.absoluteFilePath();
00207 }
00208 loadStartupFiles << fileName;
00209 }
00210 }
00211
00212 if (showUsage || nextIsErrorReportFile) {
00213
00214 cerr << "Usage: engauge "
00215 << "[" << DASH_DEBUG.toLatin1().data() << "] "
00216 << "[" << DASH_ERROR_REPORT.toLatin1().data() << " <file>] "
00217 << "[" << DASH_FILE_CMD_SCRIPT.toLatin1().data() << " <file> "
00218 << "[" << DASH_GNUPLOT.toLatin1().data() << "] "
00219 << "[" << DASH_HELP.toLatin1().data() << "] "
00220 << "[" << DASH_REGRESSION.toLatin1().data() << "] "
00221 << "[" << DASH_RESET.toLatin1().data () << "] "
00222 << "[" << DASH_STYLES.toLatin1().data () << "] "
00223 << "[<load_file1>] [<load_file2>] ..." << endl
00224 << " " << DASH_DEBUG.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00225 << QObject::tr ("Enables extra debug information. Used for debugging").toLatin1().data() << endl
00226 << " " << DASH_ERROR_REPORT.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00227 << QObject::tr ("Specifies an error report file as input. Used for debugging and testing").toLatin1().data() << endl
00228 << " " << DASH_FILE_CMD_SCRIPT.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00229 << QObject::tr ("Specifies a file command script file as input. Used for debugging and testing").toLatin1().data() << endl
00230 << " " << DASH_GNUPLOT.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00231 << QObject::tr ("Output diagnostic gnuplot input files. Used for debugging").toLatin1().data() << endl
00232 << " " << DASH_HELP.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00233 << QObject::tr ("Show this help information").toLatin1().data() << endl
00234 << " " << DASH_REGRESSION.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00235 << QObject::tr ("Executes the error report file or file command script. Used for regression testing").toLatin1().data() << endl
00236 << " " << DASH_RESET.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00237 << QObject::tr ("Removes all stored settings, including window positions. Used when windows start up offscreen").toLatin1().data() << endl
00238 << " " << DASH_STYLES.leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00239 << QObject::tr ("Show a list of available styles that can be used with the -style command").toLatin1().data() << endl
00240 << " " << QString ("<load file> ").leftJustified(COLUMN_WIDTH, ' ').toLatin1().data()
00241 << QObject::tr ("File(s) to be imported or opened at startup").toLatin1().data() << endl;
00242
00243 exit (0);
00244 }
00245 }
00246
00247 void showStylesAndExit ()
00248 {
00249 cout << "Available styles: " << QStyleFactory::keys ().join (", ").toLatin1().data() << endl;
00250 exit (0);
00251 }