[GTKUI] Autofill infohash entry from clipboard

* Create new common.is_infohash func and test.
This commit is contained in:
Calum Lind 2016-10-14 11:49:31 +01:00
parent b4787235b5
commit 9788ca08ea
3 changed files with 27 additions and 4 deletions

View File

@ -560,6 +560,20 @@ def is_url(url):
return url.partition('://')[0] in ("http", "https", "ftp", "udp")
def is_infohash(infohash):
"""
A check to determine if a string is a valid infohash.
Args:
infohash (str): The string to check.
Returns:
bool: True if valid infohash, False otherwise.
"""
return len(infohash) == 40 and infohash.isalnum()
def is_magnet(uri):
"""
A check to determine if a uri is a valid bittorrent magnet uri
@ -616,7 +630,7 @@ def get_magnet_info(uri):
except TypeError as ex:
log.debug("Invalid base32 magnet hash: %s, %s", xt_hash, ex)
break
elif len(xt_hash) == 40:
elif is_infohash(xt_hash):
info_hash = xt_hash.lower()
else:
break

View File

@ -2,8 +2,8 @@ import os
from twisted.trial import unittest
from deluge.common import (VersionSplit, fdate, fpcnt, fpeer, fsize, fspeed, ftime, get_path_size, is_ip, is_magnet,
is_url)
from deluge.common import (VersionSplit, fdate, fpcnt, fpeer, fsize, fspeed, ftime, get_path_size, is_infohash, is_ip,
is_magnet, is_url)
from deluge.ui.util import lang
@ -48,6 +48,9 @@ class CommonTestCase(unittest.TestCase):
def test_is_magnet(self):
self.failUnless(is_magnet("magnet:?xt=urn:btih:SU5225URMTUEQLDXQWRB2EQWN6KLTYKN"))
def test_is_infohash(self):
self.failUnless(is_infohash("2dc5d0e71a66fe69649a640d39cb00a259704973"))
def test_get_path_size(self):
self.failUnless(get_path_size(os.devnull) == 0)
self.failUnless(get_path_size("non-existant.file") == -1)

View File

@ -663,10 +663,16 @@ class AddTorrentDialog(component.Component):
dialog.set_default_response(gtk.RESPONSE_OK)
dialog.set_transient_for(self.dialog)
entry.grab_focus()
text = (gtk.clipboard_get(selection='PRIMARY').wait_for_text() or
gtk.clipboard_get().wait_for_text()).strip()
if deluge.common.is_infohash(text):
entry.set_text(text)
dialog.show_all()
response = dialog.run()
infohash = entry.get_text().strip()
if response == gtk.RESPONSE_OK and len(infohash) == 40:
if response == gtk.RESPONSE_OK and deluge.common.is_infohash(infohash):
trackers = []
b = textview.get_buffer()
lines = b.get_text(b.get_start_iter(), b.get_end_iter()).strip().split("\n")