2008-08-15 19:31:51 +02:00
|
|
|
# -*- coding:utf-8 -*-
|
2008-08-15 05:20:23 +02:00
|
|
|
## src/gtkgui_helpers.py
|
2005-07-21 23:39:47 +02:00
|
|
|
##
|
2013-04-05 21:35:39 +02:00
|
|
|
## Copyright (C) 2003-2013 Yann Leboulanger <asterix AT lagaule.org>
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2005-2006 Dimitur Kirov <dkirov AT gmail.com>
|
|
|
|
## Copyright (C) 2005-2007 Nikos Kouremenos <kourem AT gmail.com>
|
|
|
|
## Copyright (C) 2006 Travis Shirk <travis AT pobox.com>
|
2008-08-15 19:31:51 +02:00
|
|
|
## Copyright (C) 2006-2007 Junglecow J <junglecow AT gmail.com>
|
2008-08-15 05:20:23 +02:00
|
|
|
## Copyright (C) 2006-2008 Jean-Marie Traissard <jim AT lapin.org>
|
|
|
|
## Copyright (C) 2007 James Newton <redshodan AT gmail.com>
|
|
|
|
## Julien Pivotto <roidelapluie AT gmail.com>
|
|
|
|
## Copyright (C) 2007-2008 Stephan Erb <steve-e AT h3c.de>
|
|
|
|
## Copyright (C) 2008 Jonathan Schleifer <js-gajim AT webkeks.org>
|
2005-07-21 23:39:47 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## This file is part of Gajim.
|
|
|
|
##
|
|
|
|
## Gajim is free software; you can redistribute it and/or modify
|
2005-07-21 23:39:47 +02:00
|
|
|
## it under the terms of the GNU General Public License as published
|
2007-10-22 13:13:13 +02:00
|
|
|
## by the Free Software Foundation; version 3 only.
|
2005-07-21 23:39:47 +02:00
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## Gajim is distributed in the hope that it will be useful,
|
2005-07-21 23:39:47 +02:00
|
|
|
## but WITHOUT ANY WARRANTY; without even the implied warranty of
|
2008-08-15 05:20:23 +02:00
|
|
|
## MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
2005-07-21 23:39:47 +02:00
|
|
|
## GNU General Public License for more details.
|
|
|
|
##
|
2007-10-22 13:13:13 +02:00
|
|
|
## You should have received a copy of the GNU General Public License
|
2008-08-15 05:20:23 +02:00
|
|
|
## along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2007-10-22 13:13:13 +02:00
|
|
|
##
|
2005-07-21 23:39:47 +02:00
|
|
|
|
2005-07-23 14:45:17 +02:00
|
|
|
import xml.sax.saxutils
|
2012-12-23 16:23:43 +01:00
|
|
|
from gi.repository import Gtk
|
|
|
|
from gi.repository import Gdk
|
|
|
|
from gi.repository import GdkPixbuf
|
|
|
|
from gi.repository import GObject
|
|
|
|
from gi.repository import Pango
|
2005-08-05 01:11:55 +02:00
|
|
|
import os
|
2005-09-08 16:03:19 +02:00
|
|
|
import sys
|
2005-11-01 21:01:14 +01:00
|
|
|
|
2007-01-06 16:31:30 +01:00
|
|
|
import logging
|
|
|
|
log = logging.getLogger('gajim.gtkgui_helpers')
|
|
|
|
|
2005-07-30 12:20:24 +02:00
|
|
|
from common import i18n
|
2005-08-05 01:11:55 +02:00
|
|
|
from common import gajim
|
2013-01-02 13:54:02 +01:00
|
|
|
from common import pep
|
2005-09-17 11:37:40 +02:00
|
|
|
|
2012-12-23 16:23:43 +01:00
|
|
|
gtk_icon_theme = Gtk.IconTheme.get_default()
|
2009-11-27 16:42:32 +01:00
|
|
|
gtk_icon_theme.append_search_path(gajim.ICONS_DIR)
|
|
|
|
|
2013-08-28 16:33:27 +02:00
|
|
|
def get_icon_pixmap(icon_name, size=16, quiet=False):
|
2010-02-08 15:08:40 +01:00
|
|
|
try:
|
|
|
|
return gtk_icon_theme.load_icon(icon_name, size, 0)
|
2013-01-01 23:18:36 +01:00
|
|
|
except GObject.GError as e:
|
2013-08-28 16:33:27 +02:00
|
|
|
if not quiet:
|
|
|
|
log.error('Unable to load icon %s: %s' % (icon_name, str(e)))
|
2009-11-27 16:42:32 +01:00
|
|
|
|
|
|
|
def get_icon_path(icon_name, size=16):
|
2010-02-08 15:08:40 +01:00
|
|
|
try:
|
|
|
|
icon_info = gtk_icon_theme.lookup_icon(icon_name, size, 0)
|
|
|
|
if icon_info == None:
|
|
|
|
log.error('Icon not found: %s' % icon_name)
|
|
|
|
return ""
|
|
|
|
else:
|
|
|
|
return icon_info.get_filename()
|
2013-01-01 23:18:36 +01:00
|
|
|
except GObject.GError as e:
|
2010-02-08 15:08:40 +01:00
|
|
|
log.error("Unable to find icon %s: %s" % (icon_name, str(e)))
|
2009-11-27 16:42:32 +01:00
|
|
|
|
2009-12-02 10:23:17 +01:00
|
|
|
import vcard
|
|
|
|
import dialogs
|
|
|
|
|
|
|
|
|
|
|
|
HAS_PYWIN32 = True
|
|
|
|
if os.name == 'nt':
|
2010-02-08 15:08:40 +01:00
|
|
|
try:
|
|
|
|
import win32file
|
|
|
|
import win32con
|
|
|
|
import pywintypes
|
|
|
|
except ImportError:
|
|
|
|
HAS_PYWIN32 = False
|
2009-12-02 10:23:17 +01:00
|
|
|
|
|
|
|
from common import helpers
|
|
|
|
|
2012-12-23 16:23:43 +01:00
|
|
|
screen_w = Gdk.Screen.width()
|
|
|
|
screen_h = Gdk.Screen.height()
|
2009-12-02 10:23:17 +01:00
|
|
|
|
2009-12-01 15:50:58 +01:00
|
|
|
def add_image_to_menuitem(menuitem, icon_name):
|
2012-12-23 16:23:43 +01:00
|
|
|
img = Gtk.Image()
|
2010-02-08 15:08:40 +01:00
|
|
|
path_img = get_icon_path(icon_name)
|
|
|
|
img.set_from_file(path_img)
|
|
|
|
menuitem.set_image(img)
|
2009-12-01 15:50:58 +01:00
|
|
|
|
|
|
|
def add_image_to_button(button, icon_name):
|
2010-02-08 15:08:40 +01:00
|
|
|
add_image_to_menuitem(button, icon_name)
|
2009-12-01 15:50:58 +01:00
|
|
|
|
2010-01-22 19:57:59 +01:00
|
|
|
GUI_DIR = os.path.join(gajim.DATA_DIR, 'gui')
|
|
|
|
def get_gtk_builder(file_name, widget=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
file_path = os.path.join(GUI_DIR, file_name)
|
2012-12-23 16:23:43 +01:00
|
|
|
builder = Gtk.Builder()
|
2010-02-08 15:08:40 +01:00
|
|
|
builder.set_translation_domain(i18n.APP)
|
|
|
|
if widget:
|
|
|
|
builder.add_objects_from_file(file_path, [widget])
|
|
|
|
else:
|
|
|
|
builder.add_from_file(file_path)
|
|
|
|
return builder
|
2006-05-02 17:53:25 +02:00
|
|
|
|
2006-05-08 23:59:09 +02:00
|
|
|
def get_completion_liststore(entry):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Create a completion model for entry widget completion list consists of
|
|
|
|
(Pixbuf, Text) rows
|
|
|
|
"""
|
2012-12-23 16:23:43 +01:00
|
|
|
completion = Gtk.EntryCompletion()
|
|
|
|
liststore = Gtk.ListStore(GdkPixbuf.Pixbuf, str)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2012-12-23 16:23:43 +01:00
|
|
|
render_pixbuf = Gtk.CellRendererPixbuf()
|
2012-12-27 21:58:52 +01:00
|
|
|
completion.pack_start(render_pixbuf, False)
|
2010-02-08 15:08:40 +01:00
|
|
|
completion.add_attribute(render_pixbuf, 'pixbuf', 0)
|
|
|
|
|
|
|
|
completion.set_property('text_column', 1)
|
|
|
|
completion.set_model(liststore)
|
|
|
|
entry.set_completion(completion)
|
|
|
|
return liststore
|
2008-12-03 22:56:12 +01:00
|
|
|
|
|
|
|
|
2006-04-17 23:59:04 +02:00
|
|
|
def popup_emoticons_under_button(menu, button, parent_win):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Popup the emoticons menu under button, which is in parent_win
|
|
|
|
"""
|
2012-12-27 21:58:52 +01:00
|
|
|
window_x1, window_y1 = parent_win.get_origin()[1:]
|
2013-01-03 16:39:04 +01:00
|
|
|
|
2012-12-27 21:58:52 +01:00
|
|
|
def position_menu_under_button(menu, data):
|
2010-02-08 15:08:40 +01:00
|
|
|
# inline function, which will not keep refs, when used as CB
|
2012-12-27 21:58:52 +01:00
|
|
|
alloc = button.get_allocation()
|
|
|
|
button_x, button_y = alloc.x, alloc.y
|
2013-01-03 16:39:04 +01:00
|
|
|
translated_coordinates = button.translate_coordinates(
|
|
|
|
gajim.interface.roster.window, 0, 0)
|
|
|
|
if translated_coordinates:
|
|
|
|
button_x, button_y = translated_coordinates
|
2013-01-04 22:27:22 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
# now convert them to X11-relative
|
|
|
|
window_x, window_y = window_x1, window_y1
|
|
|
|
x = window_x + button_x
|
|
|
|
y = window_y + button_y
|
|
|
|
|
2013-01-03 16:39:04 +01:00
|
|
|
menu_height = menu.size_request().height
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
## should we pop down or up?
|
2012-12-27 21:58:52 +01:00
|
|
|
if (y + alloc.height + menu_height < Gdk.Screen.height()):
|
2010-02-08 15:08:40 +01:00
|
|
|
# now move the menu below the button
|
2012-12-27 21:58:52 +01:00
|
|
|
y += alloc.height
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
# now move the menu above the button
|
|
|
|
y -= menu_height
|
|
|
|
|
|
|
|
# push_in is True so all the menuitems are always inside screen
|
|
|
|
push_in = True
|
|
|
|
return (x, y, push_in)
|
|
|
|
|
2012-12-27 21:58:52 +01:00
|
|
|
menu.popup(None, None, position_menu_under_button, None, 1, 0)
|
2008-12-03 22:56:12 +01:00
|
|
|
|
2005-09-17 10:32:55 +02:00
|
|
|
def get_theme_font_for_option(theme, option):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Return string description of the font, stored in theme preferences
|
|
|
|
"""
|
|
|
|
font_name = gajim.config.get_per('themes', theme, option)
|
2012-12-23 16:23:43 +01:00
|
|
|
font_desc = Pango.FontDescription()
|
2010-02-08 15:08:40 +01:00
|
|
|
font_prop_str = gajim.config.get_per('themes', theme, option + 'attrs')
|
|
|
|
if font_prop_str:
|
|
|
|
if font_prop_str.find('B') != -1:
|
2012-12-23 16:23:43 +01:00
|
|
|
font_desc.set_weight(Pango.Weight.BOLD)
|
2010-02-08 15:08:40 +01:00
|
|
|
if font_prop_str.find('I') != -1:
|
2012-12-23 16:23:43 +01:00
|
|
|
font_desc.set_style(Pango.Style.ITALIC)
|
|
|
|
fd = Pango.FontDescription(font_name)
|
2010-02-08 15:08:40 +01:00
|
|
|
fd.merge(font_desc, True)
|
|
|
|
return fd.to_string()
|
2008-12-03 22:56:12 +01:00
|
|
|
|
2005-08-12 00:06:26 +02:00
|
|
|
def get_default_font():
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Get the desktop setting for application font first check for GNOME, then
|
|
|
|
Xfce and last KDE it returns None on failure or else a string 'Font Size'
|
|
|
|
"""
|
|
|
|
try:
|
2012-12-26 23:08:26 +01:00
|
|
|
from gi.repository import GConf
|
|
|
|
client = GConf.Client.get_default()
|
2010-08-06 14:01:56 +02:00
|
|
|
value = client.get_string("/desktop/gnome/interface/font_name")
|
|
|
|
return value.decode("utf8")
|
2012-12-26 23:08:26 +01:00
|
|
|
except ImportError:
|
2010-02-08 15:08:40 +01:00
|
|
|
pass
|
|
|
|
|
|
|
|
# try to get Xfce default font
|
|
|
|
# Xfce 4.2 and higher follow freedesktop.org's Base Directory Specification
|
|
|
|
# see http://www.xfce.org/~benny/xfce/file-locations.html
|
|
|
|
# and http://freedesktop.org/Standards/basedir-spec
|
|
|
|
xdg_config_home = os.environ.get('XDG_CONFIG_HOME', '')
|
|
|
|
if xdg_config_home == '':
|
|
|
|
xdg_config_home = os.path.expanduser('~/.config') # default
|
2010-02-10 16:17:09 +01:00
|
|
|
xfce_config_file = os.path.join(xdg_config_home,
|
2012-12-23 16:23:43 +01:00
|
|
|
'xfce4/mcs_settings/Gtk.xml')
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
kde_config_file = os.path.expanduser('~/.kde/share/config/kdeglobals')
|
|
|
|
|
|
|
|
if os.path.exists(xfce_config_file):
|
|
|
|
try:
|
|
|
|
for line in open(xfce_config_file):
|
|
|
|
if line.find('name="Gtk/FontName"') != -1:
|
|
|
|
start = line.find('value="') + 7
|
2013-01-01 19:44:25 +01:00
|
|
|
return line[start:line.find('"', start)]
|
2010-02-08 15:08:40 +01:00
|
|
|
except Exception:
|
|
|
|
#we talk about file
|
2013-01-01 19:36:56 +01:00
|
|
|
print(_('Error: cannot open %s for reading') % xfce_config_file,
|
|
|
|
file=sys.stderr)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
elif os.path.exists(kde_config_file):
|
|
|
|
try:
|
|
|
|
for line in open(kde_config_file):
|
|
|
|
if line.find('font=') == 0: # font=Verdana,9,other_numbers
|
|
|
|
start = 5 # 5 is len('font=')
|
|
|
|
line = line[start:]
|
|
|
|
values = line.split(',')
|
|
|
|
font_name = values[0]
|
|
|
|
font_size = values[1]
|
|
|
|
font_string = '%s %s' % (font_name, font_size) # Verdana 9
|
2013-01-01 19:44:25 +01:00
|
|
|
return font_string
|
2010-02-08 15:08:40 +01:00
|
|
|
except Exception:
|
|
|
|
#we talk about file
|
2013-01-01 19:36:56 +01:00
|
|
|
print(_('Error: cannot open %s for reading') % kde_config_file,
|
|
|
|
file=sys.stderr)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
return None
|
2008-12-03 22:56:12 +01:00
|
|
|
|
2006-11-18 21:52:28 +01:00
|
|
|
def get_running_processes():
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Return running processes or None (if /proc does not exist)
|
|
|
|
"""
|
|
|
|
if os.path.isdir('/proc'):
|
|
|
|
# under Linux: checking if 'gnome-session' or
|
|
|
|
# 'startkde' programs were run before gajim, by
|
|
|
|
# checking /proc (if it exists)
|
|
|
|
#
|
|
|
|
# if something is unclear, read `man proc`;
|
|
|
|
# if /proc exists, directories that have only numbers
|
|
|
|
# in their names contain data about processes.
|
|
|
|
# /proc/[xxx]/exe is a symlink to executable started
|
|
|
|
# as process number [xxx].
|
|
|
|
# filter out everything that we are not interested in:
|
|
|
|
files = os.listdir('/proc')
|
|
|
|
|
|
|
|
# files that doesn't have only digits in names...
|
2010-02-10 16:17:09 +01:00
|
|
|
files = [f for f in files if f.isdigit()]
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# files that aren't directories...
|
|
|
|
files = [f for f in files if os.path.isdir('/proc/' + f)]
|
|
|
|
|
|
|
|
# processes owned by somebody not running gajim...
|
|
|
|
# (we check if we have access to that file)
|
|
|
|
files = [f for f in files if os.access('/proc/' + f +'/exe', os.F_OK)]
|
|
|
|
|
|
|
|
# be sure that /proc/[number]/exe is really a symlink
|
|
|
|
# to avoid TBs in incorrectly configured systems
|
|
|
|
files = [f for f in files if os.path.islink('/proc/' + f + '/exe')]
|
|
|
|
|
|
|
|
# list of processes
|
2010-02-10 16:17:09 +01:00
|
|
|
processes = [os.path.basename(os.readlink('/proc/' + f +'/exe')) for f \
|
|
|
|
in files]
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
return processes
|
|
|
|
return []
|
2005-08-28 12:57:08 +02:00
|
|
|
|
|
|
|
def move_window(window, x, y):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Move the window, but also check if out of screen
|
|
|
|
"""
|
|
|
|
if x < 0:
|
|
|
|
x = 0
|
|
|
|
if y < 0:
|
|
|
|
y = 0
|
|
|
|
w, h = window.get_size()
|
|
|
|
if x + w > screen_w:
|
|
|
|
x = screen_w - w
|
|
|
|
if y + h > screen_h:
|
|
|
|
y = screen_h - h
|
|
|
|
window.move(x, y)
|
2005-08-28 12:57:08 +02:00
|
|
|
|
|
|
|
def resize_window(window, w, h):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Resize window, but also checks if huge window or negative values
|
|
|
|
"""
|
|
|
|
if not w or not h:
|
|
|
|
return
|
|
|
|
if w > screen_w:
|
|
|
|
w = screen_w
|
|
|
|
if h > screen_h:
|
|
|
|
h = screen_h
|
|
|
|
window.resize(abs(w), abs(h))
|
2005-08-29 22:20:08 +02:00
|
|
|
|
2006-12-23 00:30:23 +01:00
|
|
|
class HashDigest:
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self, algo, digest):
|
|
|
|
self.algo = self.cleanID(algo)
|
|
|
|
self.digest = self.cleanID(digest)
|
|
|
|
|
|
|
|
def cleanID(self, id_):
|
|
|
|
id_ = id_.strip().lower()
|
2010-02-10 16:17:09 +01:00
|
|
|
for strip in (' :.-_'):
|
|
|
|
id_ = id_.replace(strip, '')
|
2010-02-08 15:08:40 +01:00
|
|
|
return id_
|
|
|
|
|
|
|
|
def __eq__(self, other):
|
|
|
|
sa, sd = self.algo, self.digest
|
|
|
|
if isinstance(other, self.__class__):
|
|
|
|
oa, od = other.algo, other.digest
|
|
|
|
elif isinstance(other, basestring):
|
|
|
|
sa, oa, od = None, None, self.cleanID(other)
|
|
|
|
elif isinstance(other, tuple) and len(other) == 2:
|
|
|
|
oa, od = self.cleanID(other[0]), self.cleanID(other[1])
|
|
|
|
else:
|
|
|
|
return False
|
|
|
|
|
|
|
|
return sa == oa and sd == od
|
|
|
|
|
|
|
|
def __ne__(self, other):
|
|
|
|
return not self == other
|
|
|
|
|
|
|
|
def __hash__(self):
|
|
|
|
return self.algo ^ self.digest
|
|
|
|
|
|
|
|
def __str__(self):
|
|
|
|
prettydigest = ''
|
2013-01-02 18:17:51 +01:00
|
|
|
for i in list(range(0, len(self.digest), 2)):
|
2010-02-08 15:08:40 +01:00
|
|
|
prettydigest += self.digest[i:i + 2] + ':'
|
|
|
|
return prettydigest[:-1]
|
|
|
|
|
|
|
|
def __repr__(self):
|
|
|
|
return "%s(%s, %s)" % (self.__class__, repr(self.algo), repr(str(self)))
|
2006-12-23 00:30:23 +01:00
|
|
|
|
|
|
|
class ServersXMLHandler(xml.sax.ContentHandler):
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self):
|
|
|
|
xml.sax.ContentHandler.__init__(self)
|
|
|
|
self.servers = []
|
|
|
|
|
|
|
|
def startElement(self, name, attributes):
|
|
|
|
if name == 'item':
|
2010-10-05 10:51:15 +02:00
|
|
|
if 'jid' in attributes.getNames():
|
|
|
|
self.servers.append(attributes.getValue('jid'))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
def endElement(self, name):
|
|
|
|
pass
|
2005-09-18 17:03:04 +02:00
|
|
|
|
|
|
|
def parse_server_xml(path_to_file):
|
2010-02-08 15:08:40 +01:00
|
|
|
try:
|
|
|
|
handler = ServersXMLHandler()
|
|
|
|
xml.sax.parse(path_to_file, handler)
|
|
|
|
return handler.servers
|
|
|
|
# handle exception if unable to open file
|
2013-01-01 23:18:36 +01:00
|
|
|
except IOError as message:
|
2013-01-05 00:03:36 +01:00
|
|
|
print(_('Error reading file:') + str(message), file=sys.stderr)
|
2010-02-08 15:08:40 +01:00
|
|
|
# handle exception parsing file
|
2013-01-01 23:18:36 +01:00
|
|
|
except xml.sax.SAXParseException as message:
|
2013-01-05 00:03:36 +01:00
|
|
|
print(_('Error parsing file:') + str(message), file=sys.stderr)
|
2005-09-18 17:03:04 +02:00
|
|
|
|
2005-09-25 20:37:50 +02:00
|
|
|
def set_unset_urgency_hint(window, unread_messages_no):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Sets/unset urgency hint in window argument depending if we have unread
|
|
|
|
messages or not
|
|
|
|
"""
|
|
|
|
if gajim.config.get('use_urgency_hint'):
|
|
|
|
if unread_messages_no > 0:
|
|
|
|
window.props.urgency_hint = True
|
|
|
|
else:
|
|
|
|
window.props.urgency_hint = False
|
2005-09-07 23:12:30 +02:00
|
|
|
|
|
|
|
def get_abspath_for_script(scriptname, want_type = False):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Check if we are svn or normal user and return abspath to asked script if
|
|
|
|
want_type is True we return 'svn' or 'install'
|
|
|
|
"""
|
|
|
|
if os.path.isdir('.svn'): # we are svn user
|
|
|
|
type_ = 'svn'
|
|
|
|
cwd = os.getcwd() # it's always ending with src
|
|
|
|
|
|
|
|
if scriptname == 'gajim-remote':
|
|
|
|
path_to_script = cwd + '/gajim-remote.py'
|
|
|
|
|
|
|
|
elif scriptname == 'gajim':
|
|
|
|
script = '#!/bin/sh\n' # the script we may create
|
|
|
|
script += 'cd %s' % cwd
|
|
|
|
path_to_script = cwd + '/../scripts/gajim_sm_script'
|
|
|
|
|
|
|
|
try:
|
|
|
|
if os.path.exists(path_to_script):
|
|
|
|
os.remove(path_to_script)
|
|
|
|
|
|
|
|
f = open(path_to_script, 'w')
|
|
|
|
script += '\nexec python -OOt gajim.py $0 $@\n'
|
|
|
|
f.write(script)
|
|
|
|
f.close()
|
2013-01-02 13:54:02 +01:00
|
|
|
os.chmod(path_to_script, 0o700)
|
2010-02-08 15:08:40 +01:00
|
|
|
except OSError: # do not traceback (could be a permission problem)
|
|
|
|
#we talk about a file here
|
2010-02-10 16:17:09 +01:00
|
|
|
s = _('Could not write to %s. Session Management support will '
|
|
|
|
'not work') % path_to_script
|
2013-01-01 19:36:56 +01:00
|
|
|
print(s, file=sys.stderr)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
else: # normal user (not svn user)
|
|
|
|
type_ = 'install'
|
|
|
|
# always make it like '/usr/local/bin/gajim'
|
|
|
|
path_to_script = helpers.is_in_path(scriptname, True)
|
|
|
|
|
|
|
|
|
|
|
|
if want_type:
|
|
|
|
return path_to_script, type_
|
|
|
|
else:
|
|
|
|
return path_to_script
|
2005-10-03 20:19:31 +02:00
|
|
|
|
2006-01-14 14:50:35 +01:00
|
|
|
def get_pixbuf_from_data(file_data, want_type = False):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2012-12-23 16:23:43 +01:00
|
|
|
Get image data and returns GdkPixbuf.Pixbuf if want_type is True it also
|
2010-02-08 15:08:40 +01:00
|
|
|
returns 'jpeg', 'png' etc
|
|
|
|
"""
|
2012-12-23 16:23:43 +01:00
|
|
|
pixbufloader = GdkPixbuf.PixbufLoader()
|
2010-02-08 15:08:40 +01:00
|
|
|
try:
|
|
|
|
pixbufloader.write(file_data)
|
|
|
|
pixbufloader.close()
|
|
|
|
pixbuf = pixbufloader.get_pixbuf()
|
2012-12-23 16:23:43 +01:00
|
|
|
except GObject.GError: # 'unknown image format'
|
2010-02-08 15:08:40 +01:00
|
|
|
pixbufloader.close()
|
|
|
|
pixbuf = None
|
|
|
|
if want_type:
|
|
|
|
return None, None
|
|
|
|
else:
|
|
|
|
return None
|
|
|
|
|
|
|
|
if want_type:
|
2012-12-31 16:38:32 +01:00
|
|
|
typ = pixbufloader.get_format().get_name()
|
2010-02-08 15:08:40 +01:00
|
|
|
return pixbuf, typ
|
|
|
|
else:
|
|
|
|
return pixbuf
|
2005-10-16 16:19:02 +02:00
|
|
|
|
|
|
|
def get_invisible_cursor():
|
2013-01-04 13:40:18 +01:00
|
|
|
import cairo
|
|
|
|
s = cairo.ImageSurface(cairo.FORMAT_A1, 1, 1)
|
|
|
|
cursor_pixbuf = Gdk.pixbuf_get_from_surface(s, 0, 0, 1, 1)
|
|
|
|
cursor = Gdk.Cursor.new_from_pixbuf(Gdk.Display.get_default(), \
|
|
|
|
cursor_pixbuf, 0, 0)
|
2010-02-08 15:08:40 +01:00
|
|
|
return cursor
|
2005-10-31 19:06:36 +01:00
|
|
|
|
|
|
|
def get_current_desktop(window):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Return the current virtual desktop for given window
|
2009-11-25 18:52:56 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
NOTE: Window is a GDK window.
|
|
|
|
"""
|
|
|
|
prop = window.property_get('_NET_CURRENT_DESKTOP')
|
|
|
|
if prop is None: # it means it's normal window (not root window)
|
|
|
|
# so we look for it's current virtual desktop in another property
|
|
|
|
prop = window.property_get('_NET_WM_DESKTOP')
|
2005-10-31 19:06:36 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
if prop is not None:
|
|
|
|
# f.e. prop is ('CARDINAL', 32, [0]) we want 0 or 1.. from [0]
|
|
|
|
current_virtual_desktop_no = prop[2][0]
|
|
|
|
return current_virtual_desktop_no
|
2005-10-31 20:35:17 +01:00
|
|
|
|
|
|
|
def possibly_move_window_in_current_desktop(window):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Moves GTK window to current virtual desktop if it is not in the current
|
|
|
|
virtual desktop
|
|
|
|
|
|
|
|
NOTE: Window is a GDK window.
|
|
|
|
"""
|
2012-12-23 16:23:43 +01:00
|
|
|
#TODO: property_get doesn't work:
|
|
|
|
#prop_atom = Gdk.Atom.intern('_NET_CURRENT_DESKTOP', False)
|
|
|
|
#type_atom = Gdk.Atom.intern("CARDINAL", False)
|
|
|
|
#w = Gdk.Screen.get_default().get_root_window()
|
|
|
|
#Gdk.property_get(w, prop_atom, type_atom, 0, 9999, False)
|
|
|
|
return False
|
2010-02-08 15:08:40 +01:00
|
|
|
if os.name == 'nt':
|
|
|
|
return False
|
|
|
|
|
2012-12-23 16:23:43 +01:00
|
|
|
root_window = Gdk.Screen.get_default().get_root_window()
|
2010-02-08 15:08:40 +01:00
|
|
|
# current user's vd
|
|
|
|
current_virtual_desktop_no = get_current_desktop(root_window)
|
|
|
|
|
|
|
|
# vd roster window is in
|
|
|
|
window_virtual_desktop = get_current_desktop(window.window)
|
|
|
|
|
|
|
|
# if one of those is None, something went wrong and we cannot know
|
|
|
|
# VD info, just hide it (default action) and not show it afterwards
|
|
|
|
if None not in (window_virtual_desktop, current_virtual_desktop_no):
|
|
|
|
if current_virtual_desktop_no != window_virtual_desktop:
|
|
|
|
# we are in another VD that the window was
|
|
|
|
# so show it in current VD
|
|
|
|
window.present()
|
|
|
|
return True
|
|
|
|
return False
|
2005-11-01 21:01:14 +01:00
|
|
|
|
|
|
|
def file_is_locked(path_to_file):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Return True if file is locked
|
|
|
|
|
|
|
|
NOTE: Windows only.
|
|
|
|
"""
|
|
|
|
if os.name != 'nt': # just in case
|
|
|
|
return
|
|
|
|
|
|
|
|
if not HAS_PYWIN32:
|
|
|
|
return
|
|
|
|
|
|
|
|
secur_att = pywintypes.SECURITY_ATTRIBUTES()
|
|
|
|
secur_att.Initialize()
|
|
|
|
|
|
|
|
try:
|
|
|
|
# try make a handle for READING the file
|
|
|
|
hfile = win32file.CreateFile(
|
2010-02-10 16:17:09 +01:00
|
|
|
path_to_file, # path to file
|
|
|
|
win32con.GENERIC_READ, # open for reading
|
|
|
|
0, # do not share with other proc
|
2010-02-08 15:08:40 +01:00
|
|
|
secur_att,
|
2010-02-10 16:17:09 +01:00
|
|
|
win32con.OPEN_EXISTING, # existing file only
|
2010-02-08 15:08:40 +01:00
|
|
|
win32con.FILE_ATTRIBUTE_NORMAL, # normal file
|
2010-02-10 16:17:09 +01:00
|
|
|
0 # no attr. template
|
2010-02-08 15:08:40 +01:00
|
|
|
)
|
|
|
|
except pywintypes.error:
|
|
|
|
return True
|
|
|
|
else: # in case all went ok, close file handle (go to hell WinAPI)
|
|
|
|
hfile.Close()
|
|
|
|
return False
|
2005-11-07 12:07:55 +01:00
|
|
|
|
2010-02-17 11:32:55 +01:00
|
|
|
def get_fade_color(treeview, selected, focused):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2013-01-05 15:53:14 +01:00
|
|
|
Get a gdk RGBA color that is between foreground and background in 0.3
|
2010-02-08 15:08:40 +01:00
|
|
|
0.7 respectively colors of the cell for the given treeview
|
|
|
|
"""
|
2013-01-05 15:53:14 +01:00
|
|
|
context = treeview.get_style_context()
|
2010-02-08 15:08:40 +01:00
|
|
|
if selected:
|
|
|
|
if focused: # is the window focused?
|
2013-01-05 15:53:14 +01:00
|
|
|
state = Gtk.StateFlags.SELECTED
|
2010-02-08 15:08:40 +01:00
|
|
|
else: # is it not? NOTE: many gtk themes change bg on this
|
2013-01-05 15:53:14 +01:00
|
|
|
state = Gtk.StateFlags.ACTIVE
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
2013-01-05 15:53:14 +01:00
|
|
|
state = Gtk.StateFlags.NORMAL
|
|
|
|
bg = context.get_background_color(state)
|
|
|
|
fg = context.get_color(state)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
p = 0.3 # background
|
|
|
|
q = 0.7 # foreground # p + q should do 1.0
|
2013-01-05 15:53:14 +01:00
|
|
|
return Gdk.RGBA(bg.red*p + fg.red*q, bg.green*p + fg.green*q,
|
|
|
|
bg.blue*p + fg.blue*q)
|
2005-11-08 14:04:59 +01:00
|
|
|
|
2006-02-09 17:47:31 +01:00
|
|
|
def get_scaled_pixbuf(pixbuf, kind):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Return scaled pixbuf, keeping ratio etc or None kind is either "chat",
|
|
|
|
"roster", "notification", "tooltip", "vcard"
|
|
|
|
"""
|
|
|
|
# resize to a width / height for the avatar not to have distortion
|
|
|
|
# (keep aspect ratio)
|
|
|
|
width = gajim.config.get(kind + '_avatar_width')
|
|
|
|
height = gajim.config.get(kind + '_avatar_height')
|
|
|
|
if width < 1 or height < 1:
|
|
|
|
return None
|
|
|
|
|
|
|
|
# Pixbuf size
|
|
|
|
pix_width = pixbuf.get_width()
|
|
|
|
pix_height = pixbuf.get_height()
|
|
|
|
# don't make avatars bigger than they are
|
|
|
|
if pix_width < width and pix_height < height:
|
|
|
|
return pixbuf # we don't want to make avatar bigger
|
|
|
|
|
|
|
|
ratio = float(pix_width) / float(pix_height)
|
|
|
|
if ratio > 1:
|
|
|
|
w = width
|
|
|
|
h = int(w / ratio)
|
|
|
|
else:
|
|
|
|
h = height
|
|
|
|
w = int(h * ratio)
|
2012-12-23 16:23:43 +01:00
|
|
|
scaled_buf = pixbuf.scale_simple(w, h, GdkPixbuf.InterpType.HYPER)
|
2010-02-08 15:08:40 +01:00
|
|
|
return scaled_buf
|
2005-11-12 22:24:54 +01:00
|
|
|
|
2009-11-29 23:06:49 +01:00
|
|
|
def get_avatar_pixbuf_from_cache(fjid, use_local=True):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Check if jid has cached avatar and if that avatar is valid image (can be
|
|
|
|
shown)
|
|
|
|
|
|
|
|
Returns None if there is no image in vcard/
|
|
|
|
Returns 'ask' if cached vcard should not be used (user changed his vcard, so
|
|
|
|
we have new sha) or if we don't have the vcard
|
|
|
|
"""
|
|
|
|
jid, nick = gajim.get_room_and_nick_from_fjid(fjid)
|
|
|
|
if gajim.config.get('hide_avatar_of_transport') and\
|
|
|
|
gajim.jid_is_transport(jid):
|
|
|
|
# don't show avatar for the transport itself
|
|
|
|
return None
|
|
|
|
|
2010-03-09 22:37:57 +01:00
|
|
|
if any(jid in gajim.contacts.get_gc_list(acc) for acc in \
|
|
|
|
gajim.contacts.get_accounts()):
|
2010-02-08 15:08:40 +01:00
|
|
|
is_groupchat_contact = True
|
|
|
|
else:
|
|
|
|
is_groupchat_contact = False
|
|
|
|
|
|
|
|
puny_jid = helpers.sanitize_filename(jid)
|
|
|
|
if is_groupchat_contact:
|
|
|
|
puny_nick = helpers.sanitize_filename(nick)
|
|
|
|
path = os.path.join(gajim.VCARD_PATH, puny_jid, puny_nick)
|
|
|
|
local_avatar_basepath = os.path.join(gajim.AVATAR_PATH, puny_jid,
|
|
|
|
puny_nick) + '_local'
|
|
|
|
else:
|
|
|
|
path = os.path.join(gajim.VCARD_PATH, puny_jid)
|
|
|
|
local_avatar_basepath = os.path.join(gajim.AVATAR_PATH, puny_jid) + \
|
|
|
|
'_local'
|
|
|
|
if use_local:
|
|
|
|
for extension in ('.png', '.jpeg'):
|
|
|
|
local_avatar_path = local_avatar_basepath + extension
|
|
|
|
if os.path.isfile(local_avatar_path):
|
|
|
|
avatar_file = open(local_avatar_path, 'rb')
|
|
|
|
avatar_data = avatar_file.read()
|
|
|
|
avatar_file.close()
|
|
|
|
return get_pixbuf_from_data(avatar_data)
|
|
|
|
|
|
|
|
if not os.path.isfile(path):
|
|
|
|
return 'ask'
|
|
|
|
|
2013-01-02 13:54:02 +01:00
|
|
|
vcard_dict = list(gajim.connections.values())[0].get_cached_vcard(fjid,
|
2010-02-08 15:08:40 +01:00
|
|
|
is_groupchat_contact)
|
|
|
|
if not vcard_dict: # This can happen if cached vcard is too old
|
|
|
|
return 'ask'
|
|
|
|
if 'PHOTO' not in vcard_dict:
|
|
|
|
return None
|
|
|
|
pixbuf = vcard.get_avatar_pixbuf_encoded_mime(vcard_dict['PHOTO'])[0]
|
|
|
|
return pixbuf
|
2005-11-20 22:58:08 +01:00
|
|
|
|
|
|
|
def make_gtk_month_python_month(month):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
GTK starts counting months from 0, so January is 0 but Python's time start
|
|
|
|
from 1, so align to Python
|
2009-11-25 18:52:56 +01:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
NOTE: Month MUST be an integer.
|
|
|
|
"""
|
|
|
|
return month + 1
|
2005-11-24 02:39:47 +01:00
|
|
|
|
|
|
|
def make_python_month_gtk_month(month):
|
2010-02-08 15:08:40 +01:00
|
|
|
return month - 1
|
2005-12-01 19:16:30 +01:00
|
|
|
|
|
|
|
def make_color_string(color):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Create #aabbcc color string from gtk color
|
|
|
|
"""
|
|
|
|
col = '#'
|
|
|
|
for i in ('red', 'green', 'blue'):
|
2013-01-06 00:57:53 +01:00
|
|
|
h = hex(int(getattr(color, i) / (16*16)))
|
|
|
|
h = h.split('x')[1]
|
2010-02-08 15:08:40 +01:00
|
|
|
if len(h) == 1:
|
|
|
|
h = '0' + h
|
|
|
|
col += h
|
|
|
|
return col
|
2006-01-12 23:48:49 +01:00
|
|
|
|
|
|
|
def make_pixbuf_grayscale(pixbuf):
|
2010-02-08 15:08:40 +01:00
|
|
|
pixbuf2 = pixbuf.copy()
|
|
|
|
pixbuf.saturate_and_pixelate(pixbuf2, 0.0, False)
|
|
|
|
return pixbuf2
|
2006-01-18 20:50:16 +01:00
|
|
|
|
|
|
|
def decode_filechooser_file_paths(file_paths):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Decode as UTF-8 under Windows and ask sys.getfilesystemencoding() in POSIX
|
|
|
|
file_paths MUST be LIST
|
|
|
|
"""
|
|
|
|
file_paths_list = list()
|
|
|
|
|
|
|
|
if os.name == 'nt': # decode as UTF-8 under Windows
|
|
|
|
for file_path in file_paths:
|
|
|
|
file_path = file_path.decode('utf8')
|
|
|
|
file_paths_list.append(file_path)
|
|
|
|
else:
|
|
|
|
for file_path in file_paths:
|
|
|
|
try:
|
|
|
|
file_path = file_path.decode(sys.getfilesystemencoding())
|
|
|
|
except Exception:
|
|
|
|
try:
|
2013-01-01 19:44:25 +01:00
|
|
|
file_path = file_path
|
2010-02-08 15:08:40 +01:00
|
|
|
except Exception:
|
|
|
|
pass
|
|
|
|
file_paths_list.append(file_path)
|
|
|
|
|
|
|
|
return file_paths_list
|
2006-01-20 18:36:22 +01:00
|
|
|
|
|
|
|
def possibly_set_gajim_as_xmpp_handler():
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Register (by default only the first time) 'xmmp:' to Gajim
|
|
|
|
"""
|
|
|
|
path_to_dot_kde = os.path.expanduser('~/.kde')
|
|
|
|
if os.path.exists(path_to_dot_kde):
|
|
|
|
path_to_kde_file = os.path.join(path_to_dot_kde,
|
|
|
|
'share/services/xmpp.protocol')
|
|
|
|
else:
|
|
|
|
path_to_kde_file = None
|
|
|
|
|
|
|
|
def set_gajim_as_xmpp_handler(is_checked=None):
|
|
|
|
if is_checked is not None:
|
|
|
|
# come from confirmation dialog
|
|
|
|
gajim.config.set('check_if_gajim_is_default', is_checked)
|
|
|
|
path_to_gajim_script, typ = get_abspath_for_script('gajim-remote', True)
|
|
|
|
if path_to_gajim_script:
|
|
|
|
if typ == 'svn':
|
|
|
|
command = path_to_gajim_script + ' handle_uri %s'
|
|
|
|
else: # 'installed'
|
|
|
|
command = 'gajim-remote handle_uri %s'
|
|
|
|
|
|
|
|
# setting for GNOME/Gconf
|
|
|
|
client.set_bool('/desktop/gnome/url-handlers/xmpp/enabled', True)
|
2010-02-10 16:17:09 +01:00
|
|
|
client.set_string('/desktop/gnome/url-handlers/xmpp/command',
|
|
|
|
command)
|
|
|
|
client.set_bool('/desktop/gnome/url-handlers/xmpp/needs_terminal',
|
|
|
|
False)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
# setting for KDE
|
|
|
|
if path_to_kde_file is not None: # user has run kde at least once
|
|
|
|
try:
|
|
|
|
f = open(path_to_kde_file, 'a')
|
|
|
|
f.write('''\
|
2006-01-21 22:23:30 +01:00
|
|
|
[Protocol]
|
2006-01-20 18:36:22 +01:00
|
|
|
exec=%s "%%u"
|
|
|
|
protocol=xmpp
|
|
|
|
input=none
|
|
|
|
output=none
|
|
|
|
helper=true
|
|
|
|
listing=false
|
|
|
|
reading=false
|
|
|
|
writing=false
|
|
|
|
makedir=false
|
|
|
|
deleting=false
|
|
|
|
icon=gajim
|
|
|
|
Description=xmpp
|
|
|
|
''' % command)
|
2010-02-08 15:08:40 +01:00
|
|
|
f.close()
|
|
|
|
except IOError:
|
2010-02-10 16:17:09 +01:00
|
|
|
log.debug("I/O Error writing settings to %s",
|
|
|
|
repr(path_to_kde_file), exc_info=True)
|
2010-02-08 15:08:40 +01:00
|
|
|
else: # no gajim remote, stop ask user everytime
|
|
|
|
gajim.config.set('check_if_gajim_is_default', False)
|
|
|
|
|
|
|
|
try:
|
2012-12-23 16:23:43 +01:00
|
|
|
from gi.repository import GConf
|
2010-02-08 15:08:40 +01:00
|
|
|
# in try because daemon may not be there
|
2012-12-23 16:23:43 +01:00
|
|
|
client = GConf.Client.get_default()
|
2010-02-08 15:08:40 +01:00
|
|
|
except Exception:
|
|
|
|
return
|
|
|
|
|
|
|
|
old_command = client.get_string('/desktop/gnome/url-handlers/xmpp/command')
|
|
|
|
if not old_command or old_command.endswith(' open_chat %s'):
|
|
|
|
# first time (GNOME/GCONF) or old Gajim version
|
|
|
|
we_set = True
|
|
|
|
elif path_to_kde_file is not None and not os.path.exists(path_to_kde_file):
|
|
|
|
# only the first time (KDE)
|
|
|
|
we_set = True
|
|
|
|
else:
|
|
|
|
we_set = False
|
|
|
|
|
|
|
|
if we_set:
|
|
|
|
set_gajim_as_xmpp_handler()
|
|
|
|
elif old_command and not old_command.endswith(' handle_uri %s'):
|
|
|
|
# xmpp: is currently handled by another program, so ask the user
|
|
|
|
pritext = _('Gajim is not the default Jabber client')
|
|
|
|
sectext = _('Would you like to make Gajim the default Jabber client?')
|
2010-02-10 16:17:09 +01:00
|
|
|
checktext = _('Always check to see if Gajim is the default Jabber '
|
|
|
|
'client on startup')
|
2010-02-08 15:08:40 +01:00
|
|
|
def on_cancel(checked):
|
|
|
|
gajim.config.set('check_if_gajim_is_default', checked)
|
|
|
|
dlg = dialogs.ConfirmationDialogCheck(pritext, sectext, checktext,
|
2010-02-10 16:17:09 +01:00
|
|
|
set_gajim_as_xmpp_handler, on_cancel)
|
2010-02-08 15:08:40 +01:00
|
|
|
if gajim.config.get('check_if_gajim_is_default'):
|
|
|
|
dlg.checkbutton.set_active(True)
|
2006-02-08 06:13:26 +01:00
|
|
|
|
|
|
|
def escape_underscore(s):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Escape underlines to prevent them from being interpreted as keyboard
|
|
|
|
accelerators
|
|
|
|
"""
|
|
|
|
return s.replace('_', '__')
|
2006-02-26 15:33:44 +01:00
|
|
|
|
|
|
|
def get_state_image_from_file_path_show(file_path, show):
|
2010-02-08 15:08:40 +01:00
|
|
|
state_file = show.replace(' ', '_')
|
|
|
|
files = []
|
|
|
|
files.append(os.path.join(file_path, state_file + '.png'))
|
|
|
|
files.append(os.path.join(file_path, state_file + '.gif'))
|
2012-12-23 16:23:43 +01:00
|
|
|
image = Gtk.Image()
|
2010-02-08 15:08:40 +01:00
|
|
|
image.set_from_pixbuf(None)
|
|
|
|
for file_ in files:
|
|
|
|
if os.path.exists(file_):
|
|
|
|
image.set_from_file(file_)
|
|
|
|
break
|
|
|
|
|
|
|
|
return image
|
2006-03-29 18:39:14 +02:00
|
|
|
|
2006-03-29 18:46:12 +02:00
|
|
|
def get_possible_button_event(event):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Mouse or keyboard caused the event?
|
|
|
|
"""
|
2012-12-27 21:58:52 +01:00
|
|
|
if event.type == Gdk.EventType.KEY_PRESS:
|
2010-02-08 15:08:40 +01:00
|
|
|
return 0 # no event.button so pass 0
|
|
|
|
# BUTTON_PRESS event, so pass event.button
|
|
|
|
return event.button
|
2006-04-07 17:51:17 +02:00
|
|
|
|
|
|
|
def destroy_widget(widget):
|
2010-02-08 15:08:40 +01:00
|
|
|
widget.destroy()
|
2006-05-26 15:19:52 +02:00
|
|
|
|
2010-02-10 16:17:09 +01:00
|
|
|
def on_avatar_save_as_menuitem_activate(widget, jid, default_name=''):
|
2010-02-08 15:08:40 +01:00
|
|
|
def on_continue(response, file_path):
|
|
|
|
if response < 0:
|
|
|
|
return
|
|
|
|
pixbuf = get_avatar_pixbuf_from_cache(jid)
|
2010-02-10 16:17:09 +01:00
|
|
|
extension = os.path.splitext(file_path)[1]
|
2010-02-08 15:08:40 +01:00
|
|
|
if not extension:
|
|
|
|
# Silently save as Jpeg image
|
|
|
|
image_format = 'jpeg'
|
|
|
|
file_path += '.jpeg'
|
|
|
|
elif extension == 'jpg':
|
|
|
|
image_format = 'jpeg'
|
|
|
|
else:
|
|
|
|
image_format = extension[1:] # remove leading dot
|
|
|
|
|
|
|
|
# Save image
|
|
|
|
try:
|
2012-12-31 16:38:32 +01:00
|
|
|
pixbuf.savev(file_path, image_format, [], [])
|
2013-01-01 23:18:36 +01:00
|
|
|
except Exception as e:
|
2010-02-08 15:08:40 +01:00
|
|
|
log.debug('Error saving avatar: %s' % str(e))
|
|
|
|
if os.path.exists(file_path):
|
|
|
|
os.remove(file_path)
|
|
|
|
new_file_path = '.'.join(file_path.split('.')[:-1]) + '.jpeg'
|
|
|
|
def on_ok(file_path, pixbuf):
|
2012-12-31 16:38:32 +01:00
|
|
|
pixbuf.savev(file_path, 'jpeg', [], [])
|
2010-02-08 15:08:40 +01:00
|
|
|
dialogs.ConfirmationDialog(_('Extension not supported'),
|
2010-02-10 16:17:09 +01:00
|
|
|
_('Image cannot be saved in %(type)s format. Save as '
|
|
|
|
'%(new_filename)s?') % {'type': image_format,
|
|
|
|
'new_filename': new_file_path},
|
|
|
|
on_response_ok = (on_ok, new_file_path, pixbuf))
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
dialog.destroy()
|
|
|
|
|
|
|
|
def on_ok(widget):
|
|
|
|
file_path = dialog.get_filename()
|
|
|
|
file_path = decode_filechooser_file_paths((file_path,))[0]
|
|
|
|
if os.path.exists(file_path):
|
|
|
|
# check if we have write permissions
|
|
|
|
if not os.access(file_path, os.W_OK):
|
|
|
|
file_name = os.path.basename(file_path)
|
2010-02-10 16:17:09 +01:00
|
|
|
dialogs.ErrorDialog(_('Cannot overwrite existing file "%s"') % \
|
|
|
|
file_name, _('A file with this name already exists and you '
|
|
|
|
'do not have permission to overwrite it.'))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
dialog2 = dialogs.FTOverwriteConfirmationDialog(
|
2010-02-10 16:17:09 +01:00
|
|
|
_('This file already exists'), _('What do you want to do?'),
|
|
|
|
propose_resume=False, on_response=(on_continue, file_path))
|
2010-02-08 15:08:40 +01:00
|
|
|
dialog2.set_transient_for(dialog)
|
|
|
|
dialog2.set_destroy_with_parent(True)
|
|
|
|
else:
|
|
|
|
dirname = os.path.dirname(file_path)
|
|
|
|
if not os.access(dirname, os.W_OK):
|
|
|
|
dialogs.ErrorDialog(_('Directory "%s" is not writable') % \
|
2010-02-10 16:17:09 +01:00
|
|
|
dirname, _('You do not have permission to create files in '
|
|
|
|
'this directory.'))
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
|
|
|
|
on_continue(0, file_path)
|
|
|
|
|
|
|
|
def on_cancel(widget):
|
|
|
|
dialog.destroy()
|
|
|
|
|
|
|
|
dialog = dialogs.FileChooserDialog(title_text=_('Save Image as...'),
|
2012-12-23 16:23:43 +01:00
|
|
|
action=Gtk.FileChooserAction.SAVE, buttons=(Gtk.STOCK_CANCEL,
|
|
|
|
Gtk.ResponseType.CANCEL, Gtk.STOCK_SAVE, Gtk.ResponseType.OK),
|
|
|
|
default_response=Gtk.ResponseType.OK,
|
2010-02-10 16:17:09 +01:00
|
|
|
current_folder=gajim.config.get('last_save_dir'), on_response_ok=on_ok,
|
|
|
|
on_response_cancel=on_cancel)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
dialog.set_current_name(default_name + '.jpeg')
|
|
|
|
dialog.connect('delete-event', lambda widget, event:
|
2010-02-10 16:17:09 +01:00
|
|
|
on_cancel(widget))
|
2006-11-18 21:52:28 +01:00
|
|
|
|
|
|
|
def on_bm_header_changed_state(widget, event):
|
2012-12-23 16:23:43 +01:00
|
|
|
widget.set_state(Gtk.StateType.NORMAL) #do not allow selected_state
|
2007-03-20 15:09:27 +01:00
|
|
|
|
|
|
|
def create_combobox(value_list, selected_value = None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Value_list is [(label1, value1)]
|
|
|
|
"""
|
2012-12-23 16:23:43 +01:00
|
|
|
liststore = Gtk.ListStore(str, str)
|
2013-01-02 17:57:20 +01:00
|
|
|
combobox = Gtk.ComboBox.new_with_model(liststore)
|
2012-12-23 16:23:43 +01:00
|
|
|
cell = Gtk.CellRendererText()
|
2013-01-02 17:57:20 +01:00
|
|
|
combobox.pack_start(cell, True)
|
2010-02-08 15:08:40 +01:00
|
|
|
combobox.add_attribute(cell, 'text', 0)
|
|
|
|
i = -1
|
|
|
|
for value in value_list:
|
|
|
|
liststore.append(value)
|
|
|
|
if selected_value == value[1]:
|
|
|
|
i = value_list.index(value)
|
|
|
|
if i > -1:
|
|
|
|
combobox.set_active(i)
|
|
|
|
combobox.show_all()
|
|
|
|
return combobox
|
2008-04-17 16:17:14 +02:00
|
|
|
|
2009-08-31 21:45:52 +02:00
|
|
|
def create_list_multi(value_list, selected_values=None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Value_list is [(label1, value1)]
|
|
|
|
"""
|
2012-12-23 16:23:43 +01:00
|
|
|
liststore = Gtk.ListStore(str, str)
|
2013-01-02 17:57:20 +01:00
|
|
|
treeview = Gtk.TreeView.new_with_model(liststore)
|
2012-12-23 16:23:43 +01:00
|
|
|
treeview.get_selection().set_mode(Gtk.SelectionMode.MULTIPLE)
|
2010-02-08 15:08:40 +01:00
|
|
|
treeview.set_headers_visible(False)
|
2012-12-23 16:23:43 +01:00
|
|
|
col = Gtk.TreeViewColumn()
|
2010-02-08 15:08:40 +01:00
|
|
|
treeview.append_column(col)
|
2012-12-23 16:23:43 +01:00
|
|
|
cell = Gtk.CellRendererText()
|
|
|
|
col.pack_start(cell, True, True, 0)
|
2010-02-08 15:08:40 +01:00
|
|
|
col.set_attributes(cell, text=0)
|
|
|
|
for value in value_list:
|
|
|
|
iter = liststore.append(value)
|
|
|
|
if value[1] in selected_values:
|
|
|
|
treeview.get_selection().select_iter(iter)
|
|
|
|
treeview.show_all()
|
|
|
|
return treeview
|
2009-08-31 21:45:52 +02:00
|
|
|
|
|
|
|
def load_iconset(path, pixbuf2=None, transport=False):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Load full iconset from the given path, and add pixbuf2 on top left of each
|
|
|
|
static images
|
|
|
|
"""
|
|
|
|
path += '/'
|
|
|
|
if transport:
|
|
|
|
list_ = ('online', 'chat', 'away', 'xa', 'dnd', 'offline',
|
|
|
|
'not in roster')
|
|
|
|
else:
|
|
|
|
list_ = ('connecting', 'online', 'chat', 'away', 'xa', 'dnd',
|
|
|
|
'invisible', 'offline', 'error', 'requested', 'event', 'opened',
|
|
|
|
'closed', 'not in roster', 'muc_active', 'muc_inactive')
|
|
|
|
if pixbuf2:
|
|
|
|
list_ = ('connecting', 'online', 'chat', 'away', 'xa', 'dnd',
|
|
|
|
'offline', 'error', 'requested', 'event', 'not in roster')
|
|
|
|
return _load_icon_list(list_, path, pixbuf2)
|
2008-04-17 16:17:14 +02:00
|
|
|
|
|
|
|
def load_icon(icon_name):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Load an icon from the iconset in 16x16
|
|
|
|
"""
|
|
|
|
iconset = gajim.config.get('iconset')
|
|
|
|
path = os.path.join(helpers.get_iconset_path(iconset), '16x16', '')
|
|
|
|
icon_list = _load_icon_list([icon_name], path)
|
|
|
|
return icon_list[icon_name]
|
2008-07-22 23:08:52 +02:00
|
|
|
|
|
|
|
def load_mood_icon(icon_name):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Load an icon from the mood iconset in 16x16
|
|
|
|
"""
|
|
|
|
iconset = gajim.config.get('mood_iconset')
|
|
|
|
path = os.path.join(helpers.get_mood_iconset_path(iconset), '')
|
|
|
|
icon_list = _load_icon_list([icon_name], path)
|
|
|
|
return icon_list[icon_name]
|
2008-04-17 16:17:14 +02:00
|
|
|
|
2008-10-22 16:21:57 +02:00
|
|
|
def load_activity_icon(category, activity = None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Load an icon from the activity iconset in 16x16
|
|
|
|
"""
|
|
|
|
iconset = gajim.config.get('activity_iconset')
|
|
|
|
path = os.path.join(helpers.get_activity_iconset_path(iconset),
|
|
|
|
category, '')
|
|
|
|
if activity is None:
|
|
|
|
activity = 'category'
|
|
|
|
icon_list = _load_icon_list([activity], path)
|
|
|
|
return icon_list[activity]
|
2008-07-28 22:57:56 +02:00
|
|
|
|
2013-01-02 13:54:02 +01:00
|
|
|
def get_pep_as_pixbuf(pep_class):
|
|
|
|
if isinstance(pep_class, pep.UserMoodPEP):
|
|
|
|
assert not pep_class._retracted
|
|
|
|
received_mood = pep_class._pep_specific_data['mood']
|
|
|
|
mood = received_mood if received_mood in pep.MOODS else 'unknown'
|
|
|
|
pixbuf = load_mood_icon(mood).get_pixbuf()
|
|
|
|
return pixbuf
|
|
|
|
elif isinstance(pep_class, pep.UserTunePEP):
|
2013-09-28 23:06:37 +02:00
|
|
|
icon = get_icon_pixmap('audio-x-generic', quiet=True)
|
2013-09-08 19:58:54 +02:00
|
|
|
if not icon:
|
|
|
|
path = os.path.join(gajim.DATA_DIR, 'emoticons', 'static',
|
|
|
|
'music.png')
|
|
|
|
return GdkPixbuf.Pixbuf.new_from_file(path)
|
|
|
|
return icon
|
2013-01-02 13:54:02 +01:00
|
|
|
elif isinstance(pep_class, pep.UserActivityPEP):
|
|
|
|
assert not pep_class._retracted
|
|
|
|
pep_ = pep_class._pep_specific_data
|
|
|
|
activity = pep_['activity']
|
|
|
|
|
|
|
|
has_known_activity = activity in pep.ACTIVITIES
|
|
|
|
has_known_subactivity = (has_known_activity and ('subactivity' in pep_)
|
|
|
|
and (pep_['subactivity'] in pep.ACTIVITIES[activity]))
|
|
|
|
|
|
|
|
if has_known_activity:
|
|
|
|
if has_known_subactivity:
|
|
|
|
subactivity = pep_['subactivity']
|
|
|
|
return load_activity_icon(activity, subactivity).get_pixbuf()
|
|
|
|
else:
|
|
|
|
return load_activity_icon(activity).get_pixbuf()
|
|
|
|
else:
|
|
|
|
return load_activity_icon('unknown').get_pixbuf()
|
|
|
|
elif isinstance(pep_class, pep.UserLocationPEP):
|
2013-09-28 23:06:37 +02:00
|
|
|
icon = get_icon_pixmap('applications-internet', quiet=True)
|
2013-08-28 16:33:27 +02:00
|
|
|
if not icon:
|
2013-09-28 23:06:37 +02:00
|
|
|
icon = get_icon_pixmap('gajim-earth')
|
2013-08-28 16:33:27 +02:00
|
|
|
return icon
|
2013-01-02 13:54:02 +01:00
|
|
|
return None
|
|
|
|
|
2008-04-17 16:17:14 +02:00
|
|
|
def load_icons_meta():
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Load and return - AND + small icons to put on top left of an icon for meta
|
|
|
|
contacts
|
|
|
|
"""
|
|
|
|
iconset = gajim.config.get('iconset')
|
|
|
|
path = os.path.join(helpers.get_iconset_path(iconset), '16x16')
|
|
|
|
# try to find opened_meta.png file, else opened.png else nopixbuf merge
|
|
|
|
path_opened = os.path.join(path, 'opened_meta.png')
|
|
|
|
if not os.path.isfile(path_opened):
|
|
|
|
path_opened = os.path.join(path, 'opened.png')
|
|
|
|
if os.path.isfile(path_opened):
|
2012-12-23 16:23:43 +01:00
|
|
|
pixo = GdkPixbuf.Pixbuf.new_from_file(path_opened)
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
pixo = None
|
|
|
|
# Same thing for closed
|
|
|
|
path_closed = os.path.join(path, 'opened_meta.png')
|
|
|
|
if not os.path.isfile(path_closed):
|
|
|
|
path_closed = os.path.join(path, 'closed.png')
|
|
|
|
if os.path.isfile(path_closed):
|
2012-12-23 16:23:43 +01:00
|
|
|
pixc = GdkPixbuf.Pixbuf.new_from_file(path_closed)
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
|
|
|
pixc = None
|
|
|
|
return pixo, pixc
|
2008-04-17 16:17:14 +02:00
|
|
|
|
|
|
|
def _load_icon_list(icons_list, path, pixbuf2 = None):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Load icons in icons_list from the given path, and add pixbuf2 on top left of
|
|
|
|
each static images
|
|
|
|
"""
|
|
|
|
imgs = {}
|
|
|
|
for icon in icons_list:
|
|
|
|
# try to open a pixfile with the correct method
|
|
|
|
icon_file = icon.replace(' ', '_')
|
|
|
|
files = []
|
|
|
|
files.append(path + icon_file + '.gif')
|
|
|
|
files.append(path + icon_file + '.png')
|
2012-12-23 16:23:43 +01:00
|
|
|
image = Gtk.Image()
|
2010-02-08 15:08:40 +01:00
|
|
|
image.show()
|
|
|
|
imgs[icon] = image
|
|
|
|
for file_ in files: # loop seeking for either gif or png
|
|
|
|
if os.path.exists(file_):
|
|
|
|
image.set_from_file(file_)
|
2012-12-23 16:23:43 +01:00
|
|
|
if pixbuf2 and image.get_storage_type() == Gtk.ImageType.PIXBUF:
|
2010-02-08 15:08:40 +01:00
|
|
|
# add pixbuf2 on top-left corner of image
|
|
|
|
pixbuf1 = image.get_pixbuf()
|
|
|
|
pixbuf2.composite(pixbuf1, 0, 0,
|
|
|
|
pixbuf2.get_property('width'),
|
|
|
|
pixbuf2.get_property('height'), 0, 0, 1.0, 1.0,
|
2012-12-23 16:23:43 +01:00
|
|
|
GdkPixbuf.InterpType.NEAREST, 255)
|
2010-02-08 15:08:40 +01:00
|
|
|
image.set_from_pixbuf(pixbuf1)
|
|
|
|
break
|
|
|
|
return imgs
|
2008-04-17 16:17:14 +02:00
|
|
|
|
|
|
|
def make_jabber_state_images():
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Initialize jabber_state_images dictionary
|
|
|
|
"""
|
|
|
|
iconset = gajim.config.get('iconset')
|
|
|
|
if iconset:
|
|
|
|
if helpers.get_iconset_path(iconset):
|
|
|
|
path = os.path.join(helpers.get_iconset_path(iconset), '16x16')
|
|
|
|
if not os.path.exists(path):
|
|
|
|
iconset = gajim.config.DEFAULT_ICONSET
|
|
|
|
gajim.config.set('iconset', iconset)
|
|
|
|
else:
|
|
|
|
iconset = gajim.config.DEFAULT_ICONSET
|
|
|
|
gajim.config.set('iconset', iconset)
|
|
|
|
else:
|
|
|
|
iconset = gajim.config.DEFAULT_ICONSET
|
|
|
|
gajim.config.set('iconset', iconset)
|
|
|
|
|
2012-08-31 13:42:25 +02:00
|
|
|
path = os.path.join(helpers.get_iconset_path(iconset), '16x16')
|
|
|
|
gajim.interface.jabber_state_images['16'] = load_iconset(path)
|
|
|
|
|
|
|
|
pixo, pixc = load_icons_meta()
|
|
|
|
gajim.interface.jabber_state_images['opened'] = load_iconset(path, pixo)
|
|
|
|
gajim.interface.jabber_state_images['closed'] = load_iconset(path, pixc)
|
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
path = os.path.join(helpers.get_iconset_path(iconset), '32x32')
|
|
|
|
gajim.interface.jabber_state_images['32'] = load_iconset(path)
|
|
|
|
|
2012-08-31 09:54:32 +02:00
|
|
|
path = os.path.join(helpers.get_iconset_path(iconset), '24x24')
|
|
|
|
if (os.path.exists(path)):
|
|
|
|
gajim.interface.jabber_state_images['24'] = load_iconset(path)
|
|
|
|
else:
|
|
|
|
# Resize 32x32 icons to 24x24
|
|
|
|
for each in gajim.interface.jabber_state_images['32']:
|
2012-12-23 16:23:43 +01:00
|
|
|
img = Gtk.Image()
|
2012-08-31 13:42:25 +02:00
|
|
|
pix = gajim.interface.jabber_state_images['32'][each]
|
|
|
|
pix_type = pix.get_storage_type()
|
2012-12-23 16:23:43 +01:00
|
|
|
if pix_type == Gtk.ImageType.ANIMATION:
|
2012-08-31 13:42:25 +02:00
|
|
|
animation = pix.get_animation()
|
|
|
|
pixbuf = animation.get_static_image()
|
2012-12-23 16:23:43 +01:00
|
|
|
elif pix_type == Gtk.ImageType.EMPTY:
|
2012-08-31 13:42:25 +02:00
|
|
|
pix = gajim.interface.jabber_state_images['16'][each]
|
|
|
|
pix_16_type = pix.get_storage_type()
|
2012-12-23 16:23:43 +01:00
|
|
|
if pix_16_type == Gtk.ImageType.ANIMATION:
|
2012-08-31 13:42:25 +02:00
|
|
|
animation = pix.get_animation()
|
|
|
|
pixbuf = animation.get_static_image()
|
|
|
|
else:
|
|
|
|
pixbuf = pix.get_pixbuf()
|
2012-12-23 16:23:43 +01:00
|
|
|
scaled_pix = pixbuf.scale_simple(24, 24, GdkPixbuf.InterpType.BILINEAR)
|
2012-08-31 09:54:32 +02:00
|
|
|
img.set_from_pixbuf(scaled_pix)
|
|
|
|
gajim.interface.jabber_state_images['24'][each] = img
|
|
|
|
|
2008-04-17 16:17:14 +02:00
|
|
|
def reload_jabber_state_images():
|
2010-02-08 15:08:40 +01:00
|
|
|
make_jabber_state_images()
|
|
|
|
gajim.interface.roster.update_jabber_state_images()
|
2008-07-29 21:49:31 +02:00
|
|
|
|
2009-04-08 20:52:50 +02:00
|
|
|
def label_set_autowrap(widget):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Make labels automatically re-wrap if their containers are resized.
|
|
|
|
Accepts label or container widgets
|
|
|
|
"""
|
2012-12-23 16:23:43 +01:00
|
|
|
if isinstance (widget, Gtk.Container):
|
2010-02-08 15:08:40 +01:00
|
|
|
children = widget.get_children()
|
2013-01-02 18:17:51 +01:00
|
|
|
for i in list(range (len (children))):
|
2010-02-08 15:08:40 +01:00
|
|
|
label_set_autowrap(children[i])
|
2012-12-23 16:23:43 +01:00
|
|
|
elif isinstance(widget, Gtk.Label):
|
2010-02-08 15:08:40 +01:00
|
|
|
widget.set_line_wrap(True)
|
|
|
|
widget.connect_after('size-allocate', __label_size_allocate)
|
2009-04-08 20:52:50 +02:00
|
|
|
|
|
|
|
def __label_size_allocate(widget, allocation):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
|
|
|
Callback which re-allocates the size of a label
|
|
|
|
"""
|
|
|
|
layout = widget.get_layout()
|
|
|
|
|
|
|
|
lw_old, lh_old = layout.get_size()
|
|
|
|
# fixed width labels
|
2012-12-23 16:23:43 +01:00
|
|
|
if lw_old/Pango.SCALE == allocation.width:
|
2010-02-08 15:08:40 +01:00
|
|
|
return
|
|
|
|
|
2012-12-23 16:23:43 +01:00
|
|
|
# set wrap width to the Pango.Layout of the labels ###
|
2011-10-07 21:22:25 +02:00
|
|
|
widget.set_alignment(0.0, 0.0)
|
2012-12-23 16:23:43 +01:00
|
|
|
layout.set_width (allocation.width * Pango.SCALE)
|
2010-02-10 16:17:09 +01:00
|
|
|
lh = layout.get_size()[1]
|
2010-02-08 15:08:40 +01:00
|
|
|
|
|
|
|
if lh_old != lh:
|
2012-12-23 16:23:43 +01:00
|
|
|
widget.set_size_request (-1, lh / Pango.SCALE)
|