summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKrzysztof Klinikowski <kkszysiu@gmail.com>2009-12-26 23:25:40 +0100
committerKrzysztof Klinikowski <kkszysiu@gmail.com>2009-12-26 23:25:40 +0100
commit8e6896064260b232eb225307021c09e5a5266a49 (patch)
tree860714380b8cdbec1b47a1bc76c522e0a6cfb546
parent54c30801b129e01cd1081c4f779e52b832aa317f (diff)
A bit of work related with groups.
-rw-r--r--gadu/channel/group.py224
-rw-r--r--gadu/channel_manager.py3
-rw-r--r--gadu/connection.py53
-rw-r--r--gadu/handle.py8
4 files changed, 152 insertions, 136 deletions
diff --git a/gadu/channel/group.py b/gadu/channel/group.py
index c88cb96..8372d18 100644
--- a/gadu/channel/group.py
+++ b/gadu/channel/group.py
@@ -35,114 +35,122 @@ class GaduGroupChannel(GaduListChannel):
def __init__(self, connection, manager, props):
self.__pending_add = []
self.__pending_remove = []
- ButterflyListChannel.__init__(self, connection, manager, props)
+ self.conn = connection
+ GaduListChannel.__init__(self, connection, manager, props)
self.GroupFlagsChanged(telepathy.CHANNEL_GROUP_FLAG_CAN_ADD |
telepathy.CHANNEL_GROUP_FLAG_CAN_REMOVE, 0)
+# @async
+# def create_group():
+# if self._handle.group is None:
+# name = self._handle.name.encode("utf-8")
+# connection.msn_client.address_book.add_group(name)
+# create_group()
- def AddMembers(self, contacts, message):
- ab = self._conn.msn_client.address_book
- if self._handle.group is None:
- for contact_handle_id in contacts:
- contact_handle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT,
- contact_handle_id)
- logger.info("Adding contact %s to pending group %s" %
- (unicode(contact_handle), unicode(self._handle)))
- if contact_handle_id in self.__pending_remove:
- self.__pending_remove.remove(contact_handle_id)
- else:
- self.__pending_add.append(contact_handle_id)
- return
- else:
- for contact_handle_id in contacts:
- contact_handle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT,
- contact_handle_id)
- logger.info("Adding contact %s to group %s" %
- (unicode(contact_handle), unicode(self._handle)))
- contact = contact_handle.contact
- group = self._handle.group
- if contact is not None and contact.is_member(papyon.Membership.FORWARD):
- ab.add_contact_to_group(group, contact)
- else:
- contact_handle.pending_groups.add(group)
-
- def RemoveMembers(self, contacts, message):
- ab = self._conn.msn_client.address_book
- if self._handle.group is None:
- for contact_handle_id in contacts:
- contact_handle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT,
- contact_handle_id)
- logger.info("Adding contact %s to pending group %s" %
- (unicode(contact_handle), unicode(self._handle)))
- if contact_handle_id in self.__pending_add:
- self.__pending_add.remove(contact_handle_id)
- else:
- self.__pending_remove.append(contact_handle_id)
- return
- else:
- for contact_handle_id in contacts:
- contact_handle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT,
- contact_handle_id)
- logger.info("Removing contact %s from pending group %s" %
- (unicode(contact_handle), unicode(self._handle)))
- contact = contact_handle.contact
- group = self._handle.group
- if contact is not None and contact.is_member(papyon.Membership.FORWARD):
- ab.delete_contact_from_group(group, contact)
- else:
- contact_handle.pending_groups.discard(group)
-
- def Close(self):
- logger.debug("Deleting group %s" % self._handle.name)
- ab = self._conn.msn_client.address_book
- group = self._handle.group
- ab.delete_group(group)
-
- def _filter_contact(self, contact):
- if contact.is_member(papyon.Membership.FORWARD):
- for group in contact.groups:
- if group.name.decode("utf-8") == self._handle.name:
- return (True, False, False)
- return (False, False, False)
-
- def on_addressbook_group_added(self, group):
- if group.name.decode("utf-8") == self._handle.name:
- self.AddMembers(self.__pending_add, None)
- self.__pending_add = []
- self.RemoveMembers(self.__pending_remove, None)
- self.__pending_remove = []
-
- def on_addressbook_group_deleted(self, group):
- if group.name.decode("utf-8") == self._handle.name:
- self.Closed()
- self._conn.remove_channel(self)
-
- def on_addressbook_group_contact_added(self, group, contact):
- group_name = group.name.decode("utf-8")
- if group_name == self._handle.name:
- handle = ButterflyHandleFactory(self._conn_ref(), 'contact',
- contact.account, contact.network_id)
-
- added = set()
- added.add(handle)
-
- self.MembersChanged('', added, (), (), (), 0,
- telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE)
-
- logger.debug("Contact %s added to group %s" %
- (handle.name, group_name))
-
- def on_addressbook_group_contact_deleted(self, group, contact):
- group_name = group.name.decode("utf-8")
- if group_name == self._handle.name:
- handle = ButterflyHandleFactory(self._conn_ref(), 'contact',
- contact.account, contact.network_id)
-
- removed = set()
- removed.add(handle)
-
- self.MembersChanged('', (), removed, (), (), 0,
- telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE)
-
- logger.debug("Contact %s removed from group %s" %
- (handle.name, group_name))
-
+#
+# def AddMembers(self, contacts, message):
+# ab = self._conn.msn_client.address_book
+# if self._handle.group is None:
+# for contact_handle_id in contacts:
+# contact_handle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT,
+# contact_handle_id)
+# logger.info("Adding contact %s to pending group %s" %
+# (unicode(contact_handle), unicode(self._handle)))
+# if contact_handle_id in self.__pending_remove:
+# self.__pending_remove.remove(contact_handle_id)
+# else:
+# self.__pending_add.append(contact_handle_id)
+# return
+# else:
+# for contact_handle_id in contacts:
+# contact_handle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT,
+# contact_handle_id)
+# logger.info("Adding contact %s to group %s" %
+# (unicode(contact_handle), unicode(self._handle)))
+# contact = contact_handle.contact
+# group = self._handle.group
+# if contact is not None and contact.is_member(papyon.Membership.FORWARD):
+# ab.add_contact_to_group(group, contact)
+# else:
+# contact_handle.pending_groups.add(group)
+#
+# def RemoveMembers(self, contacts, message):
+# ab = self._conn.msn_client.address_book
+# if self._handle.group is None:
+# for contact_handle_id in contacts:
+# contact_handle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT,
+# contact_handle_id)
+# logger.info("Adding contact %s to pending group %s" %
+# (unicode(contact_handle), unicode(self._handle)))
+# if contact_handle_id in self.__pending_add:
+# self.__pending_add.remove(contact_handle_id)
+# else:
+# self.__pending_remove.append(contact_handle_id)
+# return
+# else:
+# for contact_handle_id in contacts:
+# contact_handle = self._conn.handle(telepathy.HANDLE_TYPE_CONTACT,
+# contact_handle_id)
+# logger.info("Removing contact %s from pending group %s" %
+# (unicode(contact_handle), unicode(self._handle)))
+# contact = contact_handle.contact
+# group = self._handle.group
+# if contact is not None and contact.is_member(papyon.Membership.FORWARD):
+# ab.delete_contact_from_group(group, contact)
+# else:
+# contact_handle.pending_groups.discard(group)
+#
+# def Close(self):
+# logger.debug("Deleting group %s" % self._handle.name)
+# ab = self._conn.msn_client.address_book
+# group = self._handle.group
+# ab.delete_group(group)
+#
+# def _filter_contact(self, contact):
+# if contact.is_member(papyon.Membership.FORWARD):
+# for group in contact.groups:
+# if group.name.decode("utf-8") == self._handle.name:
+# return (True, False, False)
+# return (False, False, False)
+#
+# def on_addressbook_group_added(self, group):
+# if group.name.decode("utf-8") == self._handle.name:
+# self.AddMembers(self.__pending_add, None)
+# self.__pending_add = []
+# self.RemoveMembers(self.__pending_remove, None)
+# self.__pending_remove = []
+#
+# def on_addressbook_group_deleted(self, group):
+# if group.name.decode("utf-8") == self._handle.name:
+# self.Closed()
+# self._conn.remove_channel(self)
+#
+# def on_addressbook_group_contact_added(self, group, contact):
+# group_name = group.name.decode("utf-8")
+# if group_name == self._handle.name:
+# handle = ButterflyHandleFactory(self._conn_ref(), 'contact',
+# contact.account, contact.network_id)
+#
+# added = set()
+# added.add(handle)
+#
+# self.MembersChanged('', added, (), (), (), 0,
+# telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE)
+#
+# logger.debug("Contact %s added to group %s" %
+# (handle.name, group_name))
+#
+# def on_addressbook_group_contact_deleted(self, group, contact):
+# group_name = group.name.decode("utf-8")
+# if group_name == self._handle.name:
+# handle = ButterflyHandleFactory(self._conn_ref(), 'contact',
+# contact.account, contact.network_id)
+#
+# removed = set()
+# removed.add(handle)
+#
+# self.MembersChanged('', (), removed, (), (), 0,
+# telepathy.CHANNEL_GROUP_CHANGE_REASON_NONE)
+#
+# logger.debug("Contact %s removed from group %s" %
+# (handle.name, group_name))
+#
diff --git a/gadu/channel_manager.py b/gadu/channel_manager.py
index c51b8c0..01703fc 100644
--- a/gadu/channel_manager.py
+++ b/gadu/channel_manager.py
@@ -21,10 +21,9 @@ import weakref
import dbus
import telepathy
-#import papyon
from gadu.channel.contact_list import GaduContactListChannelFactory
-#from butterfly.channel.group import ButterflyGroupChannel
+from gadu.channel.group import GaduGroupChannel
from gadu.channel.text import GaduTextChannel
#from butterfly.channel.media import ButterflyMediaChannel
from gadu.handle import GaduHandleFactory
diff --git a/gadu/connection.py b/gadu/connection.py
index d0a0454..310bbfb 100644
--- a/gadu/connection.py
+++ b/gadu/connection.py
@@ -7,15 +7,13 @@ import logging
import xml.etree.ElementTree as ET
from gadu.lqsoft.pygadu.twisted_protocol import GaduClient
-from gadu.lqsoft.pygadu.models import GaduProfile, GaduContact
+from gadu.lqsoft.pygadu.models import GaduProfile, GaduContact, GaduContactGroup
from twisted.internet import reactor, protocol
from twisted.python import log
import dbus
import telepathy
-#import papyon
-#import papyon.event
from gadu.presence import GaduPresence
from gadu.aliasing import GaduAliasing
@@ -107,28 +105,29 @@ class GaduConfig(object):
def get_contacts_count(self):
return self.contacts_count
-class GaduClientFactory(protocol.ClientFactory, protocol.ReconnectingClientFactory):
+#class GaduClientFactory(protocol.ClientFactory, protocol.ReconnectingClientFactory):
+class GaduClientFactory(protocol.ClientFactory):
def __init__(self, config):
self.config = config
def buildProtocol(self, addr):
# connect using current selected profile
- self.resetDelay()
+ #self.resetDelay()
return GaduClient(self.config)
def startedConnecting(self, connector):
logger.info('Started to connect.')
def clientConnectionLost(self, connector, reason):
- logger.info('Lost connection. Reason:', reason)
- protocol.ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
- # connector.connect()
- #reactor.stop()
+ logger.info('Lost connection. Reason: %s' % (reason))
+ #protocol.ReconnectingClientFactory.clientConnectionLost(self, connector, reason)
+ #connector.connect()
+ reactor.stop()
def clientConnectionFailed(self, connector, reason):
- logger.info('Connection failed. Reason:', reason)
- protocol.ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
- ##reactor.stop()
+ logger.info('Connection failed. Reason: %s' % (reason))
+ #protocol.ReconnectingClientFactory.clientConnectionFailed(self, connector, reason)
+ reactor.stop()
class GaduConnection(telepathy.server.Connection,
telepathy.server.ConnectionInterfaceRequests,
@@ -186,7 +185,12 @@ class GaduConnection(telepathy.server.Connection,
self.profile.addContact( c )
except:
pass
-
+
+ for group_from_list in contacts_list['groups']:
+ g = GaduContactGroup.from_xml(group_from_list)
+ if g.Name:
+ self.profile.addGroup(g)
+
logger.info("We have %s contacts in file." % (self.configfile.get_contacts_count()))
@@ -338,7 +342,6 @@ class GaduConnection(telepathy.server.Connection,
return props
-
@dbus.service.method(telepathy.CONNECTION, in_signature='suub',
out_signature='o', async_callbacks=('_success', '_error'))
def RequestChannel(self, type, handle_type, handle_id, suppress_handler,
@@ -369,7 +372,6 @@ class GaduConnection(telepathy.server.Connection,
Returns:
handle_id -- ID for the given username
"""
-
handle_id = 0
for handle in self._handles.values():
if handle.get_name() == name:
@@ -378,7 +380,6 @@ class GaduConnection(telepathy.server.Connection,
return handle_id
-
def updateContactsFile(self):
"""Method that updates contact file when it changes and in loop every 5 seconds."""
self.configfile.make_contacts_file(self.profile.groups, self.profile.contacts)
@@ -395,7 +396,17 @@ class GaduConnection(telepathy.server.Connection,
# props = self._generate_props(telepathy.CHANNEL_TYPE_CONTACT_LIST,
# handle, False)
# self._channel_manager.channel_for_props(props, signal=True)
-
+
+ def makeTelepathyGroupChannels(self):
+ logger.debug("Method makeTelepathyGroupChannels called.")
+ for group in self.profile.groups:
+ handle = GaduHandleFactory(self, 'group',
+ group.Name)
+ props = self._generate_props(
+ telepathy.CHANNEL_TYPE_CONTACT_LIST, handle, False)
+ self._channel_manager.channel_for_props(props, signal=True)
+
+
def on_contactsImported(self):
#TODO: that contacts should be written into XML file with contacts. I need to write it :)
logger.info("No contacts in the XML contacts file yet. Contacts imported.")
@@ -404,7 +415,8 @@ class GaduConnection(telepathy.server.Connection,
reactor.callLater(5, self.updateContactsFile)
self.makeTelepathyContactsChannel()
-
+ self.makeTelepathyGroupChannels()
+
self._status = telepathy.CONNECTION_STATUS_CONNECTED
self.StatusChanged(telepathy.CONNECTION_STATUS_CONNECTED,
telepathy.CONNECTION_STATUS_REASON_REQUESTED)
@@ -420,6 +432,7 @@ class GaduConnection(telepathy.server.Connection,
reactor.callLater(5, self.updateContactsFile)
self.makeTelepathyContactsChannel()
+ #self.makeTelepathyGroupChannels()
self._status = telepathy.CONNECTION_STATUS_CONNECTED
self.StatusChanged(telepathy.CONNECTION_STATUS_CONNECTED,
@@ -433,11 +446,9 @@ class GaduConnection(telepathy.server.Connection,
reactor.stop()
def on_updateContact(self, contact):
- #handle = GaduHandleFactory(self, 'contact',
- # contact.uin, None)
handle_id = self.get_handle_id_by_name(telepathy.constants.HANDLE_TYPE_CONTACT, str(contact.uin))
handle = self.handle(telepathy.constants.HANDLE_TYPE_CONTACT, handle_id)
- logger.info("Method on_updateContact called. Status changed for UIN: %s, handle_id: %s, contact_status: %s, contact_description: %s" % (contact.uin, handle.id, contact.status, contact.description))
+ logger.info("Method on_updateContact called, status changed for UIN: %s, id: %s, status: %s, description: %s" % (contact.uin, handle.id, contact.status, contact.description))
self._presence_changed(handle, contact.status, contact.get_desc())
def on_messageReceived(self, msg):
diff --git a/gadu/handle.py b/gadu/handle.py
index e2c3ad5..e86ec06 100644
--- a/gadu/handle.py
+++ b/gadu/handle.py
@@ -20,7 +20,6 @@ import logging
import weakref
import telepathy
-#import papyon
__all__ = ['GaduHandleFactory']
@@ -120,13 +119,12 @@ class GaduGroupHandle(GaduHandle):
def __init__(self, connection, id, group_name):
handle_type = telepathy.HANDLE_TYPE_GROUP
handle_name = group_name
+ self._connection = connection
GaduHandle.__init__(self, connection, id, handle_type, handle_name)
@property
def group(self):
- for group in self._conn.msn_client.address_book.groups:
- # Microsoft seems to like case insensitive stuff
- if group.name.decode("utf-8").lower() == self.name.lower():
+ for group in self._connection.gadu_client.groups:
+ if group.Name == handle_name:
return group
return None
-