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
|
|
|
|
Library General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU Lesser General Public
|
|
|
|
License along with this library; if not, write to the Free
|
|
|
|
Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
|
|
|
|
Boston, MA 02111 USA.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#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"
|
|
|
|
#import "Foundation/NSDate.h"
|
|
|
|
#import "Foundation/NSString.h"
|
|
|
|
|
|
|
|
@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]))
|
|
|
|
{
|
|
|
|
self.hasActionButton = YES;
|
|
|
|
}
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
- (NSString *)description
|
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
return [NSString stringWithFormat:@"<%s:%p> { title: \"%@\" "
|
|
|
|
"informativeText: \"%@\" "
|
|
|
|
"actionButtonTitle: \"%@\" }",
|
|
|
|
object_getClassName(self), self,
|
|
|
|
self.title, self.informativeText,
|
|
|
|
self.actionButtonTitle];
|
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;
|
|
|
|
@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-30 14:58:20 +00:00
|
|
|
if (nil != (self = self = [super init]))
|
|
|
|
{
|
|
|
|
_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
|
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
if (!un.deliveryDate)
|
|
|
|
{
|
|
|
|
[self deliverNotification: un];
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
[_scheduledNotifications addObject: un];
|
|
|
|
NSTimeInterval delay = [un.deliveryDate timeIntervalSinceNow];
|
|
|
|
[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
|
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
NSLog(@"%s -- needs implementation", __PRETTY_FUNCTION__);
|
2014-01-30 14:23:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
- (void) deliverNotification: (NSUserNotification *)un
|
|
|
|
{
|
2014-01-30 14:58:20 +00:00
|
|
|
[self removeScheduledNotification: un];
|
|
|
|
[self _deliverNotification: un];
|
2014-01-30 14:23:18 +00:00
|
|
|
|
2014-01-30 14:58:20 +00:00
|
|
|
NSDate *actualDeliveryDate = un.deliveryDate;
|
|
|
|
if (!actualDeliveryDate)
|
|
|
|
actualDeliveryDate = [NSDate date];
|
|
|
|
un.actualDeliveryDate = actualDeliveryDate;
|
|
|
|
[_deliveredNotifications addObject: un];
|
|
|
|
un.presented = YES;
|
2014-01-30 14:23:18 +00:00
|
|
|
|
2014-01-30 14:58:20 +00:00
|
|
|
if (self.delegate && [self.delegate respondsToSelector:
|
|
|
|
@selector(userNotificationCenter:didDeliverNotification:)])
|
|
|
|
{
|
|
|
|
[self.delegate userNotificationCenter: self didDeliverNotification: un];
|
|
|
|
}
|
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) */
|