root/branches/gajim_0.8/src/gtkgui_helpers.py

Revision 3077, 5.1 kB (checked in by dkirov, 3 years ago)

make check for unicode text as argument

Line 
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
21import xml.sax.saxutils
22import gtk
23import os
24from common import i18n
25i18n.init()
26_ = i18n._
27from common import gajim
28
29def get_default_font():
30        ''' Get the desktop setting for application font
31        first check for GNOME, then XFCE and last KDE
32        it returns None on failure or else a string 'Font Size' '''
33       
34        try:
35                import gconf
36                # in try because daemon may not be there
37                client = gconf.client_get_default()
38        except:
39                pass
40        else:
41                return client.get_string('/desktop/gnome/interface/font_name')
42
43        # try to get xfce default font
44        # Xfce 4.2 adopts freedesktop.org's Base Directory Specification
45        # see http://www.xfce.org/~benny/xfce/file-locations.html
46        # and http://freedesktop.org/Standards/basedir-spec
47        xdg_config_home = os.environ.get('XDG_CONFIG_HOME', '')
48        if xdg_config_home == '':
49                xdg_config_home = os.path.expanduser('~/.config') # default     
50        xfce_config_file = os.path.join(xdg_config_home, 'xfce4/mcs_settings/gtk.xml')
51       
52        kde_config_file = os.path.expanduser('~/.kde/share/config/kdeglobals')
53       
54        if os.path.exists(xfce_config_file):
55                try:
56                        for line in file(xfce_config_file):
57                                if line.find('name="Gtk/FontName"') != -1:
58                                        start = line.find('value="') + 7
59                                        return line[start:line.find('"', start)]
60                except:
61                        #we talk about file
62                        print _('error: cannot open %s for reading') % xfce_config_file
63       
64        elif os.path.exists(kde_config_file):
65                try:
66                        for line in file(kde_config_file):
67                                if line.find('font=') == 0: # font=Verdana,9,other_numbers
68                                        start = 5 # 5 is len('font=')
69                                        line = line[start:]
70                                        values = line.split(',')
71                                        font_name = values[0]
72                                        font_size = values[1]
73                                        font_string = '%s %s' % (font_name, font_size) # Verdana 9
74                                        return font_string
75                except:
76                        #we talk about file
77                        print _('error: cannot open %s for reading') % kde_config_file
78       
79        return None
80       
81def reduce_chars_newlines(text, max_chars = 0, max_lines = 0, 
82        widget = None):
83        ''' Cut the chars after 'max_chars' on each line
84        and show only the first 'max_lines'. If there is more text
85        to be shown, display the whole text in tooltip on 'widget'
86        If any of the params is not present(None or 0) the action
87        on it is not performed
88        '''
89        # assure that we have only unicode text
90        if type(text) == str:
91                text = unicode(text, encoding='utf-8')
92               
93        def _cut_if_long(str):
94                if len(str) > max_chars:
95                        str = str[:max_chars - 3] + '...'
96                return str
97       
98        if max_lines == 0:
99                lines = text.split('\n')
100        else:
101                lines = text.split('\n', max_lines)[:max_lines]
102        if max_chars > 0:
103                if lines:
104                        lines = map(lambda e: _cut_if_long(e), lines)
105        if lines:
106                reduced_text = reduce(lambda e, e1: e + '\n' + e1, lines)
107        else:
108                reduced_text = ''
109        if reduced_text != text and widget is not None:
110                pass # FIXME show tooltip
111        return reduced_text
112
113def escape_for_pango_markup(string):
114        # escapes < > & \ "
115        # for pango markup not to break
116        if string is None:
117                return
118        if gtk.pygtk_version >= (2, 8, 0) and gtk.gtk_version >= (2, 8, 0):
119                escaped_str = gobject.markup_escape_text(string)
120        else:
121                escaped_str =xml.sax.saxutils.escape(string, {'\\': '&apos;',
122                        '"': '&quot;'})
123       
124        return escaped_str
125
126def autodetect_browser_mailer():
127        #recognize the environment for appropriate browser/mailer
128        if os.path.isdir('/proc'):
129                # under Linux: checking if 'gnome-session' or
130                # 'startkde' programs were run before gajim, by
131                # checking /proc (if it exists)
132                #
133                # if something is unclear, read `man proc`;
134                # if /proc exists, directories that have only numbers
135                # in their names contain data about processes.
136                # /proc/[xxx]/exe is a symlink to executable started
137                # as process number [xxx].
138                # filter out everything that we are not interested in:
139                files = os.listdir('/proc')
140
141                # files that doesn't have only digits in names...
142                files = filter(str.isdigit, files)
143
144                # files that aren't directories...
145                files = filter(lambda f:os.path.isdir('/proc/' + f), files)
146
147                # processes owned by somebody not running gajim...
148                # (we check if we have access to that file)
149                files = filter(lambda f:os.access('/proc/' + f +'/exe', os.F_OK), files)
150
151                # be sure that /proc/[number]/exe is really a symlink
152                # to avoid TBs in incorrectly configured systems
153                files = filter(lambda f:os.path.islink('/proc/' + f + '/exe'), files)
154
155                # list of processes
156                processes = [os.path.basename(os.readlink('/proc/' + f +'/exe')) for f in files]
157                if 'gnome-session' in processes:
158                        gajim.config.set('openwith', 'gnome-open')
159                elif 'startkde' in processes:
160                        gajim.config.set('openwith', 'kfmclient exec')
161                else:
162                        gajim.config.set('openwith', 'custom')
Note: See TracBrowser for help on using the browser.