2014-01-30 14:23:18 +00:00
|
|
|
/* Implementation for NSUserNotification for GNUstep
|
|
|
|
Copyright (C) 2014 Free Software Foundation, Inc.
|
|
|
|
|
|
|
|
Written by: Marcus Mueller <znek@mulle-kybernetik.com>
|
|
|
|
Date: 2014
|
|
|
|
|
|
|
|
This file is part of the GNUstep Base Library.
|
|
|
|
|
|
|
|
This library is free software; you can redistribute it and/or
|
|
|
|
modify it under the terms of the GNU Lesser General Public
|
|
|
|
License as published by the Free Software Foundation; either
|
|
|
|
version 2 of the License, or (at your option) any later version.
|
|
|
|
|
|
|
|
This library is distributed in the hope that it will be useful,
|
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
2019-12-09 23:36:00 +00:00
|
|
|
Lesser General Public License for more details.
|
2014-01-30 14:23:18 +00:00
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free
|
2024-11-07 13:37:59 +00:00
|
|
|
Software Foundation, Inc., 31 Milk Street #960789 Boston, MA 02196 USA.
|
2014-01-30 14:23:18 +00:00
|
|
|
*/
|
|
|
|
|
2014-01-30 15:30:34 +00:00
|
|
|
#import <GNUstepBase/GSVersionMacros.h>
|
|
|
|
|
2014-01-30 14:23:18 +00:00
|
|
|
#if __has_feature(objc_default_synthesize_properties)
|
|
|
|
|
|
|
|
#define EXPOSE_NSUserNotification_IVARS 1
|
|
|
|
#define EXPOSE_NSUserNotificationCenter_IVARS 1
|
|
|
|
|
|
|
|
#import "GNUstepBase/NSObject+GNUstepBase.h"
|
|
|
|
#import "Foundation/NSUserNotification.h"
|
|
|
|
#import "Foundation/NSArray.h"
|
|
|
|
#import "Foundation/NSBundle.h"
|
2014-01-31 12:08:13 +00:00
|
|
|
#import "Foundation/NSCalendar.h"
|
2014-01-30 14:23:18 +00:00
|
|
|
#import "Foundation/NSDate.h"
|
|
|
|
#import "Foundation/NSString.h"
|
2014-01-31 12:08:13 +00:00
|
|
|
#import "Foundation/NSTimeZone.h"
|
2014-01-30 14:23:18 +00:00
|
|
|
|
|
|
|
@interface NSUserNotification ()
|
|
|
|
@property (readwrite) NSDate *actualDeliveryDate;
|
|
|
|
@property (readwrite, getter=isPresented) BOOL presented;
|
|
|
|
@property (readwrite, getter=isRemote) BOOL remote;
|
|
|
|
@property (readwrite) NSUserNotificationActivationType activationType;
|
|
|
|
@property (readwrite) NSAttributedString *response;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSUserNotification
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
if (nil != (self = [super init]))
|
|
|
|
{
|
2017-06-25 14:17:12 +00:00
|
|
|
[self setHasActionButton: YES];
|
2014-01-30 14:58:20 +00:00
|
|
|
}
|
|
|
|
return self;
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
RELEASE(_uniqueId);
|
|
|
|
[super dealloc];
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (id) copyWithZone: (NSZone *)zone
|
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
return NSCopyObject(self, 0, zone);
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
2017-06-25 14:17:12 +00:00
|
|
|
- (NSString *) description
|
2014-01-30 14:23:18 +00:00
|
|
|
{
|
2014-01-31 12:08:13 +00:00
|
|
|
NSMutableString *d = [NSMutableString stringWithCapacity:80];
|
2017-06-25 14:17:12 +00:00
|
|
|
|
|
|
|
[d appendFormat: @"<%s:%p< {", object_getClassName(self), self];
|
|
|
|
[d appendFormat: @" title: \"%@\"", [self title]];
|
|
|
|
[d appendFormat: @" informativeText: \"%@\"", [self informativeText]];
|
|
|
|
[d appendFormat: @" actionButtonTitle: \"%@\"", [self actionButtonTitle]];
|
|
|
|
if ([self actualDeliveryDate])
|
|
|
|
{
|
|
|
|
[d appendFormat: @" actualDeliveryDate: %@", [self actualDeliveryDate]];
|
|
|
|
[d appendFormat: @" presented: %s", [self isPresented] ? "YES" : "NO"];
|
|
|
|
}
|
|
|
|
[d appendFormat: @" next delivery date: %@", [self deliveryDate]];
|
|
|
|
[d appendString: @" }"];
|
2014-01-31 12:08:13 +00:00
|
|
|
return d;
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSUserNotificationCenter ()
|
|
|
|
@property (readwrite) NSArray *deliveredNotifications;
|
|
|
|
@end
|
|
|
|
|
|
|
|
@interface NSUserNotificationCenter (Private)
|
|
|
|
+ (Class) defaultUserNotificationCenterClass;
|
|
|
|
+ (void) setDefaultUserNotificationCenter: (NSUserNotificationCenter *)unc;
|
|
|
|
- (NSUserNotification *) deliveredNotificationWithUniqueId: (id)uniqueId;
|
2014-01-31 12:08:13 +00:00
|
|
|
- (NSDate *) nextDeliveryDateForNotification: (NSUserNotification *)un;
|
2014-01-30 14:23:18 +00:00
|
|
|
@end
|
|
|
|
|
|
|
|
@implementation NSUserNotificationCenter
|
|
|
|
|
|
|
|
static NSUserNotificationCenter *defaultUserNotificationCenter = nil;
|
|
|
|
|
|
|
|
+ (Class) defaultUserNotificationCenterClass
|
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
NSBundle *bundle = [NSBundle bundleForClass: [self class]];
|
|
|
|
NSString *bundlePath = [bundle pathForResource: @"NSUserNotification"
|
|
|
|
ofType: @"bundle"
|
|
|
|
inDirectory: nil];
|
|
|
|
if (bundlePath)
|
|
|
|
{
|
|
|
|
bundle = [NSBundle bundleWithPath: bundlePath];
|
|
|
|
if (bundle)
|
|
|
|
{
|
|
|
|
return [bundle principalClass];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return self;
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) atExit
|
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
DESTROY(defaultUserNotificationCenter);
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) initialize
|
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
if ([NSUserNotificationCenter class] == self)
|
2014-01-30 14:23:18 +00:00
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
Class uncClass = [self defaultUserNotificationCenterClass];
|
|
|
|
defaultUserNotificationCenter = [[uncClass alloc] init];
|
|
|
|
[self registerAtExit];
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
+ (void) setDefaultUserNotificationCenter: (NSUserNotificationCenter *)unc
|
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
ASSIGN(defaultUserNotificationCenter, unc);
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
+ (NSUserNotificationCenter *) defaultUserNotificationCenter
|
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
return defaultUserNotificationCenter;
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
- (id) init
|
|
|
|
{
|
2014-01-31 12:08:13 +00:00
|
|
|
self = [super init];
|
|
|
|
if (self)
|
2014-01-30 14:58:20 +00:00
|
|
|
{
|
|
|
|
_scheduledNotifications = [[NSMutableArray alloc] init];
|
|
|
|
_deliveredNotifications = [[NSMutableArray alloc] init];
|
|
|
|
}
|
|
|
|
return self;
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) dealloc
|
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
[NSObject cancelPreviousPerformRequestsWithTarget: self];
|
|
|
|
RELEASE(_scheduledNotifications);
|
|
|
|
[self removeAllDeliveredNotifications];
|
|
|
|
RELEASE(_deliveredNotifications);
|
|
|
|
[super dealloc];
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) scheduleNotification: (NSUserNotification *)un
|
|
|
|
{
|
2024-05-12 14:07:49 +00:00
|
|
|
NSTimeInterval delay;
|
|
|
|
|
2017-06-25 14:17:12 +00:00
|
|
|
if (![un deliveryDate])
|
2014-01-30 14:58:20 +00:00
|
|
|
{
|
|
|
|
[self deliverNotification: un];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[_scheduledNotifications addObject: un];
|
2024-05-12 14:07:49 +00:00
|
|
|
delay = [[un deliveryDate] timeIntervalSinceNow];
|
2014-01-30 14:58:20 +00:00
|
|
|
[self performSelector: @selector(deliverNotification:)
|
|
|
|
withObject: un
|
|
|
|
afterDelay: delay];
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeScheduledNotification: (NSUserNotification *)un
|
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
[NSObject cancelPreviousPerformRequestsWithTarget: self
|
|
|
|
selector: @selector(deliverNotification:)
|
|
|
|
object: un];
|
|
|
|
[_scheduledNotifications removeObject: un];
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) _deliverNotification: (NSUserNotification *)un
|
|
|
|
{
|
2017-06-25 14:17:12 +00:00
|
|
|
[un setPresented: YES];
|
2014-01-31 12:08:13 +00:00
|
|
|
NSLog(@"NOTE: %@", un);
|
|
|
|
}
|
|
|
|
|
|
|
|
- (NSDate *) nextDeliveryDateForNotification: (NSUserNotification *)un
|
|
|
|
{
|
2024-05-12 14:07:49 +00:00
|
|
|
NSDateComponents *repeatInterval;
|
|
|
|
NSDate *nextDeliveryDate;
|
|
|
|
NSCalendar *cal;
|
|
|
|
|
|
|
|
repeatInterval = [un deliveryRepeatInterval];
|
2014-01-31 12:08:13 +00:00
|
|
|
if (!repeatInterval)
|
|
|
|
return nil;
|
|
|
|
|
2024-05-12 14:07:49 +00:00
|
|
|
cal = [[repeatInterval calendar] copy];
|
2014-01-31 12:08:13 +00:00
|
|
|
if (!cal)
|
|
|
|
cal = [[NSCalendar currentCalendar] copy];
|
|
|
|
if ([repeatInterval timeZone])
|
|
|
|
[cal setTimeZone:[repeatInterval timeZone]];
|
|
|
|
if (![cal timeZone])
|
|
|
|
[cal setTimeZone:[NSTimeZone localTimeZone]];
|
|
|
|
|
2024-05-12 14:07:49 +00:00
|
|
|
nextDeliveryDate
|
2017-06-25 14:17:12 +00:00
|
|
|
= [cal dateByAddingComponents: repeatInterval
|
|
|
|
toDate: [un actualDeliveryDate]
|
|
|
|
options: 0];
|
2014-01-31 12:08:13 +00:00
|
|
|
RELEASE(cal);
|
|
|
|
return nextDeliveryDate;
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) deliverNotification: (NSUserNotification *)un
|
|
|
|
{
|
2024-05-12 14:07:49 +00:00
|
|
|
NSDate *actualDeliveryDate;
|
|
|
|
|
2014-01-30 14:58:20 +00:00
|
|
|
[self removeScheduledNotification: un];
|
|
|
|
[self _deliverNotification: un];
|
2014-01-30 14:23:18 +00:00
|
|
|
|
2024-05-12 14:07:49 +00:00
|
|
|
actualDeliveryDate = [un deliveryDate];
|
2014-01-30 14:58:20 +00:00
|
|
|
if (!actualDeliveryDate)
|
|
|
|
actualDeliveryDate = [NSDate date];
|
2017-06-25 14:17:12 +00:00
|
|
|
[un setActualDeliveryDate: actualDeliveryDate];
|
2014-01-30 14:58:20 +00:00
|
|
|
[_deliveredNotifications addObject: un];
|
2017-06-25 14:17:12 +00:00
|
|
|
[un setDeliveryDate: [self nextDeliveryDateForNotification: un]];
|
|
|
|
if ([un deliveryDate])
|
2014-01-31 12:08:13 +00:00
|
|
|
[self scheduleNotification: un];
|
2014-01-30 14:23:18 +00:00
|
|
|
|
2017-06-25 14:17:12 +00:00
|
|
|
if ([self delegate] && [[self delegate] respondsToSelector:
|
2014-01-30 14:58:20 +00:00
|
|
|
@selector(userNotificationCenter:didDeliverNotification:)])
|
|
|
|
{
|
2017-06-25 14:17:12 +00:00
|
|
|
[[self delegate] userNotificationCenter: self didDeliverNotification: un];
|
2014-01-30 14:58:20 +00:00
|
|
|
}
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
2014-01-31 12:08:13 +00:00
|
|
|
|
2014-01-30 14:23:18 +00:00
|
|
|
- (void) _removeDeliveredNotification: (NSUserNotification *)un
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeDeliveredNotification: (NSUserNotification *)un
|
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
[self _removeDeliveredNotification: un];
|
|
|
|
[_deliveredNotifications removeObject: un];
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) removeAllDeliveredNotifications
|
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
NSUInteger i, count = [_deliveredNotifications count];
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
NSUserNotification *un = [_deliveredNotifications objectAtIndex: i];
|
|
|
|
[self _removeDeliveredNotification:un];
|
|
|
|
}
|
|
|
|
[_deliveredNotifications removeAllObjects];
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (NSUserNotification *) deliveredNotificationWithUniqueId: (id)uniqueId
|
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
NSUInteger i, count = [_deliveredNotifications count];
|
|
|
|
|
|
|
|
for (i = 0; i < count; i++)
|
|
|
|
{
|
|
|
|
NSUserNotification *un = [_deliveredNotifications objectAtIndex: i];
|
|
|
|
if ([un->_uniqueId isEqual: uniqueId])
|
|
|
|
{
|
|
|
|
return un;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil;
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
@end
|
|
|
|
|
|
|
|
#endif /* __has_feature(objc_default_synthesize_properties) */
|