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 | |
Kateryna Kostiuk | 0c0801e | 2019-07-18 20:10:51 -0400 | [diff] [blame] | 34 | //Qt |
| 35 | #import <QtCore/QDir> |
| 36 | #import <qapplication.h> |
| 37 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 38 | static inline NSString* bestIDForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model) |
| 39 | { |
Andreas Traczyk | 4e39aa5 | 2018-05-11 17:29:26 -0400 | [diff] [blame] | 40 | try { |
| 41 | auto contact = model.owner.contactModel->getContact(conv.participants[0]); |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 42 | auto name = contact.registeredName.trimmed().replace("\r","").replace("\n",""); |
| 43 | if (!name.isEmpty()) { |
| 44 | return [name.toNSString() removeEmptyLinesAtBorders]; |
Andreas Traczyk | 4e39aa5 | 2018-05-11 17:29:26 -0400 | [diff] [blame] | 45 | } |
| 46 | else { |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 47 | return [contact.profileInfo.uri.trimmed().replace("\r","").replace("\n","").toNSString() removeEmptyLinesAtBorders]; |
Andreas Traczyk | 4e39aa5 | 2018-05-11 17:29:26 -0400 | [diff] [blame] | 48 | } |
| 49 | } catch (std::out_of_range& e) { |
| 50 | NSLog(@"bestIDForConversation: getContact - out of range"); |
Kateryna Kostiuk | 4c70387 | 2018-12-28 17:29:05 -0500 | [diff] [blame] | 51 | return @""; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 52 | } |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 53 | } |
| 54 | |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 55 | static inline NSString* bestIDForAccount(const lrc::api::account::Info& account) |
| 56 | { |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 57 | auto name = account.registeredName.trimmed().replace("\r","").replace("\n",""); |
| 58 | if (!name.isEmpty()) { |
| 59 | return [name.toNSString() removeEmptyLinesAtBorders]; |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 60 | } |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 61 | return [account.profileInfo.uri.trimmed().replace("\r","").replace("\n","").toNSString() removeEmptyLinesAtBorders]; |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 62 | } |
| 63 | |
| 64 | static inline NSString* bestNameForAccount(const lrc::api::account::Info& account) |
| 65 | { |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 66 | if (account.profileInfo.alias.isEmpty()) { |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 67 | return bestIDForAccount(account); |
| 68 | } |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 69 | return account.profileInfo.alias.toNSString(); |
Kateryna Kostiuk | d73f960 | 2018-07-24 13:51:28 -0400 | [diff] [blame] | 70 | } |
| 71 | |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 72 | static inline NSString* bestIDForContact(const lrc::api::contact::Info& contact) |
| 73 | { |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 74 | auto name = contact.registeredName.trimmed().replace("\r","").replace("\n",""); |
| 75 | if (!name.isEmpty()) { |
| 76 | return [name.toNSString() removeEmptyLinesAtBorders]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 77 | } |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 78 | return [contact.profileInfo.uri.trimmed().replace("\r","").replace("\n","").toNSString() removeEmptyLinesAtBorders]; |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | static inline NSString* bestNameForContact(const lrc::api::contact::Info& contact) |
| 82 | { |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 83 | if (contact.profileInfo.alias.isEmpty()) { |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 84 | return bestIDForContact(contact); |
| 85 | } |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 86 | return contact.profileInfo.alias.toNSString(); |
Kateryna Kostiuk | 1f8c125 | 2018-07-30 18:18:57 -0400 | [diff] [blame] | 87 | } |
| 88 | |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 89 | static inline NSString* bestNameForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model) |
| 90 | { |
Andreas Traczyk | 4e39aa5 | 2018-05-11 17:29:26 -0400 | [diff] [blame] | 91 | try { |
| 92 | auto contact = model.owner.contactModel->getContact(conv.participants[0]); |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 93 | if (contact.profileInfo.alias.isEmpty()) { |
Andreas Traczyk | 4e39aa5 | 2018-05-11 17:29:26 -0400 | [diff] [blame] | 94 | return bestIDForConversation(conv, model); |
| 95 | } |
| 96 | auto alias = contact.profileInfo.alias; |
Andreas Traczyk | 4e39aa5 | 2018-05-11 17:29:26 -0400 | [diff] [blame] | 97 | if(alias.length() == 0) { |
| 98 | return bestIDForConversation(conv, model); |
| 99 | } |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 100 | return [alias.toNSString() removeEmptyLinesAtBorders]; |
Andreas Traczyk | 4e39aa5 | 2018-05-11 17:29:26 -0400 | [diff] [blame] | 101 | } catch (std::out_of_range& e) { |
| 102 | NSLog(@"bestNameForConversation: getContact - out of range"); |
Kateryna Kostiuk | 4c70387 | 2018-12-28 17:29:05 -0500 | [diff] [blame] | 103 | return @""; |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 104 | } |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 105 | } |
| 106 | |
Kateryna Kostiuk | 0c0801e | 2019-07-18 20:10:51 -0400 | [diff] [blame] | 107 | static inline NSString* defaultRingtonePath() { |
| 108 | QDir ringtonesDir(QCoreApplication::applicationDirPath()); |
| 109 | ringtonesDir.cdUp(); |
| 110 | ringtonesDir.cd("Resources/ringtones/"); |
| 111 | return [ringtonesDir.path().toNSString() stringByAppendingString:@"/default.opus"]; |
| 112 | } |
| 113 | |
Andreas Traczyk | 252a94a | 2018-04-20 16:36:20 -0400 | [diff] [blame] | 114 | static inline lrc::api::profile::Type profileType(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model) |
| 115 | { |
| 116 | @try { |
| 117 | auto contact = model.owner.contactModel->getContact(conv.participants[0]); |
| 118 | return contact.profileInfo.type; |
| 119 | } |
| 120 | @catch (NSException *exception) { |
| 121 | lrc::api::profile::Type::INVALID; |
| 122 | } |
Olivier Soldano | d431155 | 2017-11-20 15:09:53 -0500 | [diff] [blame] | 123 | } |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 124 | |
| 125 | /** |
| 126 | * This function return an iterator pointing to a Conversation::Info in ConversationModel given its uid. If not found |
| 127 | * the iterator is invalid thus it needs to be checked by caller. |
| 128 | * @param uid UID of conversation being searched |
| 129 | * @param model ConversationModel in which to do the lookup |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 130 | * @param includeSearchResult include search result |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 131 | * @return iterator pointing to corresponding Conversation if any. Points to past-the-end element otherwise. |
| 132 | */ |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 133 | static inline lrc::api::ConversationModel::ConversationQueue::const_iterator getConversationFromUid(const QString& uid, const lrc::api::ConversationModel& model, bool includeSearchResult) { |
| 134 | auto result = std::find_if(model.allFilteredConversations().begin(), model.allFilteredConversations().end(), |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 135 | [&] (const lrc::api::conversation::Info& conv) { |
| 136 | return uid == conv.uid; |
| 137 | }); |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 138 | if (!includeSearchResult || (result != model.allFilteredConversations().end())) { |
| 139 | return result; |
| 140 | } |
| 141 | return std::find_if(model.getAllSearchResults().begin(), model.getAllSearchResults().end(), |
| 142 | [&] (const lrc::api::conversation::Info& conv) { |
| 143 | return uid == conv.uid; |
| 144 | }); |
| 145 | } |
| 146 | |
| 147 | /** |
| 148 | * This function true if conversation exists |
| 149 | * the iterator is invalid thus it needs to be checked by caller. |
| 150 | * @param conversation iterator pointing to a Conversation::Info in ConversationModel |
| 151 | * @param model ConversationModel in which to do the lookup |
| 152 | * @param includeSearchResult include search result |
| 153 | * @return iterator pointing to corresponding Conversation if any. Points to past-the-end element otherwise. |
| 154 | */ |
| 155 | static inline bool conversationExists(lrc::api::ConversationModel::ConversationQueue::const_iterator conversation, const lrc::api::ConversationModel& model, bool includeSearchResult) { |
| 156 | return includeSearchResult ? (conversation != model.allFilteredConversations().end() || conversation != model.getAllSearchResults().end()) : conversation != model.allFilteredConversations().end(); |
Anthony Léonard | 6f81975 | 2018-01-05 09:53:40 -0500 | [diff] [blame] | 157 | } |
Andreas Traczyk | 77a50d5 | 2018-05-08 17:47:31 -0400 | [diff] [blame] | 158 | |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 159 | /** |
| 160 | * This function return an iterator pointing to a Conversation::Info in ConversationModel given its participant uri. Will not work for group chat. |
| 161 | * @param uri URI of participant |
| 162 | * @param model ConversationModel in which to do the lookup |
| 163 | * @return iterator pointing to corresponding Conversation if any. Points to past-the-end element otherwise. |
| 164 | */ |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 165 | static inline lrc::api::ConversationModel::ConversationQueue::const_iterator getConversationFromURI(const QString& uri, const lrc::api::ConversationModel& model) { |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 166 | return std::find_if(model.allFilteredConversations().begin(), model.allFilteredConversations().end(), |
| 167 | [&] (const lrc::api::conversation::Info& conv) { |
| 168 | return uri == conv.participants[0]; |
| 169 | }); |
| 170 | } |
| 171 | |
Kateryna Kostiuk | 4138db1 | 2018-06-08 15:52:18 -0400 | [diff] [blame] | 172 | static inline bool isUrlAccessibleFromSandbox(NSURL* url) |
| 173 | { |
| 174 | NSFileManager* fileManager = [[NSFileManager alloc] init]; |
| 175 | NSArray* urlPathsMusic = [fileManager URLsForDirectory:NSMusicDirectory |
| 176 | inDomains:NSUserDomainMask]; |
| 177 | NSArray* urlPathsPictures = [fileManager URLsForDirectory:NSPicturesDirectory |
| 178 | inDomains:NSUserDomainMask]; |
| 179 | NSArray* urlPathsDownloads = [fileManager URLsForDirectory:NSDownloadsDirectory |
| 180 | inDomains:NSUserDomainMask]; |
| 181 | NSArray* urlPathsMovies = [fileManager URLsForDirectory:NSMoviesDirectory |
| 182 | inDomains:NSUserDomainMask]; |
| 183 | NSArray* availablePaths = [[[urlPathsMusic arrayByAddingObjectsFromArray: urlPathsPictures] arrayByAddingObjectsFromArray: urlPathsDownloads] arrayByAddingObjectsFromArray: urlPathsMovies]; |
| 184 | if([availablePaths containsObject:url]) { |
| 185 | return YES; |
| 186 | } |
| 187 | for (NSURL* availableUrl in availablePaths) { |
| 188 | if ([url.path containsString:availableUrl.path]) { |
| 189 | return YES; |
| 190 | } |
| 191 | } |
| 192 | return NO; |
| 193 | } |
| 194 | |
| 195 | static inline bool appSandboxed() |
| 196 | { |
| 197 | NSString* bundleID = [[NSBundle mainBundle] bundleIdentifier]; |
Kateryna Kostiuk | b88d27f | 2018-06-14 12:56:52 -0400 | [diff] [blame] | 198 | NSString* url = [[NSURL fileURLWithPath:NSHomeDirectory()] path]; |
| 199 | NSString* appPath = [@"Library/Containers/" stringByAppendingString:bundleID]; |
| 200 | if ([url containsString: appPath]) { |
Kateryna Kostiuk | 4138db1 | 2018-06-08 15:52:18 -0400 | [diff] [blame] | 201 | return YES; |
| 202 | } |
| 203 | return NO; |
| 204 | } |
Kateryna Kostiuk | 9fca542 | 2018-11-19 16:27:46 -0500 | [diff] [blame] | 205 | |
| 206 | static inline NSColor* colorForAccountStatus(const lrc::api::account::Status status) |
| 207 | { |
| 208 | NSColor *accountStatusColor = [NSColor unregisteredColor]; |
| 209 | switch (status) { |
| 210 | case lrc::api::account::Status::REGISTERED: |
| 211 | accountStatusColor = [NSColor presenceColor]; |
| 212 | break; |
| 213 | case lrc::api::account::Status::TRYING: |
| 214 | accountStatusColor = [NSColor orangeColor]; |
| 215 | break; |
| 216 | default: |
| 217 | break; |
| 218 | } |
| 219 | return accountStatusColor; |
| 220 | } |