summaryrefslogtreecommitdiff
path: root/src/main.cpp
blob: 569c31a3201500b0844d6619966bcaf479893ef0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
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;
}