Linux: Register .torrent file/magnet link handlers

This commit is contained in:
Feross Aboukhadijeh
2016-03-20 03:19:32 -07:00
parent 5cb0e6940f
commit 9f46da5477
4 changed files with 43 additions and 6 deletions

View File

@@ -15,6 +15,8 @@ module.exports = {
IS_PRODUCTION: isProduction(),
STATIC_PATH: pathToStatic(),
SOUND_ADD: 'file://' + path.join(__dirname, 'static', 'sound', 'add.wav'),
SOUND_DELETE: 'file://' + path.join(__dirname, 'static', 'sound', 'delete.wav'),
SOUND_DISABLE: 'file://' + path.join(__dirname, 'static', 'sound', 'disable.wav'),
@@ -36,7 +38,7 @@ function isProduction () {
return !/\\electron\.exe$/.test(process.execPath)
}
if (process.platform === 'linux') {
// TODO
return !/\/electron$/.test(process.execPath)
}
}

View File

@@ -5,7 +5,7 @@ var app = electron.app
var config = require('../config')
var ipc = require('./ipc')
var menu = require('./menu')
var registerProtocolHandler = require('./register-protocol-handler')
var registerProtocolHandler = require('./register-handlers')
var shortcuts = require('./shortcuts')
var windows = require('./windows')

View File

@@ -2,9 +2,28 @@ var config = require('../config')
module.exports = function () {
if (process.platform === 'win32') {
registerProtocolHandler('magnet', 'URL:BitTorrent Magnet URL', config.APP_FILE_ICON + '.ico', process.execPath)
registerFileHandler('.torrent', 'io.webtorrent.torrent', 'BitTorrent Document', config.APP_FILE_ICON + '.ico', process.execPath)
registerProtocolHandlerWin32('magnet', 'URL:BitTorrent Magnet URL', config.APP_FILE_ICON + '.ico', process.execPath)
registerFileHandlerWin32('.torrent', 'io.webtorrent.torrent', 'BitTorrent Document', config.APP_FILE_ICON + '.ico', process.execPath)
}
if (process.platform === 'linux') {
installDesktopFile()
}
}
function installDesktopFile () {
var fs = require('fs')
var path = require('path')
var os = require('os')
var templatePath = path.join(config.STATIC_PATH, 'webtorrent.desktop')
var desktopFile = fs.readFileSync(templatePath, 'utf8')
desktopFile = desktopFile.replace(/\$APP_NAME/g, config.APP_NAME)
desktopFile = desktopFile.replace(/\$APP_PATH/g, path.dirname(process.execPath))
desktopFile = desktopFile.replace(/\$EXEC_PATH/g, process.execPath)
var desktopFilePath = path.join(os.homedir(), '.local', 'share', 'applications', 'webtorrent.desktop')
fs.writeFileSync(desktopFilePath, desktopFile)
}
/**
@@ -29,7 +48,7 @@ module.exports = function () {
* "HKEY_CLASSES_ROOT" anyway, and can be written by unprivileged users.
*/
function registerProtocolHandler (protocol, name, icon, command) {
function registerProtocolHandlerWin32 (protocol, name, icon, command) {
var Registry = require('winreg')
var protocolKey = new Registry({
@@ -56,7 +75,7 @@ function registerProtocolHandler (protocol, name, icon, command) {
}
}
function registerFileHandler (ext, id, name, icon, command) {
function registerFileHandlerWin32 (ext, id, name, icon, command) {
var Registry = require('winreg')
var extKey = new Registry({

16
static/webtorrent.desktop Normal file
View File

@@ -0,0 +1,16 @@
[Desktop Entry]
Name=$APP_NAME
Version=1.0
GenericName=BitTorrent Client
X-GNOME-FullName=$APP_NAME
Comment=Download and share files over BitTorrent
Encoding=UTF-8
Type=Application
Icon=webtorrent
Terminal=false
Path=$APP_PATH
Exec=$EXEC_PATH %U
TryExec=$EXEC_PATH
StartupNotify=false
Categories=Network;FileTransfer;P2P;
MimeType=application/x-bittorrent;x-scheme-handler/magnet;