Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 Savoir-faire Linux Inc. |
| 3 | * Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com> |
| 4 | * |
| 5 | * This program is free software; you can redistribute it and/or modify |
| 6 | * it under the terms of the GNU General Public License as published by |
| 7 | * the Free Software Foundation; either version 3 of the License, or |
| 8 | * (at your option) any later version. |
| 9 | * |
| 10 | * This program is distributed in the hope that it will be useful, |
| 11 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 13 | * GNU General Public License for more details. |
| 14 | * |
| 15 | * You should have received a copy of the GNU General Public License |
| 16 | * along with this program; if not, write to the Free Software |
| 17 | * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. |
| 18 | */ |
| 19 | |
| 20 | #import <Foundation/Foundation.h> |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame^] | 21 | #import "NSString+Extensions.h" |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 22 | #import <api/conversation.h> |
| 23 | #import <api/conversationmodel.h> |
| 24 | #import <api/account.h> |
| 25 | #import <api/contactmodel.h> |
| 26 | #import <api/contact.h> |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 27 | #import <map> |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 28 | |
| 29 | static inline NSString* bestIDForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model) |
| 30 | { |
| 31 | auto contact = model.owner.contactModel->getContact(conv.participants[0]); |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame^] | 32 | if (!contact.registeredName.empty()) { |
| 33 | contact.registeredName.erase(std::remove(contact.registeredName.begin(), contact.registeredName.end(), '\n'), contact.registeredName.end()); |
| 34 | contact.registeredName.erase(std::remove(contact.registeredName.begin(), contact.registeredName.end(), '\r'), contact.registeredName.end()); |
| 35 | return [@(contact.registeredName.c_str()) removeEmptyLinesAtBorders]; |
| 36 | } |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 37 | else |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame^] | 38 | return [@(contact.profileInfo.uri.c_str()) removeEmptyLinesAtBorders]; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 39 | } |
| 40 | |
| 41 | static inline NSString* bestNameForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model) |
| 42 | { |
| 43 | auto contact = model.owner.contactModel->getContact(conv.participants[0]); |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame^] | 44 | if (contact.profileInfo.alias.empty()) { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 45 | return bestIDForConversation(conv, model); |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame^] | 46 | } |
| 47 | auto alias = contact.profileInfo.alias; |
| 48 | alias.erase(std::remove(alias.begin(), alias.end(), '\n'), alias.end()); |
| 49 | alias.erase(std::remove(alias.begin(), alias.end(), '\r'), alias.end()); |
| 50 | if(alias.length() == 0) { |
| 51 | return bestIDForConversation(conv, model); |
| 52 | } |
| 53 | return @(alias.c_str()); |
| 54 | } |
| 55 | |
| 56 | static inline lrc::api::profile::Type profileType(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model) |
| 57 | { |
| 58 | @try { |
| 59 | auto contact = model.owner.contactModel->getContact(conv.participants[0]); |
| 60 | return contact.profileInfo.type; |
| 61 | } |
| 62 | @catch (NSException *exception) { |
| 63 | lrc::api::profile::Type::INVALID; |
| 64 | } |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 65 | } |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 66 | |
| 67 | /** |
| 68 | * This function return an iterator pointing to a Conversation::Info in ConversationModel given its uid. If not found |
| 69 | * the iterator is invalid thus it needs to be checked by caller. |
| 70 | * @param uid UID of conversation being searched |
| 71 | * @param model ConversationModel in which to do the lookup |
| 72 | * @return iterator pointing to corresponding Conversation if any. Points to past-the-end element otherwise. |
| 73 | */ |
| 74 | static inline lrc::api::ConversationModel::ConversationQueue::const_iterator getConversationFromUid(const std::string& uid, const lrc::api::ConversationModel& model) { |
| 75 | return std::find_if(model.allFilteredConversations().begin(), model.allFilteredConversations().end(), |
| 76 | [&] (const lrc::api::conversation::Info& conv) { |
| 77 | return uid == conv.uid; |
| 78 | }); |
| 79 | } |