Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 1 | /* |
Sébastien Blin | 029ffa8 | 2019-01-02 17:43:48 -0500 | [diff] [blame] | 2 | * Copyright (C) 2017-2019 Savoir-faire Linux Inc. |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 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" |
Kateryna Kostiuk | 9fca542 | 2018-11-19 16:27:46 -0500 | [diff] [blame] | 24 | #import "views/NSColor+RingTheme.h" |
| 25 | |
Andreas Traczyk | 77a50d5 | 2018-05-08 17:47:31 -0400 | [diff] [blame] | 26 | |
| 27 | // new lrc |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 28 | #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 Traczyk | 77a50d5 | 2018-05-08 17:47:31 -0400 | [diff] [blame] | 33 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 34 | static inline NSString* bestIDForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model) |
| 35 | { |
Andreas Traczyk | 4e39aa5 | 2018-05-11 17:29:26 -0400 | [diff] [blame] | 36 | 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 Kostiuk | 4c70387 | 2018-12-28 17:29:05 -0500 | [diff] [blame] | 48 | return @""; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 49 | } |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 50 | } |
| 51 | |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 52 | static 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 | |
| 60 | static 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 Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 68 | static 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 | |
| 76 | static 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 Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 84 | static inline NSString* bestNameForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model) |
| 85 | { |
Andreas Traczyk | 4e39aa5 | 2018-05-11 17:29:26 -0400 | [diff] [blame] | 86 | 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 Kostiuk | 4c70387 | 2018-12-28 17:29:05 -0500 | [diff] [blame] | 100 | return @""; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 101 | } |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 102 | } |
| 103 | |
| 104 | static 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 Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 113 | } |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 114 | |
| 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 | */ |
| 122 | static 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 Traczyk | 77a50d5 | 2018-05-08 17:47:31 -0400 | [diff] [blame] | 128 | |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 129 | /** |
| 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 | */ |
| 135 | static 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 Kostiuk | 4138db1 | 2018-06-08 15:52:18 -0400 | [diff] [blame] | 142 | static 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 | |
| 165 | static inline bool appSandboxed() |
| 166 | { |
| 167 | NSString* bundleID = [[NSBundle mainBundle] bundleIdentifier]; |
Kateryna Kostiuk | b88d27f | 2018-06-14 12:56:52 -0400 | [diff] [blame] | 168 | NSString* url = [[NSURL fileURLWithPath:NSHomeDirectory()] path]; |
| 169 | NSString* appPath = [@"Library/Containers/" stringByAppendingString:bundleID]; |
| 170 | if ([url containsString: appPath]) { |
Kateryna Kostiuk | 4138db1 | 2018-06-08 15:52:18 -0400 | [diff] [blame] | 171 | return YES; |
| 172 | } |
| 173 | return NO; |
| 174 | } |
Kateryna Kostiuk | 9fca542 | 2018-11-19 16:27:46 -0500 | [diff] [blame] | 175 | |
| 176 | static 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 | } |