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
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
|
package de.pixart.messenger.ui.util;
import android.app.Activity;
import android.preference.PreferenceManager;
import androidx.appcompat.app.AlertDialog;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.TypefaceSpan;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import de.pixart.messenger.Config;
import de.pixart.messenger.R;
import de.pixart.messenger.entities.Contact;
import de.pixart.messenger.entities.Conversation;
import de.pixart.messenger.entities.MucOptions;
import de.pixart.messenger.entities.MucOptions.User;
import de.pixart.messenger.services.XmppConnectionService;
import de.pixart.messenger.ui.ConferenceDetailsActivity;
import de.pixart.messenger.ui.ConversationFragment;
import de.pixart.messenger.ui.ConversationsActivity;
import de.pixart.messenger.ui.MucUsersActivity;
import de.pixart.messenger.ui.XmppActivity;
import rocks.xmpp.addr.Jid;
public final class MucDetailsContextMenuHelper {
public static void onCreateContextMenu(ContextMenu menu, View v) {
final XmppActivity activity = XmppActivity.find(v);
final Object tag = v.getTag();
if (tag instanceof MucOptions.User && activity != null) {
activity.getMenuInflater().inflate(R.menu.muc_details_context, menu);
final MucOptions.User user = (MucOptions.User) tag;
String name;
final Contact contact = user.getContact();
if (contact != null && contact.showInContactList()) {
name = contact.getDisplayName();
} else if (user.getRealJid() != null) {
name = user.getRealJid().asBareJid().toString();
} else {
name = user.getName();
}
menu.setHeaderTitle(name);
MucDetailsContextMenuHelper.configureMucDetailsContextMenu(activity, menu, user.getConversation(), user);
}
}
public static void configureMucDetailsContextMenu(Activity activity, Menu menu, Conversation conversation, User user) {
final boolean advancedMode = PreferenceManager.getDefaultSharedPreferences(activity).getBoolean("advanced_muc_mode", false);
final MucOptions mucOptions = conversation.getMucOptions();
final boolean isGroupChat = mucOptions.isPrivateAndNonAnonymous();
MenuItem sendPrivateMessage = menu.findItem(R.id.send_private_message);
if (user != null && user.getRealJid() != null) {
MenuItem showContactDetails = menu.findItem(R.id.action_contact_details);
MenuItem startConversation = menu.findItem(R.id.start_conversation);
MenuItem giveMembership = menu.findItem(R.id.give_membership);
MenuItem removeMembership = menu.findItem(R.id.remove_membership);
MenuItem giveAdminPrivileges = menu.findItem(R.id.give_admin_privileges);
MenuItem removeAdminPrivileges = menu.findItem(R.id.remove_admin_privileges);
MenuItem giveOwnerPrivileges = menu.findItem(R.id.give_owner_privileges);
MenuItem removeOwnerPrivileges = menu.findItem(R.id.revoke_owner_privileges);
MenuItem managePermissions = menu.findItem(R.id.manage_permissions);
MenuItem removeFromRoom = menu.findItem(R.id.kick_from_room);
removeFromRoom.setTitle(isGroupChat ? R.string.kick_from_room : R.string.remove_from_channel);
MenuItem banFromConference = menu.findItem(R.id.ban_from_room);
banFromConference.setTitle(isGroupChat ? R.string.ban_from_conference : R.string.ban_from_channel);
MenuItem invite = menu.findItem(R.id.invite);
MenuItem highlightInMuc = menu.findItem(R.id.highlight_in_muc);
startConversation.setVisible(true);
final Contact contact = user.getContact();
final User self = conversation.getMucOptions().getSelf();
if (contact != null && contact.showInRoster()) {
showContactDetails.setVisible(!contact.isSelf());
}
if ((activity instanceof ConferenceDetailsActivity || activity instanceof MucUsersActivity) && user.getRole() == MucOptions.Role.NONE) {
invite.setVisible(true);
}
if (activity instanceof ConversationsActivity) {
highlightInMuc.setVisible(false);
} else if (activity instanceof ConferenceDetailsActivity) {
highlightInMuc.setVisible(true);
}
boolean managePermissionsVisible = false;
if ((self.getAffiliation().ranks(MucOptions.Affiliation.ADMIN) && self.getAffiliation().outranks(user.getAffiliation())) || self.getAffiliation() == MucOptions.Affiliation.OWNER) {
if (advancedMode) {
if (!user.getAffiliation().ranks(MucOptions.Affiliation.MEMBER)) {
managePermissionsVisible = true;
giveMembership.setVisible(true);
} else if (user.getAffiliation() == MucOptions.Affiliation.MEMBER) {
managePermissionsVisible = true;
removeMembership.setVisible(true);
}
if (!Config.DISABLE_BAN) {
managePermissionsVisible = true;
banFromConference.setVisible(true);
}
}
if (!Config.DISABLE_BAN) {
removeFromRoom.setVisible(true);
}
}
if (self.getAffiliation().ranks(MucOptions.Affiliation.OWNER)) {
if (isGroupChat || advancedMode || user.getAffiliation() == MucOptions.Affiliation.OWNER) {
if (!user.getAffiliation().ranks(MucOptions.Affiliation.OWNER)) {
managePermissionsVisible = true;
giveOwnerPrivileges.setVisible(true);
} else if (user.getAffiliation() == MucOptions.Affiliation.OWNER) {
managePermissionsVisible = true;
removeOwnerPrivileges.setVisible(true);
}
}
if (!isGroupChat || advancedMode || user.getAffiliation() == MucOptions.Affiliation.ADMIN) {
if (!user.getAffiliation().ranks(MucOptions.Affiliation.ADMIN)) {
managePermissionsVisible = true;
giveAdminPrivileges.setVisible(true);
} else if (user.getAffiliation() == MucOptions.Affiliation.ADMIN) {
managePermissionsVisible = true;
removeAdminPrivileges.setVisible(true);
}
}
}
managePermissions.setVisible(managePermissionsVisible);
sendPrivateMessage.setVisible(true);
sendPrivateMessage.setEnabled(mucOptions.allowPm());
} else {
sendPrivateMessage.setVisible(true);
sendPrivateMessage.setEnabled(user != null && mucOptions.allowPm() && user.getRole().ranks(MucOptions.Role.VISITOR));
}
}
public static boolean onContextItemSelected(MenuItem item, User user, XmppActivity activity) {
return onContextItemSelected(item, user, activity, null);
}
public static boolean onContextItemSelected(MenuItem item, User user, XmppActivity activity, final String fingerprint) {
final Conversation conversation = user.getConversation();
final XmppConnectionService.OnAffiliationChanged onAffiliationChanged = activity instanceof XmppConnectionService.OnAffiliationChanged ? (XmppConnectionService.OnAffiliationChanged) activity : null;
Jid jid = user.getRealJid();
switch (item.getItemId()) {
case R.id.action_contact_details:
Contact contact = user.getContact();
if (contact != null) {
activity.switchToContactDetails(contact, fingerprint);
}
return true;
case R.id.start_conversation:
startConversation(user, activity);
return true;
case R.id.give_admin_privileges:
activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.ADMIN, onAffiliationChanged);
return true;
case R.id.give_membership:
case R.id.remove_admin_privileges:
case R.id.revoke_owner_privileges:
activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.MEMBER, onAffiliationChanged);
return true;
case R.id.give_owner_privileges:
activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.OWNER, onAffiliationChanged);
return true;
case R.id.remove_membership:
activity.xmppConnectionService.changeAffiliationInConference(conversation, jid, MucOptions.Affiliation.NONE, onAffiliationChanged);
return true;
case R.id.kick_from_room:
kickFromRoom(user, activity, onAffiliationChanged);
return true;
case R.id.ban_from_room:
banFromRoom(user, activity, onAffiliationChanged);
return true;
case R.id.send_private_message:
if (activity instanceof ConversationsActivity) {
ConversationFragment conversationFragment = ConversationFragment.get(activity);
if (conversationFragment != null) {
conversationFragment.privateMessageWith(user.getFullJid());
return true;
}
}
activity.privateMsgInMuc(conversation, user.getName());
return true;
case R.id.invite:
if (user.getAffiliation().ranks(MucOptions.Affiliation.MEMBER)) {
activity.xmppConnectionService.directInvite(conversation, jid.asBareJid());
} else {
activity.xmppConnectionService.invite(conversation, jid);
}
return true;
case R.id.highlight_in_muc:
activity.highlightInMuc(conversation, user.getName());
return true;
default:
return false;
}
}
private static void kickFromRoom(final User user, XmppActivity activity, XmppConnectionService.OnAffiliationChanged onAffiliationChanged) {
final Conversation conversation = user.getConversation();
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(R.string.kick_from_conference);
String jid = user.getRealJid().asBareJid().toString();
SpannableString message;
if (conversation.getMucOptions().membersOnly()) {
message = new SpannableString(activity.getString(R.string.kicking_from_conference, jid));
} else {
message = new SpannableString(activity.getString(R.string.kicking_from_public_conference, jid));
}
int start = message.toString().indexOf(jid);
if (start >= 0) {
message.setSpan(new TypefaceSpan("monospace"), start, start + jid.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
builder.setMessage(message);
builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.kick_now, (dialog, which) -> {
activity.xmppConnectionService.changeAffiliationInConference(conversation, user.getRealJid(), MucOptions.Affiliation.NONE, onAffiliationChanged);
if (user.getRole() != MucOptions.Role.NONE) {
activity.xmppConnectionService.changeRoleInConference(conversation, user.getName(), MucOptions.Role.NONE);
}
});
builder.create().show();
}
private static void banFromRoom(final User user, XmppActivity activity, XmppConnectionService.OnAffiliationChanged onAffiliationChanged) {
final Conversation conversation = user.getConversation();
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setTitle(R.string.ban_from_conference);
String jid = user.getRealJid().asBareJid().toString();
SpannableString message;
if (conversation.getMucOptions().membersOnly()) {
message = new SpannableString(activity.getString(R.string.ban_from_conference_message, jid));
} else {
message = new SpannableString(activity.getString(R.string.ban_from_public_conference_message, jid));
}
int start = message.toString().indexOf(jid);
if (start >= 0) {
message.setSpan(new TypefaceSpan("monospace"), start, start + jid.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
}
builder.setMessage(message);
builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.ban_now, (dialog, which) -> {
activity.xmppConnectionService.changeAffiliationInConference(conversation, user.getRealJid(), MucOptions.Affiliation.OUTCAST, onAffiliationChanged);
if (user.getRole() != MucOptions.Role.NONE) {
activity.xmppConnectionService.changeRoleInConference(conversation, user.getName(), MucOptions.Role.NONE);
}
});
builder.create().show();
}
private static void startConversation(User user, XmppActivity activity) {
if (user.getRealJid() != null) {
Conversation newConversation = activity.xmppConnectionService.findOrCreateConversation(user.getAccount(), user.getRealJid().asBareJid(), false, true);
activity.switchToConversation(newConversation);
}
}
}
|