int SessionList::AdjustMsg(const nim::SessionData &msg) { int count = session_list_->GetCount(); for (int i = 0; i < count; i++) { SessionItem* item = dynamic_cast<SessionItem*>(session_list_->GetItemAt(i)); if (item) { if (msg.msg_timetag_ == 0 || msg.msg_timetag_ > item->GetMsgTime()) return i; } } return -1; }
SessionItem* SessionList::AddSessionItem(const nim::SessionData &item_data) { SessionItem* item = dynamic_cast<SessionItem*>(session_list_->FindSubControl(nbase::UTF8ToUTF16(item_data.id_))); nim::SessionData item_data_new = item_data; if (item) { if (nim::kNIMSessionCommandMsgDeleted != item_data.command_ && item->GetMsgTime() > item_data.msg_timetag_) { item_data_new = item->GetSessionData(); item_data_new.unread_count_ = item_data.unread_count_; } } int index = AdjustMsg(item_data_new); if (item && (session_list_->GetItemIndex(item) == index - 1 || session_list_->GetItemIndex(item) == index)) { item->InitMsg(item_data_new); //应该插入自己紧靠前面或后面的位置,就不用删除,直接更新。 } else { if (item) session_list_->Remove(item); item = new SessionItem; GlobalManager::FillBoxWithCache(item, L"main/session_item.xml"); index = AdjustMsg(item_data_new); //删掉之后重新算一次 if (index >= 0) session_list_->AddAt(item, index); else session_list_->Add(item); item->InitCtrl(); item->InitMsg(item_data_new); item->AttachAllEvents(nbase::Bind(&SessionList::OnItemNotify, this, std::placeholders::_1)); } InvokeUnreadCountChange(); return item; }