not really known
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

245 lines
6.3 KiB

  1. // Main file, used only for Electron
  2. var electron = require('electron'),
  3. fs = require('fs'),
  4. temp = require('tmp'),
  5. ini = require('ini'),
  6. path = require('path'),
  7. requirejs = require('requirejs');
  8. var app = electron.app;
  9. var BrowserWindow = electron.BrowserWindow;
  10. var Menu = electron.Menu;
  11. var ipc = electron.ipcMain;
  12. var dialog = electron.dialog;
  13. var mainWindow = null;
  14. var debug = false;
  15. var frameless = true;
  16. var reinit = false;
  17. // Localization features
  18. l10n = {
  19. ini: null,
  20. language: '*',
  21. init: function() {
  22. this.language = app.getLocale() || "*";
  23. this.ini = ini.parse(fs.readFileSync(app.getAppPath()+'/locale.ini', 'utf-8'));
  24. },
  25. setLanguage: function(lang) {
  26. this.language = lang;
  27. },
  28. getLanguage: function() {
  29. return this.language;
  30. },
  31. get: function(text, params) {
  32. var locales = this.ini[this.language];
  33. if (!locales) {
  34. locales = this.ini['*'];
  35. }
  36. if (!locales[text]) {
  37. return text;
  38. }
  39. var translate = locales[text];
  40. for (var param in params) {
  41. translate = translate.replace('{{'+param+'}}', params[param]);
  42. }
  43. return translate;
  44. }
  45. }
  46. // Save a file
  47. function saveFile(file, arg, sender) {
  48. var buf;
  49. if (arg.text) {
  50. buf = arg.text;
  51. } else {
  52. var data = arg.binary.replace(/^data:.+;base64,/, "");
  53. buf = new Buffer(data, 'base64');
  54. }
  55. fs.writeFile(file, buf, function(err) {
  56. sender.send('save-file-reply', {err: err, filename: file});
  57. });
  58. }
  59. // Load a file
  60. function LoadFile(event, file) {
  61. var extension = path.extname(file).substr(1);
  62. var fileProperty = {};
  63. fileProperty.name = path.basename(file);
  64. 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'};
  65. for (var ext in extToMimetypes) {
  66. if (ext == extension) {
  67. fileProperty.type = extToMimetypes[ext];
  68. break;
  69. }
  70. }
  71. var json = (extension == 'json' ? 'utf8' : null);
  72. fs.readFile(file, function(err, data) {
  73. var text = (json ? data : "data:"+fileProperty.type+";base64,"+data.toString('base64'));
  74. event.sender.send('choose-files-reply', fileProperty, err, text);
  75. });
  76. }
  77. function createWindow () {
  78. // Process argument
  79. for (var i = 0 ; i < process.argv.length ; i++) {
  80. if (process.argv[i] == '--debug') {
  81. debug = true;
  82. } else if (process.argv[i] == '--window') {
  83. frameless = false;
  84. } else if (process.argv[i] == '--init') {
  85. reinit = true;
  86. }
  87. }
  88. // Create the browser window
  89. mainWindow = new BrowserWindow({
  90. show: false,
  91. backgroundColor: '#FFF',
  92. minWidth: 640,
  93. minHeight: 480,
  94. fullscreen: frameless,
  95. frame: !frameless,
  96. webPreferences: {
  97. webSecurity: false,
  98. nodeIntegration: true
  99. },
  100. icon: './res/icon/electron/icon-1024.png'
  101. });
  102. if (process.platform === 'darwin') {
  103. app.dock.setIcon(app.getAppPath()+'/res/icon/electron/icon-1024.png');
  104. }
  105. // Load the index.html of Sugarizer
  106. mainWindow.loadURL('file://'+app.getAppPath()+'/index.html'+(reinit?'?rst=1':''));
  107. if (frameless) {
  108. mainWindow.maximize();
  109. }
  110. // Wait for 'ready-to-show' to display our window
  111. mainWindow.webContents.once('did-finish-load', function() {
  112. // Initialize locales
  113. l10n.init();
  114. // Handle save file dialog
  115. ipc.on('save-file-dialog', function(event, arg) {
  116. var saveFunction = function(file) {
  117. if (file) {
  118. saveFile(file, arg, event.sender);
  119. }
  120. }
  121. if (!arg.directory) {
  122. // Ask directory to use, then save
  123. var dialogSettings = {
  124. defaultPath: arg.filename,
  125. filters: [
  126. { name: arg.mimetype, extensions: [arg.extension] }
  127. ]
  128. };
  129. dialogSettings.title = l10n.get("SaveFile");
  130. dialogSettings.buttonLabel = l10n.get("Save");
  131. dialog.showSaveDialog(dialogSettings, saveFunction);
  132. } else {
  133. // Save in the directory provided
  134. saveFunction(path.join(arg.directory,arg.filename));
  135. }
  136. });
  137. ipc.on('choose-directory-dialog', function(event) {
  138. var dialogSettings = {
  139. properties: ['openDirectory', 'createDirectory']
  140. };
  141. dialogSettings.title = l10n.get("ChooseDirectory");
  142. dialogSettings.buttonLabel = l10n.get("Choose");
  143. dialog.showOpenDialog(dialogSettings, function(files) {
  144. if (files && files.length > 0) {
  145. event.sender.send('choose-directory-reply', files[0]);
  146. }
  147. });
  148. });
  149. ipc.on('choose-files-dialog', function(event) {
  150. var dialogSettings = {
  151. properties: ['openFile', 'multiSelections'],
  152. filters: [
  153. {name: 'Activities', extensions: ['jpg','png','json','webm','wav','mp3','mp4','pdf','txt','doc','odt']}
  154. ]
  155. };
  156. dialogSettings.title = l10n.get("ChooseFiles");
  157. dialogSettings.buttonLabel = l10n.get("Choose");
  158. dialogSettings.filters[0].name = l10n.get("FilesSupported");
  159. dialog.showOpenDialog(dialogSettings, function(files) {
  160. if (files && files.length > 0) {
  161. for (var i = 0 ; i < files.length ; i++) {
  162. LoadFile(event, files[i]);
  163. }
  164. }
  165. });
  166. });
  167. ipc.on('create-tempfile', function(event, arg) {
  168. temp.file('sugarizer', function(err, path, fd) {
  169. if (!err) {
  170. var data = arg.text.replace(/^data:.+;base64,/, "");
  171. var buf = new Buffer(data, 'base64');
  172. fs.writeFile(fd, buf, function(err) {
  173. event.sender.send('create-tempfile-reply', path);
  174. });
  175. }
  176. });
  177. });
  178. // Build menu
  179. var template = [];
  180. if (process.platform === 'darwin') {
  181. var appname = electron.app.getName();
  182. var menu = {
  183. label: appname,
  184. submenu: [{
  185. accelerator: 'Command+Q',
  186. click: function () {
  187. app.quit()
  188. }
  189. }]
  190. };
  191. menu.submenu[0].label = l10n.get("Quit");
  192. template.unshift(menu);
  193. }
  194. var menu = Menu.buildFromTemplate(template);
  195. Menu.setApplicationMenu(menu);
  196. // Debug console
  197. if (debug) {
  198. mainWindow.webContents.openDevTools();
  199. }
  200. // Show wmain window
  201. mainWindow.show();
  202. });
  203. // Emitted when the window is closed
  204. mainWindow.on('closed', function() {
  205. mainWindow = null;
  206. });
  207. }
  208. // This method will be called when Electron has finished
  209. // initialization and is ready to create browser windows
  210. app.on('ready', createWindow);
  211. // Quit when all windows are closed.
  212. app.on('window-all-closed', function () {
  213. app.quit()
  214. });
  215. app.on('activate', function () {
  216. // On OS X it's common to re-create a window in the app when the
  217. // dock icon is clicked and there are no other windows open.
  218. if (mainWindow === null) {
  219. createWindow();
  220. }
  221. });