-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathmain.js
More file actions
386 lines (301 loc) · 11.2 KB
/
main.js
File metadata and controls
386 lines (301 loc) · 11.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
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
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
global._ = require('./modules/utils/underscore');
const fs = require('fs');
const electron = require('electron');
const app = require('app'); // Module to control application life.
const timesync = require("os-timesync");
const BrowserWindow = require('browser-window'); // Module to create native browser window.
const Minimongo = require('./modules/minimongoDb.js');
const syncMinimongo = require('./modules/syncMinimongo.js');
const ipc = electron.ipcMain;
const dialog = require('dialog');
const packageJson = require('./package.json');
const i18n = require('./modules/i18n.js');
// GLOBAL Variables
global.path = {
HOME: app.getPath('home'),
APPDATA: app.getPath('appData'), // Application Support/
USERDATA: app.getPath('userData') // Application Aupport/Mist
};
global.appName = 'Mist';
global.production = false;
global.mode = 'wallet';
global.version = packageJson.version;
global.license = packageJson.license;
require('./modules/ipcCommunicator.js');
const appMenu = require('./modules/menuItems');
const ipcProviderBackend = require('./modules/ipc/ipcProviderBackend.js');
const NodeConnector = require('./modules/ipc/nodeConnector.js');
const popupWindow = require('./modules/popupWindow.js');
const ethereumNodes = require('./modules/ethereumNodes.js');
const getIpcPath = require('./modules/ipc/getIpcPath.js');
var ipcPath = getIpcPath();
global.mainWindow = null;
global.windows = {};
global.webviews = [];
global.nodes = {
geth: null,
eth: null
};
global.network = 'main'; // or 'test', will be set by the file later
global.mining = false;
global.icon = __dirname +'/icons/'+ global.mode +'/icon.png';
global.language = 'en';
global.i18n = i18n; // TODO: detect language switches somehow
global.Tabs = Minimongo('tabs');
global.nodeConnector = new NodeConnector(ipcPath);
// INTERFACE PATHS
global.interfaceAppUrl;
global.interfacePopupsUrl;
// WALLET
if(global.mode === 'wallet') {
global.interfaceAppUrl = (global.production)
? 'file://' + __dirname + '/interface/wallet/index.html'
: 'http://localhost:3050';
global.interfacePopupsUrl = (global.production)
? 'file://' + __dirname + '/interface/index.html'
: 'http://localhost:3000';
// MIST
} else {
global.interfaceAppUrl = global.interfacePopupsUrl = (global.production)
? 'file://' + __dirname + '/interface/index.html'
: 'http://localhost:3000';
}
// const getCurrentKeyboardLayout = require('keyboard-layout');
// const etcKeyboard = require('etc-keyboard');
// console.log(getCurrentKeyboardLayout());
// etcKeyboard(function (err, layout) {
// console.log('KEYBOARD:', layout);
// });
// const Menu = require('menu');
// const Tray = require('tray');
// var appIcon = null;
// const processRef = global.process;
// process.nextTick(function() { global.process = processRef; });
// prevent crashed and close gracefully
process.on('uncaughtException', function(error){
console.log('UNCAUGHT EXCEPTION', error.stack || error);
// var stack = new Error().stack;
// console.log(stack);
app.quit();
});
// Quit when all windows are closed.
app.on('window-all-closed', function() {
// if (process.platform != 'darwin')
app.quit();
});
// Listen to custom protocole incoming messages, needs registering of URL schemes
app.on('open-url', function (e, url) {
console.log('Open URL', url);
});
// app.on('will-quit', function(event){
// event.preventDefault()
// });
var killedSockets = false;
app.on('before-quit', function(event){
if(!killedSockets)
event.preventDefault();
// CLEAR open IPC sockets to geth
_.each(global.sockets, function(socket){
if(socket) {
console.log('Closing Socket ', socket.id);
socket.destroy();
}
});
// delay quit, so the sockets can close
setTimeout(function(){
killedSockets = true;
ethereumNodes.stopNodes(function(){
app.quit();
});
}, 500);
});
// Emitted when the application is activated while there is no opened windows.
// It usually happens when a user has closed all of application's windows and then
// click on the application's dock icon.
// app.on('activate-with-no-open-windows', function () {
// if (global.mainWindow) {
// global.mainWindow.show();
// }
// });
// append ignore GPU blacklist on linux
// if(process.platform === 'freebsd' ||
// process.platform === 'linux' ||
// process.platform === 'sunos') {
// app.commandLine.appendSwitch('ignore-cpu-blacklist');
// }
var appStartWindow;
var nodeType = 'geth';
var logFunction = function(data) {
data = data.toString().replace(/[\r\n]+/,'');
console.log('NODE LOG:', data);
// if(~data.indexOf('Block synchronisation started') && global.nodes[nodeType]) {
// global.nodes[nodeType].stdout.removeListener('data', logFunction);
// global.nodes[nodeType].stderr.removeListener('data', logFunction);
// }
// show line if its not empty or "------"
if(appStartWindow && !/^\-*$/.test(data) && !_.isEmpty(data)) {
console.log('"'+ data +'"');
appStartWindow.webContents.send('startScreenText', 'logText', data.replace(/^.*[0-9]\]/,''));
}
};
// This method will be called when Electron has done everything
// initialization and ready for creating browser windows.
app.on('ready', function() {
// init prepared popup window
popupWindow.loadingWindow.init();
// initialize the IPC provider on the main window
ipcProviderBackend();
// instantiate custom protocols
require('./customProtocols.js');
// add menu already here, so we have copy and past functionality
appMenu();
// appIcon = new Tray('./icons/icon-tray.png');
// var contextMenu = Menu.buildFromTemplate([
// { label: 'Item1', type: 'radio' },
// { label: 'Item2', type: 'radio' },
// { label: 'Item3', type: 'radio', checked: true },
// { label: 'Item4', type: 'radio' },
// ]);
// appIcon.setToolTip('This is my application.');
// appIcon.setContextMenu(contextMenu);
// Create the browser window.
// MIST
if(global.mode === 'mist') {
global.mainWindow = new BrowserWindow({
title: global.appName,
show: false,
width: 1024 + 208,
height: 720,
icon: global.icon,
titleBarStyle: 'hidden-inset', //hidden-inset: more space
backgroundColor: '#D2D2D2',
acceptFirstMouse: true,
darkTheme: true,
webPreferences: {
preload: __dirname +'/modules/preloader/mistUI.js',
nodeIntegration: false,
'overlay-scrollbars': true,
webaudio: true,
webgl: false,
textAreasAreResizable: true,
webSecurity: false // necessary to make routing work on file:// protocol
}
});
syncMinimongo(Tabs, global.mainWindow.webContents);
// WALLET
} else {
global.mainWindow = new BrowserWindow({
title: global.appName,
show: false,
width: 1100,
height: 720,
icon: global.icon,
titleBarStyle: 'hidden-inset', //hidden-inset: more space
backgroundColor: '#F6F6F6',
acceptFirstMouse: true,
darkTheme: true,
webPreferences: {
preload: __dirname +'/modules/preloader/wallet.js',
nodeIntegration: false,
'overlay-fullscreen-video': true,
'overlay-scrollbars': true,
webaudio: true,
webgl: false,
textAreasAreResizable: true,
webSecurity: false // necessary to make routing work on file:// protocol
}
});
}
appStartWindow = new BrowserWindow({
title: global.appName,
width: 400,
height: 230,
icon: global.icon,
resizable: false,
backgroundColor: '#F6F6F6',
useContentSize: true,
frame: false,
webPreferences: {
preload: __dirname +'/modules/preloader/splashScreen.js',
nodeIntegration: false,
webSecurity: false // necessary to make routing work on file:// protocol
}
});
appStartWindow.loadURL(global.interfacePopupsUrl + '#splashScreen_'+ global.mode);//'file://' + __dirname + '/interface/startScreen/'+ global.mode +'.html');
// check time sync
// var ntpClient = require('ntp-client');
// ntpClient.getNetworkTime("pool.ntp.org", 123, function(err, date) {
timesync.checkEnabled(function (err, enabled) {
if(err) {
console.error('Couldn\'t get time from NTP time sync server.', err);
return;
}
if(!enabled) {
dialog.showMessageBox({
type: "warning",
buttons: ['OK'],
message: global.i18n.t('mist.errors.timeSync.title'),
detail: global.i18n.t('mist.errors.timeSync.description') +"\n\n"+ global.i18n.t('mist.errors.timeSync.'+ process.platform)
}, function(){
});
}
});
appStartWindow.webContents.on('did-finish-load', function() {
// Skip node connection and go directly to main window
// since we're using external RPC provider
console.log('Skipping local node connection, using external RPC provider');
if(appStartWindow) {
appStartWindow.webContents.send('startScreenText', 'mist.startScreen.startedNode');
}
// Close splash screen and start main window
setTimeout(function() {
startMainWindow(appStartWindow);
}, 1000);
});
});
/**
Clears the socket
@method clearSocket
*/
var clearSocket = function(socket, timeout){
if(timeout) {
ethereumNodes.stopNodes();
}
socket.removeAllListeners();
socket.destroy();
socket = null;
}
/**
Start the main window and all its processes
@method startMainWindow
*/
var startMainWindow = function(appStartWindow){
// remove the splash screen logger
if(global.nodes[nodeType]) {
global.nodes[nodeType].stdout.removeListener('data', logFunction);
global.nodes[nodeType].stderr.removeListener('data', logFunction);
}
// and load the index.html of the app.
console.log('Loading Interface at '+ global.interfaceAppUrl);
global.mainWindow.loadURL(global.interfaceAppUrl);
global.mainWindow.webContents.on('did-finish-load', function() {
popupWindow.loadingWindow.hide();
global.mainWindow.show();
// global.mainWindow.center();
if(appStartWindow)
appStartWindow.close();
appStartWindow = null;
});
// close app, when the main window is closed
global.mainWindow.on('closed', function() {
global.mainWindow = null;
app.quit();
});
// STARTUP PROCESSES
// instantiate the application menu
// ipc.on('setupWebviewDevToolsMenu', function(e, webviews){
Tracker.autorun(function(){
global.webviews = Tabs.find({},{sort: {position: 1}, fields: {name: 1, _id: 1}}).fetch();
appMenu(global.webviews);
});
};