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