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 | b9f3f94 | 2016-07-23 14:29:33 -0400 | [diff] [blame] | 19 | #import <SystemConfiguration/SystemConfiguration.h> |
Kateryna Kostiuk | d5aded7 | 2020-11-06 17:03:42 -0500 | [diff] [blame] | 20 | #import <CoreFoundation/CoreFoundation.h> |
| 21 | #import <IOKit/pwr_mgt/IOPMLib.h> |
Alexandre Lision | b9f3f94 | 2016-07-23 14:29:33 -0400 | [diff] [blame] | 22 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 23 | #import "AppDelegate.h" |
| 24 | |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 25 | //lrc |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 26 | #import <api/lrc.h> |
Kateryna Kostiuk | 23222fe | 2018-11-16 14:28:02 -0500 | [diff] [blame] | 27 | #import <api/newaccountmodel.h> |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 28 | #import <api/behaviorcontroller.h> |
| 29 | #import <api/conversation.h> |
| 30 | #import <api/newcallmodel.h> |
Kateryna Kostiuk | abf4e27 | 2017-04-18 14:18:00 -0400 | [diff] [blame] | 31 | |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 32 | |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 33 | #if ENABLE_SPARKLE |
| 34 | #import <Sparkle/Sparkle.h> |
| 35 | #endif |
| 36 | |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 37 | #import "Constants.h" |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 38 | #import "RingWizardWC.h" |
Alexandre Lision | 6200531 | 2016-01-28 15:55:16 -0500 | [diff] [blame] | 39 | #import "DialpadWC.h" |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 40 | #import "utils.h" |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 41 | |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 42 | #if ENABLE_SPARKLE |
| 43 | @interface AppDelegate() <SUUpdaterDelegate> |
| 44 | #else |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 45 | @interface AppDelegate() |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 46 | #endif |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 47 | |
| 48 | @property RingWindowController* ringWindowController; |
| 49 | @property RingWizardWC* wizard; |
Alexandre Lision | 6200531 | 2016-01-28 15:55:16 -0500 | [diff] [blame] | 50 | @property DialpadWC* dialpad; |
Alexandre Lision | b9f3f94 | 2016-07-23 14:29:33 -0400 | [diff] [blame] | 51 | @property (nonatomic, strong) dispatch_queue_t scNetworkQueue; |
| 52 | @property (nonatomic, assign) SCNetworkReachabilityRef currentReachability; |
Adrien Béraud | 022b57a | 2018-09-15 15:49:07 -0400 | [diff] [blame] | 53 | @property (strong) id activity; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 54 | |
| 55 | @end |
| 56 | |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 57 | NSString * const MESSAGE_NOTIFICATION = @"message_notification_type"; |
| 58 | NSString * const CALL_NOTIFICATION = @"call_notification_type"; |
| 59 | NSString * const CONTACT_REQUEST_NOTIFICATION = @"contact_request_notification_type"; |
| 60 | |
| 61 | NSString * const ACCOUNT_ID = @"account_id_notification_info"; |
| 62 | NSString * const CALL_ID = @"call_id_notification_info"; |
| 63 | NSString * const CONVERSATION_ID = @"conversation_id_notification_info"; |
| 64 | NSString * const CONTACT_URI = @"contact_uri_notification_info"; |
| 65 | NSString * const NOTIFICATION_TYPE = @"contact_type_notification_info"; |
| 66 | |
Kateryna Kostiuk | d5aded7 | 2020-11-06 17:03:42 -0500 | [diff] [blame] | 67 | IOPMAssertionID assertionID = 0; |
| 68 | BOOL sleepDisabled = false; |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 69 | |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 70 | @implementation AppDelegate { |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 71 | |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 72 | std::unique_ptr<lrc::api::Lrc> lrc; |
| 73 | } |
Andreas Traczyk | e4d6e78 | 2018-03-22 17:51:30 -0400 | [diff] [blame] | 74 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 75 | - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { |
Kateryna Kostiuk | 0bc4c59 | 2018-06-05 13:35:19 -0400 | [diff] [blame] | 76 | |
| 77 | // hide "Check for update" menu item when sparkle is disabled |
| 78 | NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu]; |
| 79 | NSMenu *ringMenu = [[mainMenu itemAtIndex:0] submenu]; |
| 80 | NSMenuItem *updateItem = [ringMenu itemAtIndex:1]; |
| 81 | #if ENABLE_SPARKLE |
| 82 | updateItem.hidden = false; |
| 83 | #else |
| 84 | updateItem.hidden = true; |
| 85 | #endif |
| 86 | |
Kateryna Kostiuk | 353ca3f | 2019-01-08 12:02:40 -0500 | [diff] [blame] | 87 | #ifndef NDEBUG |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 88 | [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints"]; |
Kateryna Kostiuk | 353ca3f | 2019-01-08 12:02:40 -0500 | [diff] [blame] | 89 | #else |
| 90 | [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints"]; |
| 91 | #endif |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 92 | |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 93 | [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self]; |
| 94 | |
Edric Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 95 | NSAppleEventManager* appleEventManager = [NSAppleEventManager sharedAppleEventManager]; |
| 96 | [appleEventManager setEventHandler:self andSelector:@selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication]; |
Kateryna Kostiuk | 85a334e | 2018-12-03 15:54:19 -0500 | [diff] [blame] | 97 | |
| 98 | dispatch_queue_t queue = NULL; |
| 99 | queue = dispatch_queue_create("scNetworkReachability", DISPATCH_QUEUE_SERIAL); |
| 100 | [self setScNetworkQueue:queue]; |
| 101 | [self beginObservingReachabilityStatus]; |
| 102 | NSActivityOptions options = NSActivitySuddenTerminationDisabled | NSActivityAutomaticTerminationDisabled | NSActivityBackground; |
| 103 | self.activity = [[NSProcessInfo processInfo] beginActivityWithOptions:options reason:@"Receiving calls and messages"]; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 104 | lrc = std::make_unique<lrc::api::Lrc>(); |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 105 | if([self checkForRingAccount]) { |
Kateryna Kostiuk | 0c0801e | 2019-07-18 20:10:51 -0400 | [diff] [blame] | 106 | [self setRingtonePath]; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 107 | [self showMainWindow]; |
| 108 | } else { |
| 109 | [self showWizard]; |
| 110 | } |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 111 | [self connect]; |
Alexandre Lision | b9f3f94 | 2016-07-23 14:29:33 -0400 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | - (void) beginObservingReachabilityStatus |
| 115 | { |
| 116 | SCNetworkReachabilityRef reachabilityRef = NULL; |
| 117 | |
| 118 | void (^callbackBlock)(SCNetworkReachabilityFlags) = ^(SCNetworkReachabilityFlags flags) { |
Kateryna Kostiuk | 9217e29 | 2019-08-13 13:22:45 -0400 | [diff] [blame] | 119 | dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{ |
Kateryna Kostiuk | 4443201 | 2018-11-28 09:39:10 -0500 | [diff] [blame] | 120 | lrc->connectivityChanged(); |
Kateryna Kostiuk | 9217e29 | 2019-08-13 13:22:45 -0400 | [diff] [blame] | 121 | }); |
Alexandre Lision | b9f3f94 | 2016-07-23 14:29:33 -0400 | [diff] [blame] | 122 | }; |
| 123 | |
| 124 | SCNetworkReachabilityContext context = { |
| 125 | .version = 0, |
| 126 | .info = (void *)CFBridgingRetain(callbackBlock), |
| 127 | .release = CFRelease |
| 128 | }; |
| 129 | |
| 130 | reachabilityRef = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "test"); |
| 131 | if (SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context)){ |
| 132 | if (!SCNetworkReachabilitySetDispatchQueue(reachabilityRef, [self scNetworkQueue]) ){ |
| 133 | // Remove our callback if we can't use the queue |
| 134 | SCNetworkReachabilitySetCallback(reachabilityRef, NULL, NULL); |
| 135 | } |
| 136 | [self setCurrentReachability:reachabilityRef]; |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | - (void) endObsvervingReachabilityStatusForHost:(NSString *)__unused host |
| 141 | { |
| 142 | // Un-set the dispatch queue |
| 143 | if (SCNetworkReachabilitySetDispatchQueue([self currentReachability], NULL) ){ |
| 144 | SCNetworkReachabilitySetCallback([self currentReachability], NULL, NULL); |
| 145 | } |
| 146 | } |
| 147 | |
Kateryna Kostiuk | d5aded7 | 2020-11-06 17:03:42 -0500 | [diff] [blame] | 148 | - (void) disableScreenSleep |
| 149 | { |
| 150 | if (sleepDisabled) { |
| 151 | return; |
| 152 | } |
| 153 | CFStringRef reasonForActivity= CFSTR("Prevent display sleep during calls"); |
| 154 | sleepDisabled = IOPMAssertionCreateWithName(kIOPMAssertionTypeNoDisplaySleep, kIOPMAssertionLevelOn, reasonForActivity, &assertionID) == kIOReturnSuccess; |
| 155 | } |
| 156 | |
| 157 | - (void) restoreScreenSleep { |
| 158 | auto calls = [self getActiveCalls]; |
| 159 | |
| 160 | if (!sleepDisabled || !calls.empty()) { |
| 161 | return; |
| 162 | } |
| 163 | IOPMAssertionRelease(assertionID); |
| 164 | sleepDisabled = false; |
| 165 | } |
| 166 | |
Alexandre Lision | b9f3f94 | 2016-07-23 14:29:33 -0400 | [diff] [blame] | 167 | static void ReachabilityCallback(SCNetworkReachabilityRef __unused target, SCNetworkConnectionFlags flags, void* info) |
| 168 | { |
| 169 | void (^callbackBlock)(SCNetworkReachabilityFlags) = (__bridge id)info; |
| 170 | callbackBlock(flags); |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | - (void) connect |
| 174 | { |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 175 | QObject::connect(&lrc->getBehaviorController(), |
| 176 | &lrc::api::BehaviorController::newTrustRequest, |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 177 | [self] (const QString& accountId, const QString& contactUri) { |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 178 | BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ContactRequestNotifications]; |
| 179 | if(!shouldNotify) { |
| 180 | return; |
| 181 | } |
| 182 | NSUserNotification* notification = [[NSUserNotification alloc] init]; |
| 183 | auto contactModel = lrc->getAccountModel() |
| 184 | .getAccountInfo(accountId).contactModel.get(); |
| 185 | NSString* name = contactModel->getContact(contactUri) |
| 186 | .registeredName.isEmpty() ? |
| 187 | contactUri.toNSString(): |
| 188 | contactModel->getContact(contactUri).registeredName.toNSString(); |
| 189 | NSString* localizedMessage = |
| 190 | NSLocalizedString(@"Send you a contact request", |
| 191 | @"Notification message"); |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 192 | |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 193 | NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init]; |
| 194 | userInfo[ACCOUNT_ID] = accountId.toNSString(); |
| 195 | userInfo[CONTACT_URI] = contactUri.toNSString(); |
| 196 | userInfo[NOTIFICATION_TYPE] = CONTACT_REQUEST_NOTIFICATION; |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 197 | |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 198 | [notification setTitle: name]; |
| 199 | notification.informativeText = localizedMessage; |
| 200 | [notification setSoundName:NSUserNotificationDefaultSoundName]; |
| 201 | [notification setUserInfo: userInfo]; |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 202 | |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 203 | [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification]; |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 204 | |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 205 | }); |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 206 | |
| 207 | QObject::connect(&lrc->getBehaviorController(), |
| 208 | &lrc::api::BehaviorController::showIncomingCallView, |
Kateryna Kostiuk | a7b909c | 2020-10-19 11:46:26 -0400 | [diff] [blame] | 209 | [self] (const QString& accountId, |
| 210 | const QString& convUid) { |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 211 | BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::CallNotifications]; |
| 212 | if(!shouldNotify) { |
| 213 | return; |
| 214 | } |
Kateryna Kostiuk | a7b909c | 2020-10-19 11:46:26 -0400 | [diff] [blame] | 215 | auto convModel = lrc->getAccountModel().getAccountInfo(accountId).conversationModel.get(); |
| 216 | auto conversationOpt = getConversationFromUid(convUid, *convModel); |
| 217 | if (!conversationOpt.has_value()) { return; } |
kkostiuk | f81c637 | 2021-01-11 18:51:28 -0500 | [diff] [blame] | 218 | lrc::api::conversation::Info& conversation = *conversationOpt; |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 219 | bool isIncoming = false; |
Kateryna Kostiuk | a7b909c | 2020-10-19 11:46:26 -0400 | [diff] [blame] | 220 | auto callModel = lrc->getAccountModel().getAccountInfo(accountId).callModel.get(); |
| 221 | if(callModel->hasCall(conversation.callId)) { |
| 222 | isIncoming = !callModel->getCall(conversation.callId).isOutgoing; |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 223 | } |
| 224 | if(!isIncoming) { |
| 225 | return; |
| 226 | } |
Kateryna Kostiuk | a7b909c | 2020-10-19 11:46:26 -0400 | [diff] [blame] | 227 | NSString* name = bestIDForConversation(conversation, *lrc->getAccountModel().getAccountInfo(accountId).conversationModel.get()); |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 228 | NSUserNotification* notification = [[NSUserNotification alloc] init]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 229 | |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 230 | NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init]; |
| 231 | userInfo[ACCOUNT_ID] = accountId.toNSString(); |
Kateryna Kostiuk | a7b909c | 2020-10-19 11:46:26 -0400 | [diff] [blame] | 232 | userInfo[CALL_ID] = conversation.callId.toNSString(); |
| 233 | userInfo[CONVERSATION_ID] = conversation.uid.toNSString(); |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 234 | userInfo[NOTIFICATION_TYPE] = CALL_NOTIFICATION; |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 235 | |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 236 | NSString* localizedTitle = [NSString stringWithFormat: |
| 237 | NSLocalizedString(@"Incoming call from %@", @"Incoming call from {Name}"), |
| 238 | name]; |
| 239 | // try to activate action button |
| 240 | @try { |
| 241 | [notification setValue:@YES forKey:@"_showsButtons"]; |
| 242 | } |
| 243 | @catch (NSException *exception) { |
| 244 | NSLog(@"Action button not activable on notification"); |
| 245 | } |
| 246 | [notification setUserInfo: userInfo]; |
| 247 | [notification setOtherButtonTitle:NSLocalizedString(@"Refuse", @"Button Action")]; |
| 248 | [notification setActionButtonTitle:NSLocalizedString(@"Accept", @"Button Action")]; |
| 249 | [notification setTitle:localizedTitle]; |
| 250 | [notification setSoundName:NSUserNotificationDefaultSoundName]; |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 251 | |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 252 | [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification]; |
| 253 | }); |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 254 | |
| 255 | QObject::connect(&lrc->getBehaviorController(), |
| 256 | &lrc::api::BehaviorController::newUnreadInteraction, |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 257 | [self] (const QString& accountId, const QString& conversation, |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 258 | uint64_t interactionId, const lrc::api::interaction::Info& interaction) { |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 259 | BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::MessagesNotifications]; |
| 260 | if(!shouldNotify) { |
| 261 | return; |
| 262 | } |
| 263 | NSUserNotification* notification = [[NSUserNotification alloc] init]; |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 264 | |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 265 | NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init]; |
| 266 | userInfo[ACCOUNT_ID] = accountId.toNSString(); |
| 267 | userInfo[CONVERSATION_ID] = conversation.toNSString(); |
| 268 | userInfo[NOTIFICATION_TYPE] = MESSAGE_NOTIFICATION; |
| 269 | NSString* name = interaction.authorUri.toNSString(); |
Kateryna Kostiuk | a7b909c | 2020-10-19 11:46:26 -0400 | [diff] [blame] | 270 | auto convOpt = getConversationFromUid(conversation, *lrc->getAccountModel() |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 271 | .getAccountInfo(accountId) |
Kateryna Kostiuk | 3541ae2 | 2020-08-17 12:26:14 -0400 | [diff] [blame] | 272 | .conversationModel.get()); |
Kateryna Kostiuk | a7b909c | 2020-10-19 11:46:26 -0400 | [diff] [blame] | 273 | if (convOpt.has_value()) { |
kkostiuk | f81c637 | 2021-01-11 18:51:28 -0500 | [diff] [blame] | 274 | lrc::api::conversation::Info& conversation = *convOpt; |
Kateryna Kostiuk | a7b909c | 2020-10-19 11:46:26 -0400 | [diff] [blame] | 275 | name = bestIDForConversation(conversation, *lrc->getAccountModel().getAccountInfo(accountId).conversationModel.get()); |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 276 | } |
| 277 | NSString* localizedTitle = [NSString stringWithFormat: |
| 278 | NSLocalizedString(@"Incoming message from %@",@"Incoming message from {Name}"), |
| 279 | name]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 280 | |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 281 | [notification setTitle:localizedTitle]; |
| 282 | [notification setSoundName:NSUserNotificationDefaultSoundName]; |
| 283 | [notification setSubtitle:interaction.body.toNSString()]; |
| 284 | [notification setUserInfo:userInfo]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 285 | |
Kateryna Kostiuk | 146cb9e | 2020-08-11 14:29:26 -0400 | [diff] [blame] | 286 | [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification]; |
| 287 | }); |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 288 | } |
| 289 | |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 290 | - (void)userNotificationCenter:(NSUserNotificationCenter *)center didDismissAlert:(NSUserNotification *)alert { |
| 291 | // check if user click refuse on incoming call notifications |
| 292 | if(alert.activationType != NSUserNotificationActivationTypeNone) { |
| 293 | return; |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 294 | } |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 295 | |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 296 | auto info = alert.userInfo; |
| 297 | if(!info) { |
| 298 | return; |
| 299 | } |
| 300 | NSString* identifier = info[NOTIFICATION_TYPE]; |
| 301 | NSString* callId = info[CALL_ID]; |
| 302 | NSString* accountId = info[ACCOUNT_ID]; |
| 303 | if(!identifier || !callId || !accountId) { |
| 304 | return; |
| 305 | } |
| 306 | if([identifier isEqualToString: CALL_NOTIFICATION]) { |
| 307 | auto accountInfo = &lrc->getAccountModel().getAccountInfo([accountId UTF8String]); |
| 308 | if (accountInfo == nil) |
| 309 | return; |
| 310 | auto* callModel = accountInfo->callModel.get(); |
| 311 | callModel->hangUp([callId UTF8String]); |
| 312 | } |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 313 | } |
| 314 | |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 315 | - (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification |
| 316 | { |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 317 | auto info = notification.userInfo; |
| 318 | if(!info) { |
| 319 | return; |
| 320 | } |
| 321 | NSString* identifier = info[NOTIFICATION_TYPE]; |
| 322 | if([identifier isEqualToString: CALL_NOTIFICATION]) { |
| 323 | if(notification.activationType == NSUserNotificationActivationTypeActionButtonClicked |
| 324 | || notification.activationType == NSUserNotificationActivationTypeContentsClicked) { |
| 325 | NSString* callId = info[CALL_ID]; |
| 326 | NSString* accountId = info[ACCOUNT_ID]; |
| 327 | NSString *conversationId = info[CONVERSATION_ID]; |
| 328 | auto accountInfo = &lrc->getAccountModel().getAccountInfo([accountId UTF8String]); |
| 329 | if (accountInfo == nil) |
| 330 | return; |
| 331 | auto* callModel = accountInfo->callModel.get(); |
| 332 | callModel->accept([callId UTF8String]); |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 333 | [self.ringWindowController.window deminiaturize:self]; |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 334 | [_ringWindowController showCall:callId forAccount:accountId forConversation:conversationId]; |
| 335 | } |
| 336 | } else if(notification.activationType == NSUserNotificationActivationTypeContentsClicked) { |
| 337 | [self.ringWindowController.window deminiaturize:self]; |
| 338 | if([identifier isEqualToString: MESSAGE_NOTIFICATION]) { |
| 339 | NSString* accountId = info[ACCOUNT_ID]; |
| 340 | NSString *conversationId = info[CONVERSATION_ID]; |
| 341 | [_ringWindowController showConversation:conversationId forAccount:accountId]; |
| 342 | } else if([identifier isEqualToString: CONTACT_REQUEST_NOTIFICATION]) { |
| 343 | NSString* accountId = info[ACCOUNT_ID]; |
| 344 | NSString *contactUri = info[CONTACT_URI]; |
| 345 | [_ringWindowController showContactRequestFor:accountId contactUri: contactUri]; |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 346 | } |
| 347 | } |
Kateryna Kostiuk | e350384 | 2018-12-12 16:39:45 -0500 | [diff] [blame] | 348 | [[NSUserNotificationCenter defaultUserNotificationCenter] removeAllDeliveredNotifications]; |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 349 | } |
| 350 | |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 351 | - (void) showWizard |
| 352 | { |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 353 | if(self.wizard == nil) { |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 354 | self.wizard = [[RingWizardWC alloc] initWithNibName:@"RingWizard" bundle: nil accountmodel: &lrc->getAccountModel()]; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 355 | } |
Alexandre Lision | 76d5969 | 2016-01-20 18:06:05 -0500 | [diff] [blame] | 356 | [self.wizard.window makeKeyAndOrderFront:self]; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 357 | } |
| 358 | |
| 359 | - (void) showMainWindow |
| 360 | { |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 361 | if(self.ringWindowController == nil) { |
Kateryna Kostiuk | f631742 | 2018-09-27 17:08:20 -0400 | [diff] [blame] | 362 | self.ringWindowController = [[RingWindowController alloc] initWithWindowNibName:@"RingWindow" bundle: nil accountModel:&lrc->getAccountModel() dataTransferModel:&lrc->getDataTransferModel() behaviourController:&lrc->getBehaviorController() avModel: &lrc->getAVModel()]; |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 363 | } |
Kateryna Kostiuk | 465cfbe | 2018-06-05 16:13:05 -0400 | [diff] [blame] | 364 | [[NSApplication sharedApplication] removeWindowsItem:self.wizard.window]; |
Kateryna Kostiuk | b0db48e | 2019-01-10 16:03:55 -0500 | [diff] [blame] | 365 | self.wizard = nil; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 366 | [self.ringWindowController.window makeKeyAndOrderFront:self]; |
| 367 | } |
| 368 | |
Alexandre Lision | 6200531 | 2016-01-28 15:55:16 -0500 | [diff] [blame] | 369 | - (void) showDialpad |
| 370 | { |
| 371 | if (self.dialpad == nil) { |
| 372 | self.dialpad = [[DialpadWC alloc] initWithWindowNibName:@"Dialpad"]; |
| 373 | } |
| 374 | [self.dialpad.window makeKeyAndOrderFront:self]; |
| 375 | } |
| 376 | |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 377 | -(QVector<QString>) getActiveCalls { |
Kateryna Kostiuk | 5d90c3b | 2019-07-18 12:03:52 -0400 | [diff] [blame] | 378 | return lrc->activeCalls(); |
| 379 | } |
Alexandre Lision | 6200531 | 2016-01-28 15:55:16 -0500 | [diff] [blame] | 380 | |
Kateryna Kostiuk | 1705a5a | 2020-08-26 09:48:00 -0400 | [diff] [blame] | 381 | -(QVector<QString>)getConferenceSubcalls:(QString)confId { |
| 382 | return lrc->getConferenceSubcalls(confId); |
| 383 | } |
| 384 | |
Kateryna Kostiuk | 0c0801e | 2019-07-18 20:10:51 -0400 | [diff] [blame] | 385 | -(void)setRingtonePath { |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 386 | QStringList accounts = lrc->getAccountModel().getAccountList(); |
Kateryna Kostiuk | 0c0801e | 2019-07-18 20:10:51 -0400 | [diff] [blame] | 387 | NSFileManager *fileManager = [NSFileManager defaultManager]; |
| 388 | for (auto account: accounts) { |
| 389 | lrc::api::account::ConfProperties_t accountProperties = lrc->getAccountModel().getAccountConfig(account); |
Kateryna Kostiuk | c867eb9 | 2020-03-08 13:15:17 -0400 | [diff] [blame] | 390 | NSString *ringtonePath = accountProperties.Ringtone.ringtonePath.toNSString(); |
Kateryna Kostiuk | 0c0801e | 2019-07-18 20:10:51 -0400 | [diff] [blame] | 391 | if (![fileManager fileExistsAtPath: ringtonePath]) { |
| 392 | accountProperties.Ringtone.ringtonePath = [defaultRingtonePath() UTF8String]; |
| 393 | lrc->getAccountModel().setAccountConfig(account, accountProperties); |
| 394 | } |
| 395 | } |
| 396 | } |
| 397 | |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 398 | - (BOOL) checkForRingAccount |
| 399 | { |
Kateryna Kostiuk | 23222fe | 2018-11-16 14:28:02 -0500 | [diff] [blame] | 400 | return !lrc->getAccountModel().getAccountList().empty(); |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 401 | } |
| 402 | |
Alexandre Lision | 18e1fcd | 2015-08-04 14:38:42 -0400 | [diff] [blame] | 403 | -(void)applicationWillFinishLaunching:(NSNotification *)aNotification |
| 404 | { |
| 405 | NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager]; |
| 406 | [appleEventManager setEventHandler:self |
| 407 | andSelector:@selector(handleGetURLEvent:withReplyEvent:) |
| 408 | forEventClass:kInternetEventClass andEventID:kAEGetURL]; |
| 409 | } |
| 410 | |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 411 | - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag |
| 412 | { |
| 413 | if([self checkForRingAccount]) { |
| 414 | [self showMainWindow]; |
| 415 | } else { |
| 416 | [self showWizard]; |
| 417 | } |
| 418 | return YES; |
| 419 | } |
| 420 | |
Edric Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 421 | - (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent |
| 422 | { |
Kateryna Kostiuk | 0c0344a | 2018-07-27 10:35:01 -0400 | [diff] [blame] | 423 | [[NSApplication sharedApplication] terminate:self]; |
Edric Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 424 | } |
| 425 | |
| 426 | -(void)applicationWillTerminate:(NSNotification *)notification |
| 427 | { |
Alexandre Lision | 76d5969 | 2016-01-20 18:06:05 -0500 | [diff] [blame] | 428 | [self cleanExit]; |
| 429 | } |
| 430 | |
| 431 | - (void) cleanExit |
| 432 | { |
Adrien Béraud | 022b57a | 2018-09-15 15:49:07 -0400 | [diff] [blame] | 433 | if (self.activity != nil) { |
| 434 | [[NSProcessInfo processInfo] endActivity:self.activity]; |
| 435 | self.activity = nil; |
Andreas Traczyk | e4d6e78 | 2018-03-22 17:51:30 -0400 | [diff] [blame] | 436 | } |
Alexandre Lision | 76d5969 | 2016-01-20 18:06:05 -0500 | [diff] [blame] | 437 | [self.wizard close]; |
| 438 | [self.ringWindowController close]; |
Kateryna Kostiuk | 26526f2 | 2019-08-13 13:20:21 -0400 | [diff] [blame] | 439 | lrc.reset(); |
Edric Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 440 | } |
| 441 | |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 442 | #if ENABLE_SPARKLE |
| 443 | |
Alexandre Lision | 76d5969 | 2016-01-20 18:06:05 -0500 | [diff] [blame] | 444 | #pragma mark - Sparkle delegate |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 445 | |
| 446 | - (void)updater:(SUUpdater *)updater willInstallUpdate:(SUAppcastItem *)update |
| 447 | { |
| 448 | [NSApp activateIgnoringOtherApps:YES]; |
| 449 | } |
| 450 | |
| 451 | - (BOOL)updaterMayCheckForUpdates:(SUUpdater *)bundle |
| 452 | { |
| 453 | return YES; |
| 454 | } |
| 455 | |
| 456 | - (BOOL)updaterShouldRelaunchApplication:(SUUpdater *)updater |
| 457 | { |
| 458 | return YES; |
| 459 | } |
| 460 | |
| 461 | - (void)updater:(SUUpdater *)updater didAbortWithError:(NSError *)error |
| 462 | { |
| 463 | NSLog(@"Error:%@", error.localizedDescription); |
| 464 | } |
| 465 | |
| 466 | #endif |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 467 | @end |