blob: 71bbf578a3cb9271b57cb887da3a1dacc8878069 [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 Kostiuke3503842018-12-12 16:39:45 -0500155 BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::ContactRequestNotifications];
156 if(!shouldNotify) {
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500157 return;
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500158 }
159 NSUserNotification* notification = [[NSUserNotification alloc] init];
160 auto contactModel = lrc->getAccountModel()
161 .getAccountInfo(accountId).contactModel.get();
162 NSString* name = contactModel->getContact(contactUri)
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400163 .registeredName.isEmpty() ?
164 contactUri.toNSString():
165 contactModel->getContact(contactUri).registeredName.toNSString();
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500166 NSString* localizedMessage =
167 NSLocalizedString(@"Send you a contact request",
168 @"Notification message");
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500169
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500170 NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400171 userInfo[ACCOUNT_ID] = accountId.toNSString();
172 userInfo[CONTACT_URI] = contactUri.toNSString();
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500173 userInfo[NOTIFICATION_TYPE] = CONTACT_REQUEST_NOTIFICATION;
174
175 [notification setTitle: name];
176 notification.informativeText = localizedMessage;
177 [notification setSoundName:NSUserNotificationDefaultSoundName];
178 [notification setUserInfo: userInfo];
179
180 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
181
182 });
183
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 Kostiuke3503842018-12-12 16:39:45 -0500187 BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::CallNotifications];
188 if(!shouldNotify) {
189 return;
190 }
Kateryna Kostiuk383bcfd2019-01-04 16:59:38 -0500191 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 }
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500200 NSString* name = bestIDForConversation(conversationInfo, *lrc->getAccountModel().getAccountInfo(accountId).conversationModel.get());
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500201 NSUserNotification* notification = [[NSUserNotification alloc] init];
202
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500203 NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400204 userInfo[ACCOUNT_ID] = accountId.toNSString();
205 userInfo[CALL_ID] = conversationInfo.callId.toNSString();
206 userInfo[CONVERSATION_ID] = conversationInfo.uid.toNSString();
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500207 userInfo[NOTIFICATION_TYPE] = CALL_NOTIFICATION;
208
209 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];
224
225 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
226 });
227
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) {
232 BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::MessagesNotifications];
233 if(!shouldNotify) {
234 return;
235 }
236 NSUserNotification* notification = [[NSUserNotification alloc] init];
237
238 NSMutableDictionary *userInfo = [[NSMutableDictionary alloc] init];
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400239 userInfo[ACCOUNT_ID] = accountId.toNSString();
240 userInfo[CONVERSATION_ID] = conversation.toNSString();
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500241 userInfo[NOTIFICATION_TYPE] = MESSAGE_NOTIFICATION;
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400242 NSString* name = interaction.authorUri.toNSString();
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500243 auto convIt = getConversationFromUid(conversation, *lrc->getAccountModel().getAccountInfo(accountId).conversationModel.get());
244 auto convQueue = lrc->getAccountModel().getAccountInfo(accountId).conversationModel.get()->allFilteredConversations();
245 if (convIt != convQueue.end()) {
246 name = bestIDForConversation(*convIt, *lrc->getAccountModel().getAccountInfo(accountId).conversationModel.get());
247 }
248 NSString* localizedTitle = [NSString stringWithFormat:
249 NSLocalizedString(@"Incoming message from %@",@"Incoming message from {Name}"),
250 name];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500251
252 [notification setTitle:localizedTitle];
253 [notification setSoundName:NSUserNotificationDefaultSoundName];
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400254 [notification setSubtitle:interaction.body.toNSString()];
255 [notification setUserInfo:userInfo];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500256
257 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
258 });
Alexandre Lisione4041492015-03-20 18:20:43 -0400259}
260
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500261- (void)userNotificationCenter:(NSUserNotificationCenter *)center didDismissAlert:(NSUserNotification *)alert {
262 // check if user click refuse on incoming call notifications
263 if(alert.activationType != NSUserNotificationActivationTypeNone) {
264 return;
Alexandre Lision34607032016-02-08 16:16:49 -0500265 }
Alexandre Lision34607032016-02-08 16:16:49 -0500266
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500267 auto info = alert.userInfo;
268 if(!info) {
269 return;
270 }
271 NSString* identifier = info[NOTIFICATION_TYPE];
272 NSString* callId = info[CALL_ID];
273 NSString* accountId = info[ACCOUNT_ID];
274 if(!identifier || !callId || !accountId) {
275 return;
276 }
277 if([identifier isEqualToString: CALL_NOTIFICATION]) {
278 auto accountInfo = &lrc->getAccountModel().getAccountInfo([accountId UTF8String]);
279 if (accountInfo == nil)
280 return;
281 auto* callModel = accountInfo->callModel.get();
282 callModel->hangUp([callId UTF8String]);
283 }
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500284}
285
Alexandre Lision34607032016-02-08 16:16:49 -0500286- (void) userNotificationCenter:(NSUserNotificationCenter *)center didActivateNotification:(NSUserNotification *)notification
287{
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500288 auto info = notification.userInfo;
289 if(!info) {
290 return;
291 }
292 NSString* identifier = info[NOTIFICATION_TYPE];
293 if([identifier isEqualToString: CALL_NOTIFICATION]) {
294 if(notification.activationType == NSUserNotificationActivationTypeActionButtonClicked
295 || notification.activationType == NSUserNotificationActivationTypeContentsClicked) {
296 NSString* callId = info[CALL_ID];
297 NSString* accountId = info[ACCOUNT_ID];
298 NSString *conversationId = info[CONVERSATION_ID];
299 auto accountInfo = &lrc->getAccountModel().getAccountInfo([accountId UTF8String]);
300 if (accountInfo == nil)
301 return;
302 auto* callModel = accountInfo->callModel.get();
303 callModel->accept([callId UTF8String]);
Alexandre Lision34607032016-02-08 16:16:49 -0500304 [self.ringWindowController.window deminiaturize:self];
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500305 [_ringWindowController showCall:callId forAccount:accountId forConversation:conversationId];
306 }
307 } else if(notification.activationType == NSUserNotificationActivationTypeContentsClicked) {
308 [self.ringWindowController.window deminiaturize:self];
309 if([identifier isEqualToString: MESSAGE_NOTIFICATION]) {
310 NSString* accountId = info[ACCOUNT_ID];
311 NSString *conversationId = info[CONVERSATION_ID];
312 [_ringWindowController showConversation:conversationId forAccount:accountId];
313 } else if([identifier isEqualToString: CONTACT_REQUEST_NOTIFICATION]) {
314 NSString* accountId = info[ACCOUNT_ID];
315 NSString *contactUri = info[CONTACT_URI];
316 [_ringWindowController showContactRequestFor:accountId contactUri: contactUri];
Alexandre Lision34607032016-02-08 16:16:49 -0500317 }
318 }
Kateryna Kostiuke3503842018-12-12 16:39:45 -0500319 [[NSUserNotificationCenter defaultUserNotificationCenter] removeAllDeliveredNotifications];
Alexandre Lision34607032016-02-08 16:16:49 -0500320}
321
Alexandre Lision745e4d62015-03-22 20:03:10 -0400322- (void) showWizard
323{
Alexandre Lision745e4d62015-03-22 20:03:10 -0400324 if(self.wizard == nil) {
Kateryna Kostiukecaa3952018-07-13 16:00:34 -0400325 self.wizard = [[RingWizardWC alloc] initWithNibName:@"RingWizard" bundle: nil accountmodel: &lrc->getAccountModel()];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400326 }
Alexandre Lision76d59692016-01-20 18:06:05 -0500327 [self.wizard.window makeKeyAndOrderFront:self];
Alexandre Lision745e4d62015-03-22 20:03:10 -0400328}
329
330- (void) showMainWindow
331{
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400332 if(self.ringWindowController == nil) {
Kateryna Kostiukf6317422018-09-27 17:08:20 -0400333 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 -0400334 }
Kateryna Kostiuk465cfbe2018-06-05 16:13:05 -0400335 [[NSApplication sharedApplication] removeWindowsItem:self.wizard.window];
Kateryna Kostiukb0db48e2019-01-10 16:03:55 -0500336 self.wizard = nil;
Alexandre Lision745e4d62015-03-22 20:03:10 -0400337 [self.ringWindowController.window makeKeyAndOrderFront:self];
338}
339
Alexandre Lision62005312016-01-28 15:55:16 -0500340- (void) showDialpad
341{
342 if (self.dialpad == nil) {
343 self.dialpad = [[DialpadWC alloc] initWithWindowNibName:@"Dialpad"];
344 }
345 [self.dialpad.window makeKeyAndOrderFront:self];
346}
347
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400348-(QVector<QString>) getActiveCalls {
Kateryna Kostiuk5d90c3b2019-07-18 12:03:52 -0400349 return lrc->activeCalls();
350}
Alexandre Lision62005312016-01-28 15:55:16 -0500351
Kateryna Kostiuk0c0801e2019-07-18 20:10:51 -0400352-(void)setRingtonePath {
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400353 QStringList accounts = lrc->getAccountModel().getAccountList();
Kateryna Kostiuk0c0801e2019-07-18 20:10:51 -0400354 NSFileManager *fileManager = [NSFileManager defaultManager];
355 for (auto account: accounts) {
356 lrc::api::account::ConfProperties_t accountProperties = lrc->getAccountModel().getAccountConfig(account);
Kateryna Kostiukc867eb92020-03-08 13:15:17 -0400357 NSString *ringtonePath = accountProperties.Ringtone.ringtonePath.toNSString();
Kateryna Kostiuk0c0801e2019-07-18 20:10:51 -0400358 if (![fileManager fileExistsAtPath: ringtonePath]) {
359 accountProperties.Ringtone.ringtonePath = [defaultRingtonePath() UTF8String];
360 lrc->getAccountModel().setAccountConfig(account, accountProperties);
361 }
362 }
363}
364
Alexandre Lision745e4d62015-03-22 20:03:10 -0400365- (BOOL) checkForRingAccount
366{
Kateryna Kostiuk23222fe2018-11-16 14:28:02 -0500367 return !lrc->getAccountModel().getAccountList().empty();
Alexandre Lision745e4d62015-03-22 20:03:10 -0400368}
369
Alexandre Lision18e1fcd2015-08-04 14:38:42 -0400370-(void)applicationWillFinishLaunching:(NSNotification *)aNotification
371{
372 NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
373 [appleEventManager setEventHandler:self
374 andSelector:@selector(handleGetURLEvent:withReplyEvent:)
375 forEventClass:kInternetEventClass andEventID:kAEGetURL];
376}
377
Alexandre Lision745e4d62015-03-22 20:03:10 -0400378- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
379{
380 if([self checkForRingAccount]) {
381 [self showMainWindow];
382 } else {
383 [self showWizard];
384 }
385 return YES;
386}
387
Edric Milaret81315412015-05-13 15:14:56 -0400388- (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
389{
Kateryna Kostiuk0c0344a2018-07-27 10:35:01 -0400390 [[NSApplication sharedApplication] terminate:self];
Edric Milaret81315412015-05-13 15:14:56 -0400391}
392
393-(void)applicationWillTerminate:(NSNotification *)notification
394{
Alexandre Lision76d59692016-01-20 18:06:05 -0500395 [self cleanExit];
396}
397
398- (void) cleanExit
399{
Adrien Béraud022b57a2018-09-15 15:49:07 -0400400 if (self.activity != nil) {
401 [[NSProcessInfo processInfo] endActivity:self.activity];
402 self.activity = nil;
Andreas Traczyke4d6e782018-03-22 17:51:30 -0400403 }
Alexandre Lision76d59692016-01-20 18:06:05 -0500404 [self.wizard close];
405 [self.ringWindowController close];
Kateryna Kostiuk26526f22019-08-13 13:20:21 -0400406 lrc.reset();
Edric Milaret81315412015-05-13 15:14:56 -0400407}
408
Alexandre Lision3d4143a2015-06-10 14:27:49 -0400409#if ENABLE_SPARKLE
410
Alexandre Lision76d59692016-01-20 18:06:05 -0500411#pragma mark - Sparkle delegate
Alexandre Lision3d4143a2015-06-10 14:27:49 -0400412
413- (void)updater:(SUUpdater *)updater willInstallUpdate:(SUAppcastItem *)update
414{
415 [NSApp activateIgnoringOtherApps:YES];
416}
417
418- (BOOL)updaterMayCheckForUpdates:(SUUpdater *)bundle
419{
420 return YES;
421}
422
423- (BOOL)updaterShouldRelaunchApplication:(SUUpdater *)updater
424{
425 return YES;
426}
427
428- (void)updater:(SUUpdater *)updater didAbortWithError:(NSError *)error
429{
430 NSLog(@"Error:%@", error.localizedDescription);
431}
432
433#endif
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500434@end