From 8b773c5f592f0b0355a847952ab0b6dd0d7e58f8 Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Sat, 28 May 2016 18:25:25 -0700 Subject: [PATCH] Document and cleanup announcement/dialog/handlers.js --- main/announcement.js | 21 ++++++++++++++++++--- main/dialog.js | 36 +++++++++++++++++++++++------------- main/handlers.js | 33 +++++++++++++++++++++++---------- 3 files changed, 64 insertions(+), 26 deletions(-) diff --git a/main/announcement.js b/main/announcement.js index b6977533..275bc0de 100644 --- a/main/announcement.js +++ b/main/announcement.js @@ -11,21 +11,36 @@ var ANNOUNCEMENT_URL = config.ANNOUNCEMENT_URL + '?version=' + config.APP_VERSION + '&platform=' + process.platform +/** + * In certain situations, the WebTorrent team may need to show an announcement to + * all WebTorrent Desktop users. For example: a security notice, or an update + * notification (if the auto-updater stops working). + * + * When there is an announcement, the `ANNOUNCEMENT_URL` endpoint should return an + * HTTP 200 status code with a JSON object like this: + * + * { + * "title": "WebTorrent Desktop Announcement", + * "message": "Security Issue in v0.xx", + * "detail": "Please update to v0.xx as soon as possible..." + * } + */ function init () { var get = require('simple-get') get.concat(ANNOUNCEMENT_URL, onResponse) } function onResponse (err, res, data) { - if (err) return log('failed to retrieve remote message') - if (res.statusCode !== 200) return log('no remote message') + if (err) return log(`Failed to retrieve announcement: ${err.message}`) + if (res.statusCode !== 200) return log('No announcement exists') try { data = JSON.parse(data.toString()) } catch (err) { + // Support plaintext announcement messages, using a default title. data = { title: 'WebTorrent Desktop Announcement', - message: 'Announcement', + message: data.toString(), detail: data.toString() } } diff --git a/main/dialog.js b/main/dialog.js index 1aaab35f..585dc68f 100644 --- a/main/dialog.js +++ b/main/dialog.js @@ -8,38 +8,46 @@ module.exports = { var electron = require('electron') var windows = require('./windows') -// Prompts the user for a file, then creates a torrent. Only allows a single file -// selection. +/** + * Show open dialog to create a single-file torrent. + */ function openSeedFile () { - electron.dialog.showOpenDialog({ + var opts = { title: 'Select a file for the torrent file.', properties: [ 'openFile' ] - }, function (selectedPaths) { + } + electron.dialog.showOpenDialog(opts, function (selectedPaths) { if (!Array.isArray(selectedPaths)) return windows.main.send('dispatch', 'showCreateTorrent', selectedPaths) }) } -// Prompts the user for a file or directory, then creates a torrent. Only allows a -// single selection. To create a multi-file torrent, the user must select a -// directory. +/* + * Show open dialog to create a single-file or single-directory torrent. On + * Windows and Linux, open dialogs are for files *or* directories only, not both. + * This function shows a directory dialog. + */ function openSeedDirectory () { - electron.dialog.showOpenDialog({ + var opts = { title: 'Select a file or folder for the torrent file.', properties: [ 'openFile', 'openDirectory' ] - }, function (selectedPaths) { + } + electron.dialog.showOpenDialog(opts, function (selectedPaths) { if (!Array.isArray(selectedPaths)) return windows.main.send('dispatch', 'showCreateTorrent', selectedPaths) }) } -// Prompts the user to choose a torrent file, then adds it. +/* + * Show open dialog to open a .torrent file. + */ function openTorrentFile () { - electron.dialog.showOpenDialog(windows.main.win, { + var opts = { title: 'Select a .torrent file to open.', filters: [{ name: 'Torrent Files', extensions: ['torrent'] }], properties: [ 'openFile', 'multiSelections' ] - }, function (selectedPaths) { + } + electron.dialog.showOpenDialog(windows.main.win, opts, function (selectedPaths) { if (!Array.isArray(selectedPaths)) return selectedPaths.forEach(function (selectedPath) { windows.main.send('dispatch', 'addTorrent', selectedPath) @@ -47,7 +55,9 @@ function openTorrentFile () { }) } -// Prompts the user for the URL of a torrent file, then downloads and adds it +/* + * Show modal dialog to open a torrent URL (magnet uri, http torrent link, etc.) + */ function openTorrentAddress () { windows.main.send('showOpenTorrentAddress') } diff --git a/main/handlers.js b/main/handlers.js index a9aab18a..33da88fe 100644 --- a/main/handlers.js +++ b/main/handlers.js @@ -3,9 +3,8 @@ module.exports = { uninstall } -var path = require('path') - var config = require('../config') +var path = require('path') function install () { if (process.platform === 'darwin') { @@ -35,11 +34,11 @@ function installDarwin () { var electron = require('electron') var app = electron.app - // On OS X, only protocols that are listed in Info.plist can be set as the default - // handler at runtime. + // On OS X, only protocols that are listed in `Info.plist` can be set as the + // default handler at runtime. app.setAsDefaultProtocolClient('magnet') - // File handlers are registered in the Info.plist. + // File handlers are defined in `Info.plist`. } function uninstallDarwin () {} @@ -55,10 +54,22 @@ function installWin32 () { var log = require('./log') - var iconPath = path.join(process.resourcesPath, 'app.asar.unpacked', 'static', 'WebTorrentFile.ico') - - registerProtocolHandlerWin32('magnet', 'URL:BitTorrent Magnet URL', iconPath, EXEC_COMMAND) - registerFileHandlerWin32('.torrent', 'io.webtorrent.torrent', 'BitTorrent Document', iconPath, EXEC_COMMAND) + var iconPath = path.join( + process.resourcesPath, 'app.asar.unpacked', 'static', 'WebTorrentFile.ico' + ) + registerProtocolHandlerWin32( + 'magnet', + 'URL:BitTorrent Magnet URL', + iconPath, + EXEC_COMMAND + ) + registerFileHandlerWin32( + '.torrent', + 'io.webtorrent.torrent', + 'BitTorrent Document', + iconPath, + EXEC_COMMAND + ) /** * To add a protocol handler, the following keys must be added to the Windows registry: @@ -265,7 +276,9 @@ function installLinux () { installIconFile() function installDesktopFile () { - var templatePath = path.join(config.STATIC_PATH, 'linux', 'webtorrent-desktop.desktop') + var templatePath = path.join( + config.STATIC_PATH, 'linux', 'webtorrent-desktop.desktop' + ) fs.readFile(templatePath, 'utf8', writeDesktopFile) }