Alexandre Lision | 8521baa | 2015-03-13 11:08:00 -0400 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2004-2015 Savoir-Faire Linux Inc. |
| 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. |
| 18 | * |
| 19 | * Additional permission under GNU GPL version 3 section 7: |
| 20 | * |
| 21 | * If you modify this program, or any covered work, by linking or |
| 22 | * combining it with the OpenSSL project's OpenSSL library (or a |
| 23 | * modified version of that library), containing parts covered by the |
| 24 | * terms of the OpenSSL or SSLeay licenses, Savoir-Faire Linux Inc. |
| 25 | * grants you additional permission to convey the resulting work. |
| 26 | * Corresponding Source for a non-source form of such a combination |
| 27 | * shall include the source code for the parts of OpenSSL used as well |
| 28 | * as that of the covered work. |
| 29 | */ |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 30 | #import "AppDelegate.h" |
| 31 | |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 32 | #import <callmodel.h> |
Edric Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 33 | #import <qapplication.h> |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 34 | #import <accountmodel.h> |
| 35 | #import <protocolmodel.h> |
| 36 | #import <QItemSelectionModel> |
| 37 | #import <account.h> |
| 38 | |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 39 | #if ENABLE_SPARKLE |
| 40 | #import <Sparkle/Sparkle.h> |
| 41 | #endif |
| 42 | |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 43 | #import "Constants.h" |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 44 | #import "RingWizardWC.h" |
| 45 | |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 46 | #if ENABLE_SPARKLE |
| 47 | @interface AppDelegate() <SUUpdaterDelegate> |
| 48 | #else |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 49 | @interface AppDelegate() |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 50 | #endif |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 51 | |
| 52 | @property RingWindowController* ringWindowController; |
| 53 | @property RingWizardWC* wizard; |
| 54 | |
| 55 | @end |
| 56 | |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 57 | @implementation AppDelegate |
| 58 | |
| 59 | - (void)applicationDidFinishLaunching:(NSNotification *)aNotification { |
Alexandre Lision | 4a7b95e | 2015-02-20 10:06:43 -0500 | [diff] [blame] | 60 | [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints"]; |
| 61 | |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 62 | [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self]; |
| 63 | |
Edric Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 64 | NSAppleEventManager* appleEventManager = [NSAppleEventManager sharedAppleEventManager]; |
| 65 | [appleEventManager setEventHandler:self andSelector:@selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication]; |
| 66 | |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 67 | if([self checkForRingAccount]) { |
| 68 | [self showMainWindow]; |
| 69 | } else { |
| 70 | [self showWizard]; |
| 71 | } |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 72 | [self connect]; |
| 73 | } |
| 74 | |
| 75 | - (void) connect |
| 76 | { |
| 77 | QObject::connect(CallModel::instance(), |
| 78 | &CallModel::incomingCall, |
| 79 | [=](Call* call) { |
Alexandre Lision | c65310c | 2015-04-23 16:44:23 -0400 | [diff] [blame] | 80 | BOOL shouldComeToForeground = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::WindowBehaviour]; |
| 81 | BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::Notifications]; |
Alexandre Lision | e404149 | 2015-03-20 18:20:43 -0400 | [diff] [blame] | 82 | if(shouldComeToForeground) |
| 83 | [NSApp activateIgnoringOtherApps:YES]; |
| 84 | |
| 85 | if(shouldNotify) { |
| 86 | [self showIncomingNotification:call]; |
| 87 | } |
| 88 | }); |
| 89 | } |
| 90 | |
| 91 | - (void) showIncomingNotification:(Call*) call{ |
| 92 | NSUserNotification *notification = [[NSUserNotification alloc] init]; |
| 93 | notification.title = @"Incoming call", call->peerName(); |
| 94 | //notification.informativeText = @"A notification"; |
| 95 | notification.soundName = NSUserNotificationDefaultSoundName; |
| 96 | |
| 97 | [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification]; |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 98 | } |
| 99 | |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 100 | /** |
| 101 | * click in MainMenu "Setup Ring" |
| 102 | */ |
| 103 | - (IBAction)showWizard:(id)sender { |
| 104 | [self showWizard]; |
| 105 | } |
| 106 | |
| 107 | - (void) showWizard |
| 108 | { |
| 109 | NSLog(@"Showing wizard"); |
| 110 | if(self.wizard == nil) { |
| 111 | self.wizard = [[RingWizardWC alloc] initWithWindowNibName:@"RingWizard"]; |
| 112 | } |
| 113 | [self.wizard.window orderFront:self]; |
| 114 | } |
| 115 | |
| 116 | - (void) showMainWindow |
| 117 | { |
| 118 | if(self.ringWindowController == nil) |
| 119 | self.ringWindowController = [[RingWindowController alloc] initWithWindowNibName:@"RingWindow"]; |
| 120 | |
| 121 | [self.ringWindowController.window makeKeyAndOrderFront:self]; |
| 122 | } |
| 123 | |
| 124 | - (BOOL) checkForRingAccount |
| 125 | { |
Alexandre Lision | a1f07bf | 2015-07-28 10:30:55 -0400 | [diff] [blame] | 126 | BOOL foundRingAcc = NO; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 127 | for (int i = 0 ; i < AccountModel::instance()->rowCount() ; ++i) { |
| 128 | QModelIndex idx = AccountModel::instance()->index(i); |
| 129 | Account* acc = AccountModel::instance()->getAccountByModelIndex(idx); |
| 130 | if(acc->protocol() == Account::Protocol::RING) { |
Alexandre Lision | a1f07bf | 2015-07-28 10:30:55 -0400 | [diff] [blame] | 131 | if (acc->displayName().isEmpty()) |
| 132 | acc->setDisplayName(acc->alias()); |
| 133 | foundRingAcc = YES; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 134 | } |
| 135 | } |
Alexandre Lision | a1f07bf | 2015-07-28 10:30:55 -0400 | [diff] [blame] | 136 | return foundRingAcc; |
Alexandre Lision | 745e4d6 | 2015-03-22 20:03:10 -0400 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | - (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag |
| 140 | { |
| 141 | if([self checkForRingAccount]) { |
| 142 | [self showMainWindow]; |
| 143 | } else { |
| 144 | [self showWizard]; |
| 145 | } |
| 146 | return YES; |
| 147 | } |
| 148 | |
Edric Milaret | 8131541 | 2015-05-13 15:14:56 -0400 | [diff] [blame] | 149 | - (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent |
| 150 | { |
| 151 | delete CallModel::instance()->QObject::parent(); |
| 152 | [[NSApplication sharedApplication] terminate:self]; |
| 153 | } |
| 154 | |
| 155 | -(void)applicationWillTerminate:(NSNotification *)notification |
| 156 | { |
| 157 | delete CallModel::instance()->QObject::parent(); |
| 158 | [[NSApplication sharedApplication] terminate:self]; |
| 159 | } |
| 160 | |
Alexandre Lision | 3d4143a | 2015-06-10 14:27:49 -0400 | [diff] [blame] | 161 | #if ENABLE_SPARKLE |
| 162 | |
| 163 | #pragma mark - |
| 164 | #pragma mark Sparkle delegate |
| 165 | |
| 166 | - (void)updater:(SUUpdater *)updater willInstallUpdate:(SUAppcastItem *)update |
| 167 | { |
| 168 | [NSApp activateIgnoringOtherApps:YES]; |
| 169 | } |
| 170 | |
| 171 | - (BOOL)updaterMayCheckForUpdates:(SUUpdater *)bundle |
| 172 | { |
| 173 | return YES; |
| 174 | } |
| 175 | |
| 176 | - (BOOL)updaterShouldRelaunchApplication:(SUUpdater *)updater |
| 177 | { |
| 178 | return YES; |
| 179 | } |
| 180 | |
| 181 | - (void)updater:(SUUpdater *)updater didAbortWithError:(NSError *)error |
| 182 | { |
| 183 | NSLog(@"Error:%@", error.localizedDescription); |
| 184 | } |
| 185 | |
| 186 | #endif |
Alexandre Lision | 5855b6a | 2015-02-03 11:31:05 -0500 | [diff] [blame] | 187 | @end |