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