From c13bfd5f14bbee538f03aca76606437c8cb3064b Mon Sep 17 00:00:00 2001 From: Vishwas Shashidhar Date: Fri, 29 Dec 2017 12:48:19 +0530 Subject: [PATCH] electron-205: fixes the already fixed issue post merge conflict regression --- js/downloadManager/index.js | 2 +- js/windowMgr.js | 132 +++++++++++++++++------------------- 2 files changed, 65 insertions(+), 69 deletions(-) diff --git a/js/downloadManager/index.js b/js/downloadManager/index.js index 5ef5ed9e..3f1fbba5 100644 --- a/js/downloadManager/index.js +++ b/js/downloadManager/index.js @@ -56,7 +56,7 @@ function showInFinder(id) { function createDOM(arg) { if (arg && arg._id) { - + let fileDisplayName = arg.fileName; let downloadItemKey = arg._id; diff --git a/js/windowMgr.js b/js/windowMgr.js index d756c179..ff1e144a 100644 --- a/js/windowMgr.js +++ b/js/windowMgr.js @@ -145,7 +145,7 @@ function doCreateMainWindow(initialUrl, initialBounds) { } // will set the main window on top as per the user prefs - if (alwaysOnTop){ + if (alwaysOnTop) { newWinOpts.alwaysOnTop = alwaysOnTop; } @@ -179,7 +179,7 @@ function doCreateMainWindow(initialUrl, initialBounds) { loadErrors.showNetworkConnectivityError(mainWindow, url, retry); } else { // updates the notify config with user preference - notify.updateConfig({position: position, display: display}); + notify.updateConfig({ position: position, display: display }); // removes all existing notifications when main window reloads notify.reset(); log.send(logLevels.INFO, 'loaded main window url: ' + url); @@ -188,7 +188,7 @@ function doCreateMainWindow(initialUrl, initialBounds) { }); mainWindow.webContents.on('did-fail-load', function (event, errorCode, - errorDesc, validatedURL) { + errorDesc, validatedURL) { loadErrors.showLoadFailure(mainWindow, validatedURL, errorDesc, errorCode, retry, false); }); @@ -218,7 +218,7 @@ function doCreateMainWindow(initialUrl, initialBounds) { const menu = electron.Menu.buildFromTemplate(getTemplate(app)); electron.Menu.setApplicationMenu(menu); - mainWindow.on('close', function(e) { + mainWindow.on('close', function (e) { if (willQuitApp) { destroyAllWindows(); return; @@ -259,7 +259,7 @@ function doCreateMainWindow(initialUrl, initialBounds) { // When download is in progress, send necessary data to indicate the same webContents.send('downloadProgress'); - + // An extra check to see if the user created downloads directory has been deleted // This scenario can occur when user doesn't quit electron and continues using it // across days and then deletes the folder. @@ -267,21 +267,17 @@ function doCreateMainWindow(initialUrl, initialBounds) { downloadsDirectory = defaultDownloadsDirectory; updateConfigField("downloadsDirectory", downloadsDirectory); } - + // We check the downloads directory to see if a file with the similar name // already exists and get a unique filename if that's the case let newFileName = getUniqueFileName(item.getFilename()); - item.setSavePath(downloadsDirectory + "/" + newFileName); - - // Send file path to construct the DOM in the UI when the download is complete - - // if the user has set a custom downloads directory, save file to that directory - // if otherwise, we save it to the operating system's default downloads directory - if (downloadsDirectory) { - item.setSavePath(downloadsDirectory + "/" + item.getFilename()); + if (isMac) { + item.setSavePath(downloadsDirectory + "/" + newFileName); + } else { + item.setSavePath(downloadsDirectory + "\\" + newFileName); } - // Send file path when download is complete + // Send file path to construct the DOM in the UI when the download is complete item.once('done', (e, state) => { if (state === 'completed') { let data = { @@ -296,25 +292,25 @@ function doCreateMainWindow(initialUrl, initialBounds) { }); getConfigField('url') - .then(initializeCrashReporter) - .catch(app.quit); - - function initializeCrashReporter(podUrl) { + .then(initializeCrashReporter) + .catch(app.quit); + + function initializeCrashReporter(podUrl) { getConfigField('crashReporter') - .then((crashReporterConfig) => { - log.send(logLevels.INFO, 'Initializing crash reporter on the main window!'); - crashReporter.start({companyName: crashReporterConfig.companyName, submitURL: crashReporterConfig.submitURL, uploadToServer: crashReporterConfig.uploadToServer, extra: {'process': 'renderer / main window', podUrl: podUrl}}); - log.send(logLevels.INFO, 'initialized crash reporter on the main window!'); - mainWindow.webContents.send('register-crash-reporter', {companyName: crashReporterConfig.companyName, submitURL: crashReporterConfig.submitURL, uploadToServer: crashReporterConfig.uploadToServer, process: 'preload script / main window renderer'}); - }) - .catch((err) => { - log.send(logLevels.ERROR, 'Unable to initialize crash reporter in the main window. Error is -> ' + err); - }); - } + .then((crashReporterConfig) => { + log.send(logLevels.INFO, 'Initializing crash reporter on the main window!'); + crashReporter.start({ companyName: crashReporterConfig.companyName, submitURL: crashReporterConfig.submitURL, uploadToServer: crashReporterConfig.uploadToServer, extra: { 'process': 'renderer / main window', podUrl: podUrl } }); + log.send(logLevels.INFO, 'initialized crash reporter on the main window!'); + mainWindow.webContents.send('register-crash-reporter', { companyName: crashReporterConfig.companyName, submitURL: crashReporterConfig.submitURL, uploadToServer: crashReporterConfig.uploadToServer, process: 'preload script / main window renderer' }); + }) + .catch((err) => { + log.send(logLevels.ERROR, 'Unable to initialize crash reporter in the main window. Error is -> ' + err); + }); + } // open external links in default browser - a tag with href='_blank' or window.open mainWindow.webContents.on('new-window', function (event, newWinUrl, - frameName, disposition, newWinOptions) { + frameName, disposition, newWinOptions) { let newWinParsedUrl = getParsedUrl(newWinUrl); let mainWinParsedUrl = getParsedUrl(url); @@ -346,7 +342,7 @@ function doCreateMainWindow(initialUrl, initialBounds) { let newX = Number.parseInt(query.x, 10); let newY = Number.parseInt(query.y, 10); - let newWinRect = {x: newX, y: newY, width, height}; + let newWinRect = { x: newX, y: newY, width, height }; // only accept if both are successfully parsed. if (Number.isInteger(newX) && Number.isInteger(newY) && @@ -359,7 +355,7 @@ function doCreateMainWindow(initialUrl, initialBounds) { } } else { // create new window at slight offset from main window. - ({x, y} = getWindowSizeAndPosition(mainWindow)); + ({ x, y } = getWindowSizeAndPosition(mainWindow)); x += 50; y += 50; } @@ -393,18 +389,18 @@ function doCreateMainWindow(initialUrl, initialBounds) { } getConfigField('url') - .then((podUrl) => { - getConfigField('crashReporter') - .then((crashReporterConfig) => { - crashReporter.start({companyName: crashReporterConfig.companyName, submitURL: crashReporterConfig.submitURL, uploadToServer: crashReporterConfig.uploadToServer, extra: {'process': 'renderer / child window', podUrl: podUrl}}); - log.send(logLevels.INFO, 'initialized crash reporter on a child window!'); - browserWin.webContents.send('register-crash-reporter', {companyName: crashReporterConfig.companyName, submitURL: crashReporterConfig.submitURL, uploadToServer: crashReporterConfig.uploadToServer, process: 'preload script / child window renderer'}); + .then((podUrl) => { + getConfigField('crashReporter') + .then((crashReporterConfig) => { + crashReporter.start({ companyName: crashReporterConfig.companyName, submitURL: crashReporterConfig.submitURL, uploadToServer: crashReporterConfig.uploadToServer, extra: { 'process': 'renderer / child window', podUrl: podUrl } }); + log.send(logLevels.INFO, 'initialized crash reporter on a child window!'); + browserWin.webContents.send('register-crash-reporter', { companyName: crashReporterConfig.companyName, submitURL: crashReporterConfig.submitURL, uploadToServer: crashReporterConfig.uploadToServer, process: 'preload script / child window renderer' }); + }) + .catch((err) => { + log.send(logLevels.ERROR, 'Unable to initialize crash reporter in the child window. Error is -> ' + err); + }); }) - .catch((err) => { - log.send(logLevels.ERROR, 'Unable to initialize crash reporter in the child window. Error is -> ' + err); - }); - }) - .catch(app.quit); + .catch(app.quit); browserWin.winName = frameName; browserWin.setAlwaysOnTop(alwaysOnTop); @@ -412,7 +408,7 @@ function doCreateMainWindow(initialUrl, initialBounds) { let handleChildWindowClosed = () => { removeWindowKey(newWinKey); browserWin.removeListener('move', throttledBoundsChange); - browserWin.removeListener('resize', throttledBoundsChange); + browserWin.removeListener('resize', throttledBoundsChange); }; browserWin.once('closed', () => { @@ -448,11 +444,11 @@ function doCreateMainWindow(initialUrl, initialBounds) { childEvent.preventDefault(); openUrlInDefaultBrowser(childWinUrl); }; - + // In case we navigate to an external link from inside a pop-out, // we open that link in an external browser rather than creating // a new window - browserWin.webContents.on('new-window', handleChildNewWindowEvent); + browserWin.webContents.on('new-window', handleChildNewWindowEvent); addWindowKey(newWinKey, browserWin); @@ -465,7 +461,7 @@ function doCreateMainWindow(initialUrl, initialBounds) { let throttledBoundsChange = throttle(1000, sendChildWinBoundsChange.bind(null, browserWin)); browserWin.on('move', throttledBoundsChange); - browserWin.on('resize', throttledBoundsChange); + browserWin.on('resize', throttledBoundsChange); } }); } else { @@ -475,14 +471,14 @@ function doCreateMainWindow(initialUrl, initialBounds) { }); // whenever the main window is navigated for ex: window.location.href or url redirect - mainWindow.webContents.on('will-navigate', function(event, navigatedURL) { + mainWindow.webContents.on('will-navigate', function (event, navigatedURL) { deleteIndexFolder(); isWhitelisted(navigatedURL) .catch(() => { event.preventDefault(); electron.dialog.showMessageBox(mainWindow, { type: 'warning', - buttons: [ 'Ok' ], + buttons: ['Ok'], title: 'Not Allowed', message: `Sorry, you are not allowed to access this website (${navigatedURL}), please contact your administrator for more details`, }); @@ -628,7 +624,7 @@ function sendChildWinBoundsChange(window) { * @param urlToOpen */ function openUrlInDefaultBrowser(urlToOpen) { - if (urlToOpen) { + if (urlToOpen) { electron.shell.openExternal(urlToOpen); } } @@ -677,7 +673,7 @@ eventEmitter.on('notificationSettings', (notificationSettings) => { function verifyDisplays() { // This is only for Windows, macOS handles this by itself - if (!mainWindow || isMac){ + if (!mainWindow || isMac) { return; } @@ -687,14 +683,14 @@ function verifyDisplays() { let isYAxisValid = true; // checks to make sure the x,y are valid pairs - if ((bounds.x === undefined && (bounds.y || bounds.y === 0))){ + if ((bounds.x === undefined && (bounds.y || bounds.y === 0))) { isXAxisValid = false; } - if ((bounds.y === undefined && (bounds.x || bounds.x === 0))){ + if ((bounds.y === undefined && (bounds.x || bounds.x === 0))) { isYAxisValid = false; } - if (!isXAxisValid && !isYAxisValid){ + if (!isXAxisValid && !isYAxisValid) { return; } @@ -725,7 +721,7 @@ function checkExternalDisplay(appBounds) { // Loops through all the available displays and // verifies if the wrapper exists within the display bounds // returns false if not exists otherwise true - return !!screen.getAllDisplays().find(({bounds}) => { + return !!screen.getAllDisplays().find(({ bounds }) => { const leftMost = x + (width * factor); const topMost = y + (height * factor); @@ -748,7 +744,7 @@ function checkExternalDisplay(appBounds) { function repositionMainWindow() { const screen = electron.screen; - const {workArea} = screen.getPrimaryDisplay(); + const { workArea } = screen.getPrimaryDisplay(); const bounds = workArea; if (!bounds) { @@ -765,10 +761,10 @@ function repositionMainWindow() { const x = Math.round(centerX - (windowWidth / 2.0)); const y = Math.round(centerY - (windowHeight / 2.0)); - let rectangle = {x, y, width: windowWidth, height: windowHeight}; + let rectangle = { x, y, width: windowWidth, height: windowHeight }; // resetting the main window bounds - if (mainWindow){ + if (mainWindow) { if (!mainWindow.isVisible()) { mainWindow.show(); } @@ -790,33 +786,33 @@ function repositionMainWindow() { * @returns {String} the new filename */ function getUniqueFileName(filename) { - + // By default, we assume that the file exists const fileExists = true; - + // We break the file from it's extension to get the name const actualFilename = filename.substr(0, filename.lastIndexOf('.')) || filename; const fileType = filename.split('.').pop(); - + // We use this to set the new file name with an increment on the previous existing file let fileNumber = 0; let newPath; - + while (fileExists) { - + let fileNameString = fileNumber.toString(); - + // By default, we know if the file doesn't exist, // we can use the filename sent by the remote server let current = filename; - + // If the file already exists, we know that the // file number variable is increased, so, // we construct a new file name with the file number if (fileNumber > 0) { current = actualFilename + " (" + fileNameString + ")." + fileType; } - + // If the file exists, increment the file number and repeat the loop if (fs.existsSync(downloadsDirectory + "/" + current)) { fileNumber++; @@ -824,9 +820,9 @@ function getUniqueFileName(filename) { newPath = current; break; } - + } - + return newPath; }