2018-09-05 02:59:34 +02:00
|
|
|
# Copyright (C) 2003-2014 Yann Leboulanger <asterix AT lagaule.org>
|
|
|
|
# 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>
|
|
|
|
# Copyright (C) 2006-2007 Junglecow J <junglecow AT gmail.com>
|
|
|
|
# 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>
|
|
|
|
#
|
|
|
|
# This file is part of Gajim.
|
|
|
|
#
|
|
|
|
# Gajim is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published
|
|
|
|
# by the Free Software Foundation; version 3 only.
|
|
|
|
#
|
|
|
|
# Gajim is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with Gajim. If not, see <http://www.gnu.org/licenses/>.
|
2005-07-21 23:39:47 +02:00
|
|
|
|
2018-09-21 23:55:57 +02:00
|
|
|
import os
|
|
|
|
import math
|
|
|
|
import logging
|
|
|
|
from io import BytesIO
|
|
|
|
|
2012-12-23 16:23:43 +01:00
|
|
|
from gi.repository import Gtk
|
|
|
|
from gi.repository import Gdk
|
|
|
|
from gi.repository import GdkPixbuf
|
2016-04-02 13:59:42 +02:00
|
|
|
from gi.repository import GLib
|
2012-12-23 16:23:43 +01:00
|
|
|
from gi.repository import Pango
|
2018-03-01 22:47:01 +01:00
|
|
|
|
2016-04-05 18:59:55 +02:00
|
|
|
try:
|
|
|
|
from PIL import Image
|
2018-09-16 23:45:01 +02:00
|
|
|
except Exception:
|
2016-04-05 18:59:55 +02:00
|
|
|
pass
|
2007-01-06 16:31:30 +01:00
|
|
|
|
2018-10-04 23:55:35 +02:00
|
|
|
from gajim.common.i18n import _
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2018-01-12 00:21:11 +01:00
|
|
|
|
2009-12-02 10:23:17 +01:00
|
|
|
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
|
|
|
|
2018-11-18 22:13:24 +01:00
|
|
|
log = logging.getLogger('gajim.gtkgui_helpers')
|
2018-08-06 17:14:07 +02:00
|
|
|
|
2006-05-02 17:53:25 +02:00
|
|
|
|
2017-09-16 11:49:31 +02:00
|
|
|
def get_pixbuf_from_data(file_data):
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2017-09-16 11:49:31 +02:00
|
|
|
Get image data and returns GdkPixbuf.Pixbuf
|
2010-02-08 15:08:40 +01:00
|
|
|
"""
|
2016-04-10 19:02:07 +02:00
|
|
|
pixbufloader = GdkPixbuf.PixbufLoader()
|
2016-04-05 18:59:55 +02:00
|
|
|
try:
|
2016-04-10 19:02:07 +02:00
|
|
|
pixbufloader.write(file_data)
|
|
|
|
pixbufloader.close()
|
|
|
|
pixbuf = pixbufloader.get_pixbuf()
|
2017-09-16 11:49:31 +02:00
|
|
|
except GLib.GError:
|
2016-04-10 19:02:07 +02:00
|
|
|
pixbufloader.close()
|
2016-04-12 22:53:41 +02:00
|
|
|
|
2017-09-16 11:49:31 +02:00
|
|
|
log.warning('loading avatar using pixbufloader failed, trying to '
|
|
|
|
'convert avatar image using pillow')
|
2016-04-06 23:38:30 +02:00
|
|
|
try:
|
2016-04-15 01:37:08 +02:00
|
|
|
avatar = Image.open(BytesIO(file_data)).convert("RGBA")
|
2017-09-16 11:49:31 +02:00
|
|
|
array = GLib.Bytes.new(avatar.tobytes())
|
2016-04-15 01:37:08 +02:00
|
|
|
width, height = avatar.size
|
2017-09-16 11:49:31 +02:00
|
|
|
pixbuf = GdkPixbuf.Pixbuf.new_from_bytes(
|
|
|
|
array, GdkPixbuf.Colorspace.RGB,
|
|
|
|
True, 8, width, height, width * 4)
|
|
|
|
except Exception:
|
|
|
|
log.warning('Could not use pillow to convert avatar image, '
|
|
|
|
'image cannot be displayed', exc_info=True)
|
|
|
|
return
|
|
|
|
|
|
|
|
return pixbuf
|
2005-10-16 16:19:02 +02:00
|
|
|
|
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
|
|
|
|
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
|
|
|
|
2018-02-19 19:37:53 +01:00
|
|
|
def scale_with_ratio(size, width, height):
|
|
|
|
if height == width:
|
|
|
|
return size, size
|
|
|
|
if height > width:
|
|
|
|
ratio = height / float(width)
|
2018-05-20 01:03:22 +02:00
|
|
|
return int(size / ratio), size
|
2018-09-18 10:14:04 +02:00
|
|
|
|
|
|
|
ratio = width / float(height)
|
|
|
|
return size, int(size / ratio)
|
2018-02-19 19:37:53 +01:00
|
|
|
|
2018-05-20 01:03:22 +02:00
|
|
|
def scale_pixbuf(pixbuf, size):
|
|
|
|
width, height = scale_with_ratio(size,
|
|
|
|
pixbuf.get_width(),
|
|
|
|
pixbuf.get_height())
|
|
|
|
return pixbuf.scale_simple(width, height,
|
|
|
|
GdkPixbuf.InterpType.BILINEAR)
|
|
|
|
|
|
|
|
def scale_pixbuf_from_data(data, size):
|
|
|
|
pixbuf = get_pixbuf_from_data(data)
|
|
|
|
return scale_pixbuf(pixbuf, size)
|
|
|
|
|
2017-09-16 11:49:31 +02:00
|
|
|
def on_avatar_save_as_menuitem_activate(widget, avatar, default_name=''):
|
2018-09-26 19:06:47 +02:00
|
|
|
from gajim.gtk.dialogs import ErrorDialog
|
|
|
|
from gajim.gtk.dialogs import ConfirmationDialog
|
|
|
|
from gajim.gtk.dialogs import FTOverwriteConfirmationDialog
|
2018-08-19 17:44:17 +02:00
|
|
|
from gajim.gtk.filechoosers import AvatarSaveDialog
|
2010-02-08 15:08:40 +01:00
|
|
|
def on_continue(response, file_path):
|
|
|
|
if response < 0:
|
|
|
|
return
|
2017-09-16 11:49:31 +02:00
|
|
|
|
2018-05-04 00:36:10 +02:00
|
|
|
app.config.set('last_save_dir', os.path.dirname(file_path))
|
2017-09-16 11:49:31 +02:00
|
|
|
if isinstance(avatar, str):
|
|
|
|
# We got a SHA
|
|
|
|
pixbuf = app.interface.get_avatar(avatar)
|
|
|
|
else:
|
|
|
|
# We got a pixbuf
|
|
|
|
pixbuf = avatar
|
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
|
2018-05-04 00:36:10 +02:00
|
|
|
image_format = 'png'
|
|
|
|
file_path += '.png'
|
2010-02-08 15:08:40 +01:00
|
|
|
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, [], [])
|
2018-09-17 18:57:00 +02:00
|
|
|
except Exception as error:
|
|
|
|
log.error('Error saving avatar: %s', error)
|
2010-02-08 15:08:40 +01:00
|
|
|
if os.path.exists(file_path):
|
|
|
|
os.remove(file_path)
|
2018-05-04 00:36:10 +02:00
|
|
|
new_file_path = '.'.join(file_path.split('.')[:-1]) + '.png'
|
2010-02-08 15:08:40 +01:00
|
|
|
def on_ok(file_path, pixbuf):
|
2018-05-04 00:36:10 +02:00
|
|
|
pixbuf.savev(file_path, 'png', [], [])
|
2018-07-16 23:22:33 +02:00
|
|
|
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},
|
2018-09-18 12:06:01 +02:00
|
|
|
on_response_ok=(on_ok, new_file_path, pixbuf))
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2018-05-04 00:36:10 +02:00
|
|
|
def on_ok(file_path):
|
2010-02-08 15:08:40 +01:00
|
|
|
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)
|
2018-07-16 23:22:33 +02:00
|
|
|
ErrorDialog(_('Cannot overwrite existing file "%s"') % \
|
2010-02-10 16:17:09 +01:00
|
|
|
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
|
2018-07-16 23:22:33 +02:00
|
|
|
dialog2 = FTOverwriteConfirmationDialog(
|
2010-02-10 16:17:09 +01:00
|
|
|
_('This file already exists'), _('What do you want to do?'),
|
2018-05-04 00:36:10 +02:00
|
|
|
propose_resume=False, on_response=(on_continue, file_path))
|
2010-02-08 15:08:40 +01:00
|
|
|
dialog2.set_destroy_with_parent(True)
|
|
|
|
else:
|
|
|
|
dirname = os.path.dirname(file_path)
|
|
|
|
if not os.access(dirname, os.W_OK):
|
2018-07-16 23:22:33 +02:00
|
|
|
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)
|
|
|
|
|
2018-05-04 00:36:10 +02:00
|
|
|
transient = app.app.get_active_window()
|
|
|
|
AvatarSaveDialog(on_ok,
|
|
|
|
path=app.config.get('last_save_dir'),
|
|
|
|
file_name='%s.png' % default_name,
|
|
|
|
transient_for=transient)
|
2006-11-18 21:52:28 +01:00
|
|
|
|
2018-09-18 12:06:01 +02: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()
|
2017-07-27 21:50:38 +02:00
|
|
|
col.pack_start(cell, True)
|
2010-02-08 15:08:40 +01:00
|
|
|
col.set_attributes(cell, text=0)
|
|
|
|
for value in value_list:
|
2018-09-18 13:54:25 +02:00
|
|
|
iter_ = liststore.append(value)
|
2010-02-08 15:08:40 +01:00
|
|
|
if value[1] in selected_values:
|
2018-09-18 13:54:25 +02:00
|
|
|
treeview.get_selection().select_iter(iter_)
|
2010-02-08 15:08:40 +01:00
|
|
|
treeview.show_all()
|
|
|
|
return treeview
|
2009-08-31 21:45:52 +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
|
|
|
|
"""
|
2018-09-18 12:06:01 +02:00
|
|
|
if isinstance(widget, Gtk.Container):
|
2010-02-08 15:08:40 +01:00
|
|
|
children = widget.get_children()
|
2018-09-18 12:06:01 +02: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)
|
2018-09-18 12:06:01 +02: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:
|
2018-09-18 12:06:01 +02:00
|
|
|
widget.set_size_request(-1, lh / Pango.SCALE)
|
2017-01-26 21:34:54 +01:00
|
|
|
|
|
|
|
def get_action(action):
|
2017-08-13 13:18:56 +02:00
|
|
|
return app.app.lookup_action(action)
|
2017-03-02 18:01:52 +01:00
|
|
|
|
2018-03-01 22:47:01 +01:00
|
|
|
def add_css_class(widget, class_name, prefix=None):
|
|
|
|
if class_name and prefix:
|
|
|
|
class_name = prefix + class_name
|
2017-03-02 18:01:52 +01:00
|
|
|
|
2017-03-01 17:11:18 +01:00
|
|
|
style = widget.get_style_context()
|
2018-03-01 22:47:01 +01:00
|
|
|
if prefix is not None:
|
|
|
|
# Remove all css classes with prefix
|
|
|
|
for css_cls in style.list_classes():
|
|
|
|
if css_cls.startswith(prefix):
|
|
|
|
style.remove_class(css_cls)
|
|
|
|
|
|
|
|
if class_name is not None:
|
|
|
|
style.add_class(class_name)
|
2017-03-01 17:11:18 +01:00
|
|
|
|
2017-12-18 00:11:39 +01:00
|
|
|
def add_css_to_widget(widget, css):
|
|
|
|
provider = Gtk.CssProvider()
|
|
|
|
provider.load_from_data(bytes(css.encode()))
|
|
|
|
context = widget.get_style_context()
|
|
|
|
context.add_provider(provider,
|
|
|
|
Gtk.STYLE_PROVIDER_PRIORITY_USER)
|
|
|
|
|
2017-03-01 17:11:18 +01:00
|
|
|
def remove_css_class(widget, class_name):
|
|
|
|
style = widget.get_style_context()
|
2018-03-01 22:47:01 +01:00
|
|
|
style.remove_class(class_name)
|
2017-10-27 18:46:26 +02:00
|
|
|
|
2018-04-30 17:39:48 +02:00
|
|
|
def pango_to_css_weight(number):
|
|
|
|
# Pango allows for weight values between 100 and 1000
|
|
|
|
# CSS allows only full hundred numbers like 100, 200 ..
|
|
|
|
number = int(number)
|
|
|
|
if number < 100:
|
|
|
|
return 100
|
|
|
|
if number > 900:
|
|
|
|
return 900
|
|
|
|
return int(math.ceil(number / 100.0)) * 100
|