Corrected but for Ubuntu 13.10
[indicator-chars.git] / indicator-chars.py
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 #
4 # Very simple chars indicator.
5 # Author: Tobias Schlitt <toby@php.net>
6 #
7 # Copyright (c) 2011, Tobias Schlitt
8 # All rights reserved.
9 #
10 # Redistribution and use in source and binary forms, with or without
11 # modification, are permitted provided that the following conditions are met:
12 #
13 # Redistributions of source code must retain the above copyright notice,
14 # this list of conditions and the following disclaimer. Redistributions
15 # in binary form must reproduce the above copyright notice, this list of
16 # conditions and the following disclaimer in the documentation and/or
17 # other materials provided with the distribution.
18 #
19 # THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
20 # "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
21 # LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
22 # FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
23 # COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
24 # INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
25 # (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 # SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27 # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
28 # LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE
30 # USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH
31 # DAMAGE.
32
33 import os
34 import re
35 import gtk
36 import gio
37 import signal
38 import subprocess
39 import appindicator
40
41 APP_NAME = 'indicator-chars'
42 APP_VERSION = '0.2'
43
44 class IndicatorChars:
45 CHARS_PATH = os.path.join(os.getenv('HOME'), '.indicator-chars')
46 SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
47
48 submenu_title_pattern = re.compile(r'\[([^]]+)\] *')
49 description_pattern = re.compile(r' *(\([^)]+\)) *')
50
51 def __init__(self):
52 self.ind = appindicator.Indicator(
53 "Chars", os.path.join(self.SCRIPT_DIR, 'light16x16.png'),
54 appindicator.CATEGORY_APPLICATION_STATUS)
55 self.ind.set_status(appindicator.STATUS_ACTIVE)
56
57 self.update_menu()
58
59 def create_menu_item(self, label):
60 item = gtk.MenuItem()
61 item.set_label(label)
62 return item
63
64 def on_chars_changed(self, filemonitor, file, other_file, event_type):
65 if event_type == gio.FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
66 print 'Characters changed, updating menu...'
67 self.update_menu()
68
69 def update_menu(self, widget = None, data = None):
70 try:
71 charDef = open(self.CHARS_PATH).readlines()
72 except IOError:
73 charDef = []
74
75 # Create menu
76 menu = gtk.Menu()
77
78 for charLine in charDef:
79 charLine = unicode(charLine)
80 charLine = charLine.strip()
81 submenu_match = self.submenu_title_pattern.match(charLine)
82 if submenu_match:
83 submenu_title = submenu_match.group(1)
84 # remove title part from remainder:
85 charLine = charLine[submenu_match.end():]
86 else:
87 submenu_title = ''.join(
88 self.description_pattern.split(charLine)[::2])
89 parentItem = self.create_menu_item(submenu_title)
90 subMenu = gtk.Menu()
91 while charLine:
92 char = charLine[0]
93 charLine = charLine[1:]
94 description_match = self.description_pattern.match(charLine)
95 if description_match:
96 item_title = char + ' ' + description_match.group(1)
97 # remove description part from remainder:
98 charLine = charLine[description_match.end():]
99 else:
100 item_title = char
101 subItem = self.create_menu_item(item_title)
102 subItem.connect("activate", self.on_char_click, char)
103 subMenu.append(subItem)
104 parentItem.set_submenu(subMenu)
105 menu.append(parentItem)
106
107 menu.append(gtk.SeparatorMenuItem())
108 quit_item = self.create_menu_item('Quit')
109 quit_item.connect("activate", self.on_quit)
110 menu.append(quit_item)
111
112 # Show the menu
113 self.ind.set_menu(menu)
114 menu.show_all()
115
116 def on_char_click(self, widget, char):
117 cb = gtk.Clipboard(selection="PRIMARY")
118 cb.set_text(char)
119
120 def on_quit(self, widget):
121 gtk.main_quit()
122
123
124 if __name__ == "__main__":
125 # Catch CTRL-C
126 signal.signal(signal.SIGINT, lambda signal, frame: gtk.main_quit())
127
128 # Run the indicator
129 i = IndicatorChars()
130
131 # Monitor bookmarks changes
132 file = gio.File(i.CHARS_PATH)
133 monitor = file.monitor_file()
134 monitor.connect("changed", i.on_chars_changed)
135
136 # Main gtk loop
137 gtk.main()