File: NSUserNotification.m

package info (click to toggle)
gnustep-base 1.31.1-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 26,580 kB
  • sloc: objc: 239,446; ansic: 36,519; cpp: 122; sh: 112; makefile: 100; xml: 32
file content (301 lines) | stat: -rw-r--r-- 8,418 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
/* 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
   Lesser 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., 31 Milk Street #960789 Boston, MA 02196 USA.
*/

#import	<GNUstepBase/GSVersionMacros.h>

#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/NSCalendar.h"
#import "Foundation/NSDate.h"
#import "Foundation/NSString.h"
#import "Foundation/NSTimeZone.h"

@interface NSUserNotification ()
@property (readwrite, assign) NSDate *actualDeliveryDate;
@property (readwrite, getter=isPresented) BOOL presented;
@property (readwrite, getter=isRemote) BOOL remote;
@property (readwrite) NSUserNotificationActivationType activationType;
@property (readwrite, assign) NSAttributedString *response;
@end

@implementation NSUserNotification

#if !defined(__clang__)
@synthesize title, subtitle, informativeText, actionButtonTitle, userInfo;
@synthesize deliveryDate, deliveryTimeZone, deliveryRepeatInterval;
@synthesize actualDeliveryDate, presented, remote, soundName;
@synthesize hasActionButton, activationType, otherButtonTitle;
#if OS_API_VERSION(MAC_OS_X_VERSION_10_9,GS_API_LATEST)
@synthesize identifier, contentImage, hasReplyButton;
@synthesize responsePlaceholder, response;
#endif
#endif

- (id) init
{
  if (nil != (self = [super init]))
    {
      [self setHasActionButton: YES];
    }
  return self;
}

- (void) dealloc
{
  RELEASE(_uniqueId);
  [super dealloc];
}

- (id) copyWithZone: (NSZone *)zone
{
  return NSCopyObject(self, 0, zone);
}

- (NSString *) description
{
  NSMutableString *d = [NSMutableString stringWithCapacity:80];

  [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: @" }"];
  return d;
}

@end

@interface NSUserNotificationCenter ()
@property (readwrite, assign) NSArray *deliveredNotifications;
@end

@interface NSUserNotificationCenter (Private)
+ (Class) defaultUserNotificationCenterClass;
+ (void) setDefaultUserNotificationCenter: (NSUserNotificationCenter *)unc;
- (NSUserNotification *) deliveredNotificationWithUniqueId: (id)uniqueId;
- (NSDate *) nextDeliveryDateForNotification: (NSUserNotification *)un;
@end

@implementation NSUserNotificationCenter
#if !defined(__clang__)
@synthesize delegate, scheduledNotifications, deliveredNotifications;
#endif

static NSUserNotificationCenter *defaultUserNotificationCenter = nil;

+ (Class) defaultUserNotificationCenterClass
{
  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;
}

+ (void) atExit
{
  DESTROY(defaultUserNotificationCenter);
}

+ (void) initialize
{
  if ([NSUserNotificationCenter class] == self)
    {
      Class uncClass = [self defaultUserNotificationCenterClass];
      defaultUserNotificationCenter = [[uncClass alloc] init];
      [self registerAtExit];
    }
}

+ (void) setDefaultUserNotificationCenter: (NSUserNotificationCenter *)unc
{
  ASSIGN(defaultUserNotificationCenter, unc);
}

+ (NSUserNotificationCenter *) defaultUserNotificationCenter
{
  return defaultUserNotificationCenter;
}


- (id) init
{
  self = [super init];
  if (self)
    {
      _scheduledNotifications = [[NSMutableArray alloc] init];
      _deliveredNotifications = [[NSMutableArray alloc] init];
    }
  return self;
}

- (void) dealloc
{
  [NSObject cancelPreviousPerformRequestsWithTarget: self];
  RELEASE(_scheduledNotifications);
  [self removeAllDeliveredNotifications];
  RELEASE(_deliveredNotifications);
  [super dealloc];
}

- (void) scheduleNotification: (NSUserNotification *)un
{
  NSTimeInterval	delay;

  if (![un deliveryDate])
    {
      [self deliverNotification: un];
      return;
    }
  [_scheduledNotifications addObject: un];
  delay = [[un deliveryDate] timeIntervalSinceNow];
  [self performSelector: @selector(deliverNotification:)
             withObject: un
             afterDelay: delay];
}

- (void) removeScheduledNotification: (NSUserNotification *)un
{
  [NSObject cancelPreviousPerformRequestsWithTarget: self
    selector: @selector(deliverNotification:)
    object: un];
  [_scheduledNotifications removeObject: un];
}

- (void) _deliverNotification: (NSUserNotification *)un
{
  [un setPresented: YES];
  NSLog(@"NOTE: %@", un);
}

- (NSDate *) nextDeliveryDateForNotification: (NSUserNotification *)un
{
  NSDateComponents	*repeatInterval;
  NSDate 		*nextDeliveryDate;
  NSCalendar		*cal;

  repeatInterval = [un deliveryRepeatInterval];
  if (!repeatInterval)
    return nil;

  cal = [[repeatInterval calendar] copy];
  if (!cal)
    cal = [[NSCalendar currentCalendar] copy];
  if ([repeatInterval timeZone])
    [cal setTimeZone:[repeatInterval timeZone]];
  if (![cal timeZone])
    [cal setTimeZone:[NSTimeZone localTimeZone]];

  nextDeliveryDate
    = [cal dateByAddingComponents: repeatInterval
                           toDate: [un actualDeliveryDate]
                          options: 0];
  RELEASE(cal);
  return nextDeliveryDate;
}

- (void) deliverNotification: (NSUserNotification *)un
{
  NSDate	*actualDeliveryDate;

  [self removeScheduledNotification: un];
  [self _deliverNotification: un];

  actualDeliveryDate = [un deliveryDate];
  if (!actualDeliveryDate)
    actualDeliveryDate = [NSDate date];
  [un setActualDeliveryDate: actualDeliveryDate];
  [_deliveredNotifications addObject: un];
  [un setDeliveryDate: [self nextDeliveryDateForNotification: un]];
  if ([un deliveryDate])
    [self scheduleNotification: un];

  if ([self delegate] && [[self delegate] respondsToSelector:
    @selector(userNotificationCenter:didDeliverNotification:)])
    {
      [[self delegate] userNotificationCenter: self didDeliverNotification: un];
    }
}


- (void) _removeDeliveredNotification: (NSUserNotification *)un
{
}

- (void) removeDeliveredNotification: (NSUserNotification *)un
{
  [self _removeDeliveredNotification: un];
  [_deliveredNotifications removeObject: un];
}

- (void) removeAllDeliveredNotifications
{
  NSUInteger i, count = [_deliveredNotifications count];

  for (i = 0; i < count; i++)
    {
      NSUserNotification *un = [_deliveredNotifications objectAtIndex: i];
      [self _removeDeliveredNotification:un];
    }
  [_deliveredNotifications removeAllObjects];
}

- (NSUserNotification *) deliveredNotificationWithUniqueId: (id)uniqueId
{
  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;
}

@end

#endif /* __has_feature(objc_default_synthesize_properties) */