Skip to content

Commit

Permalink
Electron: Fixes #201, Fixes #216: Make sure only one update check can…
Browse files Browse the repository at this point in the history
… run at a time, and improved modal dialog boxes
  • Loading branch information
laurent22 committed Feb 15, 2018
1 parent a6e6b49 commit 951be5c
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 61 deletions.
9 changes: 5 additions & 4 deletions ElectronClient/app/app.js
Original file line number Diff line number Diff line change
Expand Up @@ -305,7 +305,7 @@ class Application extends BaseApplication {
}, {
label: _('Check for updates...'),
click: () => {
bridge().checkForUpdates(false, this.checkForUpdateLoggerPath());
bridge().checkForUpdates(false, bridge().window(), this.checkForUpdateLoggerPath());
}
}, {
label: _('About Joplin'),
Expand Down Expand Up @@ -422,13 +422,14 @@ class Application extends BaseApplication {
if (shim.isWindows() || shim.isMac()) {
const runAutoUpdateCheck = () => {
if (Setting.value('autoUpdateEnabled')) {
bridge().checkForUpdates(true, this.checkForUpdateLoggerPath());
bridge().checkForUpdates(true, bridge().window(), this.checkForUpdateLoggerPath());
}
}

// Initial check on startup
setTimeout(() => { runAutoUpdateCheck() }, 5000);
// For those who leave the app always open
setInterval(() => { runAutoUpdateCheck() }, 2 * 60 * 60 * 1000);
// Then every x hours
setInterval(() => { runAutoUpdateCheck() }, 12 * 60 * 60 * 1000);
}

this.updateTray();
Expand Down
50 changes: 25 additions & 25 deletions ElectronClient/app/bridge.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class Bridge {
const {dialog} = require('electron');
if (!options) options = {};
if (!('defaultPath' in options) && this.lastSelectedPath_) options.defaultPath = this.lastSelectedPath_;
const filePath = dialog.showSaveDialog(options);
const filePath = dialog.showSaveDialog(this.window(), options);
if (filePath) {
this.lastSelectedPath_ = filePath;
}
Expand All @@ -55,7 +55,7 @@ class Bridge {
if (!options) options = {};
if (!('defaultPath' in options) && this.lastSelectedPath_) options.defaultPath = this.lastSelectedPath_;
if (!('createDirectory' in options)) options.createDirectory = true;
const filePaths = dialog.showOpenDialog(options);
const filePaths = dialog.showOpenDialog(this.window(), options);
if (filePaths && filePaths.length) {
this.lastSelectedPath_ = dirname(filePaths[0]);
}
Expand All @@ -64,18 +64,18 @@ class Bridge {

showMessageBox(options) {
const {dialog} = require('electron');
return dialog.showMessageBox(options);
return dialog.showMessageBox(this.window(), options);
}

showErrorMessageBox(message) {
return this.showMessageBox({
return this.showMessageBox(this.window(), {
type: 'error',
message: message,
});
}

showConfirmMessageBox(message) {
const result = this.showMessageBox({
const result = this.showMessageBox(this.window(), {
type: 'question',
message: message,
buttons: [_('OK'), _('Cancel')],
Expand All @@ -84,7 +84,7 @@ class Bridge {
}

showInfoMessageBox(message) {
const result = this.showMessageBox({
const result = this.showMessageBox(this.window(), {
type: 'info',
message: message,
buttons: [_('OK')],
Expand All @@ -108,26 +108,26 @@ class Bridge {
return require('electron').shell.openItem(fullPath)
}

async checkForUpdatesAndNotify(logFilePath) {
if (!this.autoUpdater_) {
this.autoUpdateLogger_ = new Logger();
this.autoUpdateLogger_.addTarget('file', { path: logFilePath });
this.autoUpdateLogger_.setLevel(Logger.LEVEL_DEBUG);
this.autoUpdateLogger_.info('checkForUpdatesAndNotify: Initializing...');
this.autoUpdater_ = require("electron-updater").autoUpdater;
this.autoUpdater_.logger = this.autoUpdateLogger_;
}

try {
await this.autoUpdater_.checkForUpdatesAndNotify();
} catch (error) {
this.autoUpdateLogger_.error(error);
}
}

checkForUpdates(inBackground, logFilePath) {
// async checkForUpdatesAndNotify(logFilePath) {
// if (!this.autoUpdater_) {
// this.autoUpdateLogger_ = new Logger();
// this.autoUpdateLogger_.addTarget('file', { path: logFilePath });
// this.autoUpdateLogger_.setLevel(Logger.LEVEL_DEBUG);
// this.autoUpdateLogger_.info('checkForUpdatesAndNotify: Initializing...');
// this.autoUpdater_ = require("electron-updater").autoUpdater;
// this.autoUpdater_.logger = this.autoUpdateLogger_;
// }

// try {
// await this.autoUpdater_.checkForUpdatesAndNotify();
// } catch (error) {
// this.autoUpdateLogger_.error(error);
// }
// }

checkForUpdates(inBackground, window, logFilePath) {
const { checkForUpdates } = require('./checkForUpdates.js');
checkForUpdates(inBackground, logFilePath);
checkForUpdates(inBackground, window, logFilePath);
}

}
Expand Down
81 changes: 49 additions & 32 deletions ElectronClient/app/checkForUpdates.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,14 @@ const { _ } = require('lib/locale.js');

let autoUpdateLogger_ = new Logger();
let checkInBackground_ = false;
let isCheckingForUpdate_ = false;
let parentWindow_ = null;

// Note: Electron Builder's autoUpdater is incredibly buggy so currently it's only used
// to detect if a new version is present. If it is, the download link is simply opened
// in a new browser window.
autoUpdater.autoDownload = false;

autoUpdater.on('error', (error) => {
autoUpdateLogger_.error(error);
if (checkInBackground_) return;
dialog.showErrorBox(_('Error'), error == null ? "unknown" : (error.stack || error).toString())
})

function htmlToText_(html) {
let output = html.replace(/\n/g, '');
output = output.replace(/<li>/g, '- ');
Expand All @@ -28,49 +24,69 @@ function htmlToText_(html) {
return output;
}

function showErrorMessageBox(message) {
return dialog.showMessageBox(parentWindow_, {
type: 'error',
message: message,
});
}

function onCheckStarted() {
autoUpdateLogger_.info('checkForUpdates: Starting...');
isCheckingForUpdate_ = true;
}

function onCheckEnded() {
autoUpdateLogger_.info('checkForUpdates: Done.');
isCheckingForUpdate_ = false;
}

autoUpdater.on('error', (error) => {
autoUpdateLogger_.error(error);
if (checkInBackground_) return onCheckEnded();
showErrorMessageBox(error == null ? "unknown" : (error.stack || error).toString())
onCheckEnded();
})

autoUpdater.on('update-available', (info) => {
if (!info.version || !info.path) {
if (checkInBackground_) return;
dialog.showErrorBox(_('Error'), ('Could not get version info: ' + JSON.stringify(info)));
return;
if (checkInBackground_) return onCheckEnded();
showErrorMessageBox(('Could not get version info: ' + JSON.stringify(info)));
return onCheckEnded();
}

const downloadUrl = 'https://github.com/laurent22/joplin/releases/download/v' + info.version + '/' + info.path;

let releaseNotes = info.releaseNotes + '';
if (releaseNotes) releaseNotes = '\n\n' + _('Release notes:\n\n%s', htmlToText_(releaseNotes));

dialog.showMessageBox({
const buttonIndex = dialog.showMessageBox(parentWindow_, {
type: 'info',
message: _('An update is available, do you want to download it now?' + releaseNotes),
buttons: [_('Yes'), _('No')]
}, (buttonIndex) => {
if (buttonIndex === 0) {
require('electron').shell.openExternal(downloadUrl);
}
})
});

onCheckEnded();

if (buttonIndex === 0) require('electron').shell.openExternal(downloadUrl);
})

autoUpdater.on('update-not-available', () => {
if (checkInBackground_) return;

if (checkInBackground_) return onCheckEnded();
dialog.showMessageBox({ message: _('Current version is up-to-date.') })
onCheckEnded();
})

// autoUpdater.on('update-downloaded', () => {
// dialog.showMessageBox({ message: _('New version downloaded - application will quit now and update...') }, () => {
// setTimeout(() => {
// try {
// autoUpdater.quitAndInstall();
// } catch (error) {
// autoUpdateLogger_.error(error);
// dialog.showErrorBox(_('Error'), _('Could not install the update: %s', error.message));
// }
// }, 100);
// })
// })

function checkForUpdates(inBackground, logFilePath) {
function checkForUpdates(inBackground, window, logFilePath) {
if (isCheckingForUpdate_) {
autoUpdateLogger_.info('checkForUpdates: Skipping check because it is already running');
return;
}

parentWindow_ = window;

onCheckStarted();

if (logFilePath && !autoUpdateLogger_.targets().length) {
autoUpdateLogger_ = new Logger();
autoUpdateLogger_.addTarget('file', { path: logFilePath });
Expand All @@ -85,7 +101,8 @@ function checkForUpdates(inBackground, logFilePath) {
autoUpdater.checkForUpdates()
} catch (error) {
autoUpdateLogger_.error(error);
if (!checkInBackground_) dialog.showErrorBox(_('Error'), error.message);
if (!checkInBackground_) showErrorMessageBox(error.message);
onCheckEnded();
}
}

Expand Down

0 comments on commit 951be5c

Please sign in to comment.