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