Commit | Line | Data |
---|---|---|
8706f230 | 1 | #!/usr/bin/env python3 |
797fb5e9 TS |
2 | # -*- coding: utf-8 -*- |
3 | # | |
4 | # Very simple chars indicator. | |
5 | # Author: Tobias Schlitt <toby@php.net> | |
6 | # | |
51a9e97c CG |
7 | # Hacked by Cyrille37 on 2016-02-03 |
8 | # | |
8706f230 | 9 | # Ported to Python 3 by Adrian I Lam <adrianiainlam@gmail.com> |
10 | # | |
797fb5e9 TS |
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 | |
1b39c416 | 38 | import re |
dfb02694 AIL |
39 | import gi |
40 | gi.require_version('Gdk', '3.0') | |
41 | gi.require_version('Gtk', '3.0') | |
42 | gi.require_version('AppIndicator3', '0.1') | |
7e8c95bb | 43 | from gi.repository import Gdk, Gtk, Gio, AppIndicator3 |
797fb5e9 | 44 | import signal |
797fb5e9 TS |
45 | |
46 | APP_NAME = 'indicator-chars' | |
8706f230 | 47 | APP_VERSION = '0.2_python3' |
797fb5e9 TS |
48 | |
49 | class IndicatorChars: | |
1b39c416 | 50 | CHARS_PATH = os.path.join(os.getenv('HOME'), '.indicator-chars') |
91191516 | 51 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
797fb5e9 | 52 | |
1b39c416 TS |
53 | submenu_title_pattern = re.compile(r'\[([^]]+)\] *') |
54 | description_pattern = re.compile(r' *(\([^)]+\)) *') | |
55 | ||
797fb5e9 | 56 | def __init__(self): |
8706f230 | 57 | self.ind = AppIndicator3.Indicator.new( |
58 | "Chars", os.path.join(self.SCRIPT_DIR, 'light16x16.svg'), | |
59 | AppIndicator3.IndicatorCategory.APPLICATION_STATUS) | |
60 | self.ind.set_status(AppIndicator3.IndicatorStatus.ACTIVE) | |
797fb5e9 TS |
61 | |
62 | self.update_menu() | |
63 | ||
64 | def create_menu_item(self, label): | |
8706f230 | 65 | item = Gtk.MenuItem() |
797fb5e9 TS |
66 | item.set_label(label) |
67 | return item | |
68 | ||
69 | def on_chars_changed(self, filemonitor, file, other_file, event_type): | |
8706f230 | 70 | if event_type == Gio.FileMonitorEvent.CHANGES_DONE_HINT: |
71 | print('Characters changed, updating menu...') | |
797fb5e9 TS |
72 | self.update_menu() |
73 | ||
74 | def update_menu(self, widget = None, data = None): | |
75 | try: | |
76 | charDef = open(self.CHARS_PATH).readlines() | |
77 | except IOError: | |
78 | charDef = [] | |
79 | ||
80 | # Create menu | |
8706f230 | 81 | menu = Gtk.Menu() |
797fb5e9 TS |
82 | |
83 | for charLine in charDef: | |
8706f230 | 84 | charLine = str(charLine) |
797fb5e9 | 85 | charLine = charLine.strip() |
1b39c416 TS |
86 | submenu_match = self.submenu_title_pattern.match(charLine) |
87 | if submenu_match: | |
88 | submenu_title = submenu_match.group(1) | |
89 | # remove title part from remainder: | |
90 | charLine = charLine[submenu_match.end():] | |
91 | else: | |
92 | submenu_title = ''.join( | |
93 | self.description_pattern.split(charLine)[::2]) | |
94 | parentItem = self.create_menu_item(submenu_title) | |
8706f230 | 95 | subMenu = Gtk.Menu() |
1b39c416 TS |
96 | while charLine: |
97 | char = charLine[0] | |
98 | charLine = charLine[1:] | |
99 | description_match = self.description_pattern.match(charLine) | |
100 | if description_match: | |
101 | item_title = char + ' ' + description_match.group(1) | |
102 | # remove description part from remainder: | |
103 | charLine = charLine[description_match.end():] | |
104 | else: | |
105 | item_title = char | |
106 | subItem = self.create_menu_item(item_title) | |
797fb5e9 TS |
107 | subItem.connect("activate", self.on_char_click, char) |
108 | subMenu.append(subItem) | |
109 | parentItem.set_submenu(subMenu) | |
110 | menu.append(parentItem) | |
111 | ||
8706f230 | 112 | menu.append(Gtk.SeparatorMenuItem()) |
1b39c416 TS |
113 | quit_item = self.create_menu_item('Quit') |
114 | quit_item.connect("activate", self.on_quit) | |
115 | menu.append(quit_item) | |
116 | ||
797fb5e9 | 117 | # Show the menu |
146fe42b | 118 | self.ind.set_menu(menu) |
797fb5e9 TS |
119 | menu.show_all() |
120 | ||
121 | def on_char_click(self, widget, char): | |
7e8c95bb | 122 | cb = Gtk.Clipboard.get(Gdk.Atom.intern("PRIMARY", False)) |
123 | cb.set_text(char, -1) | |
124 | cb = Gtk.Clipboard.get(Gdk.Atom.intern("CLIPBOARD", False)) | |
125 | cb.set_text(char, -1) | |
797fb5e9 | 126 | |
1b39c416 | 127 | def on_quit(self, widget): |
8706f230 | 128 | Gtk.main_quit() |
1b39c416 TS |
129 | |
130 | ||
797fb5e9 TS |
131 | if __name__ == "__main__": |
132 | # Catch CTRL-C | |
8706f230 | 133 | signal.signal(signal.SIGINT, lambda signal, frame: Gtk.main_quit()) |
797fb5e9 TS |
134 | |
135 | # Run the indicator | |
136 | i = IndicatorChars() | |
137 | ||
138 | # Monitor bookmarks changes | |
8706f230 | 139 | file = Gio.File.new_for_path(i.CHARS_PATH) |
140 | monitor = file.monitor_file(Gio.FileMonitorFlags.NONE, None) | |
797fb5e9 TS |
141 | monitor.connect("changed", i.on_chars_changed) |
142 | ||
143 | # Main gtk loop | |
8706f230 | 144 | Gtk.main() |