blob: a9fdbdcb250295c95e2f942874d81176a51e8be5 [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>
Andreas Traczykf1d21902018-04-09 11:01:49 -040030#import <QDebug>
Alexandre Lision745e4d62015-03-22 20:03:10 -040031#import <account.h>
Kateryna Kostiukabf4e272017-04-18 14:18:00 -040032#import <AvailableAccountModel.h>
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040033#import <api/lrc.h>
Kateryna Kostiukabf4e272017-04-18 14:18:00 -040034
Alexandre Lision745e4d62015-03-22 20:03:10 -040035
Alexandre Lision3d4143a2015-06-10 14:27:49 -040036#if ENABLE_SPARKLE
37#import <Sparkle/Sparkle.h>
38#endif
39
Alexandre Lisionc65310c2015-04-23 16:44:23 -040040#import "Constants.h"
Alexandre Lision745e4d62015-03-22 20:03:10 -040041#import "RingWizardWC.h"
Alexandre Lision62005312016-01-28 15:55:16 -050042#import "DialpadWC.h"
Alexandre Lision745e4d62015-03-22 20:03:10 -040043
Alexandre Lision3d4143a2015-06-10 14:27:49 -040044#if ENABLE_SPARKLE
45@interface AppDelegate() <SUUpdaterDelegate>
46#else
Alexandre Lision745e4d62015-03-22 20:03:10 -040047@interface AppDelegate()
Alexandre Lision3d4143a2015-06-10 14:27:49 -040048#endif
Alexandre Lision745e4d62015-03-22 20:03:10 -040049
50@property RingWindowController* ringWindowController;
51@property RingWizardWC* wizard;
Alexandre Lision62005312016-01-28 15:55:16 -050052@property DialpadWC* dialpad;
Alexandre Lisionb9f3f942016-07-23 14:29:33 -040053@property (nonatomic, strong) dispatch_queue_t scNetworkQueue;
54@property (nonatomic, assign) SCNetworkReachabilityRef currentReachability;
Adrien Béraud022b57a2018-09-15 15:49:07 -040055@property (strong) id activity;
Alexandre Lision745e4d62015-03-22 20:03:10 -040056
57@end
58
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040059@implementation AppDelegate {
Alexandre Lision5855b6a2015-02-03 11:31:05 -050060
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040061std::unique_ptr<lrc::api::Lrc> lrc;
62}
Andreas Traczyke4d6e782018-03-22 17:51:30 -040063
Alexandre Lision5855b6a2015-02-03 11:31:05 -050064- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
Kateryna Kostiuk0bc4c592018-06-05 13:35:19 -040065
66 // hide "Check for update" menu item when sparkle is disabled
67 NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];
68 NSMenu *ringMenu = [[mainMenu itemAtIndex:0] submenu];
69 NSMenuItem *updateItem = [ringMenu itemAtIndex:1];
70#if ENABLE_SPARKLE
71 updateItem.hidden = false;
72#else
73 updateItem.hidden = true;
74#endif
75
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050076 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints"];
77
Alexandre Lisione4041492015-03-20 18:20:43 -040078 [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
79
Edric Milaret81315412015-05-13 15:14:56 -040080 NSAppleEventManager* appleEventManager = [NSAppleEventManager sharedAppleEventManager];
81 [appleEventManager setEventHandler:self andSelector:@selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040082 lrc = std::make_unique<lrc::api::Lrc>();
Edric Milaret81315412015-05-13 15:14:56 -040083
Alexandre Lision745e4d62015-03-22 20:03:10 -040084 if([self checkForRingAccount]) {
85 [self showMainWindow];
86 } else {
87 [self showWizard];
88 }
Alexandre Lisione4041492015-03-20 18:20:43 -040089 [self connect];
Alexandre Lisionb9f3f942016-07-23 14:29:33 -040090
91 dispatch_queue_t queue = NULL;
92 queue = dispatch_queue_create("scNetworkReachability", DISPATCH_QUEUE_SERIAL);
93 [self setScNetworkQueue:queue];
94 [self beginObservingReachabilityStatus];
Adrien Béraud022b57a2018-09-15 15:49:07 -040095 NSActivityOptions options = NSActivitySuddenTerminationDisabled | NSActivityAutomaticTerminationDisabled | NSActivityBackground;
96 self.activity = [[NSProcessInfo processInfo] beginActivityWithOptions:options reason:@"Receiving calls and messages"];
Alexandre Lisionb9f3f942016-07-23 14:29:33 -040097}
98
99- (void) beginObservingReachabilityStatus
100{
101 SCNetworkReachabilityRef reachabilityRef = NULL;
102
103 void (^callbackBlock)(SCNetworkReachabilityFlags) = ^(SCNetworkReachabilityFlags flags) {
104 BOOL reachable = (flags & kSCNetworkReachabilityFlagsReachable) != 0;
105 [[NSOperationQueue mainQueue] addOperationWithBlock:^{
106 AccountModel::instance().slotConnectivityChanged();
107 }];
108 };
109
110 SCNetworkReachabilityContext context = {
111 .version = 0,
112 .info = (void *)CFBridgingRetain(callbackBlock),
113 .release = CFRelease
114 };
115
116 reachabilityRef = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "test");
117 if (SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context)){
118 if (!SCNetworkReachabilitySetDispatchQueue(reachabilityRef, [self scNetworkQueue]) ){
119 // Remove our callback if we can't use the queue
120 SCNetworkReachabilitySetCallback(reachabilityRef, NULL, NULL);
121 }
122 [self setCurrentReachability:reachabilityRef];
123 }
124}
125
126- (void) endObsvervingReachabilityStatusForHost:(NSString *)__unused host
127{
128 // Un-set the dispatch queue
129 if (SCNetworkReachabilitySetDispatchQueue([self currentReachability], NULL) ){
130 SCNetworkReachabilitySetCallback([self currentReachability], NULL, NULL);
131 }
132}
133
134static void ReachabilityCallback(SCNetworkReachabilityRef __unused target, SCNetworkConnectionFlags flags, void* info)
135{
136 void (^callbackBlock)(SCNetworkReachabilityFlags) = (__bridge id)info;
137 callbackBlock(flags);
Alexandre Lisione4041492015-03-20 18:20:43 -0400138}
139
140- (void) connect
141{
Andreas Traczykf1d21902018-04-09 11:01:49 -0400142
143 //ProfileModel::instance().addCollection<LocalProfileCollection>(LoadOptions::FORCE_ENABLED);
144 QObject::connect(&AccountModel::instance(),
145 &AccountModel::registrationChanged,
146 [=](Account* a, bool registration) {
147 qDebug() << "registrationChanged:" << a->id() << ":" << registration;
148 //track buddy for account
149 AccountModel::instance().subscribeToBuddies(a->id());
150 });
151
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400152 QObject::connect(&CallModel::instance(),
Alexandre Lisione4041492015-03-20 18:20:43 -0400153 &CallModel::incomingCall,
154 [=](Call* call) {
Kateryna Kostiukabf4e272017-04-18 14:18:00 -0400155 // on incoming call set selected account match call destination account
156 if (call->account()) {
157 QModelIndex index = call->account()->index();
158 index = AvailableAccountModel::instance().mapFromSource(index);
159
160 AvailableAccountModel::instance().selectionModel()->setCurrentIndex(index,
161 QItemSelectionModel::ClearAndSelect);
162 }
Alexandre Lisionc65310c2015-04-23 16:44:23 -0400163 BOOL shouldComeToForeground = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::WindowBehaviour];
164 BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::Notifications];
Alexandre Lision61d78a42015-10-06 11:22:47 -0400165 if (shouldComeToForeground) {
Alexandre Lisione4041492015-03-20 18:20:43 -0400166 [NSApp activateIgnoringOtherApps:YES];
Alexandre Lision61d78a42015-10-06 11:22:47 -0400167 if ([self.ringWindowController.window isMiniaturized]) {
168 [self.ringWindowController.window deminiaturize:self];
169 }
170 }
Alexandre Lisione4041492015-03-20 18:20:43 -0400171
172 if(shouldNotify) {
173 [self showIncomingNotification:call];
174 }
175 });
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500176
Kateryna Kostiukf9a72c02018-08-01 14:28:49 -0400177 QObject::connect(&media::RecordingModel::instance(),
178 &media::RecordingModel::newTextMessage,
179 [=](media::TextRecording* t, ContactMethod* cm) {
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500180
181 BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::Notifications];
182 auto qIdx = t->instantTextMessagingModel()->index(t->instantTextMessagingModel()->rowCount()-1, 0);
183
184 // Don't show a notification if we are sending the text OR window already has focus OR user disabled notifications
Kateryna Kostiukf9a72c02018-08-01 14:28:49 -0400185 if(qvariant_cast<media::Media::Direction>(qIdx.data((int)media::TextRecording::Role::Direction)) == media::Media::Direction::OUT
Alexandre Lision4baba4c2016-02-11 13:00:57 -0500186 || self.ringWindowController.window.isMainWindow || !shouldNotify)
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500187 return;
188
189 NSUserNotification* notification = [[NSUserNotification alloc] init];
190
Kateryna Kostiukf9a72c02018-08-01 14:28:49 -0400191 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 -0500192
193 [notification setTitle:localizedTitle];
194 [notification setSoundName:NSUserNotificationDefaultSoundName];
195 [notification setSubtitle:qIdx.data().toString().toNSString()];
196
197 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
198 });
Alexandre Lisione4041492015-03-20 18:20:43 -0400199}
200
201- (void) showIncomingNotification:(Call*) call{
Alexandre Lision34607032016-02-08 16:16:49 -0500202 NSUserNotification* notification = [[NSUserNotification alloc] init];
Alexandre Lisionc8180112016-01-27 11:27:50 -0500203 NSString* localizedTitle = [NSString stringWithFormat:
204 NSLocalizedString(@"Incoming call from %@", @"Incoming call from {Name}"), call->peerName().toNSString()];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500205 [notification setTitle:localizedTitle];
206 [notification setSoundName:NSUserNotificationDefaultSoundName];
Alexandre Lisione4041492015-03-20 18:20:43 -0400207
Alexandre Lision34607032016-02-08 16:16:49 -0500208 // try to activate action button
209 @try {
210 [notification setValue:@YES forKey:@"_showsButtons"];
211 }
212 @catch (NSException *exception) {
213 // private API _showsButtons has changed...
214 NSLog(@"Action button not activable on notification");
215 }
216 [notification setActionButtonTitle:NSLocalizedString(@"Refuse", @"Button Action")];
217
Alexandre Lisione4041492015-03-20 18:20:43 -0400218 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500219}
220
Alexandre Lision34607032016-02-08 16:16:49 -0500221- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
222{
223 if(notification.activationType == NSUserNotificationActivationTypeActionButtonClicked) {
224 CallModel::instance().selectedCall() << Call::Action::REFUSE;
225 } else {
226 [NSApp activateIgnoringOtherApps:YES];
227 if ([self.ringWindowController.window isMiniaturized]) {
228 [self.ringWindowController.window deminiaturize:self];
229 }
230 }
231}
232
Alexandre Lision745e4d62015-03-22 20:03:10 -0400233- (void) showWizard
234{
Alexandre Lision745e4d62015-03-22 20:03:10 -0400235 if(self.wizard == nil) {
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400236 self.wizard = [[RingWizardWC alloc] initWithNibName:@"RingWizard" bundle: nil accountmodel: &lrc->getAccountModel()];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400237 }
Alexandre Lision76d59692016-01-20 18:06:05 -0500238 [self.wizard.window makeKeyAndOrderFront:self];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400239}
240
241- (void) showMainWindow
242{
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400243 if(self.ringWindowController == nil) {
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400244 self.ringWindowController = [[RingWindowController alloc] initWithWindowNibName:@"RingWindow" bundle: nil accountModel:&lrc->getAccountModel() dataTransferModel:&lrc->getDataTransferModel() behaviourController:&lrc->getBehaviorController()];
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400245 }
Kateryna Kostiuk465cfbe2018-06-05 16:13:05 -0400246 [[NSApplication sharedApplication] removeWindowsItem:self.wizard.window];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400247 [self.ringWindowController.window makeKeyAndOrderFront:self];
248}
249
Alexandre Lision62005312016-01-28 15:55:16 -0500250- (void) showDialpad
251{
252 if (self.dialpad == nil) {
253 self.dialpad = [[DialpadWC alloc] initWithWindowNibName:@"Dialpad"];
254 }
255 [self.dialpad.window makeKeyAndOrderFront:self];
256}
257
258
Alexandre Lision745e4d62015-03-22 20:03:10 -0400259- (BOOL) checkForRingAccount
260{
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400261 BOOL foundRingAcc = NO;
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400262 for (int i = 0 ; i < AccountModel::instance().rowCount() ; ++i) {
263 QModelIndex idx = AccountModel::instance().index(i);
264 Account* acc = AccountModel::instance().getAccountByModelIndex(idx);
Alexandre Lision76d59692016-01-20 18:06:05 -0500265 if(acc->protocol() == Account::Protocol::RING && !acc->isNew()) {
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400266 if (acc->displayName().isEmpty())
267 acc->setDisplayName(acc->alias());
268 foundRingAcc = YES;
Alexandre Lision745e4d62015-03-22 20:03:10 -0400269 }
270 }
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400271 return foundRingAcc;
Alexandre Lision745e4d62015-03-22 20:03:10 -0400272}
273
Alexandre Lision18e1fcd2015-08-04 14:38:42 -0400274-(void)applicationWillFinishLaunching:(NSNotification *)aNotification
275{
276 NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
277 [appleEventManager setEventHandler:self
278 andSelector:@selector(handleGetURLEvent:withReplyEvent:)
279 forEventClass:kInternetEventClass andEventID:kAEGetURL];
280}
281
282/**
283 * Recognized patterns:
284 * - ring:<hash>
285 * - ring://<hash>
286 */
287- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
288{
289 NSString* query = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
290 NSURL* url = [[NSURL alloc] initWithString:query];
291 NSString* ringID = [url host];
292 if (!ringID) {
293 //not a valid NSURL, try to parse query directly
294 ringID = [query substringFromIndex:@"ring:".length];
295 }
296
297 // check for a valid ring hash
298 NSCharacterSet *hexSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefABCDEF"];
299 BOOL valid = [[ringID stringByTrimmingCharactersInSet:hexSet] isEqualToString:@""];
300
301 if(valid && ringID.length == 40) {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400302 Call* c = CallModel::instance().dialingCall();
Alexandre Lision18e1fcd2015-08-04 14:38:42 -0400303 c->setDialNumber(QString::fromNSString([NSString stringWithFormat:@"ring:%@",ringID]));
304 c << Call::Action::ACCEPT;
305 } else {
306 NSAlert *alert = [[NSAlert alloc] init];
307 [alert addButtonWithTitle:@"OK"];
308 [alert setMessageText:@"Error"];
309 [alert setInformativeText:@"ringID cannot be read from this URL."];
310 [alert setAlertStyle:NSWarningAlertStyle];
311 [alert runModal];
312 }
313}
314
Alexandre Lision745e4d62015-03-22 20:03:10 -0400315- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
316{
317 if([self checkForRingAccount]) {
318 [self showMainWindow];
319 } else {
320 [self showWizard];
321 }
322 return YES;
323}
324
Edric Milaret81315412015-05-13 15:14:56 -0400325- (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
326{
Kateryna Kostiuk0c0344a2018-07-27 10:35:01 -0400327 [[NSApplication sharedApplication] terminate:self];
Edric Milaret81315412015-05-13 15:14:56 -0400328}
329
330-(void)applicationWillTerminate:(NSNotification *)notification
331{
Alexandre Lision76d59692016-01-20 18:06:05 -0500332 [self cleanExit];
333}
334
335- (void) cleanExit
336{
Adrien Béraud022b57a2018-09-15 15:49:07 -0400337 if (self.activity != nil) {
338 [[NSProcessInfo processInfo] endActivity:self.activity];
339 self.activity = nil;
Andreas Traczyke4d6e782018-03-22 17:51:30 -0400340 }
Alexandre Lision76d59692016-01-20 18:06:05 -0500341 [self.wizard close];
342 [self.ringWindowController close];
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400343 delete CallModel::instance().QObject::parent();
Edric Milaret81315412015-05-13 15:14:56 -0400344 [[NSApplication sharedApplication] terminate:self];
345}
346
Alexandre Lision3d4143a2015-06-10 14:27:49 -0400347#if ENABLE_SPARKLE
348
Alexandre Lision76d59692016-01-20 18:06:05 -0500349#pragma mark - Sparkle delegate
Alexandre Lision3d4143a2015-06-10 14:27:49 -0400350
351- (void)updater:(SUUpdater *)updater willInstallUpdate:(SUAppcastItem *)update
352{
353 [NSApp activateIgnoringOtherApps:YES];
354}
355
356- (BOOL)updaterMayCheckForUpdates:(SUUpdater *)bundle
357{
358 return YES;
359}
360
361- (BOOL)updaterShouldRelaunchApplication:(SUUpdater *)updater
362{
363 return YES;
364}
365
366- (void)updater:(SUUpdater *)updater didAbortWithError:(NSError *)error
367{
368 NSLog(@"Error:%@", error.localizedDescription);
369}
370
371#endif
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500372@end