/*
* Copyright (c) 2010-2020 Belledonne Communications SARL.
*
* This file is part of linphone-desktop
* (see https://www.linphone.org).
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see .
*/
#include "components/core/CoreManager.hpp"
#include "components/settings/AccountSettingsModel.hpp"
#include "components/sip-addresses/SipAddressesModel.hpp"
#include "components/chat-room/ChatRoomModel.hpp"
#include "utils/Utils.hpp"
#include "app/App.hpp"
#include "TimelineModel.hpp"
#include "TimelineListModel.hpp"
#include
#include
#include
// =============================================================================
std::shared_ptr TimelineModel::create(std::shared_ptr chatRoom, QObject *parent){
if(!CoreManager::getInstance()->getTimelineListModel() || !CoreManager::getInstance()->getTimelineListModel()->getTimeline(chatRoom, false)) {
std::shared_ptr model = std::make_shared(chatRoom, parent);
if(model && model->getChatRoomModel()){
model->mSelf = model;
chatRoom->addListener(model);
return model;
}
}
return nullptr;
}
TimelineModel::TimelineModel (std::shared_ptr chatRoom, QObject *parent) : QObject(parent) {
App::getInstance()->getEngine()->setObjectOwnership(this, QQmlEngine::CppOwnership);// Avoid QML to destroy it when passing by Q_INVOKABLE
mChatRoomModel = ChatRoomModel::create(chatRoom);
if( mChatRoomModel ){
CoreManager::getInstance()->handleChatRoomCreated(mChatRoomModel);
QObject::connect(this, &TimelineModel::selectedChanged, this, &TimelineModel::updateUnreadCount);
QObject::connect(CoreManager::getInstance()->getAccountSettingsModel(), &AccountSettingsModel::defaultProxyChanged, this, &TimelineModel::onDefaultProxyChanged);
}
mSelected = false;
}
TimelineModel::~TimelineModel(){
//mChatRoomModel->getChatRoom()->removeListener(mChatRoomModel);
}
QString TimelineModel::getFullPeerAddress() const{
return mChatRoomModel->getFullPeerAddress();
}
QString TimelineModel::getFullLocalAddress() const{
return mChatRoomModel->getLocalAddress();
}
QString TimelineModel::getUsername() const{
return mChatRoomModel->getUsername();
}
QString TimelineModel::getAvatar() const{
return "";
}
int TimelineModel::getPresenceStatus() const{
return 0;
}
ChatRoomModel *TimelineModel::getChatRoomModel() const{
return mChatRoomModel.get();
}
void TimelineModel::setSelected(const bool& selected){
if(selected != mSelected){
mSelected = selected;
if(mSelected){
qInfo() << "Chat room selected : Subject :" << mChatRoomModel->getSubject()
<< ", Username:" << mChatRoomModel->getUsername()
<< ", GroupEnabled:"<< mChatRoomModel->isGroupEnabled()
<< ", isConference:"<< mChatRoomModel->isConference()
<< ", isOneToOne:"<< mChatRoomModel->isOneToOne()
<< ", Encrypted:"<< mChatRoomModel->haveEncryption()
<< ", ephemeralEnabled:" << mChatRoomModel->isEphemeralEnabled()
<< ", isAdmin:"<< mChatRoomModel->isMeAdmin()
<< ", canHandleParticipants:"<< mChatRoomModel->canHandleParticipants()
<< ", hasBeenLeft:" << mChatRoomModel->hasBeenLeft();
}
emit selectedChanged(mSelected);
}
}
void TimelineModel::updateUnreadCount(){
if(!mSelected){// updateUnreadCount is called when selected has changed;: So if mSelected is false then we are going out of it.
mChatRoomModel->resetMessageCount();// The reset will appear when the chat room has "mark as read enabled", that means that we should have read messages when going out.
}
}
void TimelineModel::onDefaultProxyChanged(){
if( mSelected && !mChatRoomModel->isCurrentProxy())
setSelected(false);
}
//----------------------------------------------------------
//------ CHAT ROOM HANDLERS
//----------------------------------------------------------
void TimelineModel::onIsComposingReceived(const std::shared_ptr & chatRoom, const std::shared_ptr & remoteAddress, bool isComposing){
}
void TimelineModel::onMessageReceived(const std::shared_ptr & chatRoom, const std::shared_ptr & message){}
void TimelineModel::onNewEvent(const std::shared_ptr & chatRoom, const std::shared_ptr & eventLog){}
void TimelineModel::onChatMessageReceived(const std::shared_ptr & chatRoom, const std::shared_ptr & eventLog){}
void TimelineModel::onChatMessageSending(const std::shared_ptr & chatRoom, const std::shared_ptr & eventLog){}
void TimelineModel::onChatMessageSent(const std::shared_ptr & chatRoom, const std::shared_ptr & eventLog){}
void TimelineModel::onParticipantAdded(const std::shared_ptr & chatRoom, const std::shared_ptr & eventLog){}
void TimelineModel::onParticipantRemoved(const std::shared_ptr & chatRoom, const std::shared_ptr & eventLog){}
void TimelineModel::onParticipantAdminStatusChanged(const std::shared_ptr & chatRoom, const std::shared_ptr & eventLog){}
void TimelineModel::onStateChanged(const std::shared_ptr & chatRoom, linphone::ChatRoom::State newState){
if(newState == linphone::ChatRoom::State::Created && CoreManager::getInstance()->getTimelineListModel()->mAutoSelectAfterCreation) {
CoreManager::getInstance()->getTimelineListModel()->mAutoSelectAfterCreation = false;
QTimer::singleShot(200, [=](){// Delay process in order to let GUI time for Timeline building/linking before doing actions
setSelected(true);
});
}
}
void TimelineModel::onSecurityEvent(const std::shared_ptr & chatRoom, const std::shared_ptr & eventLog){}
void TimelineModel::onSubjectChanged(const std::shared_ptr & chatRoom, const std::shared_ptr & eventLog)
{
emit usernameChanged();
}
void TimelineModel::onUndecryptableMessageReceived(const std::shared_ptr & chatRoom, const std::shared_ptr & message){}
void TimelineModel::onParticipantDeviceAdded(const std::shared_ptr & chatRoom, const std::shared_ptr & eventLog){}
void TimelineModel::onParticipantDeviceRemoved(const std::shared_ptr & chatRoom, const std::shared_ptr & eventLog){}
void TimelineModel::onConferenceJoined(const std::shared_ptr & chatRoom, const std::shared_ptr & eventLog){
}
void TimelineModel::onConferenceLeft(const std::shared_ptr & chatRoom, const std::shared_ptr & eventLog){
}
void TimelineModel::onEphemeralEvent(const std::shared_ptr & chatRoom, const std::shared_ptr & eventLog){}
void TimelineModel::onEphemeralMessageTimerStarted(const std::shared_ptr & chatRoom, const std::shared_ptr & eventLog){}
void TimelineModel::onEphemeralMessageDeleted(const std::shared_ptr & chatRoom, const std::shared_ptr & eventLog){}
void TimelineModel::onConferenceAddressGeneration(const std::shared_ptr & chatRoom){}
void TimelineModel::onParticipantRegistrationSubscriptionRequested(const std::shared_ptr & chatRoom, const std::shared_ptr & participantAddress){}
void TimelineModel::onParticipantRegistrationUnsubscriptionRequested(const std::shared_ptr & chatRoom, const std::shared_ptr & participantAddress){}
void TimelineModel::onChatMessageShouldBeStored(const std::shared_ptr & chatRoom, const std::shared_ptr & message){}
void TimelineModel::onChatMessageParticipantImdnStateChanged(const std::shared_ptr & chatRoom, const std::shared_ptr & message, const std::shared_ptr & state){}