-
Notifications
You must be signed in to change notification settings - Fork 12
/
Kolibri.cpp
executable file
·339 lines (298 loc) · 9.73 KB
/
Kolibri.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
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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
#include "fle_win32_framework.h"
#include "config.h"
#include <sys/types.h>
#include <sys/stat.h>
#include "resource.h"
#include <iostream>
#include <filesystem>
#include <string>
#include <io.h>
#include <atlstr.h>
#include <fstream>
using namespace std;
// Declare global stuff that you need to use inside the functions.
fle_TrayWindow * window;
fle_TrayMenuItem * mnuLoadBrowser;
fle_TrayMenuItem * mnuOptions;
fle_TrayMenuItem * mnuRunUserLogsIn;
fle_TrayMenuItem * mnuOpenBrowserOption;
fle_TrayMenuItem * mnuRunAtStartup;
fle_TrayMenuItem * mnuExit;
bool needNotify = false;
bool isServerStarting = false;
wchar_t * getStr(int strId) {
/*
Get the equivalent strings in the resource file.
The installer will set the selected language in the KOLIBRI_GUI_LANG environment variable after the installation.
Use a sublime editor when adding a new language stings in the resource file
*/
CString str;
str.LoadString(strId);
char * userEnv = getenv("KOLIBRI_GUI_LANG");
if (userEnv != NULL) {
string langArr[28] = { "vi", "sw_TZ", "km", "es_ES", "mr", "fv", "it", "te",
"tl", "gu_IN", "yo", "zh_CN", "my", "ar", "pt_BR", "bg", "bn", "hi", "ka", \
"de", "ny", "ko", "fr", "fa", "la", "ne_NP", "ur_PK", "tr" };
int loopCount;
loopCount = 0;
for (string* lang = &langArr[0]; lang != &langArr[28]; ++lang) {
loopCount++;
if (userEnv == std::string(*lang)) {
str.LoadString(strId + loopCount);
}
}
}
TCHAR* finalStr = new TCHAR[str.GetLength() + 1];
lstrcpy(finalStr, str);
return finalStr;
}
char * getKolibriHomePath()
{
char * kolibriHomeEnv = getenv("KOLIBRI_HOME");
if (kolibriHomeEnv == NULL) {
kolibriHomeEnv = joinChr(getenv("HOMEPATH"), "\\.kolibri");
}
return kolibriHomeEnv;
}
char * getKolibriLinkAddress() {
/*
This will return the current Kolibri running link url.
It will fetch the PORT value from server.pid file.
*/
char * configName = "\\server.pid";
char * pidFile = joinChr(getKolibriHomePath(), configName);
char * httpLink = "";
std::ifstream file(pidFile);
if (file.is_open()) {
std::string line;
getline(file, line); // get the first line, do nothing
getline(file, line); // the second line should have the the port information
if (isServerOnline("Kolibri session", joinChr("http://127.0.0.1:", line.c_str()))) {
httpLink = joinChr("http://127.0.0.1:", line.c_str());
}
file.close();
return httpLink;
}
return "";
}
void kolibriScriptPath(char *buffer, const DWORD MAX_SIZE)
{
/*
Gets the path to Kolibri.bat script directory, from KOLIBRI_SCRIPT_DIR environment variable.
KOLIBRI_SCRIPT_DIR should be set at install time to e.g. \Python\Scripts, or wherever pip puts the kolibri.bat script.
:param char *buffer: the buffer to hold the path string. If KOLIBRI_SCRIPT_DIR is not set or is longer than MAX_SIZE, then this will be set to 0.
:param const DWORD MAX_SIZE: the max size of the buffer parameter. Must be large enough for path string and terminating null byte.
:returns: void
*/
LPCSTR kolibri_script_dir = "KOLIBRI_SCRIPT_DIR";
DWORD bufsize = GetEnvironmentVariableA(kolibri_script_dir, buffer, MAX_SIZE);
if (bufsize == 0) {
window->sendTrayMessage(L"Kolibri", getStr(ID_STRING_1_en));
buffer = 0;
}
else if (bufsize > MAX_SIZE) {
char err_message[255];
sprintf(err_message, "Error: the value of KOLIBRI_SCRIPT_DIR must be less than %d, but it was length %d. Please start Kolibri from the command line.", MAX_SIZE, bufsize);
MessageBox(HWND_DESKTOP, getStr(ID_STRING_18_en), getStr(ID_STRING_19_en), MB_OK | MB_ICONINFORMATION);
buffer = 0;
}
return;
}
void kolibriFailedMessage() {
MessageBox(HWND_DESKTOP, concatenateWchar(getStr(ID_STRING_21_en), getWC(getKolibriHomePath()), getWC("\\logs\\")), L"Kolibri", MB_OK | MB_ICONINFORMATION);
window->quit();
}
void startServerAction()
{
const DWORD MAX_SIZE = 255;
char script_dir[MAX_SIZE];
kolibriScriptPath(script_dir, MAX_SIZE);
if (!_access(script_dir, 0) == 0) {
window->sendTrayMessage(L"Kolibri", getStr(ID_STRING_5_en));
return;
}
SHELLEXECUTEINFO commandInfo = getCommandInfo("kolibri.exe", "start", script_dir);
ShellExecuteEx(&commandInfo);
WaitForSingleObject(commandInfo.hProcess, 10000L);
Sleep(200);
DWORD exitCode = 9000;
GetExitCodeProcess(commandInfo.hProcess, &exitCode);
WCHAR code[100];
swprintf_s(code, L"%d", exitCode);
OutputDebugString(code);
wstring wstrcode = std::wstring(code);
if (wstrcode == std::wstring(L"1")) {
if (isServerOnline("Kolibri session", getKolibriLinkAddress())){
needNotify = true;
isServerStarting = true;
window->sendTrayMessage(L"Kolibri", getStr(ID_STRING_4_en));
// Kolibri server started
}
else {
kolibriFailedMessage();
// Kolibri failed to start
}
}
if (wstrcode == std::wstring(L"259")) {
needNotify = true;
isServerStarting = true;
window->sendTrayMessage(L"Kolibri", getStr(ID_STRING_4_en));
// Kolibri still starting
}
OutputDebugString(code);
if (wstrcode == std::wstring(L"9000")) {
kolibriFailedMessage();
// Kolibri failed to start
}
}
void loadBrowserAction()
{
if (isServerOnline("Kolibri session", getKolibriLinkAddress())) {
if (!loadBrowser(getKolibriLinkAddress())) {
// Handle error.
printConsole("Failed to open the Kolibri url.\n");
}
}
else {
startServerAction();
}
}
void checkServerThread()
{
// We can handle things like checking if the server is online and controlling the state of each component.
if (isServerOnline("Kolibri session", getKolibriLinkAddress())) {
mnuLoadBrowser->enable();
if (needNotify) {
if (isSetConfigurationValueTrue("RUN_OPEN_BROWSER")) {
loadBrowserAction();
}
window->sendTrayMessage(getStr(ID_STRING_10_en), concatenateWchar(getStr(ID_STRING_11_en), getWC(getKolibriLinkAddress()), getStr(ID_STRING_20_en)));
needNotify = false;
}
isServerStarting = false;
}
}
void stopServerAction()
{
const DWORD MAX_SIZE = 255;
char script_dir[MAX_SIZE];
kolibriScriptPath(script_dir, MAX_SIZE);
if (!runShellScript("kolibri.exe", "stop", script_dir)) {
// Handle error.
}
}
void exitKolibriAction()
{
if (ask(getStr(ID_STRING_6_en), getStr(ID_STRING_7_en))) {
stopServerAction();
window->sendTrayMessage(getStr(ID_STRING_8_en), getStr(ID_STRING_9_en));
window->quit();
}
}
void runUserLogsInAction()
{
if (mnuRunUserLogsIn->isChecked()) {
if (!runShellScript("guitools.vbs", "1", NULL))
{
// Handle error.
printConsole("Failed to remove startup shortcut.\n");
}
else {
mnuRunUserLogsIn->uncheck();
setConfigurationValue("RUN_AT_LOGIN", "FALSE");
setConfigurationValue("DONT_START", "TRUE");
}
}
else {
if (!runShellScript("guitools.vbs", "0", NULL)) {
// Handle error.
printConsole("Failed to add startup shortcut.\n");
}
else
{
mnuRunUserLogsIn->check();
setConfigurationValue("RUN_AT_LOGIN", "TRUE");
}
}
}
void runOpenBrowserOption()
{
if (mnuOpenBrowserOption->isChecked()) {
mnuOpenBrowserOption->uncheck();
setConfigurationValue("RUN_OPEN_BROWSER", "FALSE");
setConfigurationValue("OPEN_BROWSER_OPTION", "TRUE");
}
else {
mnuOpenBrowserOption->check();
setConfigurationValue("RUN_OPEN_BROWSER", "TRUE");
}
}
void serverStartingMsg() {
if (isServerStarting) {
window->sendTrayMessage(L"Kolibri", getStr(ID_STRING_4_en));
}
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{
// REF: http://stackoverflow.com/questions/8799646/preventing-multiple-instances-of-my-application
// Prevent the Kolibri application to execute multiple instances.
HANDLE hMutex = CreateMutexA(NULL, FALSE, "Kolibri");
DWORD dwMutexWaitResult = WaitForSingleObject(hMutex, 0);
if (dwMutexWaitResult != WAIT_OBJECT_0) {
if (isServerOnline("Kolibri session", getKolibriLinkAddress())) {
loadBrowserAction();
}
else {
MessageBox(HWND_DESKTOP, getStr(ID_STRING_12_en), getStr(ID_STRING_13_en), MB_OK | MB_ICONINFORMATION);
}
CloseHandle(hMutex);
return false;
}
// Verify if there is an ongoing Kolibri installation.
wchar_t * kolibriSetup = _wgetenv(L"KOLIBRI_SETUP");
if (kolibriSetup != NULL) {
if (findProcessId(kolibriSetup)) {
MessageBox(HWND_DESKTOP, getStr(ID_STRING_18_en), getStr(ID_STRING_19_en), MB_OK | MB_ICONINFORMATION);
CloseHandle(hMutex);
return false;
}
}
startThread(NULL, TRUE, 3000, &checkServerThread);
startThread(NULL, TRUE, 5000, &serverStartingMsg);
window = new fle_TrayWindow(&hInstance);
window->setTrayIcon("images\\logo48.ico");
mnuLoadBrowser = new fle_TrayMenuItem(getStr(ID_STRING_14_en), &loadBrowserAction);
mnuRunUserLogsIn = new fle_TrayMenuItem(getStr(ID_STRING_15_en), &runUserLogsInAction);
mnuOpenBrowserOption = new fle_TrayMenuItem(getStr(ID_STRING_16_en), &runOpenBrowserOption);
mnuExit = new fle_TrayMenuItem(getStr(ID_STRING_17_en), &exitKolibriAction);
window->addMenu(mnuLoadBrowser);
window->addMenu(mnuRunUserLogsIn);
window->addMenu(mnuOpenBrowserOption);
window->addMenu(mnuExit);
startServerAction();
// Load configurations.
if (isSetConfigurationValueTrue("RUN_AT_LOGIN"))
{
mnuRunUserLogsIn->check();
}
if (!isSetConfigurationValueTrue("DONT_START"))
{
if (!runShellScript("guitools.vbs", "0", NULL))
{
// Handle error.
printConsole("Failed to add startup shortcut.\n");
}
else
{
mnuRunUserLogsIn->check();
setConfigurationValue("RUN_AT_LOGIN", "TRUE");
}
}
if (!isSetConfigurationValueTrue("OPEN_BROWSER_OPTION"))
{
mnuOpenBrowserOption->check();
setConfigurationValue("RUN_OPEN_BROWSER", "TRUE");
}
window->show();
return 0;
}