blob: 8ef5f400018df58ab73ec47ae4484be2b720b985 [file] [log] [blame]
Alexandre Lision8521baa2015-03-13 11:08:00 -04001/*
Alexandre Lision9fe374b2016-01-06 10:17:31 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Alexandre Lision8521baa2015-03-13 11:08:00 -04003 * 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 Lision8521baa2015-03-13 11:08:00 -040018 */
Alexandre Lision5855b6a2015-02-03 11:31:05 -050019#import "RingWindowController.h"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050020#import <QuartzCore/QuartzCore.h>
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -040021#include <qrencode.h>
Alexandre Lision0f66bd32016-01-18 11:30:45 -050022
23//Qt
24#import <QItemSelectionModel>
25#import <QItemSelection>
Alexandre Lision5855b6a2015-02-03 11:31:05 -050026
Alexandre Lisionbb5c2462015-07-30 12:55:37 -040027//LRC
Alexandre Lision5855b6a2015-02-03 11:31:05 -050028#import <accountmodel.h>
29#import <callmodel.h>
30#import <account.h>
Alexandre Lision91d11e52015-03-20 17:42:05 -040031#import <call.h>
Alexandre Lision0f66bd32016-01-18 11:30:45 -050032#import <recentmodel.h>
Kateryna Kostiuk312f9132017-04-18 12:09:06 -040033#import <AvailableAccountModel.h>
34
Alexandre Lision5855b6a2015-02-03 11:31:05 -050035
Alexandre Lision624b1a82016-09-11 19:29:01 -040036// Ring
Alexandre Lision745e4d62015-03-22 20:03:10 -040037#import "AppDelegate.h"
Alexandre Lisionc65310c2015-04-23 16:44:23 -040038#import "Constants.h"
Alexandre Lision58cab672015-06-09 15:25:40 -040039#import "CurrentCallVC.h"
Alexandre Lision624b1a82016-09-11 19:29:01 -040040#import "MigrateRingAccountsWC.h"
Alexandre Lision0f66bd32016-01-18 11:30:45 -050041#import "ConversationVC.h"
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040042#import "PreferencesWC.h"
Alexandre Lisiona3a43dc2017-03-30 16:21:30 -040043#import "SmartViewVC.h"
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -040044#import "views/IconButton.h"
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040045#import "views/NSColor+RingTheme.h"
Alexandre Lisione77f6f92016-04-17 23:39:39 -040046#import "views/BackgroundView.h"
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040047#import "ChooseAccountVC.h"
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040048#import "ContactRequestVC.h"
Alexandre Lision2db8f472015-07-22 15:05:46 -040049
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040050@interface RingWindowController () <MigrateRingAccountsDelegate, NSToolbarDelegate>
Alexandre Lision624b1a82016-09-11 19:29:01 -040051
52@property (retain) MigrateRingAccountsWC* migrateWC;
53
54@end
55
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040056@implementation RingWindowController {
Alexandre Lision5855b6a2015-02-03 11:31:05 -050057
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -040058 __unsafe_unretained IBOutlet NSLayoutConstraint* centerYQRCodeConstraint;
59 __unsafe_unretained IBOutlet NSLayoutConstraint* centerYWelcomeContainerConstraint;
60 __unsafe_unretained IBOutlet NSView* welcomeContainer;
Alexandre Lision1abdf582016-02-09 14:21:19 -050061 __unsafe_unretained IBOutlet NSView* callView;
62 __unsafe_unretained IBOutlet NSTextField* ringIDLabel;
63 __unsafe_unretained IBOutlet NSButton* shareButton;
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -040064 __unsafe_unretained IBOutlet NSImageView* qrcodeView;
Alexandre Lision58cab672015-06-09 15:25:40 -040065
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -040066 PreferencesWC* preferencesWC;
Alexandre Lisiona3a43dc2017-03-30 16:21:30 -040067 IBOutlet SmartViewVC* smartViewVC;
Alexandre Lisione77f6f92016-04-17 23:39:39 -040068
Alexandre Lision0f66bd32016-01-18 11:30:45 -050069 CurrentCallVC* currentCallVC;
70 ConversationVC* offlineVC;
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040071 // toolbar menu items
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040072 ChooseAccountVC* chooseAccountVC;
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040073 ContactRequestVC* contactRequestVC;
Alexandre Lisionbfa68f62015-09-10 08:38:42 -040074}
Alexandre Lision58cab672015-06-09 15:25:40 -040075
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040076static NSString* const kPreferencesIdentifier = @"PreferencesIdentifier";
77NSString* const kChangeAccountToolBarItemIdentifier = @"ChangeAccountToolBarItemIdentifier";
78NSString* const kTrustRequestMenuItemIdentifier = @"TrustRequestMenuItemIdentifier";
Alexandre Lisionc5148052015-03-04 15:10:35 -050079
Alexandre Lision5855b6a2015-02-03 11:31:05 -050080- (void)windowDidLoad {
81 [super windowDidLoad];
Alexandre Lision4dfcafc2015-08-20 12:43:23 -040082 [self.window setMovableByWindowBackground:YES];
Alexandre Lision58cab672015-06-09 15:25:40 -040083
Alexandre Lisione77f6f92016-04-17 23:39:39 -040084 [self.window setBackgroundColor:[NSColor colorWithRed:242.0/255 green:242.0/255 blue:242.0/255 alpha:1.0]];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040085 self.window.titleVisibility = NSWindowTitleHidden;
Alexandre Lisione77f6f92016-04-17 23:39:39 -040086
Alexandre Lision0f66bd32016-01-18 11:30:45 -050087 currentCallVC = [[CurrentCallVC alloc] initWithNibName:@"CurrentCall" bundle:nil];
88 offlineVC = [[ConversationVC alloc] initWithNibName:@"Conversation" bundle:nil];
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040089 // toolbar items
Kateryna Kostiuk13b76882017-03-30 09:18:44 -040090 chooseAccountVC = [[ChooseAccountVC alloc] initWithNibName:@"ChooseAccount" bundle:nil];
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -040091 contactRequestVC = [[ContactRequestVC alloc] initWithNibName:@"ContactRequest" bundle:nil];
Alexandre Lision58cab672015-06-09 15:25:40 -040092 [callView setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
Alexandre Lision0f66bd32016-01-18 11:30:45 -050093 [[currentCallVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
94 [[offlineVC view] setAutoresizingMask:NSViewWidthSizable | NSViewHeightSizable];
Alexandre Lision58cab672015-06-09 15:25:40 -040095
Alexandre Lision0f66bd32016-01-18 11:30:45 -050096 [callView addSubview:[currentCallVC view] positioned:NSWindowAbove relativeTo:nil];
97 [callView addSubview:[offlineVC view] positioned:NSWindowAbove relativeTo:nil];
Alexandre Lision16d9c0a2015-08-10 12:05:15 -040098
Alexandre Lision0f66bd32016-01-18 11:30:45 -050099 [currentCallVC initFrame];
100 [offlineVC initFrame];
Alexandre Lisionbb5c2462015-07-30 12:55:37 -0400101
Alexandre Lision624b1a82016-09-11 19:29:01 -0400102 [self checkAccountsToMigrate];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500103}
104
105- (void) connect
106{
Alexandre Lisionbb5c2462015-07-30 12:55:37 -0400107 // Update Ring ID label based on account model changes
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500108 QObject::connect(RecentModel::instance().selectionModel(),
109 &QItemSelectionModel::currentChanged,
110 [=](const QModelIndex &current, const QModelIndex &previous) {
111 auto call = RecentModel::instance().getActiveCall(current);
Alexandre Lision261f1b92016-04-04 12:35:34 -0400112
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500113 if(!current.isValid()) {
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500114 [offlineVC animateOut];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500115 [currentCallVC animateOut];
Kateryna Kostiukabf4e272017-04-18 14:18:00 -0400116 [chooseAccountVC enable];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500117 return;
118 }
119
120 if (!call) {
121 [currentCallVC animateOut];
122 [offlineVC animateIn];
Kateryna Kostiukabf4e272017-04-18 14:18:00 -0400123 [chooseAccountVC enable];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500124 } else {
125 [currentCallVC animateIn];
Alexandre Lision01cf5e32016-01-21 15:54:30 -0500126 [offlineVC animateOut];
Kateryna Kostiukabf4e272017-04-18 14:18:00 -0400127 [chooseAccountVC disable];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500128 }
129 });
Alexandre Lision210fe212016-01-27 11:15:13 -0500130
131 QObject::connect(CallModel::instance().selectionModel(),
132 &QItemSelectionModel::currentChanged,
133 [=](const QModelIndex &current, const QModelIndex &previous) {
134 if(!current.isValid()) {
135 return;
136 }
137
138 if (previous.isValid()) {
139 // We were already on a call
140 [currentCallVC animateOut];
Kateryna Kostiukabf4e272017-04-18 14:18:00 -0400141 [chooseAccountVC enable];
Alexandre Lision210fe212016-01-27 11:15:13 -0500142 } else {
143 // Make sure Conversation view hides when selecting a valid call
144 [currentCallVC animateIn];
145 [offlineVC animateOut];
Kateryna Kostiukabf4e272017-04-18 14:18:00 -0400146 [chooseAccountVC disable];
Alexandre Lision210fe212016-01-27 11:15:13 -0500147 }
148 });
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400149 QObject::connect(AvailableAccountModel::instance().selectionModel(),
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400150 &QItemSelectionModel::currentChanged,
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400151 [self](const QModelIndex& idx){
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400152 [self updateRingID];
Kateryna Kostiukabf4e272017-04-18 14:18:00 -0400153 [offlineVC animateOut];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400154 });
Alexandre Lisionbb5c2462015-07-30 12:55:37 -0400155}
156
157/**
158 * Implement the necessary logic to choose which Ring ID to display.
159 * This tries to choose the "best" ID to show
160 */
161- (void) updateRingID
162{
Alexandre Lisionbb5c2462015-07-30 12:55:37 -0400163 Account* finalChoice = nullptr;
164
165 [ringIDLabel setStringValue:@""];
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400166 QModelIndex index = AvailableAccountModel::instance().selectionModel()->currentIndex();
167 finalChoice = index.data(static_cast<int>(Account::Role::Object)).value<Account*>();
168 if(finalChoice == nil || (finalChoice->protocol() != Account::Protocol::RING)) {
Kateryna Kostiuka7248462017-04-18 16:15:31 -0400169 self.hideRingID = YES;
Kateryna Kostiuk312f9132017-04-18 12:09:06 -0400170 return;
Alexandre Lisionbb5c2462015-07-30 12:55:37 -0400171 }
Kateryna Kostiuka7248462017-04-18 16:15:31 -0400172 self.hideRingID = NO;
Alexandre Lision313427f2016-11-24 21:04:04 -0500173 auto name = finalChoice->registeredName();
Kateryna Kostiuk65ba43e2017-03-30 15:10:04 -0400174 NSString* uriToDisplay = nullptr;
Alexandre Lision313427f2016-11-24 21:04:04 -0500175 if (!name.isNull() && !name.isEmpty()) {
Kateryna Kostiuk65ba43e2017-03-30 15:10:04 -0400176 uriToDisplay = name.toNSString();
Alexandre Lision313427f2016-11-24 21:04:04 -0500177 } else {
Kateryna Kostiuk65ba43e2017-03-30 15:10:04 -0400178 uriToDisplay = finalChoice->username().toNSString();
Alexandre Lision313427f2016-11-24 21:04:04 -0500179 }
Kateryna Kostiuk65ba43e2017-03-30 15:10:04 -0400180 [ringIDLabel setStringValue:uriToDisplay];
181 [self drawQRCode:finalChoice->username().toNSString()];
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500182}
183
Alexandre Lision1abdf582016-02-09 14:21:19 -0500184- (IBAction)shareRingID:(id)sender {
185 NSSharingServicePicker* sharingServicePicker = [[NSSharingServicePicker alloc] initWithItems:[NSArray arrayWithObject:[ringIDLabel stringValue]]];
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400186 [sharingServicePicker setDelegate:self];
Alexandre Lision1abdf582016-02-09 14:21:19 -0500187 [sharingServicePicker showRelativeToRect:[sender bounds]
188 ofView:sender
189 preferredEdge:NSMinYEdge];
190}
191
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400192- (IBAction)toggleQRCode:(id)sender {
193 // Toggle pressed state of QRCode button
194 [sender setPressed:![sender isPressed]];
Kateryna Kostiuk65ba43e2017-03-30 15:10:04 -0400195 [self showQRCode:[sender isPressed]];
Alexandre Lision313427f2016-11-24 21:04:04 -0500196}
197
198/**
199 * Draw the QRCode in the qrCodeView
200 */
Kateryna Kostiuk65ba43e2017-03-30 15:10:04 -0400201- (void)drawQRCode:(NSString*) uriToDraw
Alexandre Lision313427f2016-11-24 21:04:04 -0500202{
Kateryna Kostiuk65ba43e2017-03-30 15:10:04 -0400203 auto qrCode = QRcode_encodeString(uriToDraw.UTF8String,
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400204 0,
205 QR_ECLEVEL_L, // Lowest level of error correction
206 QR_MODE_8, // 8-bit data mode
207 1);
208 if (!qrCode) {
209 return;
210 }
211
Alexandre Lision313427f2016-11-24 21:04:04 -0500212 unsigned char *data = 0;
213 int width;
214 data = qrCode->data;
215 width = qrCode->width;
216 int qr_margin = 3;
217
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400218 CGFloat size = qrcodeView.frame.size.width;
219
220 // create context
221 CGColorSpaceRef colorSpace = CGColorSpaceCreateDeviceRGB();
222 CGContextRef ctx = CGBitmapContextCreate(0, size, size, 8, size * 4, colorSpace, kCGImageAlphaPremultipliedLast);
223
224 CGAffineTransform translateTransform = CGAffineTransformMakeTranslation(0, -size);
225 CGAffineTransform scaleTransform = CGAffineTransformMakeScale(1, -1);
226 CGContextConcatCTM(ctx, CGAffineTransformConcat(translateTransform, scaleTransform));
227
Alexandre Lision313427f2016-11-24 21:04:04 -0500228 float zoom = ceil((double)size / (qrCode->width + 2.0 * qr_margin));
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400229 CGRect rectDraw = CGRectMake(0, 0, zoom, zoom);
230
231 int ran;
232 for(int i = 0; i < width; ++i) {
233 for(int j = 0; j < width; ++j) {
234 if(*data & 1) {
235 CGContextSetFillColorWithColor(ctx, [NSColor ringDarkGrey].CGColor);
236 rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom);
237 CGContextAddRect(ctx, rectDraw);
238 CGContextFillPath(ctx);
239 } else {
240 CGContextSetFillColorWithColor(ctx, [NSColor windowBackgroundColor].CGColor);
241 rectDraw.origin = CGPointMake((j + qr_margin) * zoom,(i + qr_margin) * zoom);
242 CGContextAddRect(ctx, rectDraw);
243 CGContextFillPath(ctx);
244 }
245 ++data;
246 }
247 }
Alexandre Lision313427f2016-11-24 21:04:04 -0500248
249 // get image
250 auto qrCGImage = CGBitmapContextCreateImage(ctx);
251 auto qrImage = [[NSImage alloc] initWithCGImage:qrCGImage size:qrcodeView.frame.size];
252
253 // some releases
254 CGContextRelease(ctx);
255 CGImageRelease(qrCGImage);
256 CGColorSpaceRelease(colorSpace);
257 QRcode_free(qrCode);
258
259 [qrcodeView setImage:qrImage];
Alexandre Lisionfd0d6c82016-03-29 17:06:54 -0400260}
261
262/**
263 * Start the in/out animation displaying the QRCode
264 * @param show should the QRCode be animated in or out
265 */
266- (void) showQRCode:(BOOL) show
267{
268 static const NSInteger offset = 110;
269 [NSAnimationContext beginGrouping];
270 NSAnimationContext.currentContext.duration = 0.5;
271 [[NSAnimationContext currentContext] setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionDefault]];
272 qrcodeView.animator.alphaValue = show ? 1.0 : 0.0;
273 [centerYQRCodeConstraint.animator setConstant: show ? offset : 0];
274 [centerYWelcomeContainerConstraint.animator setConstant:show ? -offset : 0];
275 [NSAnimationContext endGrouping];
276}
277
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500278- (IBAction)openPreferences:(id)sender
279{
Alexandre Lisionbfa68f62015-09-10 08:38:42 -0400280 preferencesWC = [[PreferencesWC alloc] initWithWindowNibName:@"PreferencesWindow"];
281 [preferencesWC.window makeKeyAndOrderFront:preferencesWC.window];
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500282}
Alexandre Lision4a7b95e2015-02-20 10:06:43 -0500283
Alexandre Lisiona3a43dc2017-03-30 16:21:30 -0400284- (IBAction)showHistory:(NSButton*)sender
285{
286 [smartViewVC showHistory];
287}
288
289- (IBAction)showContacts:(NSButton*)sender
290{
291 [smartViewVC showContacts];
292}
293
294- (IBAction)showSmartlist:(NSButton*)sender
295{
296 [smartViewVC showSmartlist];
297}
298
Alexandre Lision624b1a82016-09-11 19:29:01 -0400299#pragma mark - Ring account migration
300
301- (void) migrateRingAccount:(Account*) acc
302{
303 self.migrateWC = [[MigrateRingAccountsWC alloc] initWithDelegate:self actionCode:1];
304 self.migrateWC.account = acc;
305#if MAC_OS_X_VERSION_MIN_REQUIRED > MAC_OS_X_VERSION_10_9
306 [self.window beginSheet:self.migrateWC.window completionHandler:nil];
307#else
308 [NSApp beginSheet: self.migrateWC.window
309 modalForWindow: self.window
310 modalDelegate: self
311 didEndSelector: nil
312 contextInfo: nil];
313#endif
314}
315
316- (void)checkAccountsToMigrate
317{
318 auto ringList = AccountModel::instance().accountsToMigrate();
319 if (ringList.length() > 0){
320 Account* acc = ringList.value(0);
321 [self migrateRingAccount:acc];
322 } else {
323 // Fresh run, we need to make sure RingID appears
Alexandre Lision624b1a82016-09-11 19:29:01 -0400324 [shareButton sendActionOn:NSLeftMouseDownMask];
325
326 [self connect];
Kateryna Kostiuka7248462017-04-18 16:15:31 -0400327 [self updateRingID];
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400328 // display accounts to select
329 NSToolbar *toolbar = self.window.toolbar;
330 toolbar.delegate = self;
331 [toolbar insertItemWithItemIdentifier:kChangeAccountToolBarItemIdentifier atIndex:1];
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -0400332 [toolbar insertItemWithItemIdentifier:kTrustRequestMenuItemIdentifier atIndex:2];
Alexandre Lision624b1a82016-09-11 19:29:01 -0400333 }
334}
335
336- (void)migrationDidComplete
337{
338 [self checkAccountsToMigrate];
339}
340
341- (void)migrationDidCompleteWithError
342{
343 [self checkAccountsToMigrate];
344}
345
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400346#pragma mark - NSToolbarDelegate
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -0400347- (nullable NSToolbarItem *)toolbar:(NSToolbar *)toolbar itemForItemIdentifier:(NSString *)itemIdentifier willBeInsertedIntoToolbar:(BOOL)flag
348{
349 if(itemIdentifier == kChangeAccountToolBarItemIdentifier) {
350 NSToolbarItem *toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:kChangeAccountToolBarItemIdentifier];
351 toolbarItem.view = chooseAccountVC.view;
352 return toolbarItem;
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400353 }
Kateryna Kostiukdb1f3a12017-04-24 12:08:28 -0400354 if(itemIdentifier == kTrustRequestMenuItemIdentifier) {
355 NSToolbarItem *toolbarItem = [[NSToolbarItem alloc] initWithItemIdentifier:kTrustRequestMenuItemIdentifier];
356 toolbarItem.view = contactRequestVC.view;
357 return toolbarItem;
358 }
359 return nil;
Kateryna Kostiuk13b76882017-03-30 09:18:44 -0400360}
361
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500362@end