RingWindowController.mm 24.1 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
#include <memory>
23 24 25 26

//Qt
#import <QItemSelectionModel>
#import <QItemSelection>
27

28
//LRC
29
#import <AvailableAccountModel.h>
30
#import <api/lrc.h>
31
#import <api/account.h>
32 33 34 35
#import <api/newaccountmodel.h>
#import <api/newcallmodel.h>
#import <api/behaviorcontroller.h>
#import <api/conversation.h>
36 37
#import <api/contactmodel.h>
#import <api/contact.h>
38
#import <api/datatransfermodel.h>
39
#import <media/recordingmodel.h>
40
#import <api/avmodel.h>
41

42
// Ring
43
#import "AppDelegate.h"
44
#import "Constants.h"
Alexandre Lision's avatar
Alexandre Lision committed
45
#import "CurrentCallVC.h"
46
#import "MigrateRingAccountsWC.h"
47
#import "ConversationVC.h"
48
#import "PreferencesWC.h"
49
#import "SmartViewVC.h"
50
#import "views/IconButton.h"
51
#import "views/NSColor+RingTheme.h"
52
#import "views/HoverButton.h"
53
#import "utils.h"
54
#import "RingWizardWC.h"
55
#import "AccountSettingsVC.h"
56 57 58 59 60 61 62
#import "views/CallLayer.h"

typedef NS_ENUM(NSInteger, ViewState) {
    SHOW_WELCOME_SCREEN = 0,
    SHOW_CONVERSATION_SCREEN,
    SHOW_CALL_SCREEN,
    SHOW_SETTINGS_SCREEN,
63
    LEAVE_MESSAGE,
64
};
65

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
66
@interface RingWindowController () <MigrateRingAccountsDelegate>
67 68

@property (retain) MigrateRingAccountsWC* migrateWC;
69
@property RingWizardWC* wizard;
70 71 72

@end

73
@implementation RingWindowController {
74

75 76
    __unsafe_unretained IBOutlet NSLayoutConstraint* centerYQRCodeConstraint;
    __unsafe_unretained IBOutlet NSLayoutConstraint* centerYWelcomeContainerConstraint;
77
    IBOutlet NSLayoutConstraint* ringLabelTrailingConstraint;
78
    __unsafe_unretained IBOutlet NSView* welcomeContainer;
79 80
    __unsafe_unretained IBOutlet NSView* callView;
    __unsafe_unretained IBOutlet NSTextField* ringIDLabel;
81
    __unsafe_unretained IBOutlet NSTextField* explanationLabel;
82
    __unsafe_unretained IBOutlet NSButton* shareButton;
83
    __unsafe_unretained IBOutlet NSImageView* qrcodeView;
Alexandre Lision's avatar
Alexandre Lision committed
84

85
    PreferencesWC* preferencesWC;
86
    IBOutlet SmartViewVC* smartViewVC;
87

88
    CurrentCallVC* currentCallVC;
89
    ConversationVC* conversationVC;
90
    AccountSettingsVC* settingsVC;
91

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
92
    IBOutlet ChooseAccountVC* chooseAccountVC;
93
}
Alexandre Lision's avatar
Alexandre Lision committed
94

95
@synthesize dataTransferModel, accountModel, behaviorController, avModel;
96
@synthesize wizard;
97

98
-(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
99 100 101 102 103 104
{
    if (self =  [self initWithWindowNibName:nibNameOrNil])
    {
        self.accountModel = accountModel;
        self.dataTransferModel = dataTransferModel;
        self.behaviorController = behaviorController;
105
        self.avModel = avModel;
106 107 108 109
    }
    return self;
}

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
110 111 112 113 114 115 116
- (NSApplicationPresentationOptions)window:(NSWindow *)window willUseFullScreenPresentationOptions:(NSApplicationPresentationOptions)proposedOptions
{
    return (NSApplicationPresentationFullScreen |
            NSApplicationPresentationAutoHideMenuBar |
            NSApplicationPresentationAutoHideToolbar);
}

117 118 119 120 121 122
-(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
123
            [currentCallVC cleanUp];
124 125 126 127 128 129 130 131 132
            [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
133
            [currentCallVC cleanUp];
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 160 161 162 163 164 165
            [currentCallVC.view removeFromSuperview];
            [welcomeContainer setHidden: YES];
            [smartViewVC.view setHidden: NO];
            [settingsVC hide];
            break;
        case SHOW_CALL_SCREEN:
            [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;
166 167 168 169
        case LEAVE_MESSAGE:
            [conversationVC showWithAnimation: false];
            [currentCallVC hideWithAnimation: false];
            [conversationVC presentLeaveMessageView];
170 171 172 173 174
        default:
            break;
    }
}

175 176
- (void)windowDidLoad {
    [super windowDidLoad];
177
    [self.window setMovableByWindowBackground:YES];
Alexandre Lision's avatar
Alexandre Lision committed
178

179
    self.window.titleVisibility = NSWindowTitleHidden;
180

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

191
    [callView addSubview:[conversationVC view] positioned:NSWindowAbove relativeTo:nil];
192
    [self.window.contentView addSubview:[settingsVC view] positioned:NSWindowAbove relativeTo:nil];
Alexandre Lision's avatar
Alexandre Lision committed
193

194
    [conversationVC initFrame];
195
    [settingsVC initFrame];
196 197 198 199 200 201
    @try {
        [smartViewVC setConversationModel: [chooseAccountVC selectedAccount].conversationModel.get()];
    }
    @catch (NSException *ex) {
        NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
    }
202

203 204 205
    // Fresh run, we need to make sure RingID appears
    [shareButton sendActionOn:NSLeftMouseDownMask];

206
    [self connect];
207
    [self updateRingID];
208
    // set download folder (default - 'Documents')
209 210
    NSString* path = [[NSUserDefaults standardUserDefaults] stringForKey:Preferences::DownloadFolder];
    if (!path || path.length == 0) {
211 212
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
        path = [[paths objectAtIndex:0] stringByAppendingString:@"/"];
213
    }
214
    self.dataTransferModel->downloadDirectory = std::string([path UTF8String]);
215 216
    if(appSandboxed()) {
        NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
217
        media::RecordingModel::instance().setRecordPath(QString::fromNSString([paths objectAtIndex:0]));
218
    }
Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
219 220
    NSToolbar *tb = [[self window] toolbar];
    [tb setAllowsUserCustomization:NO];
221 222 223 224 225

    //add messages view controller to responders chain
    NSResponder * viewNextResponder = [self nextResponder];
    [self setNextResponder: [conversationVC getMessagesView]];
    [[conversationVC getMessagesView] setNextResponder: viewNextResponder];
226
    self.avModel->useAVFrame(YES);
227 228 229 230
}

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

241 242 243
                         [currentCallVC setCurrentCall:convInfo.callId
                                          conversation:convInfo.uid
                                               account:accInfo];
244 245
                         [self changeViewTo:SHOW_CALL_SCREEN];

246
                     });
247

248
    QObject::connect(self.behaviorController,
249 250 251
                     &lrc::api::BehaviorController::showIncomingCallView,
                     [self](const std::string accountId,
                            const lrc::api::conversation::Info convInfo){
252
                         auto* accInfo = &self.accountModel->getAccountInfo(accountId);
253 254 255 256 257
                         if (accInfo->contactModel->getContact(convInfo.participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING)
                             [smartViewVC selectPendingList];
                         else
                             [smartViewVC selectConversationList];

258 259 260
                         [currentCallVC setCurrentCall:convInfo.callId
                                          conversation:convInfo.uid
                                               account:accInfo];
261
                         [smartViewVC selectConversation: convInfo model:accInfo->conversationModel.get()];
262
                         [self changeViewTo:SHOW_CALL_SCREEN];
263
                     });
264

265
    QObject::connect(self.behaviorController,
266 267 268
                     &lrc::api::BehaviorController::showChatView,
                     [self](const std::string& accountId,
                            const lrc::api::conversation::Info& convInfo){
269
                         auto& accInfo = self.accountModel->getAccountInfo(accountId);
270
                         [conversationVC setConversationUid:convInfo.uid model:accInfo.conversationModel.get()];
271
                         [smartViewVC selectConversation: convInfo model:accInfo.conversationModel.get()];
272
                         [self changeViewTo:SHOW_CONVERSATION_SCREEN];
273
                     });
274 275 276 277 278 279 280 281 282
    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];
                     });
283 284 285 286 287 288 289 290
}

/**
 * Implement the necessary logic to choose which Ring ID to display.
 * This tries to choose the "best" ID to show
 */
- (void) updateRingID
{
291 292 293 294 295 296
    @try {
        auto& account = [chooseAccountVC selectedAccount];

        [ringIDLabel setStringValue:@""];

        if(account.profileInfo.type != lrc::api::profile::Type::RING) {
297 298
            self.notRingAccount = YES;
            self.isSIPAccount = YES;
299 300
            return;
        }
301 302 303
        self.isSIPAccount = NO;
        self.notRingAccount = NO;
        [ringLabelTrailingConstraint setActive:YES];
304 305 306 307 308
        auto& registeredName = account.registeredName;
        auto& ringID = account.profileInfo.uri;
        NSString* uriToDisplay = nullptr;
        if (!registeredName.empty()) {
            uriToDisplay = @(registeredName.c_str());
309
            [explanationLabel setStringValue: NSLocalizedString(@"This is your Jami username. \nCopy and share it with your friends!", @"Explanation label when user have Jami username")];
310 311
        } else {
            uriToDisplay = @(ringID.c_str());
312
            [explanationLabel setStringValue: NSLocalizedString(@"This is your ID. \nCopy and share it with your friends!", @"Explanation label when user have just ID")];
313 314 315
        }
        [ringIDLabel setStringValue:uriToDisplay];
        [self drawQRCode:@(ringID.c_str())];
316
    }
317 318
    @catch (NSException *ex) {
        NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
319 320 321
        self.notRingAccount = YES;
        self.isSIPAccount = NO;
        [ringLabelTrailingConstraint setActive:NO];
322
        [ringIDLabel setStringValue:NSLocalizedString(@"No account available", @"Displayed as RingID when no accounts are available for selection")];
323
    }
324 325
}

326 327
- (IBAction)shareRingID:(id)sender {
    NSSharingServicePicker* sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:[NSArray arrayWithObject:[ringIDLabel stringValue]]];
328
    [sharingServicePicker setDelegate:self];
329 330 331 332 333
    [sharingServicePicker showRelativeToRect:[sender bounds]
                                      ofView:sender
                               preferredEdge:NSMinYEdge];
}

334 335
- (IBAction)toggleQRCode:(id)sender {
    // Toggle pressed state of QRCode button
336 337 338
  //  [sender setPressed:![sender isPressed]];
    bool show = qrcodeView.animator.alphaValue == 0.0f ? YES: NO;
    [self showQRCode: show];
339 340 341 342 343
}

/**
 * Draw the QRCode in the qrCodeView
 */
344
- (void)drawQRCode:(NSString*) uriToDraw
345
{
346
    auto qrCode = QRcode_encodeString(uriToDraw.UTF8String,
347 348 349 350 351 352 353 354
                                      0,
                                      QR_ECLEVEL_L, // Lowest level of error correction
                                      QR_MODE_8, // 8-bit data mode
                                      1);
    if (!qrCode) {
        return;
    }

355 356 357 358 359 360
    unsigned char *data = 0;
    int width;
    data = qrCode->data;
    width = qrCode->width;
    int qr_margin = 3;

361 362 363 364 365 366 367 368 369 370
    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));

371
    float zoom = ceil((double)size / (qrCode->width + 2.0 * qr_margin));
372 373 374 375 376 377
    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) {
378
                CGContextSetFillColorWithColor(ctx, [NSColor labelColor].CGColor);
379 380 381 382 383 384 385 386 387 388 389 390
                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;
        }
    }
391 392 393 394 395 396 397 398 399 400 401 402

    // 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];
403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420
}

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

421 422
- (IBAction)openPreferences:(id)sender
{
423 424 425 426
    if (preferencesWC) {
        [preferencesWC.window orderFront:preferencesWC.window];
        return;
    }
427

428
    preferencesWC = [[PreferencesWC alloc] initWithWindowNibName: @"PreferencesWindow" bundle: nil accountModel:self.accountModel dataTransferModel:self.dataTransferModel behaviourController:self.behaviorController avModel: self.avModel];
429
    [preferencesWC.window makeKeyAndOrderFront:preferencesWC.window];
430
}
431

432 433 434 435 436 437 438 439 440 441 442 443
- (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;
    }
}
444

445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461
#pragma mark - Ring account migration

- (void) migrateRingAccount:(Account*) acc
{
    self.migrateWC = [[MigrateRingAccountsWC alloc] initWithDelegate:self actionCode:1];
    self.migrateWC.account = acc;
#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
    [self.window beginSheet:self.migrateWC.window completionHandler:nil];
#else
    [NSApp beginSheet: self.migrateWC.window
       modalForWindow: self.window
        modalDelegate: self
       didEndSelector: nil
          contextInfo: nil];
#endif
}

462
// TODO: Reimplement as a blocking loop when new LRC models handle migration
463 464 465 466 467 468 469 470 471 472 473
- (void)checkAccountsToMigrate
{
    auto ringList = AccountModel::instance().accountsToMigrate();
    if (ringList.length() > 0){
        Account* acc = ringList.value(0);
        [self migrateRingAccount:acc];
    } else {
        // Fresh run, we need to make sure RingID appears
        [shareButton sendActionOn:NSLeftMouseDownMask];

        [self connect];
474
        [self updateRingID];
475 476 477 478 479 480 481 482 483 484 485 486 487
    }
}

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

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

488
- (void) selectAccount:(const lrc::api::account::Info&)accInfo currentRemoved:(BOOL) removed
489 490
{
    // If the selected account has been changed, we close any open panel
491
    [smartViewVC setConversationModel:accInfo.conversationModel.get()];
492 493
    // Welcome view informations are also updated
    [self updateRingID];
494
    [settingsVC setSelectedAccount:accInfo.id];
495
    [self changeViewTo: ([settingsVC.view isHidden] || removed)  ?  SHOW_WELCOME_SCREEN : SHOW_SETTINGS_SCREEN];
496 497
}

498 499 500
-(void)allAccountsDeleted
{
    [smartViewVC clearConversationModel];
501
    [self changeViewTo:SHOW_WELCOME_SCREEN];
502
    [self updateRingID];
503 504 505 506 507 508
    qrcodeView.animator.alphaValue = 0.0;
    [centerYQRCodeConstraint.animator setConstant: 0];
    [centerYWelcomeContainerConstraint.animator setConstant: 0];
    [self close];
    AppDelegate* delegate = (AppDelegate*)[[NSApplication sharedApplication] delegate];
    [delegate showWizard];
509 510
}

511 512 513
-(void)rightPanelClosed
{
    [smartViewVC deselect];
514
    [welcomeContainer setHidden:NO];
515 516
}

517 518 519 520 521
-(void)currentConversationTrusted
{
    [smartViewVC selectConversationList];
}

522
-(void) listTypeChanged {
523
    [self changeViewTo:SHOW_WELCOME_SCREEN];
524 525
}

526 527
- (IBAction)openAccountSettings:(NSButton *)sender
{
528
    [self changeViewTo: [settingsVC.view isHidden] ?  SHOW_SETTINGS_SCREEN : SHOW_WELCOME_SCREEN];
529 530
}

531
- (void) createNewAccount {
532
    [self changeViewTo:SHOW_WELCOME_SCREEN];
533 534 535
    wizard = [[RingWizardWC alloc] initWithNibName:@"RingWizard" bundle: nil accountmodel: self.accountModel];
    [wizard showChooseWithCancelButton: YES andAdvanced: YES];
    [self.window beginSheet:wizard.window completionHandler:nil];
536 537
}

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
538 539 540 541 542 543 544 545
- (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;
}

546 547
-(void) accountSettingsShouldOpen: (BOOL) open {
    if (open) {
548
        [settingsVC setSelectedAccount: [chooseAccountVC selectedAccount].id];
549
    }
550 551
}

552 553 554 555 556 557
#pragma mark - CallViewControllerDelegate

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

Kateryna Kostiuk's avatar
Kateryna Kostiuk committed
558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610
-(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]
                          account:&accInfo];
    [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];
}
611
@end