From a879492529ff338a992f5c2b02024ee823d5327a Mon Sep 17 00:00:00 2001 From: Feross Aboukhadijeh Date: Mon, 4 Apr 2016 20:27:48 -0700 Subject: [PATCH] Uninstall handlers on Windows uninstall/npm run clean --- bin/clean.js | 7 ++- main/handlers.js | 118 ++++++++++++++++++++++++++++++++++++++--- main/index.js | 2 +- main/squirrel-win32.js | 6 +++ package.json | 4 +- 5 files changed, 125 insertions(+), 12 deletions(-) diff --git a/bin/clean.js b/bin/clean.js index 13319717..e3d3f37c 100755 --- a/bin/clean.js +++ b/bin/clean.js @@ -5,13 +5,18 @@ * Useful for developers. */ -var config = require('../config') var os = require('os') var path = require('path') var pathExists = require('path-exists') var rimraf = require('rimraf') +var config = require('../config') +var handlers = require('../main/handlers') + rimraf.sync(config.CONFIG_PATH) var tmpPath = path.join(pathExists.sync('/tmp') ? '/tmp' : os.tmpDir(), 'webtorrent') rimraf.sync(tmpPath) + +// Uninstall .torrent file and magnet link handlers +handlers.uninstall() diff --git a/main/handlers.js b/main/handlers.js index 1228911f..605e5b31 100644 --- a/main/handlers.js +++ b/main/handlers.js @@ -1,24 +1,37 @@ module.exports = { - init + install, + uninstall } var path = require('path') var log = require('./log') -function init () { +function install () { if (process.platform === 'darwin') { - initDarwin() + installDarwin() } if (process.platform === 'win32') { - initWin32() + installWin32() } if (process.platform === 'linux') { - initLinux() + installLinux() } } -function initDarwin () { +function uninstall () { + if (process.platform === 'darwin') { + uninstallDarwin() + } + if (process.platform === 'win32') { + uninstallWin32() + } + if (process.platform === 'linux') { + uninstallLinux() + } +} + +function installDarwin () { var electron = require('electron') var app = electron.app @@ -29,7 +42,9 @@ function initDarwin () { // File handlers are registered in the Info.plist. } -function initWin32 () { +function uninstallDarwin () {} + +function installWin32 () { var Registry = require('winreg') var iconPath = path.join(process.resourcesPath, 'app.asar.unpacked', 'static', 'WebTorrentFile.ico') @@ -163,7 +178,70 @@ function initWin32 () { } } -function initLinux () { +function uninstallWin32 () { + var Registry = require('winreg') + + unregisterProtocolHandlerWin32('magnet', process.execPath) + unregisterFileHandlerWin32('.torrent', 'io.webtorrent.torrent', process.execPath) + + function unregisterProtocolHandlerWin32 (protocol, command) { + getCommand() + + function getCommand () { + var commandKey = new Registry({ + hive: Registry.HKCU, // HKEY_CURRENT_USER + key: '\\Software\\Classes\\' + protocol + '\\shell\\open\\command' + }) + commandKey.get('', function (err, item) { + if (!err && item.value.indexOf(command) >= 0) { + eraseProtocol() + } + }) + } + + function eraseProtocol () { + var protocolKey = new Registry({ + hive: Registry.HKCU, + key: '\\Software\\Classes\\' + protocol + }) + protocolKey.erase(function () {}) + } + } + + function unregisterFileHandlerWin32 (ext, id, command) { + eraseId() + + function eraseId () { + var idKey = new Registry({ + hive: Registry.HKCU, // HKEY_CURRENT_USER + key: '\\Software\\Classes\\' + id + }) + idKey.erase(getExt) + } + + function getExt () { + var extKey = new Registry({ + hive: Registry.HKCU, + key: '\\Software\\Classes\\' + ext + }) + extKey.get('', function (err, item) { + if (!err && item.value === id) { + eraseExt() + } + }) + } + + function eraseExt () { + var extKey = new Registry({ + hive: Registry.HKCU, // HKEY_CURRENT_USER + key: '\\Software\\Classes\\' + ext + }) + extKey.erase(function () {}) + } + } +} + +function installLinux () { var config = require('../config') var fs = require('fs') var mkdirp = require('mkdirp') @@ -224,3 +302,27 @@ function initLinux () { }) } } + +function uninstallLinux () { + var os = require('os') + var path = require('path') + var rimraf = require('rimraf') + + var desktopFilePath = path.join( + os.homedir(), + '.local', + 'share', + 'applications', + 'webtorrent-desktop.desktop' + ) + rimraf.sync(desktopFilePath) + + var iconFilePath = path.join( + os.homedir(), + '.local', + 'share', + 'icons', + 'webtorrent-desktop.png' + ) + rimraf.sync(iconFilePath) +} diff --git a/main/index.js b/main/index.js index cd84e07c..bbc81a1b 100644 --- a/main/index.js +++ b/main/index.js @@ -55,7 +55,7 @@ function init () { windows.createMainWindow() shortcuts.init() tray.init() - handlers.init() + handlers.install() }) app.on('ipcReady', function () { diff --git a/main/squirrel-win32.js b/main/squirrel-win32.js index 011cfd5c..7ae1c781 100644 --- a/main/squirrel-win32.js +++ b/main/squirrel-win32.js @@ -11,6 +11,8 @@ var pathExists = require('path-exists') var app = electron.app +var handlers = require('./handlers') + var exeName = path.basename(process.execPath) var updateDotExe = path.join(process.execPath, '..', '..', 'Update.exe') @@ -41,6 +43,10 @@ function handleEvent (cmd) { removeShortcuts(function () { app.quit() }) + + // Uninstall .torrent file and magnet link handlers + handlers.uninstall() + return true } diff --git a/package.json b/package.json index 59fc9c93..857ca4e7 100644 --- a/package.json +++ b/package.json @@ -29,11 +29,12 @@ "network-address": "^1.1.0", "path-exists": "^2.1.0", "prettier-bytes": "^1.0.1", + "rimraf": "^2.5.2", "simple-get": "^2.0.0", "upload-element": "^1.0.1", "virtual-dom": "^2.1.1", "webtorrent": "^0.90.0", - "winreg": "^1.0.1" + "winreg": "feross/node-winreg" }, "devDependencies": { "electron-osx-sign": "^0.3.0", @@ -42,7 +43,6 @@ "gh-release": "^2.0.3", "nobin-debian-installer": "^0.0.6", "plist": "^1.2.0", - "rimraf": "^2.5.2", "standard": "^6.0.5" }, "homepage": "https://webtorrent.io",