blob: 5f75ef45cbc12a0c91307b13963e09ca48629d8c [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 Lision1abdf582016-02-09 14:21:19 -050044 __unsafe_unretained IBOutlet NSView* callView;
45 __unsafe_unretained IBOutlet NSTextField* ringIDLabel;
46 __unsafe_unretained IBOutlet NSButton* shareButton;
Alexandre Lision58cab672015-06-09 15:25:40 -040047
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040048 PreferencesWC *preferencesWC;
Alexandre Lision0f66bd32016-01-18 11:30:45 -050049 CurrentCallVC* currentCallVC;
50 ConversationVC* offlineVC;
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040051}
Alexandre Lision58cab672015-06-09 15:25:40 -040052
Alexandre Lisionc5148052015-03-04 15:10:35 -050053static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier";
Alexandre Lisionc5148052015-03-04 15:10:35 -050054
Alexandre Lision5855b6a2015-02-03 11:31:05 -050055- (void)windowDidLoad {
56 [super windowDidLoad];
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040057 [self.window setMovableByWindowBackground:YES];
Alexandre Lision58cab672015-06-09 15:25:40 -040058
Alexandre Lision0f66bd32016-01-18 11:30:45 -050059 currentCallVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil];
60 offlineVC = [[ConversationVC alloc] initWithNibName:@"Conversation" bundle:nil];
61
Alexandre Lision58cab672015-06-09 15:25:40 -040062 [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
Alexandre Lision0f66bd32016-01-18 11:30:45 -050063 [[currentCallVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
64 [[offlineVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
Alexandre Lision58cab672015-06-09 15:25:40 -040065
Alexandre Lision0f66bd32016-01-18 11:30:45 -050066 [callView addSubview:[currentCallVC view] positioned:NSWindowAbove relativeTo:nil];
67 [callView addSubview:[offlineVC view] positioned:NSWindowAbove relativeTo:nil];
Alexandre Lision16d9c0a2015-08-10 12:05:15 -040068
Alexandre Lision0f66bd32016-01-18 11:30:45 -050069 [currentCallVC initFrame];
70 [offlineVC initFrame];
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040071
Alexandre Lision08abfac2015-09-22 12:20:51 -040072 // Fresh run, we need to make sure RingID appears
73 [self updateRingID];
Alexandre Lision1abdf582016-02-09 14:21:19 -050074 [shareButton sendActionOn:NSLeftMouseDownMask];
Alexandre Lision08abfac2015-09-22 12:20:51 -040075
Alexandre Lision0f66bd32016-01-18 11:30:45 -050076 [self connect];
77}
78
79- (void) connect
80{
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040081 // Update Ring ID label based on account model changes
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040082 QObject::connect(&AccountModel::instance(),
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040083 &AccountModel::dataChanged,
84 [=] {
85 [self updateRingID];
86 });
Alexandre Lision0f66bd32016-01-18 11:30:45 -050087
88 QObject::connect(RecentModel::instance().selectionModel(),
89 &QItemSelectionModel::currentChanged,
90 [=](const QModelIndex &current, const QModelIndex &previous) {
91 auto call = RecentModel::instance().getActiveCall(current);
92 if(!current.isValid()) {
Alexandre Lision01cf5e32016-01-21 15:54:30 -050093 [offlineVC animateOut];
Alexandre Lision0f66bd32016-01-18 11:30:45 -050094 [currentCallVC animateOut];
95 return;
96 }
97
98 if (!call) {
99 [currentCallVC animateOut];
100 [offlineVC animateIn];
101 } else {
102 [currentCallVC animateIn];
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500103 [offlineVC animateOut];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500104 }
105 });
Alexandre Lision210fe212016-01-27 11:15:13 -0500106
107 QObject::connect(CallModel::instance().selectionModel(),
108 &QItemSelectionModel::currentChanged,
109 [=](const QModelIndex &current, const QModelIndex &previous) {
110 if(!current.isValid()) {
111 return;
112 }
113
114 if (previous.isValid()) {
115 // We were already on a call
116 [currentCallVC animateOut];
117 } else {
118 // Make sure Conversation view hides when selecting a valid call
119 [currentCallVC animateIn];
120 [offlineVC animateOut];
121 }
122 });
Alexandre Lisionbb5c2462015-07-30 12:55:37 -0400123}
124
125/**
126 * Implement the necessary logic to choose which Ring ID to display.
127 * This tries to choose the "best" ID to show
128 */
129- (void) updateRingID
130{
131 Account* registered = nullptr;
132 Account* enabled = nullptr;
133 Account* finalChoice = nullptr;
134
135 [ringIDLabel setStringValue:@""];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400136 auto ringList = AccountModel::instance().getAccountsByProtocol(Account::Protocol::RING);
Alexandre Lisionbb5c2462015-07-30 12:55:37 -0400137 for (int i = 0 ; i < ringList.size() && !registered ; ++i) {
138 Account* acc = ringList.value(i);
139 if (acc->isEnabled()) {
140 if(!enabled)
141 enabled = finalChoice = acc;
142 if (acc->registrationState() == Account::RegistrationState::READY) {
143 registered = enabled = finalChoice = acc;
144 }
145 } else {
146 if (!finalChoice)
147 finalChoice = acc;
148 }
149 }
150
Alexandre Lision7f8351b2015-08-20 11:43:37 -0400151 [ringIDLabel setStringValue:[[NSString alloc] initWithFormat:@"%@", finalChoice->username().toNSString()]];
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500152}
153
Alexandre Lision1abdf582016-02-09 14:21:19 -0500154- (IBAction)shareRingID:(id)sender {
155 NSSharingServicePicker* sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:[NSArray arrayWithObject:[ringIDLabel stringValue]]];
156 [sharingServicePicker showRelativeToRect:[sender bounds]
157 ofView:sender
158 preferredEdge:NSMinYEdge];
159}
160
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500161- (IBAction)openPreferences:(id)sender
162{
Alexandre Lisionbfa68f62015-09-10 08:38:42 -0400163 preferencesWC = [[PreferencesWC alloc] initWithWindowNibName:@"PreferencesWindow"];
164 [preferencesWC.window makeKeyAndOrderFront:preferencesWC.window];
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500165}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500166
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500167@end