summaryrefslogtreecommitdiff
path: root/src/main.cpp
diff options
context:
space:
mode:
authorJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
committerJedidiah Barber <contact@jedbarber.id.au>2021-07-14 11:49:10 +1200
commitd24f813f3f2a05c112e803e4256b53535895fc98 (patch)
tree601e6ae9a1cd44bcfdcf91739a5ca36aedd827c9 /src/main.cpp
Initial mirror commitHEADmaster
Diffstat (limited to 'src/main.cpp')
-rw-r--r--src/main.cpp102
1 files changed, 102 insertions, 0 deletions
diff --git a/src/main.cpp b/src/main.cpp
new file mode 100644
index 0000000..569c31a
--- /dev/null
+++ b/src/main.cpp
@@ -0,0 +1,102 @@
+#include <stdlib.h>
+#include <iostream>
+
+#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<QString> 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;
+}