init
[qstardict] / qstardict / application.cpp
1 /*****************************************************************************
2  * application.cpp - QStarDict, a StarDict clone written using Qt            *
3  * Copyright (C) 2008 Alexander Rodin                                        *
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 "application.h"
21
22 #ifdef QSTARDICT_WITH_TRANSLATIONS
23 #include <QLibraryInfo>
24 #include <QLocale>
25 #include <QTranslator>
26 #include <QStringList>
27 #endif // QSTARDICT_WITH_TRANSLATIONS
28 #include "dictcore.h"
29 #include "mainwindow.h"
30 #include "popupwindow.h"
31 #include "speaker.h"
32 #include "trayicon.h"
33 #ifdef QSTARDICT_WITH_DBUS
34 #include "dbusadaptor.h"
35 #endif // QSTARDICT_WITH_DBUS
36
37 namespace QStarDict
38 {
39
40 Application::Application(int &argc, char **argv)
41     : QApplication(argc, argv)
42 {
43     setOrganizationName("qstardict");
44     setApplicationName("qstardict");
45
46     #ifndef MAEMO
47     setQuitOnLastWindowClosed(false);
48     #endif // MAEMO
49
50     #ifndef MAEMO
51     setQuitOnLastWindowClosed(true);
52     #endif // MAEMO
53
54 #ifdef QSTARDICT_WITH_TRANSLATIONS
55     m_translator = new QTranslator;
56 #ifdef Q_WS_MAC
57     QString binPath = QCoreApplication::applicationDirPath();
58     // navigate through mac's bundle tree structore
59     m_translator->load("qstardict-" + QLocale::system().name(), binPath + "/../i18n/");
60 #else
61     m_translator->load("qstardict-" + QLocale::system().name(), QSTARDICT_TRANSLATIONS_DIR);
62 #endif
63     installTranslator(m_translator);
64     m_qtTranslator = new QTranslator;
65     m_qtTranslator->load("qt_" + QLocale::system().name(), QLibraryInfo::location(QLibraryInfo::TranslationsPath));
66     installTranslator(m_qtTranslator);
67 #endif // QSTARDICT_WITH_TRANSLATIONS
68
69     m_dictCore = new DictCore;
70     m_popupWindow = new PopupWindow;
71     m_popupWindow->setDict(m_dictCore);
72     m_speaker = new Speaker;
73     #ifndef MAEMO
74     m_trayIcon = new TrayIcon;
75     #endif // MAEMO
76     m_mainWindow = new MainWindow;
77     m_mainWindow->setDict(m_dictCore);
78     #ifdef MAEMO
79     m_mainWindow->setVisible(true);
80     #endif // MAEMO
81 #ifdef QSTARDICT_WITH_DBUS
82     m_dbusAdaptor = new DBusAdaptor(m_mainWindow);
83 #endif // QSTARDICT_WITH_DBUS
84 }
85
86 Application::~Application()
87 {
88     #ifndef MAEMO
89     delete m_trayIcon;
90     #endif // MAEMO
91     delete m_mainWindow;
92     delete m_popupWindow;
93     delete m_speaker;
94     delete m_dictCore;
95 #ifdef QSTARDICT_WITH_TRANSLATIONS
96     removeTranslator(m_translator);
97     delete m_translator;
98     removeTranslator(m_qtTranslator);
99     delete m_qtTranslator;
100 #endif // QSTARDICT_WITH_TRANSLATIONS
101 }
102
103 int Application::exec()
104 {
105     QString text = commandLineText();
106     if (text != QString::null)
107         m_mainWindow->showTranslation(text);
108     return QApplication::exec();
109 }
110
111 QString Application::commandLineText()
112 {
113     QStringList args(arguments());
114     for(int i = 1; i < args.count(); ++i)
115     {
116         if(! args.at(i).startsWith('-'))
117             return args.at(i);
118     }
119     return QString::null;
120 }
121
122 }
123
124 // vim: tabstop=4 softtabstop=4 shiftwidth=4 expandtab cindent textwidth=120 formatoptions=tc
125