1
0
mirror of https://github.com/laurent22/joplin.git synced 2025-11-29 22:48:10 +02:00

Electron: Fixes #201, Fixes #216: Make sure only one update check can run at a time, and improved modal dialog boxes

This commit is contained in:
Laurent Cozic
2018-02-15 23:05:04 +00:00
parent a6e6b49a9d
commit 951be5cbf6
3 changed files with 76 additions and 58 deletions

View File

@@ -305,7 +305,7 @@ class Application extends BaseApplication {
}, { }, {
label: _('Check for updates...'), label: _('Check for updates...'),
click: () => { click: () => {
bridge().checkForUpdates(false, this.checkForUpdateLoggerPath()); bridge().checkForUpdates(false, bridge().window(), this.checkForUpdateLoggerPath());
} }
}, { }, {
label: _('About Joplin'), label: _('About Joplin'),
@@ -422,13 +422,14 @@ class Application extends BaseApplication {
if (shim.isWindows() || shim.isMac()) { if (shim.isWindows() || shim.isMac()) {
const runAutoUpdateCheck = () => { const runAutoUpdateCheck = () => {
if (Setting.value('autoUpdateEnabled')) { if (Setting.value('autoUpdateEnabled')) {
bridge().checkForUpdates(true, this.checkForUpdateLoggerPath()); bridge().checkForUpdates(true, bridge().window(), this.checkForUpdateLoggerPath());
} }
} }
// Initial check on startup
setTimeout(() => { runAutoUpdateCheck() }, 5000); setTimeout(() => { runAutoUpdateCheck() }, 5000);
// For those who leave the app always open // Then every x hours
setInterval(() => { runAutoUpdateCheck() }, 2 * 60 * 60 * 1000); setInterval(() => { runAutoUpdateCheck() }, 12 * 60 * 60 * 1000);
} }
this.updateTray(); this.updateTray();

View File

@@ -43,7 +43,7 @@ class Bridge {
const {dialog} = require('electron'); const {dialog} = require('electron');
if (!options) options = {}; if (!options) options = {};
if (!('defaultPath' in options) && this.lastSelectedPath_) options.defaultPath = this.lastSelectedPath_; if (!('defaultPath' in options) && this.lastSelectedPath_) options.defaultPath = this.lastSelectedPath_;
const filePath = dialog.showSaveDialog(options); const filePath = dialog.showSaveDialog(this.window(), options);
if (filePath) { if (filePath) {
this.lastSelectedPath_ = filePath; this.lastSelectedPath_ = filePath;
} }
@@ -55,7 +55,7 @@ class Bridge {
if (!options) options = {}; if (!options) options = {};
if (!('defaultPath' in options) && this.lastSelectedPath_) options.defaultPath = this.lastSelectedPath_; if (!('defaultPath' in options) && this.lastSelectedPath_) options.defaultPath = this.lastSelectedPath_;
if (!('createDirectory' in options)) options.createDirectory = true; if (!('createDirectory' in options)) options.createDirectory = true;
const filePaths = dialog.showOpenDialog(options); const filePaths = dialog.showOpenDialog(this.window(), options);
if (filePaths && filePaths.length) { if (filePaths && filePaths.length) {
this.lastSelectedPath_ = dirname(filePaths[0]); this.lastSelectedPath_ = dirname(filePaths[0]);
} }
@@ -64,18 +64,18 @@ class Bridge {
showMessageBox(options) { showMessageBox(options) {
const {dialog} = require('electron'); const {dialog} = require('electron');
return dialog.showMessageBox(options); return dialog.showMessageBox(this.window(), options);
} }
showErrorMessageBox(message) { showErrorMessageBox(message) {
return this.showMessageBox({ return this.showMessageBox(this.window(), {
type: 'error', type: 'error',
message: message, message: message,
}); });
} }
showConfirmMessageBox(message) { showConfirmMessageBox(message) {
const result = this.showMessageBox({ const result = this.showMessageBox(this.window(), {
type: 'question', type: 'question',
message: message, message: message,
buttons: [_('OK'), _('Cancel')], buttons: [_('OK'), _('Cancel')],
@@ -84,7 +84,7 @@ class Bridge {
} }
showInfoMessageBox(message) { showInfoMessageBox(message) {
const result = this.showMessageBox({ const result = this.showMessageBox(this.window(), {
type: 'info', type: 'info',
message: message, message: message,
buttons: [_('OK')], buttons: [_('OK')],
@@ -108,26 +108,26 @@ class Bridge {
return require('electron').shell.openItem(fullPath) return require('electron').shell.openItem(fullPath)
} }
async checkForUpdatesAndNotify(logFilePath) { // async checkForUpdatesAndNotify(logFilePath) {
if (!this.autoUpdater_) { // if (!this.autoUpdater_) {
this.autoUpdateLogger_ = new Logger(); // this.autoUpdateLogger_ = new Logger();
this.autoUpdateLogger_.addTarget('file', { path: logFilePath }); // this.autoUpdateLogger_.addTarget('file', { path: logFilePath });
this.autoUpdateLogger_.setLevel(Logger.LEVEL_DEBUG); // this.autoUpdateLogger_.setLevel(Logger.LEVEL_DEBUG);
this.autoUpdateLogger_.info('checkForUpdatesAndNotify: Initializing...'); // this.autoUpdateLogger_.info('checkForUpdatesAndNotify: Initializing...');
this.autoUpdater_ = require("electron-updater").autoUpdater; // this.autoUpdater_ = require("electron-updater").autoUpdater;
this.autoUpdater_.logger = this.autoUpdateLogger_; // this.autoUpdater_.logger = this.autoUpdateLogger_;
} // }
try { // try {
await this.autoUpdater_.checkForUpdatesAndNotify(); // await this.autoUpdater_.checkForUpdatesAndNotify();
} catch (error) { // } catch (error) {
this.autoUpdateLogger_.error(error); // this.autoUpdateLogger_.error(error);
} // }
} // }
checkForUpdates(inBackground, logFilePath) { checkForUpdates(inBackground, window, logFilePath) {
const { checkForUpdates } = require('./checkForUpdates.js'); const { checkForUpdates } = require('./checkForUpdates.js');
checkForUpdates(inBackground, logFilePath); checkForUpdates(inBackground, window, logFilePath);
} }
} }

View File

@@ -5,18 +5,14 @@ const { _ } = require('lib/locale.js');
let autoUpdateLogger_ = new Logger(); let autoUpdateLogger_ = new Logger();
let checkInBackground_ = false; let checkInBackground_ = false;
let isCheckingForUpdate_ = false;
let parentWindow_ = null;
// Note: Electron Builder's autoUpdater is incredibly buggy so currently it's only used // 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 // to detect if a new version is present. If it is, the download link is simply opened
// in a new browser window. // in a new browser window.
autoUpdater.autoDownload = false; 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) { function htmlToText_(html) {
let output = html.replace(/\n/g, ''); let output = html.replace(/\n/g, '');
output = output.replace(/<li>/g, '- '); output = output.replace(/<li>/g, '- ');
@@ -28,11 +24,35 @@ function htmlToText_(html) {
return output; 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) => { autoUpdater.on('update-available', (info) => {
if (!info.version || !info.path) { if (!info.version || !info.path) {
if (checkInBackground_) return; if (checkInBackground_) return onCheckEnded();
dialog.showErrorBox(_('Error'), ('Could not get version info: ' + JSON.stringify(info))); showErrorMessageBox(('Could not get version info: ' + JSON.stringify(info)));
return; return onCheckEnded();
} }
const downloadUrl = 'https://github.com/laurent22/joplin/releases/download/v' + info.version + '/' + info.path; const downloadUrl = 'https://github.com/laurent22/joplin/releases/download/v' + info.version + '/' + info.path;
@@ -40,37 +60,33 @@ autoUpdater.on('update-available', (info) => {
let releaseNotes = info.releaseNotes + ''; let releaseNotes = info.releaseNotes + '';
if (releaseNotes) releaseNotes = '\n\n' + _('Release notes:\n\n%s', htmlToText_(releaseNotes)); if (releaseNotes) releaseNotes = '\n\n' + _('Release notes:\n\n%s', htmlToText_(releaseNotes));
dialog.showMessageBox({ const buttonIndex = dialog.showMessageBox(parentWindow_, {
type: 'info', type: 'info',
message: _('An update is available, do you want to download it now?' + releaseNotes), message: _('An update is available, do you want to download it now?' + releaseNotes),
buttons: [_('Yes'), _('No')] 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', () => { autoUpdater.on('update-not-available', () => {
if (checkInBackground_) return; if (checkInBackground_) return onCheckEnded();
dialog.showMessageBox({ message: _('Current version is up-to-date.') }) dialog.showMessageBox({ message: _('Current version is up-to-date.') })
onCheckEnded();
}) })
// autoUpdater.on('update-downloaded', () => { function checkForUpdates(inBackground, window, logFilePath) {
// dialog.showMessageBox({ message: _('New version downloaded - application will quit now and update...') }, () => { if (isCheckingForUpdate_) {
// setTimeout(() => { autoUpdateLogger_.info('checkForUpdates: Skipping check because it is already running');
// try { return;
// autoUpdater.quitAndInstall(); }
// } catch (error) {
// autoUpdateLogger_.error(error); parentWindow_ = window;
// dialog.showErrorBox(_('Error'), _('Could not install the update: %s', error.message));
// } onCheckStarted();
// }, 100);
// })
// })
function checkForUpdates(inBackground, logFilePath) {
if (logFilePath && !autoUpdateLogger_.targets().length) { if (logFilePath && !autoUpdateLogger_.targets().length) {
autoUpdateLogger_ = new Logger(); autoUpdateLogger_ = new Logger();
autoUpdateLogger_.addTarget('file', { path: logFilePath }); autoUpdateLogger_.addTarget('file', { path: logFilePath });
@@ -85,7 +101,8 @@ function checkForUpdates(inBackground, logFilePath) {
autoUpdater.checkForUpdates() autoUpdater.checkForUpdates()
} catch (error) { } catch (error) {
autoUpdateLogger_.error(error); autoUpdateLogger_.error(error);
if (!checkInBackground_) dialog.showErrorBox(_('Error'), error.message); if (!checkInBackground_) showErrorMessageBox(error.message);
onCheckEnded();
} }
} }