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
|
#include <QApplication>
#include <QDebug>
#include <QSettings>
#include <QWebEngineProfile>
#include <QWebEngineSettings>
#include <QtWebEngine>
#include <QtWidgets>
#include "common.h"
#include "def.h"
#include "mainwindow.h"
#include <singleapplication.h>
int main(int argc, char *argv[]) {
QApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
#ifdef QT_DEBUG
qputenv("QTWEBENGINE_CHROMIUM_FLAGS", "--remote-debugging-port=9421 --ignore-gpu-blocklist --no-sandbox");
#else
qputenv("QTWEBENGINE_CHROMIUM_FLAGS", "--disable-logging --ignore-gpu-blocklist --no-sandbox"); //--single-process
#endif
SingleApplication instance(argc, argv, true);
instance.setQuitOnLastWindowClosed(false);
instance.setWindowIcon(QIcon(":/icons/app/icon-128.png"));
QApplication::setApplicationName("WhatSie");
QApplication::setDesktopFileName("com.ktechpit.whatsie");
QApplication::setOrganizationDomain("com.ktechpit");
QApplication::setOrganizationName("org.keshavnrj.ubuntu");
QApplication::setApplicationVersion(VERSIONSTR);
QCommandLineParser parser;
parser.setApplicationDescription(
QObject::tr("Feature rich WhatsApp web client based on Qt WebEngine"));
QList<QCommandLineOption> secondaryInstanceCLIOptions;
QCommandLineOption showCLIHelpOption(
QStringList() << "h"
<< "help",
QObject::tr("Displays help on commandline options"));
QCommandLineOption openSettingsOption(
QStringList() << "s"
<< "open-settings",
QObject::tr("Opens Settings dialog in a running instance of ") +
QApplication::applicationName());
QCommandLineOption lockAppOption(QStringList() << "l"
<< "lock-app",
QObject::tr("Locks a running instance of ") +
QApplication::applicationName());
QCommandLineOption openAboutOption(
QStringList() << "i"
<< "open-about",
QObject::tr("Opens About dialog in a running instance of ") +
QApplication::applicationName());
QCommandLineOption toggleThemeOption(
QStringList() << "t"
<< "toggle-theme",
QObject::tr(
"Toggle between dark & light theme in a running instance of ") +
QApplication::applicationName());
QCommandLineOption reloadAppOption(
QStringList() << "r"
<< "reload-app",
QObject::tr("Reload the app in a running instance of ") +
QApplication::applicationName());
QCommandLineOption newChatOption(
QStringList() << "n"
<< "new-chat",
QObject::tr("Open new chat prompt in a running instance of ") +
QApplication::applicationName());
QCommandLineOption buildInfoOption(QStringList() << "b"
<< "build-info",
"Shows detailed current build infomation");
QCommandLineOption showAppWindowOption(
QStringList() << "w"
<< "show-window",
QObject::tr("Show main window of running instance of ") +
QApplication::applicationName());
parser.addOption(showCLIHelpOption); // [x]
parser.addVersionOption(); // [x]
parser.addOption(buildInfoOption); // [x]
parser.addOption(showAppWindowOption); // [x]
parser.addOption(openSettingsOption); // [x]
parser.addOption(lockAppOption); // [x]
parser.addOption(openAboutOption); // [x]
parser.addOption(toggleThemeOption); // [x]
parser.addOption(reloadAppOption); // [x]
parser.addOption(newChatOption); // [-]
secondaryInstanceCLIOptions << showAppWindowOption << openSettingsOption
<< lockAppOption << openAboutOption
<< toggleThemeOption << reloadAppOption
<< newChatOption;
parser.process(instance);
if (parser.isSet(showCLIHelpOption)) {
parser.showHelp();
}
if (parser.isSet(buildInfoOption)) {
qInfo().noquote()
<< parser.applicationDescription() << "\n"
<< QStringLiteral("version: %1, branch: %2, commit: %3, built_at: %4")
.arg(VERSIONSTR, GIT_BRANCH, GIT_HASH, BUILD_TIMESTAMP);
return 0;
}
// if secondary instance is invoked
if (instance.isSecondary()) {
instance.sendMessage(instance.arguments().join(' ').toUtf8());
qInfo().noquote() << QApplication::applicationName() +
" is already running with PID: " +
QString::number(instance.primaryPid()) +
" by USER:"
<< instance.primaryUser();
return 0;
}
QWebEngineSettings::defaultSettings()->setAttribute(
QWebEngineSettings::DnsPrefetchEnabled, true);
QWebEngineSettings::defaultSettings()->setAttribute(
QWebEngineSettings::FullScreenSupportEnabled, true);
QWebEngineSettings::defaultSettings()->setAttribute(
QWebEngineSettings::JavascriptCanAccessClipboard, true);
MainWindow whatsie;
// else
QObject::connect(
&instance, &SingleApplication::receivedMessage, &whatsie,
[&whatsie, &secondaryInstanceCLIOptions](int instanceId,
QByteArray message) {
qInfo().noquote() << "Another instance with PID: " +
QString::number(instanceId) +
", sent argument: " + message;
QString messageStr = QTextCodec::codecForMib(106)->toUnicode(message);
QCommandLineParser p;
p.addOptions(secondaryInstanceCLIOptions);
p.parse(QStringList(messageStr.split(" ")));
if (p.isSet("s")) {
qInfo() << "cmd:"
<< "OpenAppSettings";
whatsie.alreadyRunning();
whatsie.showSettings(true);
return;
}
if (p.isSet("l")) {
qInfo() << "cmd:"
<< "LockApp";
whatsie.alreadyRunning();
QSettings settings;
if (!settings.value("asdfg").isValid()) {
whatsie.notify(
QApplication::applicationName(),
QObject::tr("App lock is not configured, \n"
"Please setup the password in the Settings "
"first."));
} else {
whatsie.lockApp();
}
return;
}
if (p.isSet("i")) {
qInfo() << "cmd:"
<< "OpenAppAbout";
whatsie.alreadyRunning();
whatsie.showAbout();
return;
}
if (p.isSet("t")) {
qInfo() << "cmd:"
<< "ToggleAppTheme";
whatsie.alreadyRunning();
whatsie.toggleTheme();
return;
}
if (p.isSet("r")) {
qInfo() << "cmd:"
<< "ReloadApp";
whatsie.alreadyRunning();
whatsie.doReload(false, true);
return;
}
if (p.isSet("n")) {
qInfo() << "cmd:"
<< "OpenNewChatPrompt";
whatsie.alreadyRunning();
whatsie.newChat(); //TODO: invetigate the crash
return;
}
if (p.isSet("w")) {
qInfo() << "cmd:"
<< "ShowAppWindow";
whatsie.alreadyRunning();
whatsie.show();
return;
}
if (messageStr.contains("whatsapp://", Qt::CaseInsensitive)) {
QString urlStr =
"whatsapp://" + messageStr.split("whatsapp://").last();
qInfo() << "cmd:"
<< "x-schema-handler";
whatsie.loadSchemaUrl(urlStr);
} else {
whatsie.alreadyRunning(true);
}
});
foreach (QString argStr, instance.arguments()) {
if (argStr.contains("whatsapp://")) {
qInfo() << "cmd:"
<< "x-schema-handler";
whatsie.loadSchemaUrl(argStr);
}
}
QSettings settings;
if (QSystemTrayIcon::isSystemTrayAvailable() &&
settings.value("startMinimized", false).toBool()) {
whatsie.runMinimized();
} else {
whatsie.show();
}
return instance.exec();
}
|