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