RingWindowController.mm 24 KB
Newer Older
1
/*
2
 *  Copyright (C) 2015-2016 Savoir-faire Linux Inc.
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 *  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.
 */
19
#import "RingWindowController.h"
20
#import <QuartzCore/QuartzCore.h>
21
#include <qrencode.h>
22

23
//LRC
24
#import <api/lrc.h>
25
#import <api/account.h>
26 27 28 29
#import <api/newaccountmodel.h>
#import <api/newcallmodel.h>
#import <api/behaviorcontroller.h>
#import <api/conversation.h>
30 31
#import <api/contactmodel.h>
#import <api/contact.h>
32
#import <api/datatransfermodel.h>
33
#import <api/avmodel.h>
34

35
// Ring
36
#import "AppDelegate.h"
37
#import "Constants.h"
Alexandre Lision's avatar
Alexandre Lision committed
38
#import "CurrentCallVC.h"
39
#import "MigrateRingAccountsWC.h"
40
#import "ConversationVC.h"
41
#import "PreferencesWC.h"
42
#import "SmartViewVC.h"
43
#import "views/IconButton.h"
44
#import "views/NSColor+RingTheme.h"
45
#import "views/HoverButton.h"
46
#import "utils.h"
47
#import "RingWizardWC.h"
48
#import "AccountSettingsVC.h"
49 50 51 52 53 54

typedef NS_ENUM(NSInteger, ViewState) {
    SHOW_WELCOME_SCREEN = 0,
    SHOW_CONVERSATION_SCREEN,
    SHOW_CALL_SCREEN,
    SHOW_SETTINGS_SCREEN,
55
    LEAVE_MESSAGE,
56
};
57

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
58
@interface RingWindowController () <MigrateRingAccountsDelegate>
59 60

@property (retain) MigrateRingAccountsWC* migrateWC;
61
@property RingWizardWC* wizard;
62 63 64

@end

65
@implementation RingWindowController {
66

67 68
    __unsafe_unretained IBOutlet NSLayoutConstraint* centerYQRCodeConstraint;
    __unsafe_unretained IBOutlet NSLayoutConstraint* centerYWelcomeContainerConstraint;
69
    IBOutlet NSLayoutConstraint* ringLabelTrailingConstraint;
70
    __unsafe_unretained IBOutlet NSView* welcomeContainer;
71 72
    __unsafe_unretained IBOutlet NSView* callView;
    __unsafe_unretained IBOutlet NSTextField* ringIDLabel;
73
    __unsafe_unretained IBOutlet NSTextField* explanationLabel;
74
    __unsafe_unretained IBOutlet NSButton* shareButton;
75
    __unsafe_unretained IBOutlet NSImageView* qrcodeView;
Alexandre Lision's avatar
Alexandre Lision committed
76

77
    PreferencesWC* preferencesWC;
78
    IBOutlet SmartViewVC* smartViewVC;
79

80
    CurrentCallVC* currentCallVC;
81
    ConversationVC* conversationVC;
82
    AccountSettingsVC* settingsVC;
83

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
84
    IBOutlet ChooseAccountVC* chooseAccountVC;
85
}
Alexandre Lision's avatar
Alexandre Lision committed
86

87
@synthesize dataTransferModel, accountModel, behaviorController, avModel;
88
@synthesize wizard;
89

90
-(id) initWithWindowNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountModel:( lrc::api::NewAccountModel*)accountModel dataTransferModel:( lrc::api::DataTransferModel*)dataTransferModel behaviourController:( lrc::api::BehaviorController*) behaviorController avModel: (lrc::api::AVModel*)avModel
91 92 93 94 95 96
{
    if (self =  [self initWithWindowNibName:nibNameOrNil])
    {
        self.accountModel = accountModel;
        self.dataTransferModel = dataTransferModel;
        self.behaviorController = behaviorController;
97
        self.avModel = avModel;
98
        self.avModel->useAVFrame(YES);
99 100 101 102
    }
    return self;
}

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
103 104 105 106 107 108 109
- (NSApplicationPresentationOptions)window:(NSWindow *)window willUseFullScreenPresentationOptions:(NSApplicationPresentationOptions)proposedOptions
{
    return (NSApplicationPresentationFullScreen |
            NSApplicationPresentationAutoHideMenuBar |
            NSApplicationPresentationAutoHideToolbar);
}

110 111 112 113 114 115
-(void)changeViewTo:(ViewState) state  {
    switch (state) {
        case SHOW_WELCOME_SCREEN:
            [self accountSettingsShouldOpen: NO];
            [conversationVC hideWithAnimation:false];
            [currentCallVC hideWithAnimation:false];
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
116
            [currentCallVC cleanUp];
117 118 119 120 121 122 123 124 125
            [currentCallVC.view removeFromSuperview];
            [welcomeContainer setHidden: NO];
            [smartViewVC.view setHidden: NO];
            [settingsVC hide];
            break;
        case SHOW_CONVERSATION_SCREEN:
            [self accountSettingsShouldOpen: NO];
            [conversationVC showWithAnimation:false];
            [currentCallVC hideWithAnimation:false];
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
126
            [currentCallVC cleanUp];
127 128 129 130 131 132
            [currentCallVC.view removeFromSuperview];
            [welcomeContainer setHidden: YES];
            [smartViewVC.view setHidden: NO];
            [settingsVC hide];
            break;
        case SHOW_CALL_SCREEN:
133
            self.avModel->useAVFrame(YES);
134 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
            [self accountSettingsShouldOpen: NO];
            if (![currentCallVC.view superview]) {
            [callView addSubview:[currentCallVC view] positioned:NSWindowAbove relativeTo:nil];
            [currentCallVC initFrame];
            [currentCallVC showWithAnimation:false];
            [conversationVC hideWithAnimation:false];
            [welcomeContainer setHidden: YES];
            [smartViewVC.view setHidden: NO];
            [settingsVC hide];
            }
            [currentCallVC showWithAnimation:false];
            break;
        case SHOW_SETTINGS_SCREEN:
            @try {
                [self accountSettingsShouldOpen: YES];
            }
            @catch (NSException *ex) {
                return;
            }
            [welcomeContainer setHidden: YES];
            [currentCallVC hideWithAnimation:false];
            [currentCallVC.view removeFromSuperview];
            [conversationVC hideWithAnimation:false];
            [smartViewVC.view setHidden: YES];
            [settingsVC show];
            break;
160 161 162 163
        case LEAVE_MESSAGE:
            [conversationVC showWithAnimation: false];
            [currentCallVC hideWithAnimation: false];
            [conversationVC presentLeaveMessageView];
164 165 166 167 168
        default:
            break;
    }
}

169 170
- (void)windowDidLoad {
    [super windowDidLoad];
171
    [self.window setMovableByWindowBackground:YES];
Alexandre Lision's avatar
Alexandre Lision committed
172

173
    self.window.titleVisibility = NSWindowTitleHidden;
174

175
    currentCallVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil];
176
    currentCallVC.delegate = self;
177
    conversationVC = [[ConversationVC alloc] initWithNibName:@"Conversation" bundle:nil delegate:self aVModel:self.avModel];
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
178
    [chooseAccountVC updateWithDelegate: self andModel:self.accountModel];
179
    settingsVC = [[AccountSettingsVC alloc] initWithNibName:@"AccountSettings" bundle:nil accountmodel:self.accountModel];
Alexandre Lision's avatar
Alexandre Lision committed
180
    [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
181
    [[currentCallVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
182
    [[conversationVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
183
    [[settingsVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
Alexandre Lision's avatar
Alexandre Lision committed
184

185
    [callView addSubview:[conversationVC view] positioned:NSWindowAbove relativeTo:nil];
186
    [self.window.contentView addSubview:[settingsVC view] positioned:NSWindowAbove relativeTo:nil];
Alexandre Lision's avatar
Alexandre Lision committed
187

188
    [conversationVC initFrame];
189
    [settingsVC initFrame];
190

191
    [self checkAccountsToMigrate];
192

193
    // set download folder (default - 'Documents')
194 195
    NSString* path = [[NSUserDefaults standardUserDefaults] stringForKey:Preferences::DownloadFolder];
    if (!path || path.length == 0) {
196 197
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        path = [[paths objectAtIndex:0] stringByAppendingString:@"/"];
198
    }
199
    self.dataTransferModel->downloadDirectory = std::string([path UTF8String]);
200 201
    if(appSandboxed()) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
202
        avModel->setRecordPath([[paths objectAtIndex:0] UTF8String]);
203 204
    } else if (avModel->getRecordPath().empty()) {
        avModel->setRecordPath([NSHomeDirectory() UTF8String]);
205
    }
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
206 207
    NSToolbar *tb = [[self window] toolbar];
    [tb setAllowsUserCustomization:NO];
208 209 210 211 212

    //add messages view controller to responders chain
    NSResponder * viewNextResponder = [self nextResponder];
    [self setNextResponder: [conversationVC getMessagesView]];
    [[conversationVC getMessagesView] setNextResponder: viewNextResponder];
213 214 215 216
}

- (void) connect
{
217
    QObject::connect(self.behaviorController,
218 219 220
                     &lrc::api::BehaviorController::showCallView,
                     [self](const std::string accountId,
                            const lrc::api::conversation::Info convInfo){
221
                         auto* accInfo = &self.accountModel->getAccountInfo(accountId);
222 223 224 225 226
                         if (accInfo->contactModel->getContact(convInfo.participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING)
                             [smartViewVC selectPendingList];
                         else
                             [smartViewVC selectConversationList];

227 228
                         [currentCallVC setCurrentCall:convInfo.callId
                                          conversation:convInfo.uid
229 230
                                               account:accInfo
                                               avModel: avModel];
231 232
                         [self changeViewTo:SHOW_CALL_SCREEN];

233
                     });
234

235
    QObject::connect(self.behaviorController,
236 237 238
                     &lrc::api::BehaviorController::showIncomingCallView,
                     [self](const std::string accountId,
                            const lrc::api::conversation::Info convInfo){
239
                         auto* accInfo = &self.accountModel->getAccountInfo(accountId);
240 241 242 243 244
                         if (accInfo->contactModel->getContact(convInfo.participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING)
                             [smartViewVC selectPendingList];
                         else
                             [smartViewVC selectConversationList];

245 246
                         [currentCallVC setCurrentCall:convInfo.callId
                                          conversation:convInfo.uid
247 248
                                               account:accInfo
                                               avModel: avModel];
249
                         [smartViewVC selectConversation: convInfo model:accInfo->conversationModel.get()];
250
                         [self changeViewTo:SHOW_CALL_SCREEN];
251
                     });
252

253
    QObject::connect(self.behaviorController,
254 255 256
                     &lrc::api::BehaviorController::showChatView,
                     [self](const std::string& accountId,
                            const lrc::api::conversation::Info& convInfo){
257
                         auto& accInfo = self.accountModel->getAccountInfo(accountId);
258
                         [conversationVC setConversationUid:convInfo.uid model:accInfo.conversationModel.get()];
259
                         [smartViewVC selectConversation: convInfo model:accInfo.conversationModel.get()];
260
                         [self changeViewTo:SHOW_CONVERSATION_SCREEN];
261
                     });
262 263 264 265 266 267 268 269 270
    QObject::connect(self.behaviorController,
                     &lrc::api::BehaviorController::showLeaveMessageView,
                     [self](const std::string& accountId,
                            const lrc::api::conversation::Info& convInfo){
                         auto& accInfo = self.accountModel->getAccountInfo(accountId);
                         [conversationVC setConversationUid:convInfo.uid model:accInfo.conversationModel.get()];
                         [smartViewVC selectConversation: convInfo model:accInfo.conversationModel.get()];
                         [self changeViewTo:LEAVE_MESSAGE];
                     });
271 272
}

273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
#pragma mark - Ring account migration

- (void) migrateRingAccount:(std::string) acc
{
    self.migrateWC = [[MigrateRingAccountsWC alloc] initWithDelegate:self actionCode:1];
    self.migrateWC.accountModel = self.accountModel;
    self.migrateWC.accountToMigrate = acc;
    [self.window beginSheet:self.migrateWC.window completionHandler:nil];
}

- (void)checkAccountsToMigrate
{
    auto accounts = self.accountModel->getAccountList();
    for (auto accountId: accounts) {
        const lrc::api::account::Info& accountInfo = self.accountModel->getAccountInfo(accountId);
        if (accountInfo.status == lrc::api::account::Status::ERROR_NEED_MIGRATION) {
            [self migrateRingAccount:accountInfo.id];
            return;
        }
    }
    @try {
        [smartViewVC setConversationModel: [chooseAccountVC selectedAccount].conversationModel.get()];
    }
    @catch (NSException *ex) {
        NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
    }
    [shareButton sendActionOn:NSLeftMouseDownMask];
    [self connect];
    [self updateRingID];
}

- (void)migrationDidComplete
{
    [self checkAccountsToMigrate];
}

- (void)migrationDidCompleteWithError
{
    [self checkAccountsToMigrate];
}

314 315 316 317 318 319
/**
 * Implement the necessary logic to choose which Ring ID to display.
 * This tries to choose the "best" ID to show
 */
- (void) updateRingID
{
320 321 322 323 324 325
    @try {
        auto& account = [chooseAccountVC selectedAccount];

        [ringIDLabel setStringValue:@""];

        if(account.profileInfo.type != lrc::api::profile::Type::RING) {
326 327
            self.notRingAccount = YES;
            self.isSIPAccount = YES;
328 329
            return;
        }
330 331 332
        self.isSIPAccount = NO;
        self.notRingAccount = NO;
        [ringLabelTrailingConstraint setActive:YES];
333 334 335 336 337
        auto& registeredName = account.registeredName;
        auto& ringID = account.profileInfo.uri;
        NSString* uriToDisplay = nullptr;
        if (!registeredName.empty()) {
            uriToDisplay = @(registeredName.c_str());
338
            [explanationLabel setStringValue: NSLocalizedString(@"This is your Jami username. \nCopy and share it with your friends!", @"Explanation label when user have Jami username")];
339 340
        } else {
            uriToDisplay = @(ringID.c_str());
341
            [explanationLabel setStringValue: NSLocalizedString(@"This is your ID. \nCopy and share it with your friends!", @"Explanation label when user have just ID")];
342 343 344
        }
        [ringIDLabel setStringValue:uriToDisplay];
        [self drawQRCode:@(ringID.c_str())];
345
    }
346 347
    @catch (NSException *ex) {
        NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
348 349 350
        self.notRingAccount = YES;
        self.isSIPAccount = NO;
        [ringLabelTrailingConstraint setActive:NO];
351
        [ringIDLabel setStringValue:NSLocalizedString(@"No account available", @"Displayed as RingID when no accounts are available for selection")];
352
    }
353 354
}

355 356
- (IBAction)shareRingID:(id)sender {
    NSSharingServicePicker* sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:[NSArray arrayWithObject:[ringIDLabel stringValue]]];
357
    [sharingServicePicker setDelegate:self];
358 359 360 361 362
    [sharingServicePicker showRelativeToRect:[sender bounds]
                                      ofView:sender
                               preferredEdge:NSMinYEdge];
}

363 364
- (IBAction)toggleQRCode:(id)sender {
    // Toggle pressed state of QRCode button
365 366 367
  //  [sender setPressed:![sender isPressed]];
    bool show = qrcodeView.animator.alphaValue == 0.0f ? YES: NO;
    [self showQRCode: show];
368 369 370 371 372
}

/**
 * Draw the QRCode in the qrCodeView
 */
373
- (void)drawQRCode:(NSString*) uriToDraw
374
{
375
    auto qrCode = QRcode_encodeString(uriToDraw.UTF8String,
376 377 378 379 380 381 382 383
                                      0,
                                      QR_ECLEVEL_L, // Lowest level of error correction
                                      QR_MODE_8, // 8-bit data mode
                                      1);
    if (!qrCode) {
        return;
    }

384 385 386 387 388 389
    unsigned char *data = 0;
    int width;
    data = qrCode->data;
    width = qrCode->width;
    int qr_margin = 3;

390 391 392 393 394 395 396 397 398 399
    CGFloat size = qrcodeView.frame.size.width;

    // create context
    CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
    CGContextRef ctx = CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace, kCGImageAlphaPremultipliedLast);

    CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -size);
    CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1);
    CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform));

400
    float zoom = ceil((double)size / (qrCode->width + 2.0 * qr_margin));
401 402 403 404 405 406
    CGRect rectDraw = CGRectMake(0, 0, zoom, zoom);

    int ran;
    for(int i = 0; i < width; ++i) {
        for(int j = 0; j < width; ++j) {
            if(*data & 1) {
407
                CGContextSetFillColorWithColor(ctx, [NSColor labelColor].CGColor);
408 409 410 411 412 413 414 415 416 417 418 419
                rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom);
                CGContextAddRect(ctx, rectDraw);
                CGContextFillPath(ctx);
            } else {
                CGContextSetFillColorWithColor(ctx, [NSColor windowBackgroundColor].CGColor);
                rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom);
                CGContextAddRect(ctx, rectDraw);
                CGContextFillPath(ctx);
            }
            ++data;
        }
    }
420 421 422 423 424 425 426 427 428 429 430 431

    // get image
    auto qrCGImage = CGBitmapContextCreateImage(ctx);
    auto qrImage = [[NSImage alloc] initWithCGImage:qrCGImage size:qrcodeView.frame.size];

    // some releases
    CGContextRelease(ctx);
    CGImageRelease(qrCGImage);
    CGColorSpaceRelease(colorSpace);
    QRcode_free(qrCode);

    [qrcodeView setImage:qrImage];
432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
}

/**
 * Start the in/out animation displaying the QRCode
 * @param show should the QRCode be animated in or out
 */
- (void) showQRCode:(BOOL) show
{
    static const NSInteger offset = 110;
    [NSAnimationContext beginGrouping];
    NSAnimationContext.currentContext.duration = 0.5;
    [[NSAnimationContext currentContext] setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
    qrcodeView.animator.alphaValue = show ? 1.0 : 0.0;
    [centerYQRCodeConstraint.animator setConstant: show ? offset : 0];
    [centerYWelcomeContainerConstraint.animator setConstant:show ? -offset : 0];
    [NSAnimationContext endGrouping];
}

450 451
- (IBAction)openPreferences:(id)sender
{
452 453 454 455
    if (preferencesWC) {
        [preferencesWC.window orderFront:preferencesWC.window];
        return;
    }
456

457
    preferencesWC = [[PreferencesWC alloc] initWithWindowNibName: @"PreferencesWindow" bundle: nil accountModel:self.accountModel dataTransferModel:self.dataTransferModel behaviourController:self.behaviorController avModel: self.avModel];
458
    [preferencesWC.window makeKeyAndOrderFront:preferencesWC.window];
459
}
460

461 462 463 464 465 466 467 468 469 470 471 472
- (IBAction)callClickedAtRow:(id)sender
{
    NSTabViewItem *selectedTab = [smartViewVC.tabbar selectedTabViewItem];
    int index = [smartViewVC.tabbar indexOfTabViewItem:selectedTab];
    switch (index) {
        case 0:
            [smartViewVC startCallForRow:sender];
            break;
        default:
            break;
    }
}
473

474
- (void) selectAccount:(const lrc::api::account::Info&)accInfo currentRemoved:(BOOL) removed
475 476
{
    // If the selected account has been changed, we close any open panel
477
    [smartViewVC setConversationModel:accInfo.conversationModel.get()];
478 479
    // Welcome view informations are also updated
    [self updateRingID];
480
    [settingsVC setSelectedAccount:accInfo.id];
481
    [self changeViewTo: ([settingsVC.view isHidden] || removed)  ?  SHOW_WELCOME_SCREEN : SHOW_SETTINGS_SCREEN];
482 483
}

484 485 486
-(void)allAccountsDeleted
{
    [smartViewVC clearConversationModel];
487
    [self changeViewTo:SHOW_WELCOME_SCREEN];
488
    [self updateRingID];
489 490 491 492 493 494
    qrcodeView.animator.alphaValue = 0.0;
    [centerYQRCodeConstraint.animator setConstant: 0];
    [centerYWelcomeContainerConstraint.animator setConstant: 0];
    [self close];
    AppDelegate* delegate = (AppDelegate*)[[NSApplication sharedApplication] delegate];
    [delegate showWizard];
495 496
}

497 498 499
-(void)rightPanelClosed
{
    [smartViewVC deselect];
500
    [welcomeContainer setHidden:NO];
501 502
}

503 504 505 506 507
-(void)currentConversationTrusted
{
    [smartViewVC selectConversationList];
}

508
-(void) listTypeChanged {
509
    [self changeViewTo:SHOW_WELCOME_SCREEN];
510 511
}

512 513
- (IBAction)openAccountSettings:(NSButton *)sender
{
514
    [self changeViewTo: [settingsVC.view isHidden] ?  SHOW_SETTINGS_SCREEN : SHOW_WELCOME_SCREEN];
515 516
}

517
- (void) createNewAccount {
518
    [self changeViewTo:SHOW_WELCOME_SCREEN];
519 520 521
    wizard = [[RingWizardWC alloc] initWithNibName:@"RingWizard" bundle: nil accountmodel: self.accountModel];
    [wizard showChooseWithCancelButton: YES andAdvanced: YES];
    [self.window beginSheet:wizard.window completionHandler:nil];
522 523
}

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
524 525 526 527 528 529 530 531
- (NSRect)window:(NSWindow *)window willPositionSheet:(NSWindow *)sheet
       usingRect:(NSRect)rect
{
    float titleBarHeight = self.window.frame.size.height - [NSWindow contentRectForFrameRect:self.window.frame styleMask:self.window.styleMask].size.height;
    rect.origin.y = self.window.frame.size.height;
    return rect;
}

532 533
-(void) accountSettingsShouldOpen: (BOOL) open {
    if (open) {
534
        [settingsVC setSelectedAccount: [chooseAccountVC selectedAccount].id];
535
    }
536 537
}

538 539 540 541 542 543
#pragma mark - CallViewControllerDelegate

-(void) conversationInfoUpdatedFor:(const std::string&) conversationID {
    [smartViewVC reloadConversationWithUid:@(conversationID.c_str())];
}

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577
-(void) showConversation:(NSString* )conversationId forAccount:(NSString*)accountId {
    auto& accInfo = self.accountModel->getAccountInfo([accountId UTF8String]);
    [chooseAccountVC selectAccount: accountId];
    [settingsVC setSelectedAccount: [accountId UTF8String]];
    [smartViewVC setConversationModel:accInfo.conversationModel.get()];
    [smartViewVC selectConversationList];
    [self updateRingID];
    auto convInfo = getConversationFromUid([conversationId UTF8String], *accInfo.conversationModel.get());
    auto convQueue = accInfo.conversationModel.get()->allFilteredConversations();
    if (convInfo != convQueue.end()) {
        [conversationVC setConversationUid:convInfo->uid model:accInfo.conversationModel.get()];
        [smartViewVC selectConversation: *convInfo model:accInfo.conversationModel.get()];
        accInfo.conversationModel.get()->clearUnreadInteractions([conversationId UTF8String]);
    }
    [self changeViewTo:SHOW_CONVERSATION_SCREEN];
}

-(void) showCall:(NSString* )callId forAccount:(NSString*)accountId forConversation:(NSString*)conversationId {
    auto& accInfo = self.accountModel->getAccountInfo([accountId UTF8String]);
    [chooseAccountVC selectAccount: accountId];
    [settingsVC setSelectedAccount:accInfo.id];
    [smartViewVC setConversationModel:accInfo.conversationModel.get()];
    [self updateRingID];
    auto convInfo = getConversationFromUid([conversationId UTF8String], *accInfo.conversationModel.get());
    auto convQueue = accInfo.conversationModel.get()->allFilteredConversations();
    if (convInfo != convQueue.end()) {
        if (accInfo.contactModel->getContact(convInfo->participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING)
            [smartViewVC selectPendingList];
        else
            [smartViewVC selectConversationList];
        [smartViewVC selectConversation: *convInfo model:accInfo.conversationModel.get()];
    }
    [currentCallVC setCurrentCall:[callId UTF8String]
                     conversation:[conversationId UTF8String]
578 579
                          account:&accInfo
                          avModel:avModel];
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597
    [self changeViewTo:SHOW_CALL_SCREEN];
}

-(void) showContactRequestFor:(NSString* )accountId contactUri:(NSString*)uri {
    auto& accInfo = self.accountModel->getAccountInfo([accountId UTF8String]);
    [chooseAccountVC selectAccount: accountId];
    [settingsVC setSelectedAccount:accInfo.id];
    [smartViewVC setConversationModel:accInfo.conversationModel.get()];
    [self updateRingID];
    [smartViewVC selectPendingList];
    auto convInfo = getConversationFromURI([uri UTF8String], *accInfo.conversationModel.get());
    auto convQueue = accInfo.conversationModel.get()->allFilteredConversations();
    if (convInfo != convQueue.end()) {
        [conversationVC setConversationUid:convInfo->uid model:accInfo.conversationModel.get()];
        [smartViewVC selectConversation: *convInfo model:accInfo.conversationModel.get()];
    }
    [self changeViewTo:SHOW_CONVERSATION_SCREEN];
}
598 599 600 601 602

- (BOOL)windowShouldClose:(id)sender {
    [NSApp hide:nil];
    return NO;
}
603
@end