From 545d25ca93e02cc7c2f680f327f2ee457ac532c8 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Daniel=20Br=C3=B6tzmann?= <mailtrash@posteo.de>
Date: Fri, 22 Nov 2019 22:29:33 +0100
Subject: [PATCH] ServerInfo: Add contact addresses

---
 gajim/data/gui/server_info.ui | 33 ++++++++++++---
 gajim/gtk/server_info.py      | 77 +++++++++++++++++++++++++++++++++++
 2 files changed, 105 insertions(+), 5 deletions(-)

diff --git a/gajim/data/gui/server_info.ui b/gajim/data/gui/server_info.ui
index 40bbe259fe..0ad88ffc41 100644
--- a/gajim/data/gui/server_info.ui
+++ b/gajim/data/gui/server_info.ui
@@ -9,6 +9,7 @@
       <object class="GtkGrid" id="server">
         <property name="visible">True</property>
         <property name="can_focus">False</property>
+        <property name="halign">center</property>
         <property name="row_spacing">6</property>
         <property name="column_spacing">12</property>
         <child>
@@ -101,22 +102,44 @@
           </packing>
         </child>
         <child>
-          <object class="GtkBox" id="spacer1">
-            <property name="width_request">160</property>
+          <object class="GtkLabel">
             <property name="visible">True</property>
             <property name="can_focus">False</property>
-            <child>
-              <placeholder/>
-            </child>
+            <property name="margin_top">12</property>
+            <property name="label" translatable="yes">Contact Addresses</property>
+            <style>
+              <class name="bold16"/>
+            </style>
           </object>
           <packing>
             <property name="left_attach">0</property>
             <property name="top_attach">3</property>
+            <property name="width">2</property>
+          </packing>
+        </child>
+        <child>
+          <object class="GtkLabel" id="no_addresses_label">
+            <property name="can_focus">False</property>
+            <property name="no_show_all">True</property>
+            <property name="label" translatable="yes">No contact addresses published for this server.</property>
+            <property name="wrap">True</property>
+            <property name="max_width_chars">50</property>
+            <style>
+              <class name="dim-label"/>
+            </style>
+          </object>
+          <packing>
+            <property name="left_attach">0</property>
+            <property name="top_attach">4</property>
+            <property name="width">2</property>
           </packing>
         </child>
         <child>
           <placeholder/>
         </child>
+        <child>
+          <placeholder/>
+        </child>
         <style>
           <class name="margin-18"/>
         </style>
diff --git a/gajim/gtk/server_info.py b/gajim/gtk/server_info.py
index 832ac85361..b14eda5256 100644
--- a/gajim/gtk/server_info.py
+++ b/gajim/gtk/server_info.py
@@ -20,9 +20,11 @@
 from nbxmpp.util import is_error_result
 from gi.repository import Gtk
 from gi.repository import Gdk
+from gi.repository import Pango
 
 from gajim.common import app
 from gajim.common import ged
+from gajim.common.helpers import open_uri
 from gajim.common.i18n import _
 
 from gajim.gtk.util import ensure_not_destroyed
@@ -66,6 +68,9 @@ def __init__(self, account):
             self.hostname, callback=self._software_version_received)
         self.request_last_activity()
 
+        server_info = con.get_module('Discovery').server_info
+        self._add_contact_addresses(server_info.dataforms)
+
         self.cert = con.connection.Connection.ssl_certificate
         self._add_connection_info()
 
@@ -142,6 +147,78 @@ def request_last_activity(self):
         iq = nbxmpp.Iq(to=self.hostname, typ='get', queryNS=nbxmpp.NS_LAST)
         con.connection.SendAndCallForResponse(iq, self._on_last_activity)
 
+    def _add_contact_addresses(self, dataforms):
+        fields = {
+            'admin-addresses': _('Admin'),
+            'support-addresses': _('Support'),
+            'security-addresses': _('Security'),
+            'feedback-addresses': _('Feedback'),
+            'abuse-addresses': _('Abuse'),
+            'sales-addresses': _('Sales'),
+        }
+
+        addresses = self._get_addresses(fields, dataforms)
+        if addresses is None:
+            self._ui.no_addresses_label.set_visible(True)
+            return
+
+        row_count = 4
+        for address_type, values in addresses.items():
+            label = self._get_address_type_label(fields[address_type])
+            self._ui.server.attach(label, 0, row_count, 1, 1)
+            for index, value in enumerate(values):
+                last = index == len(values) - 1
+                label = self._get_address_label(value, last=last)
+                self._ui.server.attach(label, 1, row_count, 1, 1)
+                row_count += 1
+
+    @staticmethod
+    def _get_addresses(fields, dataforms):
+        addresses = {}
+        for form in dataforms:
+            type_ = form.vars.get('FORM_TYPE')
+            if type_ == 'http://jabber.org/network/serverinfo':
+                continue
+
+            for address_type in fields:
+                field = form.vars.get(address_type)
+                if field is None:
+                    continue
+
+                if field.type_ != 'list-multi':
+                    continue
+
+                if not field.values:
+                    continue
+                addresses[address_type] = field.values
+
+            return addresses or None
+        return None
+
+    @staticmethod
+    def _get_address_type_label(text):
+        label = Gtk.Label(label=text)
+        label.set_halign(Gtk.Align.END)
+        label.set_valign(Gtk.Align.START)
+        label.get_style_context().add_class('dim-label')
+        return label
+
+    def _get_address_label(self, address, last=False):
+        label = Gtk.Label()
+        label.set_markup('<a href="%s">%s</a>' % (address, address))
+        label.set_ellipsize(Pango.EllipsizeMode.END)
+        label.set_xalign(0)
+        label.set_halign(Gtk.Align.START)
+        label.get_style_context().add_class('link-button')
+        label.connect('activate-link', self._on_activate_link)
+        if last:
+            label.set_margin_bottom(6)
+        return label
+
+    def _on_activate_link(self, label, *args):
+        open_uri(label.get_text(), account=self.account)
+        return Gdk.EVENT_STOP
+
     def _on_last_activity(self, stanza):
         if self._destroyed:
             # Window got closed in the meantime
-- 
GitLab