ab77e423c2924aea4ea5072683a6a20848eb0564
[mdictionary] / src / mdictionary / gui / main.cpp
1 /*******************************************************************************
2
3     This file is part of mDictionary.
4
5     mDictionary 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 3 of the License, or
8     (at your option) any later version.
9
10     mDictionary 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
16     along with mDictionary.  If not, see <http://www.gnu.org/licenses/>.
17
18     Copyright 2010 Comarch S.A.
19
20 *******************************************************************************/
21
22 //! \file main.cpp
23 //! \author Mateusz Półrola <mateusz.polrola@comarch.pl>
24
25 #include <QtGui/QApplication>
26 #include "MainWindow.h"
27 #include "../backbone/backbone.h"
28 #include "../../include/translation.h"
29 #include "../../include/Notify.h"
30 #include "DBusAdapter.h"
31 #include <QDebug>
32 #include <QDBusConnection>
33
34
35
36 QStringList parseParameters(int argc, char *argv[]) {
37     bool wordListStarted = false;
38     QStringList result;
39     for(int i=1; i<argc; i++) {
40         QString argument(argv[i]);
41         if(!wordListStarted) {
42             if(argument == "-s" || argument == "--search") {
43                 wordListStarted = true;
44             }
45         }
46         else {
47             if(!argument.startsWith("-") &&
48                !argument.startsWith("--")) {
49                 result << argument;
50             }
51         }
52     }
53
54     return result;
55 }
56
57 void retranslate() {
58     QString locale = QLocale::system().name();
59
60     QTranslator *translator = new QTranslator;
61     QTranslator *qtTranslator = new QTranslator;
62
63     qtTranslator->load("qt_" + locale,
64     QLibraryInfo::location(QLibraryInfo::TranslationsPath));
65     QCoreApplication::installTranslator(qtTranslator);
66
67     if(!translator->load(":/translations/" + locale)) {
68         translator->load(":/translations/en_US");
69
70     }
71
72     QCoreApplication::installTranslator(translator);
73 }
74
75 int main(int argc, char *argv[]) {
76     QApplication a(argc, argv);
77     QApplication::setOrganizationName("Comarch S.A.");
78     QApplication::setOrganizationDomain("comarch.com");
79     QApplication::setApplicationName("mDictionary");
80
81     QStringList search = parseParameters(argc, argv);
82
83     qRegisterMetaType<Translation*>("Translation*");
84     qRegisterMetaType<QList<Translation*> >("QList<Translation*>");
85     qRegisterMetaType<Notify>("Notify");
86     qRegisterMetaType<Notify::NotifyType>("Notify::NotifyType");
87
88     retranslate();
89
90     Backbone* backbone = new Backbone();
91     MainWindow* mainWindow = new MainWindow(backbone);
92
93     DBusAdapter* adapter = new DBusAdapter(mainWindow);
94
95     QDBusConnection::sessionBus().registerObject("/mainWindow",
96                                                  mainWindow);
97     QDBusConnection::sessionBus().registerService("com.comarch.mdictionary");
98
99     mainWindow->show();
100
101     if(search.count() > 0) {
102         mainWindow->setExactSearch(true);
103         mainWindow->searchExact(search[0]);
104     }
105
106     int res = a.exec();
107
108     QDBusConnection::sessionBus().unregisterObject("/mainWindow");
109     QDBusConnection::sessionBus().unregisterService("com.comarch.mdictionary");
110
111     delete adapter;
112     delete mainWindow;
113     delete backbone;
114
115     return res;
116 }