~ssh-rdp/deskbar-applet/history-count

« back to all changes in this revision

Viewing changes to src/handler_prefix.py

  • Committer: rslinckx
  • Date: 2005-09-13 16:36:46 UTC
  • Revision ID: vcs-imports@canonical.com-20050913163646-gx8uamx1ea5564up
Initial revision

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import deskbar
 
2
 
 
3
 
 
4
def as_command(url_or_command):
 
5
        if url_or_command.startswith("http://"):
 
6
                return "gnome-open \"" + url_or_command + "\""
 
7
        else:
 
8
                return url_or_command
 
9
 
 
10
 
 
11
class Handler:
 
12
        def __init__(self, prefix, description, url_or_command, image_name=None):
 
13
                self.prefix = prefix
 
14
                self.needs_whitespace_afterwards = (len(prefix) > 0) and (prefix[-1].isalnum())
 
15
                if image_name == None:
 
16
                        image_name = prefix
 
17
                self.image = deskbar.load_image(image_name)
 
18
                
 
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("|")
 
24
                if bar == -1:
 
25
                        self.with_arg_description = description
 
26
                        self.sans_arg_description = None
 
27
                else:
 
28
                        self.with_arg_description = description[bar+1:]
 
29
                        self.sans_arg_description = description[:bar]
 
30
                
 
31
                self.url_or_command = url_or_command
 
32
                bar = url_or_command.find("|")
 
33
                if bar == -1:
 
34
                        self.with_arg_command = as_command(url_or_command)
 
35
                        self.sans_arg_command = None
 
36
                else:
 
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
 
41
                
 
42
 
 
43
 
 
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):
 
46
                        if check_can_handle:
 
47
                                text = self.strip_prefix(text)
 
48
 
 
49
                        if len(text) == 0:
 
50
                                d = self.sans_arg_description
 
51
                        else:
 
52
                                d = self.with_arg_description.replace("%s", "<b>" + deskbar.escape_markup(text) + "</b>")
 
53
 
 
54
                        if handler_icon == None:
 
55
                                handler_icon = self.image
 
56
                                d = "<i>" + d + "</i>"
 
57
                        
 
58
                        name = self.name()
 
59
                        if not check_can_handle:
 
60
                                name = deskbar.NO_CHECK_CAN_HANDLE + name
 
61
                        
 
62
                        completions.append([d, name, text, self.image.get_pixbuf()])
 
63
                return handler_icon
 
64
 
 
65
 
 
66
        def name(self):
 
67
                return "Handler_Prefix_" + self.prefix
 
68
 
 
69
 
 
70
        def strip_prefix(self, text):
 
71
                # strip the prefix, and then any leading/trailing whitespace
 
72
                return text[len(self.prefix):].strip()
 
73
 
 
74
 
 
75
        def can_handle(self, text, check_can_handle=True):
 
76
                if text == self.prefix:
 
77
                        return self.sans_arg_command != None
 
78
 
 
79
                if self.needs_whitespace_afterwards:
 
80
                        return text.startswith(self.prefix + " ")
 
81
                else:
 
82
                        return text.startswith(self.prefix)
 
83
 
 
84
 
 
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)
 
88
 
 
89
                if len(text) == 0:
 
90
                        deskbar.run_command(self.sans_arg_command, text)
 
91
                else:
 
92
                        deskbar.run_command(self.with_arg_command, text)