forked from llaske/sugarizer
-
Notifications
You must be signed in to change notification settings - Fork 59
/
main.js
226 lines (206 loc) · 6.31 KB
/
main.js
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
// Main file, used only for Electron
var electron = require('electron'),
fs = require('fs'),
temp = require('tmp'),
path = require('path'),
requirejs = require('requirejs'),
l10n = require('./lib/l10n');
var app = electron.app;
var BrowserWindow = electron.BrowserWindow;
var Menu = electron.Menu;
var ipc = electron.ipcMain;
var dialog = electron.dialog;
var nativeImage = electron.nativeImage;
var mainWindow = null;
var debug = false;
var frameless = true;
var reinit = false;
var logoff = false;
// Save a file
function saveFile(file, arg, sender) {
var buf;
if (arg.text) {
buf = arg.text;
} else {
var data = arg.binary.replace(/^data:.+;base64,/, "");
buf = new Buffer(data, 'base64');
}
fs.writeFile(file, buf, function(err) {
sender.send('save-file-reply', {err: err, filename: file});
});
}
// Load a file
function LoadFile(event, file) {
var extension = path.extname(file).substr(1);
var fileProperty = {};
fileProperty.name = path.basename(file);
var extToMimetypes = {'json':'application/json','jpg':'image/jpeg','png':'image/png','wav':'audio/wav','webm':'video/webm','mp3':'audio/mp3','mp4':'video/mp4','txt':'text/plain','pdf':'application/pdf','doc':'application/msword','odt':'application/vnd.oasis.opendocument.text'};
for (var ext in extToMimetypes) {
if (ext == extension) {
fileProperty.type = extToMimetypes[ext];
break;
}
}
var json = (extension == 'json' ? 'utf8' : null);
fs.readFile(file, json, function(err, data) {
var text = (json ? data : "data:"+fileProperty.type+";base64,"+data.toString('base64'));
event.sender.send('choose-files-reply', fileProperty, err, text);
});
}
function createWindow () {
// Process argument
for (var i = 0 ; i < process.argv.length ; i++) {
if (process.argv[i] == '--sdebug') {
debug = true;
} else if (process.argv[i] == '--window') {
frameless = false;
} else if (process.argv[i] == '--init') {
reinit = true;
} else if (process.argv[i] == '--logoff') {
logoff = true;
}
}
// Create the browser window
mainWindow = new BrowserWindow({
show: false,
backgroundColor: '#FFF',
minWidth: 640,
minHeight: 480,
fullscreen: frameless,
frame: !frameless,
webPreferences: {
webSecurity: false,
contextIsolation: false,
nodeIntegration: true
},
icon: nativeImage.createFromPath('./res/icon/electron/icon-1024.png')
});
if (process.platform === 'darwin') {
app.dock.setIcon(app.getAppPath()+'/res/icon/electron/icon-1024.png');
}
// Load the index.html of Sugarizer
mainWindow.loadURL('file://'+app.getAppPath()+'/index.html'+(reinit?'?rst=1':'')+(logoff?'?rst=2':''));
if (frameless) {
mainWindow.maximize();
}
// Wait for 'ready-to-show' to display our window
mainWindow.webContents.once('did-finish-load', function() {
// Initialize locales
l10n.init(app.getLocale() || "en");
// Handle save file dialog
ipc.on('save-file-dialog', function(event, arg) {
var saveFunction = function(file) {
if (file) {
saveFile(file, arg, event.sender);
}
}
if (!arg.directory) {
// Ask directory to use, then save
var dialogSettings = {
defaultPath: arg.filename,
filters: [
{ name: arg.mimetype, extensions: [arg.extension] }
]
};
dialogSettings.title = l10n.get("SaveFile");
dialogSettings.buttonLabel = l10n.get("Save");
dialog.showSaveDialog(dialogSettings).then(function(result) {
saveFunction(result.filePath);
});
} else {
// Save in the directory provided
saveFunction(path.join(arg.directory,arg.filename));
}
});
ipc.on('choose-directory-dialog', function(event) {
var dialogSettings = {
properties: ['openDirectory', 'createDirectory']
};
dialogSettings.title = l10n.get("ChooseDirectory");
dialogSettings.buttonLabel = l10n.get("Choose");
dialog.showOpenDialog(dialogSettings).then(function(result) {
var files = result.filePaths;
if (files && files.length > 0) {
event.sender.send('choose-directory-reply', files[0]);
}
});
});
ipc.on('choose-files-dialog', function(event) {
var dialogSettings = {
properties: ['openFile', 'multiSelections'],
filters: [
{name: 'Activities', extensions: ['jpg','png','json','webm','wav','mp3','mp4','pdf','txt','doc','odt']}
]
};
dialogSettings.title = l10n.get("ChooseFiles");
dialogSettings.buttonLabel = l10n.get("Choose");
dialogSettings.filters[0].name = l10n.get("FilesSupported");
dialog.showOpenDialog(dialogSettings).then(function(result) {
var files = result.filePaths;
if (files && files.length > 0) {
for (var i = 0 ; i < files.length ; i++) {
LoadFile(event, files[i]);
}
}
});
});
ipc.on('create-tempfile', function(event, arg) {
temp.file('sugarizer', function(err, path, fd) {
if (!err) {
var data = arg.text.replace(/^data:.+;base64,/, "");
var buf = Buffer.from(data, 'base64');
fs.writeFile(fd, buf, function(err) {
event.sender.send('create-tempfile-reply', path);
});
}
});
});
// Build menu
var template = [];
if (process.platform === 'darwin') {
var appname = electron.app.getName();
var menu = {
label: appname,
submenu: [{
accelerator: 'Command+Q',
click: function () {
app.quit()
}
}]
};
menu.submenu[0].label = l10n.get("Quit");
template.unshift(menu);
const { systemPreferences } = require('electron')
systemPreferences.askForMediaAccess('microphone');
systemPreferences.askForMediaAccess('camera');
}
var menu = Menu.buildFromTemplate(template);
Menu.setApplicationMenu(menu);
// Debug console
if (debug) {
var devtools = new BrowserWindow();
mainWindow.webContents.setDevToolsWebContents(devtools.webContents);
mainWindow.webContents.openDevTools({ mode: 'detach' });
}
// Show wmain window
mainWindow.show();
});
// Emitted when the window is closed
mainWindow.on('closed', function() {
mainWindow = null;
});
}
// This method will be called when Electron has finished
// initialization and is ready to create browser windows
app.on('ready', createWindow);
// Quit when all windows are closed.
app.on('window-all-closed', function () {
app.quit()
});
app.on('activate', function () {
// On OS X it's common to re-create a window in the app when the
// dock icon is clicked and there are no other windows open.
if (mainWindow === null) {
createWindow();
}
});