Fix TypeErrors due to Gtk.Clipboard API difference between PyGTK and PyGObject
[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 import subprocess
42
43 APP_NAME = 'indicator-chars'
44 APP_VERSION = '0.2_python3'
45
46 class IndicatorChars:
47 CHARS_PATH = os.path.join(os.getenv('HOME'), '.indicator-chars')
48 SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
49
50 submenu_title_pattern = re.compile(r'\[([^]]+)\] *')
51 description_pattern = re.compile(r' *(\([^)]+\)) *')
52
53 def __init__(self):
54 self.ind = AppIndicator3.Indicator.new(
55 "Chars", os.path.join(self.SCRIPT_DIR, 'light16x16.svg'),
56 AppIndicator3.IndicatorCategory.APPLICATION_STATUS)
57 self.ind.set_status(AppIndicator3.IndicatorStatus.ACTIVE)
58
59 self.update_menu()
60
61 def create_menu_item(self, label):
62 item = Gtk.MenuItem()
63 item.set_label(label)
64 return item
65
66 def on_chars_changed(self, filemonitor, file, other_file, event_type):
67 if event_type == Gio.FileMonitorEvent.CHANGES_DONE_HINT:
68 print('Characters changed, updating menu...')
69 self.update_menu()
70
71 def update_menu(self, widget = None, data = None):
72 try:
73 charDef = open(self.CHARS_PATH).readlines()
74 except IOError:
75 charDef = []
76
77 # Create menu
78 menu = Gtk.Menu()
79
80 for charLine in charDef:
81 charLine = str(charLine)
82 charLine = charLine.strip()
83 submenu_match = self.submenu_title_pattern.match(charLine)
84 if submenu_match:
85 submenu_title = submenu_match.group(1)
86 # remove title part from remainder:
87 charLine = charLine[submenu_match.end():]
88 else:
89 submenu_title = ''.join(
90 self.description_pattern.split(charLine)[::2])
91 parentItem = self.create_menu_item(submenu_title)
92 subMenu = Gtk.Menu()
93 while charLine:
94 char = charLine[0]
95 charLine = charLine[1:]
96 description_match = self.description_pattern.match(charLine)
97 if description_match:
98 item_title = char + ' ' + description_match.group(1)
99 # remove description part from remainder:
100 charLine = charLine[description_match.end():]
101 else:
102 item_title = char
103 subItem = self.create_menu_item(item_title)
104 subItem.connect("activate", self.on_char_click, char)
105 subMenu.append(subItem)
106 parentItem.set_submenu(subMenu)
107 menu.append(parentItem)
108
109 menu.append(Gtk.SeparatorMenuItem())
110 quit_item = self.create_menu_item('Quit')
111 quit_item.connect("activate", self.on_quit)
112 menu.append(quit_item)
113
114 # Show the menu
115 self.ind.set_menu(menu)
116 menu.show_all()
117
118 def on_char_click(self, widget, char):
119 cb = Gtk.Clipboard.get(Gdk.Atom.intern("PRIMARY", False))
120 cb.set_text(char, -1)
121 cb = Gtk.Clipboard.get(Gdk.Atom.intern("CLIPBOARD", False))
122 cb.set_text(char, -1)
123
124 def on_quit(self, widget):
125 Gtk.main_quit()
126
127
128 if __name__ == "__main__":
129 # Catch CTRL-C
130 signal.signal(signal.SIGINT, lambda signal, frame: Gtk.main_quit())
131
132 # Run the indicator
133 i = IndicatorChars()
134
135 # Monitor bookmarks changes
136 file = Gio.File.new_for_path(i.CHARS_PATH)
137 monitor = file.monitor_file(Gio.FileMonitorFlags.NONE, None)
138 monitor.connect("changed", i.on_chars_changed)
139
140 # Main gtk loop
141 Gtk.main()