2 # -*- coding: utf-8 -*-
4 # Very simple chars indicator.
5 # Author: Tobias Schlitt <toby@php.net>
7 # Hacked by Cyrille37 on 2016-02-03
9 # Copyright (c) 2011, Tobias Schlitt
10 # All rights reserved.
12 # Redistribution and use in source and binary forms, with or without
13 # modification, are permitted provided that the following conditions are met:
15 # Redistributions of source code must retain the above copyright notice,
16 # this list of conditions and the following disclaimer. Redistributions
17 # in binary form must reproduce the above copyright notice, this list of
18 # conditions and the following disclaimer in the documentation and/or
19 # other materials provided with the distribution.
21 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
24 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
25 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
26 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
27 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
28 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
29 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
30 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
32 # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
41 # sudo apt-get install python-appindicator
44 APP_NAME
= 'indicator-chars'
48 CHARS_PATH
= os
.path
.join(os
.getenv('HOME'), '.indicator-chars')
49 SCRIPT_DIR
= os
.path
.dirname(os
.path
.realpath(__file__
))
51 submenu_title_pattern
= re
.compile(r
'\[([^]]+)\] *')
52 description_pattern
= re
.compile(r
' *(\([^)]+\)) *')
55 self
.ind
= appindicator
.Indicator(
56 # Custom icon seems to doesn't work on my Ubuntu 12.04 LTS running Unity 2D
57 #"Chars", os.path.join(self.SCRIPT_DIR, 'light16x16.png'),
58 # So fallback to an referenced theme's icon name
59 "Chars", "accessories-character-map",
60 appindicator
.CATEGORY_APPLICATION_STATUS
)
61 self
.ind
.set_status(appindicator
.STATUS_ACTIVE
)
65 def create_menu_item(self
, label
):
70 def on_chars_changed(self
, filemonitor
, file, other_file
, event_type
):
71 if event_type
== gio
.FILE_MONITOR_EVENT_CHANGES_DONE_HINT
:
72 print 'Characters changed, updating menu...'
75 def update_menu(self
, widget
= None, data
= None):
77 charDef
= open(self
.CHARS_PATH
).readlines()
84 for charLine
in charDef
:
85 charLine
= unicode(charLine
)
86 charLine
= charLine
.strip()
87 submenu_match
= self
.submenu_title_pattern
.match(charLine
)
89 submenu_title
= submenu_match
.group(1)
90 # remove title part from remainder:
91 charLine
= charLine
[submenu_match
.end():]
93 submenu_title
= ''.join(
94 self
.description_pattern
.split(charLine
)[::2])
95 parentItem
= self
.create_menu_item(submenu_title
)
99 charLine
= charLine
[1:]
100 description_match
= self
.description_pattern
.match(charLine
)
101 if description_match
:
102 item_title
= char
+ ' ' + description_match
.group(1)
103 # remove description part from remainder:
104 charLine
= charLine
[description_match
.end():]
107 subItem
= self
.create_menu_item(item_title
)
108 subItem
.connect("activate", self
.on_char_click
, char
)
109 subMenu
.append(subItem
)
110 parentItem
.set_submenu(subMenu
)
111 menu
.append(parentItem
)
113 menu
.append(gtk
.SeparatorMenuItem())
114 quit_item
= self
.create_menu_item('Quit')
115 quit_item
.connect("activate", self
.on_quit
)
116 menu
.append(quit_item
)
119 self
.ind
.set_menu(menu
)
122 def on_char_click(self
, widget
, char
):
123 cb
= gtk
.Clipboard(selection
="PRIMARY")
125 cb
= gtk
.Clipboard(selection
="CLIPBOARD")
128 def on_quit(self
, widget
):
132 if __name__
== "__main__":
134 signal
.signal(signal
.SIGINT
, lambda signal
, frame
: gtk
.main_quit())
139 # Monitor bookmarks changes
140 file = gio
.File(i
.CHARS_PATH
)
141 monitor
= file.monitor_file()
142 monitor
.connect("changed", i
.on_chars_changed
)