4
def as_command(url_or_command):
5
if url_or_command.startswith("http://"):
6
return "gnome-open \"" + url_or_command + "\""
12
def __init__(self, prefix, description, url_or_command, image_name=None):
14
self.needs_whitespace_afterwards = (len(prefix) > 0) and (prefix[-1].isalnum())
15
if image_name == None:
17
self.image = deskbar.load_image(image_name)
19
# if the arg is optional, then url_or_command has the format
20
# "no arg command|with arg command with %s", and description
21
# is similarly two-part, split by the "|" character
22
self.description = description
23
bar = description.find("|")
25
self.with_arg_description = description
26
self.sans_arg_description = None
28
self.with_arg_description = description[bar+1:]
29
self.sans_arg_description = description[:bar]
31
self.url_or_command = url_or_command
32
bar = url_or_command.find("|")
34
self.with_arg_command = as_command(url_or_command)
35
self.sans_arg_command = None
37
self.with_arg_command = as_command(url_or_command[bar+1:])
38
self.sans_arg_command = as_command(url_or_command[:bar])
39
if self.sans_arg_description == None:
40
self.sans_arg_description = self.with_arg_description
44
def add_to_completions(self, text, completions, handler_icon, check_can_handle=True):
45
if (not check_can_handle) or self.can_handle(text):
47
text = self.strip_prefix(text)
50
d = self.sans_arg_description
52
d = self.with_arg_description.replace("%s", "<b>" + deskbar.escape_markup(text) + "</b>")
54
if handler_icon == None:
55
handler_icon = self.image
56
d = "<i>" + d + "</i>"
59
if not check_can_handle:
60
name = deskbar.NO_CHECK_CAN_HANDLE + name
62
completions.append([d, name, text, self.image.get_pixbuf()])
67
return "Handler_Prefix_" + self.prefix
70
def strip_prefix(self, text):
71
# strip the prefix, and then any leading/trailing whitespace
72
return text[len(self.prefix):].strip()
75
def can_handle(self, text, check_can_handle=True):
76
if text == self.prefix:
77
return self.sans_arg_command != None
79
if self.needs_whitespace_afterwards:
80
return text.startswith(self.prefix + " ")
82
return text.startswith(self.prefix)
85
def handle(self, text, check_can_handle=True):
86
if check_can_handle and text.startswith(self.prefix):
87
text = self.strip_prefix(text)
90
deskbar.run_command(self.sans_arg_command, text)
92
deskbar.run_command(self.with_arg_command, text)