summaryrefslogtreecommitdiff
path: root/examples/roomlist.py
blob: b723a3eed42e4bfb306b5255c0ac179d8296b3ff (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
import dbus.glib
import gobject
import logging
import sys

from time import sleep

from account import connection_from_file

from telepathy.client.channel import Channel
from telepathy.constants import (
    CONNECTION_HANDLE_TYPE_NONE as HANDLE_TYPE_NONE,
    CONNECTION_HANDLE_TYPE_ROOM as HANDLE_TYPE_ROOM,
    CONNECTION_STATUS_CONNECTED,
    CHANNEL_TEXT_MESSAGE_TYPE_NORMAL)
from telepathy.interfaces import CHANNEL_TYPE_ROOM_LIST, CONN_INTERFACE

logging.basicConfig()

class RoomListExample:
    def __init__(self, conn):
        self.conn = conn

        conn[CONN_INTERFACE].connect_to_signal('StatusChanged',
            self.status_changed_cb)

    def run(self):
        print "main loop running"
        self.loop = gobject.MainLoop()
        self.loop.run()

    def quit(self):
        if self.loop:
            self.loop.quit()
            self.loop = None

    def status_changed_cb(self, state, reason):
        if state != CONNECTION_STATUS_CONNECTED:
            return
        print "connection became ready, requesting channel"

        try:
            channel = conn.request_channel(
                CHANNEL_TYPE_ROOM_LIST, HANDLE_TYPE_NONE, 0, True)
        except Exception, e:
            print e
            self.quit()
            return

        print "Connecting to ListingRooms"
        channel[CHANNEL_TYPE_ROOM_LIST].connect_to_signal('ListingRooms',
                                                         self.listing_cb)
        print "Connecting to GotRooms"
        channel[CHANNEL_TYPE_ROOM_LIST].connect_to_signal('GotRooms',
                                                         self.rooms_cb)
        print "Calling ListRooms"
        channel[CHANNEL_TYPE_ROOM_LIST].ListRooms()

    def listing_cb(self, listing):
        if listing:
            print "Listing rooms..."
        else:
            print "Finished listing rooms"
            self.quit()

    def rooms_cb(self, rooms):
        handles = [room[0] for room in rooms]
        names = self.conn[CONN_INTERFACE].InspectHandles(HANDLE_TYPE_ROOM,
                                                         handles)

        for i in xrange(len(rooms)):
            handle, ctype, info = rooms[i]
            name = names[i]
            print "Found room:", name
            print "\t", ctype
            for key in info:
                print "\t", repr(str(key)), " => ", repr(info[key])

if __name__ == '__main__':
    conn = connection_from_file(sys.argv[1])

    ex = RoomListExample(conn)

    print "connecting"
    conn[CONN_INTERFACE].Connect()

    try:
        ex.run()
    except KeyboardInterrupt:
        print "killed"

    print "disconnecting"
    conn[CONN_INTERFACE].Disconnect()