ConversationVC.mm 10.2 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
/*
 *  Copyright (C) 2016 Savoir-faire Linux Inc.
 *  Author: Alexandre Lision <alexandre.lision@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.
 */

#import "ConversationVC.h"

#import <QItemSelectionModel>
#import <qstring.h>
#import <QPixmap>
#import <QtMacExtras/qmacfunctions.h>

#import <media/media.h>
#import <recentmodel.h>
#import <person.h>
#import <contactmethod.h>
#import <media/text.h>
#import <media/textrecording.h>
#import <callmodel.h>
#import <globalinstances.h>

#import "views/IconButton.h"
#import "views/IMTableCellView.h"
#import "views/NSColor+RingTheme.h"
#import "QNSTreeController.h"
40
#import "INDSequentialTextSelectionManager.h"
41
#import "delegates/ImageManipulationDelegate.h"
42 43 44
#import "PhoneDirectoryModel.h"
#import "account.h"
#import "AvailableAccountModel.h"
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
45
#import "MessagesVC.h"
46

47 48 49

#import <QuartzCore/QuartzCore.h>

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
50
@interface ConversationVC () <NSOutlineViewDelegate, MessagesVCDelegate> {
51 52 53 54 55

    __unsafe_unretained IBOutlet NSTextField* messageField;
    QVector<ContactMethod*> contactMethods;
    NSMutableString* textSelection;

56 57
    QMetaObject::Connection contactMethodChanged;
    ContactMethod* selectedContactMethod;
58 59 60 61 62 63

    __unsafe_unretained IBOutlet NSView* sendPanel;
    __unsafe_unretained IBOutlet NSTextField* conversationTitle;
    __unsafe_unretained IBOutlet NSTextField* emptyConversationPlaceHolder;
    __unsafe_unretained IBOutlet IconButton* sendButton;
    __unsafe_unretained IBOutlet NSPopUpButton* contactMethodsPopupButton;
64 65
    __unsafe_unretained IBOutlet NSLayoutConstraint* sentContactRequestWidth;
    __unsafe_unretained IBOutlet NSButton* sentContactRequestButton;
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
66
    IBOutlet MessagesVC* messagesViewVC;
67 68 69

    IBOutlet NSLayoutConstraint* titleHoverButtonConstraint;
    IBOutlet NSLayoutConstraint* titleTopConstraint;
70 71
}

72

73 74 75 76
@end

@implementation ConversationVC

77 78
- (void)loadView {
    [super loadView];
79 80 81 82 83 84
    // Do view setup here.
    [self.view setWantsLayer:YES];
    [self.view setLayer:[CALayer layer]];
    [self.view.layer setBackgroundColor:[NSColor ringGreyHighlight].CGColor];
    [self.view.layer setCornerRadius:5.0f];

85
    [messageField setFocusRingType:NSFocusRingTypeNone];
86 87 88 89 90

    [self setupChat];

}

91 92 93 94 95 96 97 98 99 100
-(Account* ) chosenAccount
{
    QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex();
    if(!index.isValid()) {
        return nullptr;
    }
    Account* account = index.data(static_cast<int>(Account::Role::Object)).value<Account*>();
    return account;
}

101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120
- (void) initFrame
{
    [self.view setFrame:self.view.superview.bounds];
    [self.view setHidden:YES];
    self.view.layer.position = self.view.frame.origin;
}

- (void) setupChat
{
    QObject::connect(RecentModel::instance().selectionModel(),
                     &QItemSelectionModel::currentChanged,
                     [=](const QModelIndex &current, const QModelIndex &previous) {

                         contactMethods = RecentModel::instance().getContactMethods(current);
                         if (contactMethods.isEmpty()) {
                             return ;
                         }

                         [contactMethodsPopupButton removeAllItems];
                         for (auto cm : contactMethods) {
121
                             [contactMethodsPopupButton addItemWithTitle:cm->bestId().toNSString()];
122 123
                         }

124
                         BOOL isSMultipleCM = (contactMethods.length() > 1);
125
                         BOOL hideCMPopupButton = !isSMultipleCM && (contactMethods.first()->bestId() == contactMethods.first()->bestName());
126 127 128

                         [contactMethodsPopupButton setEnabled:isSMultipleCM];
                         [contactMethodsPopupButton setBordered:isSMultipleCM];
129
                         [contactMethodsPopupButton setHidden:hideCMPopupButton];
130
                         [[contactMethodsPopupButton cell] setArrowPosition: !isSMultipleCM ? NSPopUpNoArrow : NSPopUpArrowAtBottom];
131

132 133 134
                         [titleHoverButtonConstraint setActive:hideCMPopupButton];
                         [titleTopConstraint setActive:!hideCMPopupButton];

135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163
                         [emptyConversationPlaceHolder setHidden:NO];
                         // Select first cm
                         [contactMethodsPopupButton selectItemAtIndex:0];
                         [self itemChanged:contactMethodsPopupButton];
                     });
}

- (IBAction)sendMessage:(id)sender
{
    /* make sure there is text to send */
    NSString* text = self.message;
    if (text && text.length > 0) {
        QMap<QString, QString> messages;
        messages["text/plain"] = QString::fromNSString(text);
        contactMethods.at([contactMethodsPopupButton indexOfSelectedItem])->sendOfflineTextMessage(messages);
        self.message = @"";
    }
}

- (IBAction)placeCall:(id)sender
{
    if(auto cm = contactMethods.at([contactMethodsPopupButton indexOfSelectedItem])) {
        auto c = CallModel::instance().dialingCall();
        c->setPeerContactMethod(cm);
        c << Call::Action::ACCEPT;
        CallModel::instance().selectCall(c);
    }
}

164 165
- (IBAction)backPressed:(id)sender {
    RecentModel::instance().selectionModel()->clearCurrentIndex();
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
166
    messagesViewVC.delegate = nil;
167 168
}

169
- (IBAction)sendContactRequest:(id)sender
170
{
171 172 173 174 175 176 177 178 179 180
    auto cm = contactMethods.at([contactMethodsPopupButton indexOfSelectedItem]);
    if(cm) {
        if(cm->account() == nullptr) {
            cm->setAccount([self chosenAccount]);
        }

        if(cm->account() == nullptr) {
            return;
        }
        cm->account()->sendContactRequest(cm);
181 182 183
    }
}

184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
# pragma mark private IN/OUT animations

-(void) animateIn
{
    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];
}

202
-(void) animateOut
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
{
    if(self.view.frame.origin.x < 0) {
        return;
    }

    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];
}

#pragma mark - NSTextFieldDelegate

- (BOOL)control:(NSControl *)control textView:(NSTextView *)fieldEditor doCommandBySelector:(SEL)commandSelector
{
    if (commandSelector == @selector(insertNewline:) && self.message.length > 0) {
        [self sendMessage:nil];
        return YES;
    }
    return NO;
}

236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261
-(BOOL)shouldHideSendRequestBtn {
    /*to send contact request we need to meet thre condition:
     1)contact method has RING protocol
     2)accound is used to send request is also RING
     3)contact  have not acceppt request yet*/
    if(selectedContactMethod->protocolHint() != URI::ProtocolHint::RING) {
        return YES;
    }
    if(selectedContactMethod->isConfirmed()) {
        return YES;
    }
    if(selectedContactMethod->account()) {
        return selectedContactMethod->account()->protocol() != Account::Protocol::RING;
    }
    if([self chosenAccount]) {
        return [self chosenAccount]->protocol() != Account::Protocol::RING;
    }
    return NO;
}

-(void)updateSendButtonVisibility
{
    [sentContactRequestButton setHidden:[self shouldHideSendRequestBtn]];
    sentContactRequestWidth.priority = [self shouldHideSendRequestBtn] ? 999: 250;
}

262 263 264 265 266
#pragma mark - NSPopUpButton item selection

- (IBAction)itemChanged:(id)sender {
    NSInteger index = [(NSPopUpButton *)sender indexOfSelectedItem];

267
    selectedContactMethod = contactMethods.at(index);
268

269
    [self updateSendButtonVisibility];
270

271
    [conversationTitle setStringValue:selectedContactMethod->bestName().toNSString()];
272 273 274 275
    QObject::disconnect(contactMethodChanged);
    contactMethodChanged = QObject::connect(selectedContactMethod,
                                            &ContactMethod::changed,
                                            [self] {
276
                                                [conversationTitle setStringValue:selectedContactMethod->bestName().toNSString()];
277
                                                [self updateSendButtonVisibility];
278 279 280
                                            });

    if (auto txtRecording = selectedContactMethod->textRecording()) {
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
281 282
        messagesViewVC.delegate = self;
        [messagesViewVC setUpViewWithModel:txtRecording->instantMessagingModel()];
283
        [self.view.window makeFirstResponder:messageField];
284 285 286
    }
}

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
287 288 289 290 291 292 293 294 295
#pragma mark - MessagesVC delegate

-(void) newMessageAdded {

    if (auto txtRecording = contactMethods.at([contactMethodsPopupButton indexOfSelectedItem])->textRecording()) {
        [emptyConversationPlaceHolder setHidden:txtRecording->instantMessagingModel()->rowCount() > 0];
        txtRecording->setAllRead();
    }
}
296 297

@end