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 | |
Andreas Traczyk | 77a50d5 | 2018-05-08 17:47:31 -0400 | [diff] [blame^] | 20 | #import <map> |
| 21 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 22 | #import <Foundation/Foundation.h> |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 23 | #import "NSString+Extensions.h" |
Andreas Traczyk | 77a50d5 | 2018-05-08 17:47:31 -0400 | [diff] [blame^] | 24 | |
| 25 | // new lrc |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 26 | #import <api/conversation.h> |
| 27 | #import <api/conversationmodel.h> |
| 28 | #import <api/account.h> |
| 29 | #import <api/contactmodel.h> |
| 30 | #import <api/contact.h> |
Andreas Traczyk | 77a50d5 | 2018-05-08 17:47:31 -0400 | [diff] [blame^] | 31 | |
| 32 | // old lrc |
| 33 | #import <QSortFilterProxyModel> |
| 34 | #import <accountmodel.h> |
| 35 | #import <codecmodel.h> |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 36 | |
| 37 | static inline NSString* bestIDForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model) |
| 38 | { |
| 39 | auto contact = model.owner.contactModel->getContact(conv.participants[0]); |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 40 | if (!contact.registeredName.empty()) { |
| 41 | contact.registeredName.erase(std::remove(contact.registeredName.begin(), contact.registeredName.end(), '\n'), contact.registeredName.end()); |
| 42 | contact.registeredName.erase(std::remove(contact.registeredName.begin(), contact.registeredName.end(), '\r'), contact.registeredName.end()); |
| 43 | return [@(contact.registeredName.c_str()) removeEmptyLinesAtBorders]; |
| 44 | } |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 45 | else |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 46 | return [@(contact.profileInfo.uri.c_str()) removeEmptyLinesAtBorders]; |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | static inline NSString* bestNameForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model) |
| 50 | { |
| 51 | auto contact = model.owner.contactModel->getContact(conv.participants[0]); |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 52 | if (contact.profileInfo.alias.empty()) { |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 53 | return bestIDForConversation(conv, model); |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 54 | } |
| 55 | auto alias = contact.profileInfo.alias; |
| 56 | alias.erase(std::remove(alias.begin(), alias.end(), '\n'), alias.end()); |
| 57 | alias.erase(std::remove(alias.begin(), alias.end(), '\r'), alias.end()); |
| 58 | if(alias.length() == 0) { |
| 59 | return bestIDForConversation(conv, model); |
| 60 | } |
| 61 | return @(alias.c_str()); |
| 62 | } |
| 63 | |
| 64 | static inline lrc::api::profile::Type profileType(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model) |
| 65 | { |
| 66 | @try { |
| 67 | auto contact = model.owner.contactModel->getContact(conv.participants[0]); |
| 68 | return contact.profileInfo.type; |
| 69 | } |
| 70 | @catch (NSException *exception) { |
| 71 | lrc::api::profile::Type::INVALID; |
| 72 | } |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 73 | } |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 74 | |
| 75 | /** |
| 76 | * This function return an iterator pointing to a Conversation::Info in ConversationModel given its uid. If not found |
| 77 | * the iterator is invalid thus it needs to be checked by caller. |
| 78 | * @param uid UID of conversation being searched |
| 79 | * @param model ConversationModel in which to do the lookup |
| 80 | * @return iterator pointing to corresponding Conversation if any. Points to past-the-end element otherwise. |
| 81 | */ |
| 82 | static inline lrc::api::ConversationModel::ConversationQueue::const_iterator getConversationFromUid(const std::string& uid, const lrc::api::ConversationModel& model) { |
| 83 | return std::find_if(model.allFilteredConversations().begin(), model.allFilteredConversations().end(), |
| 84 | [&] (const lrc::api::conversation::Info& conv) { |
| 85 | return uid == conv.uid; |
| 86 | }); |
| 87 | } |
Andreas Traczyk | 77a50d5 | 2018-05-08 17:47:31 -0400 | [diff] [blame^] | 88 | |
| 89 | static inline void |
| 90 | setVideoAutoQuality(bool autoQuality, std::string accountId) |
| 91 | { |
| 92 | auto thisAccount = AccountModel::instance().getById(QByteArray::fromStdString(accountId)); |
| 93 | if (const auto& codecModel = thisAccount->codecModel()) { |
| 94 | const auto& videoCodecs = codecModel->videoCodecs(); |
| 95 | for (int i=0; i < videoCodecs->rowCount();i++) { |
| 96 | const auto& idx = videoCodecs->index(i,0); |
| 97 | |
| 98 | if (autoQuality) { |
| 99 | videoCodecs->setData(idx, "true", CodecModel::Role::AUTO_QUALITY_ENABLED); |
| 100 | } else { |
| 101 | videoCodecs->setData(idx, "false", CodecModel::Role::AUTO_QUALITY_ENABLED); |
| 102 | } |
| 103 | } |
| 104 | codecModel << CodecModel::EditAction::SAVE; |
| 105 | } |
| 106 | } |