blob: 63254387147287dc160196b5bac5cd30d86ef64c [file] [log] [blame]
Alexandre Lision8521baa2015-03-13 11:08:00 -04001/*
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 Lision5855b6a2015-02-03 11:31:05 -050030#import "AppDelegate.h"
31
Alexandre Lisione4041492015-03-20 18:20:43 -040032#import <callmodel.h>
Edric Milaret81315412015-05-13 15:14:56 -040033#import <qapplication.h>
Alexandre Lision745e4d62015-03-22 20:03:10 -040034#import <accountmodel.h>
35#import <protocolmodel.h>
36#import <QItemSelectionModel>
37#import <account.h>
38
Alexandre Lision3d4143a2015-06-10 14:27:49 -040039#if ENABLE_SPARKLE
40#import <Sparkle/Sparkle.h>
41#endif
42
Alexandre Lisionc65310c2015-04-23 16:44:23 -040043#import "Constants.h"
Alexandre Lision745e4d62015-03-22 20:03:10 -040044#import "RingWizardWC.h"
45
Alexandre Lision3d4143a2015-06-10 14:27:49 -040046#if ENABLE_SPARKLE
47@interface AppDelegate() <SUUpdaterDelegate>
48#else
Alexandre Lision745e4d62015-03-22 20:03:10 -040049@interface AppDelegate()
Alexandre Lision3d4143a2015-06-10 14:27:49 -040050#endif
Alexandre Lision745e4d62015-03-22 20:03:10 -040051
52@property RingWindowController* ringWindowController;
53@property RingWizardWC* wizard;
54
55@end
56
Alexandre Lision5855b6a2015-02-03 11:31:05 -050057@implementation AppDelegate
58
59- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050060 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints"];
61
Alexandre Lisione4041492015-03-20 18:20:43 -040062 [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
63
Edric Milaret81315412015-05-13 15:14:56 -040064 NSAppleEventManager* appleEventManager = [NSAppleEventManager sharedAppleEventManager];
65 [appleEventManager setEventHandler:self andSelector:@selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication];
66
Alexandre Lision745e4d62015-03-22 20:03:10 -040067 if([self checkForRingAccount]) {
68 [self showMainWindow];
69 } else {
70 [self showWizard];
71 }
Alexandre Lisione4041492015-03-20 18:20:43 -040072 [self connect];
73}
74
75- (void) connect
76{
77 QObject::connect(CallModel::instance(),
78 &CallModel::incomingCall,
79 [=](Call* call) {
Alexandre Lisionc65310c2015-04-23 16:44:23 -040080 BOOL shouldComeToForeground = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::WindowBehaviour];
81 BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::Notifications];
Alexandre Lisione4041492015-03-20 18:20:43 -040082 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 Lision5855b6a2015-02-03 11:31:05 -050098}
99
Alexandre Lision745e4d62015-03-22 20:03:10 -0400100/**
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{
126 for (int i = 0 ; i < AccountModel::instance()->rowCount() ; ++i) {
127 QModelIndex idx = AccountModel::instance()->index(i);
128 Account* acc = AccountModel::instance()->getAccountByModelIndex(idx);
129 if(acc->protocol() == Account::Protocol::RING) {
130 return YES;
131 }
132 }
133 return FALSE;
134}
135
136- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
137{
138 if([self checkForRingAccount]) {
139 [self showMainWindow];
140 } else {
141 [self showWizard];
142 }
143 return YES;
144}
145
Edric Milaret81315412015-05-13 15:14:56 -0400146- (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
147{
148 delete CallModel::instance()->QObject::parent();
149 [[NSApplication sharedApplication] terminate:self];
150}
151
152-(void)applicationWillTerminate:(NSNotification *)notification
153{
154 delete CallModel::instance()->QObject::parent();
155 [[NSApplication sharedApplication] terminate:self];
156}
157
Alexandre Lision3d4143a2015-06-10 14:27:49 -0400158#if ENABLE_SPARKLE
159
160#pragma mark -
161#pragma mark Sparkle delegate
162
163- (void)updater:(SUUpdater *)updater willInstallUpdate:(SUAppcastItem *)update
164{
165 [NSApp activateIgnoringOtherApps:YES];
166}
167
168- (BOOL)updaterMayCheckForUpdates:(SUUpdater *)bundle
169{
170 return YES;
171}
172
173- (BOOL)updaterShouldRelaunchApplication:(SUUpdater *)updater
174{
175 return YES;
176}
177
178- (void)updater:(SUUpdater *)updater didAbortWithError:(NSError *)error
179{
180 NSLog(@"Error:%@", error.localizedDescription);
181}
182
183#endif
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500184@end