This forum has been archived. All content is frozen. Please use KDE Discuss instead.

Konqueror - associciate magnet: links with a shell script?

Tags: None
(comma "," separated)
DrTwox
Registered Member
Posts
19
Karma
0
Hi all,

I have a shell script, ~/bin/magnet.sh, that forwards a magnet: link to a remote server running the Transmission bittorrent daemon. When I click a magnet: link in Konqueror, I'd like it to automatically call this shell script with the magnet:/ link as an argument. How can I achieve this?

Thanks.

Update 1: Whoops, forgot to mention I'm in Kubuntu 12.04, if that matters!

Update 2: I've found this http://lists.debian.org/debian-kde/2.../msg00048.html describing how to add a new protocol. I've copied /usr/share/kde4/services/magnet.protcol to ~/.kde/share/kde4/services/, and the file looks like this:

[Protocol]
exec=kio_magnet
protocol=magnet
input=none
output=filesystem
copyToFile=false
copyFromFile=false
listing=Name,Type,Size,
reading=true
writing=false
makedir=false
deleting=false
moving=false
Icon=folder-remote
#maxInstancesPerHost=2
maxInstances=10
#X-DocPath=kioslave/magnet/index.html
Class=:internet

How do I modify it to pass the clicked link to ~/bin/magnet.sh?
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Protocols in KDE are handled by special processes referred to as KIO Slaves. In this case, they cannot be a simple shell script.
However, you can setup a URI scheme handler - which will do what you want.

You do this by associating the application with the x-scheme-handler/<protocol> mimetype.
To do this, simply create a *.desktop file (with any name you like) with the following contents:
Code: Select all
Type=Application
Exec=<path to your script> %u
Icon=<an appropriate icon name goes here>
MimeType=x-scheme-handler/<protocol>;
NoDisplay=true
Name=Anything goes here


In this case - your protocol is "magnet". I would advise setting the icon to be the same one used by KTorrent.

Ensure it is placed at ~/.local/share/applications/ and then run "kbuildsycoca4".


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
DrTwox
Registered Member
Posts
19
Karma
0
I have followed that guide and restarted Konqueror, however magnet links still try to open with kTorrent.... here's the .desktop file I made:

$ cat ~/.local/share/applications/transmission-get.sh.desktop
[Desktop Entry]
Type=Application
Exec=/usr/local/bin/transmission-get.sh %u
Icon=
MimeType=x-scheme-handler/magnet;
NoDisplay=true
Name=transmission-get.sh

(I changed the name of the shell script since the original posting)
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
It is likely KTorrent does a similar thing and is picked first due to various reasons.
I would suggest uninstalling KTorrent in this case, or at the very least finding the name of the *.desktop file KTorrent uses and creating a file with the same name and contents in ~/.local/share/applicatons/ - just without x-scheme-handler/magnet included (which should override it hopefully).


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
DrTwox
Registered Member
Posts
19
Karma
0
Thanks for you help with this!

I removed kTorrent, now when clicking a magnet link I get this error:

The requested operation could not be completed
Cannot Initiate the magnet Protocol
Technical Reason: Unable to Launch Process
Details of the Request:
URL: magnet:? xt=urn:btih: < Huge link too long to post >
Protocol: magnet
Date and Time: Thursday 27 September 2012 08:42
Additional Information: Unable to create io-slave: klauncher said: Error loading 'kio_magnet'.
Description:
The program on your computer which provides access to the magnet protocol could not be started. This is usually due to technical reasons.
Possible Causes:
The program which provides compatibility with this protocol may not have been updated with your last update of KDE. This can cause the program to be incompatible with the current version and thus not start.
You may have encountered a bug in the program.
Possible Solutions:
Update your software to the latest version. Your distribution should provide tools to update your software.
Contact your appropriate computer support system, whether the system administrator, or technical support group for further assistance.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
If you have created a magnet.protocol file anywhere, please remove it.
Afterwards, run kbuildsycoca4 again.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
DrTwox
Registered Member
Posts
19
Karma
0
There was a file called ~/.kde/share/kde4/services/magnet.protocol, which I removed, and run kbuildsycoca4 again. The error when clicking a magnet link is now:

The requested operation could not be completed
Cannot Initiate the magnet Protocol
Technical Reason: Unable to Launch Process

Details of the Request:
URL: magnet:?xt=urn:btih: <removed>
Protocol: magnet
Date and Time: Thursday 27 September 2012 09:18
Additional Information: Unable to create io-slave: klauncher said: Unknown protocol 'magnet'.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Please move ~/.local/share/applications/transmission-get.sh.desktop to ~/.kde4/share/kde4/services/ and ensure the .sh isn't in the filename to ensure it is not interfering.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
DrTwox
Registered Member
Posts
19
Karma
0
$ mv ~/.local/share/applications/transmission-get.sh.desktop ~/.kde/share/kde4/services/transmission-get.desktop
$ kbuildsycoca4
kbuildsycoca4 running...
kbuildsycoca4(17611)/kdecore (services) KServicePrivate::init: The desktop entry file "transmission-get.desktop" has Type= "Application" but is located under " "services" " instead of "apps"
kbuildsycoca4(17611) KBuildServiceFactory::createEntry: Invalid Service : "transmission-get.desktop"

I really appreciate your help with this!
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
I have just realised a rather crucial line is missing from the file :(
Please add at the top of it:
Code: Select all
[Desktop Entry]

I note it seems to be there in your one, but please do double check, as that is why kbuildsycoca4 prints that message.
Please make sure an icon name is set as well.

Also, move it back to ~/.local/share/applications/
Then run kbuildsycoca4.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
DrTwox
Registered Member
Posts
19
Karma
0
I moved it back - here's what it looks like:
$ cat ~/.local/share/applications/transmission-get.desktop
[Desktop Entry]
Type=Application
Exec=/usr/local/bin/transmission-get.sh %u
Icon=kget
MimeType=x-scheme-handler/magnet;
NoDisplay=true
Name=transmission-get.sh

kbuildsycoca4 runs without errors again.

However the error when clicking on a magnet link is the same:

The requested operation could not be completed
Cannot Initiate the magnet Protocol
Technical Reason: Unable to Launch Process

Details of the Request:
URL: magnet:?xt=urn:btih:< removed >
Protocol: magnet
Date and Time: Thursday 27 September 2012 15:39
Additional Information: Unable to create io-slave: klauncher said: Error loading '/usr/local/bin/transmission-get.sh %u'.

The script is definitely in that location and is executable. I can call it from a terminal and it works as expected.

I also found [url="http://lists.fedoraproject.org/pipermail/kde/2012-March/011284.html"]this recent thread[/url] that discusses a similar situation.
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Based on that output - it would seem that either the magnet.protocol file still exists somewhere on your system - or the existence is cached.

To ensure it is not cached, please logout and back in again.
Afterwards, scan ~/.kde4, ~/.local, ~/.config and /usr for any magnet.protocol files.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
DrTwox
Registered Member
Posts
19
Karma
0
I rebooted the machine and tried again. A dialog pops up:
This untrusted page links to magnet:?xt=< removed >
Do you want to follow the link?
[ Follow ][ Cancel ]

I click Follow but the error is the same error as before:
URL: magnet:?xt=< removed >
Protocol: magnet
Date and Time: Sunday 30 September 2012 09:53
Additional Information: Unable to create io-slave: klauncher said: Error loading '/usr/local/bin/transmission-get.sh "%u"'.

I checked the directories you mentioned for any file with "magnet" in the name. There was some icon files (png, svg), some files related to the kernal in /usr/src/linux-headers-X.X.XX and these:

/usr/share/locale-langpack/en_AU/LC_MESSAGES/kio_magnet.mo
/usr/share/locale-langpack/en_GB/LC_MESSAGES/kio_magnet.mo
/usr/share/kde4/config.kcfg/magnetsettings.kcfg

The first two are translation files, so probably not relevant? The last file looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<kcfg xmlns="http://www.kde.org/standards/kcfg/1.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://www.kde.org/standards/kcfg/1.0
http://www.kde.org/standards/kcfg/1.0/kcfg.xsd" >
<kcfgfile name="kio_magnetrc"/>
<include>kglobalsettings.h</include>
<group name="Magnet">
<entry name="Enabled" type="Bool">
<label>Enable the global magnet-link support.</label>
<default>true</default>
</entry>
<entry name="ShareSize" type="ULongLong">
<label>Total maximum size used by the share in MiB.</label>
<default>5120</default>
</entry>
<entry name="TrustedHosts" type="StringList">
<label>List of hosts which are directly accepted on loading</label>
</entry>
<entry name="ManagedTorrents" type="StringList">
<label>Torrents inside KTorrent which are managed by this modules.</label>
</entry>
<entry name="RunningTorrents" type="StringList">
<label>Torrents currently used by the I/O-Slaves.</label>
</entry>
<entry name="MaxShareRatio" type="Double">
<label>The maximum share ratio which should be targeted for each torrent.</label>
<default>2.0</default>
</entry>
</group>
</kcfg>
User avatar
bcooksley
Administrator
Posts
19765
Karma
87
OS
Is the following file executable?
/usr/local/bin/transmission-get.sh

And please make sure to logout and back in again after making the sweep for magnet.protocol files... you should be getting a different error message if it was failing to launch the script.


KDE Sysadmin
[img]content/bcooksley_sig.png[/img]
DrTwox
Registered Member
Posts
19
Karma
0
Yes, that script is executable. I run it frequently from a terminal and it works.

$ ls -l /usr/local/bin/transmission-get.sh
-rwx------ 1 drtwox drtwox 1010 Sep 27 16:40 /usr/local/bin/transmission-get.sh

I have logged out and rebooted several times over the last few days. Same error each time.


Bookmarks



Who is online

Registered users: Bing [Bot], Google [Bot], Yahoo [Bot]