blob: c75002ec09b92309b01ad1c02340c81dd6a82f29 [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 Lisionb9f3f942016-07-23 14:29:33 -040019#import <SystemConfiguration/SystemConfiguration.h>
20
Alexandre Lision5855b6a2015-02-03 11:31:05 -050021#import "AppDelegate.h"
22
Alexandre Lisione4041492015-03-20 18:20:43 -040023#import <callmodel.h>
Edric Milaret81315412015-05-13 15:14:56 -040024#import <qapplication.h>
Alexandre Lision745e4d62015-03-22 20:03:10 -040025#import <accountmodel.h>
26#import <protocolmodel.h>
Alexandre Lision0f66bd32016-01-18 11:30:45 -050027#import <media/recordingmodel.h>
28#import <media/textrecording.h>
Alexandre Lision745e4d62015-03-22 20:03:10 -040029#import <QItemSelectionModel>
30#import <account.h>
Kateryna Kostiukabf4e272017-04-18 14:18:00 -040031#import <AvailableAccountModel.h>
32
Alexandre Lision745e4d62015-03-22 20:03:10 -040033
Alexandre Lision3d4143a2015-06-10 14:27:49 -040034#if ENABLE_SPARKLE
35#import <Sparkle/Sparkle.h>
36#endif
37
Alexandre Lisionc65310c2015-04-23 16:44:23 -040038#import "Constants.h"
Alexandre Lision745e4d62015-03-22 20:03:10 -040039#import "RingWizardWC.h"
Alexandre Lision62005312016-01-28 15:55:16 -050040#import "DialpadWC.h"
Alexandre Lision745e4d62015-03-22 20:03:10 -040041
Alexandre Lision3d4143a2015-06-10 14:27:49 -040042#if ENABLE_SPARKLE
43@interface AppDelegate() <SUUpdaterDelegate>
44#else
Alexandre Lision745e4d62015-03-22 20:03:10 -040045@interface AppDelegate()
Alexandre Lision3d4143a2015-06-10 14:27:49 -040046#endif
Alexandre Lision745e4d62015-03-22 20:03:10 -040047
48@property RingWindowController* ringWindowController;
49@property RingWizardWC* wizard;
Alexandre Lision62005312016-01-28 15:55:16 -050050@property DialpadWC* dialpad;
Alexandre Lisionb9f3f942016-07-23 14:29:33 -040051@property (nonatomic, strong) dispatch_queue_t scNetworkQueue;
52@property (nonatomic, assign) SCNetworkReachabilityRef currentReachability;
Alexandre Lision745e4d62015-03-22 20:03:10 -040053
54@end
55
Alexandre Lision5855b6a2015-02-03 11:31:05 -050056@implementation AppDelegate
57
58- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050059 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints"];
60
Alexandre Lisione4041492015-03-20 18:20:43 -040061 [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
62
Edric Milaret81315412015-05-13 15:14:56 -040063 NSAppleEventManager* appleEventManager = [NSAppleEventManager sharedAppleEventManager];
64 [appleEventManager setEventHandler:self andSelector:@selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication];
65
Alexandre Lision745e4d62015-03-22 20:03:10 -040066 if([self checkForRingAccount]) {
67 [self showMainWindow];
68 } else {
69 [self showWizard];
70 }
Alexandre Lisione4041492015-03-20 18:20:43 -040071 [self connect];
Alexandre Lisionb9f3f942016-07-23 14:29:33 -040072
73 dispatch_queue_t queue = NULL;
74 queue = dispatch_queue_create("scNetworkReachability", DISPATCH_QUEUE_SERIAL);
75 [self setScNetworkQueue:queue];
76 [self beginObservingReachabilityStatus];
77}
78
79- (void) beginObservingReachabilityStatus
80{
81 SCNetworkReachabilityRef reachabilityRef = NULL;
82
83 void (^callbackBlock)(SCNetworkReachabilityFlags) = ^(SCNetworkReachabilityFlags flags) {
84 BOOL reachable = (flags & kSCNetworkReachabilityFlagsReachable) != 0;
85 [[NSOperationQueue mainQueue] addOperationWithBlock:^{
86 AccountModel::instance().slotConnectivityChanged();
87 }];
88 };
89
90 SCNetworkReachabilityContext context = {
91 .version = 0,
92 .info = (void *)CFBridgingRetain(callbackBlock),
93 .release = CFRelease
94 };
95
96 reachabilityRef = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "test");
97 if (SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context)){
98 if (!SCNetworkReachabilitySetDispatchQueue(reachabilityRef, [self scNetworkQueue]) ){
99 // Remove our callback if we can't use the queue
100 SCNetworkReachabilitySetCallback(reachabilityRef, NULL, NULL);
101 }
102 [self setCurrentReachability:reachabilityRef];
103 }
104}
105
106- (void) endObsvervingReachabilityStatusForHost:(NSString *)__unused host
107{
108 // Un-set the dispatch queue
109 if (SCNetworkReachabilitySetDispatchQueue([self currentReachability], NULL) ){
110 SCNetworkReachabilitySetCallback([self currentReachability], NULL, NULL);
111 }
112}
113
114static void ReachabilityCallback(SCNetworkReachabilityRef __unused target, SCNetworkConnectionFlags flags, void* info)
115{
116 void (^callbackBlock)(SCNetworkReachabilityFlags) = (__bridge id)info;
117 callbackBlock(flags);
Alexandre Lisione4041492015-03-20 18:20:43 -0400118}
119
120- (void) connect
121{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400122 QObject::connect(&CallModel::instance(),
Alexandre Lisione4041492015-03-20 18:20:43 -0400123 &CallModel::incomingCall,
124 [=](Call* call) {
Kateryna Kostiukabf4e272017-04-18 14:18:00 -0400125 // on incoming call set selected account match call destination account
126 if (call->account()) {
127 QModelIndex index = call->account()->index();
128 index = AvailableAccountModel::instance().mapFromSource(index);
129
130 AvailableAccountModel::instance().selectionModel()->setCurrentIndex(index,
131 QItemSelectionModel::ClearAndSelect);
132 }
Alexandre Lisionc65310c2015-04-23 16:44:23 -0400133 BOOL shouldComeToForeground = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::WindowBehaviour];
134 BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::Notifications];
Alexandre Lision61d78a42015-10-06 11:22:47 -0400135 if (shouldComeToForeground) {
Alexandre Lisione4041492015-03-20 18:20:43 -0400136 [NSApp activateIgnoringOtherApps:YES];
Alexandre Lision61d78a42015-10-06 11:22:47 -0400137 if ([self.ringWindowController.window isMiniaturized]) {
138 [self.ringWindowController.window deminiaturize:self];
139 }
140 }
Alexandre Lisione4041492015-03-20 18:20:43 -0400141
142 if(shouldNotify) {
143 [self showIncomingNotification:call];
144 }
145 });
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500146
147
Alexandre Lision08e72142016-01-19 13:29:36 -0500148 QObject::connect(&Media::RecordingModel::instance(),
149 &Media::RecordingModel::unreadMessagesCountChanged,
150 [=](int unreadCount) {
151 NSDockTile *tile = [[NSApplication sharedApplication] dockTile];
152 NSString* label = unreadCount ? [NSString stringWithFormat:@"%d", unreadCount]: @"";
153 [tile setBadgeLabel:label];
Alexandre Lisionef324562016-01-21 13:23:21 -0500154 [NSApp requestUserAttention:NSCriticalRequest];
Alexandre Lision08e72142016-01-19 13:29:36 -0500155 });
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500156
157 QObject::connect(&Media::RecordingModel::instance(),
158 &Media::RecordingModel::newTextMessage,
159 [=](Media::TextRecording* t, ContactMethod* cm) {
160
161 BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::Notifications];
162 auto qIdx = t->instantTextMessagingModel()->index(t->instantTextMessagingModel()->rowCount()-1, 0);
163
164 // Don't show a notification if we are sending the text OR window already has focus OR user disabled notifications
165 if(qvariant_cast<Media::Media::Direction>(qIdx.data((int)Media::TextRecording::Role::Direction)) == Media::Media::Direction::OUT
Alexandre Lision4baba4c2016-02-11 13:00:57 -0500166 || self.ringWindowController.window.isMainWindow || !shouldNotify)
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500167 return;
168
169 NSUserNotification* notification = [[NSUserNotification alloc] init];
170
Alexandre Lisionc8180112016-01-27 11:27:50 -0500171 NSString* localizedTitle = [NSString stringWithFormat:NSLocalizedString(@"Message from %@", @"Message from {Name}"), qIdx.data((int)Media::TextRecording::Role::AuthorDisplayname).toString().toNSString()];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500172
173 [notification setTitle:localizedTitle];
174 [notification setSoundName:NSUserNotificationDefaultSoundName];
175 [notification setSubtitle:qIdx.data().toString().toNSString()];
176
177 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
178 });
Alexandre Lisione4041492015-03-20 18:20:43 -0400179}
180
181- (void) showIncomingNotification:(Call*) call{
Alexandre Lision34607032016-02-08 16:16:49 -0500182 NSUserNotification* notification = [[NSUserNotification alloc] init];
Alexandre Lisionc8180112016-01-27 11:27:50 -0500183 NSString* localizedTitle = [NSString stringWithFormat:
184 NSLocalizedString(@"Incoming call from %@", @"Incoming call from {Name}"), call->peerName().toNSString()];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500185 [notification setTitle:localizedTitle];
186 [notification setSoundName:NSUserNotificationDefaultSoundName];
Alexandre Lisione4041492015-03-20 18:20:43 -0400187
Alexandre Lision34607032016-02-08 16:16:49 -0500188 // try to activate action button
189 @try {
190 [notification setValue:@YES forKey:@"_showsButtons"];
191 }
192 @catch (NSException *exception) {
193 // private API _showsButtons has changed...
194 NSLog(@"Action button not activable on notification");
195 }
196 [notification setActionButtonTitle:NSLocalizedString(@"Refuse", @"Button Action")];
197
Alexandre Lisione4041492015-03-20 18:20:43 -0400198 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500199}
200
Alexandre Lision34607032016-02-08 16:16:49 -0500201- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
202{
203 if(notification.activationType == NSUserNotificationActivationTypeActionButtonClicked) {
204 CallModel::instance().selectedCall() << Call::Action::REFUSE;
205 } else {
206 [NSApp activateIgnoringOtherApps:YES];
207 if ([self.ringWindowController.window isMiniaturized]) {
208 [self.ringWindowController.window deminiaturize:self];
209 }
210 }
211}
212
Alexandre Lision745e4d62015-03-22 20:03:10 -0400213- (void) showWizard
214{
Alexandre Lision745e4d62015-03-22 20:03:10 -0400215 if(self.wizard == nil) {
216 self.wizard = [[RingWizardWC alloc] initWithWindowNibName:@"RingWizard"];
217 }
Alexandre Lision76d59692016-01-20 18:06:05 -0500218 [self.wizard.window makeKeyAndOrderFront:self];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400219}
220
221- (void) showMainWindow
222{
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400223 if(self.ringWindowController == nil) {
Alexandre Lision745e4d62015-03-22 20:03:10 -0400224 self.ringWindowController = [[RingWindowController alloc] initWithWindowNibName:@"RingWindow"];
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400225 }
Alexandre Lision745e4d62015-03-22 20:03:10 -0400226 [self.ringWindowController.window makeKeyAndOrderFront:self];
227}
228
Alexandre Lision62005312016-01-28 15:55:16 -0500229- (void) showDialpad
230{
231 if (self.dialpad == nil) {
232 self.dialpad = [[DialpadWC alloc] initWithWindowNibName:@"Dialpad"];
233 }
234 [self.dialpad.window makeKeyAndOrderFront:self];
235}
236
237
Alexandre Lision745e4d62015-03-22 20:03:10 -0400238- (BOOL) checkForRingAccount
239{
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400240 BOOL foundRingAcc = NO;
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400241 for (int i = 0 ; i < AccountModel::instance().rowCount() ; ++i) {
242 QModelIndex idx = AccountModel::instance().index(i);
243 Account* acc = AccountModel::instance().getAccountByModelIndex(idx);
Alexandre Lision76d59692016-01-20 18:06:05 -0500244 if(acc->protocol() == Account::Protocol::RING && !acc->isNew()) {
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400245 if (acc->displayName().isEmpty())
246 acc->setDisplayName(acc->alias());
247 foundRingAcc = YES;
Alexandre Lision745e4d62015-03-22 20:03:10 -0400248 }
249 }
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400250 return foundRingAcc;
Alexandre Lision745e4d62015-03-22 20:03:10 -0400251}
252
Alexandre Lision18e1fcd2015-08-04 14:38:42 -0400253-(void)applicationWillFinishLaunching:(NSNotification *)aNotification
254{
255 NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
256 [appleEventManager setEventHandler:self
257 andSelector:@selector(handleGetURLEvent:withReplyEvent:)
258 forEventClass:kInternetEventClass andEventID:kAEGetURL];
259}
260
261/**
262 * Recognized patterns:
263 * - ring:<hash>
264 * - ring://<hash>
265 */
266- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
267{
268 NSString* query = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
269 NSURL* url = [[NSURL alloc] initWithString:query];
270 NSString* ringID = [url host];
271 if (!ringID) {
272 //not a valid NSURL, try to parse query directly
273 ringID = [query substringFromIndex:@"ring:".length];
274 }
275
276 // check for a valid ring hash
277 NSCharacterSet *hexSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefABCDEF"];
278 BOOL valid = [[ringID stringByTrimmingCharactersInSet:hexSet] isEqualToString:@""];
279
280 if(valid && ringID.length == 40) {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400281 Call* c = CallModel::instance().dialingCall();
Alexandre Lision18e1fcd2015-08-04 14:38:42 -0400282 c->setDialNumber(QString::fromNSString([NSString stringWithFormat:@"ring:%@",ringID]));
283 c << Call::Action::ACCEPT;
284 } else {
285 NSAlert *alert = [[NSAlert alloc] init];
286 [alert addButtonWithTitle:@"OK"];
287 [alert setMessageText:@"Error"];
288 [alert setInformativeText:@"ringID cannot be read from this URL."];
289 [alert setAlertStyle:NSWarningAlertStyle];
290 [alert runModal];
291 }
292}
293
Alexandre Lision745e4d62015-03-22 20:03:10 -0400294- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
295{
296 if([self checkForRingAccount]) {
297 [self showMainWindow];
298 } else {
299 [self showWizard];
300 }
301 return YES;
302}
303
Edric Milaret81315412015-05-13 15:14:56 -0400304- (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
305{
Alexandre Lision76d59692016-01-20 18:06:05 -0500306 [self cleanExit];
Edric Milaret81315412015-05-13 15:14:56 -0400307}
308
309-(void)applicationWillTerminate:(NSNotification *)notification
310{
Alexandre Lision76d59692016-01-20 18:06:05 -0500311 [self cleanExit];
312}
313
314- (void) cleanExit
315{
316 [self.wizard close];
317 [self.ringWindowController close];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400318 delete CallModel::instance().QObject::parent();
Edric Milaret81315412015-05-13 15:14:56 -0400319 [[NSApplication sharedApplication] terminate:self];
320}
321
Alexandre Lision3d4143a2015-06-10 14:27:49 -0400322#if ENABLE_SPARKLE
323
Alexandre Lision76d59692016-01-20 18:06:05 -0500324#pragma mark - Sparkle delegate
Alexandre Lision3d4143a2015-06-10 14:27:49 -0400325
326- (void)updater:(SUUpdater *)updater willInstallUpdate:(SUAppcastItem *)update
327{
328 [NSApp activateIgnoringOtherApps:YES];
329}
330
331- (BOOL)updaterMayCheckForUpdates:(SUUpdater *)bundle
332{
333 return YES;
334}
335
336- (BOOL)updaterShouldRelaunchApplication:(SUUpdater *)updater
337{
338 return YES;
339}
340
341- (void)updater:(SUUpdater *)updater didAbortWithError:(NSError *)error
342{
343 NSLog(@"Error:%@", error.localizedDescription);
344}
345
346#endif
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500347@end