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 cairo
|
|
|
|
import os
|
|
|
|
import sys
|
|
|
|
import math
|
|
|
|
import logging
|
|
|
|
from io import BytesIO
|
|
|
|
import xml.etree.ElementTree as ET
|
2005-07-23 14:45:17 +02:00
|
|
|
import xml.sax.saxutils
|
2018-09-21 23:55:57 +02:00
|
|
|
from xml.sax import ContentHandler # type: ignore
|
|
|
|
|
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
|
|
|
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import i18n
|
2017-08-13 13:18:56 +02:00
|
|
|
from gajim.common import app
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import configpaths
|
2018-07-05 18:52:43 +02:00
|
|
|
from gajim.common.const import PEPEventType, ACTIVITIES, MOODS
|
2005-09-17 11:37:40 +02:00
|
|
|
|
2018-09-21 23:55:57 +02:00
|
|
|
log = logging.getLogger('gajim.gtkgui_helpers')
|
|
|
|
|
2012-12-23 16:23:43 +01:00
|
|
|
gtk_icon_theme = Gtk.IconTheme.get_default()
|
2018-04-25 20:49:37 +02:00
|
|
|
gtk_icon_theme.append_search_path(configpaths.get('ICONS'))
|
2009-11-27 16:42:32 +01:00
|
|
|
|
2017-06-12 23:32:59 +02:00
|
|
|
class Color:
|
|
|
|
BLACK = Gdk.RGBA(red=0, green=0, blue=0, alpha=1)
|
2017-07-20 00:01:24 +02:00
|
|
|
GREEN = Gdk.RGBA(red=115/255, green=210/255, blue=22/255, alpha=1)
|
|
|
|
RED = Gdk.RGBA(red=204/255, green=0, blue=0, alpha=1)
|
2017-10-03 13:08:06 +02:00
|
|
|
GREY = Gdk.RGBA(red=195/255, green=195/255, blue=192/255, alpha=1)
|
2017-11-06 21:46:25 +01:00
|
|
|
ORANGE = Gdk.RGBA(red=245/255, green=121/255, blue=0/255, alpha=1)
|
2017-06-12 23:32:59 +02:00
|
|
|
|
|
|
|
def get_icon_pixmap(icon_name, size=16, color=None, quiet=False):
|
2010-02-08 15:08:40 +01:00
|
|
|
try:
|
2017-06-12 23:32:59 +02:00
|
|
|
iconinfo = gtk_icon_theme.lookup_icon(icon_name, size, 0)
|
2017-07-02 15:13:19 +02:00
|
|
|
if not iconinfo:
|
|
|
|
raise GLib.GError
|
2017-06-12 23:32:59 +02:00
|
|
|
if color:
|
2018-09-17 21:11:45 +02:00
|
|
|
pixbuf, _was_symbolic = iconinfo.load_symbolic(*color)
|
2017-06-12 23:32:59 +02:00
|
|
|
return pixbuf
|
|
|
|
return iconinfo.load_icon()
|
2018-09-17 18:57:00 +02:00
|
|
|
except GLib.GError as error:
|
2013-08-28 16:33:27 +02:00
|
|
|
if not quiet:
|
2018-09-17 18:57:00 +02:00
|
|
|
log.error('Unable to load icon %s: %s', icon_name, str(error))
|
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)
|
2018-09-18 10:14:04 +02:00
|
|
|
if icon_info is None:
|
2018-09-17 18:57:00 +02:00
|
|
|
log.error('Icon not found: %s', icon_name)
|
|
|
|
return ''
|
2018-09-18 10:14:04 +02:00
|
|
|
return icon_info.get_filename()
|
2018-09-17 18:57:00 +02:00
|
|
|
except GLib.GError as error:
|
|
|
|
log.error('Unable to find icon %s: %s', icon_name, str(error))
|
2009-11-27 16:42:32 +01:00
|
|
|
|
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
|
|
|
|
2017-06-13 23:58:06 +02:00
|
|
|
from gajim.common import helpers
|
2009-12-02 10:23:17 +01:00
|
|
|
|
2018-03-16 16:59:54 +01:00
|
|
|
def get_total_screen_geometry():
|
|
|
|
screen = Gdk.Screen.get_default()
|
|
|
|
window = Gdk.Screen.get_root_window(screen)
|
2018-07-12 23:19:22 +02:00
|
|
|
w, h = window.get_width(), window.get_height()
|
|
|
|
log.debug('Get screen geometry: %s %s', w, h)
|
|
|
|
return w, h
|
2009-12-02 10:23:17 +01:00
|
|
|
|
2014-11-11 23:11:15 +01:00
|
|
|
def add_image_to_button(button, 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)
|
2014-11-11 23:11:15 +01:00
|
|
|
button.set_image(img)
|
2009-12-01 15:50:58 +01:00
|
|
|
|
2017-08-15 19:23:23 +02:00
|
|
|
def get_image_button(icon_name, tooltip, toggle=False):
|
|
|
|
if toggle:
|
|
|
|
button = Gtk.ToggleButton()
|
2017-08-15 19:12:30 +02:00
|
|
|
icon = get_icon_pixmap(icon_name)
|
|
|
|
image = Gtk.Image()
|
|
|
|
image.set_from_pixbuf(icon)
|
|
|
|
button.set_image(image)
|
2017-08-15 19:23:23 +02:00
|
|
|
else:
|
2017-08-15 19:12:30 +02:00
|
|
|
button = Gtk.Button.new_from_icon_name(
|
|
|
|
icon_name, Gtk.IconSize.MENU)
|
2018-01-11 15:12:07 +01:00
|
|
|
button.set_tooltip_text(tooltip)
|
2017-08-15 19:23:23 +02:00
|
|
|
return button
|
|
|
|
|
2010-01-22 19:57:59 +01:00
|
|
|
def get_gtk_builder(file_name, widget=None):
|
2017-07-20 00:43:06 +02:00
|
|
|
file_path = os.path.join(configpaths.get('GUI'), file_name)
|
2018-08-06 17:14:07 +02:00
|
|
|
|
|
|
|
builder = Gtk.Builder()
|
2018-04-24 22:54:18 +02:00
|
|
|
builder.set_translation_domain(i18n.DOMAIN)
|
2006-05-02 17:53:25 +02:00
|
|
|
|
2018-06-23 17:02:23 +02:00
|
|
|
if sys.platform == "win32":
|
2018-08-06 17:14:07 +02:00
|
|
|
# This is a workaround for non working translation on Windows
|
|
|
|
tree = ET.parse(file_path)
|
2018-06-23 17:02:23 +02:00
|
|
|
for node in tree.iter():
|
2018-09-23 15:33:31 +02:00
|
|
|
if 'translatable' in node.attrib and node.text is not None:
|
2018-06-23 17:02:23 +02:00
|
|
|
node.text = _(node.text)
|
|
|
|
xml_text = ET.tostring(tree.getroot(),
|
|
|
|
encoding='unicode',
|
|
|
|
method='xml')
|
2018-08-06 17:14:07 +02:00
|
|
|
|
2018-06-23 20:34:53 +02:00
|
|
|
if widget is not None:
|
|
|
|
builder.add_objects_from_string(xml_text, [widget])
|
2018-08-06 17:14:07 +02:00
|
|
|
else:
|
2018-09-08 08:40:42 +02:00
|
|
|
# Workaround
|
|
|
|
# https://gitlab.gnome.org/GNOME/pygobject/issues/255
|
|
|
|
Gtk.Builder.__mro__[1].add_from_string(
|
|
|
|
builder, xml_text, len(xml_text.encode("utf-8")))
|
2018-06-23 17:02:23 +02:00
|
|
|
else:
|
|
|
|
if widget is not None:
|
2018-08-06 17:14:07 +02:00
|
|
|
builder.add_objects_from_file(file_path, [widget])
|
|
|
|
else:
|
|
|
|
builder.add_from_file(file_path)
|
|
|
|
return builder
|
2018-06-23 17:02:23 +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()
|
2017-10-27 12:48:01 +02:00
|
|
|
liststore = Gtk.ListStore(str, 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)
|
2017-10-27 12:48:01 +02:00
|
|
|
completion.add_attribute(render_pixbuf, 'icon_name', 0)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2016-04-04 22:49:36 +02:00
|
|
|
render_text = Gtk.CellRendererText()
|
|
|
|
completion.pack_start(render_text, True)
|
|
|
|
completion.add_attribute(render_text, 'text', 1)
|
2010-02-08 15:08:40 +01:00
|
|
|
completion.set_property('text_column', 1)
|
|
|
|
completion.set_model(liststore)
|
|
|
|
entry.set_completion(completion)
|
|
|
|
return liststore
|
2008-12-03 22:56:12 +01:00
|
|
|
|
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
|
|
|
|
"""
|
2018-03-16 16:59:54 +01:00
|
|
|
screen_w, screen_h = get_total_screen_geometry()
|
2010-02-08 15:08:40 +01:00
|
|
|
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
|
|
|
|
"""
|
2018-03-16 16:59:54 +01:00
|
|
|
screen_w, screen_h = get_total_screen_geometry()
|
2010-02-08 15:08:40 +01:00
|
|
|
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
|
|
|
|
2016-09-26 10:44:34 +02:00
|
|
|
def at_the_end(widget):
|
|
|
|
"""Determines if a Scrollbar in a GtkScrolledWindow is at the end.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
widget (GtkScrolledWindow)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: The return value is True if at the end, False if not.
|
|
|
|
"""
|
|
|
|
adj_v = widget.get_vadjustment()
|
|
|
|
max_scroll_pos = adj_v.get_upper() - adj_v.get_page_size()
|
2018-09-16 17:11:52 +02:00
|
|
|
return adj_v.get_value() == max_scroll_pos
|
2016-09-26 10:44:34 +02:00
|
|
|
|
|
|
|
def scroll_to_end(widget):
|
|
|
|
"""Scrolls to the end of a GtkScrolledWindow.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
widget (GtkScrolledWindow)
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
bool: The return value is False so it can be used with GLib.idle_add.
|
|
|
|
"""
|
|
|
|
adj_v = widget.get_vadjustment()
|
2017-08-10 11:07:57 +02:00
|
|
|
if adj_v is None:
|
|
|
|
# This can happen when the Widget is already destroyed when called
|
|
|
|
# from GLib.idle_add
|
|
|
|
return False
|
2016-09-26 10:44:34 +02:00
|
|
|
max_scroll_pos = adj_v.get_upper() - adj_v.get_page_size()
|
|
|
|
adj_v.set_value(max_scroll_pos)
|
|
|
|
|
|
|
|
adj_h = widget.get_hadjustment()
|
|
|
|
adj_h.set_value(0)
|
|
|
|
return False
|
|
|
|
|
|
|
|
|
2018-09-21 23:55:57 +02:00
|
|
|
class ServersXMLHandler(ContentHandler):
|
2010-02-08 15:08:40 +01:00
|
|
|
def __init__(self):
|
2018-09-21 23:55:57 +02:00
|
|
|
ContentHandler.__init__(self)
|
2010-02-08 15:08:40 +01:00
|
|
|
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
|
|
|
|
"""
|
2017-08-13 13:18:56 +02:00
|
|
|
if app.config.get('use_urgency_hint'):
|
2018-09-17 23:21:38 +02:00
|
|
|
window.props.urgency_hint = unread_messages_no > 0
|
2005-09-07 23:12:30 +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
|
|
|
|
2017-03-07 19:51:50 +01:00
|
|
|
def get_cursor(attr):
|
|
|
|
display = Gdk.Display.get_default()
|
|
|
|
cursor = getattr(Gdk.CursorType, attr)
|
|
|
|
return Gdk.Cursor.new_for_display(display, 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
|
|
|
|
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
|
2017-12-18 00:25:19 +01:00
|
|
|
|
|
|
|
bg = context.get_property('background-color', state)
|
2013-01-05 15:53:14 +01:00
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
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
|
|
|
|
|
|
|
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
|
|
|
|
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
|
|
|
|
"""
|
2017-08-13 13:18:56 +02:00
|
|
|
iconset = app.config.get('mood_iconset')
|
2010-02-08 15:08:40 +01:00
|
|
|
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
|
|
|
|
2018-09-18 12:06:01 +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
|
|
|
|
"""
|
2017-08-13 13:18:56 +02:00
|
|
|
iconset = app.config.get('activity_iconset')
|
2010-02-08 15:08:40 +01:00
|
|
|
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):
|
2018-07-05 18:52:43 +02:00
|
|
|
if pep_class == PEPEventType.MOOD:
|
2018-09-12 00:01:54 +02:00
|
|
|
received_mood = pep_class.data['mood']
|
2018-07-05 18:52:43 +02:00
|
|
|
mood = received_mood if received_mood in MOODS else 'unknown'
|
2013-01-02 13:54:02 +01:00
|
|
|
pixbuf = load_mood_icon(mood).get_pixbuf()
|
|
|
|
return pixbuf
|
2018-09-18 10:14:04 +02:00
|
|
|
|
|
|
|
if pep_class == PEPEventType.TUNE:
|
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:
|
2018-09-18 10:14:04 +02:00
|
|
|
path = os.path.join(
|
|
|
|
configpaths.get('DATA'), 'emoticons', 'static', 'music.png')
|
2013-09-08 19:58:54 +02:00
|
|
|
return GdkPixbuf.Pixbuf.new_from_file(path)
|
|
|
|
return icon
|
2018-09-18 10:14:04 +02:00
|
|
|
|
|
|
|
if pep_class == PEPEventType.ACTIVITY:
|
2018-09-12 00:01:54 +02:00
|
|
|
pep_ = pep_class.data
|
2013-01-02 13:54:02 +01:00
|
|
|
activity = pep_['activity']
|
|
|
|
|
2018-07-03 00:10:55 +02:00
|
|
|
has_known_activity = activity in ACTIVITIES
|
2018-09-18 10:14:04 +02:00
|
|
|
has_known_subactivity = (has_known_activity and
|
|
|
|
'subactivity' in pep_ and
|
|
|
|
pep_['subactivity'] in ACTIVITIES[activity])
|
2013-01-02 13:54:02 +01:00
|
|
|
|
|
|
|
if has_known_activity:
|
|
|
|
if has_known_subactivity:
|
|
|
|
subactivity = pep_['subactivity']
|
|
|
|
return load_activity_icon(activity, subactivity).get_pixbuf()
|
2018-09-18 10:14:04 +02:00
|
|
|
return load_activity_icon(activity).get_pixbuf()
|
|
|
|
return load_activity_icon('unknown').get_pixbuf()
|
|
|
|
|
|
|
|
if pep_class == PEPEventType.LOCATION:
|
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
|
2018-09-18 10:14:04 +02:00
|
|
|
|
2013-01-02 13:54:02 +01:00
|
|
|
return None
|
|
|
|
|
2017-10-20 17:22:44 +02:00
|
|
|
def get_iconset_name_for(name):
|
2018-03-18 00:02:28 +01:00
|
|
|
if name == 'not in roster':
|
|
|
|
name = 'notinroster'
|
2017-10-20 17:22:44 +02:00
|
|
|
iconset = app.config.get('iconset')
|
|
|
|
if not iconset:
|
2018-03-18 00:02:28 +01:00
|
|
|
iconset = app.config.DEFAULT_ICONSET
|
|
|
|
return '%s-%s' % (iconset, name)
|
2017-10-20 17:22:44 +02:00
|
|
|
|
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
|
|
|
|
"""
|
2017-08-13 13:18:56 +02:00
|
|
|
iconset = app.config.get('iconset')
|
2010-02-08 15:08:40 +01:00
|
|
|
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
|
|
|
|
2018-09-18 12:06:01 +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
|
|
|
|
"""
|
2017-08-13 13:18:56 +02:00
|
|
|
iconset = app.config.get('iconset')
|
2010-02-08 15:08:40 +01:00
|
|
|
if iconset:
|
|
|
|
if helpers.get_iconset_path(iconset):
|
|
|
|
path = os.path.join(helpers.get_iconset_path(iconset), '16x16')
|
|
|
|
if not os.path.exists(path):
|
2017-08-13 13:18:56 +02:00
|
|
|
iconset = app.config.DEFAULT_ICONSET
|
|
|
|
app.config.set('iconset', iconset)
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
2017-08-13 13:18:56 +02:00
|
|
|
iconset = app.config.DEFAULT_ICONSET
|
|
|
|
app.config.set('iconset', iconset)
|
2010-02-08 15:08:40 +01:00
|
|
|
else:
|
2017-08-13 13:18:56 +02:00
|
|
|
iconset = app.config.DEFAULT_ICONSET
|
|
|
|
app.config.set('iconset', iconset)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2012-08-31 13:42:25 +02:00
|
|
|
path = os.path.join(helpers.get_iconset_path(iconset), '16x16')
|
2017-08-13 13:18:56 +02:00
|
|
|
app.interface.jabber_state_images['16'] = load_iconset(path)
|
2012-08-31 13:42:25 +02:00
|
|
|
|
|
|
|
pixo, pixc = load_icons_meta()
|
2017-08-13 13:18:56 +02:00
|
|
|
app.interface.jabber_state_images['opened'] = load_iconset(path, pixo)
|
|
|
|
app.interface.jabber_state_images['closed'] = load_iconset(path, pixc)
|
2012-08-31 13:42:25 +02:00
|
|
|
|
2010-02-08 15:08:40 +01:00
|
|
|
path = os.path.join(helpers.get_iconset_path(iconset), '32x32')
|
2017-08-13 13:18:56 +02:00
|
|
|
app.interface.jabber_state_images['32'] = load_iconset(path)
|
2010-02-08 15:08:40 +01:00
|
|
|
|
2012-08-31 09:54:32 +02:00
|
|
|
path = os.path.join(helpers.get_iconset_path(iconset), '24x24')
|
2018-09-16 14:42:05 +02:00
|
|
|
if os.path.exists(path):
|
2017-08-13 13:18:56 +02:00
|
|
|
app.interface.jabber_state_images['24'] = load_iconset(path)
|
2012-08-31 09:54:32 +02:00
|
|
|
else:
|
|
|
|
# Resize 32x32 icons to 24x24
|
2017-08-13 13:18:56 +02:00
|
|
|
for each in app.interface.jabber_state_images['32']:
|
2012-12-23 16:23:43 +01:00
|
|
|
img = Gtk.Image()
|
2017-08-13 13:18:56 +02:00
|
|
|
pix = app.interface.jabber_state_images['32'][each]
|
2012-08-31 13:42:25 +02:00
|
|
|
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:
|
2017-08-13 13:18:56 +02:00
|
|
|
pix = app.interface.jabber_state_images['16'][each]
|
2012-08-31 13:42:25 +02:00
|
|
|
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()
|
2014-03-25 14:50:12 +01:00
|
|
|
else:
|
|
|
|
pixbuf = pix.get_pixbuf()
|
2012-08-31 13:42:25 +02:00
|
|
|
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)
|
2017-08-13 13:18:56 +02:00
|
|
|
app.interface.jabber_state_images['24'][each] = img
|
2012-08-31 09:54:32 +02:00
|
|
|
|
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()
|
2017-08-13 13:18:56 +02:00
|
|
|
app.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
|
|
|
|
"""
|
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
|
|
|
|
|
|
|
def draw_affiliation(surface, affiliation):
|
|
|
|
icon_size = 16
|
|
|
|
size = 4 * 1
|
|
|
|
if affiliation not in ('owner', 'admin', 'member'):
|
|
|
|
return
|
|
|
|
ctx = cairo.Context(surface)
|
|
|
|
ctx.rectangle(icon_size-size, icon_size-size, size, size)
|
|
|
|
if affiliation == 'owner':
|
|
|
|
ctx.set_source_rgb(204/255, 0, 0)
|
|
|
|
elif affiliation == 'admin':
|
|
|
|
ctx.set_source_rgb(255/255, 140/255, 0)
|
|
|
|
elif affiliation == 'member':
|
|
|
|
ctx.set_source_rgb(0, 255/255, 0)
|
|
|
|
ctx.fill()
|
|
|
|
|
|
|
|
def get_image_from_icon_name(icon_name, scale):
|
|
|
|
icon = get_iconset_name_for(icon_name)
|
|
|
|
surface = gtk_icon_theme.load_surface(icon, 16, scale, None, 0)
|
|
|
|
return Gtk.Image.new_from_surface(surface)
|
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
|
2018-06-01 13:54:04 +02:00
|
|
|
|
|
|
|
def get_monitor_scale_factor():
|
|
|
|
display = Gdk.Display.get_default()
|
|
|
|
monitor = display.get_primary_monitor()
|
|
|
|
if monitor is None:
|
|
|
|
log.warning('Could not determine scale factor')
|
|
|
|
return 1
|
|
|
|
return monitor.get_scale_factor()
|