blob: 9064c71e58370eec82089047219ae4be2a034953 [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>
31
Alexandre Lision3d4143a2015-06-10 14:27:49 -040032#if ENABLE_SPARKLE
33#import <Sparkle/Sparkle.h>
34#endif
35
Alexandre Lisionc65310c2015-04-23 16:44:23 -040036#import "Constants.h"
Alexandre Lision745e4d62015-03-22 20:03:10 -040037#import "RingWizardWC.h"
38
Alexandre Lision3d4143a2015-06-10 14:27:49 -040039#if ENABLE_SPARKLE
40@interface AppDelegate() <SUUpdaterDelegate>
41#else
Alexandre Lision745e4d62015-03-22 20:03:10 -040042@interface AppDelegate()
Alexandre Lision3d4143a2015-06-10 14:27:49 -040043#endif
Alexandre Lision745e4d62015-03-22 20:03:10 -040044
45@property RingWindowController* ringWindowController;
46@property RingWizardWC* wizard;
Alexandre Lisionb9f3f942016-07-23 14:29:33 -040047@property (nonatomic, strong) dispatch_queue_t scNetworkQueue;
48@property (nonatomic, assign) SCNetworkReachabilityRef currentReachability;
Alexandre Lision745e4d62015-03-22 20:03:10 -040049
50@end
51
Alexandre Lision5855b6a2015-02-03 11:31:05 -050052@implementation AppDelegate
53
54- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050055 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints"];
56
Alexandre Lisione4041492015-03-20 18:20:43 -040057 [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
58
Edric Milaret81315412015-05-13 15:14:56 -040059 NSAppleEventManager* appleEventManager = [NSAppleEventManager sharedAppleEventManager];
60 [appleEventManager setEventHandler:self andSelector:@selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication];
61
Alexandre Lision745e4d62015-03-22 20:03:10 -040062 if([self checkForRingAccount]) {
63 [self showMainWindow];
64 } else {
65 [self showWizard];
66 }
Alexandre Lisione4041492015-03-20 18:20:43 -040067 [self connect];
Alexandre Lisionb9f3f942016-07-23 14:29:33 -040068
69 dispatch_queue_t queue = NULL;
70 queue = dispatch_queue_create("scNetworkReachability", DISPATCH_QUEUE_SERIAL);
71 [self setScNetworkQueue:queue];
72 [self beginObservingReachabilityStatus];
73}
74
75- (void) beginObservingReachabilityStatus
76{
77 SCNetworkReachabilityRef reachabilityRef = NULL;
78
79 void (^callbackBlock)(SCNetworkReachabilityFlags) = ^(SCNetworkReachabilityFlags flags) {
80 BOOL reachable = (flags & kSCNetworkReachabilityFlagsReachable) != 0;
81 [[NSOperationQueue mainQueue] addOperationWithBlock:^{
82 AccountModel::instance().slotConnectivityChanged();
83 }];
84 };
85
86 SCNetworkReachabilityContext context = {
87 .version = 0,
88 .info = (void *)CFBridgingRetain(callbackBlock),
89 .release = CFRelease
90 };
91
92 reachabilityRef = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "test");
93 if (SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context)){
94 if (!SCNetworkReachabilitySetDispatchQueue(reachabilityRef, [self scNetworkQueue]) ){
95 // Remove our callback if we can't use the queue
96 SCNetworkReachabilitySetCallback(reachabilityRef, NULL, NULL);
97 }
98 [self setCurrentReachability:reachabilityRef];
99 }
100}
101
102- (void) endObsvervingReachabilityStatusForHost:(NSString *)__unused host
103{
104 // Un-set the dispatch queue
105 if (SCNetworkReachabilitySetDispatchQueue([self currentReachability], NULL) ){
106 SCNetworkReachabilitySetCallback([self currentReachability], NULL, NULL);
107 }
108}
109
110static void ReachabilityCallback(SCNetworkReachabilityRef __unused target, SCNetworkConnectionFlags flags, void* info)
111{
112 void (^callbackBlock)(SCNetworkReachabilityFlags) = (__bridge id)info;
113 callbackBlock(flags);
Alexandre Lisione4041492015-03-20 18:20:43 -0400114}
115
116- (void) connect
117{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400118 QObject::connect(&CallModel::instance(),
Alexandre Lisione4041492015-03-20 18:20:43 -0400119 &CallModel::incomingCall,
120 [=](Call* call) {
Alexandre Lisionc65310c2015-04-23 16:44:23 -0400121 BOOL shouldComeToForeground = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::WindowBehaviour];
122 BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::Notifications];
Alexandre Lision61d78a42015-10-06 11:22:47 -0400123 if (shouldComeToForeground) {
Alexandre Lisione4041492015-03-20 18:20:43 -0400124 [NSApp activateIgnoringOtherApps:YES];
Alexandre Lision61d78a42015-10-06 11:22:47 -0400125 if ([self.ringWindowController.window isMiniaturized]) {
126 [self.ringWindowController.window deminiaturize:self];
127 }
128 }
Alexandre Lisione4041492015-03-20 18:20:43 -0400129
130 if(shouldNotify) {
131 [self showIncomingNotification:call];
132 }
133 });
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500134
135
Alexandre Lision08e72142016-01-19 13:29:36 -0500136 QObject::connect(&Media::RecordingModel::instance(),
137 &Media::RecordingModel::unreadMessagesCountChanged,
138 [=](int unreadCount) {
139 NSDockTile *tile = [[NSApplication sharedApplication] dockTile];
140 NSString* label = unreadCount ? [NSString stringWithFormat:@"%d", unreadCount]: @"";
141 [tile setBadgeLabel:label];
Alexandre Lisionef324562016-01-21 13:23:21 -0500142 [NSApp requestUserAttention:NSCriticalRequest];
Alexandre Lision08e72142016-01-19 13:29:36 -0500143 });
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500144
145 QObject::connect(&Media::RecordingModel::instance(),
146 &Media::RecordingModel::newTextMessage,
147 [=](Media::TextRecording* t, ContactMethod* cm) {
148
149 BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::Notifications];
150 auto qIdx = t->instantTextMessagingModel()->index(t->instantTextMessagingModel()->rowCount()-1, 0);
151
152 // Don't show a notification if we are sending the text OR window already has focus OR user disabled notifications
153 if(qvariant_cast<Media::Media::Direction>(qIdx.data((int)Media::TextRecording::Role::Direction)) == Media::Media::Direction::OUT
Alexandre Lision4baba4c2016-02-11 13:00:57 -0500154 || self.ringWindowController.window.isMainWindow || !shouldNotify)
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500155 return;
156
157 NSUserNotification* notification = [[NSUserNotification alloc] init];
158
Alexandre Lisionc8180112016-01-27 11:27:50 -0500159 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 -0500160
161 [notification setTitle:localizedTitle];
162 [notification setSoundName:NSUserNotificationDefaultSoundName];
163 [notification setSubtitle:qIdx.data().toString().toNSString()];
164
165 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
166 });
Alexandre Lisione4041492015-03-20 18:20:43 -0400167}
168
169- (void) showIncomingNotification:(Call*) call{
Alexandre Lision34607032016-02-08 16:16:49 -0500170 NSUserNotification* notification = [[NSUserNotification alloc] init];
Alexandre Lisionc8180112016-01-27 11:27:50 -0500171 NSString* localizedTitle = [NSString stringWithFormat:
172 NSLocalizedString(@"Incoming call from %@", @"Incoming call from {Name}"), call->peerName().toNSString()];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500173 [notification setTitle:localizedTitle];
174 [notification setSoundName:NSUserNotificationDefaultSoundName];
Alexandre Lisione4041492015-03-20 18:20:43 -0400175
Alexandre Lision34607032016-02-08 16:16:49 -0500176 // try to activate action button
177 @try {
178 [notification setValue:@YES forKey:@"_showsButtons"];
179 }
180 @catch (NSException *exception) {
181 // private API _showsButtons has changed...
182 NSLog(@"Action button not activable on notification");
183 }
184 [notification setActionButtonTitle:NSLocalizedString(@"Refuse", @"Button Action")];
185
Alexandre Lisione4041492015-03-20 18:20:43 -0400186 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500187}
188
Alexandre Lision34607032016-02-08 16:16:49 -0500189- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
190{
191 if(notification.activationType == NSUserNotificationActivationTypeActionButtonClicked) {
192 CallModel::instance().selectedCall() << Call::Action::REFUSE;
193 } else {
194 [NSApp activateIgnoringOtherApps:YES];
195 if ([self.ringWindowController.window isMiniaturized]) {
196 [self.ringWindowController.window deminiaturize:self];
197 }
198 }
199}
200
Alexandre Lision745e4d62015-03-22 20:03:10 -0400201/**
202 * click in MainMenu "Setup Ring"
203 */
204- (IBAction)showWizard:(id)sender {
205 [self showWizard];
206}
207
208- (void) showWizard
209{
Alexandre Lision745e4d62015-03-22 20:03:10 -0400210 if(self.wizard == nil) {
211 self.wizard = [[RingWizardWC alloc] initWithWindowNibName:@"RingWizard"];
212 }
Alexandre Lision76d59692016-01-20 18:06:05 -0500213 [self.wizard.window makeKeyAndOrderFront:self];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400214}
215
216- (void) showMainWindow
217{
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400218 if(self.ringWindowController == nil) {
Alexandre Lision745e4d62015-03-22 20:03:10 -0400219 self.ringWindowController = [[RingWindowController alloc] initWithWindowNibName:@"RingWindow"];
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400220 }
Alexandre Lision745e4d62015-03-22 20:03:10 -0400221 [self.ringWindowController.window makeKeyAndOrderFront:self];
222}
223
224- (BOOL) checkForRingAccount
225{
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400226 BOOL foundRingAcc = NO;
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400227 for (int i = 0 ; i < AccountModel::instance().rowCount() ; ++i) {
228 QModelIndex idx = AccountModel::instance().index(i);
229 Account* acc = AccountModel::instance().getAccountByModelIndex(idx);
Alexandre Lision76d59692016-01-20 18:06:05 -0500230 if(acc->protocol() == Account::Protocol::RING && !acc->isNew()) {
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400231 if (acc->displayName().isEmpty())
232 acc->setDisplayName(acc->alias());
233 foundRingAcc = YES;
Alexandre Lision745e4d62015-03-22 20:03:10 -0400234 }
235 }
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400236 return foundRingAcc;
Alexandre Lision745e4d62015-03-22 20:03:10 -0400237}
238
Alexandre Lision18e1fcd2015-08-04 14:38:42 -0400239-(void)applicationWillFinishLaunching:(NSNotification *)aNotification
240{
241 NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
242 [appleEventManager setEventHandler:self
243 andSelector:@selector(handleGetURLEvent:withReplyEvent:)
244 forEventClass:kInternetEventClass andEventID:kAEGetURL];
245}
246
247/**
248 * Recognized patterns:
249 * - ring:<hash>
250 * - ring://<hash>
251 */
252- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
253{
254 NSString* query = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
255 NSURL* url = [[NSURL alloc] initWithString:query];
256 NSString* ringID = [url host];
257 if (!ringID) {
258 //not a valid NSURL, try to parse query directly
259 ringID = [query substringFromIndex:@"ring:".length];
260 }
261
262 // check for a valid ring hash
263 NSCharacterSet *hexSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefABCDEF"];
264 BOOL valid = [[ringID stringByTrimmingCharactersInSet:hexSet] isEqualToString:@""];
265
266 if(valid && ringID.length == 40) {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400267 Call* c = CallModel::instance().dialingCall();
Alexandre Lision18e1fcd2015-08-04 14:38:42 -0400268 c->setDialNumber(QString::fromNSString([NSString stringWithFormat:@"ring:%@",ringID]));
269 c << Call::Action::ACCEPT;
270 } else {
271 NSAlert *alert = [[NSAlert alloc] init];
272 [alert addButtonWithTitle:@"OK"];
273 [alert setMessageText:@"Error"];
274 [alert setInformativeText:@"ringID cannot be read from this URL."];
275 [alert setAlertStyle:NSWarningAlertStyle];
276 [alert runModal];
277 }
278}
279
Alexandre Lision745e4d62015-03-22 20:03:10 -0400280- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
281{
282 if([self checkForRingAccount]) {
283 [self showMainWindow];
284 } else {
285 [self showWizard];
286 }
287 return YES;
288}
289
Edric Milaret81315412015-05-13 15:14:56 -0400290- (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
291{
Alexandre Lision76d59692016-01-20 18:06:05 -0500292 [self cleanExit];
Edric Milaret81315412015-05-13 15:14:56 -0400293}
294
295-(void)applicationWillTerminate:(NSNotification *)notification
296{
Alexandre Lision76d59692016-01-20 18:06:05 -0500297 [self cleanExit];
298}
299
300- (void) cleanExit
301{
302 [self.wizard close];
303 [self.ringWindowController close];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400304 delete CallModel::instance().QObject::parent();
Edric Milaret81315412015-05-13 15:14:56 -0400305 [[NSApplication sharedApplication] terminate:self];
306}
307
Alexandre Lision3d4143a2015-06-10 14:27:49 -0400308#if ENABLE_SPARKLE
309
Alexandre Lision76d59692016-01-20 18:06:05 -0500310#pragma mark - Sparkle delegate
Alexandre Lision3d4143a2015-06-10 14:27:49 -0400311
312- (void)updater:(SUUpdater *)updater willInstallUpdate:(SUAppcastItem *)update
313{
314 [NSApp activateIgnoringOtherApps:YES];
315}
316
317- (BOOL)updaterMayCheckForUpdates:(SUUpdater *)bundle
318{
319 return YES;
320}
321
322- (BOOL)updaterShouldRelaunchApplication:(SUUpdater *)updater
323{
324 return YES;
325}
326
327- (void)updater:(SUUpdater *)updater didAbortWithError:(NSError *)error
328{
329 NSLog(@"Error:%@", error.localizedDescription);
330}
331
332#endif
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500333@end