add a char copy into Clipboard(selection="CLIPBOARD")
[indicator-chars.git] / indicator-chars.py
CommitLineData
dd443ccd 1#!/usr/bin/env python
797fb5e9
TS
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
33import os
1b39c416 34import re
797fb5e9
TS
35import gtk
36import gio
37import signal
38import subprocess
39import appindicator
40
41APP_NAME = 'indicator-chars'
1b39c416 42APP_VERSION = '0.2'
797fb5e9
TS
43
44class IndicatorChars:
1b39c416 45 CHARS_PATH = os.path.join(os.getenv('HOME'), '.indicator-chars')
91191516 46 SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__))
797fb5e9 47
1b39c416
TS
48 submenu_title_pattern = re.compile(r'\[([^]]+)\] *')
49 description_pattern = re.compile(r' *(\([^)]+\)) *')
50
797fb5e9 51 def __init__(self):
1b39c416 52 self.ind = appindicator.Indicator(
a988ac12
CG
53 # Custom icon seems to doesn't work on my Ubuntu 12.04 LTS running Unity 2D
54 #"Chars", os.path.join(self.SCRIPT_DIR, 'light16x16.png'),
55 # So fallback to an referenced theme's icon name
56 "Chars", "accessories-character-map",
1b39c416 57 appindicator.CATEGORY_APPLICATION_STATUS)
797fb5e9
TS
58 self.ind.set_status(appindicator.STATUS_ACTIVE)
59
60 self.update_menu()
61
62 def create_menu_item(self, label):
63 item = gtk.MenuItem()
64 item.set_label(label)
65 return item
66
67 def on_chars_changed(self, filemonitor, file, other_file, event_type):
68 if event_type == gio.FILE_MONITOR_EVENT_CHANGES_DONE_HINT:
69 print 'Characters changed, updating menu...'
70 self.update_menu()
71
72 def update_menu(self, widget = None, data = None):
73 try:
74 charDef = open(self.CHARS_PATH).readlines()
75 except IOError:
76 charDef = []
77
78 # Create menu
79 menu = gtk.Menu()
80 self.ind.set_menu(menu)
81
82 for charLine in charDef:
83 charLine = unicode(charLine)
84 charLine = charLine.strip()
1b39c416
TS
85 submenu_match = self.submenu_title_pattern.match(charLine)
86 if submenu_match:
87 submenu_title = submenu_match.group(1)
88 # remove title part from remainder:
89 charLine = charLine[submenu_match.end():]
90 else:
91 submenu_title = ''.join(
92 self.description_pattern.split(charLine)[::2])
93 parentItem = self.create_menu_item(submenu_title)
797fb5e9 94 subMenu = gtk.Menu()
1b39c416
TS
95 while charLine:
96 char = charLine[0]
97 charLine = charLine[1:]
98 description_match = self.description_pattern.match(charLine)
99 if description_match:
100 item_title = char + ' ' + description_match.group(1)
101 # remove description part from remainder:
102 charLine = charLine[description_match.end():]
103 else:
104 item_title = char
105 subItem = self.create_menu_item(item_title)
797fb5e9
TS
106 subItem.connect("activate", self.on_char_click, char)
107 subMenu.append(subItem)
108 parentItem.set_submenu(subMenu)
109 menu.append(parentItem)
110
1b39c416
TS
111 menu.append(gtk.SeparatorMenuItem())
112 quit_item = self.create_menu_item('Quit')
113 quit_item.connect("activate", self.on_quit)
114 menu.append(quit_item)
115
797fb5e9
TS
116 # Show the menu
117 menu.show_all()
118
119 def on_char_click(self, widget, char):
120 cb = gtk.Clipboard(selection="PRIMARY")
121 cb.set_text(char)
d2dab2c9
CG
122 cb = gtk.Clipboard(selection="CLIPBOARD")
123 cb.set_text(char)
797fb5e9 124
1b39c416
TS
125 def on_quit(self, widget):
126 gtk.main_quit()
127
128
797fb5e9
TS
129if __name__ == "__main__":
130 # Catch CTRL-C
131 signal.signal(signal.SIGINT, lambda signal, frame: gtk.main_quit())
132
133 # Run the indicator
134 i = IndicatorChars()
135
136 # Monitor bookmarks changes
137 file = gio.File(i.CHARS_PATH)
138 monitor = file.monitor_file()
139 monitor.connect("changed", i.on_chars_changed)
140
141 # Main gtk loop
142 gtk.main()