| 1 | ## gtkgui_helpers.py |
|---|
| 2 | ## |
|---|
| 3 | ## Gajim Team: |
|---|
| 4 | ## - Yann Le Boulanger <asterix@lagaule.org> |
|---|
| 5 | ## - Vincent Hanquez <tab@snarc.org> |
|---|
| 6 | ## - Nikos Kouremenos <kourem@gmail.com> |
|---|
| 7 | ## - Dimitur Kirov <dkirov@gmail.com> |
|---|
| 8 | ## |
|---|
| 9 | ## Copyright (C) 2003-2005 Gajim Team |
|---|
| 10 | ## |
|---|
| 11 | ## This program is free software; you can redistribute it and/or modify |
|---|
| 12 | ## it under the terms of the GNU General Public License as published |
|---|
| 13 | ## by the Free Software Foundation; version 2 only. |
|---|
| 14 | ## |
|---|
| 15 | ## This program is distributed in the hope that it will be useful, |
|---|
| 16 | ## but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 17 | ## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 18 | ## GNU General Public License for more details. |
|---|
| 19 | ## |
|---|
| 20 | |
|---|
| 21 | import xml.sax.saxutils |
|---|
| 22 | import gtk |
|---|
| 23 | import os |
|---|
| 24 | from common import i18n |
|---|
| 25 | i18n.init() |
|---|
| 26 | _ = i18n._ |
|---|
| 27 | from common import gajim |
|---|
| 28 | from common import helpers |
|---|
| 29 | |
|---|
| 30 | screen_w = gtk.gdk.screen_width() |
|---|
| 31 | screen_h = gtk.gdk.screen_height() |
|---|
| 32 | |
|---|
| 33 | def get_default_font(): |
|---|
| 34 | ''' Get the desktop setting for application font |
|---|
| 35 | first check for GNOME, then XFCE and last KDE |
|---|
| 36 | it returns None on failure or else a string 'Font Size' ''' |
|---|
| 37 | |
|---|
| 38 | try: |
|---|
| 39 | import gconf |
|---|
| 40 | # in try because daemon may not be there |
|---|
| 41 | client = gconf.client_get_default() |
|---|
| 42 | except: |
|---|
| 43 | pass |
|---|
| 44 | else: |
|---|
| 45 | return helpers.ensure_unicode_string( |
|---|
| 46 | client.get_string('/desktop/gnome/interface/font_name')) |
|---|
| 47 | |
|---|
| 48 | # try to get xfce default font |
|---|
| 49 | # Xfce 4.2 adopts freedesktop.org's Base Directory Specification |
|---|
| 50 | # see http://www.xfce.org/~benny/xfce/file-locations.html |
|---|
| 51 | # and http://freedesktop.org/Standards/basedir-spec |
|---|
| 52 | xdg_config_home = os.environ.get('XDG_CONFIG_HOME', '') |
|---|
| 53 | if xdg_config_home == '': |
|---|
| 54 | xdg_config_home = os.path.expanduser('~/.config') # default |
|---|
| 55 | xfce_config_file = os.path.join(xdg_config_home, 'xfce4/mcs_settings/gtk.xml') |
|---|
| 56 | |
|---|
| 57 | kde_config_file = os.path.expanduser('~/.kde/share/config/kdeglobals') |
|---|
| 58 | |
|---|
| 59 | if os.path.exists(xfce_config_file): |
|---|
| 60 | try: |
|---|
| 61 | for line in file(xfce_config_file): |
|---|
| 62 | if line.find('name="Gtk/FontName"') != -1: |
|---|
| 63 | start = line.find('value="') + 7 |
|---|
| 64 | return helpers.ensure_unicode_string( |
|---|
| 65 | line[start:line.find('"', start)]) |
|---|
| 66 | except: |
|---|
| 67 | #we talk about file |
|---|
| 68 | print _('error: cannot open %s for reading') % xfce_config_file |
|---|
| 69 | |
|---|
| 70 | elif os.path.exists(kde_config_file): |
|---|
| 71 | try: |
|---|
| 72 | for line in file(kde_config_file): |
|---|
| 73 | if line.find('font=') == 0: # font=Verdana,9,other_numbers |
|---|
| 74 | start = 5 # 5 is len('font=') |
|---|
| 75 | line = line[start:] |
|---|
| 76 | values = line.split(',') |
|---|
| 77 | font_name = values[0] |
|---|
| 78 | font_size = values[1] |
|---|
| 79 | font_string = '%s %s' % (font_name, font_size) # Verdana 9 |
|---|
| 80 | return helpers.ensure_unicode_string(font_string) |
|---|
| 81 | except: |
|---|
| 82 | #we talk about file |
|---|
| 83 | print _('error: cannot open %s for reading') % kde_config_file |
|---|
| 84 | |
|---|
| 85 | return None |
|---|
| 86 | |
|---|
| 87 | def reduce_chars_newlines(text, max_chars = 0, max_lines = 0, |
|---|
| 88 | widget = None): |
|---|
| 89 | ''' Cut the chars after 'max_chars' on each line |
|---|
| 90 | and show only the first 'max_lines'. If there is more text |
|---|
| 91 | to be shown, display the whole text in tooltip on 'widget' |
|---|
| 92 | If any of the params is not present(None or 0) the action |
|---|
| 93 | on it is not performed |
|---|
| 94 | ''' |
|---|
| 95 | text = text |
|---|
| 96 | |
|---|
| 97 | def _cut_if_long(str): |
|---|
| 98 | if len(str) > max_chars: |
|---|
| 99 | str = str[:max_chars - 3] + '...' |
|---|
| 100 | return str |
|---|
| 101 | |
|---|
| 102 | if max_lines == 0: |
|---|
| 103 | lines = text.split('\n') |
|---|
| 104 | else: |
|---|
| 105 | lines = text.split('\n', max_lines)[:max_lines] |
|---|
| 106 | if max_chars > 0: |
|---|
| 107 | if lines: |
|---|
| 108 | lines = map(lambda e: _cut_if_long(e), lines) |
|---|
| 109 | if lines: |
|---|
| 110 | reduced_text = reduce(lambda e, e1: e + '\n' + e1, lines) |
|---|
| 111 | else: |
|---|
| 112 | reduced_text = '' |
|---|
| 113 | if reduced_text != text and widget is not None: |
|---|
| 114 | pass # FIXME show tooltip |
|---|
| 115 | return reduced_text |
|---|
| 116 | |
|---|
| 117 | def escape_for_pango_markup(string): |
|---|
| 118 | # escapes < > & ' " |
|---|
| 119 | # for pango markup not to break |
|---|
| 120 | if string is None: |
|---|
| 121 | return |
|---|
| 122 | if gtk.pygtk_version >= (2, 8, 0) and gtk.gtk_version >= (2, 8, 0): |
|---|
| 123 | escaped_str = gobject.markup_escape_text(string) |
|---|
| 124 | else: |
|---|
| 125 | escaped_str =xml.sax.saxutils.escape(string, {"'": ''', |
|---|
| 126 | '"': '"'}) |
|---|
| 127 | |
|---|
| 128 | return escaped_str |
|---|
| 129 | |
|---|
| 130 | def autodetect_browser_mailer(): |
|---|
| 131 | #recognize the environment for appropriate browser/mailer |
|---|
| 132 | if os.path.isdir('/proc'): |
|---|
| 133 | # under Linux: checking if 'gnome-session' or |
|---|
| 134 | # 'startkde' programs were run before gajim, by |
|---|
| 135 | # checking /proc (if it exists) |
|---|
| 136 | # |
|---|
| 137 | # if something is unclear, read `man proc`; |
|---|
| 138 | # if /proc exists, directories that have only numbers |
|---|
| 139 | # in their names contain data about processes. |
|---|
| 140 | # /proc/[xxx]/exe is a symlink to executable started |
|---|
| 141 | # as process number [xxx]. |
|---|
| 142 | # filter out everything that we are not interested in: |
|---|
| 143 | files = os.listdir('/proc') |
|---|
| 144 | |
|---|
| 145 | # files that doesn't have only digits in names... |
|---|
| 146 | files = filter(str.isdigit, files) |
|---|
| 147 | |
|---|
| 148 | # files that aren't directories... |
|---|
| 149 | files = filter(lambda f:os.path.isdir('/proc/' + f), files) |
|---|
| 150 | |
|---|
| 151 | # processes owned by somebody not running gajim... |
|---|
| 152 | # (we check if we have access to that file) |
|---|
| 153 | files = filter(lambda f:os.access('/proc/' + f +'/exe', os.F_OK), files) |
|---|
| 154 | |
|---|
| 155 | # be sure that /proc/[number]/exe is really a symlink |
|---|
| 156 | # to avoid TBs in incorrectly configured systems |
|---|
| 157 | files = filter(lambda f:os.path.islink('/proc/' + f + '/exe'), files) |
|---|
| 158 | |
|---|
| 159 | # list of processes |
|---|
| 160 | processes = [os.path.basename(os.readlink('/proc/' + f +'/exe')) for f in files] |
|---|
| 161 | if 'gnome-session' in processes: |
|---|
| 162 | gajim.config.set('openwith', 'gnome-open') |
|---|
| 163 | elif 'startkde' in processes: |
|---|
| 164 | gajim.config.set('openwith', 'kfmclient exec') |
|---|
| 165 | else: |
|---|
| 166 | gajim.config.set('openwith', 'custom') |
|---|
| 167 | |
|---|
| 168 | def move_window(window, x, y): |
|---|
| 169 | ''' moves the window but also checks if out of screen ''' |
|---|
| 170 | if x < 0: |
|---|
| 171 | x = 0 |
|---|
| 172 | if y < 0: |
|---|
| 173 | y = 0 |
|---|
| 174 | window.move(x, y) |
|---|
| 175 | |
|---|
| 176 | def resize_window(window, w, h): |
|---|
| 177 | ''' resizes window but also checks if huge window or negative values ''' |
|---|
| 178 | if w > screen_w: |
|---|
| 179 | w = screen_w |
|---|
| 180 | if h > screen_h: |
|---|
| 181 | h = screen_h |
|---|
| 182 | window.resize(abs(w), abs(h)) |
|---|