• Skip to content
  • Skip to link menu
KDE API Documentation - main.cpp Source File (GCompris-qt)
  • KDE Home
  • Contact Us
 

GCompris-qt

  • src
  • core
main.cpp
1 /* GCompris - main.cpp
2  *
3  * Copyright (C) 2014 Bruno Coudoin <bruno.coudoin@gcompris.net>
4  *
5  * Authors:
6  * Bruno Coudoin <bruno.coudoin@gcompris.net>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 3 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, see <http://www.gnu.org/licenses/>.
20  */
21 #include <QtDebug>
22 #include <QtGui/QGuiApplication>
23 #include <QtQuick/QQuickWindow>
24 #include <QQmlApplicationEngine>
25 #include <QStandardPaths>
26 #include <QObject>
27 #include <QTranslator>
28 #include <QCommandLineParser>
29 #include <QCursor>
30 #include <QPixmap>
31 #include <QSettings>
32 
33 #include "ApplicationInfo.h"
34 #include "ActivityInfoTree.h"
35 #include "File.h"
36 #include "DownloadManager.h"
37 
38 bool loadAndroidTranslation(QTranslator &translator, const QString &locale)
39 {
40  QFile file("assets:/gcompris_" + locale + ".qm");
41 
42  file.open(QIODevice::ReadOnly);
43  QDataStream in(&file);
44  uchar *data = (uchar*)malloc(file.size());
45 
46  if(!file.exists())
47  qDebug() << "file assets:/" << locale << ".qm exists";
48 
49  in.readRawData((char*)data, file.size());
50 
51  if(!translator.load(data, file.size())) {
52  qDebug() << "Unable to load translation for locale " <<
53  locale << ", use en_US by default";
54  free(data);
55  return false;
56  }
57  // Do not free data, it is still needed by translator
58  return true;
59 }
60 
61 // Return the locale
62 QString loadTranslation(QSettings &config, QTranslator &translator)
63 {
64  QString locale;
65  // Get locale
66  if(config.contains("General/locale")) {
67  locale = config.value("General/locale").toString();
68  } else {
69  locale = GC_DEFAULT_LOCALE;
70  }
71  if(locale == GC_DEFAULT_LOCALE)
72  locale = QString(QLocale::system().name() + ".UTF-8");
73 
74  if(locale == "C.UTF-8")
75  locale = "en_US.UTF-8";
76 
77  // Load translation
78  // Remove .UTF8
79  locale.remove(".UTF-8");
80 
81 #if defined(Q_OS_ANDROID)
82  if(!loadAndroidTranslation(translator, locale))
83  loadAndroidTranslation(translator, ApplicationInfo::localeShort(locale));
84 #else
85 
86  if(!translator.load("gcompris_" + locale, QString("%1/%2/translations").arg(QCoreApplication::applicationDirPath(), GCOMPRIS_DATA_FOLDER))) {
87  qDebug() << "Unable to load translation for locale " <<
88  locale << ", use en_US by default";
89  }
90 #endif
91  return locale;
92 }
93 
94 int main(int argc, char *argv[])
95 {
96  QGuiApplication app(argc, argv);
97  app.setOrganizationName("KDE");
98  app.setApplicationName(GCOMPRIS_APPLICATION_NAME);
99  app.setOrganizationDomain("kde.org");
100  app.setApplicationVersion(ApplicationInfo::GCVersion());
101 
102  // Local scope for config
103  QSettings config(QStandardPaths::writableLocation(QStandardPaths::GenericConfigLocation) +
104  "/gcompris/" + GCOMPRIS_APPLICATION_NAME + ".conf",
105  QSettings::IniFormat);
106 
107  // Load translations
108  QTranslator translator;
109  QString locale = loadTranslation(config, translator);
110  // Apply translation
111  app.installTranslator(&translator);
112 
113  QCommandLineParser parser;
114  parser.setApplicationDescription("GCompris is an educational software for children 2 to 10");
115  parser.addHelpOption();
116  parser.addVersionOption();
117  QCommandLineOption exportActivitiesAsSQL("export-activities-as-sql", "Export activities as SQL");
118  parser.addOption(exportActivitiesAsSQL);
119  QCommandLineOption clDefaultCursor(QStringList() << "c" << "cursor",
120  QObject::tr("Run GCompris with the default system cursor."));
121  parser.addOption(clDefaultCursor);
122  QCommandLineOption clNoCursor(QStringList() << "C" << "nocursor",
123  QObject::tr("Run GCompris without cursor (touch screen mode)."));
124  parser.addOption(clNoCursor);
125  QCommandLineOption clFullscreen(QStringList() << "f" << "fullscreen",
126  QObject::tr("Run GCompris in fullscreen mode."));
127  parser.addOption(clFullscreen);
128  QCommandLineOption clWindow(QStringList() << "w" << "window",
129  QObject::tr("Run GCompris in window mode."));
130  parser.addOption(clWindow);
131  QCommandLineOption clSound(QStringList() << "s" << "sound",
132  QObject::tr("Run GCompris with sound enabled."));
133  parser.addOption(clSound);
134  QCommandLineOption clMute(QStringList() << "m" << "mute",
135  QObject::tr("Run GCompris without sound."));
136  parser.addOption(clMute);
137  QCommandLineOption clWithoutConfig(QStringList() << "disable-config",
138  QObject::tr("Disable the configuration button."));
139  parser.addOption(clWithoutConfig);
140  QCommandLineOption clWithConfig(QStringList() << "enable-config",
141  QObject::tr("Enable the configuration button (default)."));
142  parser.addOption(clWithConfig);
143  parser.process(app);
144 
145 
146  ApplicationInfo::init();
147  ActivityInfoTree::init();
148  ApplicationSettings::init();
149  File::init();
150  DownloadManager::init();
151 
152  // Tell media players to stop playing, it's GCompris time
153  ApplicationInfo::getInstance()->requestAudioFocus();
154 
155  // Must be done after ApplicationSettings is constructed because we get an
156  // async callback from the payment system
157  ApplicationSettings::getInstance()->checkPayment();
158 
159  // Getting fullscreen mode from config if exist, else true is default value
160  bool isFullscreen = true;
161  {
162 
163  if(config.contains("General/fullscreen")) {
164  isFullscreen = config.value("General/fullscreen").toBool();
165  }
166 
167  // Set the cursor image
168  bool defaultCursor = false;
169  if(config.contains("General/defaultCursor")) {
170  defaultCursor = config.value("General/defaultCursor").toBool();
171  }
172  if(!defaultCursor && !parser.isSet(clDefaultCursor))
173  QGuiApplication::setOverrideCursor(
174  QCursor(QPixmap(":/gcompris/src/core/resource/cursor.svg"),
175  0, 0));
176 
177  // Hide the cursor
178  bool noCursor = false;
179  if(config.contains("General/noCursor")) {
180  noCursor = config.value("General/noCursor").toBool();
181  }
182  if(noCursor || parser.isSet(clNoCursor))
183  QGuiApplication::setOverrideCursor(QCursor(Qt::BlankCursor));
184  }
185 
186  // Update execution counter
187  ApplicationSettings::getInstance()->setExeCount(ApplicationSettings::getInstance()->exeCount() + 1);
188 
189  // Register voices-resources for current locale, updates/downloads only if
190  // not prohibited by the settings
191  if(!DownloadManager::getInstance()->areVoicesRegistered())
192  DownloadManager::getInstance()->updateResource(DownloadManager::getInstance()
193  ->getVoicesResourceForLocale(locale));
194 
195  if(parser.isSet(clFullscreen)) {
196  isFullscreen = true;
197  }
198  if(parser.isSet(clWindow)) {
199  isFullscreen = false;
200  }
201  if(parser.isSet(clMute)) {
202  ApplicationSettings::getInstance()->setIsAudioEffectsEnabled(false);
203  ApplicationSettings::getInstance()->setIsAudioVoicesEnabled(false);
204  }
205  if(parser.isSet(clSound)) {
206  ApplicationSettings::getInstance()->setIsAudioEffectsEnabled(true);
207  ApplicationSettings::getInstance()->setIsAudioVoicesEnabled(true);
208  }
209  if(parser.isSet(clWithConfig)) {
210  ApplicationSettings::getInstance()->setKioskMode(false);
211  }
212  if(parser.isSet(clWithoutConfig)) {
213  ApplicationSettings::getInstance()->setKioskMode(true);
214  }
215 
216  QQmlApplicationEngine engine(QUrl("qrc:/gcompris/src/core/main.qml"));
217  QObject::connect(&engine, SIGNAL(quit()), DownloadManager::getInstance(),
218  SLOT(shutdown()));
219 
220  if(parser.isSet(exportActivitiesAsSQL)) {
221  ActivityInfoTree *menuTree(qobject_cast<ActivityInfoTree*>(ActivityInfoTree::menuTreeProvider(&engine, NULL)));
222  menuTree->exportAsSQL();
223  exit(0);
224  }
225 
226  QObject *topLevel = engine.rootObjects().value(0);
227 
228  QQuickWindow *window = qobject_cast<QQuickWindow *>(topLevel);
229  if (!window) {
230  qWarning("Error: Your root item has to be a Window.");
231  return -1;
232  }
233 
234  ApplicationInfo::setWindow(window);
235 
236  window->setIcon(QIcon(QPixmap(QString::fromUtf8(":/gcompris/src/core/resource/gcompris-icon.png"))));
237 
238  if(isFullscreen) {
239  window->showFullScreen();
240  }
241  else {
242  window->show();
243  }
244 
245  return app.exec();
246 
247 }
ApplicationInfo::GCVersion
QString GCVersion
GCompris version string (compile time).
Definition: ApplicationInfo.h:116
DownloadManager::updateResource
Q_INVOKABLE bool updateResource(const QString &path)
Updates a resource path from the upstream server unless prohibited by the settings and registers it i...
Definition: DownloadManager.cpp:150
ApplicationInfo::localeShort
QString localeShort
Short (2-letter) locale string of the currently active language.
Definition: ApplicationInfo.h:111
DownloadManager::init
static void init()
Registers DownloadManager singleton in the QML engine.
Definition: DownloadManager.cpp:82
main
GCompris' main QML file defining the top level window.
Definition: main.qml:41
ApplicationInfo::init
static void init()
Registers singleton in the QML engine.
Definition: ApplicationInfo.cpp:224
This file is part of the KDE documentation.
Documentation copyright © 1996-2015 The KDE developers.
Generated on Tue Jun 2 2015 21:47:47 by doxygen 1.8.9.1 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

GCompris-qt

Skip menu "GCompris-qt"
  • Main Page
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • File List
  • Modules

Class Picker

Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal