From c1e4016e93d06663f7c8bc237956b76ea2918219 Mon Sep 17 00:00:00 2001 From: Vishwas Shashidhar Date: Tue, 1 May 2018 13:35:57 +0530 Subject: [PATCH] Electron 446: Disable setting custom downloads directory (#349) - remove feature to set custom downloads directory - fix formatting - fix formatting --- js/downloadManager/index.js | 33 ++++++- js/menus/menuTemplate.js | 16 ---- js/windowMgr.js | 92 +------------------- tests/spectron/alwaysOnTop.spectron.js | 6 +- tests/spectron/full-screen.spectron.js | 6 +- tests/spectron/minimize-on-close.spectron.js | 6 +- 6 files changed, 43 insertions(+), 116 deletions(-) diff --git a/js/downloadManager/index.js b/js/downloadManager/index.js index 3f1fbba5..0187110d 100644 --- a/js/downloadManager/index.js +++ b/js/downloadManager/index.js @@ -56,15 +56,13 @@ function showInFinder(id) { function createDOM(arg) { if (arg && arg._id) { - - let fileDisplayName = arg.fileName; + let fileDisplayName = getFileDisplayName(arg.fileName); let downloadItemKey = arg._id; local.downloadItems.push(arg); let ul = document.getElementById('download-main'); if (ul) { - let li = document.createElement('li'); li.id = downloadItemKey; li.classList.add('download-element'); @@ -197,4 +195,33 @@ function initiate() { }); } } +} + +/** + * Return a file display name for the download item + */ +function getFileDisplayName(fileName) { + let fileList = local.downloadItems; + let fileNameCount = 0; + let fileDisplayName = fileName; + + /* Check if a file with the same name exists + * (akin to the user downloading a file with the same name again) + * in the download bar + */ + for (let i = 0; i < fileList.length; i++) { + if (fileName === fileList[i].fileName) { + fileNameCount++; + } + } + + /* If it exists, add a count to the name like how Chrome does */ + if (fileNameCount) { + let extLastIndex = fileDisplayName.lastIndexOf('.'); + let fileCount = ' (' + fileNameCount + ')'; + + fileDisplayName = fileDisplayName.slice(0, extLastIndex) + fileCount + fileDisplayName.slice(extLastIndex); + } + + return fileDisplayName; } \ No newline at end of file diff --git a/js/menus/menuTemplate.js b/js/menus/menuTemplate.js index f0e72b14..75065375 100644 --- a/js/menus/menuTemplate.js +++ b/js/menus/menuTemplate.js @@ -83,22 +83,6 @@ const template = [{ focusedWindow.reload(); } } - }, - { - label: 'Set Downloads Directory', - click() { - electron.dialog.showOpenDialog({ - title: 'Select Downloads Directory', - buttonLabel: 'Select', - properties: ['openDirectory', 'createDirectory'] - }, (filePaths) => { - if (!filePaths || !Array.isArray(filePaths) || filePaths.length < 1) { - return; - } - updateConfigField('downloadsDirectory', filePaths[0]); - eventEmitter.emit('setDownloadsDirectory', filePaths[0]); - }); - } }, { type: 'separator' }, buildMenuItem('resetzoom'), diff --git a/js/windowMgr.js b/js/windowMgr.js index 93a381fc..4e0de33d 100644 --- a/js/windowMgr.js +++ b/js/windowMgr.js @@ -43,10 +43,6 @@ let display; let sandboxed = false; let isAutoReload = false; -// By default, we set the user's default download directory -let defaultDownloadsDirectory = app.getPath("downloads"); -let downloadsDirectory = defaultDownloadsDirectory; - // Application menu let menu; @@ -288,48 +284,20 @@ function doCreateMainWindow(initialUrl, initialBounds, isCustomTitleBar) { } mainWindow.on('closed', destroyAllWindows); - - // if an user has set a custom downloads directory, - // we get that data from the user config file - getConfigField('downloadsDirectory') - .then((value) => { - downloadsDirectory = value; - }) - .catch((error) => { - log.send(logLevels.ERROR, 'Could not find the downloads directory config -> ' + error); - }); - + // Manage File Downloads mainWindow.webContents.session.on('will-download', (event, item, webContents) => { - // 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. - if (downloadsDirectory !== defaultDownloadsDirectory && !fs.existsSync(downloadsDirectory)) { - 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()); - if (isMac) { - item.setSavePath(downloadsDirectory + "/" + newFileName); - } else { - item.setSavePath(downloadsDirectory + "\\" + newFileName); - } - - // Send file path to construct the DOM in the UI when the download is complete + + // Send file path when download is complete item.once('done', (e, state) => { if (state === 'completed') { let data = { _id: getGuid(), savedPath: item.getSavePath() ? item.getSavePath() : '', total: filesize(item.getTotalBytes() ? item.getTotalBytes() : 0), - fileName: newFileName + fileName: item.getFilename() ? item.getFilename() : 'No name' }; webContents.send('downloadCompleted', data); } @@ -803,11 +771,6 @@ eventEmitter.on('isAlwaysOnTop', (boolean) => { isAlwaysOnTop(boolean); }); -// set downloads directory -eventEmitter.on('setDownloadsDirectory', (newDirectory) => { - downloadsDirectory = newDirectory; -}); - // node event emitter for notification settings eventEmitter.on('notificationSettings', (notificationSettings) => { position = notificationSettings.position; @@ -925,53 +888,6 @@ function repositionMainWindow() { } } -/** - * Creates a unique filename like Chrome - * from a user's download directory - * @param filename filename passed by the remote server - * @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++; - } else { - newPath = current; - break; - } - - } - - return newPath; -} - module.exports = { createMainWindow: createMainWindow, getMainWindow: getMainWindow, diff --git a/tests/spectron/alwaysOnTop.spectron.js b/tests/spectron/alwaysOnTop.spectron.js index 6d7e698f..47ef4dcd 100644 --- a/tests/spectron/alwaysOnTop.spectron.js +++ b/tests/spectron/alwaysOnTop.spectron.js @@ -138,9 +138,9 @@ describe('Tests for Always on top', () => { robot.setMouseDelay(200); robot.moveMouse(190, 0); robot.mouseClick(); - // Key tap 8 times as "Always on Top" is in the - // 8th position under view menu item - for (let i = 0; i < 8; i++) { + // Key tap 7 times as "Always on Top" is in the + // 7th position under view menu item + for (let i = 0; i < 7; i++) { robot.keyTap('down'); } robot.keyTap('enter'); diff --git a/tests/spectron/full-screen.spectron.js b/tests/spectron/full-screen.spectron.js index 24c9f0a0..c959e152 100644 --- a/tests/spectron/full-screen.spectron.js +++ b/tests/spectron/full-screen.spectron.js @@ -105,9 +105,9 @@ describe('Tests for Full screen', () => { robot.mouseClick(); robot.setKeyboardDelay(100); - // Key tap 6 times as "Enter Full Screen" is in the - // 6th position under view menu item - for (let i = 0; i < 6; i++) { + // Key tap 5 times as "Enter Full Screen" is in the + // 5th position under view menu item + for (let i = 0; i < 5; i++) { robot.keyTap('down'); } robot.keyTap('enter'); diff --git a/tests/spectron/minimize-on-close.spectron.js b/tests/spectron/minimize-on-close.spectron.js index 49fd6d05..8d0d9fd5 100644 --- a/tests/spectron/minimize-on-close.spectron.js +++ b/tests/spectron/minimize-on-close.spectron.js @@ -119,9 +119,9 @@ describe('Tests for Minimize on Close', () => { robot.mouseClick(); robot.setKeyboardDelay(100); - // Key tap 9 times as "Minimize on Close" is in the - // 9th position under view menu item - for (let i = 0; i < 9; i++) { + // Key tap 8 times as "Minimize on Close" is in the + // 8th position under view menu item + for (let i = 0; i < 8; i++) { robot.keyTap('down'); } robot.keyTap('enter');