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