blob: 111de1cef81f4bbc1c2a5d8463ed501ad01df317 [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
Alexandre Lision08e72142016-01-19 13:29:36 -050086 QObject::connect(&Media::RecordingModel::instance(),
87 &Media::RecordingModel::unreadMessagesCountChanged,
88 [=](int unreadCount) {
89 NSDockTile *tile = [[NSApplication sharedApplication] dockTile];
90 NSString* label = unreadCount ? [NSString stringWithFormat:@"%d", unreadCount]: @"";
91 [tile setBadgeLabel:label];
92 });
Alexandre Lision0f66bd32016-01-18 11:30:45 -050093
94 QObject::connect(&Media::RecordingModel::instance(),
95 &Media::RecordingModel::newTextMessage,
96 [=](Media::TextRecording* t, ContactMethod* cm) {
97
98 BOOL shouldNotify = [[NSUserDefaults standardUserDefaults] boolForKey:Preferences::Notifications];
99 auto qIdx = t->instantTextMessagingModel()->index(t->instantTextMessagingModel()->rowCount()-1, 0);
100
101 // Don't show a notification if we are sending the text OR window already has focus OR user disabled notifications
102 if(qvariant_cast<Media::Media::Direction>(qIdx.data((int)Media::TextRecording::Role::Direction)) == Media::Media::Direction::OUT
103 || self.ringWindowController.window.keyWindow || !shouldNotify)
104 return;
105
106 NSUserNotification* notification = [[NSUserNotification alloc] init];
107
108 NSString* localizedTitle = NSLocalizedString(([NSString stringWithFormat:@"Message from %@",
109 qIdx.data((int)Media::TextRecording::Role::AuthorDisplayname).toString().toNSString()]),
110 @"Text message notification title");
111
112 [notification setTitle:localizedTitle];
113 [notification setSoundName:NSUserNotificationDefaultSoundName];
114 [notification setSubtitle:qIdx.data().toString().toNSString()];
115
116 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
117 });
Alexandre Lisione4041492015-03-20 18:20:43 -0400118}
119
120- (void) showIncomingNotification:(Call*) call{
121 NSUserNotification *notification = [[NSUserNotification alloc] init];
Alexandre Lision0f66bd32016-01-18 11:30:45 -0500122 NSString* localizedTitle = NSLocalizedString(([NSString stringWithFormat:@"Incoming call from %@",
123 call->peerName().toNSString()]),
124 @"Call notification title");
125 [notification setTitle:localizedTitle];
126 [notification setSoundName:NSUserNotificationDefaultSoundName];
Alexandre Lisione4041492015-03-20 18:20:43 -0400127
128 [[NSUserNotificationCenter defaultUserNotificationCenter] deliverNotification:notification];
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500129}
130
Alexandre Lision745e4d62015-03-22 20:03:10 -0400131/**
132 * click in MainMenu "Setup Ring"
133 */
134- (IBAction)showWizard:(id)sender {
135 [self showWizard];
136}
137
138- (void) showWizard
139{
Alexandre Lision745e4d62015-03-22 20:03:10 -0400140 if(self.wizard == nil) {
141 self.wizard = [[RingWizardWC alloc] initWithWindowNibName:@"RingWizard"];
142 }
143 [self.wizard.window orderFront:self];
144}
145
146- (void) showMainWindow
147{
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400148 if(self.ringWindowController == nil) {
Alexandre Lision745e4d62015-03-22 20:03:10 -0400149 self.ringWindowController = [[RingWindowController alloc] initWithWindowNibName:@"RingWindow"];
Alexandre Lisionb3f7ed62015-08-17 11:53:13 -0400150 }
151
152 self.wizard = nil;
Alexandre Lision745e4d62015-03-22 20:03:10 -0400153
154 [self.ringWindowController.window makeKeyAndOrderFront:self];
155}
156
157- (BOOL) checkForRingAccount
158{
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400159 BOOL foundRingAcc = NO;
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400160 for (int i = 0 ; i < AccountModel::instance().rowCount() ; ++i) {
161 QModelIndex idx = AccountModel::instance().index(i);
162 Account* acc = AccountModel::instance().getAccountByModelIndex(idx);
Alexandre Lision745e4d62015-03-22 20:03:10 -0400163 if(acc->protocol() == Account::Protocol::RING) {
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400164 if (acc->displayName().isEmpty())
165 acc->setDisplayName(acc->alias());
166 foundRingAcc = YES;
Alexandre Lision745e4d62015-03-22 20:03:10 -0400167 }
168 }
Alexandre Lisiona1f07bf2015-07-28 10:30:55 -0400169 return foundRingAcc;
Alexandre Lision745e4d62015-03-22 20:03:10 -0400170}
171
Alexandre Lision18e1fcd2015-08-04 14:38:42 -0400172-(void)applicationWillFinishLaunching:(NSNotification *)aNotification
173{
174 NSAppleEventManager *appleEventManager = [NSAppleEventManager sharedAppleEventManager];
175 [appleEventManager setEventHandler:self
176 andSelector:@selector(handleGetURLEvent:withReplyEvent:)
177 forEventClass:kInternetEventClass andEventID:kAEGetURL];
178}
179
180/**
181 * Recognized patterns:
182 * - ring:<hash>
183 * - ring://<hash>
184 */
185- (void)handleGetURLEvent:(NSAppleEventDescriptor *)event withReplyEvent:(NSAppleEventDescriptor *)replyEvent
186{
187 NSString* query = [[event paramDescriptorForKeyword:keyDirectObject] stringValue];
188 NSURL* url = [[NSURL alloc] initWithString:query];
189 NSString* ringID = [url host];
190 if (!ringID) {
191 //not a valid NSURL, try to parse query directly
192 ringID = [query substringFromIndex:@"ring:".length];
193 }
194
195 // check for a valid ring hash
196 NSCharacterSet *hexSet = [NSCharacterSet characterSetWithCharactersInString:@"0123456789abcdefABCDEF"];
197 BOOL valid = [[ringID stringByTrimmingCharactersInSet:hexSet] isEqualToString:@""];
198
199 if(valid && ringID.length == 40) {
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400200 Call* c = CallModel::instance().dialingCall();
Alexandre Lision18e1fcd2015-08-04 14:38:42 -0400201 c->setDialNumber(QString::fromNSString([NSString stringWithFormat:@"ring:%@",ringID]));
202 c << Call::Action::ACCEPT;
203 } else {
204 NSAlert *alert = [[NSAlert alloc] init];
205 [alert addButtonWithTitle:@"OK"];
206 [alert setMessageText:@"Error"];
207 [alert setInformativeText:@"ringID cannot be read from this URL."];
208 [alert setAlertStyle:NSWarningAlertStyle];
209 [alert runModal];
210 }
211}
212
Alexandre Lision745e4d62015-03-22 20:03:10 -0400213- (BOOL)applicationShouldHandleReopen:(NSApplication *)theApplication hasVisibleWindows:(BOOL)flag
214{
215 if([self checkForRingAccount]) {
216 [self showMainWindow];
217 } else {
218 [self showWizard];
219 }
220 return YES;
221}
222
Edric Milaret81315412015-05-13 15:14:56 -0400223- (void)handleQuitEvent:(NSAppleEventDescriptor*)event withReplyEvent:(NSAppleEventDescriptor*)replyEvent
224{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400225 delete CallModel::instance().QObject::parent();
Edric Milaret81315412015-05-13 15:14:56 -0400226 [[NSApplication sharedApplication] terminate:self];
227}
228
229-(void)applicationWillTerminate:(NSNotification *)notification
230{
Alexandre Lisiond3aa3ad2015-10-23 14:28:41 -0400231 delete CallModel::instance().QObject::parent();
Edric Milaret81315412015-05-13 15:14:56 -0400232 [[NSApplication sharedApplication] terminate:self];
233}
234
Alexandre Lision3d4143a2015-06-10 14:27:49 -0400235#if ENABLE_SPARKLE
236
237#pragma mark -
238#pragma mark Sparkle delegate
239
240- (void)updater:(SUUpdater *)updater willInstallUpdate:(SUAppcastItem *)update
241{
242 [NSApp activateIgnoringOtherApps:YES];
243}
244
245- (BOOL)updaterMayCheckForUpdates:(SUUpdater *)bundle
246{
247 return YES;
248}
249
250- (BOOL)updaterShouldRelaunchApplication:(SUUpdater *)updater
251{
252 return YES;
253}
254
255- (void)updater:(SUUpdater *)updater didAbortWithError:(NSError *)error
256{
257 NSLog(@"Error:%@", error.localizedDescription);
258}
259
260#endif
Alexandre Lision5855b6a2015-02-03 11:31:05 -0500261@end