blob: 6861b9167fe31fcfaf2cdd7cbd6078a4d401ec26 [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
Kateryna Kostiuke3503842018-12-12 16:39:45 -050023//lrc
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040024#import <api/lrc.h>
Kateryna Kostiuk23222fe2018-11-16 14:28:02 -050025#import <api/newaccountmodel.h>
Kateryna Kostiuke3503842018-12-12 16:39:45 -050026#import <api/behaviorcontroller.h>
27#import <api/conversation.h>
28#import <api/newcallmodel.h>
Kateryna Kostiukabf4e272017-04-18 14:18:00 -040029
Alexandre Lision745e4d62015-03-22 20:03:10 -040030
Alexandre Lision3d4143a2015-06-10 14:27:49 -040031#if ENABLE_SPARKLE
32#import <Sparkle/Sparkle.h>
33#endif
34
Alexandre Lisionc65310c2015-04-23 16:44:23 -040035#import "Constants.h"
Alexandre Lision745e4d62015-03-22 20:03:10 -040036#import "RingWizardWC.h"
Alexandre Lision62005312016-01-28 15:55:16 -050037#import "DialpadWC.h"
Kateryna Kostiuke3503842018-12-12 16:39:45 -050038#import "utils.h"
Alexandre Lision745e4d62015-03-22 20:03:10 -040039
Alexandre Lision3d4143a2015-06-10 14:27:49 -040040#if ENABLE_SPARKLE
41@interface AppDelegate() <SUUpdaterDelegate>
42#else
Alexandre Lision745e4d62015-03-22 20:03:10 -040043@interface AppDelegate()
Alexandre Lision3d4143a2015-06-10 14:27:49 -040044#endif
Alexandre Lision745e4d62015-03-22 20:03:10 -040045
46@property RingWindowController* ringWindowController;
47@property RingWizardWC* wizard;
Alexandre Lision62005312016-01-28 15:55:16 -050048@property DialpadWC* dialpad;
Alexandre Lisionb9f3f942016-07-23 14:29:33 -040049@property (nonatomic, strong) dispatch_queue_t scNetworkQueue;
50@property (nonatomic, assign) SCNetworkReachabilityRef currentReachability;
Adrien Béraud022b57a2018-09-15 15:49:07 -040051@property (strong) id activity;
Alexandre Lision745e4d62015-03-22 20:03:10 -040052
53@end
54
Kateryna Kostiuke3503842018-12-12 16:39:45 -050055NSString * const MESSAGE_NOTIFICATION = @"message_notification_type";
56NSString * const CALL_NOTIFICATION = @"call_notification_type";
57NSString * const CONTACT_REQUEST_NOTIFICATION = @"contact_request_notification_type";
58
59NSString * const ACCOUNT_ID = @"account_id_notification_info";
60NSString * const CALL_ID = @"call_id_notification_info";
61NSString * const CONVERSATION_ID = @"conversation_id_notification_info";
62NSString * const CONTACT_URI = @"contact_uri_notification_info";
63NSString * const NOTIFICATION_TYPE = @"contact_type_notification_info";
64
65
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040066@implementation AppDelegate {
Alexandre Lision5855b6a2015-02-03 11:31:05 -050067
Kateryna Kostiukecaa3952018-07-13 16:00:34 -040068std::unique_ptr<lrc::api::Lrc> lrc;
69}
Andreas Traczyke4d6e782018-03-22 17:51:30 -040070
Alexandre Lision5855b6a2015-02-03 11:31:05 -050071- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
Kateryna Kostiuk0bc4c592018-06-05 13:35:19 -040072
73 // hide "Check for update" menu item when sparkle is disabled
74 NSMenu *mainMenu = [[NSApplication sharedApplication] mainMenu];
75 NSMenu *ringMenu = [[mainMenu itemAtIndex:0] submenu];
76 NSMenuItem *updateItem = [ringMenu itemAtIndex:1];
77#if ENABLE_SPARKLE
78 updateItem.hidden = false;
79#else
80 updateItem.hidden = true;
81#endif
82
Kateryna Kostiuk353ca3f2019-01-08 12:02:40 -050083#ifndef NDEBUG
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050084 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints"];
Kateryna Kostiuk353ca3f2019-01-08 12:02:40 -050085#else
86 [[NSUserDefaults standardUserDefaults] setBool:NO forKey:@"NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints"];
87#endif
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050088
Alexandre Lisione4041492015-03-20 18:20:43 -040089 [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
90
Edric Milaret81315412015-05-13 15:14:56 -040091 NSAppleEventManager* appleEventManager = [NSAppleEventManager sharedAppleEventManager];
92 [appleEventManager setEventHandler:self andSelector:@selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication];
Kateryna Kostiuk85a334e2018-12-03 15:54:19 -050093
94 dispatch_queue_t queue = NULL;
95 queue = dispatch_queue_create("scNetworkReachability", DISPATCH_QUEUE_SERIAL);
96 [self setScNetworkQueue:queue];
97 [self beginObservingReachabilityStatus];
98 NSActivityOptions options = NSActivitySuddenTerminationDisabled | NSActivityAutomaticTerminationDisabled | NSActivityBackground;
99 self.activity = [[NSProcessInfo processInfo] beginActivityWithOptions:options reason:@"Receiving calls and messages"];
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400100 lrc = std::make_unique<lrc::api::Lrc>();
Alexandre Lision745e4d62015-03-22 20:03:10 -0400101 if([self checkForRingAccount]) {
Kateryna Kostiuk0c0801e2019-07-18 20:10:51 -0400102 [self setRingtonePath];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400103 [self showMainWindow];
104 } else {
105 [self showWizard];
106 }
Alexandre Lisione4041492015-03-20 18:20:43 -0400107 [self connect];
Alexandre Lisionb9f3f942016-07-23 14:29:33 -0400108}
109
110- (void) beginObservingReachabilityStatus
111{
112 SCNetworkReachabilityRef reachabilityRef = NULL;
113
114 void (^callbackBlock)(SCNetworkReachabilityFlags) = ^(SCNetworkReachabilityFlags flags) {
Kateryna Kostiuk9217e292019-08-13 13:22:45 -0400115 dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
Kateryna Kostiuk44432012018-11-28 09:39:10 -0500116 lrc->connectivityChanged();
Kateryna Kostiuk9217e292019-08-13 13:22:45 -0400117 });
Alexandre Lisionb9f3f942016-07-23 14:29:33 -0400118 };
119
120 SCNetworkReachabilityContext context = {
121 .version = 0,
122 .info = (void *)CFBridgingRetain(callbackBlock),
123 .release = CFRelease
124 };
125
126 reachabilityRef = SCNetworkReachabilityCreateWithName(kCFAllocatorDefault, "test");
127 if (SCNetworkReachabilitySetCallback(reachabilityRef, ReachabilityCallback, &context)){
128 if (!SCNetworkReachabilitySetDispatchQueue(reachabilityRef, [self scNetworkQueue]) ){
129 // Remove our callback if we can't use the queue
130 SCNetworkReachabilitySetCallback(reachabilityRef, NULL, NULL);
131 }
132 [self setCurrentReachability:reachabilityRef];
133 }
134}
135
136- (void) endObsvervingReachabilityStatusForHost:(NSString *)__unused host
137{
138 // Un-set the dispatch queue
139 if (SCNetworkReachabilitySetDispatchQueue([self currentReachability], NULL) ){
140 SCNetworkReachabilitySetCallback([self currentReachability], NULL, NULL);
141 }
142}
143
144static void ReachabilityCallback(SCNetworkReachabilityRef __unused target, SCNetworkConnectionFlags flags, void* info)
145{
146 void (^callbackBlock)(SCNetworkReachabilityFlags) = (__bridge id)info;
147 callbackBlock(flags);
Alexandre Lisione4041492015-03-20 18:20:43 -0400148}
149
150- (void) connect
151{
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500152 QObject::connect(&lrc->getBehaviorController(),
153 &lrc::api::BehaviorController::newTrustRequest,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400154 [self] (const QString& accountId, const QString& contactUri) {
Kateryna Kostiuk146cb9e2020-08-11 14:29:26 -0400155 BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ContactRequestNotifications];
156 if(!shouldNotify) {
157 return;
158 }
159 NSUserNotification* notification = [[NSUserNotification alloc] init];
160 auto contactModel = lrc->getAccountModel()
161 .getAccountInfo(accountId).contactModel.get();
162 NSString* name = contactModel->getContact(contactUri)
163 .registeredName.isEmpty() ?
164 contactUri.toNSString():
165 contactModel->getContact(contactUri).registeredName.toNSString();
166 NSString* localizedMessage =
167 NSLocalizedString(@"Send you a contact request",
168 @"Notification message");
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500169
Kateryna Kostiuk146cb9e2020-08-11 14:29:26 -0400170 NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
171 userInfo[ACCOUNT_ID] = accountId.toNSString();
172 userInfo[CONTACT_URI] = contactUri.toNSString();
173 userInfo[NOTIFICATION_TYPE] = CONTACT_REQUEST_NOTIFICATION;
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500174
Kateryna Kostiuk146cb9e2020-08-11 14:29:26 -0400175 [notification setTitle: name];
176 notification.informativeText = localizedMessage;
177 [notification setSoundName:NSUserNotificationDefaultSoundName];
178 [notification setUserInfo: userInfo];
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500179
Kateryna Kostiuk146cb9e2020-08-11 14:29:26 -0400180 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500181
Kateryna Kostiuk146cb9e2020-08-11 14:29:26 -0400182 });
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500183
184 QObject::connect(&lrc->getBehaviorController(),
185 &lrc::api::BehaviorController::showIncomingCallView,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400186 [self] (const QString& accountId, lrc::api::conversation::Info conversationInfo) {
Kateryna Kostiuk146cb9e2020-08-11 14:29:26 -0400187 BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::CallNotifications];
188 if(!shouldNotify) {
189 return;
190 }
191 bool isIncoming = false;
192 auto callModel = lrc->getAccountModel()
193 .getAccountInfo(accountId).callModel.get();
194 if(callModel->hasCall(conversationInfo.callId)) {
195 isIncoming = !callModel->getCall(conversationInfo.callId).isOutgoing;
196 }
197 if(!isIncoming) {
198 return;
199 }
200 NSString* name = bestIDForConversation(conversationInfo, *lrc->getAccountModel().getAccountInfo(accountId).conversationModel.get());
201 NSUserNotification* notification = [[NSUserNotification alloc] init];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500202
Kateryna Kostiuk146cb9e2020-08-11 14:29:26 -0400203 NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
204 userInfo[ACCOUNT_ID] = accountId.toNSString();
205 userInfo[CALL_ID] = conversationInfo.callId.toNSString();
206 userInfo[CONVERSATION_ID] = conversationInfo.uid.toNSString();
207 userInfo[NOTIFICATION_TYPE] = CALL_NOTIFICATION;
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500208
Kateryna Kostiuk146cb9e2020-08-11 14:29:26 -0400209 NSString* localizedTitle = [NSString stringWithFormat:
210 NSLocalizedString(@"Incoming call from %@", @"Incoming call from {Name}"),
211 name];
212 // try to activate action button
213 @try {
214 [notification setValue:@YES forKey:@"_showsButtons"];
215 }
216 @catch (NSException *exception) {
217 NSLog(@"Action button not activable on notification");
218 }
219 [notification setUserInfo: userInfo];
220 [notification setOtherButtonTitle:NSLocalizedString(@"Refuse", @"Button Action")];
221 [notification setActionButtonTitle:NSLocalizedString(@"Accept", @"Button Action")];
222 [notification setTitle:localizedTitle];
223 [notification setSoundName:NSUserNotificationDefaultSoundName];
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500224
Kateryna Kostiuk146cb9e2020-08-11 14:29:26 -0400225 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
226 });
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500227
228 QObject::connect(&lrc->getBehaviorController(),
229 &lrc::api::BehaviorController::newUnreadInteraction,
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400230 [self] (const QString& accountId, const QString& conversation,
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500231 uint64_t interactionId, const lrc::api::interaction::Info& interaction) {
Kateryna Kostiuk146cb9e2020-08-11 14:29:26 -0400232 BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::MessagesNotifications];
233 if(!shouldNotify) {
234 return;
235 }
236 NSUserNotification* notification = [[NSUserNotification alloc] init];
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500237
Kateryna Kostiuk146cb9e2020-08-11 14:29:26 -0400238 NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
239 userInfo[ACCOUNT_ID] = accountId.toNSString();
240 userInfo[CONVERSATION_ID] = conversation.toNSString();
241 userInfo[NOTIFICATION_TYPE] = MESSAGE_NOTIFICATION;
242 NSString* name = interaction.authorUri.toNSString();
243 auto convIt = getConversationFromUid(conversation, *lrc->getAccountModel()
244 .getAccountInfo(accountId)
Kateryna Kostiuk3541ae22020-08-17 12:26:14 -0400245 .conversationModel.get());
Kateryna Kostiuk146cb9e2020-08-11 14:29:26 -0400246 auto convQueue = lrc->getAccountModel()
247 .getAccountInfo(accountId)
248 .conversationModel.get()->allFilteredConversations();
249 if (convIt != convQueue.end()) {
250 name = bestIDForConversation(*convIt, *lrc->getAccountModel().getAccountInfo(accountId).conversationModel.get());
251 }
252 NSString* localizedTitle = [NSString stringWithFormat:
253 NSLocalizedString(@"Incoming message from %@",@"Incoming message from {Name}"),
254 name];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500255
Kateryna Kostiuk146cb9e2020-08-11 14:29:26 -0400256 [notification setTitle:localizedTitle];
257 [notification setSoundName:NSUserNotificationDefaultSoundName];
258 [notification setSubtitle:interaction.body.toNSString()];
259 [notification setUserInfo:userInfo];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500260
Kateryna Kostiuk146cb9e2020-08-11 14:29:26 -0400261 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
262 });
Alexandre Lisione4041492015-03-20 18:20:43 -0400263}
264
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500265- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDismissAlert:(NSUserNotification *)alert {
266 // check if user click refuse on incoming call notifications
267 if(alert.activationType != NSUserNotificationActivationTypeNone) {
268 return;
Alexandre Lision34607032016-02-08 16:16:49 -0500269 }
Alexandre Lision34607032016-02-08 16:16:49 -0500270
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500271 auto info = alert.userInfo;
272 if(!info) {
273 return;
274 }
275 NSString* identifier = info[NOTIFICATION_TYPE];
276 NSString* callId = info[CALL_ID];
277 NSString* accountId = info[ACCOUNT_ID];
278 if(!identifier || !callId || !accountId) {
279 return;
280 }
281 if([identifier isEqualToString: CALL_NOTIFICATION]) {
282 auto accountInfo = &lrc->getAccountModel().getAccountInfo([accountId UTF8String]);
283 if (accountInfo == nil)
284 return;
285 auto* callModel = accountInfo->callModel.get();
286 callModel->hangUp([callId UTF8String]);
287 }
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500288}
289
Alexandre Lision34607032016-02-08 16:16:49 -0500290- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
291{
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500292 auto info = notification.userInfo;
293 if(!info) {
294 return;
295 }
296 NSString* identifier = info[NOTIFICATION_TYPE];
297 if([identifier isEqualToString: CALL_NOTIFICATION]) {
298 if(notification.activationType == NSUserNotificationActivationTypeActionButtonClicked
299 || notification.activationType == NSUserNotificationActivationTypeContentsClicked) {
300 NSString* callId = info[CALL_ID];
301 NSString* accountId = info[ACCOUNT_ID];
302 NSString *conversationId = info[CONVERSATION_ID];
303 auto accountInfo = &lrc->getAccountModel().getAccountInfo([accountId UTF8String]);
304 if (accountInfo == nil)
305 return;
306 auto* callModel = accountInfo->callModel.get();
307 callModel->accept([callId UTF8String]);
Alexandre Lision34607032016-02-08 16:16:49 -0500308 [self.ringWindowController.window deminiaturize:self];
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500309 [_ringWindowController showCall:callId forAccount:accountId forConversation:conversationId];
310 }
311 } else if(notification.activationType == NSUserNotificationActivationTypeContentsClicked) {
312 [self.ringWindowController.window deminiaturize:self];
313 if([identifier isEqualToString: MESSAGE_NOTIFICATION]) {
314 NSString* accountId = info[ACCOUNT_ID];
315 NSString *conversationId = info[CONVERSATION_ID];
316 [_ringWindowController showConversation:conversationId forAccount:accountId];
317 } else if([identifier isEqualToString: CONTACT_REQUEST_NOTIFICATION]) {
318 NSString* accountId = info[ACCOUNT_ID];
319 NSString *contactUri = info[CONTACT_URI];
320 [_ringWindowController showContactRequestFor:accountId contactUri: contactUri];
Alexandre Lision34607032016-02-08 16:16:49 -0500321 }
322 }
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500323 [[NSUserNotificationCenter defaultUserNotificationCenter] removeAllDeliveredNotifications];
Alexandre Lision34607032016-02-08 16:16:49 -0500324}
325
Alexandre Lision745e4d62015-03-22 20:03:10 -0400326- (void) showWizard
327{
Alexandre Lision745e4d62015-03-22 20:03:10 -0400328 if(self.wizard == nil) {
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400329 self.wizard = [[RingWizardWC alloc] initWithNibName:@"RingWizard" bundle: nil accountmodel: &lrc->getAccountModel()];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400330 }
Alexandre Lision76d59692016-01-20 18:06:05 -0500331 [self.wizard.window makeKeyAndOrderFront:self];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400332}
333
334- (void) showMainWindow
335{
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400336 if(self.ringWindowController == nil) {
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400337 self.ringWindowController = [[RingWindowController alloc] initWithWindowNibName:@"RingWindow" bundle: nil accountModel:&lrc->getAccountModel() dataTransferModel:&lrc->getDataTransferModel() behaviourController:&lrc->getBehaviorController() avModel: &lrc->getAVModel()];
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400338 }
Kateryna Kostiuk465cfbe2018-06-05 16:13:05 -0400339 [[NSApplication sharedApplication] removeWindowsItem:self.wizard.window];
Kateryna Kostiukb0db48e2019-01-10 16:03:55 -0500340 self.wizard = nil;
Alexandre Lision745e4d62015-03-22 20:03:10 -0400341 [self.ringWindowController.window makeKeyAndOrderFront:self];
342}
343
Alexandre Lision62005312016-01-28 15:55:16 -0500344- (void) showDialpad
345{
346 if (self.dialpad == nil) {
347 self.dialpad = [[DialpadWC alloc] initWithWindowNibName:@"Dialpad"];
348 }
349 [self.dialpad.window makeKeyAndOrderFront:self];
350}
351
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400352-(QVector<QString>) getActiveCalls {
Kateryna Kostiuk5d90c3b2019-07-18 12:03:52 -0400353 return lrc->activeCalls();
354}
Alexandre Lision62005312016-01-28 15:55:16 -0500355
Kateryna Kostiuk1705a5a2020-08-26 09:48:00 -0400356-(QVector<QString>)getConferenceSubcalls:(QString)confId {
357 return lrc->getConferenceSubcalls(confId);
358}
359
Kateryna Kostiuk0c0801e2019-07-18 20:10:51 -0400360-(void)setRingtonePath {
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400361 QStringList accounts = lrc->getAccountModel().getAccountList();
Kateryna Kostiuk0c0801e2019-07-18 20:10:51 -0400362 NSFileManager *fileManager = [NSFileManager defaultManager];
363 for (auto account: accounts) {
364 lrc::api::account::ConfProperties_t accountProperties = lrc->getAccountModel().getAccountConfig(account);
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400365 NSString *ringtonePath = accountProperties.Ringtone.ringtonePath.toNSString();
Kateryna Kostiuk0c0801e2019-07-18 20:10:51 -0400366 if (![fileManager fileExistsAtPath: ringtonePath]) {
367 accountProperties.Ringtone.ringtonePath = [defaultRingtonePath() UTF8String];
368 lrc->getAccountModel().setAccountConfig(account, accountProperties);
369 }
370 }
371}
372
Alexandre Lision745e4d62015-03-22 20:03:10 -0400373- (BOOL) checkForRingAccount
374{
Kateryna Kostiuk23222fe2018-11-16 14:28:02 -0500375 return !lrc->getAccountModel().getAccountList().empty();
Alexandre Lision745e4d62015-03-22 20:03:10 -0400376}
377
Alexandre Lision18e1fcd2015-08-04 14:38:42 -0400378-(void)applicationWillFinishLaunching:(NSNotification *)aNotification
379{
380 NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
381 [appleEventManager setEventHandler:self
382 andSelector:@selector(handleGetURLEvent:withReplyEvent:)
383 forEventClass:kInternetEventClass andEventID:kAEGetURL];
384}
385
Alexandre Lision745e4d62015-03-22 20:03:10 -0400386- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
387{
388 if([self checkForRingAccount]) {
389 [self showMainWindow];
390 } else {
391 [self showWizard];
392 }
393 return YES;
394}
395
Edric Milaret81315412015-05-13 15:14:56 -0400396- (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
397{
Kateryna Kostiuk0c0344a2018-07-27 10:35:01 -0400398 [[NSApplication sharedApplication] terminate:self];
Edric Milaret81315412015-05-13 15:14:56 -0400399}
400
401-(void)applicationWillTerminate:(NSNotification *)notification
402{
Alexandre Lision76d59692016-01-20 18:06:05 -0500403 [self cleanExit];
404}
405
406- (void) cleanExit
407{
Adrien Béraud022b57a2018-09-15 15:49:07 -0400408 if (self.activity != nil) {
409 [[NSProcessInfo processInfo] endActivity:self.activity];
410 self.activity = nil;
Andreas Traczyke4d6e782018-03-22 17:51:30 -0400411 }
Alexandre Lision76d59692016-01-20 18:06:05 -0500412 [self.wizard close];
413 [self.ringWindowController close];
Kateryna Kostiuk26526f22019-08-13 13:20:21 -0400414 lrc.reset();
Edric Milaret81315412015-05-13 15:14:56 -0400415}
416
Alexandre Lision3d4143a2015-06-10 14:27:49 -0400417#if ENABLE_SPARKLE
418
Alexandre Lision76d59692016-01-20 18:06:05 -0500419#pragma mark - Sparkle delegate
Alexandre Lision3d4143a2015-06-10 14:27:49 -0400420
421- (void)updater:(SUUpdater *)updater willInstallUpdate:(SUAppcastItem *)update
422{
423 [NSApp activateIgnoringOtherApps:YES];
424}
425
426- (BOOL)updaterMayCheckForUpdates:(SUUpdater *)bundle
427{
428 return YES;
429}
430
431- (BOOL)updaterShouldRelaunchApplication:(SUUpdater *)updater
432{
433 return YES;
434}
435
436- (void)updater:(SUUpdater *)updater didAbortWithError:(NSError *)error
437{
438 NSLog(@"Error:%@", error.localizedDescription);
439}
440
441#endif
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500442@end