summaryrefslogtreecommitdiff
path: root/plugins/telepathy-gabble-xmpp-console
blob: 961f1a5e4731c2d1a36de498feba5d9ab44887e5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#!/usr/bin/env python
# vim: set fileencoding=utf-8 sts=4 sw=4 et :
"""
The world's worst XMPP console user interface.

Pass it the bus name of a Gabble connection; type some words; get minimalistic
error reporting.

Copyright © 2011 Collabora Ltd. <http://www.collabora.co.uk/>

This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.

This library 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
Lesser General Public License for more details.

You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
"""

import sys
import re
import xml.dom.minidom

import pygtk
pygtk.require('2.0')

from gi.repository import Gtk
from gi.repository import Gio
from gi.repository import GtkSource

PADDING = 6

def pathify(name):
    return '/' + name.replace('.', '/')

CONN_FUTURE_IFACE = "org.freedesktop.Telepathy.Connection.FUTURE"
CONSOLE_IFACE = "org.freedesktop.Telepathy.Gabble.Plugin.Console"

class IQPage(Gtk.Grid):
    REPLY_PAGE = 0
    SPINNER_PAGE = 1

    def __init__(self, console_proxy):
        Gtk.Grid.__init__(self)

        self.console_proxy = console_proxy

        self.set_column_spacing(PADDING)
        self.set_row_spacing(PADDING)

        request_label = self.add_title("Request")

        recipient_label, recipient_entry = self.add_label_entry_pair(
            'To:', below=request_label)
        self.recipient_entry = recipient_entry

        type_label = self.add_label('IQ Type:', below=recipient_label)
        self.get_button = Gtk.RadioButton.new_with_label([], "get")
        self.get_button.set_active(True)
        self.set_button = Gtk.RadioButton.new_with_label_from_widget(
            self.get_button, "set")

        box = Gtk.ButtonBox.new(Gtk.Orientation.HORIZONTAL)
        box.set_layout(Gtk.ButtonBoxStyle.START)
        box.add(self.get_button)
        box.add(self.set_button)
        self.attach_next_to(box, type_label,
            Gtk.PositionType.RIGHT, 1, 1)

        body_label, body_entry = self.add_label_entry_pair(
            'Body:', below=type_label)
        body_entry.set_text(
            "<query xmlns='http://jabber.org/protocol/disco#info'/>")
        body_entry.set_icon_from_stock(
            Gtk.EntryIconPosition.SECONDARY, Gtk.STOCK_GO_FORWARD)
        body_entry.set_icon_tooltip_text(
            Gtk.EntryIconPosition.SECONDARY, "Send this IQ")
        self.body_entry = body_entry

        reply_label = self.add_title("Reply", below=body_label)

        self.b = GtkSource.Buffer()
        tv = GtkSource.View.new_with_buffer(self.b)
        self.b.set_language(
            GtkSource.LanguageManager.get_default().get_language('xml'))
        self.b.set_highlight_matching_brackets(False)
        tv.set_editable(False)
        tv.set_wrap_mode(Gtk.WrapMode.WORD_CHAR)
        tv.set_property('expand', True)
        tv.get_buffer().set_text(
            "<!-- send a request to see the reply here -->")

        sw = Gtk.ScrolledWindow()
        sw.add(tv)

        self.result_nb = Gtk.Notebook()
        self.result_nb.set_show_tabs(False)
        self.result_nb.insert_page(sw, None, self.REPLY_PAGE)

        self.spinner = Gtk.Spinner()
        self.spinner.set_property('halign', Gtk.Align.CENTER)
        self.spinner.set_property('valign', Gtk.Align.CENTER)
        self.spinner.set_property('width-request', 32)
        self.spinner.set_property('height-request', 32)
        self.result_nb.insert_page(self.spinner, None, self.SPINNER_PAGE)

        self.attach_next_to(self.result_nb, reply_label, Gtk.PositionType.BOTTOM, 2, 1)

        body_entry.connect('activate', self.send_iq)
        body_entry.connect('icon-release', self.send_iq)

    def add_title(self, title, below=None):
        label = Gtk.Label()
        label.set_markup("<b>%s</b>" % title)
        label.set_property('xalign', 0)

        if below is None:
            self.attach(label, 0, 0, 2, 1)
        else:
            self.attach_next_to(label, below, Gtk.PositionType.BOTTOM, 2, 1)

        return label

    def add_label(self, title, below):
        label = Gtk.Label(title)
        label.set_property('margin-left', PADDING)
        label.set_property('xalign', 0)
        self.attach_next_to(label, below, Gtk.PositionType.BOTTOM, 1, 1)
        return label

    def add_label_entry_pair(self, title, below):
        label = self.add_label(title, below)

        entry = Gtk.Entry()
        entry.set_property('margin-right', PADDING)
        entry.set_property('hexpand', True)

        self.attach_next_to(entry, label, Gtk.PositionType.RIGHT, 1, 1)

        return label, entry

    def send_iq(self, *misc):
        type = 'get' if self.get_button.get_active() else 'set'
        to = self.recipient_entry.get_text()
        body = self.body_entry.get_text()

        self.console_proxy.SendIQ('(sss)', type, to, body, result_handler=self.send_iq_cb)
        self.result_nb.set_current_page(self.SPINNER_PAGE)
        self.spinner.start()

    def send_iq_cb(self, proxy, result, user_data):
        try:
            reply_type, reply = result
            pretty_reply = xml.dom.minidom.parseString(reply).toprettyxml()
            self.b.set_text(pretty_reply)
        except Exception, e:
            self.b.set_text("<!-- error:\n%s\n-->" % e)

        self.spinner.stop()
        self.result_nb.set_current_page(self.REPLY_PAGE)


class Window(Gtk.Window):
    def __init__(self, bus, connection_bus_name):
        Gtk.Window.__init__(self)

        self.set_title('XMPP Console')
        self.set_default_size(600, 371)

        conn_future_proxy = Gio.DBusProxy.new_sync(bus, 0, None,
            connection_bus_name, pathify(connection_bus_name),
            CONN_FUTURE_IFACE, None)
        try:
            sidecar_path, _ = conn_future_proxy.EnsureSidecar('(s)', CONSOLE_IFACE)
        except Exception, e:
            print """
Couldn't connect to the XMPP console interface on '%(connection_bus_name)s':
  %(e)s
Check that it's a running Jabber connection, and that you have the console
plugin installed.""" % locals()

            raise SystemExit(2)

        self.console_proxy = Gio.DBusProxy.new_sync(bus, 0, None,
            connection_bus_name, sidecar_path, CONSOLE_IFACE, None)

        # Build up the UI
        self.grid = IQPage(self.console_proxy)
        self.add(self.grid)

if __name__ == '__main__':
    bus = Gio.bus_get_sync(Gio.BusType.SESSION, None)

    PREFIX = 'org.freedesktop.Telepathy.Connection.gabble.jabber.'

    if len(sys.argv) != 2 or \
       not re.match('^%s[a-zA-Z0-9_]+$' % PREFIX, sys.argv[1]):
        print "Usage: %s %ssomething" % (sys.argv[0], PREFIX)
        print "  (the argument should be the bus name of a Jabber connection)"
        raise SystemExit(1)

    win = Window(bus, sys.argv[1])
    win.show_all()
    win.connect('destroy', Gtk.main_quit)

    Gtk.main()

"""
           .,,.
         ,;;*;;;;,
        .-'``;-');;.
       /'  .-.  /*;;
     .'    \d    \;;               .;;;,
    / o      `    \;    ,__.     ,;*;;;*;,
    \__, _.__,'   \_.-') __)--.;;;;;*;;;;,
     `""`;;;\       /-')_) __)  `\' ';;;;;;
        ;*;;;        -') `)_)  |\ |  ;;;;*;
        ;;;;|        `---`    O | | ;;*;;;
        *;*;\|                 O  / ;;;;;*
       ;;;;;/|    .-------\      / ;*;;;;;
      ;;;*;/ \    |        '.   (`. ;;;*;;;
      ;;;;;'. ;   |          )   \ | ;;;;;;
      ,;*;;;;\/   |.        /   /` | ';;;*;
       ;;;;;;/    |/       /   /__/   ';;;
       '*jgs/     |       /    |      ;*;
            `""""`        `""""`     ;'
"""