blob: dc082a741a078c1744e8a4b7ab43a83d00b3b88f [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>
Alexandre Lision5855b6a2015-02-03 11:31:05 -050043
Alexandre Lision624b1a82016-09-11 19:29:01 -040044// Ring
Alexandre Lision745e4d62015-03-22 20:03:10 -040045#import "AppDelegate.h"
Alexandre Lisionc65310c2015-04-23 16:44:23 -040046#import "Constants.h"
Alexandre Lision58cab672015-06-09 15:25:40 -040047#import "CurrentCallVC.h"
Alexandre Lision624b1a82016-09-11 19:29:01 -040048#import "MigrateRingAccountsWC.h"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050049#import "ConversationVC.h"
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040050#import "PreferencesWC.h"
Alexandre Lisiona3a43dc2017-03-30 16:21:30 -040051#import "SmartViewVC.h"
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -040052#import "views/IconButton.h"
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040053#import "views/NSColor+RingTheme.h"
Alexandre Lisione77f6f92016-04-17 23:39:39 -040054#import "views/BackgroundView.h"
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040055#import "ChooseAccountVC.h"
Alexandre Lision2db8f472015-07-22 15:05:46 -040056
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040057@interface RingWindowController () <MigrateRingAccountsDelegate, NSToolbarDelegate>
Alexandre Lision624b1a82016-09-11 19:29:01 -040058
59@property (retain) MigrateRingAccountsWC* migrateWC;
60
61@end
62
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040063@implementation RingWindowController {
Alexandre Lision5855b6a2015-02-03 11:31:05 -050064
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -040065 __unsafe_unretained IBOutlet NSLayoutConstraint* centerYQRCodeConstraint;
66 __unsafe_unretained IBOutlet NSLayoutConstraint* centerYWelcomeContainerConstraint;
Kateryna Kostiukab499f42018-04-16 12:27:33 -040067 IBOutlet NSLayoutConstraint* ringLabelTrailingConstraint;
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -040068 __unsafe_unretained IBOutlet NSView* welcomeContainer;
Alexandre Lision1abdf582016-02-09 14:21:19 -050069 __unsafe_unretained IBOutlet NSView* callView;
70 __unsafe_unretained IBOutlet NSTextField* ringIDLabel;
71 __unsafe_unretained IBOutlet NSButton* shareButton;
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -040072 __unsafe_unretained IBOutlet NSImageView* qrcodeView;
Alexandre Lision58cab672015-06-09 15:25:40 -040073
Anthony Léonard79597602017-11-13 15:47:09 -050074 std::unique_ptr<lrc::api::Lrc> lrc_;
75
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -040076 PreferencesWC* preferencesWC;
Alexandre Lisiona3a43dc2017-03-30 16:21:30 -040077 IBOutlet SmartViewVC* smartViewVC;
Alexandre Lisione77f6f92016-04-17 23:39:39 -040078
Alexandre Lision0f66bd32016-01-18 11:30:45 -050079 CurrentCallVC* currentCallVC;
Andreas Traczykecdc4fa2018-03-22 16:11:47 -040080 ConversationVC* conversationVC;
81
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040082 // toolbar menu items
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040083 ChooseAccountVC* chooseAccountVC;
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040084}
Alexandre Lision58cab672015-06-09 15:25:40 -040085
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040086static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier";
87NSString* const kChangeAccountToolBarItemIdentifier = @"ChangeAccountToolBarItemIdentifier";
Alexandre Lisionc5148052015-03-04 15:10:35 -050088
Alexandre Lision5855b6a2015-02-03 11:31:05 -050089- (void)windowDidLoad {
90 [super windowDidLoad];
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040091 [self.window setMovableByWindowBackground:YES];
Alexandre Lision58cab672015-06-09 15:25:40 -040092
Alexandre Lisione77f6f92016-04-17 23:39:39 -040093 [self.window setBackgroundColor:[NSColor colorWithRed:242.0/255 green:242.0/255 blue:242.0/255 alpha:1.0]];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040094 self.window.titleVisibility = NSWindowTitleHidden;
Alexandre Lisione77f6f92016-04-17 23:39:39 -040095
Anthony Léonard79597602017-11-13 15:47:09 -050096 lrc_ = std::make_unique<lrc::api::Lrc>();
97
Alexandre Lision0f66bd32016-01-18 11:30:45 -050098 currentCallVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil];
Andreas Traczykecdc4fa2018-03-22 16:11:47 -040099 conversationVC = [[ConversationVC alloc] initWithNibName:@"Conversation" bundle:nil delegate:self];
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -0400100 // toolbar items
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500101 chooseAccountVC = [[ChooseAccountVC alloc] initWithNibName:@"ChooseAccount" bundle:nil model:&(lrc_->getAccountModel()) delegate:self];
Alexandre Lision58cab672015-06-09 15:25:40 -0400102 [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500103 [[currentCallVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400104 [[conversationVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
Alexandre Lision58cab672015-06-09 15:25:40 -0400105
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500106 [callView addSubview:[currentCallVC view] positioned:NSWindowAbove relativeTo:nil];
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400107 [callView addSubview:[conversationVC view] positioned:NSWindowAbove relativeTo:nil];
Alexandre Lision16d9c0a2015-08-10 12:05:15 -0400108
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500109 [currentCallVC initFrame];
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400110 [conversationVC initFrame];
Anthony Léonard72128c92017-12-26 16:48:39 -0500111 @try {
112 [smartViewVC setConversationModel: [chooseAccountVC selectedAccount].conversationModel.get()];
113 }
114 @catch (NSException *ex) {
115 NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
116 }
Alexandre Lisionbb5c2462015-07-30 12:55:37 -0400117
Anthony Léonard49cb2912017-11-13 16:15:39 -0500118 // Fresh run, we need to make sure RingID appears
119 [shareButton sendActionOn:NSLeftMouseDownMask];
120
Olivier Soldano994971f2017-12-05 16:30:12 -0500121 [self connect];
Anthony Léonard49cb2912017-11-13 16:15:39 -0500122 [self updateRingID];
123 // display accounts to select
124 NSToolbar *toolbar = self.window.toolbar;
125 toolbar.delegate = self;
126 [toolbar insertItemWithItemIdentifier:kChangeAccountToolBarItemIdentifier atIndex:1];
Kateryna Kostiuk67735232018-05-10 15:05:32 -0400127 // set download folder (default - 'Downloads')
128 NSString* path = [[NSUserDefaults standardUserDefaults] stringForKey:Preferences::DownloadFolder];
129 if (!path || path.length == 0) {
130 NSURL *downloadsURL = [[NSFileManager defaultManager]
131 URLForDirectory:NSDownloadsDirectory
132 inDomain:NSUserDomainMask appropriateForURL:nil
133 create:YES error:nil];
134 path = [[downloadsURL path] stringByAppendingString:@"/"];
135 }
136 lrc_->getDataTransferModel().downloadDirectory = std::string([path UTF8String]);
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500137}
138
139- (void) connect
140{
Olivier Soldano994971f2017-12-05 16:30:12 -0500141 QObject::connect(&lrc_->getBehaviorController(),
142 &lrc::api::BehaviorController::showCallView,
143 [self](const std::string accountId,
144 const lrc::api::conversation::Info convInfo){
145 auto* accInfo = &lrc_->getAccountModel().getAccountInfo(accountId);
Anthony Léonardd00cd182018-01-17 09:21:27 -0500146 if (accInfo->contactModel->getContact(convInfo.participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING)
147 [smartViewVC selectPendingList];
148 else
149 [smartViewVC selectConversationList];
150
Olivier Soldano994971f2017-12-05 16:30:12 -0500151 [currentCallVC setCurrentCall:convInfo.callId
152 conversation:convInfo.uid
153 account:accInfo];
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500154 [smartViewVC selectConversation: convInfo model:accInfo->conversationModel.get()];
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400155 [currentCallVC showWithAnimation:false];
156 [conversationVC hideWithAnimation:false];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500157 });
Alexandre Lision210fe212016-01-27 11:15:13 -0500158
Olivier Soldano994971f2017-12-05 16:30:12 -0500159 QObject::connect(&lrc_->getBehaviorController(),
160 &lrc::api::BehaviorController::showIncomingCallView,
161 [self](const std::string accountId,
162 const lrc::api::conversation::Info convInfo){
163 auto* accInfo = &lrc_->getAccountModel().getAccountInfo(accountId);
Anthony Léonardd00cd182018-01-17 09:21:27 -0500164 if (accInfo->contactModel->getContact(convInfo.participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING)
165 [smartViewVC selectPendingList];
166 else
167 [smartViewVC selectConversationList];
168
Olivier Soldano994971f2017-12-05 16:30:12 -0500169 [currentCallVC setCurrentCall:convInfo.callId
170 conversation:convInfo.uid
171 account:accInfo];
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500172 [smartViewVC selectConversation: convInfo model:accInfo->conversationModel.get()];
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400173 [currentCallVC showWithAnimation:false];
174 [conversationVC hideWithAnimation:false];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400175 });
Anthony Léonard2382b562017-12-13 15:51:28 -0500176
177 QObject::connect(&lrc_->getBehaviorController(),
178 &lrc::api::BehaviorController::showChatView,
179 [self](const std::string& accountId,
180 const lrc::api::conversation::Info& convInfo){
181 auto& accInfo = lrc_->getAccountModel().getAccountInfo(accountId);
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400182 [conversationVC setConversationUid:convInfo.uid model:accInfo.conversationModel.get()];
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500183 [smartViewVC selectConversation: convInfo model:accInfo.conversationModel.get()];
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400184 [conversationVC showWithAnimation:false];
185 [currentCallVC hideWithAnimation:false];
Anthony Léonard2382b562017-12-13 15:51:28 -0500186 });
Alexandre Lisionbb5c2462015-07-30 12:55:37 -0400187}
188
189/**
190 * Implement the necessary logic to choose which Ring ID to display.
191 * This tries to choose the "best" ID to show
192 */
193- (void) updateRingID
194{
Anthony Léonard72128c92017-12-26 16:48:39 -0500195 @try {
196 auto& account = [chooseAccountVC selectedAccount];
Alexandre Lisionbb5c2462015-07-30 12:55:37 -0400197
Anthony Léonard72128c92017-12-26 16:48:39 -0500198 [ringIDLabel setStringValue:@""];
Anthony Léonard49cb2912017-11-13 16:15:39 -0500199
Anthony Léonard72128c92017-12-26 16:48:39 -0500200 if(account.profileInfo.type != lrc::api::profile::Type::RING) {
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400201 self.notRingAccount = YES;
202 self.isSIPAccount = YES;
Anthony Léonard72128c92017-12-26 16:48:39 -0500203 return;
204 }
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400205 self.isSIPAccount = NO;
206 self.notRingAccount = NO;
207 [ringLabelTrailingConstraint setActive:YES];
Anthony Léonard72128c92017-12-26 16:48:39 -0500208 auto& registeredName = account.registeredName;
209 auto& ringID = account.profileInfo.uri;
210 NSString* uriToDisplay = nullptr;
211 if (!registeredName.empty()) {
212 uriToDisplay = @(registeredName.c_str());
213 } else {
214 uriToDisplay = @(ringID.c_str());
215 }
216 [ringIDLabel setStringValue:uriToDisplay];
217 [self drawQRCode:@(ringID.c_str())];
Alexandre Lisionbb5c2462015-07-30 12:55:37 -0400218 }
Anthony Léonard72128c92017-12-26 16:48:39 -0500219 @catch (NSException *ex) {
220 NSLog(@"Caught exception %@: %@", [ex name], [ex reason]);
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400221 self.notRingAccount = YES;
222 self.isSIPAccount = NO;
223 [ringLabelTrailingConstraint setActive:NO];
Anthony Léonard70638f02018-02-05 11:10:19 -0500224 [ringIDLabel setStringValue:NSLocalizedString(@"No account available", @"Displayed as RingID when no accounts are available for selection")];
Alexandre Lision313427f2016-11-24 21:04:04 -0500225 }
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500226}
227
Alexandre Lision1abdf582016-02-09 14:21:19 -0500228- (IBAction)shareRingID:(id)sender {
229 NSSharingServicePicker* sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:[NSArray arrayWithObject:[ringIDLabel stringValue]]];
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400230 [sharingServicePicker setDelegate:self];
Alexandre Lision1abdf582016-02-09 14:21:19 -0500231 [sharingServicePicker showRelativeToRect:[sender bounds]
232 ofView:sender
233 preferredEdge:NSMinYEdge];
234}
235
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400236- (IBAction)toggleQRCode:(id)sender {
237 // Toggle pressed state of QRCode button
238 [sender setPressed:![sender isPressed]];
Kateryna Kostiuk65ba43e2017-03-30 15:10:04 -0400239 [self showQRCode:[sender isPressed]];
Alexandre Lision313427f2016-11-24 21:04:04 -0500240}
241
242/**
243 * Draw the QRCode in the qrCodeView
244 */
Kateryna Kostiuk65ba43e2017-03-30 15:10:04 -0400245- (void)drawQRCode:(NSString*) uriToDraw
Alexandre Lision313427f2016-11-24 21:04:04 -0500246{
Kateryna Kostiuk65ba43e2017-03-30 15:10:04 -0400247 auto qrCode = QRcode_encodeString(uriToDraw.UTF8String,
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400248 0,
249 QR_ECLEVEL_L, // Lowest level of error correction
250 QR_MODE_8, // 8-bit data mode
251 1);
252 if (!qrCode) {
253 return;
254 }
255
Alexandre Lision313427f2016-11-24 21:04:04 -0500256 unsigned char *data = 0;
257 int width;
258 data = qrCode->data;
259 width = qrCode->width;
260 int qr_margin = 3;
261
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400262 CGFloat size = qrcodeView.frame.size.width;
263
264 // create context
265 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
266 CGContextRef ctx = CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace, kCGImageAlphaPremultipliedLast);
267
268 CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -size);
269 CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1);
270 CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform));
271
Alexandre Lision313427f2016-11-24 21:04:04 -0500272 float zoom = ceil((double)size / (qrCode->width + 2.0 * qr_margin));
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400273 CGRect rectDraw = CGRectMake(0, 0, zoom, zoom);
274
275 int ran;
276 for(int i = 0; i < width; ++i) {
277 for(int j = 0; j < width; ++j) {
278 if(*data & 1) {
279 CGContextSetFillColorWithColor(ctx, [NSColor ringDarkGrey].CGColor);
280 rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom);
281 CGContextAddRect(ctx, rectDraw);
282 CGContextFillPath(ctx);
283 } else {
284 CGContextSetFillColorWithColor(ctx, [NSColor windowBackgroundColor].CGColor);
285 rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom);
286 CGContextAddRect(ctx, rectDraw);
287 CGContextFillPath(ctx);
288 }
289 ++data;
290 }
291 }
Alexandre Lision313427f2016-11-24 21:04:04 -0500292
293 // get image
294 auto qrCGImage = CGBitmapContextCreateImage(ctx);
295 auto qrImage = [[NSImage alloc] initWithCGImage:qrCGImage size:qrcodeView.frame.size];
296
297 // some releases
298 CGContextRelease(ctx);
299 CGImageRelease(qrCGImage);
300 CGColorSpaceRelease(colorSpace);
301 QRcode_free(qrCode);
302
303 [qrcodeView setImage:qrImage];
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400304}
305
306/**
307 * Start the in/out animation displaying the QRCode
308 * @param show should the QRCode be animated in or out
309 */
310- (void) showQRCode:(BOOL) show
311{
312 static const NSInteger offset = 110;
313 [NSAnimationContext beginGrouping];
314 NSAnimationContext.currentContext.duration = 0.5;
315 [[NSAnimationContext currentContext] setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
316 qrcodeView.animator.alphaValue = show ? 1.0 : 0.0;
317 [centerYQRCodeConstraint.animator setConstant: show ? offset : 0];
318 [centerYWelcomeContainerConstraint.animator setConstant:show ? -offset : 0];
319 [NSAnimationContext endGrouping];
320}
321
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500322- (IBAction)openPreferences:(id)sender
323{
Kateryna Kostiuk67735232018-05-10 15:05:32 -0400324 preferencesWC = [[PreferencesWC alloc] initWithNibName:@"PreferencesWindow" bundle: nil model:&(lrc_->getDataTransferModel())];
Alexandre Lisionbfa68f62015-09-10 08:38:42 -0400325 [preferencesWC.window makeKeyAndOrderFront:preferencesWC.window];
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500326}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500327
Kateryna Kostiuk882cbac2017-07-05 17:29:00 -0400328- (IBAction)callClickedAtRow:(id)sender
329{
330 NSTabViewItem *selectedTab = [smartViewVC.tabbar selectedTabViewItem];
331 int index = [smartViewVC.tabbar indexOfTabViewItem:selectedTab];
332 switch (index) {
333 case 0:
334 [smartViewVC startCallForRow:sender];
335 break;
Kateryna Kostiuk882cbac2017-07-05 17:29:00 -0400336 default:
337 break;
338 }
339}
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500340
Alexandre Lision624b1a82016-09-11 19:29:01 -0400341#pragma mark - Ring account migration
342
343- (void) migrateRingAccount:(Account*) acc
344{
345 self.migrateWC = [[MigrateRingAccountsWC alloc] initWithDelegate:self actionCode:1];
346 self.migrateWC.account = acc;
347#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
348 [self.window beginSheet:self.migrateWC.window completionHandler:nil];
349#else
350 [NSApp beginSheet: self.migrateWC.window
351 modalForWindow: self.window
352 modalDelegate: self
353 didEndSelector: nil
354 contextInfo: nil];
355#endif
356}
357
Anthony Léonard49cb2912017-11-13 16:15:39 -0500358// TODO: Reimplement as a blocking loop when new LRC models handle migration
Alexandre Lision624b1a82016-09-11 19:29:01 -0400359- (void)checkAccountsToMigrate
360{
361 auto ringList = AccountModel::instance().accountsToMigrate();
362 if (ringList.length() > 0){
363 Account* acc = ringList.value(0);
364 [self migrateRingAccount:acc];
365 } else {
366 // Fresh run, we need to make sure RingID appears
Alexandre Lision624b1a82016-09-11 19:29:01 -0400367 [shareButton sendActionOn:NSLeftMouseDownMask];
368
369 [self connect];
Kateryna Kostiuka7248462017-04-18 16:15:31 -0400370 [self updateRingID];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400371 // display accounts to select
372 NSToolbar *toolbar = self.window.toolbar;
373 toolbar.delegate = self;
374 [toolbar insertItemWithItemIdentifier:kChangeAccountToolBarItemIdentifier atIndex:1];
Alexandre Lision624b1a82016-09-11 19:29:01 -0400375 }
376}
377
378- (void)migrationDidComplete
379{
380 [self checkAccountsToMigrate];
381}
382
383- (void)migrationDidCompleteWithError
384{
385 [self checkAccountsToMigrate];
386}
387
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500388-(void)selectAccount:(const lrc::api::account::Info&)accInfo
389{
390 // If the selected account has been changed, we close any open panel
391 if ([smartViewVC setConversationModel:accInfo.conversationModel.get()]) {
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400392 [currentCallVC hideWithAnimation:false];
393 [conversationVC hideWithAnimation:false];
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500394 }
Anthony Léonard61e3dcf2017-12-27 12:19:52 -0500395
396 // Welcome view informations are also updated
397 [self updateRingID];
Anthony Léonard9bebf1d2017-12-21 14:33:51 -0500398}
399
Kateryna Kostiukab499f42018-04-16 12:27:33 -0400400-(void)allAccountsDeleted
401{
402 [smartViewVC clearConversationModel];
403 [currentCallVC hideWithAnimation:false];
404 [conversationVC hideWithAnimation:false];
405 [self updateRingID];
406}
407
Anthony Léonard8585cc02017-12-28 14:03:45 -0500408-(void)rightPanelClosed
409{
410 [smartViewVC deselect];
411}
412
Anthony Léonard0a9904c2018-01-11 16:43:47 -0500413-(void)currentConversationTrusted
414{
415 [smartViewVC selectConversationList];
416}
417
Anthony Léonardbee94cc2018-01-16 11:42:40 -0500418-(void) listTypeChanged {
Andreas Traczykecdc4fa2018-03-22 16:11:47 -0400419 [conversationVC hideWithAnimation:false];
420 [currentCallVC hideWithAnimation:false];
Anthony Léonardbee94cc2018-01-16 11:42:40 -0500421}
422
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400423#pragma mark - NSToolbarDelegate
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -0400424- (nullable NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
425{
426 if(itemIdentifier == kChangeAccountToolBarItemIdentifier) {
427 NSToolbarItem *toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:kChangeAccountToolBarItemIdentifier];
428 toolbarItem.view = chooseAccountVC.view;
429 return toolbarItem;
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400430 }
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -0400431 return nil;
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400432}
433
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500434@end