doc
[indicator-chars.git] / indicator-chars.py
1 #!/usr/bin/env python
2 # -*- coding: utf-8 -*-
3 #
4 # Very simple chars indicator.
5 # Author: Tobias Schlitt <toby@php.net>
6 #
7 # Hacked by Cyrille37 on 2016-02-03
8 #
9 # Copyright (c) 2011, Tobias Schlitt
10 # All rights reserved.
11 #
12 # Redistribution and use in source and binary forms, with or without
13 # modification, are permitted provided that the following conditions are met:
14 #
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.
20 #
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
33 # DAMAGE.
34
35 import os
36 import re
37 import gtk
38 import gio
39 import signal
40 import subprocess
41 # sudo apt-get install python-appindicator
42 import appindicator
43
44 APP_NAME = 'indicator-chars'
45 APP_VERSION = '0.2'
46
47 class IndicatorChars:
48 CHARS_PATH = os.path.join(os.getenv('HOME'), '.indicator-chars')
49 SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
50
51 submenu_title_pattern = re.compile(r'\[([^]]+)\] *')
52 description_pattern = re.compile(r' *(\([^)]+\)) *')
53
54 def __init__(self):
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)
62
63 self.update_menu()
64
65 def create_menu_item(self, label):
66 item = gtk.MenuItem()
67 item.set_label(label)
68 return item
69
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...'
73 self.update_menu()
74
75 def update_menu(self, widget = None, data = None):
76 try:
77 charDef = open(self.CHARS_PATH).readlines()
78 except IOError:
79 charDef = []
80
81 # Create menu
82 menu = gtk.Menu()
83
84 for charLine in charDef:
85 charLine = unicode(charLine)
86 charLine = charLine.strip()
87 submenu_match = self.submenu_title_pattern.match(charLine)
88 if submenu_match:
89 submenu_title = submenu_match.group(1)
90 # remove title part from remainder:
91 charLine = charLine[submenu_match.end():]
92 else:
93 submenu_title = ''.join(
94 self.description_pattern.split(charLine)[::2])
95 parentItem = self.create_menu_item(submenu_title)
96 subMenu = gtk.Menu()
97 while charLine:
98 char = charLine[0]
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():]
105 else:
106 item_title = char
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)
112
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)
117
118 # Show the menu
119 self.ind.set_menu(menu)
120 menu.show_all()
121
122 def on_char_click(self, widget, char):
123 cb = gtk.Clipboard(selection="PRIMARY")
124 cb.set_text(char)
125 cb = gtk.Clipboard(selection="CLIPBOARD")
126 cb.set_text(char)
127
128 def on_quit(self, widget):
129 gtk.main_quit()
130
131
132 if __name__ == "__main__":
133 # Catch CTRL-C
134 signal.signal(signal.SIGINT, lambda signal, frame: gtk.main_quit())
135
136 # Run the indicator
137 i = IndicatorChars()
138
139 # Monitor bookmarks changes
140 file = gio.File(i.CHARS_PATH)
141 monitor = file.monitor_file()
142 monitor.connect("changed", i.on_chars_changed)
143
144 # Main gtk loop
145 gtk.main()