blob: 7a0a3f95ba4ea6bfed899ff2e12539aabaab5fd0 [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>
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -040021#include <qrencode.h>
Anthony Léonard79597602017-11-13 15:47:09 -050022#include <memory>
Alexandre Lision0f66bd32016-01-18 11:30:45 -050023
24//Qt
25#import <QItemSelectionModel>
26#import <QItemSelection>
Alexandre Lision5855b6a2015-02-03 11:31:05 -050027
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040028//LRC
Alexandre Lision5855b6a2015-02-03 11:31:05 -050029#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>
Kateryna Kostiuk312f9132017-04-18 12:09:06 -040033#import <AvailableAccountModel.h>
Anthony Léonard79597602017-11-13 15:47:09 -050034#import <api/lrc.h>
Anthony Léonard49cb2912017-11-13 16:15:39 -050035#import <api/account.h>
Olivier Soldano994971f2017-12-05 16:30:12 -050036#import <api/newaccountmodel.h>
37#import <api/newcallmodel.h>
38#import <api/behaviorcontroller.h>
39#import <api/conversation.h>
Anthony Léonardd00cd182018-01-17 09:21:27 -050040#import <api/contactmodel.h>
41#import <api/contact.h>
Kateryna Kostiuk67735232018-05-10 15:05:32 -040042#import <api/datatransfermodel.h>
Kateryna Kostiuk74fe20c2018-06-14 12:05:53 -040043#import <media/recordingmodel.h>
Alexandre Lision5855b6a2015-02-03 11:31:05 -050044
Alexandre Lision624b1a82016-09-11 19:29:01 -040045// Ring
Alexandre Lision745e4d62015-03-22 20:03:10 -040046#import "AppDelegate.h"
Alexandre Lisionc65310c2015-04-23 16:44:23 -040047#import "Constants.h"
Alexandre Lision58cab672015-06-09 15:25:40 -040048#import "CurrentCallVC.h"
Alexandre Lision624b1a82016-09-11 19:29:01 -040049#import "MigrateRingAccountsWC.h"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050050#import "ConversationVC.h"
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040051#import "PreferencesWC.h"
Alexandre Lisiona3a43dc2017-03-30 16:21:30 -040052#import "SmartViewVC.h"
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -040053#import "views/IconButton.h"
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040054#import "views/NSColor+RingTheme.h"
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040055#import "views/HoverButton.h"
Kateryna Kostiuk74fe20c2018-06-14 12:05:53 -040056#import "utils.h"
Kateryna Kostiukd73f9602018-07-24 13:51:28 -040057#import "RingWizardWC.h"
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040058#import "AccountSettingsVC.h"
Kateryna Kostiukc17db8d2018-10-23 09:48:03 -040059#import "views/CallLayer.h"
60
61typedef NS_ENUM(NSInteger, ViewState) {
62 SHOW_WELCOME_SCREEN = 0,
63 SHOW_CONVERSATION_SCREEN,
64 SHOW_CALL_SCREEN,
65 SHOW_SETTINGS_SCREEN,
66};
Alexandre Lision2db8f472015-07-22 15:05:46 -040067
Kateryna Kostiukef66f972018-11-02 17:10:37 -040068@interface RingWindowController () <MigrateRingAccountsDelegate>
Alexandre Lision624b1a82016-09-11 19:29:01 -040069
70@property (retain) MigrateRingAccountsWC* migrateWC;
Kateryna Kostiukd73f9602018-07-24 13:51:28 -040071@property RingWizardWC* wizard;
Alexandre Lision624b1a82016-09-11 19:29:01 -040072
73@end
74
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040075@implementation RingWindowController {
Alexandre Lision5855b6a2015-02-03 11:31:05 -050076
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -040077 __unsafe_unretained IBOutlet NSLayoutConstraint* centerYQRCodeConstraint;
78 __unsafe_unretained IBOutlet NSLayoutConstraint* centerYWelcomeContainerConstraint;
Kateryna Kostiukab499f42018-04-16 12:27:33 -040079 IBOutlet NSLayoutConstraint* ringLabelTrailingConstraint;
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -040080 __unsafe_unretained IBOutlet NSView* welcomeContainer;
Alexandre Lision1abdf582016-02-09 14:21:19 -050081 __unsafe_unretained IBOutlet NSView* callView;
82 __unsafe_unretained IBOutlet NSTextField* ringIDLabel;
83 __unsafe_unretained IBOutlet NSButton* shareButton;
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -040084 __unsafe_unretained IBOutlet NSImageView* qrcodeView;
Alexandre Lision58cab672015-06-09 15:25:40 -040085
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -040086 PreferencesWC* preferencesWC;
Alexandre Lisiona3a43dc2017-03-30 16:21:30 -040087 IBOutlet SmartViewVC* smartViewVC;
Alexandre Lisione77f6f92016-04-17 23:39:39 -040088
Alexandre Lision0f66bd32016-01-18 11:30:45 -050089 CurrentCallVC* currentCallVC;
Andreas Traczykecdc4fa2018-03-22 16:11:47 -040090 ConversationVC* conversationVC;
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040091 AccountSettingsVC* settingsVC;
Andreas Traczykecdc4fa2018-03-22 16:11:47 -040092
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040093 // toolbar menu items
Kateryna Kostiukef66f972018-11-02 17:10:37 -040094 IBOutlet ChooseAccountVC* chooseAccountVC;
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040095}
Alexandre Lision58cab672015-06-09 15:25:40 -040096
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040097static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier";
98NSString* const kChangeAccountToolBarItemIdentifier = @"ChangeAccountToolBarItemIdentifier";
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -040099NSString* const kOpenAccountToolBarItemIdentifier = @"OpenAccountToolBarItemIdentifier";
Alexandre Lisionc5148052015-03-04 15:10:35 -0500100
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400101@synthesize dataTransferModel, accountModel, behaviorController;
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400102@synthesize wizard;
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400103
104-(id) initWithWindowNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil accountModel:( lrc::api::NewAccountModel*)accountModel dataTransferModel:( lrc::api::DataTransferModel*)dataTransferModel behaviourController:( lrc::api::BehaviorController*) behaviorController
105{
106 if (self = [self initWithWindowNibName:nibNameOrNil])
107 {
108 self.accountModel = accountModel;
109 self.dataTransferModel = dataTransferModel;
110 self.behaviorController = behaviorController;
111 }
112 return self;
113}
114
Kateryna Kostiukef66f972018-11-02 17:10:37 -0400115- (NSApplicationPresentationOptions)window:(NSWindow *)window willUseFullScreenPresentationOptions:(NSApplicationPresentationOptions)proposedOptions
116{
117 return (NSApplicationPresentationFullScreen |
118 NSApplicationPresentationAutoHideMenuBar |
119 NSApplicationPresentationAutoHideToolbar);
120}
121
Kateryna Kostiukc17db8d2018-10-23 09:48:03 -0400122-(void)changeViewTo:(ViewState) state {
123 switch (state) {
124 case SHOW_WELCOME_SCREEN:
125 [self accountSettingsShouldOpen: NO];
126 [conversationVC hideWithAnimation:false];
127 [currentCallVC hideWithAnimation:false];
Kateryna Kostiuk4f37d952018-12-04 13:19:17 -0500128 [currentCallVC cleanUp];
Kateryna Kostiukc17db8d2018-10-23 09:48:03 -0400129 [currentCallVC.view removeFromSuperview];
130 [welcomeContainer setHidden: NO];
131 [smartViewVC.view setHidden: NO];
132 [settingsVC hide];
133 break;
134 case SHOW_CONVERSATION_SCREEN:
135 [self accountSettingsShouldOpen: NO];
136 [conversationVC showWithAnimation:false];
137 [currentCallVC hideWithAnimation:false];
Kateryna Kostiuk4f37d952018-12-04 13:19:17 -0500138 [currentCallVC cleanUp];
Kateryna Kostiukc17db8d2018-10-23 09:48:03 -0400139 [currentCallVC.view removeFromSuperview];
140 [welcomeContainer setHidden: YES];
141 [smartViewVC.view setHidden: NO];
142 [settingsVC hide];
143 break;
144 case SHOW_CALL_SCREEN:
145 [self accountSettingsShouldOpen: NO];
146 if (![currentCallVC.view superview]) {
147 [callView addSubview:[currentCallVC view] positioned:NSWindowAbove relativeTo:nil];
148 [currentCallVC initFrame];
149 [currentCallVC showWithAnimation:false];
150 [conversationVC hideWithAnimation:false];
151 [welcomeContainer setHidden: YES];
152 [smartViewVC.view setHidden: NO];
153 [settingsVC hide];
154 }
155 [currentCallVC showWithAnimation:false];
156 break;
157 case SHOW_SETTINGS_SCREEN:
158 @try {
159 [self accountSettingsShouldOpen: YES];
160 }
161 @catch (NSException *ex) {
162 return;
163 }
164 [welcomeContainer setHidden: YES];
165 [currentCallVC hideWithAnimation:false];
166 [currentCallVC.view removeFromSuperview];
167 [conversationVC hideWithAnimation:false];
168 [smartViewVC.view setHidden: YES];
169 [settingsVC show];
170 break;
171 default:
172 break;
173 }
174}
175
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500176- (void)windowDidLoad {
177 [super windowDidLoad];
Alexandre Lision4dfcafc2015-08-20 12:43:23 -0400178 [self.window setMovableByWindowBackground:YES];
Alexandre Lision58cab672015-06-09 15:25:40 -0400179
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400180 self.window.titleVisibility = NSWindowTitleHidden;
Alexandre Lisione77f6f92016-04-17 23:39:39 -0400181
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500182 currentCallVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil];
Kateryna Kostiuk133364b2018-10-31 11:36:08 -0400183 currentCallVC.delegate = self;
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400184 conversationVC = [[ConversationVC alloc] initWithNibName:@"Conversation" bundle:nil delegate:self];
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -0400185 // toolbar items
Kateryna Kostiukef66f972018-11-02 17:10:37 -0400186 //chooseAccountVC = [[ChooseAccountVC alloc] initWithNibName:@"ChooseAccount" bundle:nil model:self.accountModel delegate:self];
187 [chooseAccountVC updateWithDelegate: self andModel:self.accountModel];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400188 settingsVC = [[AccountSettingsVC alloc] initWithNibName:@"AccountSettings" bundle:nil accountmodel:self.accountModel];
Kateryna Kostiukef66f972018-11-02 17:10:37 -0400189 //[self.window.contentView addSubview:[chooseAccountVC view]];
Alexandre Lision58cab672015-06-09 15:25:40 -0400190 [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500191 [[currentCallVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400192 [[conversationVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400193 [[settingsVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
Alexandre Lision58cab672015-06-09 15:25:40 -0400194
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400195 [callView addSubview:[conversationVC view] positioned:NSWindowAbove relativeTo:nil];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400196 [self.window.contentView addSubview:[settingsVC view] positioned:NSWindowAbove relativeTo:nil];
Alexandre Lision16d9c0a2015-08-10 12:05:15 -0400197
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400198 [conversationVC initFrame];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400199 [settingsVC initFrame];
Anthony Léonard72128c92017-12-26 16:48:39 -0500200 @try {
201 [smartViewVC setConversationModel: [chooseAccountVC selectedAccount].conversationModel.get()];
202 }
203 @catch (NSException *ex) {
204 NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
205 }
Alexandre Lisionbb5c2462015-07-30 12:55:37 -0400206
Anthony Léonard49cb2912017-11-13 16:15:39 -0500207 // Fresh run, we need to make sure RingID appears
208 [shareButton sendActionOn:NSLeftMouseDownMask];
209
Olivier Soldano994971f2017-12-05 16:30:12 -0500210 [self connect];
Anthony Léonard49cb2912017-11-13 16:15:39 -0500211 [self updateRingID];
Kateryna Kostiuk74fe20c2018-06-14 12:05:53 -0400212 // set download folder (default - 'Documents')
Kateryna Kostiuk67735232018-05-10 15:05:32 -0400213 NSString* path = [[NSUserDefaults standardUserDefaults] stringForKey:Preferences::DownloadFolder];
214 if (!path || path.length == 0) {
Kateryna Kostiuk74fe20c2018-06-14 12:05:53 -0400215 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
216 path = [[paths objectAtIndex:0] stringByAppendingString:@"/"];
Kateryna Kostiuk67735232018-05-10 15:05:32 -0400217 }
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400218 self.dataTransferModel->downloadDirectory = std::string([path UTF8String]);
Kateryna Kostiuk74fe20c2018-06-14 12:05:53 -0400219 if(appSandboxed()) {
220 NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
Kateryna Kostiukf9a72c02018-08-01 14:28:49 -0400221 media::RecordingModel::instance().setRecordPath(QString::fromNSString([paths objectAtIndex:0]));
Kateryna Kostiuk74fe20c2018-06-14 12:05:53 -0400222 }
Kateryna Kostiukef66f972018-11-02 17:10:37 -0400223 NSToolbar *tb = [[self window] toolbar];
224 [tb setAllowsUserCustomization:NO];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500225}
226
227- (void) connect
228{
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400229 QObject::connect(self.behaviorController,
Olivier Soldano994971f2017-12-05 16:30:12 -0500230 &lrc::api::BehaviorController::showCallView,
231 [self](const std::string accountId,
232 const lrc::api::conversation::Info convInfo){
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400233 auto* accInfo = &self.accountModel->getAccountInfo(accountId);
Anthony Léonardd00cd182018-01-17 09:21:27 -0500234 if (accInfo->contactModel->getContact(convInfo.participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING)
235 [smartViewVC selectPendingList];
236 else
237 [smartViewVC selectConversationList];
238
Olivier Soldano994971f2017-12-05 16:30:12 -0500239 [currentCallVC setCurrentCall:convInfo.callId
240 conversation:convInfo.uid
241 account:accInfo];
Kateryna Kostiukc17db8d2018-10-23 09:48:03 -0400242 [self changeViewTo:SHOW_CALL_SCREEN];
243
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500244 });
Alexandre Lision210fe212016-01-27 11:15:13 -0500245
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400246 QObject::connect(self.behaviorController,
Olivier Soldano994971f2017-12-05 16:30:12 -0500247 &lrc::api::BehaviorController::showIncomingCallView,
248 [self](const std::string accountId,
249 const lrc::api::conversation::Info convInfo){
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400250 auto* accInfo = &self.accountModel->getAccountInfo(accountId);
Anthony Léonardd00cd182018-01-17 09:21:27 -0500251 if (accInfo->contactModel->getContact(convInfo.participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING)
252 [smartViewVC selectPendingList];
253 else
254 [smartViewVC selectConversationList];
255
Olivier Soldano994971f2017-12-05 16:30:12 -0500256 [currentCallVC setCurrentCall:convInfo.callId
257 conversation:convInfo.uid
258 account:accInfo];
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500259 [smartViewVC selectConversation: convInfo model:accInfo->conversationModel.get()];
Kateryna Kostiukc17db8d2018-10-23 09:48:03 -0400260 [self changeViewTo:SHOW_CALL_SCREEN];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400261 });
Anthony Léonard2382b562017-12-13 15:51:28 -0500262
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400263 QObject::connect(self.behaviorController,
Anthony Léonard2382b562017-12-13 15:51:28 -0500264 &lrc::api::BehaviorController::showChatView,
265 [self](const std::string& accountId,
266 const lrc::api::conversation::Info& convInfo){
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400267 auto& accInfo = self.accountModel->getAccountInfo(accountId);
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400268 [conversationVC setConversationUid:convInfo.uid model:accInfo.conversationModel.get()];
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500269 [smartViewVC selectConversation: convInfo model:accInfo.conversationModel.get()];
Kateryna Kostiukc17db8d2018-10-23 09:48:03 -0400270 [self changeViewTo:SHOW_CONVERSATION_SCREEN];
Anthony Léonard2382b562017-12-13 15:51:28 -0500271 });
Alexandre Lisionbb5c2462015-07-30 12:55:37 -0400272}
273
274/**
275 * Implement the necessary logic to choose which Ring ID to display.
276 * This tries to choose the "best" ID to show
277 */
278- (void) updateRingID
279{
Anthony Léonard72128c92017-12-26 16:48:39 -0500280 @try {
281 auto& account = [chooseAccountVC selectedAccount];
Alexandre Lisionbb5c2462015-07-30 12:55:37 -0400282
Anthony Léonard72128c92017-12-26 16:48:39 -0500283 [ringIDLabel setStringValue:@""];
Anthony Léonard49cb2912017-11-13 16:15:39 -0500284
Anthony Léonard72128c92017-12-26 16:48:39 -0500285 if(account.profileInfo.type != lrc::api::profile::Type::RING) {
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400286 self.notRingAccount = YES;
287 self.isSIPAccount = YES;
Anthony Léonard72128c92017-12-26 16:48:39 -0500288 return;
289 }
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400290 self.isSIPAccount = NO;
291 self.notRingAccount = NO;
292 [ringLabelTrailingConstraint setActive:YES];
Anthony Léonard72128c92017-12-26 16:48:39 -0500293 auto& registeredName = account.registeredName;
294 auto& ringID = account.profileInfo.uri;
295 NSString* uriToDisplay = nullptr;
296 if (!registeredName.empty()) {
297 uriToDisplay = @(registeredName.c_str());
298 } else {
299 uriToDisplay = @(ringID.c_str());
300 }
301 [ringIDLabel setStringValue:uriToDisplay];
302 [self drawQRCode:@(ringID.c_str())];
Alexandre Lisionbb5c2462015-07-30 12:55:37 -0400303 }
Anthony Léonard72128c92017-12-26 16:48:39 -0500304 @catch (NSException *ex) {
305 NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400306 self.notRingAccount = YES;
307 self.isSIPAccount = NO;
308 [ringLabelTrailingConstraint setActive:NO];
Anthony Léonard70638f02018-02-05 11:10:19 -0500309 [ringIDLabel setStringValue:NSLocalizedString(@"No account available", @"Displayed as RingID when no accounts are available for selection")];
Alexandre Lision313427f2016-11-24 21:04:04 -0500310 }
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500311}
312
Alexandre Lision1abdf582016-02-09 14:21:19 -0500313- (IBAction)shareRingID:(id)sender {
314 NSSharingServicePicker* sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:[NSArray arrayWithObject:[ringIDLabel stringValue]]];
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400315 [sharingServicePicker setDelegate:self];
Alexandre Lision1abdf582016-02-09 14:21:19 -0500316 [sharingServicePicker showRelativeToRect:[sender bounds]
317 ofView:sender
318 preferredEdge:NSMinYEdge];
319}
320
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400321- (IBAction)toggleQRCode:(id)sender {
322 // Toggle pressed state of QRCode button
323 [sender setPressed:![sender isPressed]];
Kateryna Kostiuk65ba43e2017-03-30 15:10:04 -0400324 [self showQRCode:[sender isPressed]];
Alexandre Lision313427f2016-11-24 21:04:04 -0500325}
326
327/**
328 * Draw the QRCode in the qrCodeView
329 */
Kateryna Kostiuk65ba43e2017-03-30 15:10:04 -0400330- (void)drawQRCode:(NSString*) uriToDraw
Alexandre Lision313427f2016-11-24 21:04:04 -0500331{
Kateryna Kostiuk65ba43e2017-03-30 15:10:04 -0400332 auto qrCode = QRcode_encodeString(uriToDraw.UTF8String,
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400333 0,
334 QR_ECLEVEL_L, // Lowest level of error correction
335 QR_MODE_8, // 8-bit data mode
336 1);
337 if (!qrCode) {
338 return;
339 }
340
Alexandre Lision313427f2016-11-24 21:04:04 -0500341 unsigned char *data = 0;
342 int width;
343 data = qrCode->data;
344 width = qrCode->width;
345 int qr_margin = 3;
346
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400347 CGFloat size = qrcodeView.frame.size.width;
348
349 // create context
350 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
351 CGContextRef ctx = CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace, kCGImageAlphaPremultipliedLast);
352
353 CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -size);
354 CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1);
355 CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform));
356
Alexandre Lision313427f2016-11-24 21:04:04 -0500357 float zoom = ceil((double)size / (qrCode->width + 2.0 * qr_margin));
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400358 CGRect rectDraw = CGRectMake(0, 0, zoom, zoom);
359
360 int ran;
361 for(int i = 0; i < width; ++i) {
362 for(int j = 0; j < width; ++j) {
363 if(*data & 1) {
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -0400364 CGContextSetFillColorWithColor(ctx, [NSColor labelColor].CGColor);
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400365 rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom);
366 CGContextAddRect(ctx, rectDraw);
367 CGContextFillPath(ctx);
368 } else {
369 CGContextSetFillColorWithColor(ctx, [NSColor windowBackgroundColor].CGColor);
370 rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom);
371 CGContextAddRect(ctx, rectDraw);
372 CGContextFillPath(ctx);
373 }
374 ++data;
375 }
376 }
Alexandre Lision313427f2016-11-24 21:04:04 -0500377
378 // get image
379 auto qrCGImage = CGBitmapContextCreateImage(ctx);
380 auto qrImage = [[NSImage alloc] initWithCGImage:qrCGImage size:qrcodeView.frame.size];
381
382 // some releases
383 CGContextRelease(ctx);
384 CGImageRelease(qrCGImage);
385 CGColorSpaceRelease(colorSpace);
386 QRcode_free(qrCode);
387
388 [qrcodeView setImage:qrImage];
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400389}
390
391/**
392 * Start the in/out animation displaying the QRCode
393 * @param show should the QRCode be animated in or out
394 */
395- (void) showQRCode:(BOOL) show
396{
397 static const NSInteger offset = 110;
398 [NSAnimationContext beginGrouping];
399 NSAnimationContext.currentContext.duration = 0.5;
400 [[NSAnimationContext currentContext] setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
401 qrcodeView.animator.alphaValue = show ? 1.0 : 0.0;
402 [centerYQRCodeConstraint.animator setConstant: show ? offset : 0];
403 [centerYWelcomeContainerConstraint.animator setConstant:show ? -offset : 0];
404 [NSAnimationContext endGrouping];
405}
406
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500407- (IBAction)openPreferences:(id)sender
408{
Kateryna Kostiukbb68a1c2018-06-12 15:16:02 -0400409 if (preferencesWC) {
410 [preferencesWC.window orderFront:preferencesWC.window];
411 return;
412 }
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400413
414 preferencesWC = [[PreferencesWC alloc] initWithWindowNibName: @"PreferencesWindow" bundle: nil accountModel:self.accountModel dataTransferModel:self.dataTransferModel behaviourController:self.behaviorController];
Alexandre Lisionbfa68f62015-09-10 08:38:42 -0400415 [preferencesWC.window makeKeyAndOrderFront:preferencesWC.window];
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500416}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500417
Kateryna Kostiuk882cbac2017-07-05 17:29:00 -0400418- (IBAction)callClickedAtRow:(id)sender
419{
420 NSTabViewItem *selectedTab = [smartViewVC.tabbar selectedTabViewItem];
421 int index = [smartViewVC.tabbar indexOfTabViewItem:selectedTab];
422 switch (index) {
423 case 0:
424 [smartViewVC startCallForRow:sender];
425 break;
Kateryna Kostiuk882cbac2017-07-05 17:29:00 -0400426 default:
427 break;
428 }
429}
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500430
Alexandre Lision624b1a82016-09-11 19:29:01 -0400431#pragma mark - Ring account migration
432
433- (void) migrateRingAccount:(Account*) acc
434{
435 self.migrateWC = [[MigrateRingAccountsWC alloc] initWithDelegate:self actionCode:1];
436 self.migrateWC.account = acc;
437#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
438 [self.window beginSheet:self.migrateWC.window completionHandler:nil];
439#else
440 [NSApp beginSheet: self.migrateWC.window
441 modalForWindow: self.window
442 modalDelegate: self
443 didEndSelector: nil
444 contextInfo: nil];
445#endif
446}
447
Anthony Léonard49cb2912017-11-13 16:15:39 -0500448// TODO: Reimplement as a blocking loop when new LRC models handle migration
Alexandre Lision624b1a82016-09-11 19:29:01 -0400449- (void)checkAccountsToMigrate
450{
451 auto ringList = AccountModel::instance().accountsToMigrate();
452 if (ringList.length() > 0){
453 Account* acc = ringList.value(0);
454 [self migrateRingAccount:acc];
455 } else {
456 // Fresh run, we need to make sure RingID appears
Alexandre Lision624b1a82016-09-11 19:29:01 -0400457 [shareButton sendActionOn:NSLeftMouseDownMask];
458
459 [self connect];
Kateryna Kostiuka7248462017-04-18 16:15:31 -0400460 [self updateRingID];
Alexandre Lision624b1a82016-09-11 19:29:01 -0400461 }
462}
463
464- (void)migrationDidComplete
465{
466 [self checkAccountsToMigrate];
467}
468
469- (void)migrationDidCompleteWithError
470{
471 [self checkAccountsToMigrate];
472}
473
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400474- (void) selectAccount:(const lrc::api::account::Info&)accInfo currentRemoved:(BOOL) removed
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500475{
476 // If the selected account has been changed, we close any open panel
Kateryna Kostiukc17db8d2018-10-23 09:48:03 -0400477 [smartViewVC setConversationModel:accInfo.conversationModel.get()];
Anthony Léonard61e3dcf2017-12-27 12:19:52 -0500478 // Welcome view informations are also updated
479 [self updateRingID];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400480 [settingsVC setSelectedAccount:accInfo.id];
Kateryna Kostiukc17db8d2018-10-23 09:48:03 -0400481 [self changeViewTo: ([settingsVC.view isHidden] || removed) ? SHOW_WELCOME_SCREEN : SHOW_SETTINGS_SCREEN];
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500482}
483
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400484-(void)allAccountsDeleted
485{
486 [smartViewVC clearConversationModel];
Kateryna Kostiukc17db8d2018-10-23 09:48:03 -0400487 [self changeViewTo:SHOW_WELCOME_SCREEN];
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400488 [self updateRingID];
489}
490
Anthony Léonard8585cc02017-12-28 14:03:45 -0500491-(void)rightPanelClosed
492{
493 [smartViewVC deselect];
Kateryna Kostiuk394f74c2018-10-05 15:45:26 -0400494 [welcomeContainer setHidden:NO];
Anthony Léonard8585cc02017-12-28 14:03:45 -0500495}
496
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500497-(void)currentConversationTrusted
498{
499 [smartViewVC selectConversationList];
500}
501
Anthony Léonardbee94cc2018-01-16 11:42:40 -0500502-(void) listTypeChanged {
Kateryna Kostiukc17db8d2018-10-23 09:48:03 -0400503 [self changeViewTo:SHOW_WELCOME_SCREEN];
Anthony Léonardbee94cc2018-01-16 11:42:40 -0500504}
505
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400506- (IBAction)openAccountSettings:(NSButton *)sender
507{
Kateryna Kostiukc17db8d2018-10-23 09:48:03 -0400508 [self changeViewTo: [settingsVC.view isHidden] ? SHOW_SETTINGS_SCREEN : SHOW_WELCOME_SCREEN];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400509}
510
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400511- (void) createNewAccount {
Kateryna Kostiukc17db8d2018-10-23 09:48:03 -0400512 [self changeViewTo:SHOW_WELCOME_SCREEN];
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400513 wizard = [[RingWizardWC alloc] initWithNibName:@"RingWizard" bundle: nil accountmodel: self.accountModel];
514 [wizard showChooseWithCancelButton: YES andAdvanced: YES];
515 [self.window beginSheet:wizard.window completionHandler:nil];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400516}
517
Kateryna Kostiuk23222fe2018-11-16 14:28:02 -0500518- (NSRect)window:(NSWindow *)window willPositionSheet:(NSWindow *)sheet
519 usingRect:(NSRect)rect
520{
521 float titleBarHeight = self.window.frame.size.height - [NSWindow contentRectForFrameRect:self.window.frame styleMask:self.window.styleMask].size.height;
522 rect.origin.y = self.window.frame.size.height;
523 return rect;
524}
525
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400526-(void) accountSettingsShouldOpen: (BOOL) open {
527 if (open) {
Kateryna Kostiukc17db8d2018-10-23 09:48:03 -0400528 [settingsVC setSelectedAccount: [chooseAccountVC selectedAccount].id];
Kateryna Kostiuk1f8c1252018-07-30 18:18:57 -0400529 }
Kateryna Kostiukd73f9602018-07-24 13:51:28 -0400530}
531
Kateryna Kostiuk133364b2018-10-31 11:36:08 -0400532#pragma mark - CallViewControllerDelegate
533
534-(void) conversationInfoUpdatedFor:(const std::string&) conversationID {
535 [smartViewVC reloadConversationWithUid:@(conversationID.c_str())];
536}
537
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500538@end