utils.h 7.75 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/*
 *  Copyright (C) 2017 Savoir-faire Linux Inc.
 *  Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 3 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.
 */

Andreas Traczyk's avatar
Andreas Traczyk committed
20 21
#import <map>

22
#import <Foundation/Foundation.h>
Andreas Traczyk's avatar
Andreas Traczyk committed
23
#import "NSString+Extensions.h"
24 25
#import "views/NSColor+RingTheme.h"

Andreas Traczyk's avatar
Andreas Traczyk committed
26 27

// new lrc
28 29 30 31 32
#import <api/conversation.h>
#import <api/conversationmodel.h>
#import <api/account.h>
#import <api/contactmodel.h>
#import <api/contact.h>
Andreas Traczyk's avatar
Andreas Traczyk committed
33

34 35
static inline NSString* bestIDForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model)
{
36 37 38 39 40 41 42 43 44 45 46 47
    try {
        auto contact = model.owner.contactModel->getContact(conv.participants[0]);
        if (!contact.registeredName.empty()) {
            contact.registeredName.erase(std::remove(contact.registeredName.begin(), contact.registeredName.end(), '\n'), contact.registeredName.end());
            contact.registeredName.erase(std::remove(contact.registeredName.begin(), contact.registeredName.end(), '\r'), contact.registeredName.end());
            return [@(contact.registeredName.c_str()) removeEmptyLinesAtBorders];
        }
        else {
            return [@(contact.profileInfo.uri.c_str()) removeEmptyLinesAtBorders];
        }
    } catch (std::out_of_range& e) {
        NSLog(@"bestIDForConversation: getContact - out of range");
48
        return @"";
Andreas Traczyk's avatar
Andreas Traczyk committed
49
    }
50 51
}

52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
static inline NSString* bestIDForAccount(const lrc::api::account::Info& account)
{
    if (!account.registeredName.empty()) {
        return [@(account.registeredName.c_str()) removeEmptyLinesAtBorders];
    }
    return [@(account.profileInfo.uri.c_str()) removeEmptyLinesAtBorders];
}

static inline NSString* bestNameForAccount(const lrc::api::account::Info& account)
{
    if (account.profileInfo.alias.empty()) {
        return bestIDForAccount(account);
    }
    return @(account.profileInfo.alias.c_str());
}

68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
static inline NSString* bestIDForContact(const lrc::api::contact::Info& contact)
{
    if (!contact.registeredName.empty()) {
        return [@(contact.registeredName.c_str()) removeEmptyLinesAtBorders];
    }
    return [@(contact.profileInfo.uri.c_str()) removeEmptyLinesAtBorders];
}

static inline NSString* bestNameForContact(const lrc::api::contact::Info& contact)
{
    if (contact.profileInfo.alias.empty()) {
        return bestIDForContact(contact);
    }
    return @(contact.profileInfo.alias.c_str());
}

84 85
static inline NSString* bestNameForConversation(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model)
{
86 87 88 89 90 91 92 93 94 95 96 97 98 99
    try {
        auto contact = model.owner.contactModel->getContact(conv.participants[0]);
        if (contact.profileInfo.alias.empty()) {
            return bestIDForConversation(conv, model);
        }
        auto alias = contact.profileInfo.alias;
        alias.erase(std::remove(alias.begin(), alias.end(), '\n'), alias.end());
        alias.erase(std::remove(alias.begin(), alias.end(), '\r'), alias.end());
        if(alias.length() == 0) {
            return bestIDForConversation(conv, model);
        }
        return @(alias.c_str());
    } catch (std::out_of_range& e) {
        NSLog(@"bestNameForConversation: getContact - out of range");
100
        return @"";
Andreas Traczyk's avatar
Andreas Traczyk committed
101 102 103 104 105 106 107 108 109 110 111 112
    }
}

static inline lrc::api::profile::Type profileType(const lrc::api::conversation::Info& conv, const lrc::api::ConversationModel& model)
{
    @try {
        auto contact = model.owner.contactModel->getContact(conv.participants[0]);
        return contact.profileInfo.type;
    }
    @catch (NSException *exception) {
        lrc::api::profile::Type::INVALID;
    }
113
}
114 115 116 117 118 119 120 121 122 123 124 125 126 127

/**
 * This function return an iterator pointing to a Conversation::Info in ConversationModel given its uid. If not found
 * the iterator is invalid thus it needs to be checked by caller.
 * @param uid UID of conversation being searched
 * @param model ConversationModel in which to do the lookup
 * @return iterator pointing to corresponding Conversation if any. Points to past-the-end element otherwise.
 */
static inline lrc::api::ConversationModel::ConversationQueue::const_iterator getConversationFromUid(const std::string& uid, const lrc::api::ConversationModel& model) {
    return std::find_if(model.allFilteredConversations().begin(), model.allFilteredConversations().end(),
                        [&] (const lrc::api::conversation::Info& conv) {
                            return uid == conv.uid;
                        });
}
Andreas Traczyk's avatar
Andreas Traczyk committed
128

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
129 130 131 132 133 134 135 136 137 138 139 140 141
/**
 * This function return an iterator pointing to a Conversation::Info in ConversationModel given its participant uri. Will not work for group chat.
 * @param uri URI of participant
 * @param model ConversationModel in which to do the lookup
 * @return iterator pointing to corresponding Conversation if any. Points to past-the-end element otherwise.
 */
static inline lrc::api::ConversationModel::ConversationQueue::const_iterator getConversationFromURI(const std::string& uri, const lrc::api::ConversationModel& model) {
    return std::find_if(model.allFilteredConversations().begin(), model.allFilteredConversations().end(),
                        [&] (const lrc::api::conversation::Info& conv) {
                            return uri == conv.participants[0];
                        });
}

142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
static inline bool isUrlAccessibleFromSandbox(NSURL* url)
{
    NSFileManager* fileManager = [[NSFileManager alloc] init];
    NSArray* urlPathsMusic = [fileManager URLsForDirectory:NSMusicDirectory
                                                 inDomains:NSUserDomainMask];
    NSArray* urlPathsPictures = [fileManager URLsForDirectory:NSPicturesDirectory
                                                    inDomains:NSUserDomainMask];
    NSArray* urlPathsDownloads = [fileManager URLsForDirectory:NSDownloadsDirectory
                                                     inDomains:NSUserDomainMask];
    NSArray* urlPathsMovies = [fileManager URLsForDirectory:NSMoviesDirectory
                                                  inDomains:NSUserDomainMask];
    NSArray* availablePaths = [[[urlPathsMusic arrayByAddingObjectsFromArray: urlPathsPictures] arrayByAddingObjectsFromArray: urlPathsDownloads] arrayByAddingObjectsFromArray: urlPathsMovies];
    if([availablePaths containsObject:url]) {
        return YES;
    }
    for (NSURL* availableUrl in availablePaths) {
        if ([url.path containsString:availableUrl.path]) {
            return YES;
        }
    }
    return NO;
}

static inline bool appSandboxed()
{
    NSString* bundleID = [[NSBundle mainBundle] bundleIdentifier];
168 169 170
    NSString* url = [[NSURL fileURLWithPath:NSHomeDirectory()] path];
    NSString* appPath = [@"Library/Containers/" stringByAppendingString:bundleID];
    if ([url containsString: appPath]) {
171 172 173 174
        return YES;
    }
    return NO;
}
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190

static inline NSColor* colorForAccountStatus(const lrc::api::account::Status status)
{
    NSColor *accountStatusColor = [NSColor unregisteredColor];
    switch (status) {
        case  lrc::api::account::Status::REGISTERED:
            accountStatusColor = [NSColor presenceColor];
            break;
        case  lrc::api::account::Status::TRYING:
            accountStatusColor = [NSColor orangeColor];
            break;
        default:
            break;
    }
    return accountStatusColor;
}