Commit 3e65731b authored by Ronan's avatar Ronan

feat(app): clean code

parent a58e2135
......@@ -140,9 +140,9 @@ public:
private:
QList<ChatEntryData>::iterator findMessageEntry (const shared_ptr<linphone::ChatMessage> &message) {
return find_if(mChatModel->mEntries.begin(), mChatModel->mEntries.end(), [&message](const ChatEntryData &pair) {
return pair.second == message;
});
return find_if(mChatModel->mEntries.begin(), mChatModel->mEntries.end(), [&message](const ChatEntryData &entry) {
return entry.second == message;
});
}
void signalDataChanged (const QList<ChatEntryData>::iterator &it) {
......@@ -210,9 +210,9 @@ private:
// -----------------------------------------------------------------------------
ChatModel::ChatModel (const QString &sipAddress) {
CoreManager *core = CoreManager::getInstance();
CoreManager *coreManager = CoreManager::getInstance();
mCoreHandlers = core->getHandlers();
mCoreHandlers = coreManager->getHandlers();
mMessageHandlers = make_shared<MessageHandlers>(this);
setSipAddress(sipAddress);
......@@ -298,6 +298,7 @@ void ChatModel::setSipAddress (const QString &sipAddress) {
handleIsComposingChanged(mChatRoom);
// Get messages.
mEntries.clear();
for (auto &message : mChatRoom->getHistory(0)) {
QVariantMap map;
......@@ -446,11 +447,11 @@ void ChatModel::downloadFile (int id) {
bool soFarSoGood;
const QString safeFilePath = Utils::getSafeFilePath(
QStringLiteral("%1%2")
QStringLiteral("%1%2")
.arg(CoreManager::getInstance()->getSettingsModel()->getDownloadFolder())
.arg(entry.first["fileName"].toString()),
&soFarSoGood
);
&soFarSoGood
);
if (!soFarSoGood) {
qWarning() << QStringLiteral("Unable to create safe file path for: %1.").arg(id);
......@@ -561,34 +562,34 @@ void ChatModel::fillCallEndEntry (QVariantMap &dest, const shared_ptr<linphone::
// -----------------------------------------------------------------------------
void ChatModel::removeEntry (ChatEntryData &pair) {
int type = pair.first["type"].toInt();
void ChatModel::removeEntry (ChatEntryData &entry) {
int type = entry.first["type"].toInt();
switch (type) {
case ChatModel::MessageEntry: {
shared_ptr<linphone::ChatMessage> message = static_pointer_cast<linphone::ChatMessage>(pair.second);
shared_ptr<linphone::ChatMessage> message = static_pointer_cast<linphone::ChatMessage>(entry.second);
removeFileMessageThumbnail(message);
mChatRoom->deleteMessage(message);
break;
}
case ChatModel::CallEntry: {
if (pair.first["status"].toInt() == linphone::CallStatusSuccess) {
if (entry.first["status"].toInt() == linphone::CallStatusSuccess) {
// WARNING: Unable to remove symmetric call here. (start/end)
// We are between `beginRemoveRows` and `endRemoveRows`.
// A solution is to schedule a `removeEntry` call in the Qt main loop.
shared_ptr<void> linphonePtr = pair.second;
shared_ptr<void> linphonePtr = entry.second;
QTimer::singleShot(0, this, [this, linphonePtr]() {
auto it = find_if(mEntries.begin(), mEntries.end(), [linphonePtr](const ChatEntryData &pair) {
return pair.second == linphonePtr;
});
if (it != mEntries.end())
removeEntry(int(distance(mEntries.begin(), it)));
auto it = find_if(mEntries.begin(), mEntries.end(), [linphonePtr](const ChatEntryData &entry) {
return entry.second == linphonePtr;
});
if (it != mEntries.end())
removeEntry(int(distance(mEntries.begin(), it)));
});
}
CoreManager::getInstance()->getCore()->removeCallLog(static_pointer_cast<linphone::CallLog>(pair.second));
CoreManager::getInstance()->getCore()->removeCallLog(static_pointer_cast<linphone::CallLog>(entry.second));
break;
}
......@@ -616,17 +617,17 @@ void ChatModel::insertCall (const shared_ptr<linphone::CallLog> &callLog) {
}
auto insertEntry = [this](
const ChatEntryData &pair,
const ChatEntryData &entry,
const QList<ChatEntryData>::iterator *start = NULL
) {
auto it = lower_bound(start ? *start : mEntries.begin(), mEntries.end(), pair, [](const ChatEntryData &a, const ChatEntryData &b) {
auto it = lower_bound(start ? *start : mEntries.begin(), mEntries.end(), entry, [](const ChatEntryData &a, const ChatEntryData &b) {
return a.first["timestamp"] < b.first["timestamp"];
});
int row = int(distance(mEntries.begin(), it));
beginInsertRows(QModelIndex(), row, row);
it = mEntries.insert(it, pair);
it = mEntries.insert(it, entry);
endInsertRows();
return it;
......
......@@ -127,7 +127,7 @@ private:
void fillCallStartEntry (QVariantMap &dest, const std::shared_ptr<linphone::CallLog> &callLog);
void fillCallEndEntry (QVariantMap &dest, const std::shared_ptr<linphone::CallLog> &callLog);
void removeEntry (ChatEntryData &pair);
void removeEntry (ChatEntryData &entry);
void insertCall (const std::shared_ptr<linphone::CallLog> &callLog);
void insertMessageAtEnd (const std::shared_ptr<linphone::ChatMessage> &message);
......
......@@ -122,12 +122,11 @@ SipAddressObserver *SipAddressesModel::getSipAddressObserver (const QString &sip
}
mObservers.insert(cleanedSipAddress, model);
QObject::connect(
model, &SipAddressObserver::destroyed, this, [this, model, cleanedSipAddress]() {
// Do not use `model` methods here. `model` is partially destroyed here!
if (mObservers.remove(cleanedSipAddress, model) == 0)
qWarning() << QStringLiteral("Unable to remove sip address `%1` from observers.").arg(cleanedSipAddress);
});
QObject::connect(model, &SipAddressObserver::destroyed, this, [this, model, cleanedSipAddress]() {
// Do not use `model` methods here. `model` is partially destroyed here!
if (mObservers.remove(cleanedSipAddress, model) == 0)
qWarning() << QStringLiteral("Unable to remove sip address `%1` from observers.").arg(cleanedSipAddress);
});
return model;
}
......@@ -136,8 +135,8 @@ SipAddressObserver *SipAddressesModel::getSipAddressObserver (const QString &sip
QString SipAddressesModel::getTransportFromSipAddress (const QString &sipAddress) const {
const shared_ptr<const linphone::Address> address = linphone::Factory::get()->createAddress(
Utils::appStringToCoreString(sipAddress)
);
Utils::appStringToCoreString(sipAddress)
);
if (!address)
return QString("");
......@@ -490,14 +489,20 @@ void SipAddressesModel::removeContactOfSipAddress (const QString &sipAddress) {
removeRow(row);
}
// -----------------------------------------------------------------------------
void SipAddressesModel::initSipAddresses () {
shared_ptr<linphone::Core> core = CoreManager::getInstance()->getCore();
initSipAddressesFromChat();
initSipAddressesFromCalls();
initRefs();
initSipAddressesFromContacts();
}
// Get sip addresses from chatrooms.
for (const auto &chatRoom : core->getChatRooms()) {
void SipAddressesModel::initSipAddressesFromChat () {
for (const auto &chatRoom : CoreManager::getInstance()->getCore()->getChatRooms()) {
list<shared_ptr<linphone::ChatMessage>> history = chatRoom->getHistory(0);
if (history.size() == 0)
if (history.empty())
continue;
QString sipAddress = Utils::coreStringToAppString(chatRoom->getPeerAddress()->asStringUriOnly());
......@@ -507,12 +512,14 @@ void SipAddressesModel::initSipAddresses () {
map["timestamp"] = QDateTime::fromMSecsSinceEpoch(history.back()->getTime() * 1000);
map["unreadMessagesCount"] = chatRoom->getUnreadMessagesCount();
qInfo() << QStringLiteral("Add sip address: `%1`.").arg(map["sipAddress"].toString());
mSipAddresses[sipAddress] = map;
}
}
// Get sip addresses from calls.
void SipAddressesModel::initSipAddressesFromCalls () {
QSet<QString> addressDone;
for (const auto &callLog : core->getCallLogs()) {
for (const auto &callLog : CoreManager::getInstance()->getCore()->getCallLogs()) {
const QString sipAddress = Utils::coreStringToAppString(callLog->getRemoteAddress()->asStringUriOnly());
if (addressDone.contains(sipAddress))
......@@ -535,17 +542,18 @@ void SipAddressesModel::initSipAddresses () {
if (it == mSipAddresses.end() || map["timestamp"] > (*it)["timestamp"])
mSipAddresses[sipAddress] = map;
}
}
for (const auto &map : mSipAddresses) {
qInfo() << QStringLiteral("Add sip address: `%1`.").arg(map["sipAddress"].toString());
mRefs << &map;
}
// Get sip addresses from contacts.
void SipAddressesModel::initSipAddressesFromContacts () {
for (auto &contact : CoreManager::getInstance()->getContactsListModel()->mList)
handleContactAdded(contact);
}
void SipAddressesModel::initRefs () {
for (const auto &map : mSipAddresses)
mRefs << &map;
}
// -----------------------------------------------------------------------------
void SipAddressesModel::updateObservers (const QString &sipAddress, ContactModel *contact) {
......
......@@ -109,6 +109,12 @@ private:
void initSipAddresses ();
void initSipAddressesFromChat ();
void initSipAddressesFromCalls ();
void initSipAddressesFromContacts ();
void initRefs ();
void updateObservers (const QString &sipAddress, ContactModel *contact);
void updateObservers (const QString &sipAddress, const Presence::PresenceStatus &presenceStatus);
void updateObservers (const QString &sipAddress, int messagesCount);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment