| 1 | /* |
| 2 | * chewing-editor: Chewing userphrase editor |
| 3 | * Copyright (C) 2014 Chewing Development Team |
| 4 | |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 2 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | |
| 15 | * You should have received a copy of the GNU General Public License along |
| 16 | * with this program; if not, write to the Free Software Foundation, Inc., |
| 17 | * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #include "config.h" |
| 21 | |
| 22 | #include <QApplication> |
| 23 | #include <QDebug> |
| 24 | #include <QLibraryInfo> |
| 25 | #include <QTranslator> |
| 26 | |
| 27 | #ifdef _MSC_VER |
| 28 | #include <QtPlugin> |
| 29 | Q_IMPORT_PLUGIN(QWindowsIntegrationPlugin) |
| 30 | #endif |
| 31 | |
| 32 | #include "ChewingEditor.h" |
| 33 | |
| 34 | void messageHandlerHelper(QtMsgType type, const QMessageLogContext& context, const QString& message, QtMsgType level) |
| 35 | { |
| 36 | if (type < level) { |
| 37 | return; |
| 38 | } |
| 39 | |
| 40 | auto msg = message.toUtf8(); |
| 41 | auto file = QFileInfo{context.file}.fileName().toUtf8(); |
| 42 | |
| 43 | switch(type) { |
| 44 | case QtDebugMsg: |
| 45 | fprintf(stdout, "Debug: %s (%s %s:%d)\n", msg.constData(), context.function, file.constData(), context.line); |
| 46 | break; |
| 47 | case QtWarningMsg: |
| 48 | fprintf(stderr, "Warning: %s (%s %s:%d)\n", msg.constData(), context.function, file.constData(), context.line); |
| 49 | break; |
| 50 | case QtCriticalMsg: |
| 51 | fprintf(stderr, "Critical: %s (%s %s:%d)\n", msg.constData(), context.function, file.constData(), context.line); |
| 52 | break; |
| 53 | case QtFatalMsg: |
| 54 | fprintf(stderr, "Fatal: %s (%s %s:%d)\n", msg.constData(), context.function, file.constData(), context.line); |
| 55 | abort(); |
| 56 | break; |
| 57 | default: |
| 58 | break; |
| 59 | } |
| 60 | } |
| 61 | |
| 62 | void emptyMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) |
| 63 | { |
| 64 | messageHandlerHelper(type, context, message, QtWarningMsg); |
| 65 | } |
| 66 | |
| 67 | void debugMessageHandler(QtMsgType type, const QMessageLogContext& context, const QString& message) |
| 68 | { |
| 69 | messageHandlerHelper(type, context, message, QtDebugMsg); |
| 70 | } |
| 71 | |
| 72 | void loadTranslation(QApplication &app, QTranslator &qtTranslator, QTranslator &chewingTranslator) |
| 73 | { |
| 74 | QString qtFileName{"qt_" + QLocale::system().name()}; |
| 75 | QString qtDirectory{QLibraryInfo::location(QLibraryInfo::TranslationsPath)}; |
| 76 | |
| 77 | bool qtLoaded = qtTranslator.load(qtFileName, qtDirectory); |
| 78 | qDebug() << "Load" << qtFileName << qtDirectory << qtLoaded; |
| 79 | |
| 80 | app.installTranslator(&qtTranslator); |
| 81 | |
| 82 | QString chewingFileName{":/" PROJECT_NAME "_" + QLocale::system().name()}; |
| 83 | |
| 84 | bool chewingLoaded = chewingTranslator.load(chewingFileName); |
| 85 | qDebug() << "Load" << chewingFileName << chewingLoaded; |
| 86 | |
| 87 | app.installTranslator(&chewingTranslator); |
| 88 | } |
| 89 | |
| 90 | void printVersion() |
| 91 | { |
| 92 | QString msg{QCoreApplication::translate("main", "%1\n") |
| 93 | .arg(PROJECT_VERSION)}; |
| 94 | |
| 95 | printf("%s", msg.toUtf8().constData()); |
| 96 | } |
| 97 | |
| 98 | void printArg(const QString &arg, const QString &description) |
| 99 | { |
| 100 | printf("%s\n\t%s\n\n", arg.toUtf8().constData(), description.toUtf8().constData()); |
| 101 | } |
| 102 | |
| 103 | void printHelp(const QString &name) |
| 104 | { |
| 105 | QString description{QCoreApplication::translate("main", |
| 106 | "chewing-editor is a cross platform chewing user phrase editor written " |
| 107 | "in Qt5. It provides a user friendly GUI to manage user phrase. With it," |
| 108 | " user can customize user phrase to enhance chewing input performance.")}; |
| 109 | |
| 110 | printf("%s\n\n", description.toUtf8().constData()); |
| 111 | |
| 112 | printArg( |
| 113 | QCoreApplication::translate("main", "-d, --debug"), |
| 114 | QCoreApplication::translate("main", "Enable debug message.")); |
| 115 | |
| 116 | printArg( |
| 117 | QCoreApplication::translate("main", "-v, --version"), |
| 118 | QCoreApplication::translate("main", "Print program version.")); |
| 119 | |
| 120 | printArg( |
| 121 | QCoreApplication::translate("main", "-h, --help"), |
| 122 | QCoreApplication::translate("main", "Print help message.")); |
| 123 | } |
| 124 | |
| 125 | void printUnknownArgs(const QString &unknown) |
| 126 | { |
| 127 | printf("Unknown options: %s\n", unknown.toUtf8().constData()); |
| 128 | } |
| 129 | |
| 130 | void readArgument(QApplication &app) |
| 131 | { |
| 132 | auto args = QCoreApplication::arguments(); |
| 133 | auto name = QFileInfo{args.at(0)}.fileName(); |
| 134 | |
| 135 | for (int i = 1; i < args.size(); ++i) { |
| 136 | auto arg = args.at(i); |
| 137 | |
| 138 | if (arg.compare("-d") == 0 || arg.compare("--debug") == 0) { |
| 139 | qInstallMessageHandler(debugMessageHandler); |
| 140 | |
| 141 | } else if (arg.compare("-v") == 0 || arg.compare("--version") == 0) { |
| 142 | printVersion(); |
| 143 | exit(0); |
| 144 | |
| 145 | } else if (arg.compare("-h") == 0 || arg.compare("--help") == 0) { |
| 146 | printHelp(name); |
| 147 | exit(0); |
| 148 | |
| 149 | } else { |
| 150 | printUnknownArgs(arg); |
| 151 | printHelp(name); |
| 152 | exit(1); |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | int main(int argc, char *argv[]) |
| 158 | { |
| 159 | qInstallMessageHandler(emptyMessageHandler); |
| 160 | |
| 161 | QApplication app{argc, argv}; |
| 162 | |
| 163 | QTranslator qtTranslator; |
| 164 | QTranslator chewingTranslator; |
| 165 | loadTranslation(app, qtTranslator, chewingTranslator); |
| 166 | |
| 167 | readArgument(app); |
| 168 | |
| 169 | ChewingEditor w; |
| 170 | w.show(); |
| 171 | |
| 172 | return app.exec(); |
| 173 | } |