-
Notifications
You must be signed in to change notification settings - Fork 112
Expand file tree
/
Copy pathmain.cpp
More file actions
276 lines (228 loc) · 9.2 KB
/
main.cpp
File metadata and controls
276 lines (228 loc) · 9.2 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
#include "library_window.h"
#include <QApplication>
#include <QDir>
#include <QFileInfo>
#include <QLocale>
#include <QSettings>
#include <QSysInfo>
#if !defined use_unarr && !defined use_libarchive
#include <QLibrary>
#endif
#include "app_language_utils.h"
#include "appearance_configuration.h"
#include "comic_db.h"
#include "data_base_management.h"
#include "db_helper.h"
#include "exit_check.h"
#include "theme_manager.h"
#include "theme_repository.h"
#include "yacreader_global.h"
#include "yacreader_http_server.h"
#include "yacreader_libraries.h"
#include "yacreader_local_server.h"
#include <QCommandLineParser>
#include <QImageReader>
#ifdef Q_OS_MACOS
#include "trayhandler.h"
#endif
#include "QsLog.h"
#include "QsLogDest.h"
#define PICTUREFLOW_QT4 1
// Server interface
YACReaderHttpServer *httpServer;
using namespace QsLogging;
void logSystemAndConfig()
{
QLOG_INFO() << "---------- System & configuration ----------";
QLOG_INFO() << "YACReader version:" << VERSION;
QLOG_INFO() << "Qt version:" << qVersion();
QLOG_INFO() << "OS:" << QSysInfo::prettyProductName() << "Version: " << QSysInfo::productVersion();
QLOG_INFO() << "Kernel:" << QSysInfo::kernelType() << QSysInfo::kernelVersion() << "Architecture:" << QSysInfo::currentCpuArchitecture() << "ABI:" << QSysInfo::buildAbi();
#if !defined use_unarr && !defined use_libarchive
#ifdef Q_OS_WIN
if (QLibrary::isLibrary(QApplication::applicationDirPath() + "/utils/7z.dll"))
#elif defined Q_OS_UNIX && !defined Q_OS_MACOS
if (QLibrary::isLibrary(QString(LIBDIR) + "/yacreader/7z.so") | QLibrary::isLibrary(QString(LIBDIR) + "/7zip/7z.so"))
#else
if (QLibrary::isLibrary(QApplication::applicationDirPath() + "/utils/7z.so"))
#endif
QLOG_INFO() << "7z : found";
else
QLOG_ERROR() << "7z : not found";
#elif defined use_libarchive
QLOG_INFO() << "using libarchive decompression backend";
#else // use_unarr
QLOG_INFO() << "using unarr decompression backend";
#endif // use_unarr
#if defined Q_OS_UNIX && !defined Q_OS_MACOS
if (QFileInfo(QString(BINDIR) + "/qrencode").exists())
#else
if (QFileInfo(QApplication::applicationDirPath() + "/utils/qrencode.exe").exists() || QFileInfo("./util/qrencode").exists())
#endif
QLOG_INFO() << "qrencode : found";
else
QLOG_INFO() << "qrencode : not found";
QSettings settings(YACReader::getSettingsPath() + "/YACReaderLibrary.ini", QSettings::IniFormat);
settings.beginGroup("libraryConfig");
if (settings.value(SERVER_ON, true).toBool())
QLOG_INFO() << "server : enabled";
else
QLOG_INFO() << "server : disabled";
auto libraries = DBHelper::getLibraries().getLibraries();
QLOG_INFO() << "Libraries: ";
for (auto library : libraries) {
QLOG_INFO() << " " << library;
auto access = DataBaseManagement::getDatabaseAccess(library.getPath());
QLOG_INFO() << " > STATUS: " << access;
}
QLOG_INFO() << "--------------------------------------------";
}
void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
Q_UNUSED(context);
QByteArray localMsg = msg.toLocal8Bit();
switch (type) {
case QtInfoMsg: {
QLOG_INFO() << localMsg.constData();
break;
}
case QtDebugMsg: {
QLOG_DEBUG() << localMsg.constData();
break;
}
case QtWarningMsg: {
QLOG_WARN() << localMsg.constData();
break;
}
case QtCriticalMsg: {
QLOG_ERROR() << localMsg.constData();
break;
}
case QtFatalMsg: {
QLOG_FATAL() << localMsg.constData();
break;
}
}
}
int main(int argc, char **argv)
{
qInstallMessageHandler(messageHandler);
static const char ENV_VAR_QT_DEVICE_PIXEL_RATIO[] = "QT_DEVICE_PIXEL_RATIO";
if (!qEnvironmentVariableIsSet(ENV_VAR_QT_DEVICE_PIXEL_RATIO) && !qEnvironmentVariableIsSet("QT_AUTO_SCREEN_SCALE_FACTOR") && !qEnvironmentVariableIsSet("QT_SCALE_FACTOR") && !qEnvironmentVariableIsSet("QT_SCREEN_SCALE_FACTORS")) {
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
}
QApplication::setAttribute(Qt::AA_UseHighDpiPixmaps);
QApplication::setHighDpiScaleFactorRoundingPolicy(Qt::HighDpiScaleFactorRoundingPolicy::PassThrough);
QImageReader::setAllocationLimit(0);
QApplication app(argc, argv);
app.setApplicationName("YACReaderLibrary");
app.setOrganizationName("YACReader");
app.setApplicationVersion(VERSION);
YACReader::initializeSharedPluginPaths();
// Theme initialization
auto *appearanceConfig = new AppearanceConfiguration(
YACReader::getSettingsPath() + "/YACReaderLibrary.ini", qApp);
auto *themeRepo = new ThemeRepository(
":/themes", YACReader::getSettingsPath() + "/themes/user", "YACReaderLibrary");
ThemeManager::instance().initialize(appearanceConfig, themeRepo);
// Set window icon according to Freedesktop icon specification
// This is mostly relevant for Linux and other Unix systems
if (QIcon::hasThemeIcon("YACReaderLibrary")) {
app.setWindowIcon(QIcon::fromTheme("YACReaderLibrary"));
}
// TODO: We might want to add a fallback icon here.
QString destLog = YACReader::getSettingsPath() + "/yacreaderlibrary.log";
QDir().mkpath(YACReader::getSettingsPath());
Logger &logger = Logger::instance();
#ifdef QT_DEBUG
logger.setLoggingLevel(QsLogging::TraceLevel);
#else
logger.setLoggingLevel(QsLogging::ErrorLevel);
#endif
DestinationPtrU fileDestination(DestinationFactory::MakeFileDestination(
destLog, LogRotationOption::EnableLogRotation, MaxSizeBytes(1048576), MaxOldLogCount(2)));
DestinationPtrU debugDestination(DestinationFactory::MakeDebugOutputDestination());
logger.addDestination(std::move(debugDestination));
logger.addDestination(std::move(fileDestination));
QSettings uiSettings(YACReader::getSettingsPath() + "/YACReaderLibrary.ini", QSettings::IniFormat);
uiSettings.beginGroup("libraryConfig");
QString selectedLanguage = uiSettings.value(UI_LANGUAGE).toString();
uiSettings.endGroup();
YACReader::UiLanguage::applyLanguage("yacreaderlibrary", selectedLanguage);
/*QTranslator viewerTranslator;
#if defined Q_OS_UNIX && !defined Q_OS_MACOS
viewerTranslator.load(QString(DATADIR) + "/yacreader/languages/yacreader_" + sufix);
#else
viewerTranslator.load(QCoreApplication::applicationDirPath() + "/languages/yacreader_" + sufix);
#endif
app.installTranslator(&viewerTranslator);*/
qRegisterMetaType<ComicDB>("ComicDB");
QCommandLineParser parser;
parser.addHelpOption();
parser.addVersionOption();
parser.addOption({ "loglevel", "Set log level. Valid values: trace, info, debug, warn, error.", "loglevel", "warning" });
parser.process(app);
if (parser.isSet("loglevel")) {
if (parser.value("loglevel") == "trace") {
logger.setLoggingLevel(QsLogging::TraceLevel);
} else if (parser.value("loglevel") == "info") {
logger.setLoggingLevel(QsLogging::InfoLevel);
} else if (parser.value("loglevel") == "debug") {
logger.setLoggingLevel(QsLogging::DebugLevel);
} else if (parser.value("loglevel") == "warn") {
logger.setLoggingLevel(QsLogging::WarnLevel);
} else if (parser.value("loglevel") == "error") {
logger.setLoggingLevel(QsLogging::ErrorLevel);
} else {
parser.showHelp();
}
}
QLOG_INFO() << "YACReaderLibrary attempting to start";
logSystemAndConfig();
if (YACReaderLocalServer::isRunning()) // only a single instance of YACReaderLibrary is allowed
{
QLOG_WARN() << "another instance of YACReaderLibrary is running";
#ifdef Q_OS_WIN
logger.shutDownLoggerThread();
#endif
return 0;
}
QLOG_INFO() << "YACReaderLibrary starting";
#ifdef SERVER_RELEASE
QSettings *settings = new QSettings(YACReader::getSettingsPath() + "/YACReaderLibrary.ini", QSettings::IniFormat);
settings->beginGroup("libraryConfig");
httpServer = new YACReaderHttpServer();
if (settings->value(SERVER_ON, true).toBool()) {
httpServer->start();
}
#endif
auto localServer = new YACReaderLocalServer();
auto mw = new LibraryWindow();
mw->connect(localServer, &YACReaderLocalServer::comicUpdated, mw, &LibraryWindow::updateViewsOnComicUpdate, Qt::QueuedConnection);
mw->connect(httpServer, &YACReaderHttpServer::comicUpdated, mw, &LibraryWindow::updateViewsOnComicUpdateWithId, Qt::QueuedConnection);
mw->connect(httpServer, &YACReaderHttpServer::clientSync, mw, &LibraryWindow::updateViewsOnClientSync, Qt::QueuedConnection);
// connections to localServer
// start as tray
if (!settings->value(START_TO_TRAY, false).toBool() || !settings->value(CLOSE_TO_TRAY, false).toBool()) {
mw->show();
}
#ifdef Q_OS_MACOS
else {
OSXHideDockIcon();
}
#endif
app.installEventFilter(mw);
int ret = app.exec();
QLOG_INFO() << "YACReaderLibrary closed with exit code :" << ret;
YACReader::exitCheck(ret);
// shutdown
httpServer->stop();
delete httpServer;
localServer->close();
delete localServer;
delete mw;
#ifdef Q_OS_WIN
logger.shutDownLoggerThread();
#endif
return ret;
}