application.cpp 3.8 KB

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