-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
127 lines (107 loc) · 3.83 KB
/
main.cpp
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
#include <QApplication>
#include <QSharedMemory>
#include <QLocalServer>
#include <QLocalSocket>
#include <QLibraryInfo>
#include <QSettings>
#include <QTranslator>
#include <jemalloc/jemalloc.h>
#include "browser_mainwindow.h"
QString url_argument()
{
const QStringList args = QCoreApplication::arguments();
for (const QString& arg : args.mid(1))
{
if (!arg.startsWith(u'-'))
{
return arg;
}
}
return "about:blank";
}
void debug_handler (QtMsgType type, const QMessageLogContext& context, const QString& msg)
{
QByteArray localMsg = msg.toLocal8Bit();
const char* file = context.file ? context.file : "";
const char* function = context.function ? context.function : "";
FILE* f = fopen ("debug.log", "a");
switch (type)
{
case QtDebugMsg:
fprintf (f, "Debug: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
case QtInfoMsg:
fprintf (f, "Info: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
case QtWarningMsg:
fprintf (f, "Warning: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
case QtCriticalMsg:
fprintf (f, "Critical: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
case QtFatalMsg:
fprintf (f, "Fatal: %s (%s:%u, %s)\n", localMsg.constData(), file, context.line, function);
break;
}
fclose (f);
}
int main(int argc, char **argv)
{
QCoreApplication::setAttribute (Qt::AA_ShareOpenGLContexts);
QCoreApplication::setApplicationName ("Gizmoduck");
// Windows will fail to write settings without organization.
#ifdef WIN32
QCoreApplication::setOrganizationName ("Clockwork Fairies");
#endif
//setWindowTitle( QCoreApplication::applicationName() );
#ifdef QT_DEBUG
const QString localServerName = "Gizmoduck debug listener";
const QString uniqueKey = "Gizmoduck debug";
#else
const QString localServerName = "Gizmoduck listener";
const QString uniqueKey = "Gizmoduck";
#endif
// EXPERT SETTINGS ARE NO LONGER ACCEPTED BY COMMANDLINE.
// Use QTWEBENGINE_CHROMIUM_FLAGS env variable instead.
// qInstallMessageHandler (debug_handler);
QApplication app (argc, argv);
app.setWindowIcon (QIcon (QStringLiteral (":/icons/gizmoduck")));
QTranslator qt_translator;
qt_translator.load("qt_" + QLocale::system().name(), QLibraryInfo::location (QLibraryInfo::TranslationsPath));
app.installTranslator(&qt_translator);
QTranslator webengine_translator;
webengine_translator.load("qtwebengine_" + QLocale::system().name(), QLibraryInfo::location (QLibraryInfo::TranslationsPath));
app.installTranslator(&webengine_translator);
QTranslator browser_translator;
browser_translator.load(":/locale/gizmoduck_" + QLocale::system().name());
app.installTranslator(&browser_translator);
QSharedMemory sharedMemory(uniqueKey);
if (sharedMemory.attach())
{
QLocalSocket socket;
socket.connectToServer(localServerName);
if (socket.waitForConnected())
{
socket.write(url_argument().toUtf8());
socket.flush();
socket.disconnectFromServer();
}
return 0;
}
sharedMemory.create(4096);
QLocalServer server;
server.listen(localServerName);
MainWindow main_window;
main_window.show();
QObject::connect(&server, &QLocalServer::newConnection, [&]()
{
QLocalSocket* clientConnection = server.nextPendingConnection();
QObject::connect(clientConnection, &QLocalSocket::readyRead, [&]()
{
QByteArray data = clientConnection->readAll();
QUrl url = QUrl::fromUserInput(data);
main_window.open_url(url);
});
});
return app.exec();
}