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> |
| 20 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 21 | #import "AppDelegate.h" |
| 22 | |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 23 | #import <callmodel.h> |
Edric Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 24 | #import <qapplication.h> |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 25 | #import <accountmodel.h> |
| 26 | #import <protocolmodel.h> |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 27 | #import <media/recordingmodel.h> |
| 28 | #import <media/textrecording.h> |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 29 | #import <QItemSelectionModel> |
Andreas Traczyk | f1d2190 | 2018-04-09 11:01:49 -0400 | [diff] [blame] | 30 | #import <QDebug> |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 31 | #import <account.h> |
Kateryna Kostiuk | abf4e27 | 2017-04-18 14:18:00 -0400 | [diff] [blame] | 32 | #import <AvailableAccountModel.h> |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 33 | #import <api/lrc.h> |
Kateryna Kostiuk | abf4e27 | 2017-04-18 14:18:00 -0400 | [diff] [blame] | 34 | |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 35 | |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 36 | #if ENABLE_SPARKLE |
| 37 | #import <Sparkle/Sparkle.h> |
| 38 | #endif |
| 39 | |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 40 | #import "Constants.h" |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 41 | #import "RingWizardWC.h" |
Alexandre Lision | 6200531 | 2016-01-28 15:55:16 -0500 | [diff] [blame] | 42 | #import "DialpadWC.h" |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 43 | |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 44 | #if ENABLE_SPARKLE |
| 45 | @interface AppDelegate() <SUUpdaterDelegate> |
| 46 | #else |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 47 | @interface AppDelegate() |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 48 | #endif |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 49 | |
| 50 | @property RingWindowController* ringWindowController; |
| 51 | @property RingWizardWC* wizard; |
Alexandre Lision | 6200531 | 2016-01-28 15:55:16 -0500 | [diff] [blame] | 52 | @property DialpadWC* dialpad; |
Alexandre Lision | b9f3f94 | 2016-07-23 14:29:33 -0400 | [diff] [blame] | 53 | @property (nonatomic, strong) dispatch_queue_t scNetworkQueue; |
| 54 | @property (nonatomic, assign) SCNetworkReachabilityRef currentReachability; |
Adrien Béraud | 022b57a | 2018-09-15 15:49:07 -0400 | [diff] [blame] | 55 | @property (strong) id activity; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 56 | |
| 57 | @end |
| 58 | |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 59 | @implementation AppDelegate { |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 60 | |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 61 | std::unique_ptr<lrc::api::Lrc> lrc; |
| 62 | } |
Andreas Traczyk | e4d6e78 | 2018-03-22 17:51:30 -0400 | [diff] [blame] | 63 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 64 | - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { |
Kateryna Kostiuk | 0bc4c59 | 2018-06-05 13:35:19 -0400 | [diff] [blame] | 65 | |
| 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 Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 76 | [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints"]; |
| 77 | |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 78 | [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self]; |
| 79 | |
Edric Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 80 | NSAppleEventManager* appleEventManager = [NSAppleEventManager sharedAppleEventManager]; |
| 81 | [appleEventManager setEventHandler:self andSelector:@selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication]; |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 82 | lrc = std::make_unique<lrc::api::Lrc>(); |
Edric Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 83 | |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 84 | if([self checkForRingAccount]) { |
| 85 | [self showMainWindow]; |
| 86 | } else { |
| 87 | [self showWizard]; |
| 88 | } |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 89 | [self connect]; |
Alexandre Lision | b9f3f94 | 2016-07-23 14:29:33 -0400 | [diff] [blame] | 90 | |
| 91 | dispatch_queue_t queue = NULL; |
| 92 | queue = dispatch_queue_create("scNetworkReachability", DISPATCH_QUEUE_SERIAL); |
| 93 | [self setScNetworkQueue:queue]; |
| 94 | [self beginObservingReachabilityStatus]; |
Adrien Béraud | 022b57a | 2018-09-15 15:49:07 -0400 | [diff] [blame] | 95 | NSActivityOptions options = NSActivitySuddenTerminationDisabled | NSActivityAutomaticTerminationDisabled | NSActivityBackground; |
| 96 | self.activity = [[NSProcessInfo processInfo] beginActivityWithOptions:options reason:@"Receiving calls and messages"]; |
Alexandre Lision | b9f3f94 | 2016-07-23 14:29:33 -0400 | [diff] [blame] | 97 | } |
| 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 | |
| 134 | static void ReachabilityCallback(SCNetworkReachabilityRef __unused target, SCNetworkConnectionFlags flags, void* info) |
| 135 | { |
| 136 | void (^callbackBlock)(SCNetworkReachabilityFlags) = (__bridge id)info; |
| 137 | callbackBlock(flags); |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | - (void) connect |
| 141 | { |
Andreas Traczyk | f1d2190 | 2018-04-09 11:01:49 -0400 | [diff] [blame] | 142 | |
| 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 Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 152 | QObject::connect(&CallModel::instance(), |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 153 | &CallModel::incomingCall, |
| 154 | [=](Call* call) { |
Kateryna Kostiuk | abf4e27 | 2017-04-18 14:18:00 -0400 | [diff] [blame] | 155 | // 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 Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 163 | BOOL shouldComeToForeground = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::WindowBehaviour]; |
| 164 | BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::Notifications]; |
Alexandre Lision | 61d78a4 | 2015-10-06 11:22:47 -0400 | [diff] [blame] | 165 | if (shouldComeToForeground) { |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 166 | [NSApp activateIgnoringOtherApps:YES]; |
Alexandre Lision | 61d78a4 | 2015-10-06 11:22:47 -0400 | [diff] [blame] | 167 | if ([self.ringWindowController.window isMiniaturized]) { |
| 168 | [self.ringWindowController.window deminiaturize:self]; |
| 169 | } |
| 170 | } |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 171 | |
| 172 | if(shouldNotify) { |
| 173 | [self showIncomingNotification:call]; |
| 174 | } |
| 175 | }); |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 176 | |
Kateryna Kostiuk | f9a72c0 | 2018-08-01 14:28:49 -0400 | [diff] [blame] | 177 | QObject::connect(&media::RecordingModel::instance(), |
| 178 | &media::RecordingModel::newTextMessage, |
| 179 | [=](media::TextRecording* t, ContactMethod* cm) { |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 180 | |
| 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 Kostiuk | f9a72c0 | 2018-08-01 14:28:49 -0400 | [diff] [blame] | 185 | if(qvariant_cast<media::Media::Direction>(qIdx.data((int)media::TextRecording::Role::Direction)) == media::Media::Direction::OUT |
Alexandre Lision | 4baba4c | 2016-02-11 13:00:57 -0500 | [diff] [blame] | 186 | || self.ringWindowController.window.isMainWindow || !shouldNotify) |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 187 | return; |
| 188 | |
| 189 | NSUserNotification* notification = [[NSUserNotification alloc] init]; |
| 190 | |
Kateryna Kostiuk | f9a72c0 | 2018-08-01 14:28:49 -0400 | [diff] [blame] | 191 | NSString* localizedTitle = [NSString stringWithFormat:NSLocalizedString(@"Message from %@", @"Message from {Name}"), qIdx.data((int)media::TextRecording::Role::AuthorDisplayname).toString().toNSString()]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 192 | |
| 193 | [notification setTitle:localizedTitle]; |
| 194 | [notification setSoundName:NSUserNotificationDefaultSoundName]; |
| 195 | [notification setSubtitle:qIdx.data().toString().toNSString()]; |
| 196 | |
| 197 | [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification]; |
| 198 | }); |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | - (void) showIncomingNotification:(Call*) call{ |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 202 | NSUserNotification* notification = [[NSUserNotification alloc] init]; |
Alexandre Lision | c818011 | 2016-01-27 11:27:50 -0500 | [diff] [blame] | 203 | NSString* localizedTitle = [NSString stringWithFormat: |
| 204 | NSLocalizedString(@"Incoming call from %@", @"Incoming call from {Name}"), call->peerName().toNSString()]; |
Alexandre Lision | 0f66bd3 | 2016-01-18 11:30:45 -0500 | [diff] [blame] | 205 | [notification setTitle:localizedTitle]; |
| 206 | [notification setSoundName:NSUserNotificationDefaultSoundName]; |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 207 | |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 208 | // 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 Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 218 | [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification]; |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 219 | } |
| 220 | |
Alexandre Lision | 3460703 | 2016-02-08 16:16:49 -0500 | [diff] [blame] | 221 | - (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 Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 233 | - (void) showWizard |
| 234 | { |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 235 | if(self.wizard == nil) { |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 236 | self.wizard = [[RingWizardWC alloc] initWithNibName:@"RingWizard" bundle: nil accountmodel: &lrc->getAccountModel()]; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 237 | } |
Alexandre Lision | 76d5969 | 2016-01-20 18:06:05 -0500 | [diff] [blame] | 238 | [self.wizard.window makeKeyAndOrderFront:self]; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | - (void) showMainWindow |
| 242 | { |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 243 | if(self.ringWindowController == nil) { |
Kateryna Kostiuk | ecaa395 | 2018-07-13 16:00:34 -0400 | [diff] [blame] | 244 | self.ringWindowController = [[RingWindowController alloc] initWithWindowNibName:@"RingWindow" bundle: nil accountModel:&lrc->getAccountModel() dataTransferModel:&lrc->getDataTransferModel() behaviourController:&lrc->getBehaviorController()]; |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 245 | } |
Kateryna Kostiuk | 465cfbe | 2018-06-05 16:13:05 -0400 | [diff] [blame] | 246 | [[NSApplication sharedApplication] removeWindowsItem:self.wizard.window]; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 247 | [self.ringWindowController.window makeKeyAndOrderFront:self]; |
| 248 | } |
| 249 | |
Alexandre Lision | 6200531 | 2016-01-28 15:55:16 -0500 | [diff] [blame] | 250 | - (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 Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 259 | - (BOOL) checkForRingAccount |
| 260 | { |
Alexandre Lision | a1f07bf | 2015-07-28 10:30:55 -0400 | [diff] [blame] | 261 | BOOL foundRingAcc = NO; |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 262 | for (int i = 0 ; i < AccountModel::instance().rowCount() ; ++i) { |
| 263 | QModelIndex idx = AccountModel::instance().index(i); |
| 264 | Account* acc = AccountModel::instance().getAccountByModelIndex(idx); |
Alexandre Lision | 76d5969 | 2016-01-20 18:06:05 -0500 | [diff] [blame] | 265 | if(acc->protocol() == Account::Protocol::RING && !acc->isNew()) { |
Alexandre Lision | a1f07bf | 2015-07-28 10:30:55 -0400 | [diff] [blame] | 266 | if (acc->displayName().isEmpty()) |
| 267 | acc->setDisplayName(acc->alias()); |
| 268 | foundRingAcc = YES; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 269 | } |
| 270 | } |
Alexandre Lision | a1f07bf | 2015-07-28 10:30:55 -0400 | [diff] [blame] | 271 | return foundRingAcc; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 272 | } |
| 273 | |
Alexandre Lision | 18e1fcd | 2015-08-04 14:38:42 -0400 | [diff] [blame] | 274 | -(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 Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 302 | Call* c = CallModel::instance().dialingCall(); |
Alexandre Lision | 18e1fcd | 2015-08-04 14:38:42 -0400 | [diff] [blame] | 303 | 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 Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 315 | - (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 Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 325 | - (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent |
| 326 | { |
Kateryna Kostiuk | 0c0344a | 2018-07-27 10:35:01 -0400 | [diff] [blame] | 327 | [[NSApplication sharedApplication] terminate:self]; |
Edric Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 328 | } |
| 329 | |
| 330 | -(void)applicationWillTerminate:(NSNotification *)notification |
| 331 | { |
Alexandre Lision | 76d5969 | 2016-01-20 18:06:05 -0500 | [diff] [blame] | 332 | [self cleanExit]; |
| 333 | } |
| 334 | |
| 335 | - (void) cleanExit |
| 336 | { |
Adrien Béraud | 022b57a | 2018-09-15 15:49:07 -0400 | [diff] [blame] | 337 | if (self.activity != nil) { |
| 338 | [[NSProcessInfo processInfo] endActivity:self.activity]; |
| 339 | self.activity = nil; |
Andreas Traczyk | e4d6e78 | 2018-03-22 17:51:30 -0400 | [diff] [blame] | 340 | } |
Alexandre Lision | 76d5969 | 2016-01-20 18:06:05 -0500 | [diff] [blame] | 341 | [self.wizard close]; |
| 342 | [self.ringWindowController close]; |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 343 | delete CallModel::instance().QObject::parent(); |
Edric Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 344 | [[NSApplication sharedApplication] terminate:self]; |
| 345 | } |
| 346 | |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 347 | #if ENABLE_SPARKLE |
| 348 | |
Alexandre Lision | 76d5969 | 2016-01-20 18:06:05 -0500 | [diff] [blame] | 349 | #pragma mark - Sparkle delegate |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 350 | |
| 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 Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 372 | @end |