BibleTime
main.cpp
Go to the documentation of this file.
1/*********
2*
3* In the name of the Father, and of the Son, and of the Holy Spirit.
4*
5* This file is part of BibleTime's source code, https://bibletime.info/
6*
7* Copyright 1999-2025 by the BibleTime developers.
8* The BibleTime source code is licensed under the GNU General Public License
9* version 2.0.
10*
11**********/
12
13#include <iostream>
14#include <QDateTime>
15#include <QLibraryInfo>
16#include <QLocale>
17#include <QQmlEngine>
18#include <QTranslator>
19#include "../backend/bookshelfmodel/btbookshelftreemodel.h"
20#include "../backend/config/btconfig.h"
21#include "../backend/managers/cswordbackend.h"
22#include "../util/directory.h"
23#include "bibletime.h"
24#include "bibletimeapp.h"
27
28// Sword includes:
29#include <swmgr.h>
30
31/// \todo Reimplement signal handler which handles consecutive crashes.
32
33namespace {
34
35/*******************************************************************************
36 Printing command-line help.
37*******************************************************************************/
38
39/**
40 Prints command-line help text for BibleTime when --help is used.
41 \param[in] executable The executed file name (argv[0]).
42*/
43void printHelp(const QString &executable) {
44 std::cout << qPrintable(executable) << std::endl << std::endl
45 << " --help, -h" << std::endl << " "
46 << qPrintable(QObject::tr("Show this help message and exit"))
47 << std::endl << std::endl
48 << " --version, -V" << std::endl << " "
49 << qPrintable(QObject::tr("Output BibleTime version and exit"))
50 << std::endl << std::endl
51 << " --ignore-session" << std::endl << " "
52 << qPrintable(QObject::tr("Open a clean session"))
53 << std::endl << std::endl
54 << " --open-default-bible <ref>" << std::endl << " "
55 << qPrintable(QObject::tr("Open the default Bible with the "
56 "reference <ref>"))
57 << std::endl << std::endl
58 << qPrintable(QObject::tr("For command-line arguments parsed by the"
59 " Qt toolkit, see %1.")
60 .arg("http://doc.qt.nokia.com/latest/qapplication.html"))
61 << std::endl;
62}
63
64/*******************************************************************************
65 Parsing command-line arguments
66*******************************************************************************/
67
68/**
69 Parses all command-line arguments.
70 \param[out] ignoreSession Whether --ignore-session was specified.
71 \param[out] openBibleKey Will be set to --open-default-bible if specified.
72 \retval -1 Parsing was successful, the application should exit with
73 EXIT_SUCCESS.
74 \retval 0 Parsing was successful.
75 \retval 1 Parsing failed, the application should exit with EXIT_FAILURE.
76*/
77int parseCommandLine(bool & showDebugMessages,
78 bool & ignoreSession,
79 QString & openBibleKey)
80{
81 QStringList args = BibleTimeApp::arguments();
82 for (int i = 1; i < args.size(); i++) {
83 const QString &arg = args.at(i);
84 if (arg == QStringLiteral("--help")
85 || arg == QStringLiteral("-h")
86 || arg == QStringLiteral("/?")
87 || arg == QStringLiteral("/h"))
88 {
89 printHelp(args.at(0));
90 return -1;
91 } else if (arg == QStringLiteral("--version")
92 || arg == QStringLiteral("-V")
93 || arg == QStringLiteral("/V"))
94 {
95 std::cout << "BibleTime " BT_VERSION << std::endl;
96 return -1;
97 } else if (arg == QStringLiteral("--debug")) {
98 showDebugMessages = true;
99 } else if (arg == QStringLiteral("--ignore-session")) {
100 ignoreSession = true;
101 } else if (arg == QStringLiteral("--open-default-bible")) {
102 i++;
103 if (i < args.size()) {
104 openBibleKey = args.at(i);
105 } else {
106 std::cerr
107 << qPrintable(
108 QObject::tr("Error: %1 expects an argument. See "
109 "--help for details.")
110 .arg(QStringLiteral("--open-default-bible")))
111 << std::endl;
112 return 1;
113 }
114 } else {
115 std::cerr << qPrintable(QObject::tr(
116 "Error: Invalid command-line argument: %1")
117 .arg(arg)) << std::endl;
118 return 1;
119 }
120 }
121 return 0;
122}
123
124/*******************************************************************************
125 Handle Qt's meta type system.
126*******************************************************************************/
127
129 qRegisterMetaType<FilterOptions>("FilterOptions");
130 qRegisterMetaType<DisplayOptions>("DisplayOptions");
131 qRegisterMetaType<alignmentMode>("alignmentMode");
132 qRegisterMetaType<CSwordModuleSearch::SearchType>("SearchType");
133 qRegisterMetaType<BtConfig::StringMap>("StringMap");
134 qRegisterMetaType<QList<int> >("QList<int>");
135 qRegisterMetaType<BtBookshelfTreeModel::Grouping>(
136 "BtBookshelfTreeModel::Grouping");
137
139 qmlRegisterSingletonType<BtQmlInterface>(
140 "BibleTime",
141 1,
142 0,
143 "BtQmlInterface",
144 [](QQmlEngine *, QJSEngine *) { return new BtQmlInterface(); });
145}
146
147} // anonymous namespace
148
149
150/*******************************************************************************
151 Program main entry point.
152*******************************************************************************/
153
154int main(int argc, char* argv[]) {
155 namespace DU = util::directory;
156
157 if (!sword::SWMgr::isICU) {
158 qFatal("SWORD library is required to be built against ICU!");
159 return EXIT_FAILURE;
160 }
161
163
164 BibleTimeApp app(argc, argv); //for QApplication
165
166 // Parse command line arguments:
167 bool ignoreSession = false;
168 QString openBibleKey;
169 {
170 bool showDebugMessages = false;
171 if (int const r = parseCommandLine(showDebugMessages,
172 ignoreSession,
173 openBibleKey))
174 return r < 0 ? EXIT_SUCCESS : EXIT_FAILURE;
175 app.setDebugMode(showDebugMessages);
176 }
177
178 if (!DU::initDirectoryCache()) {
179 qFatal("Error initializing directory cache!");
180 return EXIT_FAILURE;
181 }
182
183 app.startInit();
184 if (!app.initBtConfig()) {
185 return EXIT_FAILURE;
186 }
187
189
190#ifdef Q_OS_WIN
191 // change directory to the Sword or .sword directory in the $HOME dir so that
192 // the sword.conf is found. It points to the sword/locales.d directory
193 QString homeSwordDir = util::directory::getUserHomeDir().absolutePath();
194 QDir dir;
195 dir.setCurrent(homeSwordDir);
196#endif
197
198 //first install QT's own translations
199 QLocale const defaultLocale;
200 QTranslator qtTranslator;
201 if (qtTranslator.load(defaultLocale,
202 QStringLiteral("qt_"),
203 QString(),
204 QLibraryInfo::path(QLibraryInfo::TranslationsPath)))
205 app.installTranslator(&qtTranslator);
206 //then our own
207 QTranslator bibleTimeTranslator;
208 if (bibleTimeTranslator.load(
209 defaultLocale,
210 QStringLiteral("bibletime_ui_"),
211 QString(),
212 DU::getLocaleDir().canonicalPath()))
213 app.installTranslator(&bibleTimeTranslator);
214
215 // Initialize display template manager:
216 if (!app.initDisplayTemplateManager()) {
217 qFatal("Error initializing display template manager!");
218 return EXIT_FAILURE;
219 }
220
221 app.initIcons();
222
223 auto * const mainWindow = new BibleTime(app);
224 mainWindow->setAttribute(Qt::WA_DeleteOnClose);
225
226 auto const showWelcome = CSwordBackend::instance().moduleList().empty();
227 if (showWelcome && BtWelcomeDialog().exec() == QDialog::Accepted)
228 mainWindow->slotBookshelfWizard();
229
230 mainWindow->show();
231
232 // The following must be done after the bibletime window is visible:
233 mainWindow->processCommandline(ignoreSession, openBibleKey);
234
235 if (!showWelcome
236 && btConfig().value<bool>(QStringLiteral("GUI/showTipAtStartup"), true))
237 mainWindow->slotOpenTipDialog();
238
239 return app.exec();
240}
241
BtConfig & btConfig()
This is a shortchand for BtConfig::getInstance().
Definition btconfig.h:305
void startInit()
bool initDisplayTemplateManager()
void setDebugMode(bool const debugMode) noexcept
void initLightDarkPalette()
static int typeId
static CSwordBackend & instance() noexcept
QList< CSwordModuleInfo * > const & moduleList() const
QStringList r(content.left(bodyIndex))
int main(int argc, char *argv[])
Definition main.cpp:154
int parseCommandLine(bool &showDebugMessages, bool &ignoreSession, QString &openBibleKey)
Definition main.cpp:77
void printHelp(const QString &executable)
Definition main.cpp:43
const QDir & getUserHomeDir()