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> |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 33 | |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame^] | 34 | // Ring |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 35 | #import "AppDelegate.h" |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 36 | #import "Constants.h" |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 37 | #import "CurrentCallVC.h" |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame^] | 38 | #import "MigrateRingAccountsWC.h" |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 39 | #import "ConversationVC.h" |
Alexandre Lision | bfa68f6 | 2015-09-10 08:38:42 -0400 | [diff] [blame] | 40 | #import "PreferencesWC.h" |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 41 | #import "views/IconButton.h" |
Alexandre Lision | bfa68f6 | 2015-09-10 08:38:42 -0400 | [diff] [blame] | 42 | #import "views/NSColor+RingTheme.h" |
Alexandre Lision | 2db8f47 | 2015-07-22 15:05:46 -0400 | [diff] [blame] | 43 | |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame^] | 44 | @interface RingWindowController () <MigrateRingAccountsDelegate> |
| 45 | |
| 46 | @property (retain) MigrateRingAccountsWC* migrateWC; |
| 47 | |
| 48 | @end |
| 49 | |
Alexandre Lision | bfa68f6 | 2015-09-10 08:38:42 -0400 | [diff] [blame] | 50 | @implementation RingWindowController { |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 51 | |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 52 | __unsafe_unretained IBOutlet NSLayoutConstraint* centerYQRCodeConstraint; |
| 53 | __unsafe_unretained IBOutlet NSLayoutConstraint* centerYWelcomeContainerConstraint; |
| 54 | __unsafe_unretained IBOutlet NSView* welcomeContainer; |
Alexandre Lision | 1abdf58 | 2016-02-09 14:21:19 -0500 | [diff] [blame] | 55 | __unsafe_unretained IBOutlet NSView* callView; |
| 56 | __unsafe_unretained IBOutlet NSTextField* ringIDLabel; |
| 57 | __unsafe_unretained IBOutlet NSButton* shareButton; |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 58 | __unsafe_unretained IBOutlet NSImageView* qrcodeView; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 59 | |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 60 | PreferencesWC* preferencesWC; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 61 | CurrentCallVC* currentCallVC; |
| 62 | ConversationVC* offlineVC; |
Alexandre Lision | bfa68f6 | 2015-09-10 08:38:42 -0400 | [diff] [blame] | 63 | } |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 64 | |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame^] | 65 | QMetaObject::Connection accountUpdate; |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 66 | static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier"; |
Alexandre Lision | c514805 | 2015-03-04 15:10:35 -0500 | [diff] [blame] | 67 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 68 | - (void)windowDidLoad { |
| 69 | [super windowDidLoad]; |
Alexandre Lision | 4dfcafc | 2015-08-20 12:43:23 -0400 | [diff] [blame] | 70 | [self.window setMovableByWindowBackground:YES]; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 71 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 72 | currentCallVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil]; |
| 73 | offlineVC = [[ConversationVC alloc] initWithNibName:@"Conversation" bundle:nil]; |
| 74 | |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 75 | [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 76 | [[currentCallVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
| 77 | [[offlineVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable]; |
Alexandre Lision | 58cab67 | 2015-06-09 15:25:40 -0400 | [diff] [blame] | 78 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 79 | [callView addSubview:[currentCallVC view] positioned:NSWindowAbove relativeTo:nil]; |
| 80 | [callView addSubview:[offlineVC view] positioned:NSWindowAbove relativeTo:nil]; |
Alexandre Lision | 16d9c0a | 2015-08-10 12:05:15 -0400 | [diff] [blame] | 81 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 82 | [currentCallVC initFrame]; |
| 83 | [offlineVC initFrame]; |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 84 | |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame^] | 85 | [self checkAccountsToMigrate]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | - (void) connect |
| 89 | { |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 90 | // Update Ring ID label based on account model changes |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame^] | 91 | QObject::disconnect(accountUpdate); |
| 92 | accountUpdate = QObject::connect(&AccountModel::instance(), |
| 93 | &AccountModel::dataChanged, |
| 94 | [=] { |
| 95 | [self updateRingID]; |
| 96 | }); |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 97 | |
| 98 | QObject::connect(RecentModel::instance().selectionModel(), |
| 99 | &QItemSelectionModel::currentChanged, |
| 100 | [=](const QModelIndex ¤t, const QModelIndex &previous) { |
| 101 | auto call = RecentModel::instance().getActiveCall(current); |
Alexandre Lision | 261f1b9 | 2016-04-04 12:35:34 -0400 | [diff] [blame] | 102 | |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 103 | if(!current.isValid()) { |
Alexandre Lision | 01cf5e3 | 2016-01-21 15:54:30 -0500 | [diff] [blame] | 104 | [offlineVC animateOut]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 105 | [currentCallVC animateOut]; |
| 106 | return; |
| 107 | } |
| 108 | |
| 109 | if (!call) { |
| 110 | [currentCallVC animateOut]; |
| 111 | [offlineVC animateIn]; |
| 112 | } else { |
| 113 | [currentCallVC animateIn]; |
Alexandre Lision | 01cf5e3 | 2016-01-21 15:54:30 -0500 | [diff] [blame] | 114 | [offlineVC animateOut]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 115 | } |
| 116 | }); |
Alexandre Lision | 210fe21 | 2016-01-27 11:15:13 -0500 | [diff] [blame] | 117 | |
| 118 | QObject::connect(CallModel::instance().selectionModel(), |
| 119 | &QItemSelectionModel::currentChanged, |
| 120 | [=](const QModelIndex ¤t, const QModelIndex &previous) { |
| 121 | if(!current.isValid()) { |
| 122 | return; |
| 123 | } |
| 124 | |
| 125 | if (previous.isValid()) { |
| 126 | // We were already on a call |
| 127 | [currentCallVC animateOut]; |
| 128 | } else { |
| 129 | // Make sure Conversation view hides when selecting a valid call |
| 130 | [currentCallVC animateIn]; |
| 131 | [offlineVC animateOut]; |
| 132 | } |
| 133 | }); |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | /** |
| 137 | * Implement the necessary logic to choose which Ring ID to display. |
| 138 | * This tries to choose the "best" ID to show |
| 139 | */ |
| 140 | - (void) updateRingID |
| 141 | { |
| 142 | Account* registered = nullptr; |
| 143 | Account* enabled = nullptr; |
| 144 | Account* finalChoice = nullptr; |
| 145 | |
| 146 | [ringIDLabel setStringValue:@""]; |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 147 | auto ringList = AccountModel::instance().getAccountsByProtocol(Account::Protocol::RING); |
Alexandre Lision | bb5c246 | 2015-07-30 12:55:37 -0400 | [diff] [blame] | 148 | for (int i = 0 ; i < ringList.size() && !registered ; ++i) { |
| 149 | Account* acc = ringList.value(i); |
| 150 | if (acc->isEnabled()) { |
| 151 | if(!enabled) |
| 152 | enabled = finalChoice = acc; |
| 153 | if (acc->registrationState() == Account::RegistrationState::READY) { |
| 154 | registered = enabled = finalChoice = acc; |
| 155 | } |
| 156 | } else { |
| 157 | if (!finalChoice) |
| 158 | finalChoice = acc; |
| 159 | } |
| 160 | } |
| 161 | |
Alexandre Lision | 7f8351b | 2015-08-20 11:43:37 -0400 | [diff] [blame] | 162 | [ringIDLabel setStringValue:[[NSString alloc] initWithFormat:@"%@", finalChoice->username().toNSString()]]; |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 163 | } |
| 164 | |
Alexandre Lision | 1abdf58 | 2016-02-09 14:21:19 -0500 | [diff] [blame] | 165 | - (IBAction)shareRingID:(id)sender { |
| 166 | NSSharingServicePicker* sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:[NSArray arrayWithObject:[ringIDLabel stringValue]]]; |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 167 | [sharingServicePicker setDelegate:self]; |
Alexandre Lision | 1abdf58 | 2016-02-09 14:21:19 -0500 | [diff] [blame] | 168 | [sharingServicePicker showRelativeToRect:[sender bounds] |
| 169 | ofView:sender |
| 170 | preferredEdge:NSMinYEdge]; |
| 171 | } |
| 172 | |
Alexandre Lision | fd0d6c8 | 2016-03-29 17:06:54 -0400 | [diff] [blame] | 173 | - (IBAction)toggleQRCode:(id)sender { |
| 174 | // Toggle pressed state of QRCode button |
| 175 | [sender setPressed:![sender isPressed]]; |
| 176 | if (![sender isPressed]) { |
| 177 | // Recenter welcome view |
| 178 | [self showQRCode:NO]; |
| 179 | return; |
| 180 | } |
| 181 | |
| 182 | auto qrCode = QRcode_encodeString(ringIDLabel.stringValue.UTF8String, |
| 183 | 0, |
| 184 | QR_ECLEVEL_L, // Lowest level of error correction |
| 185 | QR_MODE_8, // 8-bit data mode |
| 186 | 1); |
| 187 | if (!qrCode) { |
| 188 | return; |
| 189 | } |
| 190 | |
| 191 | CGFloat size = qrcodeView.frame.size.width; |
| 192 | |
| 193 | // create context |
| 194 | CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB(); |
| 195 | CGContextRef ctx = CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace, kCGImageAlphaPremultipliedLast); |
| 196 | |
| 197 | CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -size); |
| 198 | CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1); |
| 199 | CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform)); |
| 200 | |
| 201 | // draw QR on this context |
| 202 | [self drawQRCode:qrCode context:ctx size:size]; |
| 203 | |
| 204 | // get image |
| 205 | auto qrCGImage = CGBitmapContextCreateImage(ctx); |
| 206 | auto qrImage = [[NSImage alloc] initWithCGImage:qrCGImage size:qrcodeView.frame.size]; |
| 207 | |
| 208 | // some releases |
| 209 | CGContextRelease(ctx); |
| 210 | CGImageRelease(qrCGImage); |
| 211 | CGColorSpaceRelease(colorSpace); |
| 212 | QRcode_free(qrCode); |
| 213 | |
| 214 | [qrcodeView setImage:qrImage]; |
| 215 | [self showQRCode:YES]; |
| 216 | } |
| 217 | |
| 218 | /** |
| 219 | * @param code the previously generated QRCode |
| 220 | * @param ctx current drawing context |
| 221 | * @param size the output size in which to draw |
| 222 | */ |
| 223 | - (void)drawQRCode:(QRcode *)code context:(CGContextRef)ctx size:(CGFloat)size { |
| 224 | unsigned char *data = 0; |
| 225 | int width; |
| 226 | data = code->data; |
| 227 | width = code->width; |
| 228 | int qr_margin = 3; |
| 229 | float zoom = ceil((double)size / (code->width + 2.0 * qr_margin)); |
| 230 | CGRect rectDraw = CGRectMake(0, 0, zoom, zoom); |
| 231 | |
| 232 | int ran; |
| 233 | for(int i = 0; i < width; ++i) { |
| 234 | for(int j = 0; j < width; ++j) { |
| 235 | if(*data & 1) { |
| 236 | CGContextSetFillColorWithColor(ctx, [NSColor ringDarkGrey].CGColor); |
| 237 | rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom); |
| 238 | CGContextAddRect(ctx, rectDraw); |
| 239 | CGContextFillPath(ctx); |
| 240 | } else { |
| 241 | CGContextSetFillColorWithColor(ctx, [NSColor windowBackgroundColor].CGColor); |
| 242 | rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom); |
| 243 | CGContextAddRect(ctx, rectDraw); |
| 244 | CGContextFillPath(ctx); |
| 245 | } |
| 246 | ++data; |
| 247 | } |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | /** |
| 252 | * Start the in/out animation displaying the QRCode |
| 253 | * @param show should the QRCode be animated in or out |
| 254 | */ |
| 255 | - (void) showQRCode:(BOOL) show |
| 256 | { |
| 257 | static const NSInteger offset = 110; |
| 258 | [NSAnimationContext beginGrouping]; |
| 259 | NSAnimationContext.currentContext.duration = 0.5; |
| 260 | [[NSAnimationContext currentContext] setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]]; |
| 261 | qrcodeView.animator.alphaValue = show ? 1.0 : 0.0; |
| 262 | [centerYQRCodeConstraint.animator setConstant: show ? offset : 0]; |
| 263 | [centerYWelcomeContainerConstraint.animator setConstant:show ? -offset : 0]; |
| 264 | [NSAnimationContext endGrouping]; |
| 265 | } |
| 266 | |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 267 | - (IBAction)openPreferences:(id)sender |
| 268 | { |
Alexandre Lision | bfa68f6 | 2015-09-10 08:38:42 -0400 | [diff] [blame] | 269 | preferencesWC = [[PreferencesWC alloc] initWithWindowNibName:@"PreferencesWindow"]; |
| 270 | [preferencesWC.window makeKeyAndOrderFront:preferencesWC.window]; |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 271 | } |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 272 | |
Alexandre Lision | 624b1a8 | 2016-09-11 19:29:01 -0400 | [diff] [blame^] | 273 | #pragma mark - Ring account migration |
| 274 | |
| 275 | - (void) migrateRingAccount:(Account*) acc |
| 276 | { |
| 277 | self.migrateWC = [[MigrateRingAccountsWC alloc] initWithDelegate:self actionCode:1]; |
| 278 | self.migrateWC.account = acc; |
| 279 | #if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9 |
| 280 | [self.window beginSheet:self.migrateWC.window completionHandler:nil]; |
| 281 | #else |
| 282 | [NSApp beginSheet: self.migrateWC.window |
| 283 | modalForWindow: self.window |
| 284 | modalDelegate: self |
| 285 | didEndSelector: nil |
| 286 | contextInfo: nil]; |
| 287 | #endif |
| 288 | } |
| 289 | |
| 290 | - (void)checkAccountsToMigrate |
| 291 | { |
| 292 | auto ringList = AccountModel::instance().accountsToMigrate(); |
| 293 | if (ringList.length() > 0){ |
| 294 | Account* acc = ringList.value(0); |
| 295 | [self migrateRingAccount:acc]; |
| 296 | } else { |
| 297 | // Fresh run, we need to make sure RingID appears |
| 298 | [self updateRingID]; |
| 299 | [shareButton sendActionOn:NSLeftMouseDownMask]; |
| 300 | |
| 301 | [self connect]; |
| 302 | } |
| 303 | } |
| 304 | |
| 305 | - (void)migrationDidComplete |
| 306 | { |
| 307 | [self checkAccountsToMigrate]; |
| 308 | } |
| 309 | |
| 310 | - (void)migrationDidCompleteWithError |
| 311 | { |
| 312 | [self checkAccountsToMigrate]; |
| 313 | } |
| 314 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 315 | @end |