Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 1 | /* |
Alexandre Lision | 9fe374b | 2016-01-06 10:17:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2015-2016 Savoir-faire Linux Inc. |
Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 3 | * 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 Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 18 | */ |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 19 | #import "RingWindowController.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 20 | #import <QuartzCore/QuartzCore.h> |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 21 | #include <qrencode.h> |
Anthony Léonard | 7959760 | 2017-11-13 15:47:09 -0500 | [diff] [blame] | 22 | #include <memory> |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 23 | |
| 24 | //Qt |
| 25 | #import <QItemSelectionModel> |
| 26 | #import <QItemSelection> |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 27 | |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 28 | //LRC |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 29 | #import <callmodel.h> |
| 30 | #import <account.h> |
Alexandre Lision | 91d11e5 | 2015-03-20 17:42:05 -0400 | [diff] [blame] | 31 | #import <call.h> |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 32 | #import <recentmodel.h> |
Kateryna Kostiuk | 312f913 | 2017-04-18 12:09:06 -0400 | [diff] [blame] | 33 | #import <AvailableAccountModel.h> |
Anthony Léonard | 7959760 | 2017-11-13 15:47:09 -0500 | [diff] [blame] | 34 | #import <api/lrc.h> |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 35 | #import <api/account.h> |
Olivier Soldano | 994971f | 2017-12-05 16:30:12 -0500 | [diff] [blame] | 36 | #import <api/newaccountmodel.h> |
| 37 | #import <api/newcallmodel.h> |
| 38 | #import <api/behaviorcontroller.h> |
| 39 | #import <api/conversation.h> |
Anthony Léonard | d00cd18 | 2018-01-17 09:21:27 -0500 | [diff] [blame] | 40 | #import <api/contactmodel.h> |
| 41 | #import <api/contact.h> |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 42 | #import <api/datatransfermodel.h> |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 43 | |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame] | 44 | // Ring |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 45 | #import "AppDelegate.h" |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 46 | #import "Constants.h" |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 47 | #import "CurrentCallVC.h" |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame] | 48 | #import "MigrateRingAccountsWC.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 49 | #import "ConversationVC.h" |
Alexandre Lision | bfa68f6 | 2015-09-10 08:38:42 -0400 | [diff] [blame] | 50 | #import "PreferencesWC.h" |
Alexandre Lision | a3a43dc | 2017-03-30 16:21:30 -0400 | [diff] [blame] | 51 | #import "SmartViewVC.h" |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 52 | #import "views/IconButton.h" |
Alexandre Lision | bfa68f6 | 2015-09-10 08:38:42 -0400 | [diff] [blame] | 53 | #import "views/NSColor+RingTheme.h" |
Alexandre Lision | e77f6f9 | 2016-04-17 23:39:39 -0400 | [diff] [blame] | 54 | #import "views/BackgroundView.h" |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 55 | #import "ChooseAccountVC.h" |
Alexandre Lision | 2db8f47 | 2015-07-22 15:05:46 -0400 | [diff] [blame] | 56 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 57 | @interface RingWindowController () <MigrateRingAccountsDelegate, NSToolbarDelegate> |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame] | 58 | |
| 59 | @property (retain) MigrateRingAccountsWC* migrateWC; |
| 60 | |
| 61 | @end |
| 62 | |
Alexandre Lision | bfa68f6 | 2015-09-10 08:38:42 -0400 | [diff] [blame] | 63 | @implementation RingWindowController { |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 64 | |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 65 | __unsafe_unretained IBOutlet NSLayoutConstraint* centerYQRCodeConstraint; |
| 66 | __unsafe_unretained IBOutlet NSLayoutConstraint* centerYWelcomeContainerConstraint; |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 67 | IBOutlet NSLayoutConstraint* ringLabelTrailingConstraint; |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 68 | __unsafe_unretained IBOutlet NSView* welcomeContainer; |
Alexandre Lision | 1abdf58 | 2016-02-09 14:21:19 -0500 | [diff] [blame] | 69 | __unsafe_unretained IBOutlet NSView* callView; |
| 70 | __unsafe_unretained IBOutlet NSTextField* ringIDLabel; |
| 71 | __unsafe_unretained IBOutlet NSButton* shareButton; |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 72 | __unsafe_unretained IBOutlet NSImageView* qrcodeView; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 73 | |
Anthony Léonard | 7959760 | 2017-11-13 15:47:09 -0500 | [diff] [blame] | 74 | std::unique_ptr<lrc::api::Lrc> lrc_; |
| 75 | |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 76 | PreferencesWC* preferencesWC; |
Alexandre Lision | a3a43dc | 2017-03-30 16:21:30 -0400 | [diff] [blame] | 77 | IBOutlet SmartViewVC* smartViewVC; |
Alexandre Lision | e77f6f9 | 2016-04-17 23:39:39 -0400 | [diff] [blame] | 78 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 79 | CurrentCallVC* currentCallVC; |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 80 | ConversationVC* conversationVC; |
| 81 | |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 82 | // toolbar menu items |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 83 | ChooseAccountVC* chooseAccountVC; |
Alexandre Lision | bfa68f6 | 2015-09-10 08:38:42 -0400 | [diff] [blame] | 84 | } |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 85 | |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 86 | static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier"; |
| 87 | NSString* const kChangeAccountToolBarItemIdentifier = @"ChangeAccountToolBarItemIdentifier"; |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 88 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 89 | - (void)windowDidLoad { |
| 90 | [super windowDidLoad]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 91 | [self.window setMovableByWindowBackground:YES]; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 92 | |
Alexandre Lision | e77f6f9 | 2016-04-17 23:39:39 -0400 | [diff] [blame] | 93 | [self.window setBackgroundColor:[NSColor colorWithRed:242.0/255 green:242.0/255 blue:242.0/255 alpha:1.0]]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 94 | self.window.titleVisibility = NSWindowTitleHidden; |
Alexandre Lision | e77f6f9 | 2016-04-17 23:39:39 -0400 | [diff] [blame] | 95 | |
Anthony Léonard | 7959760 | 2017-11-13 15:47:09 -0500 | [diff] [blame] | 96 | lrc_ = std::make_unique<lrc::api::Lrc>(); |
| 97 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 98 | currentCallVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil]; |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 99 | conversationVC = [[ConversationVC alloc] initWithNibName:@"Conversation" bundle:nil delegate:self]; |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 100 | // toolbar items |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 101 | chooseAccountVC = [[ChooseAccountVC alloc] initWithNibName:@"ChooseAccount" bundle:nil model:&(lrc_->getAccountModel()) delegate:self]; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 102 | [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 103 | [[currentCallVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 104 | [[conversationVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 105 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 106 | [callView addSubview:[currentCallVC view] positioned:NSWindowAbove relativeTo:nil]; |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 107 | [callView addSubview:[conversationVC view] positioned:NSWindowAbove relativeTo:nil]; |
Alexandre Lision | 16d9c0a | 2015-08-10 12:05:15 -0400 | [diff] [blame] | 108 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 109 | [currentCallVC initFrame]; |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 110 | [conversationVC initFrame]; |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 111 | @try { |
| 112 | [smartViewVC setConversationModel: [chooseAccountVC selectedAccount].conversationModel.get()]; |
| 113 | } |
| 114 | @catch (NSException *ex) { |
| 115 | NSLog(@"Caught exception %@: %@", [ex name], [ex reason]); |
| 116 | } |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 117 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 118 | // Fresh run, we need to make sure RingID appears |
| 119 | [shareButton sendActionOn:NSLeftMouseDownMask]; |
| 120 | |
Olivier Soldano | 994971f | 2017-12-05 16:30:12 -0500 | [diff] [blame] | 121 | [self connect]; |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 122 | [self updateRingID]; |
| 123 | // display accounts to select |
| 124 | NSToolbar *toolbar = self.window.toolbar; |
| 125 | toolbar.delegate = self; |
| 126 | [toolbar insertItemWithItemIdentifier:kChangeAccountToolBarItemIdentifier atIndex:1]; |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 127 | // 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 Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | - (void) connect |
| 140 | { |
Olivier Soldano | 994971f | 2017-12-05 16:30:12 -0500 | [diff] [blame] | 141 | 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éonard | d00cd18 | 2018-01-17 09:21:27 -0500 | [diff] [blame] | 146 | if (accInfo->contactModel->getContact(convInfo.participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING) |
| 147 | [smartViewVC selectPendingList]; |
| 148 | else |
| 149 | [smartViewVC selectConversationList]; |
| 150 | |
Olivier Soldano | 994971f | 2017-12-05 16:30:12 -0500 | [diff] [blame] | 151 | [currentCallVC setCurrentCall:convInfo.callId |
| 152 | conversation:convInfo.uid |
| 153 | account:accInfo]; |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 154 | [smartViewVC selectConversation: convInfo model:accInfo->conversationModel.get()]; |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 155 | [currentCallVC showWithAnimation:false]; |
| 156 | [conversationVC hideWithAnimation:false]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 157 | }); |
Alexandre Lision | 210fe21 | 2016-01-27 11:15:13 -0500 | [diff] [blame] | 158 | |
Olivier Soldano | 994971f | 2017-12-05 16:30:12 -0500 | [diff] [blame] | 159 | 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éonard | d00cd18 | 2018-01-17 09:21:27 -0500 | [diff] [blame] | 164 | if (accInfo->contactModel->getContact(convInfo.participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING) |
| 165 | [smartViewVC selectPendingList]; |
| 166 | else |
| 167 | [smartViewVC selectConversationList]; |
| 168 | |
Olivier Soldano | 994971f | 2017-12-05 16:30:12 -0500 | [diff] [blame] | 169 | [currentCallVC setCurrentCall:convInfo.callId |
| 170 | conversation:convInfo.uid |
| 171 | account:accInfo]; |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 172 | [smartViewVC selectConversation: convInfo model:accInfo->conversationModel.get()]; |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 173 | [currentCallVC showWithAnimation:false]; |
| 174 | [conversationVC hideWithAnimation:false]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 175 | }); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 176 | |
| 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 Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 182 | [conversationVC setConversationUid:convInfo.uid model:accInfo.conversationModel.get()]; |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 183 | [smartViewVC selectConversation: convInfo model:accInfo.conversationModel.get()]; |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 184 | [conversationVC showWithAnimation:false]; |
| 185 | [currentCallVC hideWithAnimation:false]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 186 | }); |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 187 | } |
| 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éonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 195 | @try { |
| 196 | auto& account = [chooseAccountVC selectedAccount]; |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 197 | |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 198 | [ringIDLabel setStringValue:@""]; |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 199 | |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 200 | if(account.profileInfo.type != lrc::api::profile::Type::RING) { |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 201 | self.notRingAccount = YES; |
| 202 | self.isSIPAccount = YES; |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 203 | return; |
| 204 | } |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 205 | self.isSIPAccount = NO; |
| 206 | self.notRingAccount = NO; |
| 207 | [ringLabelTrailingConstraint setActive:YES]; |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 208 | 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 Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 218 | } |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 219 | @catch (NSException *ex) { |
| 220 | NSLog(@"Caught exception %@: %@", [ex name], [ex reason]); |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 221 | self.notRingAccount = YES; |
| 222 | self.isSIPAccount = NO; |
| 223 | [ringLabelTrailingConstraint setActive:NO]; |
Anthony Léonard | 70638f0 | 2018-02-05 11:10:19 -0500 | [diff] [blame] | 224 | [ringIDLabel setStringValue:NSLocalizedString(@"No account available", @"Displayed as RingID when no accounts are available for selection")]; |
Alexandre Lision | 313427f | 2016-11-24 21:04:04 -0500 | [diff] [blame] | 225 | } |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 226 | } |
| 227 | |
Alexandre Lision | 1abdf58 | 2016-02-09 14:21:19 -0500 | [diff] [blame] | 228 | - (IBAction)shareRingID:(id)sender { |
| 229 | NSSharingServicePicker* sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:[NSArray arrayWithObject:[ringIDLabel stringValue]]]; |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 230 | [sharingServicePicker setDelegate:self]; |
Alexandre Lision | 1abdf58 | 2016-02-09 14:21:19 -0500 | [diff] [blame] | 231 | [sharingServicePicker showRelativeToRect:[sender bounds] |
| 232 | ofView:sender |
| 233 | preferredEdge:NSMinYEdge]; |
| 234 | } |
| 235 | |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 236 | - (IBAction)toggleQRCode:(id)sender { |
| 237 | // Toggle pressed state of QRCode button |
| 238 | [sender setPressed:![sender isPressed]]; |
Kateryna Kostiuk | 65ba43e | 2017-03-30 15:10:04 -0400 | [diff] [blame] | 239 | [self showQRCode:[sender isPressed]]; |
Alexandre Lision | 313427f | 2016-11-24 21:04:04 -0500 | [diff] [blame] | 240 | } |
| 241 | |
| 242 | /** |
| 243 | * Draw the QRCode in the qrCodeView |
| 244 | */ |
Kateryna Kostiuk | 65ba43e | 2017-03-30 15:10:04 -0400 | [diff] [blame] | 245 | - (void)drawQRCode:(NSString*) uriToDraw |
Alexandre Lision | 313427f | 2016-11-24 21:04:04 -0500 | [diff] [blame] | 246 | { |
Kateryna Kostiuk | 65ba43e | 2017-03-30 15:10:04 -0400 | [diff] [blame] | 247 | auto qrCode = QRcode_encodeString(uriToDraw.UTF8String, |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 248 | 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 Lision | 313427f | 2016-11-24 21:04:04 -0500 | [diff] [blame] | 256 | unsigned char *data = 0; |
| 257 | int width; |
| 258 | data = qrCode->data; |
| 259 | width = qrCode->width; |
| 260 | int qr_margin = 3; |
| 261 | |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 262 | 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 Lision | 313427f | 2016-11-24 21:04:04 -0500 | [diff] [blame] | 272 | float zoom = ceil((double)size / (qrCode->width + 2.0 * qr_margin)); |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 273 | 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 Lision | 313427f | 2016-11-24 21:04:04 -0500 | [diff] [blame] | 292 | |
| 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 Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 304 | } |
| 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 Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 322 | - (IBAction)openPreferences:(id)sender |
| 323 | { |
Kateryna Kostiuk | 6773523 | 2018-05-10 15:05:32 -0400 | [diff] [blame] | 324 | preferencesWC = [[PreferencesWC alloc] initWithNibName:@"PreferencesWindow" bundle: nil model:&(lrc_->getDataTransferModel())]; |
Alexandre Lision | bfa68f6 | 2015-09-10 08:38:42 -0400 | [diff] [blame] | 325 | [preferencesWC.window makeKeyAndOrderFront:preferencesWC.window]; |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 326 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 327 | |
Kateryna Kostiuk | 882cbac | 2017-07-05 17:29:00 -0400 | [diff] [blame] | 328 | - (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 Kostiuk | 882cbac | 2017-07-05 17:29:00 -0400 | [diff] [blame] | 336 | default: |
| 337 | break; |
| 338 | } |
| 339 | } |
Anthony Léonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 340 | |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame] | 341 | #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éonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 358 | // TODO: Reimplement as a blocking loop when new LRC models handle migration |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame] | 359 | - (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 Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame] | 367 | [shareButton sendActionOn:NSLeftMouseDownMask]; |
| 368 | |
| 369 | [self connect]; |
Kateryna Kostiuk | a724846 | 2017-04-18 16:15:31 -0400 | [diff] [blame] | 370 | [self updateRingID]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 371 | // display accounts to select |
| 372 | NSToolbar *toolbar = self.window.toolbar; |
| 373 | toolbar.delegate = self; |
| 374 | [toolbar insertItemWithItemIdentifier:kChangeAccountToolBarItemIdentifier atIndex:1]; |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame] | 375 | } |
| 376 | } |
| 377 | |
| 378 | - (void)migrationDidComplete |
| 379 | { |
| 380 | [self checkAccountsToMigrate]; |
| 381 | } |
| 382 | |
| 383 | - (void)migrationDidCompleteWithError |
| 384 | { |
| 385 | [self checkAccountsToMigrate]; |
| 386 | } |
| 387 | |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 388 | -(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 Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 392 | [currentCallVC hideWithAnimation:false]; |
| 393 | [conversationVC hideWithAnimation:false]; |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 394 | } |
Anthony Léonard | 61e3dcf | 2017-12-27 12:19:52 -0500 | [diff] [blame] | 395 | |
| 396 | // Welcome view informations are also updated |
| 397 | [self updateRingID]; |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 398 | } |
| 399 | |
Kateryna Kostiuk | ab499f4 | 2018-04-16 12:27:33 -0400 | [diff] [blame] | 400 | -(void)allAccountsDeleted |
| 401 | { |
| 402 | [smartViewVC clearConversationModel]; |
| 403 | [currentCallVC hideWithAnimation:false]; |
| 404 | [conversationVC hideWithAnimation:false]; |
| 405 | [self updateRingID]; |
| 406 | } |
| 407 | |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 408 | -(void)rightPanelClosed |
| 409 | { |
| 410 | [smartViewVC deselect]; |
| 411 | } |
| 412 | |
Anthony Léonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 413 | -(void)currentConversationTrusted |
| 414 | { |
| 415 | [smartViewVC selectConversationList]; |
| 416 | } |
| 417 | |
Anthony Léonard | bee94cc | 2018-01-16 11:42:40 -0500 | [diff] [blame] | 418 | -(void) listTypeChanged { |
Andreas Traczyk | ecdc4fa | 2018-03-22 16:11:47 -0400 | [diff] [blame] | 419 | [conversationVC hideWithAnimation:false]; |
| 420 | [currentCallVC hideWithAnimation:false]; |
Anthony Léonard | bee94cc | 2018-01-16 11:42:40 -0500 | [diff] [blame] | 421 | } |
| 422 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 423 | #pragma mark - NSToolbarDelegate |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 424 | - (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 Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 430 | } |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 431 | return nil; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 432 | } |
| 433 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 434 | @end |