[client_icons] Format code

This commit is contained in:
Daniel Brötzmann
2019-06-22 13:04:54 +02:00
parent d0c300239b
commit 3cc00d278a

View File

@@ -28,15 +28,25 @@ class ClientsIconsPlugin(GajimPlugin):
self.config_dialog = partial(ClientsIconsConfigDialog, self) self.config_dialog = partial(ClientsIconsConfigDialog, self)
self.events_handlers = { self.events_handlers = {
'caps-update': (ged.POSTGUI, self._on_caps_update), 'caps-update': (
ged.POSTGUI, self._on_caps_update),
} }
self.gui_extension_points = { self.gui_extension_points = {
'groupchat_control': (self.connect_with_groupchat_control, 'groupchat_control': (
self.connect_with_groupchat_control,
self.disconnect_from_groupchat_control), self.disconnect_from_groupchat_control),
'roster_draw_contact': (self.connect_with_roster_draw_contact, None), 'roster_draw_contact': (
'roster_tooltip_populate': (self.connect_with_roster_tooltip_populate, None), self.connect_with_roster_draw_contact,
'gc_tooltip_populate': (self.connect_with_gc_tooltip_populate, None), None),
'roster_tooltip_populate': (
self.connect_with_roster_tooltip_populate,
None),
'gc_tooltip_populate': (
self.connect_with_gc_tooltip_populate,
None),
} }
self.config_default_values = { self.config_default_values = {
'show_in_roster': (True, ''), 'show_in_roster': (True, ''),
'show_in_groupchats': (True, ''), 'show_in_groupchats': (True, ''),
@@ -50,7 +60,6 @@ class ClientsIconsPlugin(GajimPlugin):
if _icon_theme is not None: if _icon_theme is not None:
_icon_theme.append_search_path(str(Path(__file__).parent)) _icon_theme.append_search_path(str(Path(__file__).parent))
@staticmethod @staticmethod
def get_client_identity_name(contact): def get_client_identity_name(contact):
identities = contact.client_caps.get_cache_lookup_strategy()( identities = contact.client_caps.get_cache_lookup_strategy()(
@@ -69,39 +78,41 @@ class ClientsIconsPlugin(GajimPlugin):
def add_tooltip_row(self, tooltip, contact, tooltip_grid): def add_tooltip_row(self, tooltip, contact, tooltip_grid):
caps = contact.client_caps._node caps = contact.client_caps._node
caps_image, client_name = self.get_icon(caps, contact, tooltip_grid) caps_image, client_name = self.get_icon(caps, contact, tooltip_grid)
caps_image.set_halign(Gtk.PositionType.RIGHT) caps_image.set_halign(Gtk.Align.END)
# fill clients table # Fill clients grid
self.table = Gtk.Grid() self.grid = Gtk.Grid()
self.table.set_name('client_icons_grid') self.grid.set_name('client_icons_grid')
self.table.set_property('column-spacing', 5) self.grid.set_property('column-spacing', 5)
self.table.attach(caps_image, 1, 1, 1, 1) self.grid.attach(caps_image, 1, 1, 1, 1)
label_name = Gtk.Label() label_name = Gtk.Label()
label_name.set_halign(Gtk.PositionType.RIGHT) label_name.set_halign(Gtk.Align.END)
label_name.set_valign(Gtk.Align.CENTER)
label_name.set_markup(client_name) label_name.set_markup(client_name)
self.table.attach(label_name, 2, 1, 1, 1) self.grid.attach(label_name, 2, 1, 1, 1)
self.table.show_all() self.grid.show_all()
# set label # Set label
label = Gtk.Label() label = Gtk.Label()
label.set_name('client_icons_label') label.set_name('client_icons_label')
label.set_halign(Gtk.PositionType.RIGHT) label.set_halign(Gtk.Align.END)
label.set_valign(Gtk.Align.CENTER)
label.set_markup(_('Client:')) label.set_markup(_('Client:'))
label.show() label.show()
# set client table to tooltip # Set client grid to tooltip
tooltip_grid.insert_next_to(tooltip.resource_label, tooltip_grid.insert_next_to(tooltip.resource_label,
Gtk.PositionType.BOTTOM) Gtk.PositionType.BOTTOM)
tooltip_grid.attach_next_to(label, tooltip.resource_label, tooltip_grid.attach_next_to(label, tooltip.resource_label,
Gtk.PositionType.BOTTOM, 1, 1) Gtk.PositionType.BOTTOM, 1, 1)
tooltip_grid.attach_next_to(self.table, label, tooltip_grid.attach_next_to(self.grid, label,
Gtk.PositionType.RIGHT, 1, 1) Gtk.PositionType.RIGHT, 1, 1)
def connect_with_gc_tooltip_populate(self, tooltip, contact, tooltip_grid): def connect_with_gc_tooltip_populate(self, tooltip, contact, tooltip_grid):
if not self.config['show_in_tooltip']: if not self.config['show_in_tooltip']:
return return
# Check if clients info already attached to tooltip
# Check if clients info already attached to tooltip
node = contact.client_caps._node node = contact.client_caps._node
image, client_name = self.get_icon(node, contact, tooltip_grid) image, client_name = self.get_icon(node, contact, tooltip_grid)
label = Gtk.Label(label=client_name) label = Gtk.Label(label=client_name)
@@ -116,7 +127,8 @@ class ClientsIconsPlugin(GajimPlugin):
tooltip_grid.attach_next_to(box, tooltip._ui.affiliation, tooltip_grid.attach_next_to(box, tooltip._ui.affiliation,
Gtk.PositionType.BOTTOM, 1, 1) Gtk.PositionType.BOTTOM, 1, 1)
def connect_with_roster_tooltip_populate(self, tooltip, contacts, tooltip_grid): def connect_with_roster_tooltip_populate(self, tooltip, contacts,
tooltip_grid):
if not self.config['show_in_tooltip']: if not self.config['show_in_tooltip']:
return return
if len(contacts) == 1 and contacts[0].jid in app.get_our_jids(): if len(contacts) == 1 and contacts[0].jid in app.get_our_jids():
@@ -124,7 +136,7 @@ class ClientsIconsPlugin(GajimPlugin):
if self.is_groupchat(contacts[0]): if self.is_groupchat(contacts[0]):
return return
# put contacts in dict, where key is priority # Put contacts in dict, where key is priority
num_resources = 0 num_resources = 0
contacts_dict = {} contacts_dict = {}
for contact in contacts: for contact in contacts:
@@ -139,29 +151,31 @@ class ClientsIconsPlugin(GajimPlugin):
contact_keys = sorted(contacts_dict.keys()) contact_keys = sorted(contacts_dict.keys())
contact_keys.reverse() contact_keys.reverse()
# fill clients table # Fill clients grid
table = Gtk.Grid() grid = Gtk.Grid()
table.insert_row(0) grid.insert_row(0)
table.insert_row(0) grid.insert_row(0)
table.insert_column(0) grid.insert_column(0)
table.set_property('column-spacing', 2) grid.set_property('column-spacing', 2)
vcard_current_row = 0 vcard_current_row = 0
for priority in contact_keys: for priority in contact_keys:
for acontact in contacts_dict[priority]: for acontact in contacts_dict[priority]:
caps = acontact.client_caps._node caps = acontact.client_caps._node
caps_image, client_name = self.get_icon(caps, acontact, tooltip_grid) caps_image, client_name = self.get_icon(
caps, acontact, tooltip_grid)
caps_image.set_valign(Gtk.Align.START) caps_image.set_valign(Gtk.Align.START)
table.attach(caps_image, 1, vcard_current_row, 1, 1) grid.attach(caps_image, 1, vcard_current_row, 1, 1)
label = Gtk.Label(label=client_name) label = Gtk.Label(label=client_name)
label.set_valign(Gtk.Align.START) label.set_valign(Gtk.Align.START)
label.set_halign(Gtk.Align.START)
label.set_xalign(0) label.set_xalign(0)
table.attach(label, 2, vcard_current_row, 1, 1) grid.attach(label, 2, vcard_current_row, 1, 1)
vcard_current_row += 1 vcard_current_row += 1
table.show_all() grid.show_all()
table.set_valign(Gtk.Align.START) grid.set_valign(Gtk.Align.START)
# set label # Set label
label = Gtk.Label() label = Gtk.Label()
label.set_halign(Gtk.Align.END) label.set_halign(Gtk.Align.END)
label.set_valign(Gtk.Align.START) label.set_valign(Gtk.Align.START)
@@ -170,12 +184,13 @@ class ClientsIconsPlugin(GajimPlugin):
else: else:
label.set_text(_('Client:')) label.set_text(_('Client:'))
label.show() label.show()
# set clients table to tooltip
# Set clients grid to tooltip
tooltip_grid.insert_next_to(tooltip._ui.resource_label, tooltip_grid.insert_next_to(tooltip._ui.resource_label,
Gtk.PositionType.BOTTOM) Gtk.PositionType.BOTTOM)
tooltip_grid.attach_next_to(label, tooltip._ui.resource_label, tooltip_grid.attach_next_to(label, tooltip._ui.resource_label,
Gtk.PositionType.BOTTOM, 1, 1) Gtk.PositionType.BOTTOM, 1, 1)
tooltip_grid.attach_next_to(table, label, tooltip_grid.attach_next_to(grid, label,
Gtk.PositionType.RIGHT, 1, 1) Gtk.PositionType.RIGHT, 1, 1)
def get_icon(self, node, contact, widget): def get_icon(self, node, contact, widget):
@@ -191,13 +206,15 @@ class ClientsIconsPlugin(GajimPlugin):
return return
if self.is_groupchat(contact): if self.is_groupchat(contact):
return return
child_iters = roster._get_contact_iter(jid, account, contact, roster.model) child_iters = roster._get_contact_iter(
jid, account, contact, roster.model)
if not child_iters: if not child_iters:
return return
for iter_ in child_iters: for iter_ in child_iters:
if roster.model[iter_][self.renderer_num] is None: if roster.model[iter_][self.renderer_num] is None:
node = contact.client_caps._node node = contact.client_caps._node
self.set_icon(roster.model, iter_, self.renderer_num, node, contact) self.set_icon(
roster.model, iter_, self.renderer_num, node, contact)
def connect_with_groupchat_control(self, chat_control): def connect_with_groupchat_control(self, chat_control):
chat_control.nb_ext_renderers += 1 chat_control.nb_ext_renderers += 1
@@ -210,10 +227,12 @@ class ClientsIconsPlugin(GajimPlugin):
'client_icon', Gtk.CellRendererPixbuf(), False, 'client_icon', Gtk.CellRendererPixbuf(), False,
'icon_name', self.muc_renderer_num, 'icon_name', self.muc_renderer_num,
self.tree_cell_data_func, chat_control) self.tree_cell_data_func, chat_control)
# remove old column
# Remove old column
chat_control.list_treeview.remove_column( chat_control.list_treeview.remove_column(
chat_control.list_treeview.get_column(0)) chat_control.list_treeview.get_column(0))
# add new renderer in renderers list
# Add new renderer in renderers list
position_list = ['name', 'avatar'] position_list = ['name', 'avatar']
position = position_list[int(self.config['pos_in_list'])] position = position_list[int(self.config['pos_in_list'])]
for renderer in chat_control.renderers_list: for renderer in chat_control.renderers_list:
@@ -221,7 +240,8 @@ class ClientsIconsPlugin(GajimPlugin):
break break
num = chat_control.renderers_list.index(renderer) num = chat_control.renderers_list.index(renderer)
chat_control.renderers_list.insert(num, client_icon_rend) chat_control.renderers_list.insert(num, client_icon_rend)
# fill and append column
# Fill and append column
chat_control.fill_column(col) chat_control.fill_column(col)
chat_control.list_treeview.insert_column(col, 0) chat_control.list_treeview.insert_column(col, 0)
@@ -230,7 +250,7 @@ class ClientsIconsPlugin(GajimPlugin):
chat_control.model.set_sort_column_id(1, Gtk.SortType.ASCENDING) chat_control.model.set_sort_column_id(1, Gtk.SortType.ASCENDING)
chat_control.list_treeview.set_model(chat_control.model) chat_control.list_treeview.set_model(chat_control.model)
# draw roster # Draw roster
for nick in app.contacts.get_nick_list( for nick in app.contacts.get_nick_list(
chat_control.account, chat_control.room_jid): chat_control.account, chat_control.room_jid):
gc_contact = app.contacts.get_gc_contact( gc_contact = app.contacts.get_gc_contact(
@@ -243,7 +263,8 @@ class ClientsIconsPlugin(GajimPlugin):
chat_control.model, iter_, chat_control.model, iter_,
self.muc_renderer_num, caps, gc_contact) self.muc_renderer_num, caps, gc_contact)
chat_control.draw_all_roles() chat_control.draw_all_roles()
# Recalculate column width for ellipsizin
# Recalculate column width for ellipsizing
chat_control.list_treeview.columns_autosize() chat_control.list_treeview.columns_autosize()
def disconnect_from_groupchat_control(self, gc_control): def disconnect_from_groupchat_control(self, gc_control):
@@ -276,9 +297,11 @@ class ClientsIconsPlugin(GajimPlugin):
'client_icon', self.renderer, False, 'client_icon', self.renderer, False,
'icon_name', self.renderer_num, 'icon_name', self.renderer_num,
self._roster_icon_renderer, self.renderer_num) self._roster_icon_renderer, self.renderer_num)
# remove old column
# Remove old column
roster.tree.remove_column(roster.tree.get_column(0)) roster.tree.remove_column(roster.tree.get_column(0))
# add new renderer in renderers list
# Add new renderer in renderers list
position_list = ['name', 'avatar'] position_list = ['name', 'avatar']
position = position_list[int(self.config['pos_in_list'])] position = position_list[int(self.config['pos_in_list'])]
for renderer in roster.renderers_list: for renderer in roster.renderers_list:
@@ -286,10 +309,12 @@ class ClientsIconsPlugin(GajimPlugin):
break break
num = roster.renderers_list.index(renderer) num = roster.renderers_list.index(renderer)
roster.renderers_list.insert(num, client_icon_rend) roster.renderers_list.insert(num, client_icon_rend)
# fill and append column
# Fill and append column
roster.fill_column(col) roster.fill_column(col)
roster.tree.insert_column(col, 0) roster.tree.insert_column(col, 0)
# redraw roster
# Redraw roster
roster.columns += [str] roster.columns += [str]
self.active = True self.active = True
roster.setup_and_draw_roster() roster.setup_and_draw_roster()
@@ -300,7 +325,7 @@ class ClientsIconsPlugin(GajimPlugin):
except TypeError: except TypeError:
return return
# allocate space for the icon only if needed # Allocate space for the icon only if needed
if model[titer][data] is None: if model[titer][data] is None:
renderer.set_property('visible', False) renderer.set_property('visible', False)
else: else:
@@ -310,12 +335,14 @@ class ClientsIconsPlugin(GajimPlugin):
app.interface.roster._set_account_row_background_color(renderer) app.interface.roster._set_account_row_background_color(renderer)
renderer.set_property('xalign', 1) renderer.set_property('xalign', 1)
elif type_: elif type_:
if not model[titer][Column.JID] or not model[titer][Column.ACCOUNT]: if (not model[titer][Column.JID] or
not model[titer][Column.ACCOUNT]):
# This can append at the moment we add the row # This can append at the moment we add the row
return return
jid = model[titer][Column.JID] jid = model[titer][Column.JID]
account = model[titer][Column.ACCOUNT] account = model[titer][Column.ACCOUNT]
app.interface.roster._set_contact_row_background_color(renderer, jid, account) app.interface.roster._set_contact_row_background_color(
renderer, jid, account)
def deactivate(self): def deactivate(self):
self.active = None self.active = None
@@ -335,11 +362,12 @@ class ClientsIconsPlugin(GajimPlugin):
roster.setup_and_draw_roster() roster.setup_and_draw_roster()
def _on_caps_update(self, event): def _on_caps_update(self, event):
# Zeroconf
if event.conn.name == 'Local': if event.conn.name == 'Local':
# zeroconf
return return
contact = self._get_contact_or_gc_contact_for_jid(event.conn.name, event.fjid) contact = self._get_contact_or_gc_contact_for_jid(
event.conn.name, event.fjid)
if contact is None: if contact is None:
return return
@@ -355,28 +383,30 @@ class ClientsIconsPlugin(GajimPlugin):
if contact.is_groupchat: if contact.is_groupchat:
return return
roster = app.interface.roster roster = app.interface.roster
iters = roster._get_contact_iter(event.jid, event.conn.name, contact, iters = roster._get_contact_iter(
roster.model) event.jid, event.conn.name, contact, roster.model)
iter_ = iters[0] iter_ = iters[0]
# highest contact changed # Highest contact changed
caps = contact.client_caps._node caps = contact.client_caps._node
if not caps: if not caps:
return return
if roster.model[iter_][self.renderer_num] is not None: if roster.model[iter_][self.renderer_num] is not None:
self.set_icon(roster.model, iter_, self.renderer_num, caps, contact) self.set_icon(
roster.model, iter_, self.renderer_num, caps, contact)
return return
for iter_ in iters: for iter_ in iters:
self.set_icon(roster.model, iter_, self.renderer_num, caps, contact) self.set_icon(
roster.model, iter_, self.renderer_num, caps, contact)
def _draw_gc_contact(self, event, contact): def _draw_gc_contact(self, event, contact):
if not self.config['show_in_groupchats']: if not self.config['show_in_groupchats']:
return return
control = app.interface.msg_win_mgr.get_gc_control(contact.room_jid, control = app.interface.msg_win_mgr.get_gc_control(
event.conn.name) contact.room_jid, event.conn.name)
if control is None: if control is None:
return return
iter_ = control.get_contact_iter(contact.name) iter_ = control.get_contact_iter(contact.name)
@@ -385,7 +415,8 @@ class ClientsIconsPlugin(GajimPlugin):
caps = contact.client_caps._node caps = contact.client_caps._node
if not caps: if not caps:
return return
self.set_icon(control.model, iter_, self.muc_renderer_num, caps, contact) self.set_icon(
control.model, iter_, self.muc_renderer_num, caps, contact)
def _get_contact_or_gc_contact_for_jid(self, account, fjid): def _get_contact_or_gc_contact_for_jid(self, account, fjid):
contact = app.contacts.get_contact_from_full_jid(account, fjid) contact = app.contacts.get_contact_from_full_jid(account, fjid)
@@ -421,8 +452,8 @@ class ClientsIconsPlugin(GajimPlugin):
if not contact: if not contact:
return return
bgcolor = app.config.get_per('themes', app.config.get( bgcolor = app.config.get_per(
'roster_theme'), 'contactbgcolor') 'themes', app.config.get('roster_theme'), 'contactbgcolor')
if bgcolor: if bgcolor:
renderer.set_property('cell-background', bgcolor) renderer.set_property('cell-background', bgcolor)
else: else: