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> |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 42 | |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame] | 43 | // Ring |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 44 | #import "AppDelegate.h" |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 45 | #import "Constants.h" |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 46 | #import "CurrentCallVC.h" |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame] | 47 | #import "MigrateRingAccountsWC.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 48 | #import "ConversationVC.h" |
Alexandre Lision | bfa68f6 | 2015-09-10 08:38:42 -0400 | [diff] [blame] | 49 | #import "PreferencesWC.h" |
Alexandre Lision | a3a43dc | 2017-03-30 16:21:30 -0400 | [diff] [blame] | 50 | #import "SmartViewVC.h" |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 51 | #import "views/IconButton.h" |
Alexandre Lision | bfa68f6 | 2015-09-10 08:38:42 -0400 | [diff] [blame] | 52 | #import "views/NSColor+RingTheme.h" |
Alexandre Lision | e77f6f9 | 2016-04-17 23:39:39 -0400 | [diff] [blame] | 53 | #import "views/BackgroundView.h" |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 54 | #import "ChooseAccountVC.h" |
Alexandre Lision | 2db8f47 | 2015-07-22 15:05:46 -0400 | [diff] [blame] | 55 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 56 | @interface RingWindowController () <MigrateRingAccountsDelegate, NSToolbarDelegate> |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame] | 57 | |
| 58 | @property (retain) MigrateRingAccountsWC* migrateWC; |
| 59 | |
| 60 | @end |
| 61 | |
Alexandre Lision | bfa68f6 | 2015-09-10 08:38:42 -0400 | [diff] [blame] | 62 | @implementation RingWindowController { |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 63 | |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 64 | __unsafe_unretained IBOutlet NSLayoutConstraint* centerYQRCodeConstraint; |
| 65 | __unsafe_unretained IBOutlet NSLayoutConstraint* centerYWelcomeContainerConstraint; |
| 66 | __unsafe_unretained IBOutlet NSView* welcomeContainer; |
Alexandre Lision | 1abdf58 | 2016-02-09 14:21:19 -0500 | [diff] [blame] | 67 | __unsafe_unretained IBOutlet NSView* callView; |
| 68 | __unsafe_unretained IBOutlet NSTextField* ringIDLabel; |
| 69 | __unsafe_unretained IBOutlet NSButton* shareButton; |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 70 | __unsafe_unretained IBOutlet NSImageView* qrcodeView; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 71 | |
Anthony Léonard | 7959760 | 2017-11-13 15:47:09 -0500 | [diff] [blame] | 72 | std::unique_ptr<lrc::api::Lrc> lrc_; |
| 73 | |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 74 | PreferencesWC* preferencesWC; |
Alexandre Lision | a3a43dc | 2017-03-30 16:21:30 -0400 | [diff] [blame] | 75 | IBOutlet SmartViewVC* smartViewVC; |
Alexandre Lision | e77f6f9 | 2016-04-17 23:39:39 -0400 | [diff] [blame] | 76 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 77 | CurrentCallVC* currentCallVC; |
| 78 | ConversationVC* offlineVC; |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 79 | // toolbar menu items |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 80 | ChooseAccountVC* chooseAccountVC; |
Alexandre Lision | bfa68f6 | 2015-09-10 08:38:42 -0400 | [diff] [blame] | 81 | } |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 82 | |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 83 | static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier"; |
| 84 | NSString* const kChangeAccountToolBarItemIdentifier = @"ChangeAccountToolBarItemIdentifier"; |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 85 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 86 | - (void)windowDidLoad { |
| 87 | [super windowDidLoad]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 88 | [self.window setMovableByWindowBackground:YES]; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 89 | |
Alexandre Lision | e77f6f9 | 2016-04-17 23:39:39 -0400 | [diff] [blame] | 90 | [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] | 91 | self.window.titleVisibility = NSWindowTitleHidden; |
Alexandre Lision | e77f6f9 | 2016-04-17 23:39:39 -0400 | [diff] [blame] | 92 | |
Anthony Léonard | 7959760 | 2017-11-13 15:47:09 -0500 | [diff] [blame] | 93 | lrc_ = std::make_unique<lrc::api::Lrc>(); |
| 94 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 95 | currentCallVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil]; |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 96 | offlineVC = [[ConversationVC alloc] initWithNibName:@"Conversation" bundle:nil delegate:self]; |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 97 | // toolbar items |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 98 | chooseAccountVC = [[ChooseAccountVC alloc] initWithNibName:@"ChooseAccount" bundle:nil model:&(lrc_->getAccountModel()) delegate:self]; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 99 | [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 100 | [[currentCallVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
| 101 | [[offlineVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 102 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 103 | [callView addSubview:[currentCallVC view] positioned:NSWindowAbove relativeTo:nil]; |
| 104 | [callView addSubview:[offlineVC view] positioned:NSWindowAbove relativeTo:nil]; |
Alexandre Lision | 16d9c0a | 2015-08-10 12:05:15 -0400 | [diff] [blame] | 105 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 106 | [currentCallVC initFrame]; |
| 107 | [offlineVC initFrame]; |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 108 | @try { |
| 109 | [smartViewVC setConversationModel: [chooseAccountVC selectedAccount].conversationModel.get()]; |
| 110 | } |
| 111 | @catch (NSException *ex) { |
| 112 | NSLog(@"Caught exception %@: %@", [ex name], [ex reason]); |
| 113 | } |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 114 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 115 | // Fresh run, we need to make sure RingID appears |
| 116 | [shareButton sendActionOn:NSLeftMouseDownMask]; |
| 117 | |
Olivier Soldano | 994971f | 2017-12-05 16:30:12 -0500 | [diff] [blame] | 118 | [self connect]; |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 119 | [self updateRingID]; |
| 120 | // display accounts to select |
| 121 | NSToolbar *toolbar = self.window.toolbar; |
| 122 | toolbar.delegate = self; |
| 123 | [toolbar insertItemWithItemIdentifier:kChangeAccountToolBarItemIdentifier atIndex:1]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 124 | } |
| 125 | |
| 126 | - (void) connect |
| 127 | { |
Olivier Soldano | 994971f | 2017-12-05 16:30:12 -0500 | [diff] [blame] | 128 | QObject::connect(&lrc_->getBehaviorController(), |
| 129 | &lrc::api::BehaviorController::showCallView, |
| 130 | [self](const std::string accountId, |
| 131 | const lrc::api::conversation::Info convInfo){ |
| 132 | auto* accInfo = &lrc_->getAccountModel().getAccountInfo(accountId); |
Anthony Léonard | d00cd18 | 2018-01-17 09:21:27 -0500 | [diff] [blame^] | 133 | if (accInfo->contactModel->getContact(convInfo.participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING) |
| 134 | [smartViewVC selectPendingList]; |
| 135 | else |
| 136 | [smartViewVC selectConversationList]; |
| 137 | |
Olivier Soldano | 994971f | 2017-12-05 16:30:12 -0500 | [diff] [blame] | 138 | [currentCallVC setCurrentCall:convInfo.callId |
| 139 | conversation:convInfo.uid |
| 140 | account:accInfo]; |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 141 | [smartViewVC selectConversation: convInfo model:accInfo->conversationModel.get()]; |
Olivier Soldano | 994971f | 2017-12-05 16:30:12 -0500 | [diff] [blame] | 142 | [currentCallVC animateIn]; |
| 143 | [offlineVC animateOut]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 144 | }); |
Alexandre Lision | 210fe21 | 2016-01-27 11:15:13 -0500 | [diff] [blame] | 145 | |
Olivier Soldano | 994971f | 2017-12-05 16:30:12 -0500 | [diff] [blame] | 146 | QObject::connect(&lrc_->getBehaviorController(), |
| 147 | &lrc::api::BehaviorController::showIncomingCallView, |
| 148 | [self](const std::string accountId, |
| 149 | const lrc::api::conversation::Info convInfo){ |
| 150 | auto* accInfo = &lrc_->getAccountModel().getAccountInfo(accountId); |
Anthony Léonard | d00cd18 | 2018-01-17 09:21:27 -0500 | [diff] [blame^] | 151 | if (accInfo->contactModel->getContact(convInfo.participants[0]).profileInfo.type == lrc::api::profile::Type::PENDING) |
| 152 | [smartViewVC selectPendingList]; |
| 153 | else |
| 154 | [smartViewVC selectConversationList]; |
| 155 | |
Olivier Soldano | 994971f | 2017-12-05 16:30:12 -0500 | [diff] [blame] | 156 | [currentCallVC setCurrentCall:convInfo.callId |
| 157 | conversation:convInfo.uid |
| 158 | account:accInfo]; |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 159 | [smartViewVC selectConversation: convInfo model:accInfo->conversationModel.get()]; |
Olivier Soldano | 994971f | 2017-12-05 16:30:12 -0500 | [diff] [blame] | 160 | [currentCallVC animateIn]; |
Kateryna Kostiuk | abf4e27 | 2017-04-18 14:18:00 -0400 | [diff] [blame] | 161 | [offlineVC animateOut]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 162 | }); |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 163 | |
| 164 | QObject::connect(&lrc_->getBehaviorController(), |
| 165 | &lrc::api::BehaviorController::showChatView, |
| 166 | [self](const std::string& accountId, |
| 167 | const lrc::api::conversation::Info& convInfo){ |
| 168 | auto& accInfo = lrc_->getAccountModel().getAccountInfo(accountId); |
| 169 | [offlineVC setConversationUid:convInfo.uid model:accInfo.conversationModel.get()]; |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 170 | [smartViewVC selectConversation: convInfo model:accInfo.conversationModel.get()]; |
Anthony Léonard | 2382b56 | 2017-12-13 15:51:28 -0500 | [diff] [blame] | 171 | [offlineVC animateIn]; |
| 172 | [currentCallVC animateOut]; |
| 173 | }); |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | /** |
| 177 | * Implement the necessary logic to choose which Ring ID to display. |
| 178 | * This tries to choose the "best" ID to show |
| 179 | */ |
| 180 | - (void) updateRingID |
| 181 | { |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 182 | @try { |
| 183 | auto& account = [chooseAccountVC selectedAccount]; |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 184 | |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 185 | [ringIDLabel setStringValue:@""]; |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 186 | |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 187 | if(account.profileInfo.type != lrc::api::profile::Type::RING) { |
| 188 | self.hideRingID = YES; |
| 189 | return; |
| 190 | } |
| 191 | self.hideRingID = NO; |
| 192 | auto& registeredName = account.registeredName; |
| 193 | auto& ringID = account.profileInfo.uri; |
| 194 | NSString* uriToDisplay = nullptr; |
| 195 | if (!registeredName.empty()) { |
| 196 | uriToDisplay = @(registeredName.c_str()); |
| 197 | } else { |
| 198 | uriToDisplay = @(ringID.c_str()); |
| 199 | } |
| 200 | [ringIDLabel setStringValue:uriToDisplay]; |
| 201 | [self drawQRCode:@(ringID.c_str())]; |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 202 | } |
Anthony Léonard | 72128c9 | 2017-12-26 16:48:39 -0500 | [diff] [blame] | 203 | @catch (NSException *ex) { |
| 204 | NSLog(@"Caught exception %@: %@", [ex name], [ex reason]); |
| 205 | self.hideRingID = NO; |
| 206 | [ringIDLabel setStringValue:@"No account available"]; |
Alexandre Lision | 313427f | 2016-11-24 21:04:04 -0500 | [diff] [blame] | 207 | } |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 208 | } |
| 209 | |
Alexandre Lision | 1abdf58 | 2016-02-09 14:21:19 -0500 | [diff] [blame] | 210 | - (IBAction)shareRingID:(id)sender { |
| 211 | NSSharingServicePicker* sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:[NSArray arrayWithObject:[ringIDLabel stringValue]]]; |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 212 | [sharingServicePicker setDelegate:self]; |
Alexandre Lision | 1abdf58 | 2016-02-09 14:21:19 -0500 | [diff] [blame] | 213 | [sharingServicePicker showRelativeToRect:[sender bounds] |
| 214 | ofView:sender |
| 215 | preferredEdge:NSMinYEdge]; |
| 216 | } |
| 217 | |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 218 | - (IBAction)toggleQRCode:(id)sender { |
| 219 | // Toggle pressed state of QRCode button |
| 220 | [sender setPressed:![sender isPressed]]; |
Kateryna Kostiuk | 65ba43e | 2017-03-30 15:10:04 -0400 | [diff] [blame] | 221 | [self showQRCode:[sender isPressed]]; |
Alexandre Lision | 313427f | 2016-11-24 21:04:04 -0500 | [diff] [blame] | 222 | } |
| 223 | |
| 224 | /** |
| 225 | * Draw the QRCode in the qrCodeView |
| 226 | */ |
Kateryna Kostiuk | 65ba43e | 2017-03-30 15:10:04 -0400 | [diff] [blame] | 227 | - (void)drawQRCode:(NSString*) uriToDraw |
Alexandre Lision | 313427f | 2016-11-24 21:04:04 -0500 | [diff] [blame] | 228 | { |
Kateryna Kostiuk | 65ba43e | 2017-03-30 15:10:04 -0400 | [diff] [blame] | 229 | auto qrCode = QRcode_encodeString(uriToDraw.UTF8String, |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 230 | 0, |
| 231 | QR_ECLEVEL_L, // Lowest level of error correction |
| 232 | QR_MODE_8, // 8-bit data mode |
| 233 | 1); |
| 234 | if (!qrCode) { |
| 235 | return; |
| 236 | } |
| 237 | |
Alexandre Lision | 313427f | 2016-11-24 21:04:04 -0500 | [diff] [blame] | 238 | unsigned char *data = 0; |
| 239 | int width; |
| 240 | data = qrCode->data; |
| 241 | width = qrCode->width; |
| 242 | int qr_margin = 3; |
| 243 | |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 244 | CGFloat size = qrcodeView.frame.size.width; |
| 245 | |
| 246 | // create context |
| 247 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
| 248 | CGContextRef ctx = CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace, kCGImageAlphaPremultipliedLast); |
| 249 | |
| 250 | CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -size); |
| 251 | CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1); |
| 252 | CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform)); |
| 253 | |
Alexandre Lision | 313427f | 2016-11-24 21:04:04 -0500 | [diff] [blame] | 254 | float zoom = ceil((double)size / (qrCode->width + 2.0 * qr_margin)); |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 255 | CGRect rectDraw = CGRectMake(0, 0, zoom, zoom); |
| 256 | |
| 257 | int ran; |
| 258 | for(int i = 0; i < width; ++i) { |
| 259 | for(int j = 0; j < width; ++j) { |
| 260 | if(*data & 1) { |
| 261 | CGContextSetFillColorWithColor(ctx, [NSColor ringDarkGrey].CGColor); |
| 262 | rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom); |
| 263 | CGContextAddRect(ctx, rectDraw); |
| 264 | CGContextFillPath(ctx); |
| 265 | } else { |
| 266 | CGContextSetFillColorWithColor(ctx, [NSColor windowBackgroundColor].CGColor); |
| 267 | rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom); |
| 268 | CGContextAddRect(ctx, rectDraw); |
| 269 | CGContextFillPath(ctx); |
| 270 | } |
| 271 | ++data; |
| 272 | } |
| 273 | } |
Alexandre Lision | 313427f | 2016-11-24 21:04:04 -0500 | [diff] [blame] | 274 | |
| 275 | // get image |
| 276 | auto qrCGImage = CGBitmapContextCreateImage(ctx); |
| 277 | auto qrImage = [[NSImage alloc] initWithCGImage:qrCGImage size:qrcodeView.frame.size]; |
| 278 | |
| 279 | // some releases |
| 280 | CGContextRelease(ctx); |
| 281 | CGImageRelease(qrCGImage); |
| 282 | CGColorSpaceRelease(colorSpace); |
| 283 | QRcode_free(qrCode); |
| 284 | |
| 285 | [qrcodeView setImage:qrImage]; |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 286 | } |
| 287 | |
| 288 | /** |
| 289 | * Start the in/out animation displaying the QRCode |
| 290 | * @param show should the QRCode be animated in or out |
| 291 | */ |
| 292 | - (void) showQRCode:(BOOL) show |
| 293 | { |
| 294 | static const NSInteger offset = 110; |
| 295 | [NSAnimationContext beginGrouping]; |
| 296 | NSAnimationContext.currentContext.duration = 0.5; |
| 297 | [[NSAnimationContext currentContext] setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]]; |
| 298 | qrcodeView.animator.alphaValue = show ? 1.0 : 0.0; |
| 299 | [centerYQRCodeConstraint.animator setConstant: show ? offset : 0]; |
| 300 | [centerYWelcomeContainerConstraint.animator setConstant:show ? -offset : 0]; |
| 301 | [NSAnimationContext endGrouping]; |
| 302 | } |
| 303 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 304 | - (IBAction)openPreferences:(id)sender |
| 305 | { |
Alexandre Lision | bfa68f6 | 2015-09-10 08:38:42 -0400 | [diff] [blame] | 306 | preferencesWC = [[PreferencesWC alloc] initWithWindowNibName:@"PreferencesWindow"]; |
| 307 | [preferencesWC.window makeKeyAndOrderFront:preferencesWC.window]; |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 308 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 309 | |
Kateryna Kostiuk | 882cbac | 2017-07-05 17:29:00 -0400 | [diff] [blame] | 310 | - (IBAction)callClickedAtRow:(id)sender |
| 311 | { |
| 312 | NSTabViewItem *selectedTab = [smartViewVC.tabbar selectedTabViewItem]; |
| 313 | int index = [smartViewVC.tabbar indexOfTabViewItem:selectedTab]; |
| 314 | switch (index) { |
| 315 | case 0: |
| 316 | [smartViewVC startCallForRow:sender]; |
| 317 | break; |
Kateryna Kostiuk | 882cbac | 2017-07-05 17:29:00 -0400 | [diff] [blame] | 318 | default: |
| 319 | break; |
| 320 | } |
| 321 | } |
Anthony Léonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 322 | |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame] | 323 | #pragma mark - Ring account migration |
| 324 | |
| 325 | - (void) migrateRingAccount:(Account*) acc |
| 326 | { |
| 327 | self.migrateWC = [[MigrateRingAccountsWC alloc] initWithDelegate:self actionCode:1]; |
| 328 | self.migrateWC.account = acc; |
| 329 | #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9 |
| 330 | [self.window beginSheet:self.migrateWC.window completionHandler:nil]; |
| 331 | #else |
| 332 | [NSApp beginSheet: self.migrateWC.window |
| 333 | modalForWindow: self.window |
| 334 | modalDelegate: self |
| 335 | didEndSelector: nil |
| 336 | contextInfo: nil]; |
| 337 | #endif |
| 338 | } |
| 339 | |
Anthony Léonard | 49cb291 | 2017-11-13 16:15:39 -0500 | [diff] [blame] | 340 | // TODO: Reimplement as a blocking loop when new LRC models handle migration |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame] | 341 | - (void)checkAccountsToMigrate |
| 342 | { |
| 343 | auto ringList = AccountModel::instance().accountsToMigrate(); |
| 344 | if (ringList.length() > 0){ |
| 345 | Account* acc = ringList.value(0); |
| 346 | [self migrateRingAccount:acc]; |
| 347 | } else { |
| 348 | // Fresh run, we need to make sure RingID appears |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame] | 349 | [shareButton sendActionOn:NSLeftMouseDownMask]; |
| 350 | |
| 351 | [self connect]; |
Kateryna Kostiuk | a724846 | 2017-04-18 16:15:31 -0400 | [diff] [blame] | 352 | [self updateRingID]; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 353 | // display accounts to select |
| 354 | NSToolbar *toolbar = self.window.toolbar; |
| 355 | toolbar.delegate = self; |
| 356 | [toolbar insertItemWithItemIdentifier:kChangeAccountToolBarItemIdentifier atIndex:1]; |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame] | 357 | } |
| 358 | } |
| 359 | |
| 360 | - (void)migrationDidComplete |
| 361 | { |
| 362 | [self checkAccountsToMigrate]; |
| 363 | } |
| 364 | |
| 365 | - (void)migrationDidCompleteWithError |
| 366 | { |
| 367 | [self checkAccountsToMigrate]; |
| 368 | } |
| 369 | |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 370 | -(void)selectAccount:(const lrc::api::account::Info&)accInfo |
| 371 | { |
| 372 | // If the selected account has been changed, we close any open panel |
| 373 | if ([smartViewVC setConversationModel:accInfo.conversationModel.get()]) { |
| 374 | [currentCallVC animateOut]; |
| 375 | [offlineVC animateOut]; |
| 376 | } |
Anthony Léonard | 61e3dcf | 2017-12-27 12:19:52 -0500 | [diff] [blame] | 377 | |
| 378 | // Welcome view informations are also updated |
| 379 | [self updateRingID]; |
Anthony Léonard | 9bebf1d | 2017-12-21 14:33:51 -0500 | [diff] [blame] | 380 | } |
| 381 | |
Anthony Léonard | 8585cc0 | 2017-12-28 14:03:45 -0500 | [diff] [blame] | 382 | -(void)rightPanelClosed |
| 383 | { |
| 384 | [smartViewVC deselect]; |
| 385 | } |
| 386 | |
Anthony Léonard | 0a9904c | 2018-01-11 16:43:47 -0500 | [diff] [blame] | 387 | -(void)currentConversationTrusted |
| 388 | { |
| 389 | [smartViewVC selectConversationList]; |
| 390 | } |
| 391 | |
Anthony Léonard | bee94cc | 2018-01-16 11:42:40 -0500 | [diff] [blame] | 392 | -(void) listTypeChanged { |
| 393 | [offlineVC animateOut]; |
| 394 | [currentCallVC animateOut]; |
| 395 | } |
| 396 | |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 397 | #pragma mark - NSToolbarDelegate |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 398 | - (nullable NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag |
| 399 | { |
| 400 | if(itemIdentifier == kChangeAccountToolBarItemIdentifier) { |
| 401 | NSToolbarItem *toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:kChangeAccountToolBarItemIdentifier]; |
| 402 | toolbarItem.view = chooseAccountVC.view; |
| 403 | return toolbarItem; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 404 | } |
Kateryna Kostiuk | db1f3a1 | 2017-04-24 12:08:28 -0400 | [diff] [blame] | 405 | return nil; |
Kateryna Kostiuk | 13b7688 | 2017-03-30 09:18:44 -0400 | [diff] [blame] | 406 | } |
| 407 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 408 | @end |