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 | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 19 | #import "AppDelegate.h" |
| 20 | |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 21 | #import <callmodel.h> |
Edric Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 22 | #import <qapplication.h> |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 23 | #import <accountmodel.h> |
| 24 | #import <protocolmodel.h> |
| 25 | #import <QItemSelectionModel> |
| 26 | #import <account.h> |
| 27 | |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 28 | #if ENABLE_SPARKLE |
| 29 | #import <Sparkle/Sparkle.h> |
| 30 | #endif |
| 31 | |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 32 | #import "Constants.h" |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 33 | #import "RingWizardWC.h" |
| 34 | |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 35 | #if ENABLE_SPARKLE |
| 36 | @interface AppDelegate() <SUUpdaterDelegate> |
| 37 | #else |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 38 | @interface AppDelegate() |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 39 | #endif |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 40 | |
| 41 | @property RingWindowController* ringWindowController; |
| 42 | @property RingWizardWC* wizard; |
| 43 | |
| 44 | @end |
| 45 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 46 | @implementation AppDelegate |
| 47 | |
| 48 | - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 49 | [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints"]; |
| 50 | |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 51 | [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self]; |
| 52 | |
Edric Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 53 | NSAppleEventManager* appleEventManager = [NSAppleEventManager sharedAppleEventManager]; |
| 54 | [appleEventManager setEventHandler:self andSelector:@selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication]; |
| 55 | |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 56 | if([self checkForRingAccount]) { |
| 57 | [self showMainWindow]; |
| 58 | } else { |
| 59 | [self showWizard]; |
| 60 | } |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 61 | [self connect]; |
| 62 | } |
| 63 | |
| 64 | - (void) connect |
| 65 | { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 66 | QObject::connect(&CallModel::instance(), |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 67 | &CallModel::incomingCall, |
| 68 | [=](Call* call) { |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 69 | BOOL shouldComeToForeground = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::WindowBehaviour]; |
| 70 | BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::Notifications]; |
Alexandre Lision | 61d78a4 | 2015-10-06 11:22:47 -0400 | [diff] [blame] | 71 | if (shouldComeToForeground) { |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 72 | [NSApp activateIgnoringOtherApps:YES]; |
Alexandre Lision | 61d78a4 | 2015-10-06 11:22:47 -0400 | [diff] [blame] | 73 | if ([self.ringWindowController.window isMiniaturized]) { |
| 74 | [self.ringWindowController.window deminiaturize:self]; |
| 75 | } |
| 76 | } |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 77 | |
| 78 | if(shouldNotify) { |
| 79 | [self showIncomingNotification:call]; |
| 80 | } |
| 81 | }); |
| 82 | } |
| 83 | |
| 84 | - (void) showIncomingNotification:(Call*) call{ |
| 85 | NSUserNotification *notification = [[NSUserNotification alloc] init]; |
| 86 | notification.title = @"Incoming call", call->peerName(); |
| 87 | //notification.informativeText = @"A notification"; |
| 88 | notification.soundName = NSUserNotificationDefaultSoundName; |
| 89 | |
| 90 | [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification]; |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 91 | } |
| 92 | |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 93 | /** |
| 94 | * click in MainMenu "Setup Ring" |
| 95 | */ |
| 96 | - (IBAction)showWizard:(id)sender { |
| 97 | [self showWizard]; |
| 98 | } |
| 99 | |
| 100 | - (void) showWizard |
| 101 | { |
| 102 | NSLog(@"Showing wizard"); |
| 103 | if(self.wizard == nil) { |
| 104 | self.wizard = [[RingWizardWC alloc] initWithWindowNibName:@"RingWizard"]; |
| 105 | } |
| 106 | [self.wizard.window orderFront:self]; |
| 107 | } |
| 108 | |
| 109 | - (void) showMainWindow |
| 110 | { |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 111 | if(self.ringWindowController == nil) { |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 112 | self.ringWindowController = [[RingWindowController alloc] initWithWindowNibName:@"RingWindow"]; |
Alexandre Lision | b3f7ed6 | 2015-08-17 11:53:13 -0400 | [diff] [blame] | 113 | } |
| 114 | |
| 115 | self.wizard = nil; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 116 | |
| 117 | [self.ringWindowController.window makeKeyAndOrderFront:self]; |
| 118 | } |
| 119 | |
| 120 | - (BOOL) checkForRingAccount |
| 121 | { |
Alexandre Lision | a1f07bf | 2015-07-28 10:30:55 -0400 | [diff] [blame] | 122 | BOOL foundRingAcc = NO; |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 123 | for (int i = 0 ; i < AccountModel::instance().rowCount() ; ++i) { |
| 124 | QModelIndex idx = AccountModel::instance().index(i); |
| 125 | Account* acc = AccountModel::instance().getAccountByModelIndex(idx); |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 126 | if(acc->protocol() == Account::Protocol::RING) { |
Alexandre Lision | a1f07bf | 2015-07-28 10:30:55 -0400 | [diff] [blame] | 127 | if (acc->displayName().isEmpty()) |
| 128 | acc->setDisplayName(acc->alias()); |
| 129 | foundRingAcc = YES; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 130 | } |
| 131 | } |
Alexandre Lision | a1f07bf | 2015-07-28 10:30:55 -0400 | [diff] [blame] | 132 | return foundRingAcc; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 133 | } |
| 134 | |
Alexandre Lision | 18e1fcd | 2015-08-04 14:38:42 -0400 | [diff] [blame] | 135 | -(void)applicationWillFinishLaunching:(NSNotification *)aNotification |
| 136 | { |
| 137 | NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager]; |
| 138 | [appleEventManager setEventHandler:self |
| 139 | andSelector:@selector(handleGetURLEvent:withReplyEvent:) |
| 140 | forEventClass:kInternetEventClass andEventID:kAEGetURL]; |
| 141 | } |
| 142 | |
| 143 | /** |
| 144 | * Recognized patterns: |
| 145 | * - ring:<hash> |
| 146 | * - ring://<hash> |
| 147 | */ |
| 148 | - (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent |
| 149 | { |
| 150 | NSString* query = [[event paramDescriptorForKeyword:keyDirectObject] stringValue]; |
| 151 | NSURL* url = [[NSURL alloc] initWithString:query]; |
| 152 | NSString* ringID = [url host]; |
| 153 | if (!ringID) { |
| 154 | //not a valid NSURL, try to parse query directly |
| 155 | ringID = [query substringFromIndex:@"ring:".length]; |
| 156 | } |
| 157 | |
| 158 | // check for a valid ring hash |
| 159 | NSCharacterSet *hexSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefABCDEF"]; |
| 160 | BOOL valid = [[ringID stringByTrimmingCharactersInSet:hexSet] isEqualToString:@""]; |
| 161 | |
| 162 | if(valid && ringID.length == 40) { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 163 | Call* c = CallModel::instance().dialingCall(); |
Alexandre Lision | 18e1fcd | 2015-08-04 14:38:42 -0400 | [diff] [blame] | 164 | c->setDialNumber(QString::fromNSString([NSString stringWithFormat:@"ring:%@",ringID])); |
| 165 | c << Call::Action::ACCEPT; |
| 166 | } else { |
| 167 | NSAlert *alert = [[NSAlert alloc] init]; |
| 168 | [alert addButtonWithTitle:@"OK"]; |
| 169 | [alert setMessageText:@"Error"]; |
| 170 | [alert setInformativeText:@"ringID cannot be read from this URL."]; |
| 171 | [alert setAlertStyle:NSWarningAlertStyle]; |
| 172 | [alert runModal]; |
| 173 | } |
| 174 | } |
| 175 | |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 176 | - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag |
| 177 | { |
| 178 | if([self checkForRingAccount]) { |
| 179 | [self showMainWindow]; |
| 180 | } else { |
| 181 | [self showWizard]; |
| 182 | } |
| 183 | return YES; |
| 184 | } |
| 185 | |
Edric Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 186 | - (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent |
| 187 | { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 188 | delete CallModel::instance().QObject::parent(); |
Edric Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 189 | [[NSApplication sharedApplication] terminate:self]; |
| 190 | } |
| 191 | |
| 192 | -(void)applicationWillTerminate:(NSNotification *)notification |
| 193 | { |
Alexandre Lision | d3aa3ad | 2015-10-23 14:28:41 -0400 | [diff] [blame] | 194 | delete CallModel::instance().QObject::parent(); |
Edric Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 195 | [[NSApplication sharedApplication] terminate:self]; |
| 196 | } |
| 197 | |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 198 | #if ENABLE_SPARKLE |
| 199 | |
| 200 | #pragma mark - |
| 201 | #pragma mark Sparkle delegate |
| 202 | |
| 203 | - (void)updater:(SUUpdater *)updater willInstallUpdate:(SUAppcastItem *)update |
| 204 | { |
| 205 | [NSApp activateIgnoringOtherApps:YES]; |
| 206 | } |
| 207 | |
| 208 | - (BOOL)updaterMayCheckForUpdates:(SUUpdater *)bundle |
| 209 | { |
| 210 | return YES; |
| 211 | } |
| 212 | |
| 213 | - (BOOL)updaterShouldRelaunchApplication:(SUUpdater *)updater |
| 214 | { |
| 215 | return YES; |
| 216 | } |
| 217 | |
| 218 | - (void)updater:(SUUpdater *)updater didAbortWithError:(NSError *)error |
| 219 | { |
| 220 | NSLog(@"Error:%@", error.localizedDescription); |
| 221 | } |
| 222 | |
| 223 | #endif |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 224 | @end |