blob: b05157f9d64f668c4024ac6d7553262d1c34ca41 [file] [log] [blame]
Alexandre Lision8521baa2015-03-13 11:08:00 -04001/*
Alexandre Lision9fe374b2016-01-06 10:17:31 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Alexandre Lision8521baa2015-03-13 11:08:00 -04003 * Author: Alexandre Lision <alexandre.lision@savoirfairelinux.com>
4 *
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 3 of the License, or
8 * (at your option) any later version.
9 *
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
14 *
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
Alexandre Lision8521baa2015-03-13 11:08:00 -040018 */
Alexandre Lision5855b6a2015-02-03 11:31:05 -050019#import "RingWindowController.h"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050020#import <QuartzCore/QuartzCore.h>
21
22
23//Qt
24#import <QItemSelectionModel>
25#import <QItemSelection>
Alexandre Lision5855b6a2015-02-03 11:31:05 -050026
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040027//LRC
Alexandre Lision5855b6a2015-02-03 11:31:05 -050028#import <accountmodel.h>
29#import <callmodel.h>
30#import <account.h>
Alexandre Lision91d11e52015-03-20 17:42:05 -040031#import <call.h>
Alexandre Lision0f66bd32016-01-18 11:30:45 -050032#import <recentmodel.h>
Alexandre Lision5855b6a2015-02-03 11:31:05 -050033
Alexandre Lision745e4d62015-03-22 20:03:10 -040034#import "AppDelegate.h"
Alexandre Lisionc65310c2015-04-23 16:44:23 -040035#import "Constants.h"
Alexandre Lision58cab672015-06-09 15:25:40 -040036#import "CurrentCallVC.h"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050037#import "ConversationVC.h"
Alexandre Lision745e4d62015-03-22 20:03:10 -040038
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040039#import "PreferencesWC.h"
40#import "views/NSColor+RingTheme.h"
Alexandre Lision2db8f472015-07-22 15:05:46 -040041
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040042@implementation RingWindowController {
Alexandre Lision5855b6a2015-02-03 11:31:05 -050043
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040044 __unsafe_unretained IBOutlet NSView *callView;
45 __unsafe_unretained IBOutlet NSTextField *ringIDLabel;
Alexandre Lision58cab672015-06-09 15:25:40 -040046
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040047 PreferencesWC *preferencesWC;
Alexandre Lision0f66bd32016-01-18 11:30:45 -050048 CurrentCallVC* currentCallVC;
49 ConversationVC* offlineVC;
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040050}
Alexandre Lision58cab672015-06-09 15:25:40 -040051
Alexandre Lisionc5148052015-03-04 15:10:35 -050052static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier";
Alexandre Lisionc5148052015-03-04 15:10:35 -050053
Alexandre Lision5855b6a2015-02-03 11:31:05 -050054- (void)windowDidLoad {
55 [super windowDidLoad];
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040056 [self.window setMovableByWindowBackground:YES];
Alexandre Lision58cab672015-06-09 15:25:40 -040057
Alexandre Lision0f66bd32016-01-18 11:30:45 -050058 currentCallVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil];
59 offlineVC = [[ConversationVC alloc] initWithNibName:@"Conversation" bundle:nil];
60
Alexandre Lision58cab672015-06-09 15:25:40 -040061 [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
Alexandre Lision0f66bd32016-01-18 11:30:45 -050062 [[currentCallVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
63 [[offlineVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
Alexandre Lision58cab672015-06-09 15:25:40 -040064
Alexandre Lision0f66bd32016-01-18 11:30:45 -050065 [callView addSubview:[currentCallVC view] positioned:NSWindowAbove relativeTo:nil];
66 [callView addSubview:[offlineVC view] positioned:NSWindowAbove relativeTo:nil];
Alexandre Lision16d9c0a2015-08-10 12:05:15 -040067
Alexandre Lision0f66bd32016-01-18 11:30:45 -050068 [currentCallVC initFrame];
69 [offlineVC initFrame];
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040070
Alexandre Lision08abfac2015-09-22 12:20:51 -040071 // Fresh run, we need to make sure RingID appears
72 [self updateRingID];
73
Alexandre Lision0f66bd32016-01-18 11:30:45 -050074 [self connect];
75}
76
77- (void) connect
78{
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040079 // Update Ring ID label based on account model changes
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040080 QObject::connect(&AccountModel::instance(),
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040081 &AccountModel::dataChanged,
82 [=] {
83 [self updateRingID];
84 });
Alexandre Lision0f66bd32016-01-18 11:30:45 -050085
86 QObject::connect(RecentModel::instance().selectionModel(),
87 &QItemSelectionModel::currentChanged,
88 [=](const QModelIndex &current, const QModelIndex &previous) {
89 auto call = RecentModel::instance().getActiveCall(current);
90 if(!current.isValid()) {
Alexandre Lision01cf5e32016-01-21 15:54:30 -050091 [offlineVC animateOut];
Alexandre Lision0f66bd32016-01-18 11:30:45 -050092 [currentCallVC animateOut];
93 return;
94 }
95
96 if (!call) {
97 [currentCallVC animateOut];
98 [offlineVC animateIn];
99 } else {
100 [currentCallVC animateIn];
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500101 [offlineVC animateOut];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500102 }
103 });
Alexandre Lisionbb5c2462015-07-30 12:55:37 -0400104}
105
106/**
107 * Implement the necessary logic to choose which Ring ID to display.
108 * This tries to choose the "best" ID to show
109 */
110- (void) updateRingID
111{
112 Account* registered = nullptr;
113 Account* enabled = nullptr;
114 Account* finalChoice = nullptr;
115
116 [ringIDLabel setStringValue:@""];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400117 auto ringList = AccountModel::instance().getAccountsByProtocol(Account::Protocol::RING);
Alexandre Lisionbb5c2462015-07-30 12:55:37 -0400118 for (int i = 0 ; i < ringList.size() && !registered ; ++i) {
119 Account* acc = ringList.value(i);
120 if (acc->isEnabled()) {
121 if(!enabled)
122 enabled = finalChoice = acc;
123 if (acc->registrationState() == Account::RegistrationState::READY) {
124 registered = enabled = finalChoice = acc;
125 }
126 } else {
127 if (!finalChoice)
128 finalChoice = acc;
129 }
130 }
131
Alexandre Lision7f8351b2015-08-20 11:43:37 -0400132 [ringIDLabel setStringValue:[[NSString alloc] initWithFormat:@"%@", finalChoice->username().toNSString()]];
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500133}
134
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500135- (IBAction)openPreferences:(id)sender
136{
Alexandre Lisionbfa68f62015-09-10 08:38:42 -0400137 preferencesWC = [[PreferencesWC alloc] initWithWindowNibName:@"PreferencesWindow"];
138 [preferencesWC.window makeKeyAndOrderFront:preferencesWC.window];
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500139}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500140
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500141@end