ConversationVC.mm 9.74 KB
Newer Older
1
/*
Sébastien Blin's avatar
Sébastien Blin committed
2
 *  Copyright (C) 2016-2019 Savoir-faire Linux Inc.
3
 *  Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
4
 *  Author: Anthony Léonard <anthony.leonard@savoirfairelinux.com>
5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
 *
 *  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.
 */

#import "ConversationVC.h"

#import <qstring.h>
#import <QPixmap>
#import <QtMacExtras/qmacfunctions.h>
26
#import <QuartzCore/QuartzCore.h>
27

28
// LRC
29 30

#import "views/IconButton.h"
31
#import "views/HoverButton.h"
32 33 34
#import "views/IMTableCellView.h"
#import "views/NSColor+RingTheme.h"
#import "delegates/ImageManipulationDelegate.h"
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
35
#import "MessagesVC.h"
36
#import "utils.h"
37
#import "RingWindowController.h"
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
38
#import "NSString+Extensions.h"
39 40 41 42
#import "LeaveMessageVC.h"
#import <QuickLook/QuickLook.h>
#import <Quartz/Quartz.h>
#import "LeaveMessageVC.h"
43

44
@interface ConversationVC () <QLPreviewPanelDataSource, QLPreviewPanelDelegate>{
45 46

    __unsafe_unretained IBOutlet NSTextField* conversationTitle;
47
    __unsafe_unretained IBOutlet NSTextField *conversationID;
48
    __unsafe_unretained IBOutlet HoverButton *addContactButton;
49
    __unsafe_unretained IBOutlet NSLayoutConstraint* sentContactRequestWidth;
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
50

51
    __unsafe_unretained IBOutlet NSButton* sentContactRequestButton;
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
52
    IBOutlet MessagesVC* messagesViewVC;
53
    LeaveMessageVC* leaveMessageVC;
54

55
    IBOutlet NSLayoutConstraint *titleCenteredConstraint;
56
    IBOutlet NSLayoutConstraint* titleTopConstraint;
57 58 59 60 61

    std::string convUid_;
    const lrc::api::conversation::Info* cachedConv_;
    lrc::api::ConversationModel* convModel_;

62
    RingWindowController* delegate;
63
    NSMutableArray* leaveMessageConversations;
64

65
    // All those connections are needed to invalidate cached conversation as pointer
66
    // may not be referencing the same conversation anymore
67
    QMetaObject::Connection modelSortedConnection_, filterChangedConnection_, newConversationConnection_, conversationRemovedConnection_;
68 69 70 71
}

@end

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
72 73 74 75
NSInteger const MEESAGE_MARGIN = 21;
NSInteger const SEND_PANEL_DEFAULT_HEIGHT = 60;
NSInteger const SEND_PANEL_MAX_HEIGHT = 120;

76 77
@implementation ConversationVC

78
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil delegate:(RingWindowController*) mainWindow aVModel:(lrc::api::AVModel*) avModel
79 80 81 82
{
    if (self = [self initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
    {
        delegate = mainWindow;
83 84 85 86 87 88 89
        leaveMessageVC = [[LeaveMessageVC alloc] initWithNibName:@"LeaveMessageVC" bundle:nil];
        [[leaveMessageVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
        [self.view addSubview:[leaveMessageVC view] positioned:NSWindowAbove relativeTo:nil];
        [leaveMessageVC initFrame];
        [leaveMessageVC setAVModel: avModel];
        leaveMessageConversations = [[NSMutableArray alloc] init];
        leaveMessageVC.delegate = self;
90 91 92 93
    }
    return self;
}

94 95 96 97
-(NSViewController*) getMessagesView {
    return messagesViewVC;
}

Andreas Traczyk's avatar
Andreas Traczyk committed
98 99 100 101 102 103 104 105 106 107 108 109
-(void) clearData {
    cachedConv_ = nil;
    convUid_ = "";
    convModel_ = nil;

    [messagesViewVC clearData];
    QObject::disconnect(modelSortedConnection_);
    QObject::disconnect(filterChangedConnection_);
    QObject::disconnect(newConversationConnection_);
    QObject::disconnect(conversationRemovedConnection_);
}

110 111 112 113 114 115 116 117
-(const lrc::api::conversation::Info*) getCurrentConversation
{
    if (convModel_ == nil || convUid_.empty())
        return nil;

    if (cachedConv_ != nil)
        return cachedConv_;

118
    auto convQueue = convModel_->allFilteredConversations();
119

120
    auto it = getConversationFromUid(convUid_, *convModel_);
121 122 123 124 125 126 127 128 129 130

    if (it != convQueue.end())
        cachedConv_ = &(*it);

    return cachedConv_;
}

-(void) setConversationUid:(const std::string)convUid model:(lrc::api::ConversationModel *)model {
    if (convUid_ == convUid && convModel_ == model)
        return;
131
    [self clearData];
132 133 134 135 136 137 138 139
    cachedConv_ = nil;
    convUid_ = convUid;
    convModel_ = model;

    [messagesViewVC setConversationUid:convUid_ model:convModel_];

    if (convUid_.empty() || convModel_ == nil)
        return;
140 141 142 143 144
    if([leaveMessageConversations containsObject:@(convUid_.c_str())]) {
        [leaveMessageVC setConversationUID: convUid_ conversationModel: convModel_];
    } else {
        [leaveMessageVC hide];
    }
145 146 147

    // Signals tracking changes in conversation list, we need them as cached conversation can be invalid
    // after a reordering.
148 149 150 151 152
    QObject::disconnect(modelSortedConnection_);
    QObject::disconnect(filterChangedConnection_);
    QObject::disconnect(newConversationConnection_);
    QObject::disconnect(conversationRemovedConnection_);
    modelSortedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::modelSorted,
153 154 155
                                          [self](){
                                              cachedConv_ = nil;
                                          });
156
    filterChangedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::filterChanged,
157 158 159
                                            [self](){
                                                cachedConv_ = nil;
                                            });
160
    newConversationConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::newConversation,
161 162 163
                                                [self](){
                                                    cachedConv_ = nil;
                                                });
164
    conversationRemovedConnection_ = QObject::connect(convModel_, &lrc::api::ConversationModel::conversationRemoved,
165 166 167
                                                [self](){
                                                    cachedConv_ = nil;
                                                });
168 169 170 171 172 173 174 175

    auto* conv = [self getCurrentConversation];

    if (conv == nil)
        return;

    // Setup UI elements according to new conversation
    NSString* bestName = bestNameForConversation(*conv, *convModel_);
176
    NSString* bestId = bestIDForConversation(*conv, *convModel_);
177
    [conversationTitle setStringValue: bestName];
178
    [conversationID setStringValue: bestId];
179

180
    BOOL hideBestId = [bestNameForConversation(*conv, *convModel_) isEqualTo:bestIDForConversation(*conv, *convModel_)];
181

182 183 184
    [conversationID setHidden:hideBestId];
    [titleCenteredConstraint setActive:hideBestId];
    [titleTopConstraint setActive:!hideBestId];
185 186
    auto accountType = convModel_->owner.profileInfo.type;
    [addContactButton setHidden:((convModel_->owner.contactModel->getContact(conv->participants[0]).profileInfo.type != lrc::api::profile::Type::TEMPORARY) || accountType == lrc::api::profile::Type::SIP)];
187 188
}

189 190 191 192 193 194 195 196 197
- (void) initFrame
{
    [self.view setFrame:self.view.superview.bounds];
    [self.view setHidden:YES];
    self.view.layer.position = self.view.frame.origin;
}

- (IBAction)placeCall:(id)sender
{
198 199
    auto* conv = [self getCurrentConversation];
    convModel_->placeCall(conv->uid);
200 201
}

202 203 204 205 206 207 208 209 210 211 212 213
- (IBAction)placeAudioCall:(id)sender
{
    auto* conv = [self getCurrentConversation];
    convModel_->placeAudioOnlyCall(conv->uid);
}

- (IBAction)addContact:(id)sender
{
    auto* conv = [self getCurrentConversation];
    convModel_->makePermanent(conv->uid);
}

214
- (IBAction)backPressed:(id)sender {
215
    [delegate rightPanelClosed];
216
    [self hideWithAnimation:false];
217 218
}

219 220
# pragma mark private IN/OUT animations

221
-(void) showWithAnimation:(BOOL)animate
222
{
223 224 225 226 227
    if (!animate) {
        [self.view setHidden:NO];
        return;
    }

228 229 230 231 232 233 234 235 236 237 238 239 240 241
    CGRect frame = CGRectOffset(self.view.superview.bounds, -self.view.superview.bounds.size.width, 0);
    [self.view setHidden:NO];

    [CATransaction begin];
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    [animation setFromValue:[NSValue valueWithPoint:frame.origin]];
    [animation setToValue:[NSValue valueWithPoint:self.view.superview.bounds.origin]];
    [animation setDuration:0.2f];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseIn]];
    [self.view.layer addAnimation:animation forKey:animation.keyPath];

    [CATransaction commit];
}

242
-(void) hideWithAnimation:(BOOL)animate
243 244 245 246 247
{
    if(self.view.frame.origin.x < 0) {
        return;
    }

Andreas Traczyk's avatar
Andreas Traczyk committed
248 249
    [self clearData];

250 251 252 253 254
    if (!animate) {
        [self.view setHidden:YES];
        return;
    }

255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271
    CGRect frame = CGRectOffset(self.view.frame, -self.view.frame.size.width, 0);
    [CATransaction begin];
    CABasicAnimation *animation = [CABasicAnimation animationWithKeyPath:@"position"];
    [animation setFromValue:[NSValue valueWithPoint:self.view.frame.origin]];
    [animation setToValue:[NSValue valueWithPoint:frame.origin]];
    [animation setDuration:0.2f];
    [animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseOut]];

    [CATransaction setCompletionBlock:^{
        [self.view setHidden:YES];
    }];
    [self.view.layer addAnimation:animation forKey:animation.keyPath];

    [self.view.layer setPosition:frame.origin];
    [CATransaction commit];
}

272 273 274 275 276 277 278 279
- (void) presentLeaveMessageView {
    [leaveMessageVC setConversationUID: convUid_ conversationModel: convModel_];
    [leaveMessageConversations addObject:@(convUid_.c_str())];
}

-(void) messageCompleted {
    [leaveMessageConversations removeObject:@(convUid_.c_str())];
}
280 281

@end