blob: 77d58e513cf104676ad6766878db631487d51759 [file] [log] [blame]
Alexandre Lision8521baa2015-03-13 11:08:00 -04001/*
Alexandre Lision9fe374b2016-01-06 10:17:31 -05002 * Copyright (C) 2015-2016 Savoir-faire Linux Inc.
Alexandre Lision8521baa2015-03-13 11:08:00 -04003 * 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 Lision8521baa2015-03-13 11:08:00 -040018 */
Alexandre Lision5855b6a2015-02-03 11:31:05 -050019#import "AppDelegate.h"
20
Alexandre Lisione4041492015-03-20 18:20:43 -040021#import <callmodel.h>
Edric Milaret81315412015-05-13 15:14:56 -040022#import <qapplication.h>
Alexandre Lision745e4d62015-03-22 20:03:10 -040023#import <accountmodel.h>
24#import <protocolmodel.h>
Alexandre Lision0f66bd32016-01-18 11:30:45 -050025#import <media/recordingmodel.h>
26#import <media/textrecording.h>
Alexandre Lision745e4d62015-03-22 20:03:10 -040027#import <QItemSelectionModel>
28#import <account.h>
29
Alexandre Lision3d4143a2015-06-10 14:27:49 -040030#if ENABLE_SPARKLE
31#import <Sparkle/Sparkle.h>
32#endif
33
Alexandre Lisionc65310c2015-04-23 16:44:23 -040034#import "Constants.h"
Alexandre Lision745e4d62015-03-22 20:03:10 -040035#import "RingWizardWC.h"
36
Alexandre Lision3d4143a2015-06-10 14:27:49 -040037#if ENABLE_SPARKLE
38@interface AppDelegate() <SUUpdaterDelegate>
39#else
Alexandre Lision745e4d62015-03-22 20:03:10 -040040@interface AppDelegate()
Alexandre Lision3d4143a2015-06-10 14:27:49 -040041#endif
Alexandre Lision745e4d62015-03-22 20:03:10 -040042
43@property RingWindowController* ringWindowController;
44@property RingWizardWC* wizard;
45
46@end
47
Alexandre Lision5855b6a2015-02-03 11:31:05 -050048@implementation AppDelegate
49
50- (void)applicationDidFinishLaunching:(NSNotification *)aNotification {
Alexandre Lision4a7b95e2015-02-20 10:06:43 -050051 [[NSUserDefaults standardUserDefaults] setBool:YES forKey:@"NSConstraintBasedLayoutVisualizeMutuallyExclusiveConstraints"];
52
Alexandre Lisione4041492015-03-20 18:20:43 -040053 [[NSUserNotificationCenter defaultUserNotificationCenter] setDelegate:self];
54
Edric Milaret81315412015-05-13 15:14:56 -040055 NSAppleEventManager* appleEventManager = [NSAppleEventManager sharedAppleEventManager];
56 [appleEventManager setEventHandler:self andSelector:@selector(handleQuitEvent:withReplyEvent:) forEventClass:kCoreEventClass andEventID:kAEQuitApplication];
57
Alexandre Lision745e4d62015-03-22 20:03:10 -040058 if([self checkForRingAccount]) {
59 [self showMainWindow];
60 } else {
61 [self showWizard];
62 }
Alexandre Lisione4041492015-03-20 18:20:43 -040063 [self connect];
64}
65
66- (void) connect
67{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -040068 QObject::connect(&CallModel::instance(),
Alexandre Lisione4041492015-03-20 18:20:43 -040069 &CallModel::incomingCall,
70 [=](Call* call) {
Alexandre Lisionc65310c2015-04-23 16:44:23 -040071 BOOL shouldComeToForeground = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::WindowBehaviour];
72 BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::Notifications];
Alexandre Lision61d78a42015-10-06 11:22:47 -040073 if (shouldComeToForeground) {
Alexandre Lisione4041492015-03-20 18:20:43 -040074 [NSApp activateIgnoringOtherApps:YES];
Alexandre Lision61d78a42015-10-06 11:22:47 -040075 if ([self.ringWindowController.window isMiniaturized]) {
76 [self.ringWindowController.window deminiaturize:self];
77 }
78 }
Alexandre Lisione4041492015-03-20 18:20:43 -040079
80 if(shouldNotify) {
81 [self showIncomingNotification:call];
82 }
83 });
Alexandre Lision0f66bd32016-01-18 11:30:45 -050084
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 Lisione4041492015-03-20 18:20:43 -0400111}
112
113- (void) showIncomingNotification:(Call*) call{
114 NSUserNotification *notification = [[NSUserNotification alloc] init];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500115 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 Lisione4041492015-03-20 18:20:43 -0400120
121 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500122}
123
Alexandre Lision745e4d62015-03-22 20:03:10 -0400124/**
125 * click in MainMenu "Setup Ring"
126 */
127- (IBAction)showWizard:(id)sender {
128 [self showWizard];
129}
130
131- (void) showWizard
132{
Alexandre Lision745e4d62015-03-22 20:03:10 -0400133 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 Lisionb3f7ed62015-08-17 11:53:13 -0400141 if(self.ringWindowController == nil) {
Alexandre Lision745e4d62015-03-22 20:03:10 -0400142 self.ringWindowController = [[RingWindowController alloc] initWithWindowNibName:@"RingWindow"];
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400143 }
144
145 self.wizard = nil;
Alexandre Lision745e4d62015-03-22 20:03:10 -0400146
147 [self.ringWindowController.window makeKeyAndOrderFront:self];
148}
149
150- (BOOL) checkForRingAccount
151{
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400152 BOOL foundRingAcc = NO;
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400153 for (int i = 0 ; i < AccountModel::instance().rowCount() ; ++i) {
154 QModelIndex idx = AccountModel::instance().index(i);
155 Account* acc = AccountModel::instance().getAccountByModelIndex(idx);
Alexandre Lision745e4d62015-03-22 20:03:10 -0400156 if(acc->protocol() == Account::Protocol::RING) {
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400157 if (acc->displayName().isEmpty())
158 acc->setDisplayName(acc->alias());
159 foundRingAcc = YES;
Alexandre Lision745e4d62015-03-22 20:03:10 -0400160 }
161 }
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400162 return foundRingAcc;
Alexandre Lision745e4d62015-03-22 20:03:10 -0400163}
164
Alexandre Lision18e1fcd2015-08-04 14:38:42 -0400165-(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 Lisiond3aa3ad2015-10-23 14:28:41 -0400193 Call* c = CallModel::instance().dialingCall();
Alexandre Lision18e1fcd2015-08-04 14:38:42 -0400194 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 Lision745e4d62015-03-22 20:03:10 -0400206- (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 Milaret81315412015-05-13 15:14:56 -0400216- (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
217{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400218 delete CallModel::instance().QObject::parent();
Edric Milaret81315412015-05-13 15:14:56 -0400219 [[NSApplication sharedApplication] terminate:self];
220}
221
222-(void)applicationWillTerminate:(NSNotification *)notification
223{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400224 delete CallModel::instance().QObject::parent();
Edric Milaret81315412015-05-13 15:14:56 -0400225 [[NSApplication sharedApplication] terminate:self];
226}
227
Alexandre Lision3d4143a2015-06-10 14:27:49 -0400228#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 Lision5855b6a2015-02-03 11:31:05 -0500254@end