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

Revision 3446, 5.6 kB (checked in by nk, 3 years ago)

gtkgui_helpers

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