blob: bf07d1a1415d8316a779fd60f0e31ec7722ba761 [file] [log] [blame]
Olivier Soldanod4311552017-11-20 15:09:53 -05001/*
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 Traczyk77a50d52018-05-08 17:47:31 -040020#import <map>
21
Olivier Soldanod4311552017-11-20 15:09:53 -050022#import <Foundation/Foundation.h>
Andreas Traczyk252a94a2018-04-20 16:36:20 -040023#import "NSString+Extensions.h"
Andreas Traczyk77a50d52018-05-08 17:47:31 -040024
25// new lrc
Olivier Soldanod4311552017-11-20 15:09:53 -050026#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 Traczyk77a50d52018-05-08 17:47:31 -040031
32// old lrc
33#import <QSortFilterProxyModel>
34#import <accountmodel.h>
35#import <codecmodel.h>
Olivier Soldanod4311552017-11-20 15:09:53 -050036
37static 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 Traczyk252a94a2018-04-20 16:36:20 -040040 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 Soldanod4311552017-11-20 15:09:53 -050045 else
Andreas Traczyk252a94a2018-04-20 16:36:20 -040046 return [@(contact.profileInfo.uri.c_str()) removeEmptyLinesAtBorders];
Olivier Soldanod4311552017-11-20 15:09:53 -050047}
48
49static 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 Traczyk252a94a2018-04-20 16:36:20 -040052 if (contact.profileInfo.alias.empty()) {
Olivier Soldanod4311552017-11-20 15:09:53 -050053 return bestIDForConversation(conv, model);
Andreas Traczyk252a94a2018-04-20 16:36:20 -040054 }
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
64static 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 Soldanod4311552017-11-20 15:09:53 -050073}
Anthony Léonard6f819752018-01-05 09:53:40 -050074
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 */
82static 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 Traczyk77a50d52018-05-08 17:47:31 -040088
89static inline void
90setVideoAutoQuality(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}