| 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 gtk |
| 35 | import gio |
| 36 | import signal |
| 37 | import subprocess |
| 38 | import appindicator |
| 39 | |
| 40 | APP_NAME = 'indicator-chars' |
| 41 | APP_VERSION = '0.1' |
| 42 | |
| 43 | class IndicatorChars: |
| 44 | CHARS_PATH = os.getenv('HOME') + '/.indicator-chars' |
| 45 | SCRIPT_DIR = os.path.dirname(os.path.realpath(__file__)) |
| 46 | |
| 47 | def __init__(self): |
| 48 | self.ind = appindicator.Indicator("Chars", self.SCRIPT_DIR + '/light16x16.png', appindicator.CATEGORY_APPLICATION_STATUS) |
| 49 | self.ind.set_status(appindicator.STATUS_ACTIVE) |
| 50 | |
| 51 | self.update_menu() |
| 52 | |
| 53 | def create_menu_item(self, label): |
| 54 | item = gtk.MenuItem() |
| 55 | item.set_label(label) |
| 56 | return item |
| 57 | |
| 58 | def on_chars_changed(self, filemonitor, file, other_file, event_type): |
| 59 | if event_type == gio.FILE_MONITOR_EVENT_CHANGES_DONE_HINT: |
| 60 | print 'Characters changed, updating menu...' |
| 61 | self.update_menu() |
| 62 | |
| 63 | def update_menu(self, widget = None, data = None): |
| 64 | try: |
| 65 | charDef = open(self.CHARS_PATH).readlines() |
| 66 | except IOError: |
| 67 | charDef = [] |
| 68 | |
| 69 | # Create menu |
| 70 | menu = gtk.Menu() |
| 71 | self.ind.set_menu(menu) |
| 72 | |
| 73 | for charLine in charDef: |
| 74 | charLine = unicode(charLine) |
| 75 | charLine = charLine.strip() |
| 76 | parentItem = self.create_menu_item(charLine) |
| 77 | subMenu = gtk.Menu() |
| 78 | for char in charLine: |
| 79 | subItem = self.create_menu_item(char) |
| 80 | subItem.connect("activate", self.on_char_click, char) |
| 81 | subMenu.append(subItem) |
| 82 | parentItem.set_submenu(subMenu) |
| 83 | menu.append(parentItem) |
| 84 | |
| 85 | # Show the menu |
| 86 | menu.show_all() |
| 87 | |
| 88 | def on_char_click(self, widget, char): |
| 89 | cb = gtk.Clipboard(selection="PRIMARY") |
| 90 | cb.set_text(char) |
| 91 | |
| 92 | if __name__ == "__main__": |
| 93 | # Catch CTRL-C |
| 94 | signal.signal(signal.SIGINT, lambda signal, frame: gtk.main_quit()) |
| 95 | |
| 96 | # Run the indicator |
| 97 | i = IndicatorChars() |
| 98 | |
| 99 | # Monitor bookmarks changes |
| 100 | file = gio.File(i.CHARS_PATH) |
| 101 | monitor = file.monitor_file() |
| 102 | monitor.connect("changed", i.on_chars_changed) |
| 103 | |
| 104 | # Main gtk loop |
| 105 | gtk.main() |