Merge pull request #238 from VishwasShashidhar/electron-205

Electron 205 - Allow user to set Downloads folder through Menu dropdown
This commit is contained in:
Vikas Shashidhar 2017-11-23 20:29:25 +05:30 committed by GitHub
commit 59658c819c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 51 additions and 3 deletions

View File

@ -65,7 +65,23 @@ const template = [{
}
},
{
label: 'Open Crashes Directory',
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]);
});
}
},
{
label: 'Open Crashes Directory',
click() {
const crashesDirectory = electron.crashReporter.getCrashesDirectory() + '/completed';
electron.shell.showItemInFolder(crashesDirectory);

View File

@ -1,5 +1,6 @@
'use strict';
const fs = require('fs');
const electron = require('electron');
const app = electron.app;
const crashReporter = electron.crashReporter;
@ -36,6 +37,7 @@ let alwaysOnTop = false;
let position = 'lower-right';
let display;
let sandboxed = false;
let downloadsDirectory;
// note: this file is built using browserify in prebuild step.
const preloadMainScript = path.join(__dirname, 'preload/_preloadMain.js');
@ -236,12 +238,37 @@ function doCreateMainWindow(initialUrl, initialBounds) {
}
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;
// if the directory has been deleted, try creating it.
if (!fs.existsSync(downloadsDirectory)) {
const directoryCreated = fs.mkdirSync(downloadsDirectory);
// If the directory creation failed, we use the default downloads directory
if (!directoryCreated) {
downloadsDirectory = null;
}
}
})
.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');
// 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());
}
// Send file path when download is complete
item.once('done', (e, state) => {
if (state === 'completed') {
@ -605,6 +632,11 @@ 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;