| 1 |
/*************************************************************************** |
| 2 |
main.cpp |
| 3 |
------------------- |
| 4 |
A brewing recipe calculator |
| 5 |
------------------- |
| 6 |
Copyright 1999-2008 David Johnson <[email protected]> |
| 7 |
All rights reserved. |
| 8 |
|
| 9 |
Redistribution and use in source and binary forms, with or without |
| 10 |
modification, are permitted provided that the following conditions |
| 11 |
are met: |
| 12 |
|
| 13 |
1. Redistributions of source code must retain the above copyright |
| 14 |
notice, this list of conditions and the following disclaimer. |
| 15 |
|
| 16 |
2. Redistributions in binary form must reproduce the above copyright |
| 17 |
notice, this list of conditions and the following disclaimer in the |
| 18 |
documentation and/or other materials provided with the distribution. |
| 19 |
|
| 20 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
| 21 |
"AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
| 22 |
LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
| 23 |
A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
| 24 |
OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
| 25 |
SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
| 26 |
LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
| 27 |
DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
| 28 |
THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
| 29 |
(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
| 30 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
| 31 |
****************************************************************************/ |
| 32 |
|
| 33 |
#include <cstdlib> |
| 34 |
|
| 35 |
#include <QApplication> |
| 36 |
#include <QLibraryInfo> |
| 37 |
#include <QLocale> |
| 38 |
#include <QMainWindow> |
| 39 |
#include <QStringList> |
| 40 |
#include <QTextStream> |
| 41 |
#include <QTranslator> |
| 42 |
|
| 43 |
#if defined(Q_WS_MACX) |
| 44 |
extern void qt_mac_set_menubar_icons(bool enable); |
| 45 |
#endif |
| 46 |
|
| 47 |
#include "qbrew.h" |
| 48 |
#include "resource.h" |
| 49 |
|
| 50 |
using namespace std; |
| 51 |
using namespace Resource; |
| 52 |
|
| 53 |
////////////////////////////////////////////////////////////////////////////// |
| 54 |
// doversion() |
| 55 |
// ----------- |
| 56 |
// Print out copyright and version info to stdout |
| 57 |
|
| 58 |
void doversion(QTextStream &stream) |
| 59 |
{ |
| 60 |
stream << PACKAGE << ' ' << VERSION << '\n'; |
| 61 |
stream << QObject::tr(DESCRIPTION) << '\n'; |
| 62 |
stream << QObject::tr(COPYRIGHT); |
| 63 |
stream << ' ' << AUTHOR; |
| 64 |
stream << " <" << AUTHOR_EMAIL << ">"; |
| 65 |
stream << endl; |
| 66 |
} |
| 67 |
|
| 68 |
////////////////////////////////////////////////////////////////////////////// |
| 69 |
// dohelp() |
| 70 |
// -------- |
| 71 |
// Print out help info to stdout |
| 72 |
|
| 73 |
void dohelp(QTextStream &stream) |
| 74 |
{ |
| 75 |
stream << QObject::tr("Usage: %1 [options] [file]\n").arg(PACKAGE); |
| 76 |
stream << QObject::tr(DESCRIPTION) << "\n\n"; |
| 77 |
|
| 78 |
// arguments |
| 79 |
stream << QObject::tr("Arguments\n"); |
| 80 |
stream << QObject::tr(" file File to open") << "\n\n"; |
| 81 |
|
| 82 |
// general options |
| 83 |
stream << QObject::tr("Options\n"); |
| 84 |
stream << QObject::tr(" --help Print the command line options.\n"); |
| 85 |
stream << QObject::tr(" --version Print the application version.\n"); |
| 86 |
stream << endl; |
| 87 |
} |
| 88 |
|
| 89 |
////////////////////////////////////////////////////////////////////////////// |
| 90 |
// doargs() |
| 91 |
// -------- |
| 92 |
// Process the arguments that QApplication doesn't take care of |
| 93 |
|
| 94 |
QString doargs(QStringList arguments, QTextStream &stream) |
| 95 |
{ |
| 96 |
QString arg; |
| 97 |
for (int n=1; n<arguments.count(); ++n) { |
| 98 |
arg = arguments.at(n); |
| 99 |
if ((arg == "-h") || (arg == "-help") || |
| 100 |
(arg == "--help")) { |
| 101 |
dohelp(stream); |
| 102 |
exit(0); |
| 103 |
} else if ((arg == "-v") || (arg == "-version") || |
| 104 |
(arg == "--version")) { |
| 105 |
doversion(stream); |
| 106 |
exit(0); |
| 107 |
} else if (arg[0] == '-') { |
| 108 |
// no other valid options |
| 109 |
stream << QObject::tr("Invalid parameter \"%1\"\n") .arg(arg); |
| 110 |
dohelp(stream); |
| 111 |
exit(1); |
| 112 |
} else { |
| 113 |
// it must be a filename |
| 114 |
return arg; |
| 115 |
} |
| 116 |
} |
| 117 |
return (QObject::tr(DEFAULT_FILE)); |
| 118 |
} |
| 119 |
|
| 120 |
////////////////////////////////////////////////////////////////////////////// |
| 121 |
// main() |
| 122 |
// ------ |
| 123 |
// Entry point of application |
| 124 |
|
| 125 |
int main(int argc, char **argv) |
| 126 |
{ |
| 127 |
Q_INIT_RESOURCE(qbrew); |
| 128 |
|
| 129 |
QApplication app(argc, argv); |
| 130 |
app.setApplicationName(PACKAGE); |
| 131 |
app.setOrganizationName(TITLE); |
| 132 |
|
| 133 |
#if defined(Q_WS_MACX) |
| 134 |
// disable icons on Mac menus (as is customary) |
| 135 |
qt_mac_set_menubar_icons(false); |
| 136 |
#endif |
| 137 |
|
| 138 |
// load qbrew translations |
| 139 |
QString transdir = QBrew::instance()->dataBase() + "translations"; |
| 140 |
QTranslator translator; |
| 141 |
if (translator.load("qbrew_" + QLocale::system().name(), transdir)) { |
| 142 |
app.installTranslator(&translator); |
| 143 |
} |
| 144 |
// load qt translations |
| 145 |
QString qttransdir = QLibraryInfo::location(QLibraryInfo::TranslationsPath); |
| 146 |
QTranslator qttranslator; |
| 147 |
if (qttranslator.load("qt_" + QLocale::system().name(), qttransdir)) { |
| 148 |
app.installTranslator(&qttranslator); |
| 149 |
} |
| 150 |
|
| 151 |
// check for additional command line arguments |
| 152 |
QTextStream stream(stdout); |
| 153 |
QString filename = doargs(app.arguments(), stream); |
| 154 |
|
| 155 |
QBrew* mainwindow = QBrew::instance(); |
| 156 |
mainwindow->initialize(filename); |
| 157 |
mainwindow->show(); |
| 158 |
|
| 159 |
app.installEventFilter(mainwindow); // for handling file open events |
| 160 |
|
| 161 |
return app.exec(); |
| 162 |
} |