MucDetailsContextMenuHelper.java

  1package eu.siacs.conversations.ui.util;
  2
  3import android.support.v7.app.AlertDialog;
  4import android.view.Menu;
  5import android.view.MenuItem;
  6import android.widget.Toast;
  7
  8import eu.siacs.conversations.Config;
  9import eu.siacs.conversations.entities.Contact;
 10import eu.siacs.conversations.entities.Conversation;
 11import eu.siacs.conversations.entities.MucOptions;
 12import eu.siacs.conversations.entities.MucOptions.User;
 13import eu.siacs.conversations.R;
 14import eu.siacs.conversations.services.XmppConnectionService;
 15import eu.siacs.conversations.ui.XmppActivity;
 16import rocks.xmpp.addr.Jid;
 17
 18
 19public final class MucDetailsContextMenuHelper {
 20    public static void configureMucDetailsContextMenu(Menu menu, Conversation conversation, User user, boolean advancedMode) {
 21        if (user != null) {
 22            if (user.getRealJid() != null) {
 23                MenuItem showContactDetails = menu.findItem(R.id.action_contact_details);
 24                MenuItem startConversation = menu.findItem(R.id.start_conversation);
 25                MenuItem giveMembership = menu.findItem(R.id.give_membership);
 26                MenuItem removeMembership = menu.findItem(R.id.remove_membership);
 27                MenuItem giveAdminPrivileges = menu.findItem(R.id.give_admin_privileges);
 28                MenuItem removeAdminPrivileges = menu.findItem(R.id.remove_admin_privileges);
 29                MenuItem removeFromRoom = menu.findItem(R.id.remove_from_room);
 30                MenuItem banFromConference = menu.findItem(R.id.ban_from_conference);
 31                MenuItem invite = menu.findItem(R.id.invite);
 32                startConversation.setVisible(true);
 33                final Contact contact = user.getContact();
 34                final User self = conversation.getMucOptions().getSelf();
 35                if (contact != null && contact.showInRoster()) {
 36                    showContactDetails.setVisible(!contact.isSelf());
 37                }
 38                if (user.getRole() == MucOptions.Role.NONE) {
 39                    invite.setVisible(true);
 40                }
 41                if (self.getAffiliation().ranks(MucOptions.Affiliation.ADMIN) &&
 42                        self.getAffiliation().outranks(user.getAffiliation())) {
 43                    if (advancedMode) {
 44                        if (user.getAffiliation() == MucOptions.Affiliation.NONE) {
 45                            giveMembership.setVisible(true);
 46                        } else {
 47                            removeMembership.setVisible(true);
 48                        }
 49                        if (!Config.DISABLE_BAN) {
 50                            banFromConference.setVisible(true);
 51                        }
 52                    } else {
 53                        if (!Config.DISABLE_BAN || conversation.getMucOptions().membersOnly()) {
 54                            removeFromRoom.setVisible(true);
 55                        }
 56                    }
 57                    if (user.getAffiliation() != MucOptions.Affiliation.ADMIN) {
 58                        giveAdminPrivileges.setVisible(true);
 59                    } else {
 60                        removeAdminPrivileges.setVisible(true);
 61                    }
 62                }
 63            } else {
 64                MenuItem sendPrivateMessage = menu.findItem(R.id.send_private_message);
 65                sendPrivateMessage.setVisible(true);
 66                sendPrivateMessage.setEnabled(user.getRole().ranks(MucOptions.Role.VISITOR));
 67            }
 68        } else {
 69            MenuItem sendPrivateMessage = menu.findItem(R.id.send_private_message);
 70            sendPrivateMessage.setVisible(true);
 71            sendPrivateMessage.setEnabled(false);
 72        }
 73    }
 74
 75    public static boolean onContextItemSelected(MenuItem item, User user, Conversation conversation, XmppActivity activity, XmppConnectionService.OnAffiliationChanged onAffiliationChanged, XmppConnectionService.OnRoleChanged onRoleChanged) {
 76        Jid jid = user.getRealJid();
 77        switch (item.getItemId()) {
 78            case R.id.action_contact_details:
 79                Contact contact = user.getContact();
 80                if (contact != null) {
 81                    activity.switchToContactDetails(contact);
 82                }
 83                return true;
 84            case R.id.start_conversation:
 85                startConversation(user, conversation, activity);
 86                return true;
 87            case R.id.give_admin_privileges:
 88                activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.ADMIN, onAffiliationChanged);
 89                return true;
 90            case R.id.give_membership:
 91                activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.MEMBER, onAffiliationChanged);
 92                return true;
 93            case R.id.remove_membership:
 94                activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.NONE, onAffiliationChanged);
 95                return true;
 96            case R.id.remove_admin_privileges:
 97                activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.MEMBER, onAffiliationChanged);
 98                return true;
 99            case R.id.remove_from_room:
100                removeFromRoom(user, conversation, activity, onAffiliationChanged, onRoleChanged);
101                return true;
102            case R.id.ban_from_conference:
103                activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.OUTCAST, onAffiliationChanged);
104                if (user.getRole() != MucOptions.Role.NONE) {
105                    activity.xmppConnectionService.changeRoleInConference(conversation, user.getName(), MucOptions.Role.NONE, onRoleChanged);
106                }
107                return true;
108            case R.id.send_private_message:
109                if (conversation.getMucOptions().allowPm()) {
110                    activity.privateMsgInMuc(conversation, user.getName());
111                } else {
112                    Toast.makeText(activity, R.string.private_messages_are_disabled, Toast.LENGTH_SHORT).show();
113                }
114                return true;
115            case R.id.invite:
116                activity.xmppConnectionService.directInvite(conversation, jid);
117                return true;
118            default:
119                return false;
120        }
121    }
122
123    public static void removeFromRoom(final User user, Conversation conversation, XmppActivity activity, XmppConnectionService.OnAffiliationChanged onAffiliationChanged, XmppConnectionService.OnRoleChanged onRoleChanged) {
124        if (conversation.getMucOptions().membersOnly()) {
125            activity.xmppConnectionService.changeAffiliationInConference(conversation, user.getRealJid(), MucOptions.Affiliation.NONE, onAffiliationChanged);
126            if (user.getRole() != MucOptions.Role.NONE) {
127                activity.xmppConnectionService.changeRoleInConference(conversation, user.getName(), MucOptions.Role.NONE, onRoleChanged);
128            }
129        } else {
130            AlertDialog.Builder builder = new AlertDialog.Builder(activity);
131            builder.setTitle(R.string.ban_from_conference);
132            builder.setMessage(activity.getString(R.string.removing_from_public_conference, user.getName()));
133            builder.setNegativeButton(R.string.cancel, null);
134            builder.setPositiveButton(R.string.ban_now, (dialog, which) -> {
135                activity.xmppConnectionService.changeAffiliationInConference(conversation, user.getRealJid(), MucOptions.Affiliation.OUTCAST, onAffiliationChanged);
136                if (user.getRole() != MucOptions.Role.NONE) {
137                    activity.xmppConnectionService.changeRoleInConference(conversation, user.getName(), MucOptions.Role.NONE, onRoleChanged);
138                }
139            });
140            builder.create().show();
141        }
142    }
143
144    public static void startConversation(User user, Conversation conversation, XmppActivity activity) {
145        if (user.getRealJid() != null) {
146            Conversation newConversation = activity.xmppConnectionService.findOrCreateConversation(conversation.getAccount(), user.getRealJid().asBareJid(), false, true);
147            activity.switchToConversation(newConversation);
148        }
149    }
150}