blob: 71297ae48916097b9beef86dbccdf2c203393e39 [file] [log] [blame]
Olivier Soldanod4311552017-11-20 15:09:53 -05001/*
Sébastien Blin029ffa82019-01-02 17:43:48 -05002 * Copyright (C) 2017-2019 Savoir-faire Linux Inc.
Olivier Soldanod4311552017-11-20 15:09:53 -05003 * 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"
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -050024#import "views/NSColor+RingTheme.h"
25
Andreas Traczyk77a50d52018-05-08 17:47:31 -040026
27// new lrc
Olivier Soldanod4311552017-11-20 15:09:53 -050028#import <api/conversation.h>
29#import <api/conversationmodel.h>
30#import <api/account.h>
31#import <api/contactmodel.h>
32#import <api/contact.h>
Andreas Traczyk77a50d52018-05-08 17:47:31 -040033
Olivier Soldanod4311552017-11-20 15:09:53 -050034static inline NSString* bestIDForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model)
35{
Andreas Traczyk4e39aa52018-05-11 17:29:26 -040036 try {
37 auto contact = model.owner.contactModel->getContact(conv.participants[0]);
38 if (!contact.registeredName.empty()) {
39 contact.registeredName.erase(std::remove(contact.registeredName.begin(), contact.registeredName.end(), '\n'), contact.registeredName.end());
40 contact.registeredName.erase(std::remove(contact.registeredName.begin(), contact.registeredName.end(), '\r'), contact.registeredName.end());
41 return [@(contact.registeredName.c_str()) removeEmptyLinesAtBorders];
42 }
43 else {
44 return [@(contact.profileInfo.uri.c_str()) removeEmptyLinesAtBorders];
45 }
46 } catch (std::out_of_range& e) {
47 NSLog(@"bestIDForConversation: getContact - out of range");
Kateryna Kostiuk4c703872018-12-28 17:29:05 -050048 return @"";
Andreas Traczyk252a94a2018-04-20 16:36:20 -040049 }
Olivier Soldanod4311552017-11-20 15:09:53 -050050}
51
Kateryna Kostiukd73f9602018-07-24 13:51:28 -040052static inline NSString* bestIDForAccount(const lrc::api::account::Info& account)
53{
54 if (!account.registeredName.empty()) {
55 return [@(account.registeredName.c_str()) removeEmptyLinesAtBorders];
56 }
57 return [@(account.profileInfo.uri.c_str()) removeEmptyLinesAtBorders];
58}
59
60static inline NSString* bestNameForAccount(const lrc::api::account::Info& account)
61{
62 if (account.profileInfo.alias.empty()) {
63 return bestIDForAccount(account);
64 }
65 return @(account.profileInfo.alias.c_str());
66}
67
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040068static inline NSString* bestIDForContact(const lrc::api::contact::Info& contact)
69{
70 if (!contact.registeredName.empty()) {
71 return [@(contact.registeredName.c_str()) removeEmptyLinesAtBorders];
72 }
73 return [@(contact.profileInfo.uri.c_str()) removeEmptyLinesAtBorders];
74}
75
76static inline NSString* bestNameForContact(const lrc::api::contact::Info& contact)
77{
78 if (contact.profileInfo.alias.empty()) {
79 return bestIDForContact(contact);
80 }
81 return @(contact.profileInfo.alias.c_str());
82}
83
Olivier Soldanod4311552017-11-20 15:09:53 -050084static inline NSString* bestNameForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model)
85{
Andreas Traczyk4e39aa52018-05-11 17:29:26 -040086 try {
87 auto contact = model.owner.contactModel->getContact(conv.participants[0]);
88 if (contact.profileInfo.alias.empty()) {
89 return bestIDForConversation(conv, model);
90 }
91 auto alias = contact.profileInfo.alias;
92 alias.erase(std::remove(alias.begin(), alias.end(), '\n'), alias.end());
93 alias.erase(std::remove(alias.begin(), alias.end(), '\r'), alias.end());
94 if(alias.length() == 0) {
95 return bestIDForConversation(conv, model);
96 }
97 return @(alias.c_str());
98 } catch (std::out_of_range& e) {
99 NSLog(@"bestNameForConversation: getContact - out of range");
Kateryna Kostiuk4c703872018-12-28 17:29:05 -0500100 return @"";
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400101 }
Andreas Traczyk252a94a2018-04-20 16:36:20 -0400102}
103
104static inline lrc::api::profile::Type profileType(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model)
105{
106 @try {
107 auto contact = model.owner.contactModel->getContact(conv.participants[0]);
108 return contact.profileInfo.type;
109 }
110 @catch (NSException *exception) {
111 lrc::api::profile::Type::INVALID;
112 }
Olivier Soldanod4311552017-11-20 15:09:53 -0500113}
Anthony Léonard6f819752018-01-05 09:53:40 -0500114
115/**
116 * This function return an iterator pointing to a Conversation::Info in ConversationModel given its uid. If not found
117 * the iterator is invalid thus it needs to be checked by caller.
118 * @param uid UID of conversation being searched
119 * @param model ConversationModel in which to do the lookup
120 * @return iterator pointing to corresponding Conversation if any. Points to past-the-end element otherwise.
121 */
122static inline lrc::api::ConversationModel::ConversationQueue::const_iterator getConversationFromUid(const std::string& uid, const lrc::api::ConversationModel& model) {
123 return std::find_if(model.allFilteredConversations().begin(), model.allFilteredConversations().end(),
124 [&] (const lrc::api::conversation::Info& conv) {
125 return uid == conv.uid;
126 });
127}
Andreas Traczyk77a50d52018-05-08 17:47:31 -0400128
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500129/**
130 * This function return an iterator pointing to a Conversation::Info in ConversationModel given its participant uri. Will not work for group chat.
131 * @param uri URI of participant
132 * @param model ConversationModel in which to do the lookup
133 * @return iterator pointing to corresponding Conversation if any. Points to past-the-end element otherwise.
134 */
135static inline lrc::api::ConversationModel::ConversationQueue::const_iterator getConversationFromURI(const std::string& uri, const lrc::api::ConversationModel& model) {
136 return std::find_if(model.allFilteredConversations().begin(), model.allFilteredConversations().end(),
137 [&] (const lrc::api::conversation::Info& conv) {
138 return uri == conv.participants[0];
139 });
140}
141
Kateryna Kostiuk4138db12018-06-08 15:52:18 -0400142static inline bool isUrlAccessibleFromSandbox(NSURL* url)
143{
144 NSFileManager* fileManager = [[NSFileManager alloc] init];
145 NSArray* urlPathsMusic = [fileManager URLsForDirectory:NSMusicDirectory
146 inDomains:NSUserDomainMask];
147 NSArray* urlPathsPictures = [fileManager URLsForDirectory:NSPicturesDirectory
148 inDomains:NSUserDomainMask];
149 NSArray* urlPathsDownloads = [fileManager URLsForDirectory:NSDownloadsDirectory
150 inDomains:NSUserDomainMask];
151 NSArray* urlPathsMovies = [fileManager URLsForDirectory:NSMoviesDirectory
152 inDomains:NSUserDomainMask];
153 NSArray* availablePaths = [[[urlPathsMusic arrayByAddingObjectsFromArray: urlPathsPictures] arrayByAddingObjectsFromArray: urlPathsDownloads] arrayByAddingObjectsFromArray: urlPathsMovies];
154 if([availablePaths containsObject:url]) {
155 return YES;
156 }
157 for (NSURL* availableUrl in availablePaths) {
158 if ([url.path containsString:availableUrl.path]) {
159 return YES;
160 }
161 }
162 return NO;
163}
164
165static inline bool appSandboxed()
166{
167 NSString* bundleID = [[NSBundle mainBundle] bundleIdentifier];
Kateryna Kostiukb88d27f2018-06-14 12:56:52 -0400168 NSString* url = [[NSURL fileURLWithPath:NSHomeDirectory()] path];
169 NSString* appPath = [@"Library/Containers/" stringByAppendingString:bundleID];
170 if ([url containsString: appPath]) {
Kateryna Kostiuk4138db12018-06-08 15:52:18 -0400171 return YES;
172 }
173 return NO;
174}
Kateryna Kostiuk9fca5422018-11-19 16:27:46 -0500175
176static inline NSColor* colorForAccountStatus(const lrc::api::account::Status status)
177{
178 NSColor *accountStatusColor = [NSColor unregisteredColor];
179 switch (status) {
180 case lrc::api::account::Status::REGISTERED:
181 accountStatusColor = [NSColor presenceColor];
182 break;
183 case lrc::api::account::Status::TRYING:
184 accountStatusColor = [NSColor orangeColor];
185 break;
186 default:
187 break;
188 }
189 return accountStatusColor;
190}