mirror of
https://github.com/finos/SymphonyElectron.git
synced 2025-02-25 18:55:29 -06:00
Electron 446: Disable setting custom downloads directory (#349)
- remove feature to set custom downloads directory - fix formatting - fix formatting
This commit is contained in:
parent
4d15f03492
commit
c1e4016e93
@ -56,15 +56,13 @@ function showInFinder(id) {
|
|||||||
function createDOM(arg) {
|
function createDOM(arg) {
|
||||||
|
|
||||||
if (arg && arg._id) {
|
if (arg && arg._id) {
|
||||||
|
let fileDisplayName = getFileDisplayName(arg.fileName);
|
||||||
let fileDisplayName = arg.fileName;
|
|
||||||
let downloadItemKey = arg._id;
|
let downloadItemKey = arg._id;
|
||||||
|
|
||||||
local.downloadItems.push(arg);
|
local.downloadItems.push(arg);
|
||||||
|
|
||||||
let ul = document.getElementById('download-main');
|
let ul = document.getElementById('download-main');
|
||||||
if (ul) {
|
if (ul) {
|
||||||
|
|
||||||
let li = document.createElement('li');
|
let li = document.createElement('li');
|
||||||
li.id = downloadItemKey;
|
li.id = downloadItemKey;
|
||||||
li.classList.add('download-element');
|
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;
|
||||||
}
|
}
|
@ -83,22 +83,6 @@ const template = [{
|
|||||||
focusedWindow.reload();
|
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' },
|
{ type: 'separator' },
|
||||||
buildMenuItem('resetzoom'),
|
buildMenuItem('resetzoom'),
|
||||||
|
@ -43,10 +43,6 @@ let display;
|
|||||||
let sandboxed = false;
|
let sandboxed = false;
|
||||||
let isAutoReload = false;
|
let isAutoReload = false;
|
||||||
|
|
||||||
// By default, we set the user's default download directory
|
|
||||||
let defaultDownloadsDirectory = app.getPath("downloads");
|
|
||||||
let downloadsDirectory = defaultDownloadsDirectory;
|
|
||||||
|
|
||||||
// Application menu
|
// Application menu
|
||||||
let menu;
|
let menu;
|
||||||
|
|
||||||
@ -288,48 +284,20 @@ function doCreateMainWindow(initialUrl, initialBounds, isCustomTitleBar) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
mainWindow.on('closed', destroyAllWindows);
|
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
|
// Manage File Downloads
|
||||||
mainWindow.webContents.session.on('will-download', (event, item, webContents) => {
|
mainWindow.webContents.session.on('will-download', (event, item, webContents) => {
|
||||||
|
|
||||||
// When download is in progress, send necessary data to indicate the same
|
// When download is in progress, send necessary data to indicate the same
|
||||||
webContents.send('downloadProgress');
|
webContents.send('downloadProgress');
|
||||||
|
|
||||||
// An extra check to see if the user created downloads directory has been deleted
|
// Send file path when download is complete
|
||||||
// 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
|
|
||||||
item.once('done', (e, state) => {
|
item.once('done', (e, state) => {
|
||||||
if (state === 'completed') {
|
if (state === 'completed') {
|
||||||
let data = {
|
let data = {
|
||||||
_id: getGuid(),
|
_id: getGuid(),
|
||||||
savedPath: item.getSavePath() ? item.getSavePath() : '',
|
savedPath: item.getSavePath() ? item.getSavePath() : '',
|
||||||
total: filesize(item.getTotalBytes() ? item.getTotalBytes() : 0),
|
total: filesize(item.getTotalBytes() ? item.getTotalBytes() : 0),
|
||||||
fileName: newFileName
|
fileName: item.getFilename() ? item.getFilename() : 'No name'
|
||||||
};
|
};
|
||||||
webContents.send('downloadCompleted', data);
|
webContents.send('downloadCompleted', data);
|
||||||
}
|
}
|
||||||
@ -803,11 +771,6 @@ eventEmitter.on('isAlwaysOnTop', (boolean) => {
|
|||||||
isAlwaysOnTop(boolean);
|
isAlwaysOnTop(boolean);
|
||||||
});
|
});
|
||||||
|
|
||||||
// set downloads directory
|
|
||||||
eventEmitter.on('setDownloadsDirectory', (newDirectory) => {
|
|
||||||
downloadsDirectory = newDirectory;
|
|
||||||
});
|
|
||||||
|
|
||||||
// node event emitter for notification settings
|
// node event emitter for notification settings
|
||||||
eventEmitter.on('notificationSettings', (notificationSettings) => {
|
eventEmitter.on('notificationSettings', (notificationSettings) => {
|
||||||
position = notificationSettings.position;
|
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 = {
|
module.exports = {
|
||||||
createMainWindow: createMainWindow,
|
createMainWindow: createMainWindow,
|
||||||
getMainWindow: getMainWindow,
|
getMainWindow: getMainWindow,
|
||||||
|
@ -138,9 +138,9 @@ describe('Tests for Always on top', () => {
|
|||||||
robot.setMouseDelay(200);
|
robot.setMouseDelay(200);
|
||||||
robot.moveMouse(190, 0);
|
robot.moveMouse(190, 0);
|
||||||
robot.mouseClick();
|
robot.mouseClick();
|
||||||
// Key tap 8 times as "Always on Top" is in the
|
// Key tap 7 times as "Always on Top" is in the
|
||||||
// 8th position under view menu item
|
// 7th position under view menu item
|
||||||
for (let i = 0; i < 8; i++) {
|
for (let i = 0; i < 7; i++) {
|
||||||
robot.keyTap('down');
|
robot.keyTap('down');
|
||||||
}
|
}
|
||||||
robot.keyTap('enter');
|
robot.keyTap('enter');
|
||||||
|
@ -105,9 +105,9 @@ describe('Tests for Full screen', () => {
|
|||||||
robot.mouseClick();
|
robot.mouseClick();
|
||||||
robot.setKeyboardDelay(100);
|
robot.setKeyboardDelay(100);
|
||||||
|
|
||||||
// Key tap 6 times as "Enter Full Screen" is in the
|
// Key tap 5 times as "Enter Full Screen" is in the
|
||||||
// 6th position under view menu item
|
// 5th position under view menu item
|
||||||
for (let i = 0; i < 6; i++) {
|
for (let i = 0; i < 5; i++) {
|
||||||
robot.keyTap('down');
|
robot.keyTap('down');
|
||||||
}
|
}
|
||||||
robot.keyTap('enter');
|
robot.keyTap('enter');
|
||||||
|
@ -119,9 +119,9 @@ describe('Tests for Minimize on Close', () => {
|
|||||||
robot.mouseClick();
|
robot.mouseClick();
|
||||||
robot.setKeyboardDelay(100);
|
robot.setKeyboardDelay(100);
|
||||||
|
|
||||||
// Key tap 9 times as "Minimize on Close" is in the
|
// Key tap 8 times as "Minimize on Close" is in the
|
||||||
// 9th position under view menu item
|
// 8th position under view menu item
|
||||||
for (let i = 0; i < 9; i++) {
|
for (let i = 0; i < 8; i++) {
|
||||||
robot.keyTap('down');
|
robot.keyTap('down');
|
||||||
}
|
}
|
||||||
robot.keyTap('enter');
|
robot.keyTap('enter');
|
||||||
|
Loading…
Reference in New Issue
Block a user