This is the example, I have to pals, Scooby and Shaggy. Scooby is in Shaggy’s roster and Shaggy is in Scooby’s roster too. When Shaggy asks to remove Scooby from his roster, I try with this code:
…
RosterEntry entry = roster.getEntry(userName);
connection.removeEntry(entry)
…
But Scooby lost Shaggy’s entry too, and Shaggy desapear from his roster.
Is it possible from smack to change the subscription type from “both” to “from”?
You can do this on Scooby’s side - when he receives the unsubscribed packet, you can ask the user if they want to unsubscribe too, or do an action by default (which usually is to unsubscribe aswell).
Thanks Op3racional. But what Im needing is what is the method that allow me to change the subscription type on a roster entry from smack. I got the concept, I need the method.
If you set your Roster.SubscriptionMode to manual, that means your application has to react to subscription Packets in
public void processPacket(Packet packet)
/** listens for Presence packets for manual subscriprion mode */
@Override
public void processPacket(Packet packet) {
// packetlistener
final Presence presence = (Presence) packet;
final String from = presence.getFrom();
if (presence.getType() == Presence.Type.subscribe) {
// check if the user is already subscribed
if (roster.isSubscribed(from)) {
Presence response = new Presence(Presence.Type.subscribed);
response.setTo(from);
con.sendPacket(response);
return;
}
// ask user to accept or deny subscription request //snip if (n == JOptionPane.YES_OPTION) {
// send subscription accept packet
Presence response = new Presence(Presence.Type.subscribed);
response.setTo(from);
con.sendPacket(response);
} else {
// send subscription deny packet
Presence response = new Presence(Presence.Type.unsubscribe);
response.setTo(from);
con.sendPacket(response);
}
} else if (presence.getType() == Presence.Type.unsubscribe) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final RosterEntry entry = roster.getEntry(from);
if (entry != null) {
try {
roster.removeEntry(entry);
}
catch (XMPPException ex) {
Presence unsub = new Presence(Presence.Type
.unsubscribed);
unsub.setTo(from);
con.sendPacket(unsub);
ex.printStackTrace();
}
}
}
});
} else if (presence.getType() == Presence.Type.subscribed) {
// update roster GUI
} else if (presence.getType() == Presence.Type.unsubscribed) {
SwingUtilities.invokeLater(new Runnable() {
@Override
public void run() {
final RosterEntry entry = roster.getEntry(from);
if (entry != null) {
try {
roster.removeEntry(entry);
}
catch (XMPPException ex) {
ex.printStackTrace();
}
}
}
});
}
}
Thanks a lot Op3racional. I almost resolved it. I think the problem is that I set “Roster.setDefaultSubscriptionMode(accept_all)”. When I did it, I was thinking about subscription and have forgotten unsubscription (it seems that hapen automatically too!). I’ll try, then I’ll tell you about it …