use nick from bookmark if available

This commit is contained in:
iNPUTmice 2014-07-15 17:11:43 +02:00
parent 2ebd92b7a7
commit 2a82f23f36
6 changed files with 78 additions and 65 deletions

View File

@ -7,6 +7,7 @@ import eu.siacs.conversations.crypto.PgpEngine;
import eu.siacs.conversations.xml.Element; import eu.siacs.conversations.xml.Element;
import eu.siacs.conversations.xmpp.stanzas.PresencePacket; import eu.siacs.conversations.xmpp.stanzas.PresencePacket;
import android.annotation.SuppressLint; import android.annotation.SuppressLint;
import android.util.Log;
@SuppressLint("DefaultLocale") @SuppressLint("DefaultLocale")
public class MucOptions { public class MucOptions {
@ -87,6 +88,7 @@ public class MucOptions {
private boolean aboutToRename = false; private boolean aboutToRename = false;
private User self = new User(); private User self = new User();
private String subject = null; private String subject = null;
private String nick;
public MucOptions(Account account) { public MucOptions(Account account) {
this.account = account; this.account = account;
@ -123,7 +125,7 @@ public class MucOptions {
user.setAffiliation(item.getAttribute("affiliation")); user.setAffiliation(item.getAttribute("affiliation"));
user.setRole(item.getAttribute("role")); user.setRole(item.getAttribute("role"));
user.setName(name); user.setName(name);
if (name.equals(getNick())) { if (name.equals(getJoinNick())) {
this.isOnline = true; this.isOnline = true;
this.error = 0; this.error = 0;
self = user; self = user;
@ -145,7 +147,7 @@ public class MucOptions {
} }
} }
} else if (type.equals("unavailable")) { } else if (type.equals("unavailable")) {
if (name.equals(getNick())) { if (name.equals(getJoinNick())) {
Element item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item"); Element item = packet.findChild("x","http://jabber.org/protocol/muc#user").findChild("item");
String nick = item.getAttribute("nick"); String nick = item.getAttribute("nick");
if (nick!=null) { if (nick!=null) {
@ -153,7 +155,7 @@ public class MucOptions {
if (renameListener!=null) { if (renameListener!=null) {
renameListener.onRename(true); renameListener.onRename(true);
} }
this.setNick(nick); this.setJoinNick(nick);
} }
} }
deleteUser(packet.getAttribute("from").split("/")[1]); deleteUser(packet.getAttribute("from").split("/")[1]);
@ -177,22 +179,25 @@ public class MucOptions {
return this.users; return this.users;
} }
public String getNick() { public String getProposedNick() {
String[] split = conversation.getContactJid().split("/"); String[] mucParts = conversation.getContactJid().split("/");
if (split.length == 2) { if (conversation.getBookmark() != null && conversation.getBookmark().getNick() != null) {
return split[1]; return conversation.getBookmark().getNick();
} else { } else {
if (conversation.getAccount()!=null) { if (mucParts.length == 2) {
return conversation.getAccount().getUsername(); return mucParts[1];
} else { } else {
return null; return account.getUsername();
} }
} }
} }
public void setNick(String nick) { public String getJoinNick() {
String jid = conversation.getContactJid().split("/")[0]+"/"+nick; return this.nick;
conversation.setContactJid(jid); }
public void setJoinNick(String nick) {
this.nick = nick;
} }
public void setConversation(Conversation conversation) { public void setConversation(Conversation conversation) {
@ -268,4 +273,8 @@ public class MucOptions {
} }
return true; return true;
} }
public String getJoinJid() {
return this.conversation.getContactJid().split("/")[0]+"/"+this.getJoinNick();
}
} }

View File

@ -121,7 +121,7 @@ public class MessageParser extends AbstractParser implements
return null; return null;
} }
String counterPart = fromParts[1]; String counterPart = fromParts[1];
if (counterPart.equals(conversation.getMucOptions().getNick())) { if (counterPart.equals(conversation.getMucOptions().getJoinNick())) {
if (mXmppConnectionService.markMessage(conversation, if (mXmppConnectionService.markMessage(conversation,
packet.getId(), Message.STATUS_SEND)) { packet.getId(), Message.STATUS_SEND)) {
return null; return null;

View File

@ -691,6 +691,7 @@ public class XmppConnectionService extends Service {
if (bookmark.autojoin()) { if (bookmark.autojoin()) {
conversation = findOrCreateConversation(account, bookmark.getJid(), true); conversation = findOrCreateConversation(account, bookmark.getJid(), true);
conversation.setBookmark(bookmark); conversation.setBookmark(bookmark);
joinMuc(conversation);
} }
} }
} }
@ -840,10 +841,6 @@ public class XmppConnectionService extends Service {
this.databaseBackend.createConversation(conversation); this.databaseBackend.createConversation(conversation);
} }
this.conversations.add(conversation); this.conversations.add(conversation);
if ((account.getStatus() == Account.STATUS_ONLINE)
&& (conversation.getMode() == Conversation.MODE_MULTI)) {
joinMuc(conversation);
}
updateConversationUi(); updateConversationUi();
return conversation; return conversation;
} }
@ -933,19 +930,12 @@ public class XmppConnectionService extends Service {
} }
public void joinMuc(Conversation conversation) { public void joinMuc(Conversation conversation) {
Log.d(LOGTAG,"joining conversation "+conversation.getContactJid());
Account account = conversation.getAccount(); Account account = conversation.getAccount();
String[] mucParts = conversation.getContactJid().split("/"); String nick = conversation.getMucOptions().getProposedNick();
String muc; conversation.getMucOptions().setJoinNick(nick);
String nick;
if (mucParts.length == 2) {
muc = mucParts[0];
nick = mucParts[1];
} else {
muc = mucParts[0];
nick = account.getUsername();
}
PresencePacket packet = new PresencePacket(); PresencePacket packet = new PresencePacket();
packet.setAttribute("to", muc + "/" + nick); packet.setAttribute("to",conversation.getMucOptions().getJoinJid());
Element x = new Element("x"); Element x = new Element("x");
x.setAttribute("xmlns", "http://jabber.org/protocol/muc"); x.setAttribute("xmlns", "http://jabber.org/protocol/muc");
String sig = account.getPgpSignature(); String sig = account.getPgpSignature();
@ -963,6 +953,7 @@ public class XmppConnectionService extends Service {
mDateFormat.format(date)); mDateFormat.format(date));
} }
packet.addChild(x); packet.addChild(x);
Log.d(LOGTAG,packet.toString());
sendPresencePacket(account, packet); sendPresencePacket(account, packet);
} }
@ -984,10 +975,13 @@ public class XmppConnectionService extends Service {
renameListener.onRename(success); renameListener.onRename(success);
} }
if (success) { if (success) {
String jid = conversation.getContactJid().split("/")[0] conversation.setContactJid(conversation.getMucOptions().getJoinNick());
+ "/" + nick;
conversation.setContactJid(jid);
databaseBackend.updateConversation(conversation); databaseBackend.updateConversation(conversation);
Bookmark bookmark = conversation.getBookmark();
if (bookmark!=null) {
bookmark.setNick(nick);
pushBookmarks(bookmark.getAccount());
}
} }
} }
}); });
@ -1009,6 +1003,11 @@ public class XmppConnectionService extends Service {
conversation.setContactJid(jid); conversation.setContactJid(jid);
databaseBackend.updateConversation(conversation); databaseBackend.updateConversation(conversation);
if (conversation.getAccount().getStatus() == Account.STATUS_ONLINE) { if (conversation.getAccount().getStatus() == Account.STATUS_ONLINE) {
Bookmark bookmark = conversation.getBookmark();
if (bookmark!=null) {
bookmark.setNick(nick);
pushBookmarks(bookmark.getAccount());
}
joinMuc(conversation); joinMuc(conversation);
} }
} }
@ -1016,8 +1015,7 @@ public class XmppConnectionService extends Service {
public void leaveMuc(Conversation conversation) { public void leaveMuc(Conversation conversation) {
PresencePacket packet = new PresencePacket(); PresencePacket packet = new PresencePacket();
packet.setAttribute("to", conversation.getContactJid().split("/")[0] packet.setAttribute("to", conversation.getMucOptions().getJoinJid());
+ "/" + conversation.getMucOptions().getNick());
packet.setAttribute("from", conversation.getAccount().getFullJid()); packet.setAttribute("from", conversation.getAccount().getFullJid());
packet.setAttribute("type", "unavailable"); packet.setAttribute("type", "unavailable");
sendPresencePacket(conversation.getAccount(),packet); sendPresencePacket(conversation.getAccount(),packet);

View File

@ -48,7 +48,7 @@ public class MucDetailsActivity extends XmppActivity {
public void onClick(View arg0) { public void onClick(View arg0) {
MucOptions options = conversation.getMucOptions(); MucOptions options = conversation.getMucOptions();
String nick = mYourNick.getText().toString(); String nick = mYourNick.getText().toString();
if (!options.getNick().equals(nick)) { if (!options.getJoinNick().equals(nick)) {
xmppConnectionService.renameInMuc(conversation, nick); xmppConnectionService.renameInMuc(conversation, nick);
finish(); finish();
} }
@ -149,7 +149,7 @@ public class MucDetailsActivity extends XmppActivity {
mSubject.setText(conversation.getMucOptions().getSubject()); mSubject.setText(conversation.getMucOptions().getSubject());
setTitle(conversation.getName(useSubject)); setTitle(conversation.getName(useSubject));
mFullJid.setText(conversation.getContactJid().split("/")[0]); mFullJid.setText(conversation.getContactJid().split("/")[0]);
mYourNick.setText(conversation.getMucOptions().getNick()); mYourNick.setText(conversation.getMucOptions().getJoinNick());
mRoleAffiliaton = (TextView) findViewById(R.id.muc_role); mRoleAffiliaton = (TextView) findViewById(R.id.muc_role);
if (conversation.getMucOptions().online()) { if (conversation.getMucOptions().online()) {
mMoreDetails.setVisibility(View.VISIBLE); mMoreDetails.setVisibility(View.VISIBLE);

View File

@ -43,7 +43,6 @@ import eu.siacs.conversations.entities.Contact;
import eu.siacs.conversations.entities.Conversation; import eu.siacs.conversations.entities.Conversation;
import eu.siacs.conversations.entities.ListItem; import eu.siacs.conversations.entities.ListItem;
import eu.siacs.conversations.utils.KnownHostsAdapter; import eu.siacs.conversations.utils.KnownHostsAdapter;
import eu.siacs.conversations.utils.UIHelper;
import eu.siacs.conversations.utils.Validator; import eu.siacs.conversations.utils.Validator;
public class StartConversation extends XmppActivity { public class StartConversation extends XmppActivity {
@ -65,7 +64,7 @@ public class StartConversation extends XmppActivity {
private List<String> mKnownConferenceHosts; private List<String> mKnownConferenceHosts;
private EditText mSearchEditText; private EditText mSearchEditText;
public int conference_context_id; public int conference_context_id;
public int contact_context_id; public int contact_context_id;
@ -178,14 +177,15 @@ public class StartConversation extends XmppActivity {
mConferenceAdapter = new ListItemAdapter(conferences); mConferenceAdapter = new ListItemAdapter(conferences);
mConferenceListFragment.setListAdapter(mConferenceAdapter); mConferenceListFragment.setListAdapter(mConferenceAdapter);
mConferenceListFragment.setContextMenu(R.menu.conference_context); mConferenceListFragment.setContextMenu(R.menu.conference_context);
mConferenceListFragment.setOnListItemClickListener(new OnItemClickListener() { mConferenceListFragment
.setOnListItemClickListener(new OnItemClickListener() {
@Override @Override
public void onItemClick(AdapterView<?> arg0, View arg1, public void onItemClick(AdapterView<?> arg0, View arg1,
int position, long arg3) { int position, long arg3) {
openConversationForBookmark(position); openConversationForBookmark(position);
} }
}); });
mContactsAdapter = new ListItemAdapter(contacts); mContactsAdapter = new ListItemAdapter(contacts);
mContactsListFragment.setListAdapter(mContactsAdapter); mContactsListFragment.setListAdapter(mContactsAdapter);
@ -209,20 +209,23 @@ public class StartConversation extends XmppActivity {
contact.getJid(), false); contact.getJid(), false);
switchToConversation(conversation); switchToConversation(conversation);
} }
protected void openConversationForContact() { protected void openConversationForContact() {
int position = contact_context_id; int position = contact_context_id;
openConversationForContact(position); openConversationForContact(position);
} }
protected void openConversationForBookmark() { protected void openConversationForBookmark() {
openConversationForBookmark(conference_context_id); openConversationForBookmark(conference_context_id);
} }
protected void openConversationForBookmark(int position) { protected void openConversationForBookmark(int position) {
Bookmark bookmark = (Bookmark) conferences.get(position); Bookmark bookmark = (Bookmark) conferences.get(position);
Conversation conversation = xmppConnectionService.findOrCreateConversation(bookmark.getAccount(), bookmark.getJid(), true); Conversation conversation = xmppConnectionService
.findOrCreateConversation(bookmark.getAccount(),
bookmark.getJid(), true);
conversation.setBookmark(bookmark); conversation.setBookmark(bookmark);
xmppConnectionService.joinMuc(conversation);
if (!bookmark.autojoin()) { if (!bookmark.autojoin()) {
bookmark.setAutojoin(true); bookmark.setAutojoin(true);
xmppConnectionService.pushBookmarks(bookmark.getAccount()); xmppConnectionService.pushBookmarks(bookmark.getAccount());
@ -242,7 +245,7 @@ public class StartConversation extends XmppActivity {
xmppConnectionService.deleteContactOnServer(contact); xmppConnectionService.deleteContactOnServer(contact);
filter(mSearchEditText.getText().toString()); filter(mSearchEditText.getText().toString());
} }
protected void deleteConference() { protected void deleteConference() {
int position = contact_context_id; int position = contact_context_id;
Bookmark bookmark = (Bookmark) conferences.get(position); Bookmark bookmark = (Bookmark) conferences.get(position);
@ -307,7 +310,8 @@ public class StartConversation extends XmppActivity {
jid.setAdapter(new KnownHostsAdapter(this, jid.setAdapter(new KnownHostsAdapter(this,
android.R.layout.simple_list_item_1, mKnownConferenceHosts)); android.R.layout.simple_list_item_1, mKnownConferenceHosts));
populateAccountSpinner(spinner); populateAccountSpinner(spinner);
final CheckBox bookmarkCheckBox = (CheckBox) dialogView.findViewById(R.id.bookmark); final CheckBox bookmarkCheckBox = (CheckBox) dialogView
.findViewById(R.id.bookmark);
builder.setView(dialogView); builder.setView(dialogView);
builder.setNegativeButton(R.string.cancel, null); builder.setNegativeButton(R.string.cancel, null);
builder.setPositiveButton(R.string.join, null); builder.setPositiveButton(R.string.join, null);
@ -328,20 +332,23 @@ public class StartConversation extends XmppActivity {
if (account.hasBookmarkFor(conferenceJid)) { if (account.hasBookmarkFor(conferenceJid)) {
jid.setError(getString(R.string.bookmark_already_exists)); jid.setError(getString(R.string.bookmark_already_exists));
} else { } else {
Bookmark bookmark = new Bookmark(account, conferenceJid); Bookmark bookmark = new Bookmark(account,
conferenceJid);
bookmark.setAutojoin(true); bookmark.setAutojoin(true);
account.getBookmarks().add(bookmark); account.getBookmarks().add(bookmark);
xmppConnectionService.pushBookmarks(account); xmppConnectionService
.pushBookmarks(account);
Conversation conversation = xmppConnectionService Conversation conversation = xmppConnectionService
.findOrCreateConversation(account, .findOrCreateConversation(account,
conferenceJid, true); conferenceJid, true);
conversation.setBookmark(bookmark); conversation.setBookmark(bookmark);
switchToConversation(conversation); xmppConnectionService.joinMuc(conversation);
switchToConversation(conversation);
} }
} else { } else {
Conversation conversation = xmppConnectionService Conversation conversation = xmppConnectionService
.findOrCreateConversation(account, .findOrCreateConversation(account,
conferenceJid, true); conferenceJid, true);
switchToConversation(conversation); switchToConversation(conversation);
} }
} else { } else {
@ -416,7 +423,7 @@ public class StartConversation extends XmppActivity {
this.mKnownConferenceHosts = xmppConnectionService this.mKnownConferenceHosts = xmppConnectionService
.getKnownConferenceHosts(); .getKnownConferenceHosts();
} }
protected void filter(String needle) { protected void filter(String needle) {
this.filterContacts(needle); this.filterContacts(needle);
this.filterConferences(needle); this.filterConferences(needle);
@ -436,12 +443,12 @@ public class StartConversation extends XmppActivity {
Collections.sort(this.contacts); Collections.sort(this.contacts);
mContactsAdapter.notifyDataSetChanged(); mContactsAdapter.notifyDataSetChanged();
} }
protected void filterConferences(String needle) { protected void filterConferences(String needle) {
this.conferences.clear(); this.conferences.clear();
for (Account account : xmppConnectionService.getAccounts()) { for (Account account : xmppConnectionService.getAccounts()) {
if (account.getStatus() != Account.STATUS_DISABLED) { if (account.getStatus() != Account.STATUS_DISABLED) {
for(Bookmark bookmark : account.getBookmarks()) { for (Bookmark bookmark : account.getBookmarks()) {
if (bookmark.match(needle)) { if (bookmark.match(needle)) {
this.conferences.add(bookmark); this.conferences.add(bookmark);
} }
@ -486,7 +493,7 @@ public class StartConversation extends XmppActivity {
public static class MyListFragment extends ListFragment { public static class MyListFragment extends ListFragment {
private AdapterView.OnItemClickListener mOnItemClickListener; private AdapterView.OnItemClickListener mOnItemClickListener;
private int mResContextMenu; private int mResContextMenu;
public void setContextMenu(int res) { public void setContextMenu(int res) {
this.mResContextMenu = res; this.mResContextMenu = res;
} }
@ -513,8 +520,7 @@ public class StartConversation extends XmppActivity {
ContextMenuInfo menuInfo) { ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo); super.onCreateContextMenu(menu, v, menuInfo);
StartConversation activity = (StartConversation) getActivity(); StartConversation activity = (StartConversation) getActivity();
activity.getMenuInflater().inflate(mResContextMenu, activity.getMenuInflater().inflate(mResContextMenu, menu);
menu);
AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo; AdapterView.AdapterContextMenuInfo acmi = (AdapterContextMenuInfo) menuInfo;
if (mResContextMenu == R.menu.conference_context) { if (mResContextMenu == R.menu.conference_context) {
activity.conference_context_id = acmi.position; activity.conference_context_id = acmi.position;

View File

@ -216,7 +216,7 @@ public class UIHelper {
bgColor, fgColor); bgColor, fgColor);
} }
String[] names = new String[members.size() + 1]; String[] names = new String[members.size() + 1];
names[0] = conversation.getMucOptions().getNick(); names[0] = conversation.getMucOptions().getJoinNick();
for (int i = 0; i < members.size(); ++i) { for (int i = 0; i < members.size(); ++i) {
names[i + 1] = members.get(i).getName(); names[i + 1] = members.get(i).getName();
} }
@ -343,7 +343,7 @@ public class UIHelper {
if ((currentCon != null) if ((currentCon != null)
&& (currentCon.getMode() == Conversation.MODE_MULTI) && (currentCon.getMode() == Conversation.MODE_MULTI)
&& (!alwaysNotify)) { && (!alwaysNotify)) {
String nick = currentCon.getMucOptions().getNick(); String nick = currentCon.getMucOptions().getJoinNick();
Pattern highlight = generateNickHighlightPattern(nick); Pattern highlight = generateNickHighlightPattern(nick);
Matcher m = highlight.matcher(currentCon.getLatestMessage() Matcher m = highlight.matcher(currentCon.getLatestMessage()
.getBody()); .getBody());
@ -463,7 +463,7 @@ public class UIHelper {
private static boolean wasHighlighted(Conversation conversation) { private static boolean wasHighlighted(Conversation conversation) {
List<Message> messages = conversation.getMessages(); List<Message> messages = conversation.getMessages();
String nick = conversation.getMucOptions().getNick(); String nick = conversation.getMucOptions().getJoinNick();
Pattern highlight = generateNickHighlightPattern(nick); Pattern highlight = generateNickHighlightPattern(nick);
for (int i = messages.size() - 1; i >= 0; --i) { for (int i = messages.size() - 1; i >= 0; --i) {
if (messages.get(i).isRead()) { if (messages.get(i).isRead()) {