#include #include #include "main.h" #include "version.h" #include "strings.h" #include "main-view/AppModel.h" #include "main-view/MainWindow.h" int main(int argc, char *argv[]) { setlocale(LC_ALL, "en_US.UTF-8"); QApplication app(argc, argv); initSettings(); setTranslations(); QString dictFilePath = ""; BuildStr = Strings::tr(Strings::s_build) + ": " + BUILD_REVISION; if(processParams(dictFilePath)) return 0; Q_INIT_RESOURCE(application); AppModel model; MainWindow mainWin(&model); mainWin.show(); if(!dictFilePath.isEmpty()) mainWin.openFile(dictFilePath); return app.exec(); } void initSettings() { QSettings::setDefaultFormat(QSettings::IniFormat); QCoreApplication::setOrganizationName("freshmemory"); QCoreApplication::setApplicationName("freshmemory"); } void setTranslations() { QString locale = QSettings().value("lang").toString(); if(locale.isEmpty()) locale = QLocale::system().name(); installTranslator("qt_" + locale, QLibraryInfo::location(QLibraryInfo::TranslationsPath)); installTranslator("freshmemory_" + locale, getResourcePath() + "/tr"); } void installTranslator(const QString& fileName, const QString& path) { QTranslator* translator = new QTranslator; translator->load(fileName, path); qApp->installTranslator(translator); } QString getResourcePath() { #if defined(Q_OS_LINUX) || defined(Q_OS_MAC) return "/usr/share/freshmemory"; #else return QCoreApplication::applicationDirPath(); #endif } bool processParams(QString& dictFilePath) { QStringList argList = QCoreApplication::arguments(); QListIterator argIter = argList; argIter.next(); // The first argument is the program calling full name while( argIter.hasNext() ) { QString argStr = argIter.next(); if( argStr == "--version" || argStr == "-v" ) { QString formattedBuildStr; if( !BuildStr.isEmpty() ) formattedBuildStr = BuildStr + "\n"; std::cout << (Strings::tr(Strings::s_appTitle) + " " + FM_VERSION + "\n" + formattedBuildStr + Strings::tr(Strings::s_author) + "\n" + MainWindow::tr("Website:") + " http://fresh-memory.com\n").toStdString(); return true; } else if( argStr == "--help" || argStr == "-h" ) { std::cout << (MainWindow::tr("Usage:") + " freshmemory [OPTIONS] [FILE]\n" + MainWindow::tr("FILE is a dictionary filename to load.") +"\n\n" + MainWindow::tr("Options:") + "\n" + "\t-h, --help\t" + MainWindow::tr("Display this help and exit") + "\n" + "\t-v, --version\t" + MainWindow::tr("Output version information and exit") + "\n") .toStdString(); return true; } else if( !argIter.hasNext() ) // the last argument { dictFilePath = argStr; return false; } } return false; }