RingWindowController.mm 5.41 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 21 22 23 24 25
#import <QuartzCore/QuartzCore.h>


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

27
//LRC
28 29 30
#import <accountmodel.h>
#import <callmodel.h>
#import <account.h>
31
#import <call.h>
32
#import <recentmodel.h>
33

34
#import "AppDelegate.h"
35
#import "Constants.h"
Alexandre Lision's avatar
Alexandre Lision committed
36
#import "CurrentCallVC.h"
37
#import "ConversationVC.h"
38

39 40
#import "PreferencesWC.h"
#import "views/NSColor+RingTheme.h"
41

42
@implementation RingWindowController {
43

44 45
    __unsafe_unretained IBOutlet NSView *callView;
    __unsafe_unretained IBOutlet NSTextField *ringIDLabel;
Alexandre Lision's avatar
Alexandre Lision committed
46

47
    PreferencesWC *preferencesWC;
48 49
    CurrentCallVC* currentCallVC;
    ConversationVC* offlineVC;
50
}
Alexandre Lision's avatar
Alexandre Lision committed
51

52 53
static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier";

54 55
- (void)windowDidLoad {
    [super windowDidLoad];
56
    [self.window setMovableByWindowBackground:YES];
Alexandre Lision's avatar
Alexandre Lision committed
57

58 59 60
    currentCallVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil];
    offlineVC = [[ConversationVC alloc] initWithNibName:@"Conversation" bundle:nil];

Alexandre Lision's avatar
Alexandre Lision committed
61
    [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
62 63
    [[currentCallVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
    [[offlineVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
Alexandre Lision's avatar
Alexandre Lision committed
64

65 66
    [callView addSubview:[currentCallVC view] positioned:NSWindowAbove relativeTo:nil];
    [callView addSubview:[offlineVC view] positioned:NSWindowAbove relativeTo:nil];
Alexandre Lision's avatar
Alexandre Lision committed
67

68 69
    [currentCallVC initFrame];
    [offlineVC initFrame];
70

Alexandre Lision's avatar
Alexandre Lision committed
71 72 73
    // Fresh run, we need to make sure RingID appears
    [self updateRingID];

74 75 76 77 78
    [self connect];
}

- (void) connect
{
79
    // Update Ring ID label based on account model changes
80
    QObject::connect(&AccountModel::instance(),
81 82 83 84
                     &AccountModel::dataChanged,
                     [=] {
                         [self updateRingID];
                     });
85 86 87 88 89 90

    QObject::connect(RecentModel::instance().selectionModel(),
                     &QItemSelectionModel::currentChanged,
                     [=](const QModelIndex &current, const QModelIndex &previous) {
                         auto call = RecentModel::instance().getActiveCall(current);
                         if(!current.isValid()) {
91
                             [offlineVC animateOut];
92 93 94 95 96 97 98 99 100
                             [currentCallVC animateOut];
                             return;
                         }

                         if (!call) {
                             [currentCallVC animateOut];
                             [offlineVC animateIn];
                         } else {
                             [currentCallVC animateIn];
101
                             [offlineVC animateOut];
102 103
                         }
                     });
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120

    QObject::connect(CallModel::instance().selectionModel(),
                     &QItemSelectionModel::currentChanged,
                     [=](const QModelIndex &current, const QModelIndex &previous) {
                         if(!current.isValid()) {
                             return;
                         }

                         if (previous.isValid()) {
                             // We were already on a call
                             [currentCallVC animateOut];
                         } else {
                             // Make sure Conversation view hides when selecting a valid call
                             [currentCallVC animateIn];
                             [offlineVC animateOut];
                         }
                     });
121 122 123 124 125 126 127 128 129 130 131 132 133
}

/**
 * Implement the necessary logic to choose which Ring ID to display.
 * This tries to choose the "best" ID to show
 */
- (void) updateRingID
{
    Account* registered = nullptr;
    Account* enabled = nullptr;
    Account* finalChoice = nullptr;

    [ringIDLabel setStringValue:@""];
134
    auto ringList = AccountModel::instance().getAccountsByProtocol(Account::Protocol::RING);
135 136 137 138 139 140 141 142 143 144 145 146 147 148
    for (int i = 0 ; i < ringList.size() && !registered ; ++i) {
        Account* acc = ringList.value(i);
        if (acc->isEnabled()) {
            if(!enabled)
                enabled = finalChoice = acc;
            if (acc->registrationState() == Account::RegistrationState::READY) {
                registered = enabled = finalChoice = acc;
            }
        } else {
            if (!finalChoice)
                finalChoice = acc;
        }
    }

149
    [ringIDLabel setStringValue:[[NSString alloc] initWithFormat:@"%@", finalChoice->username().toNSString()]];
150 151
}

152 153
- (IBAction)openPreferences:(id)sender
{
154 155
    preferencesWC = [[PreferencesWC alloc] initWithWindowNibName:@"PreferencesWindow"];
    [preferencesWC.window makeKeyAndOrderFront:preferencesWC.window];
156
}
157

158
@end