diff --git a/application/ChatProtocolMessages.h b/application/ChatProtocolMessages.h index 6fe2653..2c032d1 100644 --- a/application/ChatProtocolMessages.h +++ b/application/ChatProtocolMessages.h @@ -84,7 +84,8 @@ enum im_what_code { * Messages related changes in general users. */ - /*! User's nick has changed →App */ + /*! User's nick has changed →App + Requires: String "user_id", String "user_name" */ IM_USER_NICKNAME_SET = 40, /*! Received new status for user →App @@ -256,7 +257,7 @@ enum im_what_code { /*! User has explicitly joined →App Requires: String "chat_id", String "user_id" - Accepts: String "body" */ + Accepts: String "body", String "user_name" */ IM_ROOM_PARTICIPANT_JOINED = 160, /*! A user left the room →App diff --git a/application/Conversation.cpp b/application/Conversation.cpp index 5f82c82..cb96d3a 100644 --- a/application/Conversation.cpp +++ b/application/Conversation.cpp @@ -54,9 +54,8 @@ Conversation::~Conversation() { ((TheApp*)be_app)->GetMainWindow()->RemoveConversation(this); - ProtocolLooper* looper = GetProtocolLooper(); - if (looper != NULL) - looper->RemoveConversation(this); + if (fLooper != NULL) + fLooper->RemoveConversation(this); delete fChatView; delete fConversationItem; @@ -237,6 +236,27 @@ Conversation::ImMessage(BMessage* msg) GetView()->MessageReceived(msg); break; } + case IM_USER_NICKNAME_SET: + { + BString user_id = msg->FindString("user_id"); + BString user_name = msg->FindString("user_name"); + if (user_id.IsEmpty() == false && user_name.IsEmpty() == false) { + User* user = UserById(user_id); + + BString text(B_TRANSLATE("** %old% has changed their nick to %new%.")); + text.ReplaceAll("%new%", user_name); + if (user != NULL) + text.ReplaceAll("%old%", user->GetName()); + else + text.ReplaceAll("%old%", user_id); + + BMessage* notify = new BMessage(IM_MESSAGE); + notify->AddInt32("im_what", IM_MESSAGE_RECEIVED); + notify->AddString("body", text); + GetView()->MessageReceived(notify); + } + break; + } case IM_LOGS_RECEIVED: default: GetView()->MessageReceived(msg); @@ -611,9 +631,8 @@ Conversation::_EnsureUser(BMessage* msg) NotifyInteger(INT_ROOM_MEMBERS, fUsers.CountItems()); } - if (name.IsEmpty() == false) { + if (name.IsEmpty() == false && user->GetName() != name) user->SetNotifyName(name); - } user->RegisterObserver(this); return user; } diff --git a/application/Server.cpp b/application/Server.cpp index 2d146f0..e7d0574 100644 --- a/application/Server.cpp +++ b/application/Server.cpp @@ -288,15 +288,32 @@ Server::ImMessage(BMessage* msg) } case IM_OWN_NICKNAME_SET: { - BString nick = msg->FindString("user_name"); + BString nick; ProtocolLooper* looper = _LooperFromMessage(msg); - if (looper == NULL) + if (looper == NULL || msg->FindString("user_name", &nick) != B_OK) break; Contact* contact = looper->GetOwnContact(); if (contact != NULL) contact->SetNotifyName(nick.String()); break; } + case IM_USER_NICKNAME_SET: + { + BString nick; + if (msg->FindString("user_name", &nick) != B_OK) + return B_SKIP_MESSAGE; + + User* user = _EnsureUser(msg); + if (user == NULL) + break; + + ChatMap conv = user->Conversations(); + for (int i = 0; i < conv.CountItems(); i++) + conv.ValueAt(i)->ImMessage(msg); + + user->SetNotifyName(nick); + break; + } case IM_USER_STATUS_SET: { int32 status; @@ -984,7 +1001,8 @@ Server::_EnsureConversation(BMessage* message) if (item == NULL) { item = new Conversation(chat_id, Looper()); item->SetProtocolLooper(looper); - item->AddUser(looper->GetOwnContact()); + if (looper->GetOwnContact() != NULL) + item->AddUser(looper->GetOwnContact()); looper->AddConversation(item); BMessage meta(IM_MESSAGE);