From e32b56e234f6722a9954d1a98e354481b2360ab5 Mon Sep 17 00:00:00 2001 From: Damian Minkov Date: Tue, 19 Nov 2013 15:44:48 +0200 Subject: [PATCH] Handles more cases while moving contacts and renaming groups from remote logged in locations. --- .../MetaContactListServiceImpl.java | 17 ++-- .../ServerStoredContactListJabberImpl.java | 90 +++++++++++++++++-- .../contactlist/TestMetaContactList.java | 2 +- 3 files changed, 95 insertions(+), 14 deletions(-) diff --git a/src/net/java/sip/communicator/impl/contactlist/MetaContactListServiceImpl.java b/src/net/java/sip/communicator/impl/contactlist/MetaContactListServiceImpl.java index bbf7545eb..db44fa1ad 100644 --- a/src/net/java/sip/communicator/impl/contactlist/MetaContactListServiceImpl.java +++ b/src/net/java/sip/communicator/impl/contactlist/MetaContactListServiceImpl.java @@ -2792,7 +2792,6 @@ public void groupResolved(ServerStoredGroupEvent evt) */ public void groupRemoved(ServerStoredGroupEvent evt) { - if (logger.isTraceEnabled()) logger.trace("ContactGroup removed: " + evt); @@ -2810,10 +2809,10 @@ public void groupRemoved(ServerStoredGroupEvent evt) removeContactGroupFromMetaContactGroup(metaContactGroup, evt.getSourceGroup(), evt.getSourceProvider()); - //do not remove the meta contact group even if this is the las - //protocol specific contact group. Contrary to contacts, meta - //contact groups are to only be remove upon user indication or - //otherwise it would be difficult for a user to create a new grp. + if(metaContactGroup.countContactGroups() == 0) + { + removeMetaContactGroup(metaContactGroup); + } } /** @@ -2830,6 +2829,14 @@ public void groupNameChanged(ServerStoredGroupEvent evt) MetaContactGroup metaContactGroup = findMetaContactGroupByContactGroup(evt.getSourceGroup()); + if(metaContactGroup.countContactGroups() == 1) + { + // if the only group contained in this group is renamed + // rename it + ((MetaContactGroupImpl)metaContactGroup) + .setGroupName(evt.getSourceGroup().getGroupName()); + } + fireMetaContactGroupEvent( metaContactGroup , evt.getSourceProvider() diff --git a/src/net/java/sip/communicator/impl/protocol/jabber/ServerStoredContactListJabberImpl.java b/src/net/java/sip/communicator/impl/protocol/jabber/ServerStoredContactListJabberImpl.java index c9298e831..315a8f443 100644 --- a/src/net/java/sip/communicator/impl/protocol/jabber/ServerStoredContactListJabberImpl.java +++ b/src/net/java/sip/communicator/impl/protocol/jabber/ServerStoredContactListJabberImpl.java @@ -341,7 +341,8 @@ private ContactGroupJabberImpl findContactGroupByNameCopy(String name) ContactGroupJabberImpl contactGroup = (ContactGroupJabberImpl) contactGroups.next(); - if (contactGroup.getNameCopy().trim().equals(name)) + if (contactGroup.getNameCopy() != null + && contactGroup.getNameCopy().trim().equals(name)) return contactGroup; } @@ -639,7 +640,7 @@ public void createGroup(String groupName) ContactGroupJabberImpl newGroup = new ContactGroupJabberImpl(newRosterGroup, - new Vector().iterator(), + new ArrayList().iterator(), this, true); rootGroup.addSubGroup(newGroup); @@ -1364,7 +1365,8 @@ public void entriesUpdated(Collection addresses) for (RosterGroup gr : entry.getGroups()) { - if(findContactGroup(gr.getName()) == null) + ContactGroup cgr = findContactGroup(gr.getName()); + if(cgr == null) { // such group does not exist. so it must be // renamed one @@ -1380,10 +1382,53 @@ public void entriesUpdated(Collection addresses) } else { - // strange ??? + // the group was renamed on different location + // so we do not have it at our side + // now lets find the group for the contact + // and rename it, + // - if it is the only contact in + // the group this is rename, otherwise it is move + ContactGroup currentParentGroup = + contact.getParentContactGroup(); + + if(currentParentGroup.countContacts() > 1) + { + cgr = currentParentGroup; + } + else + { + // make sure this group name is not present + // in entry groups + boolean present = false; + for (RosterGroup entryGr : entry.getGroups()) + { + if(entryGr.getName().equals( + currentParentGroup.getGroupName())) + { + present = true; + break; + } + } + + if(!present + && currentParentGroup instanceof + ContactGroupJabberImpl) + { + ContactGroupJabberImpl currentGroup = + (ContactGroupJabberImpl) + currentParentGroup; + currentGroup.setSourceGroup(gr); + + fireGroupEvent( + currentGroup, + ServerStoredGroupEvent + .GROUP_RENAMED_EVENT); + } + } } } - else + + if(cgr != null) { // the group is found the contact may be moved from // one group to another @@ -1405,16 +1450,45 @@ else if(contactGroup instanceof RootContactGroupJabberImpl) ContactGroupJabberImpl newParentGroup = findContactGroup(gr.getName()); + // the new parent group maybe missing + if(newParentGroup == null) + { + // create the group as it doesn't exist + newParentGroup = + new ContactGroupJabberImpl( + gr, + new ArrayList().iterator(), + ServerStoredContactListJabberImpl.this, + true); + + rootGroup.addSubGroup(newParentGroup); + + //tell listeners about the added group + fireGroupEvent(newParentGroup, + ServerStoredGroupEvent.GROUP_CREATED_EVENT); + } + newParentGroup.addContact(contact); fireContactMoved(contactGroup, - newParentGroup, - contact); + newParentGroup, + contact); + + if(contactGroup instanceof ContactGroupJabberImpl + && contactGroup.countContacts() == 0) + { + // in xmpp if group is empty it is removed + rootGroup.removeSubGroup( + (ContactGroupJabberImpl)contactGroup); + + fireGroupEvent( + (ContactGroupJabberImpl)contactGroup, + ServerStoredGroupEvent.GROUP_REMOVED_EVENT); + } } else { // check for change in display name - checkForRename(entry.getName(), contact); } } diff --git a/test/net/java/sip/communicator/slick/contactlist/TestMetaContactList.java b/test/net/java/sip/communicator/slick/contactlist/TestMetaContactList.java index 9c65e9fcb..3c0d56869 100644 --- a/test/net/java/sip/communicator/slick/contactlist/TestMetaContactList.java +++ b/test/net/java/sip/communicator/slick/contactlist/TestMetaContactList.java @@ -606,7 +606,7 @@ public void testGroupChangeEventHandling() throws Exception evt = (MetaContactGroupEvent)mclEvtCollector .collectedMetaContactGroupEvents.get( mclEvtCollector - .collectedMetaContactGroupEvents.size() - 1); + .collectedMetaContactGroupEvents.size() - 2); mclEvtCollector.collectedMetaContactGroupEvents.clear(); assertEquals("ID of the generated event",